More fio.h cleanups
[fio.git] / stat.c
CommitLineData
3c39a379
JA
1#include <stdio.h>
2#include <string.h>
3#include <sys/time.h>
4#include <sys/types.h>
5c4e1dbc 5#include <sys/stat.h>
3c39a379
JA
6#include <dirent.h>
7#include <libgen.h>
8#include <math.h>
9
10#include "fio.h"
7c9b1bce 11#include "diskutil.h"
3c39a379 12
dbe1125e 13/*
34403fb1 14 * Cheesy number->string conversion, complete with carry rounding error.
dbe1125e 15 */
b3605062 16static char *num2str(unsigned long num, int maxlen, int base, int pow2)
dbe1125e 17{
b3605062
JA
18 char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
19 unsigned int thousand;
dbe1125e
JA
20 char *buf;
21 int i;
22
b3605062
JA
23 if (pow2)
24 thousand = 1024;
25 else
26 thousand = 1000;
27
dbe1125e
JA
28 buf = malloc(128);
29
30 for (i = 0; base > 1; i++)
31 base /= thousand;
32
33 do {
34 int len, carry = 0;
35
36 len = sprintf(buf, "%'lu", num);
37 if (len <= maxlen) {
b3605062
JA
38 if (i >= 1) {
39 buf[len] = postfix[i];
40 buf[len + 1] = '\0';
41 }
dbe1125e
JA
42 return buf;
43 }
44
45 if ((num % thousand) >= (thousand / 2))
46 carry = 1;
47
48 num /= thousand;
49 num += carry;
50 i++;
f3502ba2 51 } while (i <= 5);
dbe1125e
JA
52
53 return buf;
54}
55
3c39a379
JA
56void update_rusage_stat(struct thread_data *td)
57{
756867bd 58 struct thread_stat *ts = &td->ts;
3c39a379 59
079ad09b
JA
60 getrusage(RUSAGE_SELF, &ts->ru_end);
61
5ec10eaa
JA
62 ts->usr_time += mtime_since(&ts->ru_start.ru_utime,
63 &ts->ru_end.ru_utime);
64 ts->sys_time += mtime_since(&ts->ru_start.ru_stime,
65 &ts->ru_end.ru_stime);
66 ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw
67 - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw);
e7823a94
JA
68 ts->minf += ts->ru_end.ru_minflt - ts->ru_start.ru_minflt;
69 ts->majf += ts->ru_end.ru_majflt - ts->ru_start.ru_majflt;
5ec10eaa 70
079ad09b 71 memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end));
3c39a379
JA
72}
73
74static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
75 double *mean, double *dev)
76{
68704084 77 double n = is->samples;
3c39a379
JA
78
79 if (is->samples == 0)
80 return 0;
81
82 *min = is->min_val;
83 *max = is->max_val;
84
85 n = (double) is->samples;
68704084 86 *mean = is->mean;
e6d276f2 87
68704084
JA
88 if (n > 1.0)
89 *dev = sqrt(is->S / (n - 1.0));
ef9c5c40 90 else
4b43f54e 91 *dev = 0;
ef9c5c40 92
3c39a379
JA
93 return 1;
94}
95
96static void show_group_stats(struct group_run_stats *rs, int id)
97{
dbe1125e
JA
98 char *p1, *p2, *p3, *p4;
99 const char *ddir_str[] = { " READ", " WRITE" };
100 int i;
101
6d86144d 102 log_info("\nRun status group %d (all jobs):\n", id);
3c39a379 103
dbe1125e
JA
104 for (i = 0; i <= DDIR_WRITE; i++) {
105 if (!rs->max_run[i])
106 continue;
107
8879fd15
JA
108 p1 = num2str(rs->io_kb[i], 6, 1024, 1);
109 p2 = num2str(rs->agg[i], 6, 1024, 1);
110 p3 = num2str(rs->min_bw[i], 6, 1024, 1);
111 p4 = num2str(rs->max_bw[i], 6, 1024, 1);
dbe1125e 112
5ec10eaa
JA
113 log_info("%s: io=%siB, aggrb=%siB/s, minb=%siB/s, maxb=%siB/s,"
114 " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
115 p3, p4, rs->min_run[i],
116 rs->max_run[i]);
dbe1125e
JA
117
118 free(p1);
119 free(p2);
120 free(p3);
121 free(p4);
122 }
3c39a379
JA
123}
124
b3605062
JA
125#define ts_total_io_u(ts) \
126 ((ts)->total_io_u[0] + (ts)->total_io_u[1])
127
838bc709
JA
128static void stat_calc_dist(unsigned int *map, unsigned long total,
129 double *io_u_dist)
2270890c
JA
130{
131 int i;
132
133 /*
134 * Do depth distribution calculations
135 */
136 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
838bc709
JA
137 if (total) {
138 io_u_dist[i] = (double) map[i] / (double) total;
139 io_u_dist[i] *= 100.0;
140 if (io_u_dist[i] < 0.1 && map[i])
141 io_u_dist[i] = 0.1;
142 } else
143 io_u_dist[i] = 0.0;
2270890c
JA
144 }
145}
146
04a0feae
JA
147static void stat_calc_lat(struct thread_stat *ts, double *dst,
148 unsigned int *src, int nr)
2270890c 149{
838bc709 150 unsigned long total = ts_total_io_u(ts);
2270890c
JA
151 int i;
152
153 /*
154 * Do latency distribution calculations
155 */
04a0feae 156 for (i = 0; i < nr; i++) {
838bc709
JA
157 if (total) {
158 dst[i] = (double) src[i] / (double) total;
159 dst[i] *= 100.0;
160 if (dst[i] < 0.01 && src[i])
161 dst[i] = 0.01;
162 } else
163 dst[i] = 0.0;
2270890c
JA
164 }
165}
166
04a0feae
JA
167static void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
168{
169 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
170}
171
172static void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
173{
174 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
175}
176
ea2accc5
JA
177static int usec_to_msec(unsigned long *min, unsigned long *max, double *mean,
178 double *dev)
179{
180 if (*min > 1000 && *max > 1000 && *mean > 1000.0 && *dev > 1000.0) {
181 *min /= 1000;
182 *max /= 1000;
183 *mean /= 1000.0;
184 *dev /= 1000.0;
185 return 0;
186 }
187
188 return 1;
189}
190
756867bd 191static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
3c39a379
JA
192 int ddir)
193{
3c9b60c1 194 const char *ddir_str[] = { "read ", "write" };
8879fd15 195 unsigned long min, max, runt;
b3605062 196 unsigned long long bw, iops;
3c39a379 197 double mean, dev;
b3605062 198 char *io_p, *bw_p, *iops_p;
3c39a379 199
756867bd 200 if (!ts->runtime[ddir])
3c39a379
JA
201 return;
202
8879fd15
JA
203 runt = ts->runtime[ddir];
204
205 bw = (1000 * ts->io_bytes[ddir]) / runt;
206 io_p = num2str(ts->io_bytes[ddir] >> 10, 6, 1024, 1);
207 bw_p = num2str(bw >> 10, 6, 1024, 1);
208
209 iops = (1000 * ts->total_io_u[ddir]) / runt;
b3605062 210 iops_p = num2str(iops, 6, 1, 0);
dbe1125e 211
5ec10eaa
JA
212 log_info(" %s: io=%siB, bw=%siB/s, iops=%s, runt=%6lumsec\n",
213 ddir_str[ddir], io_p, bw_p, iops_p,
214 ts->runtime[ddir]);
dbe1125e
JA
215
216 free(io_p);
217 free(bw_p);
b3605062 218 free(iops_p);
3c39a379 219
d85f5118
JA
220 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
221 const char *base = "(usec)";
d9309cb1 222 char *minp, *maxp;
d85f5118 223
ea2accc5 224 if (!usec_to_msec(&min, &max, &mean, &dev))
d85f5118 225 base = "(msec)";
ea2accc5 226
d9309cb1
JA
227 minp = num2str(min, 6, 1, 0);
228 maxp = num2str(max, 6, 1, 0);
229
5ec10eaa
JA
230 log_info(" slat %s: min=%s, max=%s, avg=%5.02f,"
231 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
d9309cb1
JA
232
233 free(minp);
234 free(maxp);
d85f5118
JA
235 }
236 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
237 const char *base = "(usec)";
d9309cb1 238 char *minp, *maxp;
d85f5118 239
ea2accc5
JA
240 if (!usec_to_msec(&min, &max, &mean, &dev))
241 base = "(msec)";
242
d9309cb1
JA
243 minp = num2str(min, 6, 1, 0);
244 maxp = num2str(max, 6, 1, 0);
5ec10eaa
JA
245
246 log_info(" clat %s: min=%s, max=%s, avg=%5.02f,"
247 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
d9309cb1
JA
248
249 free(minp);
250 free(maxp);
d85f5118 251 }
079ad09b 252 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
3c39a379
JA
253 double p_of_agg;
254
255 p_of_agg = mean * 100 / (double) rs->agg[ddir];
5ec10eaa
JA
256 log_info(" bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
257 " avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg,
258 mean, dev);
3c39a379
JA
259 }
260}
261
04a0feae
JA
262static void show_lat(double *io_u_lat, int nr, const char **ranges,
263 const char *msg)
264{
265 int new_line = 1, i, line = 0;
266
267 for (i = 0; i < nr; i++) {
268 if (io_u_lat[i] <= 0.0)
269 continue;
270 if (new_line) {
4539ed73
JA
271 if (line)
272 log_info("\n");
04a0feae
JA
273 log_info(" lat (%s): ", msg);
274 new_line = 0;
275 line = 0;
276 }
277 if (line)
278 log_info(", ");
279 log_info("%s%3.2f%%", ranges[i], io_u_lat[i]);
280 line++;
281 if (line == 5)
282 new_line = 1;
283 }
04a0feae
JA
284}
285
286static void show_lat_u(double *io_u_lat_u)
287{
288 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
289 "250=", "500=", "750=", "1000=", };
290
291 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec");
292}
293
294static void show_lat_m(double *io_u_lat_m)
295{
296 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
297 "250=", "500=", "750=", "1000=", "2000=",
298 ">=2000=", };
299
300 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec");
301}
302
303static void show_latencies(double *io_u_lat_u, double *io_u_lat_m)
304{
305 show_lat_u(io_u_lat_u);
4539ed73 306 log_info("\n");
04a0feae
JA
307 show_lat_m(io_u_lat_m);
308 log_info("\n");
309}
310
756867bd 311static void show_thread_status(struct thread_stat *ts,
3c39a379
JA
312 struct group_run_stats *rs)
313{
314 double usr_cpu, sys_cpu;
69008999 315 unsigned long runtime;
71619dc2 316 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
317 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
318 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
3c39a379 319
b4c5e1ac
JA
320 if (!(ts->io_bytes[0] + ts->io_bytes[1]) &&
321 !(ts->total_io_u[0] + ts->total_io_u[1]))
3c39a379
JA
322 return;
323
5ec10eaa
JA
324 if (!ts->error) {
325 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n",
326 ts->name, ts->groupid, ts->members,
5921e80c 327 ts->error, (int) ts->pid);
5ec10eaa
JA
328 } else {
329 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n",
330 ts->name, ts->groupid, ts->members,
5921e80c 331 ts->error, ts->verror, (int) ts->pid);
5ec10eaa 332 }
3c39a379 333
7bdce1bd 334 if (ts->description)
6d86144d 335 log_info(" Description : [%s]\n", ts->description);
7bdce1bd 336
756867bd
JA
337 if (ts->io_bytes[DDIR_READ])
338 show_ddir_status(rs, ts, DDIR_READ);
339 if (ts->io_bytes[DDIR_WRITE])
340 show_ddir_status(rs, ts, DDIR_WRITE);
3c39a379 341
756867bd 342 runtime = ts->total_run_time;
69008999 343 if (runtime) {
1e97cce9 344 double runt = (double) runtime;
3c39a379 345
756867bd
JA
346 usr_cpu = (double) ts->usr_time * 100 / runt;
347 sys_cpu = (double) ts->sys_time * 100 / runt;
3c39a379
JA
348 } else {
349 usr_cpu = 0;
350 sys_cpu = 0;
351 }
352
5ec10eaa
JA
353 log_info(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu, majf=%lu,"
354 " minf=%lu\n", usr_cpu, sys_cpu, ts->ctx, ts->majf, ts->minf);
71619dc2 355
838bc709 356 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
5ec10eaa
JA
357 log_info(" IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
358 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
359 io_u_dist[1], io_u_dist[2],
360 io_u_dist[3], io_u_dist[4],
361 io_u_dist[5], io_u_dist[6]);
838bc709
JA
362
363 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
364 log_info(" submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
365 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
366 io_u_dist[1], io_u_dist[2],
367 io_u_dist[3], io_u_dist[4],
368 io_u_dist[5], io_u_dist[6]);
369 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
370 log_info(" complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
371 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
372 io_u_dist[1], io_u_dist[2],
373 io_u_dist[3], io_u_dist[4],
374 io_u_dist[5], io_u_dist[6]);
5ec10eaa
JA
375 log_info(" issued r/w: total=%lu/%lu, short=%lu/%lu\n",
376 ts->total_io_u[0], ts->total_io_u[1],
377 ts->short_io_u[0], ts->short_io_u[1]);
838bc709
JA
378 stat_calc_lat_u(ts, io_u_lat_u);
379 stat_calc_lat_m(ts, io_u_lat_m);
04a0feae 380 show_latencies(io_u_lat_u, io_u_lat_m);
3c39a379
JA
381}
382
756867bd 383static void show_ddir_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
384 struct group_run_stats *rs, int ddir)
385{
386 unsigned long min, max;
387 unsigned long long bw;
388 double mean, dev;
389
390 bw = 0;
756867bd
JA
391 if (ts->runtime[ddir])
392 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
c6ae0a5b 393
5ec10eaa
JA
394 log_info(";%llu;%llu;%lu", ts->io_bytes[ddir] >> 10, bw,
395 ts->runtime[ddir]);
c6ae0a5b 396
079ad09b 397 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 398 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 399 else
6d86144d 400 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 401
079ad09b 402 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 403 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 404 else
6d86144d 405 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 406
079ad09b 407 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
c6ae0a5b
JA
408 double p_of_agg;
409
410 p_of_agg = mean * 100 / (double) rs->agg[ddir];
6d86144d 411 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
c6ae0a5b 412 } else
6d86144d 413 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
c6ae0a5b
JA
414}
415
416
756867bd 417static void show_thread_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
418 struct group_run_stats *rs)
419{
2270890c 420 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
421 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
422 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
c6ae0a5b 423 double usr_cpu, sys_cpu;
04a0feae 424 int i;
c6ae0a5b 425
6d86144d 426 log_info("%s;%d;%d", ts->name, ts->groupid, ts->error);
c6ae0a5b 427
756867bd
JA
428 show_ddir_status_terse(ts, rs, 0);
429 show_ddir_status_terse(ts, rs, 1);
c6ae0a5b 430
756867bd
JA
431 if (ts->total_run_time) {
432 double runt = (double) ts->total_run_time;
c6ae0a5b 433
756867bd
JA
434 usr_cpu = (double) ts->usr_time * 100 / runt;
435 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
436 } else {
437 usr_cpu = 0;
438 sys_cpu = 0;
439 }
440
5ec10eaa
JA
441 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
442 ts->minf);
2270890c 443
838bc709 444 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
04a0feae
JA
445 stat_calc_lat_u(ts, io_u_lat_u);
446 stat_calc_lat_m(ts, io_u_lat_m);
2270890c 447
5ec10eaa
JA
448 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
449 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
450 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
2270890c 451
04a0feae
JA
452 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
453 log_info(";%3.2f%%", io_u_lat_u[i]);
454 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
455 log_info(";%3.2f%%", io_u_lat_m[i]);
456 log_info("\n");
2270890c
JA
457
458 if (ts->description)
6d86144d 459 log_info(";%s", ts->description);
2270890c 460
6d86144d 461 log_info("\n");
756867bd
JA
462}
463
197574e4 464static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
756867bd
JA
465{
466 double mean, S;
467
468 dst->min_val = min(dst->min_val, src->min_val);
469 dst->max_val = max(dst->max_val, src->max_val);
470 dst->samples += src->samples;
471
472 /*
473 * Needs a new method for calculating stddev, we cannot just
474 * average them we do below for nr > 1
475 */
476 if (nr == 1) {
477 mean = src->mean;
478 S = src->S;
479 } else {
5ec10eaa
JA
480 mean = ((src->mean * (double) (nr - 1))
481 + dst->mean) / ((double) nr);
c39cced6 482 S = ((src->S * (double) (nr - 1)) + dst->S) / ((double) nr);
756867bd
JA
483 }
484
485 dst->mean = mean;
486 dst->S = S;
487}
488
3c39a379
JA
489void show_run_stats(void)
490{
491 struct group_run_stats *runstats, *rs;
492 struct thread_data *td;
756867bd 493 struct thread_stat *threadstats, *ts;
197574e4 494 int i, j, k, l, nr_ts, last_ts, idx;
3c39a379
JA
495
496 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
497
498 for (i = 0; i < groupid + 1; i++) {
499 rs = &runstats[i];
500
501 memset(rs, 0, sizeof(*rs));
502 rs->min_bw[0] = rs->min_run[0] = ~0UL;
503 rs->min_bw[1] = rs->min_run[1] = ~0UL;
504 }
505
756867bd
JA
506 /*
507 * find out how many threads stats we need. if group reporting isn't
508 * enabled, it's one-per-td.
509 */
510 nr_ts = 0;
511 last_ts = -1;
512 for_each_td(td, i) {
2dc1bbeb 513 if (!td->o.group_reporting) {
756867bd
JA
514 nr_ts++;
515 continue;
516 }
517 if (last_ts == td->groupid)
518 continue;
519
520 last_ts = td->groupid;
521 nr_ts++;
522 }
523
524 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
525
526 for (i = 0; i < nr_ts; i++) {
527 ts = &threadstats[i];
528
529 memset(ts, 0, sizeof(*ts));
de64df05 530 for (j = 0; j <= DDIR_WRITE; j++) {
197574e4
JA
531 ts->clat_stat[j].min_val = -1UL;
532 ts->slat_stat[j].min_val = -1UL;
533 ts->bw_stat[j].min_val = -1UL;
534 }
7abd0e3a 535 ts->groupid = -1;
756867bd
JA
536 }
537
538 j = 0;
539 last_ts = -1;
197574e4 540 idx = 0;
34572e28 541 for_each_td(td, i) {
2dc1bbeb
JA
542 if (idx && (!td->o.group_reporting ||
543 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
544 idx = 0;
545 j++;
546 }
547
548 last_ts = td->groupid;
549
756867bd
JA
550 ts = &threadstats[j];
551
197574e4 552 idx++;
6586ee89 553 ts->members++;
756867bd 554
7abd0e3a 555 if (ts->groupid == -1) {
2dc84ba7
JA
556 /*
557 * These are per-group shared already
558 */
2dc1bbeb
JA
559 ts->name = td->o.name;
560 ts->description = td->o.description;
756867bd 561 ts->groupid = td->groupid;
2dc84ba7
JA
562
563 /*
564 * first pid in group, not very useful...
565 */
756867bd 566 ts->pid = td->pid;
2dc84ba7
JA
567 }
568
569 if (td->error && !ts->error) {
570 ts->error = td->error;
756867bd
JA
571 ts->verror = td->verror;
572 }
573
de64df05 574 for (l = 0; l <= DDIR_WRITE; l++) {
197574e4
JA
575 sum_stat(&ts->clat_stat[l], &td->ts.clat_stat[l], idx);
576 sum_stat(&ts->slat_stat[l], &td->ts.slat_stat[l], idx);
577 sum_stat(&ts->bw_stat[l], &td->ts.bw_stat[l], idx);
756867bd 578
197574e4
JA
579 ts->stat_io_bytes[l] += td->ts.stat_io_bytes[l];
580 ts->io_bytes[l] += td->ts.io_bytes[l];
581
582 if (ts->runtime[l] < td->ts.runtime[l])
583 ts->runtime[l] = td->ts.runtime[l];
584 }
756867bd
JA
585
586 ts->usr_time += td->ts.usr_time;
587 ts->sys_time += td->ts.sys_time;
588 ts->ctx += td->ts.ctx;
e7823a94
JA
589 ts->majf += td->ts.majf;
590 ts->minf += td->ts.minf;
756867bd
JA
591
592 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
593 ts->io_u_map[k] += td->ts.io_u_map[k];
838bc709
JA
594 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
595 ts->io_u_submit[k] += td->ts.io_u_submit[k];
596 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
597 ts->io_u_complete[k] += td->ts.io_u_complete[k];
04a0feae
JA
598 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
599 ts->io_u_lat_u[k] += td->ts.io_u_lat_u[k];
600 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
601 ts->io_u_lat_m[k] += td->ts.io_u_lat_m[k];
602
756867bd 603
30061b97 604 for (k = 0; k <= DDIR_WRITE; k++) {
b3605062 605 ts->total_io_u[k] += td->ts.total_io_u[k];
30061b97
JA
606 ts->short_io_u[k] += td->ts.short_io_u[k];
607 }
756867bd
JA
608
609 ts->total_run_time += td->ts.total_run_time;
838bc709
JA
610 ts->total_submit += td->ts.total_submit;
611 ts->total_complete += td->ts.total_complete;
756867bd
JA
612 }
613
614 for (i = 0; i < nr_ts; i++) {
94370ac4 615 unsigned long long bw;
3c39a379 616
756867bd
JA
617 ts = &threadstats[i];
618 rs = &runstats[ts->groupid];
3c39a379 619
de64df05 620 for (j = 0; j <= DDIR_WRITE; j++) {
94370ac4
JA
621 if (!ts->runtime[j])
622 continue;
623 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
624 rs->min_run[j] = ts->runtime[j];
625 if (ts->runtime[j] > rs->max_run[j])
626 rs->max_run[j] = ts->runtime[j];
627
628 bw = 0;
8879fd15
JA
629 if (ts->runtime[j]) {
630 unsigned long runt;
631
632 runt = ts->runtime[j] * 1024 / 1000;
633 bw = ts->io_bytes[j] / runt;
634 }
94370ac4
JA
635 if (bw < rs->min_bw[j])
636 rs->min_bw[j] = bw;
637 if (bw > rs->max_bw[j])
638 rs->max_bw[j] = bw;
639
640 rs->io_kb[j] += ts->io_bytes[j] >> 10;
641 }
3c39a379
JA
642 }
643
644 for (i = 0; i < groupid + 1; i++) {
8879fd15
JA
645 unsigned long max_run[2];
646
3c39a379 647 rs = &runstats[i];
8879fd15
JA
648 max_run[0] = rs->max_run[0] * 1024 / 1000;
649 max_run[1] = rs->max_run[1] * 1024 / 1000;
3c39a379
JA
650
651 if (rs->max_run[0])
8879fd15 652 rs->agg[0] = (rs->io_kb[0]*1024) / max_run[0];
3c39a379 653 if (rs->max_run[1])
8879fd15 654 rs->agg[1] = (rs->io_kb[1]*1024) / max_run[1];
3c39a379
JA
655 }
656
657 /*
658 * don't overwrite last signal output
659 */
c6ae0a5b
JA
660 if (!terse_output)
661 printf("\n");
3c39a379 662
756867bd
JA
663 for (i = 0; i < nr_ts; i++) {
664 ts = &threadstats[i];
665 rs = &runstats[ts->groupid];
3c39a379 666
c6ae0a5b 667 if (terse_output)
756867bd 668 show_thread_status_terse(ts, rs);
c6ae0a5b 669 else
756867bd 670 show_thread_status(ts, rs);
3c39a379
JA
671 }
672
c6ae0a5b
JA
673 if (!terse_output) {
674 for (i = 0; i < groupid + 1; i++)
675 show_group_stats(&runstats[i], i);
3c39a379 676
c6ae0a5b
JA
677 show_disk_util();
678 }
eecf272f
JA
679
680 free(runstats);
756867bd 681 free(threadstats);
3c39a379
JA
682}
683
68704084 684static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 685{
68704084 686 double val = data;
6660cc67 687 double delta;
68704084
JA
688
689 if (data > is->max_val)
690 is->max_val = data;
691 if (data < is->min_val)
692 is->min_val = data;
693
694 delta = val - is->mean;
ef11d737
JA
695 if (delta) {
696 is->mean += delta / (is->samples + 1.0);
697 is->S += delta * (val - is->mean);
698 }
3c39a379 699
3c39a379
JA
700 is->samples++;
701}
702
bb3884d8 703static void __add_log_sample(struct io_log *iolog, unsigned long val,
306ddc97
JA
704 enum fio_ddir ddir, unsigned int bs,
705 unsigned long time)
3c39a379 706{
306ddc97
JA
707 const int nr_samples = iolog->nr_samples;
708
3c39a379
JA
709 if (iolog->nr_samples == iolog->max_samples) {
710 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
711
712 iolog->log = realloc(iolog->log, new_size);
713 iolog->max_samples <<= 1;
714 }
715
306ddc97
JA
716 iolog->log[nr_samples].val = val;
717 iolog->log[nr_samples].time = time;
718 iolog->log[nr_samples].ddir = ddir;
719 iolog->log[nr_samples].bs = bs;
3c39a379
JA
720 iolog->nr_samples++;
721}
722
bb3884d8 723static void add_log_sample(struct thread_data *td, struct io_log *iolog,
306ddc97
JA
724 unsigned long val, enum fio_ddir ddir,
725 unsigned int bs)
bb3884d8 726{
306ddc97 727 __add_log_sample(iolog, val, ddir, bs, mtime_since_now(&td->epoch));
bb3884d8
JA
728}
729
306ddc97 730void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
bb3884d8
JA
731{
732 struct io_log *iolog = agg_io_log[ddir];
733
306ddc97 734 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
bb3884d8
JA
735}
736
1e97cce9 737void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 738 unsigned long usec, unsigned int bs)
3c39a379 739{
756867bd 740 struct thread_stat *ts = &td->ts;
079ad09b 741
d85f5118 742 add_stat_sample(&ts->clat_stat[ddir], usec);
3c39a379 743
079ad09b 744 if (ts->clat_log)
306ddc97 745 add_log_sample(td, ts->clat_log, usec, ddir, bs);
3c39a379
JA
746}
747
1e97cce9 748void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 749 unsigned long usec, unsigned int bs)
3c39a379 750{
756867bd 751 struct thread_stat *ts = &td->ts;
079ad09b 752
d85f5118 753 add_stat_sample(&ts->slat_stat[ddir], usec);
3c39a379 754
079ad09b 755 if (ts->slat_log)
306ddc97 756 add_log_sample(td, ts->slat_log, usec, ddir, bs);
3c39a379
JA
757}
758
306ddc97 759void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1e97cce9 760 struct timeval *t)
3c39a379 761{
756867bd 762 struct thread_stat *ts = &td->ts;
079ad09b 763 unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t);
3c39a379
JA
764 unsigned long rate;
765
2dc1bbeb 766 if (spent < td->o.bw_avg_time)
3c39a379
JA
767 return;
768
8879fd15 769 rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) * 1000 / spent / 1024;
079ad09b 770 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 771
079ad09b 772 if (ts->bw_log)
306ddc97 773 add_log_sample(td, ts->bw_log, rate, ddir, bs);
3c39a379 774
079ad09b
JA
775 fio_gettime(&ts->stat_sample_time[ddir], NULL);
776 ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
3c39a379 777}