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