2 * I/O monitor based on block queue trace data
4 * Copyright IBM Corp. 2008
6 * Author(s): Martin Peschke <mp3@de.ibm.com>
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.
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.
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
23 #include <sys/types.h>
45 struct blk_io_trace bit;
52 struct rb_node **node_ptr;
53 struct rb_node *parent;
58 struct blkiomon_stat stat;
74 static char blkiomon_version[] = "0.2";
77 static int interval = -1;
79 static struct trace *vacant_traces_list = NULL;
80 static int vacant_traces = 0;
81 static struct rb_root trace_tree = RB_ROOT;
83 #define TRACE_HASH_SIZE 128
84 struct trace *thash[TRACE_HASH_SIZE] = {};
86 static struct dstat *vacant_dstats_list = NULL;
87 static struct rb_root dstat_tree[2] = { RB_ROOT, RB_ROOT };
88 static struct dstat *dstat_list[2] = {};
89 static int dstat_curr = 0;
91 static struct output human, binary, debug;
93 static char *msg_q_name = NULL;
94 static int msg_q_id = -1, msg_q = -1;
95 static long msg_id = -1;
97 static pthread_t interval_thread;
98 static pthread_mutex_t dstat_mutex = PTHREAD_MUTEX_INITIALIZER;
100 int data_is_native = -1;
105 static long leftover = 0, driverdata = 0, match = 0, mismatch = 0, sequence = 0;
107 static void dump_bit(struct trace *t, const char *descr)
109 struct blk_io_trace *bit = &t->bit;
114 fprintf(debug.fp, "--- %s ---\n", descr);
115 fprintf(debug.fp, "magic %16d\n", bit->magic);
116 fprintf(debug.fp, "sequence %16d\n", bit->sequence);
117 fprintf(debug.fp, "time %16ld\n", (unsigned long)bit->time);
118 fprintf(debug.fp, "sector %16ld\n", (unsigned long)bit->sector);
119 fprintf(debug.fp, "bytes %16d\n", bit->bytes);
120 fprintf(debug.fp, "action %16x\n", bit->action);
121 fprintf(debug.fp, "pid %16d\n", bit->pid);
122 fprintf(debug.fp, "device %16d\n", bit->device);
123 fprintf(debug.fp, "cpu %16d\n", bit->cpu);
124 fprintf(debug.fp, "error %16d\n", bit->error);
125 fprintf(debug.fp, "pdu_len %16d\n", bit->pdu_len);
127 fprintf(debug.fp, "order %16ld\n", t->sequence);
130 static void dump_bits(struct trace *t1, struct trace *t2, const char *descr)
132 struct blk_io_trace *bit1 = &t1->bit;
133 struct blk_io_trace *bit2 = &t2->bit;
138 fprintf(debug.fp, "--- %s ---\n", descr);
139 fprintf(debug.fp, "magic %16d %16d\n", bit1->magic, bit2->magic);
140 fprintf(debug.fp, "sequence %16d %16d\n",
141 bit1->sequence, bit2->sequence);
142 fprintf(debug.fp, "time %16ld %16ld\n",
143 (unsigned long)bit1->time, (unsigned long)bit2->time);
144 fprintf(debug.fp, "sector %16ld %16ld\n",
145 (unsigned long)bit1->sector, (unsigned long)bit2->sector);
146 fprintf(debug.fp, "bytes %16d %16d\n", bit1->bytes, bit2->bytes);
147 fprintf(debug.fp, "action %16x %16x\n", bit1->action, bit2->action);
148 fprintf(debug.fp, "pid %16d %16d\n", bit1->pid, bit2->pid);
149 fprintf(debug.fp, "device %16d %16d\n", bit1->device, bit2->device);
150 fprintf(debug.fp, "cpu %16d %16d\n", bit1->cpu, bit2->cpu);
151 fprintf(debug.fp, "error %16d %16d\n", bit1->error, bit2->error);
152 fprintf(debug.fp, "pdu_len %16d %16d\n", bit1->pdu_len, bit2->pdu_len);
154 fprintf(debug.fp, "order %16ld %16ld\n", t1->sequence, t2->sequence);
157 static struct dstat *blkiomon_alloc_dstat(void)
161 if (vacant_dstats_list) {
162 dstat = vacant_dstats_list;
163 vacant_dstats_list = dstat->next;
165 dstat = malloc(sizeof(*dstat));
167 perror("blkiomon: could not allocate device statistic");
171 memset(dstat, 0, sizeof(*dstat));
175 static struct dstat *blkiomon_find_dstat(struct rb_search *search, __u32 device)
177 struct rb_node **p = &(dstat_tree[dstat_curr].rb_node);
178 struct rb_node *parent = NULL;
184 dstat = rb_entry(parent, struct dstat, node);
186 if (dstat->msg.stat.device < device)
188 else if (dstat->msg.stat.device > device)
193 search->node_ptr = p;
194 search->parent = parent;
198 static struct dstat *blkiomon_get_dstat(__u32 device)
201 struct rb_search search;
203 pthread_mutex_lock(&dstat_mutex);
205 dstat = blkiomon_find_dstat(&search, device);
209 dstat = blkiomon_alloc_dstat();
213 dstat->msg.stat.device = device;
214 dstat->msg.stat.size_mm.min = -1ULL;
215 dstat->msg.stat.d2c_mm.min = -1ULL;
217 rb_link_node(&dstat->node, search.parent, search.node_ptr);
218 rb_insert_color(&dstat->node, &dstat_tree[dstat_curr]);
220 dstat->next = dstat_list[dstat_curr];
221 dstat_list[dstat_curr] = dstat;
224 pthread_mutex_unlock(&dstat_mutex);
228 static int blkiomon_output_msg_q(struct dstat *dstat)
233 dstat->msg.mtype = msg_id;
234 return msgsnd(msg_q, &dstat->msg, sizeof(struct blkiomon_stat), 0);
237 static int blkiomon_output_binary(struct dstat *dstat)
239 struct blkiomon_stat *p = &dstat->msg.stat;
244 if (fwrite(p, sizeof(*p), 1, binary.fp) != 1)
246 if (binary.pipe && fflush(binary.fp))
251 fprintf(stderr, "blkiomon: could not write to %s\n", binary.fn);
257 static struct dstat *blkiomon_output(struct dstat *head, struct timespec *ts)
259 struct dstat *dstat, *tail = NULL;
261 for (dstat = head; dstat; dstat = dstat->next) {
262 dstat->msg.stat.time = ts->tv_sec;
263 blkiomon_stat_print(human.fp, &dstat->msg.stat);
264 blkiomon_stat_to_be(&dstat->msg.stat);
265 blkiomon_output_binary(dstat);
266 blkiomon_output_msg_q(dstat);
272 static void *blkiomon_interval(void *data)
274 struct timespec wake, r;
275 struct dstat *head, *tail;
278 clock_gettime(CLOCK_REALTIME, &wake);
281 wake.tv_sec += interval;
282 if (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &wake, &r)) {
283 perror("blkiomon: interrupted sleep");
287 /* grab tree and make data gatherer build up another tree */
288 pthread_mutex_lock(&dstat_mutex);
289 finished = dstat_curr;
290 dstat_curr = dstat_curr ? 0 : 1;
291 pthread_mutex_unlock(&dstat_mutex);
293 head = dstat_list[finished];
296 dstat_list[finished] = NULL;
297 dstat_tree[finished] = RB_ROOT;
298 tail = blkiomon_output(head, &wake);
300 pthread_mutex_lock(&dstat_mutex);
301 tail->next = vacant_dstats_list;
302 vacant_dstats_list = head;
303 pthread_mutex_unlock(&dstat_mutex);
308 #define BLK_DATADIR(a) (((a) >> BLK_TC_SHIFT) & (BLK_TC_READ | BLK_TC_WRITE))
310 static int blkiomon_account(struct blk_io_trace *bit_d,
311 struct blk_io_trace *bit_c)
314 struct blkiomon_stat *p;
315 __u64 d2c = (bit_c->time - bit_d->time) / 1000; /* ns -> us */
316 __u32 size = bit_d->bytes;
318 dstat = blkiomon_get_dstat(bit_d->device);
321 p = &dstat->msg.stat;
323 if (BLK_DATADIR(bit_c->action) & BLK_TC_READ)
325 else if (BLK_DATADIR(bit_c->action) & BLK_TC_WRITE)
330 histlog2_account(p->size_hist, size, &size_hist);
331 histlog2_account(p->d2c_hist, d2c, &d2c_hist);
332 minmax_account(&p->size_mm, size);
333 minmax_account(&p->d2c_mm, d2c);
337 static struct trace *blkiomon_alloc_trace(void)
339 struct trace *t = vacant_traces_list;
341 vacant_traces_list = t->next;
344 t = malloc(sizeof(*t));
345 memset(t, 0, sizeof(*t));
349 static void blkiomon_free_trace(struct trace *t)
351 if (vacant_traces < 256) {
352 t->next = vacant_traces_list;
353 vacant_traces_list = t;
359 static int action(int a)
361 int bits = BLK_TC_WRITE | BLK_TC_READ | BLK_TC_FS | BLK_TC_PC;
362 return a & (BLK_TC_ACT(bits));
365 static void blkiomon_store_trace(struct trace *t)
367 int i = t->bit.sector % TRACE_HASH_SIZE;
373 static struct trace *blkiomon_fetch_trace(struct blk_io_trace *bit)
375 int i = bit->sector % TRACE_HASH_SIZE;
376 struct trace *t, *prev = NULL;
378 for (t = thash[i]; t; t = t->next) {
379 if (t->bit.device == bit->device &&
380 t->bit.sector == bit->sector &&
381 action(t->bit.action) == action(bit->action)) {
383 prev->next = t->next;
393 static struct trace *blkiomon_do_trace(struct trace *t)
395 struct trace *t_stored, *t_old, *t_young;
397 /* store trace if there is no match yet */
398 t_stored = blkiomon_fetch_trace(&t->bit);
400 blkiomon_store_trace(t);
401 return blkiomon_alloc_trace();
404 /* figure out older trace and younger trace */
405 if (t_stored->bit.time < t->bit.time) {
413 /* we need an older D trace and a younger C trace */
414 if (t_old->bit.action & BLK_TC_ACT(BLK_TC_ISSUE) &&
415 t_young->bit.action & BLK_TC_ACT(BLK_TC_COMPLETE)) {
416 /* matching D and C traces - update statistics */
418 blkiomon_account(&t_old->bit, &t_young->bit);
419 blkiomon_free_trace(t_stored);
423 /* no matching D and C traces - keep more recent trace */
424 dump_bits(t_old, t_young, "mismatch");
426 blkiomon_store_trace(t_young);
430 static int blkiomon_do_fifo(void)
433 struct blk_io_trace *bit;
434 void *pdu_buf = NULL;
436 t = blkiomon_alloc_trace();
442 if (fread(bit, sizeof(*bit), 1, ifp) != 1) {
445 "blkiomon: could not read trace");
450 perror("blkiomon: error while reading trace");
454 if (data_is_native == -1 && check_data_endianness(bit->magic))
459 if (verify_trace(bit)) {
460 perror("blkiomon: bad trace");
464 /* read additional trace payload */
466 pdu_buf = realloc(pdu_buf, bit->pdu_len);
467 if (fread(pdu_buf, bit->pdu_len, 1, ifp) != 1) {
469 perror("blkiomon: could not read payload");
474 t->sequence = sequence++;
476 if (!(bit->action & BLK_TC_ACT(BLK_TC_ISSUE | BLK_TC_COMPLETE)))
479 /* try to find matching trace and update statistics */
480 t = blkiomon_do_trace(t);
484 /* t and bit will be recycled for next incoming trace */
486 blkiomon_free_trace(t);
491 static int blkiomon_open_output(struct output *out)
498 if (!strcmp(out->fn, "-")) {
499 out->fp = fdopen(STDOUT_FILENO, "w");
504 out->fp = fopen(out->fn, "w");
506 vbuf_size = 128 * 1024;
511 out->buf = malloc(128 * 1024);
512 if (setvbuf(out->fp, out->buf, mode, vbuf_size))
517 fprintf(stderr, "blkiomon: could not write to %s\n", out->fn);
523 static int blkiomon_open_msg_q(void)
529 if (!msg_q_id || msg_id <= 0)
531 key = ftok(msg_q_name, msg_q_id);
535 msg_q = msgget(key, S_IRWXU);
539 return (msg_q >= 0 ? 0 : -1);
542 static void blkiomon_debug(void)
550 for (n = rb_first(&trace_tree); n; n = rb_next(n)) {
551 t = rb_entry(n, struct trace, node);
552 dump_bit(t, "leftover");
555 fprintf(debug.fp, "%ld leftover, %ld match, %ld mismatch, "
556 "%ld driverdata, %ld overall\n",
557 leftover, match, mismatch, driverdata, sequence);
560 #define S_OPTS "b:D:h:I:Q:q:m:V"
562 static char usage_str[] = "\n\nblkiomon " \
563 "-I <interval> | --interval=<interval>\n" \
564 "[ -h <file> | --human-readable=<file> ]\n" \
565 "[ -b <file> | --binary=<file> ]\n" \
566 "[ -D <file> | --debug=<file> ]\n" \
567 "[ -Q <path name> | --msg-queue-name=<path name>]\n" \
568 "[ -q <msg queue id> | --msg-queue-id=<msg queue id>]\n" \
569 "[ -m <msg id> | --msg-id=<msg id>]\n" \
570 "[ -V | --version ]\n\n" \
571 "\t-I Sample interval.\n" \
572 "\t-h Human-readable output file.\n" \
573 "\t-b Binary output file.\n" \
574 "\t-D Output file for debugging data.\n" \
575 "\t-Qqm Output to message queue using given ID for messages.\n" \
576 "\t-V Print program version.\n\n";
578 static struct option l_opts[] = {
580 .name = "human-readable",
581 .has_arg = required_argument,
587 .has_arg = required_argument,
593 .has_arg = required_argument,
599 .has_arg = required_argument,
605 .has_arg = required_argument,
610 .name = "msg-queue-id",
611 .has_arg = required_argument,
617 .has_arg = required_argument,
623 .has_arg = no_argument,
632 static void blkiomon_signal(int signal)
634 fprintf(stderr, "blkiomon: terminated by signal\n");
638 int main(int argc, char *argv[])
642 signal(SIGALRM, blkiomon_signal);
643 signal(SIGINT, blkiomon_signal);
644 signal(SIGTERM, blkiomon_signal);
645 signal(SIGQUIT, blkiomon_signal);
647 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
659 interval = atoi(optarg);
665 msg_q_id = atoi(optarg);
668 msg_id = atoi(optarg);
671 printf("%s version %s\n", argv[0], blkiomon_version);
674 fprintf(stderr, "Usage: %s", usage_str);
680 fprintf(stderr, "Usage: %s", usage_str);
684 ifp = fdopen(STDIN_FILENO, "r");
686 perror("blkiomon: could not open stdin for reading");
690 if (blkiomon_open_output(&human))
692 if (blkiomon_open_output(&binary))
694 if (blkiomon_open_output(&debug))
696 if (blkiomon_open_msg_q())
699 if (pthread_create(&interval_thread, NULL, blkiomon_interval, NULL)) {
700 perror("blkiomon: could not create thread");