b53dfd947ca823ebf349a68eb3a743e4c52e7ab3
[blktrace.git] / blkiomon.c
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 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <getopt.h>
32 #include <errno.h>
33 #include <locale.h>
34 #include <libgen.h>
35 #include <sys/msg.h>
36 #include <pthread.h>
37 #include <time.h>
38
39 #include "blktrace.h"
40 #include "rbtree.h"
41 #include "jhash.h"
42 #include "blkiomon.h"
43
44 struct trace {
45         struct blk_io_trace bit;
46         struct rb_node node;
47         struct trace *next;
48         long sequence;
49 };
50
51 struct rb_search {
52         struct rb_node **node_ptr;
53         struct rb_node *parent;
54 };
55
56 struct dstat_msg {
57         long mtype;
58         struct blkiomon_stat stat;
59 };
60
61 struct dstat {
62         struct dstat_msg msg;
63         struct rb_node node;
64         struct dstat *next;
65 };
66
67 struct output {
68         char *fn;
69         FILE *fp;
70         char *buf;
71         int pipe;
72 };
73
74 static char blkiomon_version[] = "0.2";
75
76 static FILE *ifp;
77 static int interval = -1;
78
79 static struct trace *vacant_traces_list = NULL;
80 static int vacant_traces = 0;
81
82 #define TRACE_HASH_SIZE 128
83 struct trace *thash[TRACE_HASH_SIZE] = {};
84
85 static struct dstat *vacant_dstats_list = NULL;
86 static struct rb_root dstat_tree[2] = { RB_ROOT, RB_ROOT };
87 static struct dstat *dstat_list[2] = {};
88 static int dstat_curr = 0;
89
90 static struct output drvdata, human, binary, debug;
91
92 static char *msg_q_name = NULL;
93 static int msg_q_id = -1, msg_q = -1;
94 static long msg_id = -1;
95
96 static pthread_t interval_thread;
97 static pthread_mutex_t dstat_mutex = PTHREAD_MUTEX_INITIALIZER;
98
99 int data_is_native = -1;
100
101 static int up = 1;
102
103 /* debugging */
104 static long leftover = 0, driverdata = 0, match = 0, mismatch = 0, sequence = 0;
105
106 static void dump_bit(struct trace *t, const char *descr)
107 {
108         struct blk_io_trace *bit = &t->bit;
109
110         if (!debug.fn)
111                 return;
112
113         fprintf(debug.fp, "--- %s ---\n", descr);
114         fprintf(debug.fp, "magic    %16d\n", bit->magic);
115         fprintf(debug.fp, "sequence %16d\n", bit->sequence);
116         fprintf(debug.fp, "time     %16ld\n", (unsigned long)bit->time);
117         fprintf(debug.fp, "sector   %16ld\n", (unsigned long)bit->sector);
118         fprintf(debug.fp, "bytes    %16d\n", bit->bytes);
119         fprintf(debug.fp, "action   %16x\n", bit->action);
120         fprintf(debug.fp, "pid      %16d\n", bit->pid);
121         fprintf(debug.fp, "device   %16d\n", bit->device);
122         fprintf(debug.fp, "cpu      %16d\n", bit->cpu);
123         fprintf(debug.fp, "error    %16d\n", bit->error);
124         fprintf(debug.fp, "pdu_len  %16d\n", bit->pdu_len);
125
126         fprintf(debug.fp, "order    %16ld\n", t->sequence);
127 }
128
129 static void dump_bits(struct trace *t1, struct trace *t2, const char *descr)
130 {
131         struct blk_io_trace *bit1 = &t1->bit;
132         struct blk_io_trace *bit2 = &t2->bit;
133
134         if (!debug.fn)
135                 return;
136
137         fprintf(debug.fp, "--- %s ---\n", descr);
138         fprintf(debug.fp, "magic    %16d %16d\n", bit1->magic, bit2->magic);
139         fprintf(debug.fp, "sequence %16d %16d\n",
140                 bit1->sequence, bit2->sequence);
141         fprintf(debug.fp, "time     %16ld %16ld\n",
142                 (unsigned long)bit1->time, (unsigned long)bit2->time);
143         fprintf(debug.fp, "sector   %16ld %16ld\n",
144                 (unsigned long)bit1->sector, (unsigned long)bit2->sector);
145         fprintf(debug.fp, "bytes    %16d %16d\n", bit1->bytes, bit2->bytes);
146         fprintf(debug.fp, "action   %16x %16x\n", bit1->action, bit2->action);
147         fprintf(debug.fp, "pid      %16d %16d\n", bit1->pid, bit2->pid);
148         fprintf(debug.fp, "device   %16d %16d\n", bit1->device, bit2->device);
149         fprintf(debug.fp, "cpu      %16d %16d\n", bit1->cpu, bit2->cpu);
150         fprintf(debug.fp, "error    %16d %16d\n", bit1->error, bit2->error);
151         fprintf(debug.fp, "pdu_len  %16d %16d\n", bit1->pdu_len, bit2->pdu_len);
152
153         fprintf(debug.fp, "order    %16ld %16ld\n", t1->sequence, t2->sequence);
154 }
155
156 static struct dstat *blkiomon_alloc_dstat(void)
157 {
158         struct dstat *dstat;
159
160         if (vacant_dstats_list) {
161                 dstat = vacant_dstats_list;
162                 vacant_dstats_list = dstat->next;
163         } else
164                 dstat = malloc(sizeof(*dstat));
165         if (!dstat) {
166                 fprintf(stderr,
167                         "blkiomon: could not allocate device statistic");
168                 return NULL;
169         }
170
171         memset(dstat, 0, sizeof(*dstat));
172         return dstat;
173 }
174
175 static struct dstat *blkiomon_find_dstat(struct rb_search *search, __u32 device)
176 {
177         struct rb_node **p = &(dstat_tree[dstat_curr].rb_node);
178         struct rb_node *parent = NULL;
179         struct dstat *dstat;
180
181         while (*p) {
182                 parent = *p;
183
184                 dstat = rb_entry(parent, struct dstat, node);
185
186                 if (dstat->msg.stat.device < device)
187                         p = &(*p)->rb_left;
188                 else if (dstat->msg.stat.device > device)
189                         p = &(*p)->rb_right;
190                 else
191                         return dstat;
192         }
193         search->node_ptr = p;
194         search->parent = parent;
195         return NULL;
196 }
197
198 static struct dstat *blkiomon_get_dstat(__u32 device)
199 {
200         struct dstat *dstat;
201         struct rb_search search;
202
203         pthread_mutex_lock(&dstat_mutex);
204
205         dstat = blkiomon_find_dstat(&search, device);
206         if (dstat)
207                 goto out;
208
209         dstat = blkiomon_alloc_dstat();
210         if (!dstat)
211                 goto out;
212
213         dstat->msg.stat.device = device;
214         dstat->msg.stat.size_r.min = -1ULL;
215         dstat->msg.stat.size_w.min = -1ULL;
216         dstat->msg.stat.d2c_r.min = -1ULL;
217         dstat->msg.stat.d2c_w.min = -1ULL;
218
219         rb_link_node(&dstat->node, search.parent, search.node_ptr);
220         rb_insert_color(&dstat->node, &dstat_tree[dstat_curr]);
221
222         dstat->next = dstat_list[dstat_curr];
223         dstat_list[dstat_curr] = dstat;
224
225 out:
226         pthread_mutex_unlock(&dstat_mutex);
227         return dstat;
228 }
229
230 static int blkiomon_output_msg_q(struct dstat *dstat)
231 {
232         if (!msg_q_name)
233                 return 0;
234
235         dstat->msg.mtype = msg_id;
236         return msgsnd(msg_q, &dstat->msg, sizeof(struct blkiomon_stat), 0);
237 }
238
239 static int blkiomon_output_binary(struct dstat *dstat)
240 {
241         struct blkiomon_stat *p = &dstat->msg.stat;
242
243         if (!binary.fn)
244                 return 0;
245
246         if (fwrite(p, sizeof(*p), 1, binary.fp) != 1)
247                 goto failed;
248         if (binary.pipe && fflush(binary.fp))
249                 goto failed;
250         return 0;
251
252 failed:
253         fprintf(stderr, "blkiomon: could not write to %s\n", binary.fn);
254         fclose(binary.fp);
255         binary.fn = NULL;
256         return 1;
257 }
258
259 static struct dstat *blkiomon_output(struct dstat *head, struct timespec *ts)
260 {
261         struct dstat *dstat, *tail = NULL;
262
263         for (dstat = head; dstat; dstat = dstat->next) {
264                 dstat->msg.stat.time = ts->tv_sec;
265                 blkiomon_stat_print(human.fp, &dstat->msg.stat);
266                 blkiomon_stat_to_be(&dstat->msg.stat);
267                 blkiomon_output_binary(dstat);
268                 blkiomon_output_msg_q(dstat);
269                 tail = dstat;
270         }
271         return tail;
272 }
273
274 static void *blkiomon_interval(void *data)
275 {
276         struct timespec wake, r;
277         struct dstat *head, *tail;
278         int finished;
279
280         clock_gettime(CLOCK_REALTIME, &wake);
281
282         while (1) {
283                 wake.tv_sec += interval;
284                 if (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &wake, &r)) {
285                         fprintf(stderr, "blkiomon: interrupted sleep");
286                         continue;
287                 }
288
289                 /* grab tree and make data gatherer build up another tree */
290                 pthread_mutex_lock(&dstat_mutex);
291                 finished = dstat_curr;
292                 dstat_curr = dstat_curr ? 0 : 1;
293                 pthread_mutex_unlock(&dstat_mutex);
294
295                 head = dstat_list[finished];
296                 if (!head)
297                         continue;
298                 dstat_list[finished] = NULL;
299                 dstat_tree[finished] = RB_ROOT;
300                 tail = blkiomon_output(head, &wake);
301
302                 pthread_mutex_lock(&dstat_mutex);
303                 tail->next = vacant_dstats_list;
304                 vacant_dstats_list = head;
305                 pthread_mutex_unlock(&dstat_mutex);
306         }
307         return data;
308 }
309
310 #define BLK_DATADIR(a) (((a) >> BLK_TC_SHIFT) & (BLK_TC_READ | BLK_TC_WRITE))
311
312 static int blkiomon_account(struct blk_io_trace *bit_d,
313                             struct blk_io_trace *bit_c)
314 {
315         struct dstat *dstat;
316         struct blkiomon_stat *p;
317         __u64 d2c = (bit_c->time - bit_d->time) / 1000; /* ns -> us */
318         __u32 size = bit_d->bytes;
319
320         dstat = blkiomon_get_dstat(bit_d->device);
321         if (!dstat)
322                 return 1;
323         p = &dstat->msg.stat;
324
325         if (BLK_DATADIR(bit_c->action) & BLK_TC_READ) {
326                 minmax_account(&p->size_r, size);
327                 minmax_account(&p->d2c_r, d2c);
328         } else if (BLK_DATADIR(bit_c->action) & BLK_TC_WRITE) {
329                 minmax_account(&p->size_w, size);
330                 minmax_account(&p->d2c_w, d2c);
331         } else
332                 p->bidir++;
333
334         histlog2_account(p->size_hist, size, &size_hist);
335         histlog2_account(p->d2c_hist, d2c, &d2c_hist);
336         return 0;
337 }
338
339 static struct trace *blkiomon_alloc_trace(void)
340 {
341         struct trace *t = vacant_traces_list;
342         if (t) {
343                 vacant_traces_list = t->next;
344                 vacant_traces--;
345         } else
346                 t = malloc(sizeof(*t));
347         memset(t, 0, sizeof(*t));
348         return t;
349 }
350
351 static void blkiomon_free_trace(struct trace *t)
352 {
353         if (vacant_traces < 256) {
354                 t->next = vacant_traces_list;
355                 vacant_traces_list = t;
356                 vacant_traces++;
357         } else
358                 free(t);
359 }
360
361 static int action(int a)
362 {
363         int bits = BLK_TC_WRITE | BLK_TC_READ | BLK_TC_FS | BLK_TC_PC;
364         return a & (BLK_TC_ACT(bits));
365 }
366
367 static void blkiomon_store_trace(struct trace *t)
368 {
369         int i = t->bit.sector % TRACE_HASH_SIZE;
370
371         t->next = thash[i];
372         thash[i] = t;
373 }
374
375 static struct trace *blkiomon_fetch_trace(struct blk_io_trace *bit)
376 {
377         int i = bit->sector % TRACE_HASH_SIZE;
378         struct trace *t, *prev = NULL;
379
380         for (t = thash[i]; t; t = t->next) {
381                 if (t->bit.device == bit->device &&
382                     t->bit.sector == bit->sector &&
383                     action(t->bit.action) == action(bit->action)) {
384                         if (prev)
385                                 prev->next = t->next;
386                         else
387                                 thash[i] = t->next;
388                         return t;
389                 }
390                 prev = t;
391         }
392         return NULL;
393 }
394
395 static struct trace *blkiomon_do_trace(struct trace *t)
396 {
397         struct trace *t_stored, *t_old, *t_young;
398
399         /* store trace if there is no match yet */
400         t_stored = blkiomon_fetch_trace(&t->bit);
401         if (!t_stored) {
402                 blkiomon_store_trace(t);
403                 return blkiomon_alloc_trace();
404         }
405
406         /* figure out older trace and younger trace */
407         if (t_stored->bit.time < t->bit.time) {
408                 t_old = t_stored;
409                 t_young = t;
410         } else {
411                 t_old = t;
412                 t_young = t_stored;
413         }
414
415         /* we need an older D trace and a younger C trace */
416         if (t_old->bit.action & BLK_TC_ACT(BLK_TC_ISSUE) &&
417             t_young->bit.action & BLK_TC_ACT(BLK_TC_COMPLETE)) {
418                 /* matching D and C traces - update statistics */
419                 match++;
420                 blkiomon_account(&t_old->bit, &t_young->bit);
421                 blkiomon_free_trace(t_stored);
422                 return t;
423         }
424
425         /* no matching D and C traces - keep more recent trace */
426         dump_bits(t_old, t_young, "mismatch");
427         mismatch++;
428         blkiomon_store_trace(t_young);
429         return t_old;
430 }
431
432 static int blkiomon_dump_drvdata(struct blk_io_trace *bit, void *pdu_buf)
433 {
434         if (!drvdata.fn)
435                 return 0;
436
437         if (fwrite(bit, sizeof(*bit), 1, drvdata.fp) != 1)
438                 goto failed;
439         if (fwrite(pdu_buf, bit->pdu_len, 1, drvdata.fp) != 1)
440                 goto failed;
441         if (drvdata.pipe && fflush(drvdata.fp))
442                 goto failed;
443         return 0;
444
445 failed:
446         fprintf(stderr, "blkiomon: could not write to %s\n", drvdata.fn);
447         fclose(drvdata.fp);
448         drvdata.fn = NULL;
449         return 1;
450 }
451
452 static int blkiomon_do_fifo(void)
453 {
454         struct trace *t;
455         struct blk_io_trace *bit;
456         void *pdu_buf = NULL;
457
458         t = blkiomon_alloc_trace();
459         if (!t)
460                 return 1;
461         bit = &t->bit;
462
463         while (up) {
464                 if (fread(bit, sizeof(*bit), 1, ifp) != 1) {
465                         if (!feof(ifp))
466                                 fprintf(stderr,
467                                         "blkiomon: could not read trace");
468                         break;
469                 }
470                 if (ferror(ifp)) {
471                         clearerr(ifp);
472                         fprintf(stderr, "blkiomon: error while reading trace");
473                         break;
474                 }
475
476                 if (data_is_native == -1 && check_data_endianness(bit->magic)) {
477                         fprintf(stderr, "blkiomon: endianess problem\n");
478                         break;
479                 }
480
481                 /* endianess */
482                 trace_to_cpu(bit);
483                 if (verify_trace(bit)) {
484                         fprintf(stderr, "blkiomon: bad trace\n");
485                         break;
486                 }
487
488                 /* read additional trace payload */
489                 if (bit->pdu_len) {
490                         pdu_buf = realloc(pdu_buf, bit->pdu_len);
491                         if (fread(pdu_buf, bit->pdu_len, 1, ifp) != 1) {
492                                 clearerr(ifp);
493                                 fprintf(stderr, "blkiomon: could not read payload\n");
494                                 break;
495                         }
496                 }
497
498                 t->sequence = sequence++;
499
500                 /* forward low-level device driver trace to other tool */
501                 if (bit->action & BLK_TC_ACT(BLK_TC_DRV_DATA)) {
502                         driverdata++;
503                         if (blkiomon_dump_drvdata(bit, pdu_buf)) {
504                                 fprintf(stderr, "blkiomon: could not send trace\n");
505                                 break;
506                         }
507                         continue;
508                 }
509
510                 if (!(bit->action & BLK_TC_ACT(BLK_TC_ISSUE | BLK_TC_COMPLETE)))
511                         continue;
512
513                 /* try to find matching trace and update statistics */
514                 t = blkiomon_do_trace(t);
515                 if (!t) {
516                         fprintf(stderr, "blkiomon: could not alloc trace\n");
517                         break;
518                 }
519                 bit = &t->bit;
520                 /* t and bit will be recycled for next incoming trace */
521         }
522         blkiomon_free_trace(t);
523         free(pdu_buf);
524         return 0;
525 }
526
527 static int blkiomon_open_output(struct output *out)
528 {
529         int mode, vbuf_size;
530
531         if (!out->fn)
532                 return 0;
533
534         if (!strcmp(out->fn, "-")) {
535                 out->fp = fdopen(STDOUT_FILENO, "w");
536                 mode = _IOLBF;
537                 vbuf_size = 4096;
538                 out->pipe = 1;
539         } else {
540                 out->fp = fopen(out->fn, "w");
541                 mode = _IOFBF;
542                 vbuf_size = 128 * 1024;
543                 out->pipe = 0;
544         }
545         if (!out->fp)
546                 goto failed;
547         out->buf = malloc(128 * 1024);
548         if (setvbuf(out->fp, out->buf, mode, vbuf_size))
549                 goto failed;
550         return 0;
551
552 failed:
553         fprintf(stderr, "blkiomon: could not write to %s\n", out->fn);
554         out->fn = NULL;
555         free(out->buf);
556         return 1;
557 }
558
559 static int blkiomon_open_msg_q(void)
560 {
561         key_t key;
562
563         if (!msg_q_name)
564                 return 0;
565         if (!msg_q_id || msg_id <= 0)
566                 return 1;
567         key = ftok(msg_q_name, msg_q_id);
568         if (key == -1)
569                 return 1;
570         while (up) {
571                 msg_q = msgget(key, S_IRWXU);
572                 if (msg_q >= 0)
573                         break;
574         }
575         return (msg_q >= 0 ? 0 : -1);
576 }
577
578 static void blkiomon_debug(void)
579 {
580         int i;
581         struct trace *t;
582
583         if (!debug.fn)
584                 return;
585
586         for (i = 0; i < TRACE_HASH_SIZE; i++)
587                 for (t = thash[i]; t; t = t->next) {
588                         dump_bit(t, "leftover");
589                         leftover++;
590                 }
591
592         fprintf(debug.fp, "%ld leftover, %ld match, %ld mismatch, "
593                 "%ld driverdata, %ld overall\n",
594                 leftover, match, mismatch, driverdata, sequence);
595 }
596
597 #define S_OPTS "b:d:D:h:I:Q:q:m:V"
598
599 static char usage_str[] = "\n\nblkiomon " \
600         "-I <interval>       | --interval=<interval>\n" \
601         "[ -h <file>         | --human-readable=<file> ]\n" \
602         "[ -b <file>         | --binary=<file> ]\n" \
603         "[ -D <file>         | --debug=<file> ]\n" \
604         "[ -Q <path name>    | --msg-queue-name=<path name>]\n" \
605         "[ -q <msg queue id> | --msg-queue-id=<msg queue id>]\n" \
606         "[ -m <msg id>       | --msg-id=<msg id>]\n" \
607         "[ -V                | --version ]\n\n" \
608         "\t-I   Sample interval.\n" \
609         "\t-h   Human-readable output file.\n" \
610         "\t-b   Binary output file.\n" \
611         "\t-d   Output file for data emitted by low level device driver.\n" \
612         "\t-D   Output file for debugging data.\n" \
613         "\t-Qqm Output to message queue using given ID for messages.\n" \
614         "\t-V   Print program version.\n\n";
615
616 static struct option l_opts[] = {
617         {
618                 .name = "human-readable",
619                 .has_arg = required_argument,
620                 .flag = NULL,
621                 .val = 'h'
622         },
623         {
624                 .name = "binary",
625                 .has_arg = required_argument,
626                 .flag = NULL,
627                 .val = 'b'
628         },
629         {
630                 .name = "dump-lldd",
631                 .has_arg = required_argument,
632                 .flag = NULL,
633                 .val = 'd'
634         },
635         {
636                 .name = "debug",
637                 .has_arg = required_argument,
638                 .flag = NULL,
639                 .val = 'D'
640         },
641         {
642                 .name = "interval",
643                 .has_arg = required_argument,
644                 .flag = NULL,
645                 .val = 'I'
646         },
647         {
648                 .name = "msg-queue",
649                 .has_arg = required_argument,
650                 .flag = NULL,
651                 .val = 'Q'
652         },
653         {
654                 .name = "msg-queue-id",
655                 .has_arg = required_argument,
656                 .flag = NULL,
657                 .val = 'q'
658         },
659         {
660                 .name = "msg-id",
661                 .has_arg = required_argument,
662                 .flag = NULL,
663                 .val = 'm'
664         },
665         {
666                 .name = "version",
667                 .has_arg = no_argument,
668                 .flag = NULL,
669                 .val = 'V'
670         },
671         {
672                 .name = NULL,
673         }
674 };
675
676 static void blkiomon_signal(int signal)
677 {
678         fprintf(stderr, "blkiomon: terminated by signal\n");
679         up = signal & 0;
680 }
681
682 int main(int argc, char *argv[])
683 {
684         int c;
685
686         signal(SIGALRM, blkiomon_signal);
687         signal(SIGINT, blkiomon_signal);
688         signal(SIGTERM, blkiomon_signal);
689         signal(SIGQUIT, blkiomon_signal);
690
691         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
692                 switch (c) {
693                 case 'h':
694                         human.fn = optarg;
695                         break;
696                 case 'b':
697                         binary.fn = optarg;
698                         break;
699                 case 'd':
700                         drvdata.fn = optarg;
701                         break;
702                 case 'D':
703                         debug.fn = optarg;
704                         break;
705                 case 'I':
706                         interval = atoi(optarg);
707                         break;
708                 case 'Q':
709                         msg_q_name = optarg;
710                         break;
711                 case 'q':
712                         msg_q_id = atoi(optarg);
713                         break;
714                 case 'm':
715                         msg_id = atoi(optarg);
716                         break;
717                 case 'V':
718                         printf("%s version %s\n", argv[0], blkiomon_version);
719                         return 0;
720                 default:
721                         fprintf(stderr, "Usage: %s", usage_str);
722                         return 1;
723                 }
724         }
725
726         if (interval <= 0) {
727                 fprintf(stderr, "Usage: %s", usage_str);
728                 return 1;
729         }
730
731         ifp = fdopen(STDIN_FILENO, "r");
732         if (!ifp) {
733                 perror("blkiomon: could not open stdin for reading");
734                 return 1;
735         }
736
737         if (blkiomon_open_output(&human))
738                 return 1;
739         if (blkiomon_open_output(&binary))
740                 return 1;
741         if (blkiomon_open_output(&drvdata))
742                 return 1;
743         if (blkiomon_open_output(&debug))
744                 return 1;
745         if (blkiomon_open_msg_q())
746                 return 1;
747
748         if (pthread_create(&interval_thread, NULL, blkiomon_interval, NULL)) {
749                 fprintf(stderr, "blkiomon: could not create thread");
750                 return 1;
751         }
752
753         blkiomon_do_fifo();
754
755         blkiomon_debug();
756         return 0;
757 }