diff options
author | Josh Stone <jistone@redhat.com> | 2009-10-13 14:10:08 -0700 |
---|---|---|
committer | Josh Stone <jistone@redhat.com> | 2009-10-13 14:10:08 -0700 |
commit | 42b2e52658603d3e1384a909ad816d05529fc1d3 (patch) | |
tree | c80d87dfd4821ed53ce7b623c256c3abf3b625a3 | |
parent | 334d767529cba0540299adb731e54a8b390ee98c (diff) | |
download | systemtap-steved-42b2e52658603d3e1384a909ad816d05529fc1d3.tar.gz systemtap-steved-42b2e52658603d3e1384a909ad816d05529fc1d3.tar.xz systemtap-steved-42b2e52658603d3e1384a909ad816d05529fc1d3.zip |
Refactor some of the histogram printing
* runtime/stat-common.c (reprint_buf): Removed.
(_stp_stat_print_histogram_buf): Use a local HIST_PRINTF macro to
abstract the buffer management. Also convert reprint_buf calls to
either %* formats or simple for-loops.
-rw-r--r-- | runtime/stat-common.c | 85 |
1 files changed, 29 insertions, 56 deletions
diff --git a/runtime/stat-common.c b/runtime/stat-common.c index db85c35b..e6fd3a11 100644 --- a/runtime/stat-common.c +++ b/runtime/stat-common.c @@ -53,17 +53,6 @@ static int needed_space(int64_t v) return space; } -static int reprint_buf(char *buf, size_t size, int num, char *s) -{ - char *cur_buf = buf, *fake = buf; - char **ptr = (buf == NULL ? &fake : &cur_buf); - while (num > 0) { - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, s); - num--; - } - return *ptr - buf; -} - /* Given a bucket number for a log histogram, return the value. */ static int64_t _stp_bucket_to_val(int num) { @@ -145,10 +134,13 @@ static void _stp_stat_print_histogram_buf(char *buf, size_t size, Hist st, stat { int scale, i, j, val_space, cnt_space; int low_bucket = -1, high_bucket = 0, over = 0, under = 0; - int64_t val, v, max = 0; + int64_t val, v, valmax = 0; int eliding = 0; char *cur_buf = buf, *fake = buf; - char **ptr = (buf == NULL ? &fake : &cur_buf); + char **bufptr = (buf == NULL ? &fake : &cur_buf); + +#define HIST_PRINTF(fmt, args...) \ + (*bufptr += _stp_snprintf(cur_buf, buf + size - cur_buf, fmt, ## args)) if (st->type != HIST_LOG && st->type != HIST_LINEAR) return; @@ -160,8 +152,8 @@ static void _stp_stat_print_histogram_buf(char *buf, size_t size, Hist st, stat low_bucket = i; if (sd->histogram[i] > 0) high_bucket = i; - if (sd->histogram[i] > max) - max = sd->histogram[i]; + if (sd->histogram[i] > valmax) + valmax = sd->histogram[i]; } /* Touch up the bucket margin to show up to two zero-slots on @@ -191,45 +183,37 @@ static void _stp_stat_print_histogram_buf(char *buf, size_t size, Hist st, stat over = 1; } - if (max <= HIST_WIDTH) + if (valmax <= HIST_WIDTH) scale = 1; else { - int64_t tmp = max; + int64_t tmp = valmax; int rem = do_div(tmp, HIST_WIDTH); scale = tmp; if (rem) scale++; } /* count space */ - cnt_space = needed_space(max); + cnt_space = needed_space(valmax); /* Compute value space */ if (st->type == HIST_LINEAR) { - i = needed_space(st->start) + under; - val_space = needed_space(st->start + st->interval * high_bucket) + over; + val_space = max(needed_space(st->start) + under, + needed_space(st->start + st->interval * high_bucket) + over); } else { - i = needed_space(_stp_bucket_to_val(high_bucket)); - val_space = needed_space(_stp_bucket_to_val(low_bucket)); + val_space = max(needed_space(_stp_bucket_to_val(high_bucket)), + needed_space(_stp_bucket_to_val(low_bucket))); } - if (i > val_space) - val_space = i; - + val_space = max(val_space, 5 /* = sizeof("value") */); /* print header */ - j = 0; - if (val_space > 5) /* 5 = sizeof("value") */ - j = val_space - 5; - else - val_space = 5; - for ( i = 0; i < j; i++) - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, " "); - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, "value |"); - *ptr += reprint_buf(cur_buf, buf + size - cur_buf, HIST_WIDTH, "-"); - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, " count\n"); + HIST_PRINTF("%*s |", val_space, "value"); + for (j = 0; j < HIST_WIDTH; ++j) + HIST_PRINTF("-"); + HIST_PRINTF(" count\n"); eliding = 0; for (i = low_bucket; i <= high_bucket; i++) { - int over_under = 0; + const char *val_prefix = ""; /* Elide consecutive zero buckets. Specifically, skip this row if it is zero and some of its nearest @@ -259,8 +243,7 @@ static void _stp_stat_print_histogram_buf(char *buf, size_t size, Hist st, stat about to print a new one. So let's print a mark on the vertical axis to represent the missing rows. */ if (eliding) { - *ptr += reprint_buf(cur_buf, buf + size - cur_buf, val_space, " "); - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, " ~\n"); + HIST_PRINTF("%*s ~\n", val_space, ""); eliding = 0; } } @@ -269,38 +252,28 @@ static void _stp_stat_print_histogram_buf(char *buf, size_t size, Hist st, stat if (i == 0) { /* underflow */ val = st->start; - over_under = 1; + val_prefix = "<"; } else if (i == st->buckets-1) { /* overflow */ val = st->start + (i - 2) * st->interval; - over_under = 1; + val_prefix = ">"; } else val = st->start + (i - 1) * st->interval; } else val = _stp_bucket_to_val(i); - *ptr += reprint_buf(cur_buf, buf + size - cur_buf, val_space - needed_space(val) - over_under, " "); - - if (over_under) { - if (i == 0) - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, "<%lld", val); - else if (i == st->buckets-1) - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, ">%lld", val); - else - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, "%lld", val); - } else - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, "%lld", val); - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, " |"); + HIST_PRINTF("%*s%lld |", val_space - needed_space(val), val_prefix, val); /* v = s->histogram[i] / scale; */ v = sd->histogram[i]; do_div(v, scale); - *ptr += reprint_buf(cur_buf, buf + size - cur_buf, v, "@"); - *ptr += reprint_buf(cur_buf, buf + size - cur_buf, HIST_WIDTH - v + 1 + cnt_space - needed_space(sd->histogram[i]), " "); - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, "%lld\n", sd->histogram[i]); + for (j = 0; j < v; ++j) + HIST_PRINTF("@"); + HIST_PRINTF("%*lld\n", HIST_WIDTH - v + 1 + cnt_space, sd->histogram[i]); } - *ptr += _stp_snprintf(cur_buf, buf + size - cur_buf, "\n"); + HIST_PRINTF("\n"); +#undef HIST_PRINTF } static void _stp_stat_print_histogram(Hist st, stat *sd) |