14 * Cheesy number->string conversion, complete with carry rounding error.
16 static char *num2str(unsigned long num, int maxlen, int base, int pow2)
18 char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
19 unsigned int thousand;
30 for (i = 0; base > 1; i++)
36 len = sprintf(buf, "%'lu", num);
39 buf[len] = postfix[i];
45 if ((num % thousand) >= (thousand / 2))
56 void update_rusage_stat(struct thread_data *td)
58 struct thread_stat *ts = &td->ts;
60 getrusage(RUSAGE_SELF, &ts->ru_end);
62 ts->usr_time += mtime_since(&ts->ru_start.ru_utime,
63 &ts->ru_end.ru_utime);
64 ts->sys_time += mtime_since(&ts->ru_start.ru_stime,
65 &ts->ru_end.ru_stime);
66 ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw
67 - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw);
68 ts->minf += ts->ru_end.ru_minflt - ts->ru_start.ru_minflt;
69 ts->majf += ts->ru_end.ru_majflt - ts->ru_start.ru_majflt;
71 memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end));
74 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
75 double *mean, double *dev)
77 double n = is->samples;
85 n = (double) is->samples;
89 *dev = sqrt(is->S / (n - 1.0));
96 static void show_group_stats(struct group_run_stats *rs, int id)
98 char *p1, *p2, *p3, *p4;
99 const char *ddir_str[] = { " READ", " WRITE" };
102 log_info("\nRun status group %d (all jobs):\n", id);
104 for (i = 0; i <= DDIR_WRITE; i++) {
108 p1 = num2str(rs->io_kb[i], 6, 1024, 1);
109 p2 = num2str(rs->agg[i], 6, 1024, 1);
110 p3 = num2str(rs->min_bw[i], 6, 1024, 1);
111 p4 = num2str(rs->max_bw[i], 6, 1024, 1);
113 log_info("%s: io=%siB, aggrb=%siB/s, minb=%siB/s, maxb=%siB/s,"
114 " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
115 p3, p4, rs->min_run[i],
125 #define ts_total_io_u(ts) \
126 ((ts)->total_io_u[0] + (ts)->total_io_u[1])
128 static void stat_calc_dist(unsigned int *map, unsigned long total,
134 * Do depth distribution calculations
136 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
138 io_u_dist[i] = (double) map[i] / (double) total;
139 io_u_dist[i] *= 100.0;
140 if (io_u_dist[i] < 0.1 && map[i])
147 static void stat_calc_lat(struct thread_stat *ts, double *dst,
148 unsigned int *src, int nr)
150 unsigned long total = ts_total_io_u(ts);
154 * Do latency distribution calculations
156 for (i = 0; i < nr; i++) {
158 dst[i] = (double) src[i] / (double) total;
160 if (dst[i] < 0.01 && src[i])
167 static void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
169 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
172 static void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
174 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
177 static int usec_to_msec(unsigned long *min, unsigned long *max, double *mean,
180 if (*min > 1000 && *max > 1000 && *mean > 1000.0 && *dev > 1000.0) {
191 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
194 const char *ddir_str[] = { "read ", "write" };
195 unsigned long min, max, runt;
196 unsigned long long bw, iops;
198 char *io_p, *bw_p, *iops_p;
200 if (!ts->runtime[ddir])
203 runt = ts->runtime[ddir];
205 bw = (1000 * ts->io_bytes[ddir]) / runt;
206 io_p = num2str(ts->io_bytes[ddir] >> 10, 6, 1024, 1);
207 bw_p = num2str(bw >> 10, 6, 1024, 1);
209 iops = (1000 * ts->total_io_u[ddir]) / runt;
210 iops_p = num2str(iops, 6, 1, 0);
212 log_info(" %s: io=%siB, bw=%siB/s, iops=%s, runt=%6lumsec\n",
213 ddir_str[ddir], io_p, bw_p, iops_p,
220 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
221 const char *base = "(usec)";
224 if (!usec_to_msec(&min, &max, &mean, &dev))
227 minp = num2str(min, 6, 1, 0);
228 maxp = num2str(max, 6, 1, 0);
230 log_info(" slat %s: min=%s, max=%s, avg=%5.02f,"
231 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
236 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
237 const char *base = "(usec)";
240 if (!usec_to_msec(&min, &max, &mean, &dev))
243 minp = num2str(min, 6, 1, 0);
244 maxp = num2str(max, 6, 1, 0);
246 log_info(" clat %s: min=%s, max=%s, avg=%5.02f,"
247 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
252 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
255 p_of_agg = mean * 100 / (double) rs->agg[ddir];
256 log_info(" bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
257 " avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg,
262 static void show_lat(double *io_u_lat, int nr, const char **ranges,
265 int new_line = 1, i, line = 0;
267 for (i = 0; i < nr; i++) {
268 if (io_u_lat[i] <= 0.0)
273 log_info(" lat (%s): ", msg);
279 log_info("%s%3.2f%%", ranges[i], io_u_lat[i]);
286 static void show_lat_u(double *io_u_lat_u)
288 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
289 "250=", "500=", "750=", "1000=", };
291 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec");
294 static void show_lat_m(double *io_u_lat_m)
296 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
297 "250=", "500=", "750=", "1000=", "2000=",
300 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec");
303 static void show_latencies(double *io_u_lat_u, double *io_u_lat_m)
305 show_lat_u(io_u_lat_u);
307 show_lat_m(io_u_lat_m);
311 static void show_thread_status(struct thread_stat *ts,
312 struct group_run_stats *rs)
314 double usr_cpu, sys_cpu;
315 unsigned long runtime;
316 double io_u_dist[FIO_IO_U_MAP_NR];
317 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
318 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
320 if (!(ts->io_bytes[0] + ts->io_bytes[1]) &&
321 !(ts->total_io_u[0] + ts->total_io_u[1]))
325 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n",
326 ts->name, ts->groupid, ts->members,
327 ts->error, (int) ts->pid);
329 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n",
330 ts->name, ts->groupid, ts->members,
331 ts->error, ts->verror, (int) ts->pid);
335 log_info(" Description : [%s]\n", ts->description);
337 if (ts->io_bytes[DDIR_READ])
338 show_ddir_status(rs, ts, DDIR_READ);
339 if (ts->io_bytes[DDIR_WRITE])
340 show_ddir_status(rs, ts, DDIR_WRITE);
342 runtime = ts->total_run_time;
344 double runt = (double) runtime;
346 usr_cpu = (double) ts->usr_time * 100 / runt;
347 sys_cpu = (double) ts->sys_time * 100 / runt;
353 log_info(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu, majf=%lu,"
354 " minf=%lu\n", usr_cpu, sys_cpu, ts->ctx, ts->majf, ts->minf);
356 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
357 log_info(" IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
358 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
359 io_u_dist[1], io_u_dist[2],
360 io_u_dist[3], io_u_dist[4],
361 io_u_dist[5], io_u_dist[6]);
363 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
364 log_info(" submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
365 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
366 io_u_dist[1], io_u_dist[2],
367 io_u_dist[3], io_u_dist[4],
368 io_u_dist[5], io_u_dist[6]);
369 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
370 log_info(" complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
371 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
372 io_u_dist[1], io_u_dist[2],
373 io_u_dist[3], io_u_dist[4],
374 io_u_dist[5], io_u_dist[6]);
375 log_info(" issued r/w: total=%lu/%lu, short=%lu/%lu\n",
376 ts->total_io_u[0], ts->total_io_u[1],
377 ts->short_io_u[0], ts->short_io_u[1]);
378 stat_calc_lat_u(ts, io_u_lat_u);
379 stat_calc_lat_m(ts, io_u_lat_m);
380 show_latencies(io_u_lat_u, io_u_lat_m);
383 static void show_ddir_status_terse(struct thread_stat *ts,
384 struct group_run_stats *rs, int ddir)
386 unsigned long min, max;
387 unsigned long long bw;
391 if (ts->runtime[ddir])
392 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
394 log_info(";%llu;%llu;%lu", ts->io_bytes[ddir] >> 10, bw,
397 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
398 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
400 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
402 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
403 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
405 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
407 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
410 p_of_agg = mean * 100 / (double) rs->agg[ddir];
411 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
413 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
417 static void show_thread_status_terse(struct thread_stat *ts,
418 struct group_run_stats *rs)
420 double io_u_dist[FIO_IO_U_MAP_NR];
421 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
422 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
423 double usr_cpu, sys_cpu;
426 log_info("%s;%d;%d", ts->name, ts->groupid, ts->error);
428 show_ddir_status_terse(ts, rs, 0);
429 show_ddir_status_terse(ts, rs, 1);
431 if (ts->total_run_time) {
432 double runt = (double) ts->total_run_time;
434 usr_cpu = (double) ts->usr_time * 100 / runt;
435 sys_cpu = (double) ts->sys_time * 100 / runt;
441 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
444 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
445 stat_calc_lat_u(ts, io_u_lat_u);
446 stat_calc_lat_m(ts, io_u_lat_m);
448 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
449 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
450 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
452 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
453 log_info(";%3.2f%%", io_u_lat_u[i]);
454 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
455 log_info(";%3.2f%%", io_u_lat_m[i]);
459 log_info(";%s", ts->description);
464 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
468 dst->min_val = min(dst->min_val, src->min_val);
469 dst->max_val = max(dst->max_val, src->max_val);
470 dst->samples += src->samples;
473 * Needs a new method for calculating stddev, we cannot just
474 * average them we do below for nr > 1
480 mean = ((src->mean * (double) (nr - 1))
481 + dst->mean) / ((double) nr);
482 S = ((src->S * (double) (nr - 1)) + dst->S) / ((double) nr);
489 void show_run_stats(void)
491 struct group_run_stats *runstats, *rs;
492 struct thread_data *td;
493 struct thread_stat *threadstats, *ts;
494 int i, j, k, l, nr_ts, last_ts, idx;
496 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
498 for (i = 0; i < groupid + 1; i++) {
501 memset(rs, 0, sizeof(*rs));
502 rs->min_bw[0] = rs->min_run[0] = ~0UL;
503 rs->min_bw[1] = rs->min_run[1] = ~0UL;
507 * find out how many threads stats we need. if group reporting isn't
508 * enabled, it's one-per-td.
513 if (!td->o.group_reporting) {
517 if (last_ts == td->groupid)
520 last_ts = td->groupid;
524 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
526 for (i = 0; i < nr_ts; i++) {
527 ts = &threadstats[i];
529 memset(ts, 0, sizeof(*ts));
530 for (j = 0; j <= DDIR_WRITE; j++) {
531 ts->clat_stat[j].min_val = -1UL;
532 ts->slat_stat[j].min_val = -1UL;
533 ts->bw_stat[j].min_val = -1UL;
542 if (idx && (!td->o.group_reporting ||
543 (td->o.group_reporting && last_ts != td->groupid))) {
548 last_ts = td->groupid;
550 ts = &threadstats[j];
555 if (ts->groupid == -1) {
557 * These are per-group shared already
559 ts->name = td->o.name;
560 ts->description = td->o.description;
561 ts->groupid = td->groupid;
564 * first pid in group, not very useful...
569 if (td->error && !ts->error) {
570 ts->error = td->error;
571 ts->verror = td->verror;
574 for (l = 0; l <= DDIR_WRITE; l++) {
575 sum_stat(&ts->clat_stat[l], &td->ts.clat_stat[l], idx);
576 sum_stat(&ts->slat_stat[l], &td->ts.slat_stat[l], idx);
577 sum_stat(&ts->bw_stat[l], &td->ts.bw_stat[l], idx);
579 ts->stat_io_bytes[l] += td->ts.stat_io_bytes[l];
580 ts->io_bytes[l] += td->ts.io_bytes[l];
582 if (ts->runtime[l] < td->ts.runtime[l])
583 ts->runtime[l] = td->ts.runtime[l];
586 ts->usr_time += td->ts.usr_time;
587 ts->sys_time += td->ts.sys_time;
588 ts->ctx += td->ts.ctx;
589 ts->majf += td->ts.majf;
590 ts->minf += td->ts.minf;
592 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
593 ts->io_u_map[k] += td->ts.io_u_map[k];
594 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
595 ts->io_u_submit[k] += td->ts.io_u_submit[k];
596 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
597 ts->io_u_complete[k] += td->ts.io_u_complete[k];
598 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
599 ts->io_u_lat_u[k] += td->ts.io_u_lat_u[k];
600 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
601 ts->io_u_lat_m[k] += td->ts.io_u_lat_m[k];
604 for (k = 0; k <= DDIR_WRITE; k++) {
605 ts->total_io_u[k] += td->ts.total_io_u[k];
606 ts->short_io_u[k] += td->ts.short_io_u[k];
609 ts->total_run_time += td->ts.total_run_time;
610 ts->total_submit += td->ts.total_submit;
611 ts->total_complete += td->ts.total_complete;
614 for (i = 0; i < nr_ts; i++) {
615 unsigned long long bw;
617 ts = &threadstats[i];
618 rs = &runstats[ts->groupid];
620 for (j = 0; j <= DDIR_WRITE; j++) {
623 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
624 rs->min_run[j] = ts->runtime[j];
625 if (ts->runtime[j] > rs->max_run[j])
626 rs->max_run[j] = ts->runtime[j];
629 if (ts->runtime[j]) {
632 runt = ts->runtime[j] * 1024 / 1000;
633 bw = ts->io_bytes[j] / runt;
635 if (bw < rs->min_bw[j])
637 if (bw > rs->max_bw[j])
640 rs->io_kb[j] += ts->io_bytes[j] >> 10;
644 for (i = 0; i < groupid + 1; i++) {
645 unsigned long max_run[2];
648 max_run[0] = rs->max_run[0] * 1024 / 1000;
649 max_run[1] = rs->max_run[1] * 1024 / 1000;
652 rs->agg[0] = (rs->io_kb[0]*1024) / max_run[0];
654 rs->agg[1] = (rs->io_kb[1]*1024) / max_run[1];
658 * don't overwrite last signal output
663 for (i = 0; i < nr_ts; i++) {
664 ts = &threadstats[i];
665 rs = &runstats[ts->groupid];
668 show_thread_status_terse(ts, rs);
670 show_thread_status(ts, rs);
674 for (i = 0; i < groupid + 1; i++)
675 show_group_stats(&runstats[i], i);
684 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
689 if (data > is->max_val)
691 if (data < is->min_val)
694 delta = val - is->mean;
696 is->mean += delta / (is->samples + 1.0);
697 is->S += delta * (val - is->mean);
703 static void __add_log_sample(struct io_log *iolog, unsigned long val,
704 enum fio_ddir ddir, unsigned int bs,
707 const int nr_samples = iolog->nr_samples;
709 if (iolog->nr_samples == iolog->max_samples) {
710 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
712 iolog->log = realloc(iolog->log, new_size);
713 iolog->max_samples <<= 1;
716 iolog->log[nr_samples].val = val;
717 iolog->log[nr_samples].time = time;
718 iolog->log[nr_samples].ddir = ddir;
719 iolog->log[nr_samples].bs = bs;
723 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
724 unsigned long val, enum fio_ddir ddir,
727 __add_log_sample(iolog, val, ddir, bs, mtime_since_now(&td->epoch));
730 void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
732 struct io_log *iolog = agg_io_log[ddir];
734 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
737 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
738 unsigned long usec, unsigned int bs)
740 struct thread_stat *ts = &td->ts;
742 add_stat_sample(&ts->clat_stat[ddir], usec);
745 add_log_sample(td, ts->clat_log, usec, ddir, bs);
748 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
749 unsigned long usec, unsigned int bs)
751 struct thread_stat *ts = &td->ts;
753 add_stat_sample(&ts->slat_stat[ddir], usec);
756 add_log_sample(td, ts->slat_log, usec, ddir, bs);
759 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
762 struct thread_stat *ts = &td->ts;
763 unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t);
766 if (spent < td->o.bw_avg_time)
769 rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) * 1000 / spent / 1024;
770 add_stat_sample(&ts->bw_stat[ddir], rate);
773 add_log_sample(td, ts->bw_log, rate, ddir, bs);
775 fio_gettime(&ts->stat_sample_time[ddir], NULL);
776 ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];