blkiomon: separate statistics for read and write requests
[blktrace.git] / blkiomon.h
1 /*
2  * I/O monitor based on block queue trace data
3  *
4  * Copyright IBM Corp. 2008
5  *
6  * Author(s): Martin Peschke <mp3@de.ibm.com>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef BLKIOMON_H
24 #define BLKIOMON_H
25
26 #include <string.h>
27
28 #include "stats.h"
29 #include "blktrace.h"
30
31 #define BLKIOMON_SIZE_BUCKETS 16
32 #define BLKIOMON_D2C_BUCKETS 25
33 struct blkiomon_stat {
34         __u64 time;
35         __u32 size_hist[BLKIOMON_SIZE_BUCKETS];
36         __u32 d2c_hist[BLKIOMON_D2C_BUCKETS];
37         struct minmax size_r;
38         struct minmax size_w;
39         struct minmax d2c_r;
40         struct minmax d2c_w;
41         __u64 bidir;
42         __u32 device;
43 } __attribute__ ((packed));
44
45 static struct histlog2 size_hist = {
46         .first = 0,
47         .delta = 1024,
48         .num = BLKIOMON_SIZE_BUCKETS
49 };
50
51 static struct histlog2 d2c_hist = {
52         .first = 0,
53         .delta = 8,
54         .num = BLKIOMON_D2C_BUCKETS
55 };
56
57 static inline void blkiomon_stat_init(struct blkiomon_stat *bstat)
58 {
59         memset(bstat, 0, sizeof(*bstat));
60         minmax_init(&bstat->size_r);
61         minmax_init(&bstat->size_w);
62         minmax_init(&bstat->d2c_r);
63         minmax_init(&bstat->d2c_w);
64 }
65
66 static inline void blkiomon_stat_to_be(struct blkiomon_stat *bstat)
67 {
68         histlog2_to_be(bstat->size_hist, &size_hist);
69         histlog2_to_be(bstat->d2c_hist, &d2c_hist);
70         minmax_to_be(&bstat->size_r);
71         minmax_to_be(&bstat->size_w);
72         minmax_to_be(&bstat->d2c_r);
73         minmax_to_be(&bstat->d2c_w);
74         bstat->bidir = cpu_to_be64(bstat->bidir);
75         bstat->time = cpu_to_be64(bstat->time);
76         bstat->device = cpu_to_be32(bstat->device);
77 }
78
79 static inline void blkiomon_stat_merge(struct blkiomon_stat *dst,
80                                        struct blkiomon_stat *src)
81 {
82         histlog2_merge(&size_hist, dst->size_hist, src->size_hist);
83         histlog2_merge(&d2c_hist, dst->d2c_hist, src->d2c_hist);
84         minmax_merge(&dst->size_r, &src->size_r);
85         minmax_merge(&dst->size_w, &src->size_w);
86         minmax_merge(&dst->d2c_r, &src->d2c_r);
87         minmax_merge(&dst->d2c_w, &src->d2c_w);
88         dst->bidir += src->bidir;
89 }
90
91 static inline void blkiomon_stat_print(FILE *fp, struct blkiomon_stat *p)
92 {
93         if (!fp)
94                 return;
95
96         fprintf(fp, "\ntime: %s", ctime((void *)&p->time));
97         fprintf(fp, "device: %d,%d\n", MAJOR(p->device), MINOR(p->device));
98         minmax_print(fp, "sizes read (bytes)", &p->size_r);
99         minmax_print(fp, "sizes write (bytes)", &p->size_w);
100         minmax_print(fp, "d2c read (usec)", &p->d2c_r);
101         minmax_print(fp, "d2c write (usec)", &p->d2c_w);
102         histlog2_print(fp, "sizes histogram (bytes)", p->size_hist, &size_hist);
103         histlog2_print(fp, "d2c histogram (usec)", p->d2c_hist, &d2c_hist);
104         fprintf(fp, "bidirectional requests: %ld\n", (unsigned long)p->bidir);
105 }
106
107 #endif