num2str: add arguments to represent values in terms of bytes/bits
authorSteven Noonan <steven@uplinklabs.net>
Fri, 5 Apr 2013 23:57:38 +0000 (16:57 -0700)
committerJens Axboe <axboe@kernel.dk>
Tue, 9 Apr 2013 18:00:31 +0000 (20:00 +0200)
This allows for representing I/O rates in terms of e.g. megabits (Mb) versus
megabytes (MB).

Signed-off-by: Steven Noonan <steven@uplinklabs.net>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
eta.c
fio.h
lib/num2str.c
stat.c

diff --git a/eta.c b/eta.c
index f90d428197656f362fcdf199865a65f06dbebbec..e08b5f7975a2ef1d5b91b071725b90192f706b92 100644 (file)
--- a/eta.c
+++ b/eta.c
@@ -449,9 +449,9 @@ void display_thread_status(struct jobs_eta *je)
        if (je->m_rate || je->t_rate) {
                char *tr, *mr;
 
-               mr = num2str(je->m_rate, 4, 0, je->is_pow2);
-               tr = num2str(je->t_rate, 4, 0, je->is_pow2);
-               p += sprintf(p, ", CR=%s/%s KB/s", tr, mr);
+               mr = num2str(je->m_rate, 4, 0, je->is_pow2, 8);
+               tr = num2str(je->t_rate, 4, 0, je->is_pow2, 8);
+               p += sprintf(p, ", CR=%s/%s /s", tr, mr);
                free(tr);
                free(mr);
        } else if (je->m_iops || je->t_iops)
@@ -474,8 +474,8 @@ void display_thread_status(struct jobs_eta *je)
 
                for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
                        rate_str[ddir] = num2str(je->rate[ddir], 5,
-                                               1024, je->is_pow2);
-                       iops_str[ddir] = num2str(je->iops[ddir], 4, 1, 0);
+                                               1024, je->is_pow2, 8);
+                       iops_str[ddir] = num2str(je->iops[ddir], 4, 1, 0, 0);
                }
 
                left = sizeof(output) - (p - output) - 1;
diff --git a/fio.h b/fio.h
index db594ab8e4d01250fe7360a48b23d3a3211f350a..cc439767b11f09fb288a656df050dc3ee2857e9d 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -686,7 +686,7 @@ extern void fio_options_mem_dupe(struct thread_data *);
 extern void options_mem_dupe(void *data, struct fio_option *options);
 extern void td_fill_rand_seeds(struct thread_data *);
 extern void add_job_opts(const char **);
-extern char *num2str(unsigned long, int, int, int);
+extern char *num2str(unsigned long, int, int, int, int);
 extern int ioengine_load(struct thread_data *);
 
 #define FIO_GETOPT_JOB         0x89000000
index 559cbeb590e4926b2d34f3b34cf6ac4cc0eadf10..a1041926c5e5aff50dc540227a89d9d76d17dfc6 100644 (file)
@@ -5,12 +5,13 @@
 /*
  * Cheesy number->string conversion, complete with carry rounding error.
  */
-char *num2str(unsigned long num, int maxlen, int base, int pow2)
+char *num2str(unsigned long num, int maxlen, int base, int pow2, int unit_base)
 {
-       char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
-       unsigned int thousand[] = { 1000, 1024 };
+       const char *postfix[] = { "", "K", "M", "G", "P", "E" };
+       const char *byte_postfix[] = { "", "B", "bit" };
+       const unsigned int thousand[] = { 1000, 1024 };
        unsigned int modulo, decimals;
-       int post_index, carry = 0;
+       int byte_post_index = 0, post_index, carry = 0;
        char tmp[32];
        char *buf;
 
@@ -19,6 +20,16 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2)
        for (post_index = 0; base > 1; post_index++)
                base /= thousand[!!pow2];
 
+       switch (unit_base) {
+       case 1:
+               byte_post_index = 2;
+               num *= 8;
+               break;
+       case 8:
+               byte_post_index = 1;
+               break;
+       }
+
        modulo = -1U;
        while (post_index < sizeof(postfix)) {
                sprintf(tmp, "%lu", num);
@@ -33,7 +44,8 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2)
 
        if (modulo == -1U) {
 done:
-               sprintf(buf, "%lu%c", num, postfix[post_index]);
+               sprintf(buf, "%lu%s%s", num, postfix[post_index],
+                       byte_postfix[byte_post_index]);
                return buf;
        }
 
@@ -53,6 +65,7 @@ done:
                modulo = (modulo + 9) / 10;
        } while (1);
 
-       sprintf(buf, "%lu.%u%c", num, modulo, postfix[post_index]);
+       sprintf(buf, "%lu.%u%s%s", num, modulo, postfix[post_index],
+               byte_postfix[byte_post_index]);
        return buf;
 }
diff --git a/stat.c b/stat.c
index 38c5d0238e33ef498c3edfceeca3bfd7740c7a4a..041efeb6faf9614419cb12ac8e01a6d956b31c4d 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -276,12 +276,12 @@ void show_group_stats(struct group_run_stats *rs)
                if (!rs->max_run[i])
                        continue;
 
-               p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p);
-               p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p);
-               p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p);
-               p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p);
+               p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p, 8);
+               p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p, 8);
+               p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p, 8);
+               p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p, 8);
 
-               log_info("%s: io=%sB, aggrb=%sB/s, minb=%sB/s, maxb=%sB/s,"
+               log_info("%s: io=%s, aggrb=%s/s, minb=%s/s, maxb=%s/s,"
                         " mint=%llumsec, maxt=%llumsec\n",
                                rs->unified_rw_rep ? "  MIXED" : ddir_str[i],
                                p1, p2, p3, p4, rs->min_run[i], rs->max_run[i]);
@@ -379,13 +379,13 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
        runt = ts->runtime[ddir];
 
        bw = (1000 * ts->io_bytes[ddir]) / runt;
-       io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p);
-       bw_p = num2str(bw, 6, 1, i2p);
+       io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p, 8);
+       bw_p = num2str(bw, 6, 1, i2p, 8);
 
        iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
-       iops_p = num2str(iops, 6, 1, 0);
+       iops_p = num2str(iops, 6, 1, 0, 0);
 
-       log_info("  %s: io=%sB, bw=%sB/s, iops=%s, runt=%6llumsec\n",
+       log_info("  %s: io=%s, bw=%s/s, iops=%s, runt=%6llumsec\n",
                                rs->unified_rw_rep ? "mixed" : ddir_str[ddir],
                                io_p, bw_p, iops_p, ts->runtime[ddir]);
 
@@ -400,8 +400,8 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
                if (!usec_to_msec(&min, &max, &mean, &dev))
                        base = "(msec)";
 
-               minp = num2str(min, 6, 1, 0);
-               maxp = num2str(max, 6, 1, 0);
+               minp = num2str(min, 6, 1, 0, 0);
+               maxp = num2str(max, 6, 1, 0, 0);
 
                log_info("    slat %s: min=%s, max=%s, avg=%5.02f,"
                         " stdev=%5.02f\n", base, minp, maxp, mean, dev);
@@ -416,8 +416,8 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
                if (!usec_to_msec(&min, &max, &mean, &dev))
                        base = "(msec)";
 
-               minp = num2str(min, 6, 1, 0);
-               maxp = num2str(max, 6, 1, 0);
+               minp = num2str(min, 6, 1, 0, 0);
+               maxp = num2str(max, 6, 1, 0, 0);
 
                log_info("    clat %s: min=%s, max=%s, avg=%5.02f,"
                         " stdev=%5.02f\n", base, minp, maxp, mean, dev);
@@ -432,8 +432,8 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
                if (!usec_to_msec(&min, &max, &mean, &dev))
                        base = "(msec)";
 
-               minp = num2str(min, 6, 1, 0);
-               maxp = num2str(max, 6, 1, 0);
+               minp = num2str(min, 6, 1, 0, 0);
+               maxp = num2str(max, 6, 1, 0, 0);
 
                log_info("     lat %s: min=%s, max=%s, avg=%5.02f,"
                         " stdev=%5.02f\n", base, minp, maxp, mean, dev);