Commit | Line | Data |
---|---|---|
cc19ddd6 MP |
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; | |
cc19ddd6 MP |
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 | ||
3bc3451b | 90 | static struct output drvdata, human, binary, debug; |
cc19ddd6 MP |
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) { | |
9b26a3a8 MP |
166 | fprintf(stderr, |
167 | "blkiomon: could not allocate device statistic"); | |
cc19ddd6 MP |
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_mm.min = -1ULL; | |
215 | dstat->msg.stat.d2c_mm.min = -1ULL; | |
216 | ||
217 | rb_link_node(&dstat->node, search.parent, search.node_ptr); | |
218 | rb_insert_color(&dstat->node, &dstat_tree[dstat_curr]); | |
219 | ||
220 | dstat->next = dstat_list[dstat_curr]; | |
221 | dstat_list[dstat_curr] = dstat; | |
222 | ||
223 | out: | |
224 | pthread_mutex_unlock(&dstat_mutex); | |
225 | return dstat; | |
226 | } | |
227 | ||
228 | static int blkiomon_output_msg_q(struct dstat *dstat) | |
229 | { | |
230 | if (!msg_q_name) | |
231 | return 0; | |
232 | ||
233 | dstat->msg.mtype = msg_id; | |
234 | return msgsnd(msg_q, &dstat->msg, sizeof(struct blkiomon_stat), 0); | |
235 | } | |
236 | ||
237 | static int blkiomon_output_binary(struct dstat *dstat) | |
238 | { | |
239 | struct blkiomon_stat *p = &dstat->msg.stat; | |
240 | ||
241 | if (!binary.fn) | |
242 | return 0; | |
243 | ||
244 | if (fwrite(p, sizeof(*p), 1, binary.fp) != 1) | |
245 | goto failed; | |
246 | if (binary.pipe && fflush(binary.fp)) | |
247 | goto failed; | |
248 | return 0; | |
249 | ||
250 | failed: | |
251 | fprintf(stderr, "blkiomon: could not write to %s\n", binary.fn); | |
252 | fclose(binary.fp); | |
253 | binary.fn = NULL; | |
254 | return 1; | |
255 | } | |
256 | ||
257 | static struct dstat *blkiomon_output(struct dstat *head, struct timespec *ts) | |
258 | { | |
259 | struct dstat *dstat, *tail = NULL; | |
260 | ||
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); | |
267 | tail = dstat; | |
268 | } | |
269 | return tail; | |
270 | } | |
271 | ||
272 | static void *blkiomon_interval(void *data) | |
273 | { | |
274 | struct timespec wake, r; | |
275 | struct dstat *head, *tail; | |
276 | int finished; | |
277 | ||
278 | clock_gettime(CLOCK_REALTIME, &wake); | |
279 | ||
280 | while (1) { | |
281 | wake.tv_sec += interval; | |
282 | if (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &wake, &r)) { | |
9b26a3a8 | 283 | fprintf(stderr, "blkiomon: interrupted sleep"); |
cc19ddd6 MP |
284 | continue; |
285 | } | |
286 | ||
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); | |
292 | ||
293 | head = dstat_list[finished]; | |
294 | if (!head) | |
295 | continue; | |
296 | dstat_list[finished] = NULL; | |
297 | dstat_tree[finished] = RB_ROOT; | |
298 | tail = blkiomon_output(head, &wake); | |
299 | ||
300 | pthread_mutex_lock(&dstat_mutex); | |
301 | tail->next = vacant_dstats_list; | |
302 | vacant_dstats_list = head; | |
303 | pthread_mutex_unlock(&dstat_mutex); | |
304 | } | |
305 | return data; | |
306 | } | |
307 | ||
308 | #define BLK_DATADIR(a) (((a) >> BLK_TC_SHIFT) & (BLK_TC_READ | BLK_TC_WRITE)) | |
309 | ||
310 | static int blkiomon_account(struct blk_io_trace *bit_d, | |
311 | struct blk_io_trace *bit_c) | |
312 | { | |
313 | struct dstat *dstat; | |
314 | struct blkiomon_stat *p; | |
315 | __u64 d2c = (bit_c->time - bit_d->time) / 1000; /* ns -> us */ | |
316 | __u32 size = bit_d->bytes; | |
317 | ||
318 | dstat = blkiomon_get_dstat(bit_d->device); | |
319 | if (!dstat) | |
320 | return 1; | |
321 | p = &dstat->msg.stat; | |
322 | ||
323 | if (BLK_DATADIR(bit_c->action) & BLK_TC_READ) | |
324 | p->read++; | |
325 | else if (BLK_DATADIR(bit_c->action) & BLK_TC_WRITE) | |
326 | p->write++; | |
327 | else | |
328 | p->bidir++; | |
329 | ||
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); | |
334 | return 0; | |
335 | } | |
336 | ||
337 | static struct trace *blkiomon_alloc_trace(void) | |
338 | { | |
339 | struct trace *t = vacant_traces_list; | |
340 | if (t) { | |
341 | vacant_traces_list = t->next; | |
342 | vacant_traces--; | |
343 | } else | |
344 | t = malloc(sizeof(*t)); | |
345 | memset(t, 0, sizeof(*t)); | |
346 | return t; | |
347 | } | |
348 | ||
349 | static void blkiomon_free_trace(struct trace *t) | |
350 | { | |
351 | if (vacant_traces < 256) { | |
352 | t->next = vacant_traces_list; | |
353 | vacant_traces_list = t; | |
354 | vacant_traces++; | |
355 | } else | |
356 | free(t); | |
357 | } | |
358 | ||
359 | static int action(int a) | |
360 | { | |
361 | int bits = BLK_TC_WRITE | BLK_TC_READ | BLK_TC_FS | BLK_TC_PC; | |
362 | return a & (BLK_TC_ACT(bits)); | |
363 | } | |
364 | ||
365 | static void blkiomon_store_trace(struct trace *t) | |
366 | { | |
367 | int i = t->bit.sector % TRACE_HASH_SIZE; | |
368 | ||
369 | t->next = thash[i]; | |
370 | thash[i] = t; | |
371 | } | |
372 | ||
373 | static struct trace *blkiomon_fetch_trace(struct blk_io_trace *bit) | |
374 | { | |
375 | int i = bit->sector % TRACE_HASH_SIZE; | |
376 | struct trace *t, *prev = NULL; | |
377 | ||
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)) { | |
382 | if (prev) | |
383 | prev->next = t->next; | |
384 | else | |
385 | thash[i] = t->next; | |
386 | return t; | |
387 | } | |
388 | prev = t; | |
389 | } | |
390 | return NULL; | |
391 | } | |
392 | ||
393 | static struct trace *blkiomon_do_trace(struct trace *t) | |
394 | { | |
395 | struct trace *t_stored, *t_old, *t_young; | |
396 | ||
397 | /* store trace if there is no match yet */ | |
398 | t_stored = blkiomon_fetch_trace(&t->bit); | |
399 | if (!t_stored) { | |
400 | blkiomon_store_trace(t); | |
401 | return blkiomon_alloc_trace(); | |
402 | } | |
403 | ||
404 | /* figure out older trace and younger trace */ | |
405 | if (t_stored->bit.time < t->bit.time) { | |
406 | t_old = t_stored; | |
407 | t_young = t; | |
408 | } else { | |
409 | t_old = t; | |
410 | t_young = t_stored; | |
411 | } | |
412 | ||
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 */ | |
417 | match++; | |
418 | blkiomon_account(&t_old->bit, &t_young->bit); | |
419 | blkiomon_free_trace(t_stored); | |
420 | return t; | |
421 | } | |
422 | ||
423 | /* no matching D and C traces - keep more recent trace */ | |
424 | dump_bits(t_old, t_young, "mismatch"); | |
425 | mismatch++; | |
426 | blkiomon_store_trace(t_young); | |
427 | return t_old; | |
428 | } | |
429 | ||
3bc3451b MP |
430 | static int blkiomon_dump_drvdata(struct blk_io_trace *bit, void *pdu_buf) |
431 | { | |
432 | if (!drvdata.fn) | |
433 | return 0; | |
434 | ||
435 | if (fwrite(bit, sizeof(*bit), 1, drvdata.fp) != 1) | |
436 | goto failed; | |
437 | if (fwrite(pdu_buf, bit->pdu_len, 1, drvdata.fp) != 1) | |
438 | goto failed; | |
439 | if (drvdata.pipe && fflush(drvdata.fp)) | |
440 | goto failed; | |
441 | return 0; | |
442 | ||
443 | failed: | |
444 | fprintf(stderr, "blkiomon: could not write to %s\n", drvdata.fn); | |
445 | fclose(drvdata.fp); | |
446 | drvdata.fn = NULL; | |
447 | return 1; | |
448 | } | |
449 | ||
cc19ddd6 MP |
450 | static int blkiomon_do_fifo(void) |
451 | { | |
452 | struct trace *t; | |
453 | struct blk_io_trace *bit; | |
454 | void *pdu_buf = NULL; | |
455 | ||
456 | t = blkiomon_alloc_trace(); | |
457 | if (!t) | |
458 | return 1; | |
459 | bit = &t->bit; | |
460 | ||
461 | while (up) { | |
462 | if (fread(bit, sizeof(*bit), 1, ifp) != 1) { | |
463 | if (!feof(ifp)) | |
464 | fprintf(stderr, | |
465 | "blkiomon: could not read trace"); | |
466 | break; | |
467 | } | |
468 | if (ferror(ifp)) { | |
469 | clearerr(ifp); | |
9b26a3a8 | 470 | fprintf(stderr, "blkiomon: error while reading trace"); |
cc19ddd6 MP |
471 | break; |
472 | } | |
473 | ||
9b26a3a8 MP |
474 | if (data_is_native == -1 && check_data_endianness(bit->magic)) { |
475 | fprintf(stderr, "blkiomon: endianess problem\n"); | |
cc19ddd6 | 476 | break; |
9b26a3a8 | 477 | } |
cc19ddd6 MP |
478 | |
479 | /* endianess */ | |
480 | trace_to_cpu(bit); | |
481 | if (verify_trace(bit)) { | |
9b26a3a8 | 482 | fprintf(stderr, "blkiomon: bad trace\n"); |
cc19ddd6 MP |
483 | break; |
484 | } | |
485 | ||
486 | /* read additional trace payload */ | |
487 | if (bit->pdu_len) { | |
488 | pdu_buf = realloc(pdu_buf, bit->pdu_len); | |
489 | if (fread(pdu_buf, bit->pdu_len, 1, ifp) != 1) { | |
490 | clearerr(ifp); | |
9b26a3a8 | 491 | fprintf(stderr, "blkiomon: could not read payload\n"); |
cc19ddd6 MP |
492 | break; |
493 | } | |
494 | } | |
495 | ||
496 | t->sequence = sequence++; | |
497 | ||
3bc3451b MP |
498 | /* forward low-level device driver trace to other tool */ |
499 | if (bit->action & BLK_TC_ACT(BLK_TC_DRV_DATA)) { | |
500 | driverdata++; | |
9b26a3a8 MP |
501 | if (blkiomon_dump_drvdata(bit, pdu_buf)) { |
502 | fprintf(stderr, "blkiomon: could not send trace\n"); | |
3bc3451b | 503 | break; |
9b26a3a8 | 504 | } |
3bc3451b MP |
505 | continue; |
506 | } | |
507 | ||
cc19ddd6 MP |
508 | if (!(bit->action & BLK_TC_ACT(BLK_TC_ISSUE | BLK_TC_COMPLETE))) |
509 | continue; | |
510 | ||
511 | /* try to find matching trace and update statistics */ | |
512 | t = blkiomon_do_trace(t); | |
9b26a3a8 MP |
513 | if (!t) { |
514 | fprintf(stderr, "blkiomon: could not alloc trace\n"); | |
cc19ddd6 | 515 | break; |
9b26a3a8 | 516 | } |
cc19ddd6 MP |
517 | bit = &t->bit; |
518 | /* t and bit will be recycled for next incoming trace */ | |
519 | } | |
520 | blkiomon_free_trace(t); | |
521 | free(pdu_buf); | |
522 | return 0; | |
523 | } | |
524 | ||
525 | static int blkiomon_open_output(struct output *out) | |
526 | { | |
527 | int mode, vbuf_size; | |
528 | ||
529 | if (!out->fn) | |
530 | return 0; | |
531 | ||
532 | if (!strcmp(out->fn, "-")) { | |
533 | out->fp = fdopen(STDOUT_FILENO, "w"); | |
534 | mode = _IOLBF; | |
535 | vbuf_size = 4096; | |
536 | out->pipe = 1; | |
537 | } else { | |
538 | out->fp = fopen(out->fn, "w"); | |
539 | mode = _IOFBF; | |
540 | vbuf_size = 128 * 1024; | |
541 | out->pipe = 0; | |
542 | } | |
543 | if (!out->fp) | |
544 | goto failed; | |
545 | out->buf = malloc(128 * 1024); | |
546 | if (setvbuf(out->fp, out->buf, mode, vbuf_size)) | |
547 | goto failed; | |
548 | return 0; | |
549 | ||
550 | failed: | |
551 | fprintf(stderr, "blkiomon: could not write to %s\n", out->fn); | |
552 | out->fn = NULL; | |
553 | free(out->buf); | |
554 | return 1; | |
555 | } | |
556 | ||
557 | static int blkiomon_open_msg_q(void) | |
558 | { | |
559 | key_t key; | |
560 | ||
561 | if (!msg_q_name) | |
562 | return 0; | |
563 | if (!msg_q_id || msg_id <= 0) | |
564 | return 1; | |
565 | key = ftok(msg_q_name, msg_q_id); | |
566 | if (key == -1) | |
567 | return 1; | |
568 | while (up) { | |
569 | msg_q = msgget(key, S_IRWXU); | |
570 | if (msg_q >= 0) | |
571 | break; | |
572 | } | |
573 | return (msg_q >= 0 ? 0 : -1); | |
574 | } | |
575 | ||
576 | static void blkiomon_debug(void) | |
577 | { | |
4bd8e3ba | 578 | int i; |
cc19ddd6 MP |
579 | struct trace *t; |
580 | ||
581 | if (!debug.fn) | |
582 | return; | |
583 | ||
4bd8e3ba MP |
584 | for (i = 0; i < TRACE_HASH_SIZE; i++) |
585 | for (t = thash[i]; t; t = t->next) { | |
586 | dump_bit(t, "leftover"); | |
587 | leftover++; | |
588 | } | |
589 | ||
cc19ddd6 MP |
590 | fprintf(debug.fp, "%ld leftover, %ld match, %ld mismatch, " |
591 | "%ld driverdata, %ld overall\n", | |
592 | leftover, match, mismatch, driverdata, sequence); | |
593 | } | |
594 | ||
3bc3451b | 595 | #define S_OPTS "b:d:D:h:I:Q:q:m:V" |
cc19ddd6 MP |
596 | |
597 | static char usage_str[] = "\n\nblkiomon " \ | |
598 | "-I <interval> | --interval=<interval>\n" \ | |
599 | "[ -h <file> | --human-readable=<file> ]\n" \ | |
600 | "[ -b <file> | --binary=<file> ]\n" \ | |
601 | "[ -D <file> | --debug=<file> ]\n" \ | |
602 | "[ -Q <path name> | --msg-queue-name=<path name>]\n" \ | |
603 | "[ -q <msg queue id> | --msg-queue-id=<msg queue id>]\n" \ | |
604 | "[ -m <msg id> | --msg-id=<msg id>]\n" \ | |
605 | "[ -V | --version ]\n\n" \ | |
606 | "\t-I Sample interval.\n" \ | |
607 | "\t-h Human-readable output file.\n" \ | |
608 | "\t-b Binary output file.\n" \ | |
3bc3451b | 609 | "\t-d Output file for data emitted by low level device driver.\n" \ |
cc19ddd6 MP |
610 | "\t-D Output file for debugging data.\n" \ |
611 | "\t-Qqm Output to message queue using given ID for messages.\n" \ | |
612 | "\t-V Print program version.\n\n"; | |
613 | ||
614 | static struct option l_opts[] = { | |
615 | { | |
616 | .name = "human-readable", | |
617 | .has_arg = required_argument, | |
618 | .flag = NULL, | |
619 | .val = 'h' | |
620 | }, | |
621 | { | |
622 | .name = "binary", | |
623 | .has_arg = required_argument, | |
624 | .flag = NULL, | |
625 | .val = 'b' | |
626 | }, | |
3bc3451b MP |
627 | { |
628 | .name = "dump-lldd", | |
629 | .has_arg = required_argument, | |
630 | .flag = NULL, | |
631 | .val = 'd' | |
632 | }, | |
cc19ddd6 MP |
633 | { |
634 | .name = "debug", | |
635 | .has_arg = required_argument, | |
636 | .flag = NULL, | |
637 | .val = 'D' | |
638 | }, | |
639 | { | |
640 | .name = "interval", | |
641 | .has_arg = required_argument, | |
642 | .flag = NULL, | |
643 | .val = 'I' | |
644 | }, | |
645 | { | |
646 | .name = "msg-queue", | |
647 | .has_arg = required_argument, | |
648 | .flag = NULL, | |
649 | .val = 'Q' | |
650 | }, | |
651 | { | |
652 | .name = "msg-queue-id", | |
653 | .has_arg = required_argument, | |
654 | .flag = NULL, | |
655 | .val = 'q' | |
656 | }, | |
657 | { | |
658 | .name = "msg-id", | |
659 | .has_arg = required_argument, | |
660 | .flag = NULL, | |
661 | .val = 'm' | |
662 | }, | |
663 | { | |
664 | .name = "version", | |
665 | .has_arg = no_argument, | |
666 | .flag = NULL, | |
667 | .val = 'V' | |
668 | }, | |
669 | { | |
670 | .name = NULL, | |
671 | } | |
672 | }; | |
673 | ||
674 | static void blkiomon_signal(int signal) | |
675 | { | |
676 | fprintf(stderr, "blkiomon: terminated by signal\n"); | |
677 | up = signal & 0; | |
678 | } | |
679 | ||
680 | int main(int argc, char *argv[]) | |
681 | { | |
682 | int c; | |
683 | ||
684 | signal(SIGALRM, blkiomon_signal); | |
685 | signal(SIGINT, blkiomon_signal); | |
686 | signal(SIGTERM, blkiomon_signal); | |
687 | signal(SIGQUIT, blkiomon_signal); | |
688 | ||
689 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) { | |
690 | switch (c) { | |
691 | case 'h': | |
692 | human.fn = optarg; | |
693 | break; | |
694 | case 'b': | |
695 | binary.fn = optarg; | |
696 | break; | |
3bc3451b MP |
697 | case 'd': |
698 | drvdata.fn = optarg; | |
699 | break; | |
cc19ddd6 MP |
700 | case 'D': |
701 | debug.fn = optarg; | |
702 | break; | |
703 | case 'I': | |
704 | interval = atoi(optarg); | |
705 | break; | |
706 | case 'Q': | |
707 | msg_q_name = optarg; | |
708 | break; | |
709 | case 'q': | |
710 | msg_q_id = atoi(optarg); | |
711 | break; | |
712 | case 'm': | |
713 | msg_id = atoi(optarg); | |
714 | break; | |
715 | case 'V': | |
716 | printf("%s version %s\n", argv[0], blkiomon_version); | |
717 | return 0; | |
718 | default: | |
719 | fprintf(stderr, "Usage: %s", usage_str); | |
720 | return 1; | |
721 | } | |
722 | } | |
723 | ||
724 | if (interval <= 0) { | |
725 | fprintf(stderr, "Usage: %s", usage_str); | |
726 | return 1; | |
727 | } | |
728 | ||
729 | ifp = fdopen(STDIN_FILENO, "r"); | |
730 | if (!ifp) { | |
731 | perror("blkiomon: could not open stdin for reading"); | |
732 | return 1; | |
733 | } | |
734 | ||
735 | if (blkiomon_open_output(&human)) | |
736 | return 1; | |
737 | if (blkiomon_open_output(&binary)) | |
738 | return 1; | |
3bc3451b MP |
739 | if (blkiomon_open_output(&drvdata)) |
740 | return 1; | |
cc19ddd6 MP |
741 | if (blkiomon_open_output(&debug)) |
742 | return 1; | |
743 | if (blkiomon_open_msg_q()) | |
744 | return 1; | |
745 | ||
746 | if (pthread_create(&interval_thread, NULL, blkiomon_interval, NULL)) { | |
9b26a3a8 | 747 | fprintf(stderr, "blkiomon: could not create thread"); |
cc19ddd6 MP |
748 | return 1; |
749 | } | |
750 | ||
751 | blkiomon_do_fifo(); | |
752 | ||
753 | blkiomon_debug(); | |
754 | return 0; | |
755 | } |