2b1f7b29cc1bd0e05781327163fa6806c518c8dc
[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_mm;
38         struct minmax d2c_mm;
39         __u64 read;
40         __u64 write;
41         __u64 bidir;
42         __u32 device;
43 };
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_mm);
61         minmax_init(&bstat->d2c_mm);
62 }
63
64 static inline void blkiomon_stat_to_be(struct blkiomon_stat *bstat)
65 {
66         histlog2_to_be(bstat->size_hist, &size_hist);
67         histlog2_to_be(bstat->d2c_hist, &d2c_hist);
68         minmax_to_be(&bstat->size_mm);
69         minmax_to_be(&bstat->d2c_mm);
70         bstat->read = cpu_to_be64(bstat->read);
71         bstat->write = cpu_to_be64(bstat->write);
72         bstat->bidir = cpu_to_be64(bstat->bidir);
73         bstat->time = cpu_to_be64(bstat->time);
74         bstat->device = cpu_to_be32(bstat->device);
75 }
76
77 static inline void blkiomon_stat_merge(struct blkiomon_stat *dst,
78                                        struct blkiomon_stat *src)
79 {
80         histlog2_merge(&size_hist, dst->size_hist, src->size_hist);
81         histlog2_merge(&d2c_hist, dst->d2c_hist, src->d2c_hist);
82         minmax_merge(&dst->size_mm, &src->size_mm);
83         minmax_merge(&dst->d2c_mm, &src->d2c_mm);
84         dst->read += src->read;
85         dst->write += src->write;
86         dst->bidir += src->bidir;
87 }
88
89 static inline void blkiomon_stat_print(FILE *fp, struct blkiomon_stat *p)
90 {
91         if (!fp)
92                 return;
93
94         fprintf(fp, "\ntime: %s", ctime((void *)&p->time));
95         fprintf(fp, "device: %d,%d\n", MAJOR(p->device), MINOR(p->device));
96         fprintf(fp, "requests: read %ld, write %ld, bidir: %ld\n",
97                 (unsigned long)p->read, (unsigned long)p->write,
98                 (unsigned long)p->bidir);
99         minmax_print(fp, "sizes", &p->size_mm);
100         minmax_print(fp, "d2c", &p->d2c_mm);
101         histlog2_print(fp, "sizes histogram (in kB)", p->size_hist, &size_hist);
102         histlog2_print(fp, "d2c histogram (in usec)", p->d2c_hist, &d2c_hist);
103 }
104
105 #endif