Merge branch 'security-token' of https://github.com/sfc-gh-rnarubin/fio
[fio.git] / stat.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <sys/time.h>
5#include <sys/stat.h>
6#include <math.h>
7
8#include "fio.h"
9#include "diskutil.h"
10#include "lib/ieee754.h"
11#include "json.h"
12#include "lib/getrusage.h"
13#include "idletime.h"
14#include "lib/pow2.h"
15#include "lib/output_buffer.h"
16#include "helper_thread.h"
17#include "smalloc.h"
18#include "zbd.h"
19#include "oslib/asprintf.h"
20
21#ifdef WIN32
22#define LOG_MSEC_SLACK 2
23#else
24#define LOG_MSEC_SLACK 1
25#endif
26
27struct log_sample {
28 union io_sample_data data;
29 uint32_t ddir;
30 uint64_t bs;
31 uint64_t offset;
32 uint16_t priority;
33 uint64_t issue_time;
34};
35
36struct fio_sem *stat_sem;
37
38void clear_rusage_stat(struct thread_data *td)
39{
40 struct thread_stat *ts = &td->ts;
41
42 fio_getrusage(&td->ru_start);
43 ts->usr_time = ts->sys_time = 0;
44 ts->ctx = 0;
45 ts->minf = ts->majf = 0;
46}
47
48void update_rusage_stat(struct thread_data *td)
49{
50 struct thread_stat *ts = &td->ts;
51
52 fio_getrusage(&td->ru_end);
53 ts->usr_time += mtime_since_tv(&td->ru_start.ru_utime,
54 &td->ru_end.ru_utime);
55 ts->sys_time += mtime_since_tv(&td->ru_start.ru_stime,
56 &td->ru_end.ru_stime);
57 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
58 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
59 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
60 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
61
62 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
63}
64
65/*
66 * Given a latency, return the index of the corresponding bucket in
67 * the structure tracking percentiles.
68 *
69 * (1) find the group (and error bits) that the value (latency)
70 * belongs to by looking at its MSB. (2) find the bucket number in the
71 * group by looking at the index bits.
72 *
73 */
74static unsigned int plat_val_to_idx(unsigned long long val)
75{
76 unsigned int msb, error_bits, base, offset, idx;
77
78 /* Find MSB starting from bit 0 */
79 if (val == 0)
80 msb = 0;
81 else
82 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
83
84 /*
85 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
86 * all bits of the sample as index
87 */
88 if (msb <= FIO_IO_U_PLAT_BITS)
89 return val;
90
91 /* Compute the number of error bits to discard*/
92 error_bits = msb - FIO_IO_U_PLAT_BITS;
93
94 /* Compute the number of buckets before the group */
95 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
96
97 /*
98 * Discard the error bits and apply the mask to find the
99 * index for the buckets in the group
100 */
101 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
102
103 /* Make sure the index does not exceed (array size - 1) */
104 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1) ?
105 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
106
107 return idx;
108}
109
110/*
111 * Convert the given index of the bucket array to the value
112 * represented by the bucket
113 */
114static unsigned long long plat_idx_to_val(unsigned int idx)
115{
116 unsigned int error_bits;
117 unsigned long long k, base;
118
119 assert(idx < FIO_IO_U_PLAT_NR);
120
121 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
122 * all bits of the sample as index */
123 if (idx < (FIO_IO_U_PLAT_VAL << 1))
124 return idx;
125
126 /* Find the group and compute the minimum value of that group */
127 error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1;
128 base = ((unsigned long long) 1) << (error_bits + FIO_IO_U_PLAT_BITS);
129
130 /* Find its bucket number of the group */
131 k = idx % FIO_IO_U_PLAT_VAL;
132
133 /* Return the mean of the range of the bucket */
134 return base + ((k + 0.5) * (1 << error_bits));
135}
136
137static int double_cmp(const void *a, const void *b)
138{
139 const fio_fp64_t fa = *(const fio_fp64_t *) a;
140 const fio_fp64_t fb = *(const fio_fp64_t *) b;
141 int cmp = 0;
142
143 if (fa.u.f > fb.u.f)
144 cmp = 1;
145 else if (fa.u.f < fb.u.f)
146 cmp = -1;
147
148 return cmp;
149}
150
151unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
152 fio_fp64_t *plist, unsigned long long **output,
153 unsigned long long *maxv, unsigned long long *minv)
154{
155 unsigned long long sum = 0;
156 unsigned int len, i, j = 0;
157 unsigned long long *ovals = NULL;
158 bool is_last;
159
160 *minv = -1ULL;
161 *maxv = 0;
162
163 len = 0;
164 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
165 len++;
166
167 if (!len)
168 return 0;
169
170 /*
171 * Sort the percentile list. Note that it may already be sorted if
172 * we are using the default values, but since it's a short list this
173 * isn't a worry. Also note that this does not work for NaN values.
174 */
175 if (len > 1)
176 qsort(plist, len, sizeof(plist[0]), double_cmp);
177
178 ovals = malloc(len * sizeof(*ovals));
179 if (!ovals)
180 return 0;
181
182 /*
183 * Calculate bucket values, note down max and min values
184 */
185 is_last = false;
186 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
187 sum += io_u_plat[i];
188 while (sum >= ((long double) plist[j].u.f / 100.0 * nr)) {
189 assert(plist[j].u.f <= 100.0);
190
191 ovals[j] = plat_idx_to_val(i);
192 if (ovals[j] < *minv)
193 *minv = ovals[j];
194 if (ovals[j] > *maxv)
195 *maxv = ovals[j];
196
197 is_last = (j == len - 1) != 0;
198 if (is_last)
199 break;
200
201 j++;
202 }
203 }
204
205 if (!is_last)
206 log_err("fio: error calculating latency percentiles\n");
207
208 *output = ovals;
209 return len;
210}
211
212/*
213 * Find and display the p-th percentile of clat
214 */
215static void show_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
216 fio_fp64_t *plist, unsigned int precision,
217 const char *pre, struct buf_output *out)
218{
219 unsigned int divisor, len, i, j = 0;
220 unsigned long long minv, maxv;
221 unsigned long long *ovals;
222 int per_line, scale_down, time_width;
223 bool is_last;
224 char fmt[32];
225
226 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
227 if (!len || !ovals)
228 return;
229
230 /*
231 * We default to nsecs, but if the value range is such that we
232 * should scale down to usecs or msecs, do that.
233 */
234 if (minv > 2000000 && maxv > 99999999ULL) {
235 scale_down = 2;
236 divisor = 1000000;
237 log_buf(out, " %s percentiles (msec):\n |", pre);
238 } else if (minv > 2000 && maxv > 99999) {
239 scale_down = 1;
240 divisor = 1000;
241 log_buf(out, " %s percentiles (usec):\n |", pre);
242 } else {
243 scale_down = 0;
244 divisor = 1;
245 log_buf(out, " %s percentiles (nsec):\n |", pre);
246 }
247
248
249 time_width = max(5, (int) (log10(maxv / divisor) + 1));
250 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
251 precision, time_width);
252 /* fmt will be something like " %5.2fth=[%4llu]%c" */
253 per_line = (80 - 7) / (precision + 10 + time_width);
254
255 for (j = 0; j < len; j++) {
256 /* for formatting */
257 if (j != 0 && (j % per_line) == 0)
258 log_buf(out, " |");
259
260 /* end of the list */
261 is_last = (j == len - 1) != 0;
262
263 for (i = 0; i < scale_down; i++)
264 ovals[j] = (ovals[j] + 999) / 1000;
265
266 log_buf(out, fmt, plist[j].u.f, ovals[j], is_last ? '\n' : ',');
267
268 if (is_last)
269 break;
270
271 if ((j % per_line) == per_line - 1) /* for formatting */
272 log_buf(out, "\n");
273 }
274
275 free(ovals);
276}
277
278static int get_nr_prios_with_samples(struct thread_stat *ts, enum fio_ddir ddir)
279{
280 int i, nr_prios_with_samples = 0;
281
282 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
283 if (ts->clat_prio[ddir][i].clat_stat.samples)
284 nr_prios_with_samples++;
285 }
286
287 return nr_prios_with_samples;
288}
289
290bool calc_lat(struct io_stat *is, unsigned long long *min,
291 unsigned long long *max, double *mean, double *dev)
292{
293 double n = (double) is->samples;
294
295 if (n == 0)
296 return false;
297
298 *min = is->min_val;
299 *max = is->max_val;
300 *mean = is->mean.u.f;
301
302 if (n > 1.0)
303 *dev = sqrt(is->S.u.f / (n - 1.0));
304 else
305 *dev = 0;
306
307 return true;
308}
309
310void show_mixed_group_stats(struct group_run_stats *rs, struct buf_output *out)
311{
312 char *io, *agg, *min, *max;
313 char *ioalt, *aggalt, *minalt, *maxalt;
314 uint64_t io_mix = 0, agg_mix = 0, min_mix = -1, max_mix = 0;
315 uint64_t min_run = -1, max_run = 0;
316 const int i2p = is_power_of_2(rs->kb_base);
317 int i;
318
319 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
320 if (!rs->max_run[i])
321 continue;
322 io_mix += rs->iobytes[i];
323 agg_mix += rs->agg[i];
324 min_mix = min_mix < rs->min_bw[i] ? min_mix : rs->min_bw[i];
325 max_mix = max_mix > rs->max_bw[i] ? max_mix : rs->max_bw[i];
326 min_run = min_run < rs->min_run[i] ? min_run : rs->min_run[i];
327 max_run = max_run > rs->max_run[i] ? max_run : rs->max_run[i];
328 }
329 io = num2str(io_mix, rs->sig_figs, 1, i2p, N2S_BYTE);
330 ioalt = num2str(io_mix, rs->sig_figs, 1, !i2p, N2S_BYTE);
331 agg = num2str(agg_mix, rs->sig_figs, 1, i2p, rs->unit_base);
332 aggalt = num2str(agg_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
333 min = num2str(min_mix, rs->sig_figs, 1, i2p, rs->unit_base);
334 minalt = num2str(min_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
335 max = num2str(max_mix, rs->sig_figs, 1, i2p, rs->unit_base);
336 maxalt = num2str(max_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
337 log_buf(out, " MIXED: bw=%s (%s), %s-%s (%s-%s), io=%s (%s), run=%llu-%llumsec\n",
338 agg, aggalt, min, max, minalt, maxalt, io, ioalt,
339 (unsigned long long) min_run,
340 (unsigned long long) max_run);
341 free(io);
342 free(agg);
343 free(min);
344 free(max);
345 free(ioalt);
346 free(aggalt);
347 free(minalt);
348 free(maxalt);
349}
350
351void show_group_stats(struct group_run_stats *rs, struct buf_output *out)
352{
353 char *io, *agg, *min, *max;
354 char *ioalt, *aggalt, *minalt, *maxalt;
355 const char *str[] = { " READ", " WRITE" , " TRIM"};
356 int i;
357
358 log_buf(out, "\nRun status group %d (all jobs):\n", rs->groupid);
359
360 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
361 const int i2p = is_power_of_2(rs->kb_base);
362
363 if (!rs->max_run[i])
364 continue;
365
366 io = num2str(rs->iobytes[i], rs->sig_figs, 1, i2p, N2S_BYTE);
367 ioalt = num2str(rs->iobytes[i], rs->sig_figs, 1, !i2p, N2S_BYTE);
368 agg = num2str(rs->agg[i], rs->sig_figs, 1, i2p, rs->unit_base);
369 aggalt = num2str(rs->agg[i], rs->sig_figs, 1, !i2p, rs->unit_base);
370 min = num2str(rs->min_bw[i], rs->sig_figs, 1, i2p, rs->unit_base);
371 minalt = num2str(rs->min_bw[i], rs->sig_figs, 1, !i2p, rs->unit_base);
372 max = num2str(rs->max_bw[i], rs->sig_figs, 1, i2p, rs->unit_base);
373 maxalt = num2str(rs->max_bw[i], rs->sig_figs, 1, !i2p, rs->unit_base);
374 log_buf(out, "%s: bw=%s (%s), %s-%s (%s-%s), io=%s (%s), run=%llu-%llumsec\n",
375 (rs->unified_rw_rep == UNIFIED_MIXED) ? " MIXED" : str[i],
376 agg, aggalt, min, max, minalt, maxalt, io, ioalt,
377 (unsigned long long) rs->min_run[i],
378 (unsigned long long) rs->max_run[i]);
379
380 free(io);
381 free(agg);
382 free(min);
383 free(max);
384 free(ioalt);
385 free(aggalt);
386 free(minalt);
387 free(maxalt);
388 }
389
390 /* Need to aggregate statistics to show mixed values */
391 if (rs->unified_rw_rep == UNIFIED_BOTH)
392 show_mixed_group_stats(rs, out);
393}
394
395void stat_calc_dist(uint64_t *map, unsigned long total, double *io_u_dist)
396{
397 int i;
398
399 /*
400 * Do depth distribution calculations
401 */
402 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
403 if (total) {
404 io_u_dist[i] = (double) map[i] / (double) total;
405 io_u_dist[i] *= 100.0;
406 if (io_u_dist[i] < 0.1 && map[i])
407 io_u_dist[i] = 0.1;
408 } else
409 io_u_dist[i] = 0.0;
410 }
411}
412
413static void stat_calc_lat(struct thread_stat *ts, double *dst,
414 uint64_t *src, int nr)
415{
416 unsigned long total = ddir_rw_sum(ts->total_io_u);
417 int i;
418
419 /*
420 * Do latency distribution calculations
421 */
422 for (i = 0; i < nr; i++) {
423 if (total) {
424 dst[i] = (double) src[i] / (double) total;
425 dst[i] *= 100.0;
426 if (dst[i] < 0.01 && src[i])
427 dst[i] = 0.01;
428 } else
429 dst[i] = 0.0;
430 }
431}
432
433/*
434 * To keep the terse format unaltered, add all of the ns latency
435 * buckets to the first us latency bucket
436 */
437static void stat_calc_lat_nu(struct thread_stat *ts, double *io_u_lat_u)
438{
439 unsigned long ntotal = 0, total = ddir_rw_sum(ts->total_io_u);
440 int i;
441
442 stat_calc_lat(ts, io_u_lat_u, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
443
444 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
445 ntotal += ts->io_u_lat_n[i];
446
447 io_u_lat_u[0] += 100.0 * (double) ntotal / (double) total;
448}
449
450void stat_calc_lat_n(struct thread_stat *ts, double *io_u_lat)
451{
452 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_n, FIO_IO_U_LAT_N_NR);
453}
454
455void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
456{
457 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
458}
459
460void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
461{
462 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
463}
464
465static void display_lat(const char *name, unsigned long long min,
466 unsigned long long max, double mean, double dev,
467 struct buf_output *out)
468{
469 const char *base = "(nsec)";
470 char *minp, *maxp;
471
472 if (nsec_to_msec(&min, &max, &mean, &dev))
473 base = "(msec)";
474 else if (nsec_to_usec(&min, &max, &mean, &dev))
475 base = "(usec)";
476
477 minp = num2str(min, 6, 1, 0, N2S_NONE);
478 maxp = num2str(max, 6, 1, 0, N2S_NONE);
479
480 log_buf(out, " %s %s: min=%s, max=%s, avg=%5.02f,"
481 " stdev=%5.02f\n", name, base, minp, maxp, mean, dev);
482
483 free(minp);
484 free(maxp);
485}
486
487static struct thread_stat *gen_mixed_ddir_stats_from_ts(struct thread_stat *ts)
488{
489 struct thread_stat *ts_lcl;
490
491 /*
492 * Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and
493 * Trims (ddir = 2)
494 */
495 ts_lcl = malloc(sizeof(struct thread_stat));
496 if (!ts_lcl) {
497 log_err("fio: failed to allocate local thread stat\n");
498 return NULL;
499 }
500
501 init_thread_stat(ts_lcl);
502
503 /* calculate mixed stats */
504 ts_lcl->unified_rw_rep = UNIFIED_MIXED;
505 ts_lcl->lat_percentiles = ts->lat_percentiles;
506 ts_lcl->clat_percentiles = ts->clat_percentiles;
507 ts_lcl->slat_percentiles = ts->slat_percentiles;
508 ts_lcl->percentile_precision = ts->percentile_precision;
509 memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
510 ts_lcl->sig_figs = ts->sig_figs;
511
512 sum_thread_stats(ts_lcl, ts);
513
514 return ts_lcl;
515}
516
517static double convert_agg_kbytes_percent(struct group_run_stats *rs,
518 enum fio_ddir ddir, int mean)
519{
520 double p_of_agg = 100.0;
521 if (rs && rs->agg[ddir] > 1024) {
522 p_of_agg = mean * 100.0 / (double) (rs->agg[ddir] / 1024.0);
523
524 if (p_of_agg > 100.0)
525 p_of_agg = 100.0;
526 }
527 return p_of_agg;
528}
529
530static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
531 enum fio_ddir ddir, struct buf_output *out)
532{
533 unsigned long runt;
534 unsigned long long min, max, bw, iops;
535 double mean, dev;
536 char *io_p, *bw_p, *bw_p_alt, *iops_p, *post_st = NULL;
537 int i2p, i;
538 const char *clat_type = ts->lat_percentiles ? "lat" : "clat";
539
540 if (ddir_sync(ddir)) {
541 if (calc_lat(&ts->sync_stat, &min, &max, &mean, &dev)) {
542 log_buf(out, " %s:\n", "fsync/fdatasync/sync_file_range");
543 display_lat(io_ddir_name(ddir), min, max, mean, dev, out);
544 show_clat_percentiles(ts->io_u_sync_plat,
545 ts->sync_stat.samples,
546 ts->percentile_list,
547 ts->percentile_precision,
548 io_ddir_name(ddir), out);
549 }
550 return;
551 }
552
553 assert(ddir_rw(ddir));
554
555 if (!ts->runtime[ddir])
556 return;
557
558 i2p = is_power_of_2(rs->kb_base);
559 runt = ts->runtime[ddir];
560
561 bw = (1000 * ts->io_bytes[ddir]) / runt;
562 io_p = num2str(ts->io_bytes[ddir], ts->sig_figs, 1, i2p, N2S_BYTE);
563 bw_p = num2str(bw, ts->sig_figs, 1, i2p, ts->unit_base);
564 bw_p_alt = num2str(bw, ts->sig_figs, 1, !i2p, ts->unit_base);
565
566 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
567 iops_p = num2str(iops, ts->sig_figs, 1, 0, N2S_NONE);
568 if (ddir == DDIR_WRITE || ddir == DDIR_TRIM)
569 post_st = zbd_write_status(ts);
570 else if (ddir == DDIR_READ && ts->cachehit && ts->cachemiss) {
571 uint64_t total;
572 double hit;
573
574 total = ts->cachehit + ts->cachemiss;
575 hit = (double) ts->cachehit / (double) total;
576 hit *= 100.0;
577 if (asprintf(&post_st, "; Cachehit=%0.2f%%", hit) < 0)
578 post_st = NULL;
579 }
580
581 log_buf(out, " %s: IOPS=%s, BW=%s (%s)(%s/%llumsec)%s\n",
582 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir),
583 iops_p, bw_p, bw_p_alt, io_p,
584 (unsigned long long) ts->runtime[ddir],
585 post_st ? : "");
586
587 free(post_st);
588 free(io_p);
589 free(bw_p);
590 free(bw_p_alt);
591 free(iops_p);
592
593 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
594 display_lat("slat", min, max, mean, dev, out);
595 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
596 display_lat("clat", min, max, mean, dev, out);
597 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
598 display_lat(" lat", min, max, mean, dev, out);
599
600 /* Only print per prio stats if there are >= 2 prios with samples */
601 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
602 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
603 char buf[64];
604
605 if (!calc_lat(&ts->clat_prio[ddir][i].clat_stat, &min,
606 &max, &mean, &dev))
607 continue;
608
609 snprintf(buf, sizeof(buf),
610 "%s prio %u/%u/%u",
611 clat_type,
612 ioprio_class(ts->clat_prio[ddir][i].ioprio),
613 ioprio(ts->clat_prio[ddir][i].ioprio),
614 ioprio_hint(ts->clat_prio[ddir][i].ioprio));
615 display_lat(buf, min, max, mean, dev, out);
616 }
617 }
618
619 if (ts->slat_percentiles && ts->slat_stat[ddir].samples > 0)
620 show_clat_percentiles(ts->io_u_plat[FIO_SLAT][ddir],
621 ts->slat_stat[ddir].samples,
622 ts->percentile_list,
623 ts->percentile_precision, "slat", out);
624 if (ts->clat_percentiles && ts->clat_stat[ddir].samples > 0)
625 show_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
626 ts->clat_stat[ddir].samples,
627 ts->percentile_list,
628 ts->percentile_precision, "clat", out);
629 if (ts->lat_percentiles && ts->lat_stat[ddir].samples > 0)
630 show_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
631 ts->lat_stat[ddir].samples,
632 ts->percentile_list,
633 ts->percentile_precision, "lat", out);
634
635 if (ts->clat_percentiles || ts->lat_percentiles) {
636 char prio_name[64];
637 uint64_t samples;
638
639 if (ts->lat_percentiles)
640 samples = ts->lat_stat[ddir].samples;
641 else
642 samples = ts->clat_stat[ddir].samples;
643
644 /* Only print per prio stats if there are >= 2 prios with samples */
645 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
646 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
647 uint64_t prio_samples =
648 ts->clat_prio[ddir][i].clat_stat.samples;
649
650 if (!prio_samples)
651 continue;
652
653 snprintf(prio_name, sizeof(prio_name),
654 "%s prio %u/%u/%u (%.2f%% of IOs)",
655 clat_type,
656 ioprio_class(ts->clat_prio[ddir][i].ioprio),
657 ioprio(ts->clat_prio[ddir][i].ioprio),
658 ioprio_hint(ts->clat_prio[ddir][i].ioprio),
659 100. * (double) prio_samples / (double) samples);
660 show_clat_percentiles(ts->clat_prio[ddir][i].io_u_plat,
661 prio_samples, ts->percentile_list,
662 ts->percentile_precision,
663 prio_name, out);
664 }
665 }
666 }
667
668 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
669 double p_of_agg = 100.0, fkb_base = (double)rs->kb_base;
670 const char *bw_str;
671
672 if ((rs->unit_base == 1) && i2p)
673 bw_str = "Kibit";
674 else if (rs->unit_base == 1)
675 bw_str = "kbit";
676 else if (i2p)
677 bw_str = "KiB";
678 else
679 bw_str = "kB";
680
681 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
682
683 if (rs->unit_base == 1) {
684 min *= 8.0;
685 max *= 8.0;
686 mean *= 8.0;
687 dev *= 8.0;
688 }
689
690 if (mean > fkb_base * fkb_base) {
691 min /= fkb_base;
692 max /= fkb_base;
693 mean /= fkb_base;
694 dev /= fkb_base;
695 bw_str = (rs->unit_base == 1 ? "Mibit" : "MiB");
696 }
697
698 log_buf(out, " bw (%5s/s): min=%5llu, max=%5llu, per=%3.2f%%, "
699 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
700 bw_str, min, max, p_of_agg, mean, dev,
701 (&ts->bw_stat[ddir])->samples);
702 }
703 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
704 log_buf(out, " iops : min=%5llu, max=%5llu, "
705 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
706 min, max, mean, dev, (&ts->iops_stat[ddir])->samples);
707 }
708}
709
710static void show_mixed_ddir_status(struct group_run_stats *rs,
711 struct thread_stat *ts,
712 struct buf_output *out)
713{
714 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
715
716 if (ts_lcl)
717 show_ddir_status(rs, ts_lcl, DDIR_READ, out);
718
719 free_clat_prio_stats(ts_lcl);
720 free(ts_lcl);
721}
722
723static bool show_lat(double *io_u_lat, int nr, const char **ranges,
724 const char *msg, struct buf_output *out)
725{
726 bool new_line = true, shown = false;
727 int i, line = 0;
728
729 for (i = 0; i < nr; i++) {
730 if (io_u_lat[i] <= 0.0)
731 continue;
732 shown = true;
733 if (new_line) {
734 if (line)
735 log_buf(out, "\n");
736 log_buf(out, " lat (%s) : ", msg);
737 new_line = false;
738 line = 0;
739 }
740 if (line)
741 log_buf(out, ", ");
742 log_buf(out, "%s%3.2f%%", ranges[i], io_u_lat[i]);
743 line++;
744 if (line == 5)
745 new_line = true;
746 }
747
748 if (shown)
749 log_buf(out, "\n");
750
751 return true;
752}
753
754static void show_lat_n(double *io_u_lat_n, struct buf_output *out)
755{
756 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
757 "250=", "500=", "750=", "1000=", };
758
759 show_lat(io_u_lat_n, FIO_IO_U_LAT_N_NR, ranges, "nsec", out);
760}
761
762static void show_lat_u(double *io_u_lat_u, struct buf_output *out)
763{
764 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
765 "250=", "500=", "750=", "1000=", };
766
767 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec", out);
768}
769
770static void show_lat_m(double *io_u_lat_m, struct buf_output *out)
771{
772 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
773 "250=", "500=", "750=", "1000=", "2000=",
774 ">=2000=", };
775
776 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec", out);
777}
778
779static void show_latencies(struct thread_stat *ts, struct buf_output *out)
780{
781 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
782 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
783 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
784
785 stat_calc_lat_n(ts, io_u_lat_n);
786 stat_calc_lat_u(ts, io_u_lat_u);
787 stat_calc_lat_m(ts, io_u_lat_m);
788
789 show_lat_n(io_u_lat_n, out);
790 show_lat_u(io_u_lat_u, out);
791 show_lat_m(io_u_lat_m, out);
792}
793
794static int block_state_category(int block_state)
795{
796 switch (block_state) {
797 case BLOCK_STATE_UNINIT:
798 return 0;
799 case BLOCK_STATE_TRIMMED:
800 case BLOCK_STATE_WRITTEN:
801 return 1;
802 case BLOCK_STATE_WRITE_FAILURE:
803 case BLOCK_STATE_TRIM_FAILURE:
804 return 2;
805 default:
806 /* Silence compile warning on some BSDs and have a return */
807 assert(0);
808 return -1;
809 }
810}
811
812static int compare_block_infos(const void *bs1, const void *bs2)
813{
814 uint64_t block1 = *(uint64_t *)bs1;
815 uint64_t block2 = *(uint64_t *)bs2;
816 int state1 = BLOCK_INFO_STATE(block1);
817 int state2 = BLOCK_INFO_STATE(block2);
818 int bscat1 = block_state_category(state1);
819 int bscat2 = block_state_category(state2);
820 int cycles1 = BLOCK_INFO_TRIMS(block1);
821 int cycles2 = BLOCK_INFO_TRIMS(block2);
822
823 if (bscat1 < bscat2)
824 return -1;
825 if (bscat1 > bscat2)
826 return 1;
827
828 if (cycles1 < cycles2)
829 return -1;
830 if (cycles1 > cycles2)
831 return 1;
832
833 if (state1 < state2)
834 return -1;
835 if (state1 > state2)
836 return 1;
837
838 assert(block1 == block2);
839 return 0;
840}
841
842static int calc_block_percentiles(int nr_block_infos, uint32_t *block_infos,
843 fio_fp64_t *plist, unsigned int **percentiles,
844 unsigned int *types)
845{
846 int len = 0;
847 int i, nr_uninit;
848
849 qsort(block_infos, nr_block_infos, sizeof(uint32_t), compare_block_infos);
850
851 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
852 len++;
853
854 if (!len)
855 return 0;
856
857 /*
858 * Sort the percentile list. Note that it may already be sorted if
859 * we are using the default values, but since it's a short list this
860 * isn't a worry. Also note that this does not work for NaN values.
861 */
862 if (len > 1)
863 qsort(plist, len, sizeof(plist[0]), double_cmp);
864
865 /* Start only after the uninit entries end */
866 for (nr_uninit = 0;
867 nr_uninit < nr_block_infos
868 && BLOCK_INFO_STATE(block_infos[nr_uninit]) == BLOCK_STATE_UNINIT;
869 nr_uninit ++)
870 ;
871
872 if (nr_uninit == nr_block_infos)
873 return 0;
874
875 *percentiles = calloc(len, sizeof(**percentiles));
876
877 for (i = 0; i < len; i++) {
878 int idx = (plist[i].u.f * (nr_block_infos - nr_uninit) / 100)
879 + nr_uninit;
880 (*percentiles)[i] = BLOCK_INFO_TRIMS(block_infos[idx]);
881 }
882
883 memset(types, 0, sizeof(*types) * BLOCK_STATE_COUNT);
884 for (i = 0; i < nr_block_infos; i++)
885 types[BLOCK_INFO_STATE(block_infos[i])]++;
886
887 return len;
888}
889
890static const char *block_state_names[] = {
891 [BLOCK_STATE_UNINIT] = "unwritten",
892 [BLOCK_STATE_TRIMMED] = "trimmed",
893 [BLOCK_STATE_WRITTEN] = "written",
894 [BLOCK_STATE_TRIM_FAILURE] = "trim failure",
895 [BLOCK_STATE_WRITE_FAILURE] = "write failure",
896};
897
898static void show_block_infos(int nr_block_infos, uint32_t *block_infos,
899 fio_fp64_t *plist, struct buf_output *out)
900{
901 int len, pos, i;
902 unsigned int *percentiles = NULL;
903 unsigned int block_state_counts[BLOCK_STATE_COUNT];
904
905 len = calc_block_percentiles(nr_block_infos, block_infos, plist,
906 &percentiles, block_state_counts);
907
908 log_buf(out, " block lifetime percentiles :\n |");
909 pos = 0;
910 for (i = 0; i < len; i++) {
911 uint32_t block_info = percentiles[i];
912#define LINE_LENGTH 75
913 char str[LINE_LENGTH];
914 int strln = snprintf(str, LINE_LENGTH, " %3.2fth=%u%c",
915 plist[i].u.f, block_info,
916 i == len - 1 ? '\n' : ',');
917 assert(strln < LINE_LENGTH);
918 if (pos + strln > LINE_LENGTH) {
919 pos = 0;
920 log_buf(out, "\n |");
921 }
922 log_buf(out, "%s", str);
923 pos += strln;
924#undef LINE_LENGTH
925 }
926 if (percentiles)
927 free(percentiles);
928
929 log_buf(out, " states :");
930 for (i = 0; i < BLOCK_STATE_COUNT; i++)
931 log_buf(out, " %s=%u%c",
932 block_state_names[i], block_state_counts[i],
933 i == BLOCK_STATE_COUNT - 1 ? '\n' : ',');
934}
935
936static void show_ss_normal(struct thread_stat *ts, struct buf_output *out)
937{
938 char *p1, *p1alt, *p2;
939 unsigned long long bw_mean, iops_mean;
940 const int i2p = is_power_of_2(ts->kb_base);
941
942 if (!ts->ss_dur)
943 return;
944
945 bw_mean = steadystate_bw_mean(ts);
946 iops_mean = steadystate_iops_mean(ts);
947
948 p1 = num2str(bw_mean / ts->kb_base, ts->sig_figs, ts->kb_base, i2p, ts->unit_base);
949 p1alt = num2str(bw_mean / ts->kb_base, ts->sig_figs, ts->kb_base, !i2p, ts->unit_base);
950 p2 = num2str(iops_mean, ts->sig_figs, 1, 0, N2S_NONE);
951
952 log_buf(out, " steadystate : attained=%s, bw=%s (%s), iops=%s, %s%s=%.3f%s\n",
953 ts->ss_state & FIO_SS_ATTAINED ? "yes" : "no",
954 p1, p1alt, p2,
955 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
956 ts->ss_state & FIO_SS_SLOPE ? " slope": " mean dev",
957 ts->ss_criterion.u.f,
958 ts->ss_state & FIO_SS_PCT ? "%" : "");
959
960 free(p1);
961 free(p1alt);
962 free(p2);
963}
964
965static void show_agg_stats(struct disk_util_agg *agg, int terse,
966 struct buf_output *out)
967{
968 if (!agg->slavecount)
969 return;
970
971 if (!terse) {
972 log_buf(out, ", aggrios=%llu/%llu, aggsectors=%llu/%llu, "
973 "aggrmerge=%llu/%llu, aggrticks=%llu/%llu, "
974 "aggrin_queue=%llu, aggrutil=%3.2f%%",
975 (unsigned long long) agg->ios[0] / agg->slavecount,
976 (unsigned long long) agg->ios[1] / agg->slavecount,
977 (unsigned long long) agg->sectors[0] / agg->slavecount,
978 (unsigned long long) agg->sectors[1] / agg->slavecount,
979 (unsigned long long) agg->merges[0] / agg->slavecount,
980 (unsigned long long) agg->merges[1] / agg->slavecount,
981 (unsigned long long) agg->ticks[0] / agg->slavecount,
982 (unsigned long long) agg->ticks[1] / agg->slavecount,
983 (unsigned long long) agg->time_in_queue / agg->slavecount,
984 agg->max_util.u.f);
985 } else {
986 log_buf(out, ";slaves;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
987 (unsigned long long) agg->ios[0] / agg->slavecount,
988 (unsigned long long) agg->ios[1] / agg->slavecount,
989 (unsigned long long) agg->merges[0] / agg->slavecount,
990 (unsigned long long) agg->merges[1] / agg->slavecount,
991 (unsigned long long) agg->ticks[0] / agg->slavecount,
992 (unsigned long long) agg->ticks[1] / agg->slavecount,
993 (unsigned long long) agg->time_in_queue / agg->slavecount,
994 agg->max_util.u.f);
995 }
996}
997
998static void aggregate_slaves_stats(struct disk_util *masterdu)
999{
1000 struct disk_util_agg *agg = &masterdu->agg;
1001 struct disk_util_stat *dus;
1002 struct flist_head *entry;
1003 struct disk_util *slavedu;
1004 double util;
1005
1006 flist_for_each(entry, &masterdu->slaves) {
1007 slavedu = flist_entry(entry, struct disk_util, slavelist);
1008 dus = &slavedu->dus;
1009 agg->ios[0] += dus->s.ios[0];
1010 agg->ios[1] += dus->s.ios[1];
1011 agg->merges[0] += dus->s.merges[0];
1012 agg->merges[1] += dus->s.merges[1];
1013 agg->sectors[0] += dus->s.sectors[0];
1014 agg->sectors[1] += dus->s.sectors[1];
1015 agg->ticks[0] += dus->s.ticks[0];
1016 agg->ticks[1] += dus->s.ticks[1];
1017 agg->time_in_queue += dus->s.time_in_queue;
1018 agg->slavecount++;
1019
1020 util = (double) (100 * dus->s.io_ticks / (double) slavedu->dus.s.msec);
1021 /* System utilization is the utilization of the
1022 * component with the highest utilization.
1023 */
1024 if (util > agg->max_util.u.f)
1025 agg->max_util.u.f = util;
1026
1027 }
1028
1029 if (agg->max_util.u.f > 100.0)
1030 agg->max_util.u.f = 100.0;
1031}
1032
1033void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
1034 int terse, struct buf_output *out)
1035{
1036 double util = 0;
1037
1038 if (dus->s.msec)
1039 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1040 if (util > 100.0)
1041 util = 100.0;
1042
1043 if (!terse) {
1044 if (agg->slavecount)
1045 log_buf(out, " ");
1046
1047 log_buf(out, " %s: ios=%llu/%llu, sectors=%llu/%llu, "
1048 "merge=%llu/%llu, ticks=%llu/%llu, in_queue=%llu, "
1049 "util=%3.2f%%",
1050 dus->name,
1051 (unsigned long long) dus->s.ios[0],
1052 (unsigned long long) dus->s.ios[1],
1053 (unsigned long long) dus->s.sectors[0],
1054 (unsigned long long) dus->s.sectors[1],
1055 (unsigned long long) dus->s.merges[0],
1056 (unsigned long long) dus->s.merges[1],
1057 (unsigned long long) dus->s.ticks[0],
1058 (unsigned long long) dus->s.ticks[1],
1059 (unsigned long long) dus->s.time_in_queue,
1060 util);
1061 } else {
1062 log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
1063 dus->name,
1064 (unsigned long long) dus->s.ios[0],
1065 (unsigned long long) dus->s.ios[1],
1066 (unsigned long long) dus->s.merges[0],
1067 (unsigned long long) dus->s.merges[1],
1068 (unsigned long long) dus->s.ticks[0],
1069 (unsigned long long) dus->s.ticks[1],
1070 (unsigned long long) dus->s.time_in_queue,
1071 util);
1072 }
1073
1074 /*
1075 * If the device has slaves, aggregate the stats for
1076 * those slave devices also.
1077 */
1078 show_agg_stats(agg, terse, out);
1079
1080 if (!terse)
1081 log_buf(out, "\n");
1082}
1083
1084void json_array_add_disk_util(struct disk_util_stat *dus,
1085 struct disk_util_agg *agg, struct json_array *array)
1086{
1087 struct json_object *obj;
1088 double util = 0;
1089
1090 if (dus->s.msec)
1091 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1092 if (util > 100.0)
1093 util = 100.0;
1094
1095 obj = json_create_object();
1096 json_array_add_value_object(array, obj);
1097
1098 json_object_add_value_string(obj, "name", (const char *)dus->name);
1099 json_object_add_value_int(obj, "read_ios", dus->s.ios[0]);
1100 json_object_add_value_int(obj, "write_ios", dus->s.ios[1]);
1101 json_object_add_value_int(obj, "read_sectors", dus->s.sectors[0]);
1102 json_object_add_value_int(obj, "write_sectors", dus->s.sectors[1]);
1103 json_object_add_value_int(obj, "read_merges", dus->s.merges[0]);
1104 json_object_add_value_int(obj, "write_merges", dus->s.merges[1]);
1105 json_object_add_value_int(obj, "read_ticks", dus->s.ticks[0]);
1106 json_object_add_value_int(obj, "write_ticks", dus->s.ticks[1]);
1107 json_object_add_value_int(obj, "in_queue", dus->s.time_in_queue);
1108 json_object_add_value_float(obj, "util", util);
1109
1110 /*
1111 * If the device has slaves, aggregate the stats for
1112 * those slave devices also.
1113 */
1114 if (!agg->slavecount)
1115 return;
1116 json_object_add_value_int(obj, "aggr_read_ios",
1117 agg->ios[0] / agg->slavecount);
1118 json_object_add_value_int(obj, "aggr_write_ios",
1119 agg->ios[1] / agg->slavecount);
1120 json_object_add_value_int(obj, "aggr_read_sectors",
1121 agg->sectors[0] / agg->slavecount);
1122 json_object_add_value_int(obj, "aggr_write_sectors",
1123 agg->sectors[1] / agg->slavecount);
1124 json_object_add_value_int(obj, "aggr_read_merges",
1125 agg->merges[0] / agg->slavecount);
1126 json_object_add_value_int(obj, "aggr_write_merge",
1127 agg->merges[1] / agg->slavecount);
1128 json_object_add_value_int(obj, "aggr_read_ticks",
1129 agg->ticks[0] / agg->slavecount);
1130 json_object_add_value_int(obj, "aggr_write_ticks",
1131 agg->ticks[1] / agg->slavecount);
1132 json_object_add_value_int(obj, "aggr_in_queue",
1133 agg->time_in_queue / agg->slavecount);
1134 json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
1135}
1136
1137static void json_object_add_disk_utils(struct json_object *obj,
1138 struct flist_head *head)
1139{
1140 struct json_array *array = json_create_array();
1141 struct flist_head *entry;
1142 struct disk_util *du;
1143
1144 json_object_add_value_array(obj, "disk_util", array);
1145
1146 flist_for_each(entry, head) {
1147 du = flist_entry(entry, struct disk_util, list);
1148
1149 aggregate_slaves_stats(du);
1150 json_array_add_disk_util(&du->dus, &du->agg, array);
1151 }
1152}
1153
1154void show_disk_util(int terse, struct json_object *parent,
1155 struct buf_output *out)
1156{
1157 struct flist_head *entry;
1158 struct disk_util *du;
1159 bool do_json;
1160
1161 if (!is_running_backend())
1162 return;
1163
1164 if (flist_empty(&disk_list))
1165 return;
1166
1167 if ((output_format & FIO_OUTPUT_JSON) && parent)
1168 do_json = true;
1169 else
1170 do_json = false;
1171
1172 if (!terse && !do_json)
1173 log_buf(out, "\nDisk stats (read/write):\n");
1174
1175 if (do_json) {
1176 json_object_add_disk_utils(parent, &disk_list);
1177 } else if (output_format & ~(FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS)) {
1178 flist_for_each(entry, &disk_list) {
1179 du = flist_entry(entry, struct disk_util, list);
1180
1181 aggregate_slaves_stats(du);
1182 print_disk_util(&du->dus, &du->agg, terse, out);
1183 }
1184 }
1185}
1186
1187static void show_thread_status_normal(struct thread_stat *ts,
1188 struct group_run_stats *rs,
1189 struct buf_output *out)
1190{
1191 double usr_cpu, sys_cpu;
1192 unsigned long runtime;
1193 double io_u_dist[FIO_IO_U_MAP_NR];
1194 time_t time_p;
1195 char time_buf[32];
1196
1197 if (!ddir_rw_sum(ts->io_bytes) && !ddir_rw_sum(ts->total_io_u))
1198 return;
1199
1200 memset(time_buf, 0, sizeof(time_buf));
1201
1202 time(&time_p);
1203 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
1204
1205 if (!ts->error) {
1206 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
1207 ts->name, ts->groupid, ts->members,
1208 ts->error, (int) ts->pid, time_buf);
1209 } else {
1210 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
1211 ts->name, ts->groupid, ts->members,
1212 ts->error, ts->verror, (int) ts->pid,
1213 time_buf);
1214 }
1215
1216 if (strlen(ts->description))
1217 log_buf(out, " Description : [%s]\n", ts->description);
1218
1219 for_each_rw_ddir(ddir) {
1220 if (ts->io_bytes[ddir])
1221 show_ddir_status(rs, ts, ddir, out);
1222 }
1223
1224 if (ts->unified_rw_rep == UNIFIED_BOTH)
1225 show_mixed_ddir_status(rs, ts, out);
1226
1227 show_latencies(ts, out);
1228
1229 if (ts->sync_stat.samples)
1230 show_ddir_status(rs, ts, DDIR_SYNC, out);
1231
1232 runtime = ts->total_run_time;
1233 if (runtime) {
1234 double runt = (double) runtime;
1235
1236 usr_cpu = (double) ts->usr_time * 100 / runt;
1237 sys_cpu = (double) ts->sys_time * 100 / runt;
1238 } else {
1239 usr_cpu = 0;
1240 sys_cpu = 0;
1241 }
1242
1243 log_buf(out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%llu,"
1244 " majf=%llu, minf=%llu\n", usr_cpu, sys_cpu,
1245 (unsigned long long) ts->ctx,
1246 (unsigned long long) ts->majf,
1247 (unsigned long long) ts->minf);
1248
1249 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1250 log_buf(out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
1251 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1252 io_u_dist[1], io_u_dist[2],
1253 io_u_dist[3], io_u_dist[4],
1254 io_u_dist[5], io_u_dist[6]);
1255
1256 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
1257 log_buf(out, " submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
1258 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1259 io_u_dist[1], io_u_dist[2],
1260 io_u_dist[3], io_u_dist[4],
1261 io_u_dist[5], io_u_dist[6]);
1262 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
1263 log_buf(out, " complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
1264 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1265 io_u_dist[1], io_u_dist[2],
1266 io_u_dist[3], io_u_dist[4],
1267 io_u_dist[5], io_u_dist[6]);
1268 log_buf(out, " issued rwts: total=%llu,%llu,%llu,%llu"
1269 " short=%llu,%llu,%llu,0"
1270 " dropped=%llu,%llu,%llu,0\n",
1271 (unsigned long long) ts->total_io_u[0],
1272 (unsigned long long) ts->total_io_u[1],
1273 (unsigned long long) ts->total_io_u[2],
1274 (unsigned long long) ts->total_io_u[3],
1275 (unsigned long long) ts->short_io_u[0],
1276 (unsigned long long) ts->short_io_u[1],
1277 (unsigned long long) ts->short_io_u[2],
1278 (unsigned long long) ts->drop_io_u[0],
1279 (unsigned long long) ts->drop_io_u[1],
1280 (unsigned long long) ts->drop_io_u[2]);
1281 if (ts->continue_on_error) {
1282 log_buf(out, " errors : total=%llu, first_error=%d/<%s>\n",
1283 (unsigned long long)ts->total_err_count,
1284 ts->first_error,
1285 strerror(ts->first_error));
1286 }
1287 if (ts->latency_depth) {
1288 log_buf(out, " latency : target=%llu, window=%llu, percentile=%.2f%%, depth=%u\n",
1289 (unsigned long long)ts->latency_target,
1290 (unsigned long long)ts->latency_window,
1291 ts->latency_percentile.u.f,
1292 ts->latency_depth);
1293 }
1294
1295 if (ts->nr_block_infos)
1296 show_block_infos(ts->nr_block_infos, ts->block_infos,
1297 ts->percentile_list, out);
1298
1299 if (ts->ss_dur)
1300 show_ss_normal(ts, out);
1301}
1302
1303static void show_ddir_status_terse(struct thread_stat *ts,
1304 struct group_run_stats *rs,
1305 enum fio_ddir ddir, int ver,
1306 struct buf_output *out)
1307{
1308 unsigned long long min, max, minv, maxv, bw, iops;
1309 unsigned long long *ovals = NULL;
1310 double mean, dev;
1311 unsigned int len;
1312 int i, bw_stat;
1313
1314 assert(ddir_rw(ddir));
1315
1316 iops = bw = 0;
1317 if (ts->runtime[ddir]) {
1318 uint64_t runt = ts->runtime[ddir];
1319
1320 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024; /* KiB/s */
1321 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
1322 }
1323
1324 log_buf(out, ";%llu;%llu;%llu;%llu",
1325 (unsigned long long) ts->io_bytes[ddir] >> 10, bw, iops,
1326 (unsigned long long) ts->runtime[ddir]);
1327
1328 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
1329 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1330 else
1331 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1332
1333 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
1334 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1335 else
1336 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1337
1338 if (ts->lat_percentiles) {
1339 len = calc_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
1340 ts->lat_stat[ddir].samples,
1341 ts->percentile_list, &ovals, &maxv,
1342 &minv);
1343 } else if (ts->clat_percentiles) {
1344 len = calc_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
1345 ts->clat_stat[ddir].samples,
1346 ts->percentile_list, &ovals, &maxv,
1347 &minv);
1348 } else {
1349 len = 0;
1350 }
1351
1352 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1353 if (i >= len) {
1354 log_buf(out, ";0%%=0");
1355 continue;
1356 }
1357 log_buf(out, ";%f%%=%llu", ts->percentile_list[i].u.f, ovals[i]/1000);
1358 }
1359
1360 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
1361 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1362 else
1363 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1364
1365 free(ovals);
1366
1367 bw_stat = calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev);
1368 if (bw_stat) {
1369 double p_of_agg = 100.0;
1370
1371 if (rs->agg[ddir]) {
1372 p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024);
1373 if (p_of_agg > 100.0)
1374 p_of_agg = 100.0;
1375 }
1376
1377 log_buf(out, ";%llu;%llu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
1378 } else {
1379 log_buf(out, ";%llu;%llu;%f%%;%f;%f", 0ULL, 0ULL, 0.0, 0.0, 0.0);
1380 }
1381
1382 if (ver == 5) {
1383 if (bw_stat)
1384 log_buf(out, ";%" PRIu64, (&ts->bw_stat[ddir])->samples);
1385 else
1386 log_buf(out, ";%lu", 0UL);
1387
1388 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev))
1389 log_buf(out, ";%llu;%llu;%f;%f;%" PRIu64, min, max,
1390 mean, dev, (&ts->iops_stat[ddir])->samples);
1391 else
1392 log_buf(out, ";%llu;%llu;%f;%f;%lu", 0ULL, 0ULL, 0.0, 0.0, 0UL);
1393 }
1394}
1395
1396static void show_mixed_ddir_status_terse(struct thread_stat *ts,
1397 struct group_run_stats *rs,
1398 int ver, struct buf_output *out)
1399{
1400 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
1401
1402 if (ts_lcl)
1403 show_ddir_status_terse(ts_lcl, rs, DDIR_READ, ver, out);
1404
1405 free_clat_prio_stats(ts_lcl);
1406 free(ts_lcl);
1407}
1408
1409static struct json_object *add_ddir_lat_json(struct thread_stat *ts,
1410 uint32_t percentiles,
1411 struct io_stat *lat_stat,
1412 uint64_t *io_u_plat)
1413{
1414 char buf[120];
1415 double mean, dev;
1416 unsigned int i, len;
1417 struct json_object *lat_object, *percentile_object, *clat_bins_object;
1418 unsigned long long min, max, maxv, minv, *ovals = NULL;
1419
1420 if (!calc_lat(lat_stat, &min, &max, &mean, &dev)) {
1421 min = max = 0;
1422 mean = dev = 0.0;
1423 }
1424 lat_object = json_create_object();
1425 json_object_add_value_int(lat_object, "min", min);
1426 json_object_add_value_int(lat_object, "max", max);
1427 json_object_add_value_float(lat_object, "mean", mean);
1428 json_object_add_value_float(lat_object, "stddev", dev);
1429 json_object_add_value_int(lat_object, "N", lat_stat->samples);
1430
1431 if (percentiles && lat_stat->samples) {
1432 len = calc_clat_percentiles(io_u_plat, lat_stat->samples,
1433 ts->percentile_list, &ovals, &maxv, &minv);
1434
1435 if (len > FIO_IO_U_LIST_MAX_LEN)
1436 len = FIO_IO_U_LIST_MAX_LEN;
1437
1438 percentile_object = json_create_object();
1439 json_object_add_value_object(lat_object, "percentile", percentile_object);
1440 for (i = 0; i < len; i++) {
1441 snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
1442 json_object_add_value_int(percentile_object, buf, ovals[i]);
1443 }
1444 free(ovals);
1445
1446 if (output_format & FIO_OUTPUT_JSON_PLUS) {
1447 clat_bins_object = json_create_object();
1448 json_object_add_value_object(lat_object, "bins", clat_bins_object);
1449
1450 for(i = 0; i < FIO_IO_U_PLAT_NR; i++)
1451 if (io_u_plat[i]) {
1452 snprintf(buf, sizeof(buf), "%llu", plat_idx_to_val(i));
1453 json_object_add_value_int(clat_bins_object, buf, io_u_plat[i]);
1454 }
1455 }
1456 }
1457
1458 return lat_object;
1459}
1460
1461static void add_ddir_status_json(struct thread_stat *ts,
1462 struct group_run_stats *rs, enum fio_ddir ddir,
1463 struct json_object *parent)
1464{
1465 unsigned long long min, max;
1466 unsigned long long bw_bytes, bw;
1467 double mean, dev, iops;
1468 struct json_object *dir_object, *tmp_object;
1469 double p_of_agg = 100.0;
1470
1471 assert(ddir_rw(ddir) || ddir_sync(ddir));
1472
1473 if ((ts->unified_rw_rep == UNIFIED_MIXED) && ddir != DDIR_READ)
1474 return;
1475
1476 dir_object = json_create_object();
1477 json_object_add_value_object(parent,
1478 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir), dir_object);
1479
1480 if (ddir_rw(ddir)) {
1481 bw_bytes = 0;
1482 bw = 0;
1483 iops = 0.0;
1484 if (ts->runtime[ddir]) {
1485 uint64_t runt = ts->runtime[ddir];
1486
1487 bw_bytes = ((1000 * ts->io_bytes[ddir]) / runt); /* Bytes/s */
1488 bw = bw_bytes / 1024; /* KiB/s */
1489 iops = (1000.0 * (uint64_t) ts->total_io_u[ddir]) / runt;
1490 }
1491
1492 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir]);
1493 json_object_add_value_int(dir_object, "io_kbytes", ts->io_bytes[ddir] >> 10);
1494 json_object_add_value_int(dir_object, "bw_bytes", bw_bytes);
1495 json_object_add_value_int(dir_object, "bw", bw);
1496 json_object_add_value_float(dir_object, "iops", iops);
1497 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
1498 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[ddir]);
1499 json_object_add_value_int(dir_object, "short_ios", ts->short_io_u[ddir]);
1500 json_object_add_value_int(dir_object, "drop_ios", ts->drop_io_u[ddir]);
1501
1502 tmp_object = add_ddir_lat_json(ts, ts->slat_percentiles,
1503 &ts->slat_stat[ddir], ts->io_u_plat[FIO_SLAT][ddir]);
1504 json_object_add_value_object(dir_object, "slat_ns", tmp_object);
1505
1506 tmp_object = add_ddir_lat_json(ts, ts->clat_percentiles,
1507 &ts->clat_stat[ddir], ts->io_u_plat[FIO_CLAT][ddir]);
1508 json_object_add_value_object(dir_object, "clat_ns", tmp_object);
1509
1510 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles,
1511 &ts->lat_stat[ddir], ts->io_u_plat[FIO_LAT][ddir]);
1512 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
1513 } else {
1514 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[DDIR_SYNC]);
1515 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles | ts->clat_percentiles,
1516 &ts->sync_stat, ts->io_u_sync_plat);
1517 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
1518 }
1519
1520 if (!ddir_rw(ddir))
1521 return;
1522
1523 /* Only include per prio stats if there are >= 2 prios with samples */
1524 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
1525 struct json_array *array = json_create_array();
1526 const char *obj_name;
1527 int i;
1528
1529 if (ts->lat_percentiles)
1530 obj_name = "lat_ns";
1531 else
1532 obj_name = "clat_ns";
1533
1534 json_object_add_value_array(dir_object, "prios", array);
1535
1536 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
1537 struct json_object *obj;
1538
1539 if (!ts->clat_prio[ddir][i].clat_stat.samples)
1540 continue;
1541
1542 obj = json_create_object();
1543
1544 json_object_add_value_int(obj, "prioclass",
1545 ioprio_class(ts->clat_prio[ddir][i].ioprio));
1546 json_object_add_value_int(obj, "prio",
1547 ioprio(ts->clat_prio[ddir][i].ioprio));
1548 json_object_add_value_int(obj, "priohint",
1549 ioprio_hint(ts->clat_prio[ddir][i].ioprio));
1550
1551 tmp_object = add_ddir_lat_json(ts,
1552 ts->clat_percentiles | ts->lat_percentiles,
1553 &ts->clat_prio[ddir][i].clat_stat,
1554 ts->clat_prio[ddir][i].io_u_plat);
1555 json_object_add_value_object(obj, obj_name, tmp_object);
1556 json_array_add_value_object(array, obj);
1557 }
1558 }
1559
1560 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
1561 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
1562 } else {
1563 min = max = 0;
1564 p_of_agg = mean = dev = 0.0;
1565 }
1566
1567 json_object_add_value_int(dir_object, "bw_min", min);
1568 json_object_add_value_int(dir_object, "bw_max", max);
1569 json_object_add_value_float(dir_object, "bw_agg", p_of_agg);
1570 json_object_add_value_float(dir_object, "bw_mean", mean);
1571 json_object_add_value_float(dir_object, "bw_dev", dev);
1572 json_object_add_value_int(dir_object, "bw_samples",
1573 (&ts->bw_stat[ddir])->samples);
1574
1575 if (!calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
1576 min = max = 0;
1577 mean = dev = 0.0;
1578 }
1579 json_object_add_value_int(dir_object, "iops_min", min);
1580 json_object_add_value_int(dir_object, "iops_max", max);
1581 json_object_add_value_float(dir_object, "iops_mean", mean);
1582 json_object_add_value_float(dir_object, "iops_stddev", dev);
1583 json_object_add_value_int(dir_object, "iops_samples",
1584 (&ts->iops_stat[ddir])->samples);
1585
1586 if (ts->cachehit + ts->cachemiss) {
1587 uint64_t total;
1588 double hit;
1589
1590 total = ts->cachehit + ts->cachemiss;
1591 hit = (double) ts->cachehit / (double) total;
1592 hit *= 100.0;
1593 json_object_add_value_float(dir_object, "cachehit", hit);
1594 }
1595}
1596
1597static void add_mixed_ddir_status_json(struct thread_stat *ts,
1598 struct group_run_stats *rs, struct json_object *parent)
1599{
1600 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
1601
1602 /* add the aggregated stats to json parent */
1603 if (ts_lcl)
1604 add_ddir_status_json(ts_lcl, rs, DDIR_READ, parent);
1605
1606 free_clat_prio_stats(ts_lcl);
1607 free(ts_lcl);
1608}
1609
1610static void show_thread_status_terse_all(struct thread_stat *ts,
1611 struct group_run_stats *rs, int ver,
1612 struct buf_output *out)
1613{
1614 double io_u_dist[FIO_IO_U_MAP_NR];
1615 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1616 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1617 double usr_cpu, sys_cpu;
1618 int i;
1619
1620 /* General Info */
1621 if (ver == 2)
1622 log_buf(out, "2;%s;%d;%d", ts->name, ts->groupid, ts->error);
1623 else
1624 log_buf(out, "%d;%s;%s;%d;%d", ver, fio_version_string,
1625 ts->name, ts->groupid, ts->error);
1626
1627 /* Log Read Status, or mixed if unified_rw_rep = 1 */
1628 show_ddir_status_terse(ts, rs, DDIR_READ, ver, out);
1629 if (ts->unified_rw_rep != UNIFIED_MIXED) {
1630 /* Log Write Status */
1631 show_ddir_status_terse(ts, rs, DDIR_WRITE, ver, out);
1632 /* Log Trim Status */
1633 if (ver == 2 || ver == 4 || ver == 5)
1634 show_ddir_status_terse(ts, rs, DDIR_TRIM, ver, out);
1635 }
1636 if (ts->unified_rw_rep == UNIFIED_BOTH)
1637 show_mixed_ddir_status_terse(ts, rs, ver, out);
1638 /* CPU Usage */
1639 if (ts->total_run_time) {
1640 double runt = (double) ts->total_run_time;
1641
1642 usr_cpu = (double) ts->usr_time * 100 / runt;
1643 sys_cpu = (double) ts->sys_time * 100 / runt;
1644 } else {
1645 usr_cpu = 0;
1646 sys_cpu = 0;
1647 }
1648
1649 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
1650 (unsigned long long) ts->ctx,
1651 (unsigned long long) ts->majf,
1652 (unsigned long long) ts->minf);
1653
1654 /* Calc % distribution of IO depths, usecond, msecond latency */
1655 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1656 stat_calc_lat_nu(ts, io_u_lat_u);
1657 stat_calc_lat_m(ts, io_u_lat_m);
1658
1659 /* Only show fixed 7 I/O depth levels*/
1660 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
1661 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
1662 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
1663
1664 /* Microsecond latency */
1665 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
1666 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
1667 /* Millisecond latency */
1668 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
1669 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
1670
1671 /* disk util stats, if any */
1672 if (ver >= 3 && is_running_backend())
1673 show_disk_util(1, NULL, out);
1674
1675 /* Additional output if continue_on_error set - default off*/
1676 if (ts->continue_on_error)
1677 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
1678
1679 /* Additional output if description is set */
1680 if (strlen(ts->description)) {
1681 if (ver == 2)
1682 log_buf(out, "\n");
1683 log_buf(out, ";%s", ts->description);
1684 }
1685
1686 log_buf(out, "\n");
1687}
1688
1689static void json_add_job_opts(struct json_object *root, const char *name,
1690 struct flist_head *opt_list)
1691{
1692 struct json_object *dir_object;
1693 struct flist_head *entry;
1694 struct print_option *p;
1695
1696 if (flist_empty(opt_list))
1697 return;
1698
1699 dir_object = json_create_object();
1700 json_object_add_value_object(root, name, dir_object);
1701
1702 flist_for_each(entry, opt_list) {
1703 p = flist_entry(entry, struct print_option, list);
1704 json_object_add_value_string(dir_object, p->name, p->value);
1705 }
1706}
1707
1708static struct json_object *show_thread_status_json(struct thread_stat *ts,
1709 struct group_run_stats *rs,
1710 struct flist_head *opt_list)
1711{
1712 struct json_object *root, *tmp;
1713 struct jobs_eta *je;
1714 double io_u_dist[FIO_IO_U_MAP_NR];
1715 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
1716 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1717 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1718 double usr_cpu, sys_cpu;
1719 int i;
1720 size_t size;
1721
1722 root = json_create_object();
1723 json_object_add_value_string(root, "jobname", ts->name);
1724 json_object_add_value_int(root, "groupid", ts->groupid);
1725 json_object_add_value_int(root, "job_start", ts->job_start);
1726 json_object_add_value_int(root, "error", ts->error);
1727
1728 /* ETA Info */
1729 je = get_jobs_eta(true, &size);
1730 if (je) {
1731 json_object_add_value_int(root, "eta", je->eta_sec);
1732 json_object_add_value_int(root, "elapsed", je->elapsed_sec);
1733 free(je);
1734 }
1735
1736 if (opt_list)
1737 json_add_job_opts(root, "job options", opt_list);
1738
1739 add_ddir_status_json(ts, rs, DDIR_READ, root);
1740 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
1741 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
1742 add_ddir_status_json(ts, rs, DDIR_SYNC, root);
1743
1744 if (ts->unified_rw_rep == UNIFIED_BOTH)
1745 add_mixed_ddir_status_json(ts, rs, root);
1746
1747 /* CPU Usage */
1748 if (ts->total_run_time) {
1749 double runt = (double) ts->total_run_time;
1750
1751 usr_cpu = (double) ts->usr_time * 100 / runt;
1752 sys_cpu = (double) ts->sys_time * 100 / runt;
1753 } else {
1754 usr_cpu = 0;
1755 sys_cpu = 0;
1756 }
1757 json_object_add_value_int(root, "job_runtime", ts->total_run_time);
1758 json_object_add_value_float(root, "usr_cpu", usr_cpu);
1759 json_object_add_value_float(root, "sys_cpu", sys_cpu);
1760 json_object_add_value_int(root, "ctx", ts->ctx);
1761 json_object_add_value_int(root, "majf", ts->majf);
1762 json_object_add_value_int(root, "minf", ts->minf);
1763
1764 /* Calc % distribution of IO depths */
1765 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1766 tmp = json_create_object();
1767 json_object_add_value_object(root, "iodepth_level", tmp);
1768 /* Only show fixed 7 I/O depth levels*/
1769 for (i = 0; i < 7; i++) {
1770 char name[20];
1771 if (i < 6)
1772 snprintf(name, 20, "%d", 1 << i);
1773 else
1774 snprintf(name, 20, ">=%d", 1 << i);
1775 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1776 }
1777
1778 /* Calc % distribution of submit IO depths */
1779 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
1780 tmp = json_create_object();
1781 json_object_add_value_object(root, "iodepth_submit", tmp);
1782 /* Only show fixed 7 I/O depth levels*/
1783 for (i = 0; i < 7; i++) {
1784 char name[20];
1785 if (i == 0)
1786 snprintf(name, 20, "0");
1787 else if (i < 6)
1788 snprintf(name, 20, "%d", 1 << (i+1));
1789 else
1790 snprintf(name, 20, ">=%d", 1 << i);
1791 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1792 }
1793
1794 /* Calc % distribution of completion IO depths */
1795 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
1796 tmp = json_create_object();
1797 json_object_add_value_object(root, "iodepth_complete", tmp);
1798 /* Only show fixed 7 I/O depth levels*/
1799 for (i = 0; i < 7; i++) {
1800 char name[20];
1801 if (i == 0)
1802 snprintf(name, 20, "0");
1803 else if (i < 6)
1804 snprintf(name, 20, "%d", 1 << (i+1));
1805 else
1806 snprintf(name, 20, ">=%d", 1 << i);
1807 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1808 }
1809
1810 /* Calc % distribution of nsecond, usecond, msecond latency */
1811 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1812 stat_calc_lat_n(ts, io_u_lat_n);
1813 stat_calc_lat_u(ts, io_u_lat_u);
1814 stat_calc_lat_m(ts, io_u_lat_m);
1815
1816 /* Nanosecond latency */
1817 tmp = json_create_object();
1818 json_object_add_value_object(root, "latency_ns", tmp);
1819 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++) {
1820 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1821 "250", "500", "750", "1000", };
1822 json_object_add_value_float(tmp, ranges[i], io_u_lat_n[i]);
1823 }
1824 /* Microsecond latency */
1825 tmp = json_create_object();
1826 json_object_add_value_object(root, "latency_us", tmp);
1827 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1828 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1829 "250", "500", "750", "1000", };
1830 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
1831 }
1832 /* Millisecond latency */
1833 tmp = json_create_object();
1834 json_object_add_value_object(root, "latency_ms", tmp);
1835 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
1836 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1837 "250", "500", "750", "1000", "2000",
1838 ">=2000", };
1839 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
1840 }
1841
1842 /* Additional output if continue_on_error set - default off*/
1843 if (ts->continue_on_error) {
1844 json_object_add_value_int(root, "total_err", ts->total_err_count);
1845 json_object_add_value_int(root, "first_error", ts->first_error);
1846 }
1847
1848 if (ts->latency_depth) {
1849 json_object_add_value_int(root, "latency_depth", ts->latency_depth);
1850 json_object_add_value_int(root, "latency_target", ts->latency_target);
1851 json_object_add_value_float(root, "latency_percentile", ts->latency_percentile.u.f);
1852 json_object_add_value_int(root, "latency_window", ts->latency_window);
1853 }
1854
1855 /* Additional output if description is set */
1856 if (strlen(ts->description))
1857 json_object_add_value_string(root, "desc", ts->description);
1858
1859 if (ts->nr_block_infos) {
1860 /* Block error histogram and types */
1861 int len;
1862 unsigned int *percentiles = NULL;
1863 unsigned int block_state_counts[BLOCK_STATE_COUNT];
1864
1865 len = calc_block_percentiles(ts->nr_block_infos, ts->block_infos,
1866 ts->percentile_list,
1867 &percentiles, block_state_counts);
1868
1869 if (len) {
1870 struct json_object *block, *percentile_object, *states;
1871 int state;
1872 block = json_create_object();
1873 json_object_add_value_object(root, "block", block);
1874
1875 percentile_object = json_create_object();
1876 json_object_add_value_object(block, "percentiles",
1877 percentile_object);
1878 for (i = 0; i < len; i++) {
1879 char buf[20];
1880 snprintf(buf, sizeof(buf), "%f",
1881 ts->percentile_list[i].u.f);
1882 json_object_add_value_int(percentile_object,
1883 buf,
1884 percentiles[i]);
1885 }
1886
1887 states = json_create_object();
1888 json_object_add_value_object(block, "states", states);
1889 for (state = 0; state < BLOCK_STATE_COUNT; state++) {
1890 json_object_add_value_int(states,
1891 block_state_names[state],
1892 block_state_counts[state]);
1893 }
1894 free(percentiles);
1895 }
1896 }
1897
1898 if (ts->ss_dur) {
1899 struct json_object *data;
1900 struct json_array *iops, *bw;
1901 int j, k, l;
1902 char ss_buf[64];
1903 int intervals = ts->ss_dur / (ss_check_interval / 1000L);
1904
1905 snprintf(ss_buf, sizeof(ss_buf), "%s%s:%f%s",
1906 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
1907 ts->ss_state & FIO_SS_SLOPE ? "_slope" : "",
1908 (float) ts->ss_limit.u.f,
1909 ts->ss_state & FIO_SS_PCT ? "%" : "");
1910
1911 tmp = json_create_object();
1912 json_object_add_value_object(root, "steadystate", tmp);
1913 json_object_add_value_string(tmp, "ss", ss_buf);
1914 json_object_add_value_int(tmp, "duration", (int)ts->ss_dur);
1915 json_object_add_value_int(tmp, "attained", (ts->ss_state & FIO_SS_ATTAINED) > 0);
1916
1917 snprintf(ss_buf, sizeof(ss_buf), "%f%s", (float) ts->ss_criterion.u.f,
1918 ts->ss_state & FIO_SS_PCT ? "%" : "");
1919 json_object_add_value_string(tmp, "criterion", ss_buf);
1920 json_object_add_value_float(tmp, "max_deviation", ts->ss_deviation.u.f);
1921 json_object_add_value_float(tmp, "slope", ts->ss_slope.u.f);
1922
1923 data = json_create_object();
1924 json_object_add_value_object(tmp, "data", data);
1925 bw = json_create_array();
1926 iops = json_create_array();
1927
1928 /*
1929 ** if ss was attained or the buffer is not full,
1930 ** ss->head points to the first element in the list.
1931 ** otherwise it actually points to the second element
1932 ** in the list
1933 */
1934 if ((ts->ss_state & FIO_SS_ATTAINED) || !(ts->ss_state & FIO_SS_BUFFER_FULL))
1935 j = ts->ss_head;
1936 else
1937 j = ts->ss_head == 0 ? intervals - 1 : ts->ss_head - 1;
1938 for (l = 0; l < intervals; l++) {
1939 k = (j + l) % intervals;
1940 json_array_add_value_int(bw, ts->ss_bw_data[k]);
1941 json_array_add_value_int(iops, ts->ss_iops_data[k]);
1942 }
1943 json_object_add_value_int(data, "bw_mean", steadystate_bw_mean(ts));
1944 json_object_add_value_int(data, "iops_mean", steadystate_iops_mean(ts));
1945 json_object_add_value_array(data, "iops", iops);
1946 json_object_add_value_array(data, "bw", bw);
1947 }
1948
1949 return root;
1950}
1951
1952static void show_thread_status_terse(struct thread_stat *ts,
1953 struct group_run_stats *rs,
1954 struct buf_output *out)
1955{
1956 if (terse_version >= 2 && terse_version <= 5)
1957 show_thread_status_terse_all(ts, rs, terse_version, out);
1958 else
1959 log_err("fio: bad terse version!? %d\n", terse_version);
1960}
1961
1962struct json_object *show_thread_status(struct thread_stat *ts,
1963 struct group_run_stats *rs,
1964 struct flist_head *opt_list,
1965 struct buf_output *out)
1966{
1967 struct json_object *ret = NULL;
1968
1969 if (output_format & FIO_OUTPUT_TERSE)
1970 show_thread_status_terse(ts, rs, out);
1971 if (output_format & FIO_OUTPUT_JSON)
1972 ret = show_thread_status_json(ts, rs, opt_list);
1973 if (output_format & FIO_OUTPUT_NORMAL)
1974 show_thread_status_normal(ts, rs, out);
1975
1976 return ret;
1977}
1978
1979static void __sum_stat(struct io_stat *dst, struct io_stat *src, bool first)
1980{
1981 double mean, S;
1982
1983 dst->min_val = min(dst->min_val, src->min_val);
1984 dst->max_val = max(dst->max_val, src->max_val);
1985
1986 /*
1987 * Compute new mean and S after the merge
1988 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1989 * #Parallel_algorithm>
1990 */
1991 if (first) {
1992 mean = src->mean.u.f;
1993 S = src->S.u.f;
1994 } else {
1995 double delta = src->mean.u.f - dst->mean.u.f;
1996
1997 mean = ((src->mean.u.f * src->samples) +
1998 (dst->mean.u.f * dst->samples)) /
1999 (dst->samples + src->samples);
2000
2001 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
2002 (dst->samples * src->samples) /
2003 (dst->samples + src->samples);
2004 }
2005
2006 dst->samples += src->samples;
2007 dst->mean.u.f = mean;
2008 dst->S.u.f = S;
2009
2010}
2011
2012/*
2013 * We sum two kinds of stats - one that is time based, in which case we
2014 * apply the proper summing technique, and then one that is iops/bw
2015 * numbers. For group_reporting, we should just add those up, not make
2016 * them the mean of everything.
2017 */
2018static void sum_stat(struct io_stat *dst, struct io_stat *src, bool pure_sum)
2019{
2020 bool first = dst->samples == 0;
2021
2022 if (src->samples == 0)
2023 return;
2024
2025 if (!pure_sum) {
2026 __sum_stat(dst, src, first);
2027 return;
2028 }
2029
2030 if (first) {
2031 dst->min_val = src->min_val;
2032 dst->max_val = src->max_val;
2033 dst->samples = src->samples;
2034 dst->mean.u.f = src->mean.u.f;
2035 dst->S.u.f = src->S.u.f;
2036 } else {
2037 dst->min_val += src->min_val;
2038 dst->max_val += src->max_val;
2039 dst->samples += src->samples;
2040 dst->mean.u.f += src->mean.u.f;
2041 dst->S.u.f += src->S.u.f;
2042 }
2043}
2044
2045void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
2046{
2047 int i;
2048
2049 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2050 if (dst->max_run[i] < src->max_run[i])
2051 dst->max_run[i] = src->max_run[i];
2052 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
2053 dst->min_run[i] = src->min_run[i];
2054 if (dst->max_bw[i] < src->max_bw[i])
2055 dst->max_bw[i] = src->max_bw[i];
2056 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
2057 dst->min_bw[i] = src->min_bw[i];
2058
2059 dst->iobytes[i] += src->iobytes[i];
2060 dst->agg[i] += src->agg[i];
2061 }
2062
2063 if (!dst->kb_base)
2064 dst->kb_base = src->kb_base;
2065 if (!dst->unit_base)
2066 dst->unit_base = src->unit_base;
2067 if (!dst->sig_figs)
2068 dst->sig_figs = src->sig_figs;
2069}
2070
2071/*
2072 * Free the clat_prio_stat arrays allocated by alloc_clat_prio_stat_ddir().
2073 */
2074void free_clat_prio_stats(struct thread_stat *ts)
2075{
2076 enum fio_ddir ddir;
2077
2078 if (!ts)
2079 return;
2080
2081 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2082 sfree(ts->clat_prio[ddir]);
2083 ts->clat_prio[ddir] = NULL;
2084 ts->nr_clat_prio[ddir] = 0;
2085 }
2086}
2087
2088/*
2089 * Allocate a clat_prio_stat array. The array has to be allocated/freed using
2090 * smalloc/sfree, so that it is accessible by the process/thread summing the
2091 * thread_stats.
2092 */
2093int alloc_clat_prio_stat_ddir(struct thread_stat *ts, enum fio_ddir ddir,
2094 int nr_prios)
2095{
2096 struct clat_prio_stat *clat_prio;
2097 int i;
2098
2099 clat_prio = scalloc(nr_prios, sizeof(*ts->clat_prio[ddir]));
2100 if (!clat_prio) {
2101 log_err("fio: failed to allocate ts clat data\n");
2102 return 1;
2103 }
2104
2105 for (i = 0; i < nr_prios; i++)
2106 clat_prio[i].clat_stat.min_val = ULONG_MAX;
2107
2108 ts->clat_prio[ddir] = clat_prio;
2109 ts->nr_clat_prio[ddir] = nr_prios;
2110
2111 return 0;
2112}
2113
2114static int grow_clat_prio_stat(struct thread_stat *dst, enum fio_ddir ddir)
2115{
2116 int curr_len = dst->nr_clat_prio[ddir];
2117 void *new_arr;
2118
2119 new_arr = scalloc(curr_len + 1, sizeof(*dst->clat_prio[ddir]));
2120 if (!new_arr) {
2121 log_err("fio: failed to grow clat prio array\n");
2122 return 1;
2123 }
2124
2125 memcpy(new_arr, dst->clat_prio[ddir],
2126 curr_len * sizeof(*dst->clat_prio[ddir]));
2127 sfree(dst->clat_prio[ddir]);
2128
2129 dst->clat_prio[ddir] = new_arr;
2130 dst->clat_prio[ddir][curr_len].clat_stat.min_val = ULONG_MAX;
2131 dst->nr_clat_prio[ddir]++;
2132
2133 return 0;
2134}
2135
2136static int find_clat_prio_index(struct thread_stat *dst, enum fio_ddir ddir,
2137 uint32_t ioprio)
2138{
2139 int i, nr_prios = dst->nr_clat_prio[ddir];
2140
2141 for (i = 0; i < nr_prios; i++) {
2142 if (dst->clat_prio[ddir][i].ioprio == ioprio)
2143 return i;
2144 }
2145
2146 return -1;
2147}
2148
2149static int alloc_or_get_clat_prio_index(struct thread_stat *dst,
2150 enum fio_ddir ddir, uint32_t ioprio,
2151 int *idx)
2152{
2153 int index = find_clat_prio_index(dst, ddir, ioprio);
2154
2155 if (index == -1) {
2156 index = dst->nr_clat_prio[ddir];
2157
2158 if (grow_clat_prio_stat(dst, ddir))
2159 return 1;
2160
2161 dst->clat_prio[ddir][index].ioprio = ioprio;
2162 }
2163
2164 *idx = index;
2165
2166 return 0;
2167}
2168
2169static int clat_prio_stats_copy(struct thread_stat *dst, struct thread_stat *src,
2170 enum fio_ddir dst_ddir, enum fio_ddir src_ddir)
2171{
2172 size_t sz = sizeof(*src->clat_prio[src_ddir]) *
2173 src->nr_clat_prio[src_ddir];
2174
2175 dst->clat_prio[dst_ddir] = smalloc(sz);
2176 if (!dst->clat_prio[dst_ddir]) {
2177 log_err("fio: failed to alloc clat prio array\n");
2178 return 1;
2179 }
2180
2181 memcpy(dst->clat_prio[dst_ddir], src->clat_prio[src_ddir], sz);
2182 dst->nr_clat_prio[dst_ddir] = src->nr_clat_prio[src_ddir];
2183
2184 return 0;
2185}
2186
2187static int clat_prio_stat_add_samples(struct thread_stat *dst,
2188 enum fio_ddir dst_ddir, uint32_t ioprio,
2189 struct io_stat *io_stat,
2190 uint64_t *io_u_plat)
2191{
2192 int i, dst_index;
2193
2194 if (!io_stat->samples)
2195 return 0;
2196
2197 if (alloc_or_get_clat_prio_index(dst, dst_ddir, ioprio, &dst_index))
2198 return 1;
2199
2200 sum_stat(&dst->clat_prio[dst_ddir][dst_index].clat_stat, io_stat,
2201 false);
2202
2203 for (i = 0; i < FIO_IO_U_PLAT_NR; i++)
2204 dst->clat_prio[dst_ddir][dst_index].io_u_plat[i] += io_u_plat[i];
2205
2206 return 0;
2207}
2208
2209static int sum_clat_prio_stats_src_single_prio(struct thread_stat *dst,
2210 struct thread_stat *src,
2211 enum fio_ddir dst_ddir,
2212 enum fio_ddir src_ddir)
2213{
2214 struct io_stat *io_stat;
2215 uint64_t *io_u_plat;
2216
2217 /*
2218 * If src ts has no clat_prio_stat array, then all I/Os were submitted
2219 * using src->ioprio. Thus, the global samples in src->clat_stat (or
2220 * src->lat_stat) can be used as the 'per prio' samples for src->ioprio.
2221 */
2222 assert(!src->clat_prio[src_ddir]);
2223 assert(src->nr_clat_prio[src_ddir] == 0);
2224
2225 if (src->lat_percentiles) {
2226 io_u_plat = src->io_u_plat[FIO_LAT][src_ddir];
2227 io_stat = &src->lat_stat[src_ddir];
2228 } else {
2229 io_u_plat = src->io_u_plat[FIO_CLAT][src_ddir];
2230 io_stat = &src->clat_stat[src_ddir];
2231 }
2232
2233 return clat_prio_stat_add_samples(dst, dst_ddir, src->ioprio, io_stat,
2234 io_u_plat);
2235}
2236
2237static int sum_clat_prio_stats_src_multi_prio(struct thread_stat *dst,
2238 struct thread_stat *src,
2239 enum fio_ddir dst_ddir,
2240 enum fio_ddir src_ddir)
2241{
2242 int i;
2243
2244 /*
2245 * If src ts has a clat_prio_stat array, then there are multiple prios
2246 * in use (i.e. src ts had cmdprio_percentage or cmdprio_bssplit set).
2247 * The samples for the default prio will exist in the src->clat_prio
2248 * array, just like the samples for any other prio.
2249 */
2250 assert(src->clat_prio[src_ddir]);
2251 assert(src->nr_clat_prio[src_ddir]);
2252
2253 /* If the dst ts doesn't yet have a clat_prio array, simply memcpy. */
2254 if (!dst->clat_prio[dst_ddir])
2255 return clat_prio_stats_copy(dst, src, dst_ddir, src_ddir);
2256
2257 /* The dst ts already has a clat_prio_array, add src stats into it. */
2258 for (i = 0; i < src->nr_clat_prio[src_ddir]; i++) {
2259 struct io_stat *io_stat = &src->clat_prio[src_ddir][i].clat_stat;
2260 uint64_t *io_u_plat = src->clat_prio[src_ddir][i].io_u_plat;
2261 uint32_t ioprio = src->clat_prio[src_ddir][i].ioprio;
2262
2263 if (clat_prio_stat_add_samples(dst, dst_ddir, ioprio, io_stat, io_u_plat))
2264 return 1;
2265 }
2266
2267 return 0;
2268}
2269
2270static int sum_clat_prio_stats(struct thread_stat *dst, struct thread_stat *src,
2271 enum fio_ddir dst_ddir, enum fio_ddir src_ddir)
2272{
2273 if (dst->disable_prio_stat)
2274 return 0;
2275
2276 if (!src->clat_prio[src_ddir])
2277 return sum_clat_prio_stats_src_single_prio(dst, src, dst_ddir,
2278 src_ddir);
2279
2280 return sum_clat_prio_stats_src_multi_prio(dst, src, dst_ddir, src_ddir);
2281}
2282
2283void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src)
2284{
2285 int k, l, m;
2286
2287 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
2288 if (dst->unified_rw_rep != UNIFIED_MIXED) {
2289 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], false);
2290 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], false);
2291 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], false);
2292 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], true);
2293 sum_stat(&dst->iops_stat[l], &src->iops_stat[l], true);
2294 sum_clat_prio_stats(dst, src, l, l);
2295
2296 dst->io_bytes[l] += src->io_bytes[l];
2297
2298 if (dst->runtime[l] < src->runtime[l])
2299 dst->runtime[l] = src->runtime[l];
2300 } else {
2301 sum_stat(&dst->clat_stat[0], &src->clat_stat[l], false);
2302 sum_stat(&dst->slat_stat[0], &src->slat_stat[l], false);
2303 sum_stat(&dst->lat_stat[0], &src->lat_stat[l], false);
2304 sum_stat(&dst->bw_stat[0], &src->bw_stat[l], true);
2305 sum_stat(&dst->iops_stat[0], &src->iops_stat[l], true);
2306 sum_clat_prio_stats(dst, src, 0, l);
2307
2308 dst->io_bytes[0] += src->io_bytes[l];
2309
2310 if (dst->runtime[0] < src->runtime[l])
2311 dst->runtime[0] = src->runtime[l];
2312 }
2313 }
2314
2315 sum_stat(&dst->sync_stat, &src->sync_stat, false);
2316 dst->usr_time += src->usr_time;
2317 dst->sys_time += src->sys_time;
2318 dst->ctx += src->ctx;
2319 dst->majf += src->majf;
2320 dst->minf += src->minf;
2321
2322 for (k = 0; k < FIO_IO_U_MAP_NR; k++) {
2323 dst->io_u_map[k] += src->io_u_map[k];
2324 dst->io_u_submit[k] += src->io_u_submit[k];
2325 dst->io_u_complete[k] += src->io_u_complete[k];
2326 }
2327
2328 for (k = 0; k < FIO_IO_U_LAT_N_NR; k++)
2329 dst->io_u_lat_n[k] += src->io_u_lat_n[k];
2330 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
2331 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
2332 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
2333 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
2334
2335 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
2336 if (dst->unified_rw_rep != UNIFIED_MIXED) {
2337 dst->total_io_u[k] += src->total_io_u[k];
2338 dst->short_io_u[k] += src->short_io_u[k];
2339 dst->drop_io_u[k] += src->drop_io_u[k];
2340 } else {
2341 dst->total_io_u[0] += src->total_io_u[k];
2342 dst->short_io_u[0] += src->short_io_u[k];
2343 dst->drop_io_u[0] += src->drop_io_u[k];
2344 }
2345 }
2346
2347 dst->total_io_u[DDIR_SYNC] += src->total_io_u[DDIR_SYNC];
2348
2349 for (k = 0; k < FIO_LAT_CNT; k++)
2350 for (l = 0; l < DDIR_RWDIR_CNT; l++)
2351 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
2352 if (dst->unified_rw_rep != UNIFIED_MIXED)
2353 dst->io_u_plat[k][l][m] += src->io_u_plat[k][l][m];
2354 else
2355 dst->io_u_plat[k][0][m] += src->io_u_plat[k][l][m];
2356
2357 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
2358 dst->io_u_sync_plat[k] += src->io_u_sync_plat[k];
2359
2360 dst->total_run_time += src->total_run_time;
2361 dst->total_submit += src->total_submit;
2362 dst->total_complete += src->total_complete;
2363 dst->nr_zone_resets += src->nr_zone_resets;
2364 dst->cachehit += src->cachehit;
2365 dst->cachemiss += src->cachemiss;
2366}
2367
2368void init_group_run_stat(struct group_run_stats *gs)
2369{
2370 int i;
2371 memset(gs, 0, sizeof(*gs));
2372
2373 for (i = 0; i < DDIR_RWDIR_CNT; i++)
2374 gs->min_bw[i] = gs->min_run[i] = ~0UL;
2375}
2376
2377void init_thread_stat_min_vals(struct thread_stat *ts)
2378{
2379 int i;
2380
2381 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2382 ts->clat_stat[i].min_val = ULONG_MAX;
2383 ts->slat_stat[i].min_val = ULONG_MAX;
2384 ts->lat_stat[i].min_val = ULONG_MAX;
2385 ts->bw_stat[i].min_val = ULONG_MAX;
2386 ts->iops_stat[i].min_val = ULONG_MAX;
2387 }
2388 ts->sync_stat.min_val = ULONG_MAX;
2389}
2390
2391void init_thread_stat(struct thread_stat *ts)
2392{
2393 memset(ts, 0, sizeof(*ts));
2394
2395 init_thread_stat_min_vals(ts);
2396 ts->groupid = -1;
2397}
2398
2399static void init_per_prio_stats(struct thread_stat *threadstats, int nr_ts)
2400{
2401 struct thread_stat *ts;
2402 int i, j, last_ts, idx;
2403 enum fio_ddir ddir;
2404
2405 j = 0;
2406 last_ts = -1;
2407 idx = 0;
2408
2409 /*
2410 * Loop through all tds, if a td requires per prio stats, temporarily
2411 * store a 1 in ts->disable_prio_stat, and then do an additional
2412 * loop at the end where we invert the ts->disable_prio_stat values.
2413 */
2414 for_each_td(td) {
2415 if (!td->o.stats)
2416 continue;
2417 if (idx &&
2418 (!td->o.group_reporting ||
2419 (td->o.group_reporting && last_ts != td->groupid))) {
2420 idx = 0;
2421 j++;
2422 }
2423
2424 last_ts = td->groupid;
2425 ts = &threadstats[j];
2426
2427 /* idx == 0 means first td in group, or td is not in a group. */
2428 if (idx == 0)
2429 ts->ioprio = td->ioprio;
2430 else if (td->ioprio != ts->ioprio)
2431 ts->disable_prio_stat = 1;
2432
2433 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2434 if (td->ts.clat_prio[ddir]) {
2435 ts->disable_prio_stat = 1;
2436 break;
2437 }
2438 }
2439
2440 idx++;
2441 } end_for_each();
2442
2443 /* Loop through all dst threadstats and fixup the values. */
2444 for (i = 0; i < nr_ts; i++) {
2445 ts = &threadstats[i];
2446 ts->disable_prio_stat = !ts->disable_prio_stat;
2447 }
2448}
2449
2450void __show_run_stats(void)
2451{
2452 struct group_run_stats *runstats, *rs;
2453 struct thread_stat *threadstats, *ts;
2454 int i, j, k, nr_ts, last_ts, idx;
2455 bool kb_base_warned = false;
2456 bool unit_base_warned = false;
2457 struct json_object *root = NULL;
2458 struct json_array *array = NULL;
2459 struct buf_output output[FIO_OUTPUT_NR];
2460 struct flist_head **opt_lists;
2461
2462 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2463
2464 for (i = 0; i < groupid + 1; i++)
2465 init_group_run_stat(&runstats[i]);
2466
2467 /*
2468 * find out how many threads stats we need. if group reporting isn't
2469 * enabled, it's one-per-td.
2470 */
2471 nr_ts = 0;
2472 last_ts = -1;
2473 for_each_td(td) {
2474 if (!td->o.group_reporting) {
2475 nr_ts++;
2476 continue;
2477 }
2478 if (last_ts == td->groupid)
2479 continue;
2480 if (!td->o.stats)
2481 continue;
2482
2483 last_ts = td->groupid;
2484 nr_ts++;
2485 } end_for_each();
2486
2487 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
2488 opt_lists = malloc(nr_ts * sizeof(struct flist_head *));
2489
2490 for (i = 0; i < nr_ts; i++) {
2491 init_thread_stat(&threadstats[i]);
2492 opt_lists[i] = NULL;
2493 }
2494
2495 init_per_prio_stats(threadstats, nr_ts);
2496
2497 j = 0;
2498 last_ts = -1;
2499 idx = 0;
2500 for_each_td(td) {
2501 if (!td->o.stats)
2502 continue;
2503 if (idx && (!td->o.group_reporting ||
2504 (td->o.group_reporting && last_ts != td->groupid))) {
2505 idx = 0;
2506 j++;
2507 }
2508
2509 last_ts = td->groupid;
2510
2511 ts = &threadstats[j];
2512
2513 ts->clat_percentiles = td->o.clat_percentiles;
2514 ts->lat_percentiles = td->o.lat_percentiles;
2515 ts->slat_percentiles = td->o.slat_percentiles;
2516 ts->percentile_precision = td->o.percentile_precision;
2517 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
2518 opt_lists[j] = &td->opt_list;
2519
2520 idx++;
2521
2522 if (ts->groupid == -1) {
2523 /*
2524 * These are per-group shared already
2525 */
2526 snprintf(ts->name, sizeof(ts->name), "%s", td->o.name);
2527 if (td->o.description)
2528 snprintf(ts->description,
2529 sizeof(ts->description), "%s",
2530 td->o.description);
2531 else
2532 memset(ts->description, 0, FIO_JOBDESC_SIZE);
2533
2534 /*
2535 * If multiple entries in this group, this is
2536 * the first member.
2537 */
2538 ts->thread_number = td->thread_number;
2539 ts->groupid = td->groupid;
2540 ts->job_start = td->job_start;
2541
2542 /*
2543 * first pid in group, not very useful...
2544 */
2545 ts->pid = td->pid;
2546
2547 ts->kb_base = td->o.kb_base;
2548 ts->unit_base = td->o.unit_base;
2549 ts->sig_figs = td->o.sig_figs;
2550 ts->unified_rw_rep = td->o.unified_rw_rep;
2551 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
2552 log_info("fio: kb_base differs for jobs in group, using"
2553 " %u as the base\n", ts->kb_base);
2554 kb_base_warned = true;
2555 } else if (ts->unit_base != td->o.unit_base && !unit_base_warned) {
2556 log_info("fio: unit_base differs for jobs in group, using"
2557 " %u as the base\n", ts->unit_base);
2558 unit_base_warned = true;
2559 }
2560
2561 ts->continue_on_error = td->o.continue_on_error;
2562 ts->total_err_count += td->total_err_count;
2563 ts->first_error = td->first_error;
2564 if (!ts->error) {
2565 if (!td->error && td->o.continue_on_error &&
2566 td->first_error) {
2567 ts->error = td->first_error;
2568 snprintf(ts->verror, sizeof(ts->verror), "%s",
2569 td->verror);
2570 } else if (td->error) {
2571 ts->error = td->error;
2572 snprintf(ts->verror, sizeof(ts->verror), "%s",
2573 td->verror);
2574 }
2575 }
2576
2577 ts->latency_depth = td->latency_qd;
2578 ts->latency_target = td->o.latency_target;
2579 ts->latency_percentile = td->o.latency_percentile;
2580 ts->latency_window = td->o.latency_window;
2581
2582 ts->nr_block_infos = td->ts.nr_block_infos;
2583 for (k = 0; k < ts->nr_block_infos; k++)
2584 ts->block_infos[k] = td->ts.block_infos[k];
2585
2586 sum_thread_stats(ts, &td->ts);
2587
2588 ts->members++;
2589
2590 if (td->o.ss_dur) {
2591 ts->ss_state = td->ss.state;
2592 ts->ss_dur = td->ss.dur;
2593 ts->ss_head = td->ss.head;
2594 ts->ss_bw_data = td->ss.bw_data;
2595 ts->ss_iops_data = td->ss.iops_data;
2596 ts->ss_limit.u.f = td->ss.limit;
2597 ts->ss_slope.u.f = td->ss.slope;
2598 ts->ss_deviation.u.f = td->ss.deviation;
2599 ts->ss_criterion.u.f = td->ss.criterion;
2600 }
2601 else
2602 ts->ss_dur = ts->ss_state = 0;
2603 } end_for_each();
2604
2605 for (i = 0; i < nr_ts; i++) {
2606 unsigned long long bw;
2607
2608 ts = &threadstats[i];
2609 if (ts->groupid == -1)
2610 continue;
2611 rs = &runstats[ts->groupid];
2612 rs->kb_base = ts->kb_base;
2613 rs->unit_base = ts->unit_base;
2614 rs->sig_figs = ts->sig_figs;
2615 rs->unified_rw_rep |= ts->unified_rw_rep;
2616
2617 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
2618 if (!ts->runtime[j])
2619 continue;
2620 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
2621 rs->min_run[j] = ts->runtime[j];
2622 if (ts->runtime[j] > rs->max_run[j])
2623 rs->max_run[j] = ts->runtime[j];
2624
2625 bw = 0;
2626 if (ts->runtime[j])
2627 bw = ts->io_bytes[j] * 1000 / ts->runtime[j];
2628 if (bw < rs->min_bw[j])
2629 rs->min_bw[j] = bw;
2630 if (bw > rs->max_bw[j])
2631 rs->max_bw[j] = bw;
2632
2633 rs->iobytes[j] += ts->io_bytes[j];
2634 }
2635 }
2636
2637 for (i = 0; i < groupid + 1; i++) {
2638 enum fio_ddir ddir;
2639
2640 rs = &runstats[i];
2641
2642 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2643 if (rs->max_run[ddir])
2644 rs->agg[ddir] = (rs->iobytes[ddir] * 1000) /
2645 rs->max_run[ddir];
2646 }
2647 }
2648
2649 for (i = 0; i < FIO_OUTPUT_NR; i++)
2650 buf_output_init(&output[i]);
2651
2652 /*
2653 * don't overwrite last signal output
2654 */
2655 if (output_format & FIO_OUTPUT_NORMAL)
2656 log_buf(&output[__FIO_OUTPUT_NORMAL], "\n");
2657 if (output_format & FIO_OUTPUT_JSON) {
2658 struct thread_data *global;
2659 char time_buf[32];
2660 struct timeval now;
2661 unsigned long long ms_since_epoch;
2662 time_t tv_sec;
2663
2664 gettimeofday(&now, NULL);
2665 ms_since_epoch = (unsigned long long)(now.tv_sec) * 1000 +
2666 (unsigned long long)(now.tv_usec) / 1000;
2667
2668 tv_sec = now.tv_sec;
2669 os_ctime_r(&tv_sec, time_buf, sizeof(time_buf));
2670 if (time_buf[strlen(time_buf) - 1] == '\n')
2671 time_buf[strlen(time_buf) - 1] = '\0';
2672
2673 root = json_create_object();
2674 json_object_add_value_string(root, "fio version", fio_version_string);
2675 json_object_add_value_int(root, "timestamp", now.tv_sec);
2676 json_object_add_value_int(root, "timestamp_ms", ms_since_epoch);
2677 json_object_add_value_string(root, "time", time_buf);
2678 global = get_global_options();
2679 json_add_job_opts(root, "global options", &global->opt_list);
2680 array = json_create_array();
2681 json_object_add_value_array(root, "jobs", array);
2682 }
2683
2684 if (is_backend)
2685 fio_server_send_job_options(&get_global_options()->opt_list, -1U);
2686
2687 for (i = 0; i < nr_ts; i++) {
2688 ts = &threadstats[i];
2689 rs = &runstats[ts->groupid];
2690
2691 if (is_backend) {
2692 fio_server_send_job_options(opt_lists[i], i);
2693 fio_server_send_ts(ts, rs);
2694 } else {
2695 if (output_format & FIO_OUTPUT_TERSE)
2696 show_thread_status_terse(ts, rs, &output[__FIO_OUTPUT_TERSE]);
2697 if (output_format & FIO_OUTPUT_JSON) {
2698 struct json_object *tmp = show_thread_status_json(ts, rs, opt_lists[i]);
2699 json_array_add_value_object(array, tmp);
2700 }
2701 if (output_format & FIO_OUTPUT_NORMAL)
2702 show_thread_status_normal(ts, rs, &output[__FIO_OUTPUT_NORMAL]);
2703 }
2704 }
2705 if (!is_backend && (output_format & FIO_OUTPUT_JSON)) {
2706 /* disk util stats, if any */
2707 show_disk_util(1, root, &output[__FIO_OUTPUT_JSON]);
2708
2709 show_idle_prof_stats(FIO_OUTPUT_JSON, root, &output[__FIO_OUTPUT_JSON]);
2710
2711 json_print_object(root, &output[__FIO_OUTPUT_JSON]);
2712 log_buf(&output[__FIO_OUTPUT_JSON], "\n");
2713 json_free_object(root);
2714 }
2715
2716 for (i = 0; i < groupid + 1; i++) {
2717 rs = &runstats[i];
2718
2719 rs->groupid = i;
2720 if (is_backend)
2721 fio_server_send_gs(rs);
2722 else if (output_format & FIO_OUTPUT_NORMAL)
2723 show_group_stats(rs, &output[__FIO_OUTPUT_NORMAL]);
2724 }
2725
2726 if (is_backend)
2727 fio_server_send_du();
2728 else if (output_format & FIO_OUTPUT_NORMAL) {
2729 show_disk_util(0, NULL, &output[__FIO_OUTPUT_NORMAL]);
2730 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, &output[__FIO_OUTPUT_NORMAL]);
2731 }
2732
2733 for (i = 0; i < FIO_OUTPUT_NR; i++) {
2734 struct buf_output *out = &output[i];
2735
2736 log_info_buf(out->buf, out->buflen);
2737 buf_output_free(out);
2738 }
2739
2740 fio_idle_prof_cleanup();
2741
2742 log_info_flush();
2743 free(runstats);
2744
2745 /* free arrays allocated by sum_thread_stats(), if any */
2746 for (i = 0; i < nr_ts; i++) {
2747 ts = &threadstats[i];
2748 free_clat_prio_stats(ts);
2749 }
2750 free(threadstats);
2751 free(opt_lists);
2752}
2753
2754int __show_running_run_stats(void)
2755{
2756 unsigned long long *rt;
2757 struct timespec ts;
2758
2759 fio_sem_down(stat_sem);
2760
2761 rt = malloc(thread_number * sizeof(unsigned long long));
2762 fio_gettime(&ts, NULL);
2763
2764 for_each_td(td) {
2765 if (td->runstate >= TD_EXITED)
2766 continue;
2767
2768 td->update_rusage = 1;
2769 for_each_rw_ddir(ddir) {
2770 td->ts.io_bytes[ddir] = td->io_bytes[ddir];
2771 }
2772 td->ts.total_run_time = mtime_since(&td->epoch, &ts);
2773
2774 rt[__td_index] = mtime_since(&td->start, &ts);
2775 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
2776 td->ts.runtime[DDIR_READ] += rt[__td_index];
2777 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
2778 td->ts.runtime[DDIR_WRITE] += rt[__td_index];
2779 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
2780 td->ts.runtime[DDIR_TRIM] += rt[__td_index];
2781 } end_for_each();
2782
2783 for_each_td(td) {
2784 if (td->runstate >= TD_EXITED)
2785 continue;
2786 if (td->rusage_sem) {
2787 td->update_rusage = 1;
2788 fio_sem_down(td->rusage_sem);
2789 }
2790 td->update_rusage = 0;
2791 } end_for_each();
2792
2793 __show_run_stats();
2794
2795 for_each_td(td) {
2796 if (td->runstate >= TD_EXITED)
2797 continue;
2798
2799 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
2800 td->ts.runtime[DDIR_READ] -= rt[__td_index];
2801 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
2802 td->ts.runtime[DDIR_WRITE] -= rt[__td_index];
2803 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
2804 td->ts.runtime[DDIR_TRIM] -= rt[__td_index];
2805 } end_for_each();
2806
2807 free(rt);
2808 fio_sem_up(stat_sem);
2809
2810 return 0;
2811}
2812
2813static bool status_file_disabled;
2814
2815#define FIO_STATUS_FILE "fio-dump-status"
2816
2817static int check_status_file(void)
2818{
2819 struct stat sb;
2820 const char *temp_dir;
2821 char fio_status_file_path[PATH_MAX];
2822
2823 if (status_file_disabled)
2824 return 0;
2825
2826 temp_dir = getenv("TMPDIR");
2827 if (temp_dir == NULL) {
2828 temp_dir = getenv("TEMP");
2829 if (temp_dir && strlen(temp_dir) >= PATH_MAX)
2830 temp_dir = NULL;
2831 }
2832 if (temp_dir == NULL)
2833 temp_dir = "/tmp";
2834#ifdef __COVERITY__
2835 __coverity_tainted_data_sanitize__(temp_dir);
2836#endif
2837
2838 snprintf(fio_status_file_path, sizeof(fio_status_file_path), "%s/%s", temp_dir, FIO_STATUS_FILE);
2839
2840 if (stat(fio_status_file_path, &sb))
2841 return 0;
2842
2843 if (unlink(fio_status_file_path) < 0) {
2844 log_err("fio: failed to unlink %s: %s\n", fio_status_file_path,
2845 strerror(errno));
2846 log_err("fio: disabling status file updates\n");
2847 status_file_disabled = true;
2848 }
2849
2850 return 1;
2851}
2852
2853void check_for_running_stats(void)
2854{
2855 if (check_status_file()) {
2856 show_running_run_stats();
2857 return;
2858 }
2859}
2860
2861static inline void add_stat_sample(struct io_stat *is, unsigned long long data)
2862{
2863 double val = data;
2864 double delta;
2865
2866 if (data > is->max_val)
2867 is->max_val = data;
2868 if (data < is->min_val)
2869 is->min_val = data;
2870
2871 delta = val - is->mean.u.f;
2872 if (delta) {
2873 is->mean.u.f += delta / (is->samples + 1.0);
2874 is->S.u.f += delta * (val - is->mean.u.f);
2875 }
2876
2877 is->samples++;
2878}
2879
2880static inline void add_stat_prio_sample(struct clat_prio_stat *clat_prio,
2881 unsigned short clat_prio_index,
2882 unsigned long long nsec)
2883{
2884 if (clat_prio)
2885 add_stat_sample(&clat_prio[clat_prio_index].clat_stat, nsec);
2886}
2887
2888/*
2889 * Return a struct io_logs, which is added to the tail of the log
2890 * list for 'iolog'.
2891 */
2892static struct io_logs *get_new_log(struct io_log *iolog)
2893{
2894 size_t new_samples;
2895 struct io_logs *cur_log;
2896
2897 /*
2898 * Cap the size at MAX_LOG_ENTRIES, so we don't keep doubling
2899 * forever
2900 */
2901 if (!iolog->cur_log_max) {
2902 if (iolog->td)
2903 new_samples = iolog->td->o.log_entries;
2904 else
2905 new_samples = DEF_LOG_ENTRIES;
2906 } else {
2907 new_samples = iolog->cur_log_max * 2;
2908 if (new_samples > MAX_LOG_ENTRIES)
2909 new_samples = MAX_LOG_ENTRIES;
2910 }
2911
2912 cur_log = smalloc(sizeof(*cur_log));
2913 if (cur_log) {
2914 INIT_FLIST_HEAD(&cur_log->list);
2915 cur_log->log = calloc(new_samples, log_entry_sz(iolog));
2916 if (cur_log->log) {
2917 cur_log->nr_samples = 0;
2918 cur_log->max_samples = new_samples;
2919 flist_add_tail(&cur_log->list, &iolog->io_logs);
2920 iolog->cur_log_max = new_samples;
2921 return cur_log;
2922 }
2923 sfree(cur_log);
2924 }
2925
2926 return NULL;
2927}
2928
2929/*
2930 * Add and return a new log chunk, or return current log if big enough
2931 */
2932static struct io_logs *regrow_log(struct io_log *iolog)
2933{
2934 struct io_logs *cur_log;
2935 int i;
2936
2937 if (!iolog || iolog->disabled)
2938 goto disable;
2939
2940 cur_log = iolog_cur_log(iolog);
2941 if (!cur_log) {
2942 cur_log = get_new_log(iolog);
2943 if (!cur_log)
2944 return NULL;
2945 }
2946
2947 if (cur_log->nr_samples < cur_log->max_samples)
2948 return cur_log;
2949
2950 /*
2951 * No room for a new sample. If we're compressing on the fly, flush
2952 * out the current chunk
2953 */
2954 if (iolog->log_gz) {
2955 if (iolog_cur_flush(iolog, cur_log)) {
2956 log_err("fio: failed flushing iolog! Will stop logging.\n");
2957 return NULL;
2958 }
2959 }
2960
2961 /*
2962 * Get a new log array, and add to our list
2963 */
2964 cur_log = get_new_log(iolog);
2965 if (!cur_log) {
2966 log_err("fio: failed extending iolog! Will stop logging.\n");
2967 return NULL;
2968 }
2969
2970 if (!iolog->pending || !iolog->pending->nr_samples)
2971 return cur_log;
2972
2973 /*
2974 * Flush pending items to new log
2975 */
2976 for (i = 0; i < iolog->pending->nr_samples; i++) {
2977 struct io_sample *src, *dst;
2978
2979 src = get_sample(iolog, iolog->pending, i);
2980 dst = get_sample(iolog, cur_log, i);
2981 memcpy(dst, src, log_entry_sz(iolog));
2982 }
2983 cur_log->nr_samples = iolog->pending->nr_samples;
2984
2985 iolog->pending->nr_samples = 0;
2986 return cur_log;
2987disable:
2988 if (iolog)
2989 iolog->disabled = true;
2990 return NULL;
2991}
2992
2993void regrow_logs(struct thread_data *td)
2994{
2995 regrow_log(td->slat_log);
2996 regrow_log(td->clat_log);
2997 regrow_log(td->clat_hist_log);
2998 regrow_log(td->lat_log);
2999 regrow_log(td->bw_log);
3000 regrow_log(td->iops_log);
3001 td->flags &= ~TD_F_REGROW_LOGS;
3002}
3003
3004void regrow_agg_logs(void)
3005{
3006 enum fio_ddir ddir;
3007
3008 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
3009 regrow_log(agg_io_log[ddir]);
3010}
3011
3012static struct io_logs *get_cur_log(struct io_log *iolog)
3013{
3014 struct io_logs *cur_log;
3015
3016 cur_log = iolog_cur_log(iolog);
3017 if (!cur_log) {
3018 cur_log = get_new_log(iolog);
3019 if (!cur_log)
3020 return NULL;
3021 }
3022
3023 if (cur_log->nr_samples < cur_log->max_samples)
3024 return cur_log;
3025
3026 /*
3027 * Out of space. If we're in IO offload mode, or we're not doing
3028 * per unit logging (hence logging happens outside of the IO thread
3029 * as well), add a new log chunk inline. If we're doing inline
3030 * submissions, flag 'td' as needing a log regrow and we'll take
3031 * care of it on the submission side.
3032 */
3033 if ((iolog->td && iolog->td->o.io_submit_mode == IO_MODE_OFFLOAD) ||
3034 !per_unit_log(iolog))
3035 return regrow_log(iolog);
3036
3037 if (iolog->td)
3038 iolog->td->flags |= TD_F_REGROW_LOGS;
3039 if (iolog->pending)
3040 assert(iolog->pending->nr_samples < iolog->pending->max_samples);
3041 return iolog->pending;
3042}
3043
3044static void __add_log_sample(struct io_log *iolog, unsigned long t,
3045 struct log_sample *sample)
3046{
3047 struct io_logs *cur_log;
3048
3049 if (iolog->disabled)
3050 return;
3051 if (flist_empty(&iolog->io_logs))
3052 iolog->avg_last[sample->ddir] = t;
3053
3054 cur_log = get_cur_log(iolog);
3055 if (cur_log) {
3056 struct io_sample *s;
3057
3058 s = get_sample(iolog, cur_log, cur_log->nr_samples);
3059
3060 s->data = sample->data;
3061 s->time = t;
3062 if (iolog->td && iolog->td->o.log_alternate_epoch)
3063 s->time += iolog->td->alternate_epoch;
3064 io_sample_set_ddir(iolog, s, sample->ddir);
3065 s->bs = sample->bs;
3066 s->priority = sample->priority;
3067
3068 if (iolog->log_offset)
3069 s->aux[IOS_AUX_OFFSET_INDEX] = sample->offset;
3070
3071 if (iolog->log_issue_time)
3072 s->aux[IOS_AUX_ISSUE_TIME_INDEX] = sample->issue_time;
3073
3074 cur_log->nr_samples++;
3075 return;
3076 }
3077
3078 iolog->disabled = true;
3079}
3080
3081static inline void reset_io_stat(struct io_stat *ios)
3082{
3083 ios->min_val = -1ULL;
3084 ios->max_val = ios->samples = 0;
3085 ios->mean.u.f = ios->S.u.f = 0;
3086}
3087
3088static inline void reset_io_u_plat(uint64_t *io_u_plat)
3089{
3090 int i;
3091
3092 for (i = 0; i < FIO_IO_U_PLAT_NR; i++)
3093 io_u_plat[i] = 0;
3094}
3095
3096static inline void reset_clat_prio_stats(struct thread_stat *ts)
3097{
3098 enum fio_ddir ddir;
3099 int i;
3100
3101 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
3102 if (!ts->clat_prio[ddir])
3103 continue;
3104
3105 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
3106 reset_io_stat(&ts->clat_prio[ddir][i].clat_stat);
3107 reset_io_u_plat(ts->clat_prio[ddir][i].io_u_plat);
3108 }
3109 }
3110}
3111
3112void reset_io_stats(struct thread_data *td)
3113{
3114 struct thread_stat *ts = &td->ts;
3115 int i, j;
3116
3117 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
3118 reset_io_stat(&ts->clat_stat[i]);
3119 reset_io_stat(&ts->slat_stat[i]);
3120 reset_io_stat(&ts->lat_stat[i]);
3121 reset_io_stat(&ts->bw_stat[i]);
3122 reset_io_stat(&ts->iops_stat[i]);
3123
3124 ts->io_bytes[i] = 0;
3125 ts->runtime[i] = 0;
3126 ts->total_io_u[i] = 0;
3127 ts->short_io_u[i] = 0;
3128 ts->drop_io_u[i] = 0;
3129 }
3130
3131 for (i = 0; i < FIO_LAT_CNT; i++)
3132 for (j = 0; j < DDIR_RWDIR_CNT; j++)
3133 reset_io_u_plat(ts->io_u_plat[i][j]);
3134
3135 reset_clat_prio_stats(ts);
3136
3137 ts->total_io_u[DDIR_SYNC] = 0;
3138 reset_io_u_plat(ts->io_u_sync_plat);
3139
3140 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
3141 ts->io_u_map[i] = 0;
3142 ts->io_u_submit[i] = 0;
3143 ts->io_u_complete[i] = 0;
3144 }
3145
3146 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
3147 ts->io_u_lat_n[i] = 0;
3148 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
3149 ts->io_u_lat_u[i] = 0;
3150 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
3151 ts->io_u_lat_m[i] = 0;
3152
3153 ts->total_submit = 0;
3154 ts->total_complete = 0;
3155 ts->nr_zone_resets = 0;
3156 ts->cachehit = ts->cachemiss = 0;
3157}
3158
3159static void __add_stat_to_log(struct io_log *iolog, enum fio_ddir ddir,
3160 unsigned long elapsed, int log_max)
3161{
3162 /*
3163 * Note an entry in the log. Use the mean from the logged samples,
3164 * making sure to properly round up. Only write a log entry if we
3165 * had actual samples done.
3166 */
3167 if (iolog->avg_window[ddir].samples) {
3168 struct log_sample sample = { {{ 0, 0 }}, ddir, 0, 0, 0, 0 };
3169 union io_sample_data *d = &sample.data;
3170
3171 if (log_max == IO_LOG_SAMPLE_AVG) {
3172 d->val.val0 = iolog->avg_window[ddir].mean.u.f + 0.50;
3173 d->val.val1 = 0;
3174 } else if (log_max == IO_LOG_SAMPLE_MAX) {
3175 d->val.val0 = iolog->avg_window[ddir].max_val;
3176 d->val.val1 = 0;
3177 } else {
3178 d->val.val0 = iolog->avg_window[ddir].mean.u.f + 0.50;
3179 d->val.val1 = iolog->avg_window[ddir].max_val;
3180 }
3181
3182 __add_log_sample(iolog, elapsed, &sample);
3183 }
3184
3185 reset_io_stat(&iolog->avg_window[ddir]);
3186}
3187
3188static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed,
3189 int log_max)
3190{
3191 enum fio_ddir ddir;
3192
3193 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
3194 __add_stat_to_log(iolog, ddir, elapsed, log_max);
3195}
3196
3197static unsigned long add_log_sample(struct thread_data *td,
3198 struct io_log *iolog,
3199 struct log_sample *sample)
3200{
3201 unsigned long elapsed, this_window;
3202 enum fio_ddir ddir = sample->ddir;
3203
3204 if (!ddir_rw(ddir))
3205 return 0;
3206
3207 elapsed = mtime_since_now(&td->epoch);
3208
3209 /*
3210 * If no time averaging, just add the log sample.
3211 */
3212 if (!iolog->avg_msec) {
3213 __add_log_sample(iolog, elapsed, sample);
3214 return 0;
3215 }
3216
3217 /*
3218 * Add the sample. If the time period has passed, then
3219 * add that entry to the log and clear.
3220 */
3221 add_stat_sample(&iolog->avg_window[ddir], sample->data.val.val0);
3222
3223 /*
3224 * If period hasn't passed, adding the above sample is all we
3225 * need to do.
3226 */
3227 this_window = elapsed - iolog->avg_last[ddir];
3228 if (elapsed < iolog->avg_last[ddir])
3229 return iolog->avg_last[ddir] - elapsed;
3230 else if (this_window < iolog->avg_msec) {
3231 unsigned long diff = iolog->avg_msec - this_window;
3232
3233 if (inline_log(iolog) || diff > LOG_MSEC_SLACK)
3234 return diff;
3235 }
3236
3237 __add_stat_to_log(iolog, ddir, elapsed, td->o.log_max);
3238
3239 iolog->avg_last[ddir] = elapsed - (elapsed % iolog->avg_msec);
3240
3241 return iolog->avg_msec;
3242}
3243
3244void finalize_logs(struct thread_data *td, bool unit_logs)
3245{
3246 unsigned long elapsed;
3247
3248 elapsed = mtime_since_now(&td->epoch);
3249
3250 if (td->clat_log && unit_logs)
3251 _add_stat_to_log(td->clat_log, elapsed, td->o.log_max);
3252 if (td->slat_log && unit_logs)
3253 _add_stat_to_log(td->slat_log, elapsed, td->o.log_max);
3254 if (td->lat_log && unit_logs)
3255 _add_stat_to_log(td->lat_log, elapsed, td->o.log_max);
3256 if (td->bw_log && (unit_logs == per_unit_log(td->bw_log)))
3257 _add_stat_to_log(td->bw_log, elapsed, td->o.log_max);
3258 if (td->iops_log && (unit_logs == per_unit_log(td->iops_log)))
3259 _add_stat_to_log(td->iops_log, elapsed, td->o.log_max);
3260}
3261
3262void add_agg_sample(union io_sample_data data, enum fio_ddir ddir,
3263 unsigned long long bs)
3264{
3265 struct io_log *iolog;
3266 struct log_sample sample = { data, ddir, bs, 0, 0, 0 };
3267
3268 if (!ddir_rw(ddir))
3269 return;
3270
3271 iolog = agg_io_log[ddir];
3272 __add_log_sample(iolog, mtime_since_genesis(), &sample);
3273}
3274
3275void add_sync_clat_sample(struct thread_stat *ts, unsigned long long nsec)
3276{
3277 unsigned int idx = plat_val_to_idx(nsec);
3278 assert(idx < FIO_IO_U_PLAT_NR);
3279
3280 ts->io_u_sync_plat[idx]++;
3281 add_stat_sample(&ts->sync_stat, nsec);
3282}
3283
3284static inline void add_lat_percentile_sample(struct thread_stat *ts,
3285 unsigned long long nsec,
3286 enum fio_ddir ddir,
3287 enum fio_lat lat)
3288{
3289 unsigned int idx = plat_val_to_idx(nsec);
3290 assert(idx < FIO_IO_U_PLAT_NR);
3291
3292 ts->io_u_plat[lat][ddir][idx]++;
3293}
3294
3295static inline void
3296add_lat_percentile_prio_sample(struct thread_stat *ts, unsigned long long nsec,
3297 enum fio_ddir ddir,
3298 unsigned short clat_prio_index)
3299{
3300 unsigned int idx = plat_val_to_idx(nsec);
3301
3302 if (ts->clat_prio[ddir])
3303 ts->clat_prio[ddir][clat_prio_index].io_u_plat[idx]++;
3304}
3305
3306void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
3307 unsigned long long nsec, unsigned long long bs,
3308 struct io_u *io_u)
3309{
3310 const bool needs_lock = td_async_processing(td);
3311 unsigned long elapsed, this_window;
3312 struct thread_stat *ts = &td->ts;
3313 struct io_log *iolog = td->clat_hist_log;
3314 uint64_t offset = 0;
3315 unsigned int ioprio = 0;
3316 unsigned short clat_prio_index = 0;
3317
3318 if (needs_lock)
3319 __td_io_u_lock(td);
3320
3321 if (io_u) {
3322 offset = io_u->offset;
3323 ioprio = io_u->ioprio;
3324 clat_prio_index = io_u->clat_prio_index;
3325 }
3326
3327 add_stat_sample(&ts->clat_stat[ddir], nsec);
3328
3329 /*
3330 * When lat_percentiles=1 (default 0), the reported per priority
3331 * percentiles and stats are used for describing total latency values,
3332 * even though the variable names themselves start with clat_.
3333 *
3334 * Because of the above definition, add a prio stat sample only when
3335 * lat_percentiles=0. add_lat_sample() will add the prio stat sample
3336 * when lat_percentiles=1.
3337 */
3338 if (!ts->lat_percentiles)
3339 add_stat_prio_sample(ts->clat_prio[ddir], clat_prio_index,
3340 nsec);
3341
3342 if (td->clat_log) {
3343 struct log_sample sample = { sample_val(nsec), ddir, bs,
3344 offset, ioprio, 0 };
3345
3346 if (io_u)
3347 sample.issue_time =
3348 ntime_since(&td->epoch, &io_u->issue_time);
3349
3350 add_log_sample(td, td->clat_log, &sample);
3351 }
3352
3353 if (ts->clat_percentiles) {
3354 /*
3355 * Because of the above definition, add a prio lat percentile
3356 * sample only when lat_percentiles=0. add_lat_sample() will add
3357 * the prio lat percentile sample when lat_percentiles=1.
3358 */
3359 add_lat_percentile_sample(ts, nsec, ddir, FIO_CLAT);
3360 if (!ts->lat_percentiles)
3361 add_lat_percentile_prio_sample(ts, nsec, ddir,
3362 clat_prio_index);
3363 }
3364
3365 if (iolog && iolog->hist_msec) {
3366 struct io_hist *hw = &iolog->hist_window[ddir];
3367
3368 hw->samples++;
3369 elapsed = mtime_since_now(&td->epoch);
3370 if (!hw->hist_last)
3371 hw->hist_last = elapsed;
3372 this_window = elapsed - hw->hist_last;
3373
3374 if (this_window >= iolog->hist_msec) {
3375 uint64_t *io_u_plat;
3376 struct io_u_plat_entry *dst;
3377 struct log_sample sample = { {{ 0, 0 }}, ddir, bs,
3378 offset, ioprio, 0 };
3379
3380 /*
3381 * Make a byte-for-byte copy of the latency histogram
3382 * stored in td->ts.io_u_plat[ddir], recording it in a
3383 * log sample. Note that the matching call to free() is
3384 * located in iolog.c after printing this sample to the
3385 * log file.
3386 */
3387 io_u_plat = (uint64_t *) td->ts.io_u_plat[FIO_CLAT][ddir];
3388 dst = malloc(sizeof(struct io_u_plat_entry));
3389 memcpy(&(dst->io_u_plat), io_u_plat,
3390 FIO_IO_U_PLAT_NR * sizeof(uint64_t));
3391 flist_add(&dst->list, &hw->list);
3392
3393 sample.data = sample_plat(dst);
3394 __add_log_sample(iolog, elapsed, &sample);
3395
3396 /*
3397 * Update the last time we recorded as being now, minus
3398 * any drift in time we encountered before actually
3399 * making the record.
3400 */
3401 hw->hist_last = elapsed - (this_window - iolog->hist_msec);
3402 hw->samples = 0;
3403 }
3404 }
3405
3406 if (needs_lock)
3407 __td_io_u_unlock(td);
3408}
3409
3410void add_slat_sample(struct thread_data *td, struct io_u *io_u)
3411{
3412 const bool needs_lock = td_async_processing(td);
3413 struct thread_stat *ts = &td->ts;
3414 enum fio_ddir ddir;
3415 unsigned long long nsec;
3416
3417 ddir = io_u->ddir;
3418 if (!ddir_rw(ddir))
3419 return;
3420
3421 if (needs_lock)
3422 __td_io_u_lock(td);
3423
3424 nsec = ntime_since(&io_u->start_time, &io_u->issue_time);
3425
3426 add_stat_sample(&ts->slat_stat[ddir], nsec);
3427
3428 if (td->slat_log) {
3429 struct log_sample sample = { sample_val(nsec), ddir,
3430 io_u->xfer_buflen, io_u->offset, io_u->ioprio,
3431 ntime_since(&td->epoch, &io_u->issue_time) };
3432
3433 add_log_sample(td, td->slat_log, &sample);
3434 }
3435
3436 if (ts->slat_percentiles)
3437 add_lat_percentile_sample(ts, nsec, ddir, FIO_SLAT);
3438
3439 if (needs_lock)
3440 __td_io_u_unlock(td);
3441}
3442
3443void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
3444 unsigned long long nsec, unsigned long long bs,
3445 struct io_u * io_u)
3446{
3447 const bool needs_lock = td_async_processing(td);
3448 struct thread_stat *ts = &td->ts;
3449
3450 if (!ddir_rw(ddir))
3451 return;
3452
3453 if (needs_lock)
3454 __td_io_u_lock(td);
3455
3456 add_stat_sample(&ts->lat_stat[ddir], nsec);
3457
3458 if (td->lat_log) {
3459 struct log_sample sample = { sample_val(nsec), ddir, bs,
3460 io_u->offset, io_u->ioprio, 0 };
3461
3462 add_log_sample(td, td->lat_log, &sample);
3463 }
3464
3465 /*
3466 * When lat_percentiles=1 (default 0), the reported per priority
3467 * percentiles and stats are used for describing total latency values,
3468 * even though the variable names themselves start with clat_.
3469 *
3470 * Because of the above definition, add a prio stat and prio lat
3471 * percentile sample only when lat_percentiles=1. add_clat_sample() will
3472 * add the prio stat and prio lat percentile sample when
3473 * lat_percentiles=0.
3474 */
3475 if (ts->lat_percentiles) {
3476 add_lat_percentile_sample(ts, nsec, ddir, FIO_LAT);
3477 add_lat_percentile_prio_sample(ts, nsec, ddir,
3478 io_u->clat_prio_index);
3479 add_stat_prio_sample(ts->clat_prio[ddir], io_u->clat_prio_index,
3480 nsec);
3481 }
3482 if (needs_lock)
3483 __td_io_u_unlock(td);
3484}
3485
3486void add_bw_sample(struct thread_data *td, struct io_u *io_u,
3487 unsigned int bytes, unsigned long long spent)
3488{
3489 const bool needs_lock = td_async_processing(td);
3490 struct thread_stat *ts = &td->ts;
3491 unsigned long rate;
3492
3493 if (spent)
3494 rate = (unsigned long) (bytes * 1000000ULL / spent);
3495 else
3496 rate = 0;
3497
3498 if (needs_lock)
3499 __td_io_u_lock(td);
3500
3501 add_stat_sample(&ts->bw_stat[io_u->ddir], rate);
3502
3503 if (td->bw_log) {
3504 struct log_sample sample = { sample_val(rate), io_u->ddir,
3505 bytes, io_u->offset, io_u->ioprio, 0 };
3506
3507 add_log_sample(td, td->bw_log, &sample);
3508 }
3509
3510 td->stat_io_bytes[io_u->ddir] = td->this_io_bytes[io_u->ddir];
3511
3512 if (needs_lock)
3513 __td_io_u_unlock(td);
3514}
3515
3516static int __add_samples(struct thread_data *td, struct timespec *parent_tv,
3517 struct timespec *t, unsigned int avg_time,
3518 uint64_t *this_io_bytes, uint64_t *stat_io_bytes,
3519 struct io_stat *stat, struct io_log *log,
3520 bool is_kb)
3521{
3522 const bool needs_lock = td_async_processing(td);
3523 unsigned long spent, rate;
3524 enum fio_ddir ddir;
3525 unsigned long next, next_log;
3526
3527 next_log = avg_time;
3528
3529 spent = mtime_since(parent_tv, t);
3530 if (spent < avg_time && avg_time - spent > LOG_MSEC_SLACK)
3531 return avg_time - spent;
3532
3533 if (needs_lock)
3534 __td_io_u_lock(td);
3535
3536 /*
3537 * Compute both read and write rates for the interval.
3538 */
3539 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
3540 uint64_t delta;
3541
3542 delta = this_io_bytes[ddir] - stat_io_bytes[ddir];
3543 if (!delta)
3544 continue; /* No entries for interval */
3545
3546 if (spent) {
3547 if (is_kb)
3548 rate = delta * 1000 / spent / 1024; /* KiB/s */
3549 else
3550 rate = (delta * 1000) / spent;
3551 } else
3552 rate = 0;
3553
3554 add_stat_sample(&stat[ddir], rate);
3555
3556 if (log) {
3557 struct log_sample sample = {
3558 sample_val(rate), ddir, 0, 0, 0, 0 };
3559
3560 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
3561 sample.bs = td->o.min_bs[ddir];
3562 next = add_log_sample(td, log, &sample);
3563 next_log = min(next_log, next);
3564 }
3565
3566 stat_io_bytes[ddir] = this_io_bytes[ddir];
3567 }
3568
3569 *parent_tv = *t;
3570
3571 if (needs_lock)
3572 __td_io_u_unlock(td);
3573
3574 if (spent <= avg_time)
3575 next = avg_time;
3576 else
3577 next = avg_time - (1 + spent - avg_time);
3578
3579 return min(next, next_log);
3580}
3581
3582static int add_bw_samples(struct thread_data *td, struct timespec *t)
3583{
3584 return __add_samples(td, &td->bw_sample_time, t, td->o.bw_avg_time,
3585 td->this_io_bytes, td->stat_io_bytes,
3586 td->ts.bw_stat, td->bw_log, true);
3587}
3588
3589void add_iops_sample(struct thread_data *td, struct io_u *io_u,
3590 unsigned int bytes)
3591{
3592 const bool needs_lock = td_async_processing(td);
3593 struct thread_stat *ts = &td->ts;
3594
3595 if (needs_lock)
3596 __td_io_u_lock(td);
3597
3598 add_stat_sample(&ts->iops_stat[io_u->ddir], 1);
3599
3600 if (td->iops_log) {
3601 struct log_sample sample = { sample_val(1), io_u->ddir, bytes,
3602 io_u->offset, io_u->ioprio, 0 };
3603
3604 add_log_sample(td, td->iops_log, &sample);
3605 }
3606
3607 td->stat_io_blocks[io_u->ddir] = td->this_io_blocks[io_u->ddir];
3608
3609 if (needs_lock)
3610 __td_io_u_unlock(td);
3611}
3612
3613static int add_iops_samples(struct thread_data *td, struct timespec *t)
3614{
3615 return __add_samples(td, &td->iops_sample_time, t, td->o.iops_avg_time,
3616 td->this_io_blocks, td->stat_io_blocks,
3617 td->ts.iops_stat, td->iops_log, false);
3618}
3619
3620static bool td_in_logging_state(struct thread_data *td)
3621{
3622 if (in_ramp_time(td))
3623 return false;
3624
3625 switch(td->runstate) {
3626 case TD_RUNNING:
3627 case TD_VERIFYING:
3628 case TD_FINISHING:
3629 case TD_EXITED:
3630 return true;
3631 default:
3632 return false;
3633 }
3634}
3635
3636/*
3637 * Returns msecs to next event
3638 */
3639int calc_log_samples(void)
3640{
3641 unsigned int next = ~0U, tmp = 0, next_mod = 0, log_avg_msec_min = -1U;
3642 struct timespec now;
3643 long elapsed_time = 0;
3644
3645 for_each_td(td) {
3646 fio_gettime(&now, NULL);
3647 elapsed_time = mtime_since(&td->epoch, &now);
3648
3649 if (!td->o.stats)
3650 continue;
3651 if (!td_in_logging_state(td)) {
3652 next = min(td->o.iops_avg_time, td->o.bw_avg_time);
3653 continue;
3654 }
3655 if (!td->bw_log ||
3656 (td->bw_log && !per_unit_log(td->bw_log))) {
3657 tmp = add_bw_samples(td, &now);
3658
3659 if (td->bw_log)
3660 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->bw_log->avg_msec);
3661 }
3662 if (!td->iops_log ||
3663 (td->iops_log && !per_unit_log(td->iops_log))) {
3664 tmp = add_iops_samples(td, &now);
3665
3666 if (td->iops_log)
3667 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->iops_log->avg_msec);
3668 }
3669
3670 if (tmp < next)
3671 next = tmp;
3672 } end_for_each();
3673
3674 /* if log_avg_msec_min has not been changed, set it to 0 */
3675 if (log_avg_msec_min == -1U)
3676 log_avg_msec_min = 0;
3677
3678 if (log_avg_msec_min == 0)
3679 next_mod = elapsed_time;
3680 else
3681 next_mod = elapsed_time % log_avg_msec_min;
3682
3683 /* correction to keep the time on the log avg msec boundary */
3684 next = min(next, (log_avg_msec_min - next_mod));
3685
3686 return next == ~0U ? 0 : next;
3687}
3688
3689void stat_init(void)
3690{
3691 stat_sem = fio_sem_init(FIO_SEM_UNLOCKED);
3692}
3693
3694void stat_exit(void)
3695{
3696 /*
3697 * When we have the mutex, we know out-of-band access to it
3698 * have ended.
3699 */
3700 fio_sem_down(stat_sem);
3701 fio_sem_remove(stat_sem);
3702}
3703
3704/*
3705 * Called from signal handler. Wake up status thread.
3706 */
3707void show_running_run_stats(void)
3708{
3709 helper_do_stat();
3710}
3711
3712uint32_t *io_u_block_info(struct thread_data *td, struct io_u *io_u)
3713{
3714 /* Ignore io_u's which span multiple blocks--they will just get
3715 * inaccurate counts. */
3716 int idx = (io_u->offset - io_u->file->file_offset)
3717 / td->o.bs[DDIR_TRIM];
3718 uint32_t *info = &td->ts.block_infos[idx];
3719 assert(idx < td->ts.nr_block_infos);
3720 return info;
3721}
3722