blkparse: Avoid segfault for wrong cpu number.
[blktrace.git] / blkiomon.c
index 049f00bee4b54c6ce7adea8a929da04d67ceb193..a895f6525bd287abbeadeb41f04a8621900006ca 100644 (file)
@@ -71,7 +71,7 @@ struct output {
        int pipe;
 };
 
-static char blkiomon_version[] = "0.2";
+static char blkiomon_version[] = "0.3";
 
 static FILE *ifp;
 static int interval = -1;
@@ -163,11 +163,12 @@ static struct dstat *blkiomon_alloc_dstat(void)
        } else
                dstat = malloc(sizeof(*dstat));
        if (!dstat) {
-               perror("blkiomon: could not allocate device statistic");
+               fprintf(stderr,
+                       "blkiomon: could not allocate device statistic");
                return NULL;
        }
 
-       memset(dstat, 0, sizeof(*dstat));
+       blkiomon_stat_init(&dstat->msg.stat);
        return dstat;
 }
 
@@ -210,8 +211,6 @@ static struct dstat *blkiomon_get_dstat(__u32 device)
                goto out;
 
        dstat->msg.stat.device = device;
-       dstat->msg.stat.size_mm.min = -1ULL;
-       dstat->msg.stat.d2c_mm.min = -1ULL;
 
        rb_link_node(&dstat->node, search.parent, search.node_ptr);
        rb_insert_color(&dstat->node, &dstat_tree[dstat_curr]);
@@ -279,7 +278,7 @@ static void *blkiomon_interval(void *data)
        while (1) {
                wake.tv_sec += interval;
                if (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &wake, &r)) {
-                       perror("blkiomon: interrupted sleep");
+                       fprintf(stderr, "blkiomon: interrupted sleep");
                        continue;
                }
 
@@ -313,23 +312,26 @@ static int blkiomon_account(struct blk_io_trace *bit_d,
        struct blkiomon_stat *p;
        __u64 d2c = (bit_c->time - bit_d->time) / 1000; /* ns -> us */
        __u32 size = bit_d->bytes;
+       __u64 thrput = size * 1000 / d2c;
 
        dstat = blkiomon_get_dstat(bit_d->device);
        if (!dstat)
                return 1;
        p = &dstat->msg.stat;
 
-       if (BLK_DATADIR(bit_c->action) & BLK_TC_READ)
-               p->read++;
-       else if (BLK_DATADIR(bit_c->action) & BLK_TC_WRITE)
-               p->write++;
-       else
+       if (BLK_DATADIR(bit_c->action) & BLK_TC_READ) {
+               minmax_account(&p->thrput_r, thrput);
+               minmax_account(&p->size_r, size);
+               minmax_account(&p->d2c_r, d2c);
+       } else if (BLK_DATADIR(bit_c->action) & BLK_TC_WRITE) {
+               minmax_account(&p->thrput_w, thrput);
+               minmax_account(&p->size_w, size);
+               minmax_account(&p->d2c_w, d2c);
+       } else
                p->bidir++;
 
        histlog2_account(p->size_hist, size, &size_hist);
        histlog2_account(p->d2c_hist, d2c, &d2c_hist);
-       minmax_account(&p->size_mm, size);
-       minmax_account(&p->d2c_mm, d2c);
        return 0;
 }
 
@@ -466,17 +468,19 @@ static int blkiomon_do_fifo(void)
                }
                if (ferror(ifp)) {
                        clearerr(ifp);
-                       perror("blkiomon: error while reading trace");
+                       fprintf(stderr, "blkiomon: error while reading trace");
                        break;
                }
 
-               if (data_is_native == -1 && check_data_endianness(bit->magic))
+               if (data_is_native == -1 && check_data_endianness(bit->magic)) {
+                       fprintf(stderr, "blkiomon: endianess problem\n");
                        break;
+               }
 
                /* endianess */
                trace_to_cpu(bit);
                if (verify_trace(bit)) {
-                       perror("blkiomon: bad trace");
+                       fprintf(stderr, "blkiomon: bad trace\n");
                        break;
                }
 
@@ -485,7 +489,7 @@ static int blkiomon_do_fifo(void)
                        pdu_buf = realloc(pdu_buf, bit->pdu_len);
                        if (fread(pdu_buf, bit->pdu_len, 1, ifp) != 1) {
                                clearerr(ifp);
-                               perror("blkiomon: could not read payload");
+                               fprintf(stderr, "blkiomon: could not read payload\n");
                                break;
                        }
                }
@@ -495,8 +499,10 @@ static int blkiomon_do_fifo(void)
                /* forward low-level device driver trace to other tool */
                if (bit->action & BLK_TC_ACT(BLK_TC_DRV_DATA)) {
                        driverdata++;
-                       if (blkiomon_dump_drvdata(bit, pdu_buf))
+                       if (blkiomon_dump_drvdata(bit, pdu_buf)) {
+                               fprintf(stderr, "blkiomon: could not send trace\n");
                                break;
+                       }
                        continue;
                }
 
@@ -505,8 +511,10 @@ static int blkiomon_do_fifo(void)
 
                /* try to find matching trace and update statistics */
                t = blkiomon_do_trace(t);
-               if (!t)
+               if (!t) {
+                       fprintf(stderr, "blkiomon: could not alloc trace\n");
                        break;
+               }
                bit = &t->bit;
                /* t and bit will be recycled for next incoming trace */
        }
@@ -591,8 +599,9 @@ static char usage_str[] = "\n\nblkiomon " \
        "-I <interval>       | --interval=<interval>\n" \
        "[ -h <file>         | --human-readable=<file> ]\n" \
        "[ -b <file>         | --binary=<file> ]\n" \
+       "[ -d <file>         | --dump-lldd=<file> ]\n" \
        "[ -D <file>         | --debug=<file> ]\n" \
-       "[ -Q <path name>    | --msg-queue-name=<path name>]\n" \
+       "[ -Q <path name>    | --msg-queue=<path name>]\n" \
        "[ -q <msg queue id> | --msg-queue-id=<msg queue id>]\n" \
        "[ -m <msg id>       | --msg-id=<msg id>]\n" \
        "[ -V                | --version ]\n\n" \
@@ -737,7 +746,7 @@ int main(int argc, char *argv[])
                return 1;
 
        if (pthread_create(&interval_thread, NULL, blkiomon_interval, NULL)) {
-               perror("blkiomon: could not create thread");
+               fprintf(stderr, "blkiomon: could not create thread");
                return 1;
        }