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