Fio 1.43.1
[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
b22989b9 175 log_info(" %s: io=%sB, bw=%sB/s, iops=%s, runt=%6lumsec\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]);
5ec10eaa
JA
354 log_info(" issued r/w: total=%lu/%lu, short=%lu/%lu\n",
355 ts->total_io_u[0], ts->total_io_u[1],
356 ts->short_io_u[0], ts->short_io_u[1]);
838bc709
JA
357 stat_calc_lat_u(ts, io_u_lat_u);
358 stat_calc_lat_m(ts, io_u_lat_m);
04a0feae 359 show_latencies(io_u_lat_u, io_u_lat_m);
f2bba182 360 if (ts->continue_on_error) {
1ec99eea
JA
361 log_info(" errors : total=%lu, first_error=%d/<%s>\n",
362 ts->total_err_count,
363 ts->first_error,
364 strerror(ts->first_error));
f2bba182 365 }
3c39a379
JA
366}
367
756867bd 368static void show_ddir_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
369 struct group_run_stats *rs, int ddir)
370{
371 unsigned long min, max;
372 unsigned long long bw;
373 double mean, dev;
374
ff58fced
JA
375 assert(ddir_rw(ddir));
376
c6ae0a5b 377 bw = 0;
756867bd
JA
378 if (ts->runtime[ddir])
379 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
c6ae0a5b 380
5ec10eaa
JA
381 log_info(";%llu;%llu;%lu", ts->io_bytes[ddir] >> 10, bw,
382 ts->runtime[ddir]);
c6ae0a5b 383
079ad09b 384 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 385 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 386 else
6d86144d 387 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 388
079ad09b 389 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 390 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 391 else
6d86144d 392 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 393
02af0988
JA
394 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
395 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
396 else
397 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
398
079ad09b 399 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
c6ae0a5b
JA
400 double p_of_agg;
401
402 p_of_agg = mean * 100 / (double) rs->agg[ddir];
6d86144d 403 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
c6ae0a5b 404 } else
6d86144d 405 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
c6ae0a5b
JA
406}
407
525c2bfa 408#define FIO_TERSE_VERSION "2"
c6ae0a5b 409
756867bd 410static void show_thread_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
411 struct group_run_stats *rs)
412{
2270890c 413 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
414 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
415 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
c6ae0a5b 416 double usr_cpu, sys_cpu;
04a0feae 417 int i;
c6ae0a5b 418
525c2bfa
JA
419 log_info("%s;%s;%d;%d", FIO_TERSE_VERSION, ts->name, ts->groupid,
420 ts->error);
c6ae0a5b 421
756867bd
JA
422 show_ddir_status_terse(ts, rs, 0);
423 show_ddir_status_terse(ts, rs, 1);
c6ae0a5b 424
756867bd
JA
425 if (ts->total_run_time) {
426 double runt = (double) ts->total_run_time;
c6ae0a5b 427
756867bd
JA
428 usr_cpu = (double) ts->usr_time * 100 / runt;
429 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
430 } else {
431 usr_cpu = 0;
432 sys_cpu = 0;
433 }
434
5ec10eaa
JA
435 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
436 ts->minf);
2270890c 437
838bc709 438 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
04a0feae
JA
439 stat_calc_lat_u(ts, io_u_lat_u);
440 stat_calc_lat_m(ts, io_u_lat_m);
2270890c 441
5ec10eaa
JA
442 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
443 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
444 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
2270890c 445
04a0feae
JA
446 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
447 log_info(";%3.2f%%", io_u_lat_u[i]);
448 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
449 log_info(";%3.2f%%", io_u_lat_m[i]);
f2bba182
RR
450 if (ts->continue_on_error)
451 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
04a0feae 452 log_info("\n");
2270890c
JA
453
454 if (ts->description)
6d86144d 455 log_info(";%s", ts->description);
2270890c 456
6d86144d 457 log_info("\n");
756867bd
JA
458}
459
197574e4 460static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
756867bd
JA
461{
462 double mean, S;
463
464 dst->min_val = min(dst->min_val, src->min_val);
465 dst->max_val = max(dst->max_val, src->max_val);
466 dst->samples += src->samples;
467
468 /*
469 * Needs a new method for calculating stddev, we cannot just
470 * average them we do below for nr > 1
471 */
472 if (nr == 1) {
473 mean = src->mean;
474 S = src->S;
475 } else {
5ec10eaa
JA
476 mean = ((src->mean * (double) (nr - 1))
477 + dst->mean) / ((double) nr);
c39cced6 478 S = ((src->S * (double) (nr - 1)) + dst->S) / ((double) nr);
756867bd
JA
479 }
480
481 dst->mean = mean;
482 dst->S = S;
483}
484
3c39a379
JA
485void show_run_stats(void)
486{
487 struct group_run_stats *runstats, *rs;
488 struct thread_data *td;
756867bd 489 struct thread_stat *threadstats, *ts;
197574e4 490 int i, j, k, l, nr_ts, last_ts, idx;
90fef2d1 491 int kb_base_warned = 0;
3c39a379
JA
492
493 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
494
495 for (i = 0; i < groupid + 1; i++) {
496 rs = &runstats[i];
497
498 memset(rs, 0, sizeof(*rs));
499 rs->min_bw[0] = rs->min_run[0] = ~0UL;
500 rs->min_bw[1] = rs->min_run[1] = ~0UL;
501 }
502
756867bd
JA
503 /*
504 * find out how many threads stats we need. if group reporting isn't
505 * enabled, it's one-per-td.
506 */
507 nr_ts = 0;
508 last_ts = -1;
509 for_each_td(td, i) {
2dc1bbeb 510 if (!td->o.group_reporting) {
756867bd
JA
511 nr_ts++;
512 continue;
513 }
514 if (last_ts == td->groupid)
515 continue;
516
517 last_ts = td->groupid;
518 nr_ts++;
519 }
520
521 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
522
523 for (i = 0; i < nr_ts; i++) {
524 ts = &threadstats[i];
525
526 memset(ts, 0, sizeof(*ts));
de64df05 527 for (j = 0; j <= DDIR_WRITE; j++) {
02af0988 528 ts->lat_stat[j].min_val = -1UL;
197574e4
JA
529 ts->clat_stat[j].min_val = -1UL;
530 ts->slat_stat[j].min_val = -1UL;
531 ts->bw_stat[j].min_val = -1UL;
532 }
7abd0e3a 533 ts->groupid = -1;
756867bd
JA
534 }
535
536 j = 0;
537 last_ts = -1;
197574e4 538 idx = 0;
34572e28 539 for_each_td(td, i) {
2dc1bbeb
JA
540 if (idx && (!td->o.group_reporting ||
541 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
542 idx = 0;
543 j++;
544 }
545
546 last_ts = td->groupid;
547
756867bd
JA
548 ts = &threadstats[j];
549
197574e4 550 idx++;
6586ee89 551 ts->members++;
756867bd 552
7abd0e3a 553 if (ts->groupid == -1) {
2dc84ba7
JA
554 /*
555 * These are per-group shared already
556 */
2dc1bbeb
JA
557 ts->name = td->o.name;
558 ts->description = td->o.description;
756867bd 559 ts->groupid = td->groupid;
2dc84ba7
JA
560
561 /*
562 * first pid in group, not very useful...
563 */
756867bd 564 ts->pid = td->pid;
90fef2d1
JA
565
566 ts->kb_base = td->o.kb_base;
567 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
568 log_info("fio: kb_base differs for jobs in group, using"
569 " %u as the base\n", ts->kb_base);
570 kb_base_warned = 1;
2dc84ba7
JA
571 }
572
f2bba182
RR
573 ts->continue_on_error = td->o.continue_on_error;
574 ts->total_err_count += td->total_err_count;
575 ts->first_error = td->first_error;
576 if (!ts->error) {
577 if (!td->error && td->o.continue_on_error &&
578 td->first_error) {
579 ts->error = td->first_error;
580 ts->verror = td->verror;
581 } else if (td->error) {
582 ts->error = td->error;
583 ts->verror = td->verror;
584 }
756867bd
JA
585 }
586
de64df05 587 for (l = 0; l <= DDIR_WRITE; l++) {
197574e4
JA
588 sum_stat(&ts->clat_stat[l], &td->ts.clat_stat[l], idx);
589 sum_stat(&ts->slat_stat[l], &td->ts.slat_stat[l], idx);
02af0988 590 sum_stat(&ts->lat_stat[l], &td->ts.lat_stat[l], idx);
197574e4 591 sum_stat(&ts->bw_stat[l], &td->ts.bw_stat[l], idx);
756867bd 592
197574e4
JA
593 ts->stat_io_bytes[l] += td->ts.stat_io_bytes[l];
594 ts->io_bytes[l] += td->ts.io_bytes[l];
595
596 if (ts->runtime[l] < td->ts.runtime[l])
597 ts->runtime[l] = td->ts.runtime[l];
598 }
756867bd
JA
599
600 ts->usr_time += td->ts.usr_time;
601 ts->sys_time += td->ts.sys_time;
602 ts->ctx += td->ts.ctx;
e7823a94
JA
603 ts->majf += td->ts.majf;
604 ts->minf += td->ts.minf;
756867bd
JA
605
606 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
607 ts->io_u_map[k] += td->ts.io_u_map[k];
838bc709
JA
608 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
609 ts->io_u_submit[k] += td->ts.io_u_submit[k];
610 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
611 ts->io_u_complete[k] += td->ts.io_u_complete[k];
04a0feae
JA
612 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
613 ts->io_u_lat_u[k] += td->ts.io_u_lat_u[k];
614 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
615 ts->io_u_lat_m[k] += td->ts.io_u_lat_m[k];
616
756867bd 617
30061b97 618 for (k = 0; k <= DDIR_WRITE; k++) {
b3605062 619 ts->total_io_u[k] += td->ts.total_io_u[k];
30061b97
JA
620 ts->short_io_u[k] += td->ts.short_io_u[k];
621 }
756867bd
JA
622
623 ts->total_run_time += td->ts.total_run_time;
838bc709
JA
624 ts->total_submit += td->ts.total_submit;
625 ts->total_complete += td->ts.total_complete;
756867bd
JA
626 }
627
628 for (i = 0; i < nr_ts; i++) {
94370ac4 629 unsigned long long bw;
3c39a379 630
756867bd
JA
631 ts = &threadstats[i];
632 rs = &runstats[ts->groupid];
90fef2d1 633 rs->kb_base = ts->kb_base;
3c39a379 634
de64df05 635 for (j = 0; j <= DDIR_WRITE; j++) {
94370ac4
JA
636 if (!ts->runtime[j])
637 continue;
638 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
639 rs->min_run[j] = ts->runtime[j];
640 if (ts->runtime[j] > rs->max_run[j])
641 rs->max_run[j] = ts->runtime[j];
642
643 bw = 0;
8879fd15
JA
644 if (ts->runtime[j]) {
645 unsigned long runt;
646
90fef2d1 647 runt = ts->runtime[j];
8879fd15
JA
648 bw = ts->io_bytes[j] / runt;
649 }
94370ac4
JA
650 if (bw < rs->min_bw[j])
651 rs->min_bw[j] = bw;
652 if (bw > rs->max_bw[j])
653 rs->max_bw[j] = bw;
654
90fef2d1 655 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
94370ac4 656 }
3c39a379
JA
657 }
658
659 for (i = 0; i < groupid + 1; i++) {
8879fd15
JA
660 unsigned long max_run[2];
661
3c39a379 662 rs = &runstats[i];
90fef2d1
JA
663 max_run[0] = rs->max_run[0];
664 max_run[1] = rs->max_run[1];
3c39a379
JA
665
666 if (rs->max_run[0])
4cf1abc3 667 rs->agg[0] = (rs->io_kb[0] * 1000) / max_run[0];
3c39a379 668 if (rs->max_run[1])
4cf1abc3 669 rs->agg[1] = (rs->io_kb[1] * 1000) / max_run[1];
3c39a379
JA
670 }
671
672 /*
673 * don't overwrite last signal output
674 */
c6ae0a5b 675 if (!terse_output)
4ceb30d4 676 log_info("\n");
3c39a379 677
756867bd
JA
678 for (i = 0; i < nr_ts; i++) {
679 ts = &threadstats[i];
680 rs = &runstats[ts->groupid];
3c39a379 681
c6ae0a5b 682 if (terse_output)
756867bd 683 show_thread_status_terse(ts, rs);
c6ae0a5b 684 else
756867bd 685 show_thread_status(ts, rs);
3c39a379
JA
686 }
687
c6ae0a5b
JA
688 if (!terse_output) {
689 for (i = 0; i < groupid + 1; i++)
690 show_group_stats(&runstats[i], i);
3c39a379 691
c6ae0a5b
JA
692 show_disk_util();
693 }
eecf272f
JA
694
695 free(runstats);
756867bd 696 free(threadstats);
3c39a379
JA
697}
698
68704084 699static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 700{
68704084 701 double val = data;
6660cc67 702 double delta;
68704084
JA
703
704 if (data > is->max_val)
705 is->max_val = data;
706 if (data < is->min_val)
707 is->min_val = data;
708
709 delta = val - is->mean;
ef11d737
JA
710 if (delta) {
711 is->mean += delta / (is->samples + 1.0);
712 is->S += delta * (val - is->mean);
713 }
3c39a379 714
3c39a379
JA
715 is->samples++;
716}
717
bb3884d8 718static void __add_log_sample(struct io_log *iolog, unsigned long val,
306ddc97
JA
719 enum fio_ddir ddir, unsigned int bs,
720 unsigned long time)
3c39a379 721{
306ddc97
JA
722 const int nr_samples = iolog->nr_samples;
723
3c39a379
JA
724 if (iolog->nr_samples == iolog->max_samples) {
725 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
726
727 iolog->log = realloc(iolog->log, new_size);
728 iolog->max_samples <<= 1;
729 }
730
306ddc97
JA
731 iolog->log[nr_samples].val = val;
732 iolog->log[nr_samples].time = time;
733 iolog->log[nr_samples].ddir = ddir;
734 iolog->log[nr_samples].bs = bs;
3c39a379
JA
735 iolog->nr_samples++;
736}
737
bb3884d8 738static void add_log_sample(struct thread_data *td, struct io_log *iolog,
306ddc97
JA
739 unsigned long val, enum fio_ddir ddir,
740 unsigned int bs)
bb3884d8 741{
ff58fced
JA
742 if (!ddir_rw(ddir))
743 return;
744
306ddc97 745 __add_log_sample(iolog, val, ddir, bs, mtime_since_now(&td->epoch));
bb3884d8
JA
746}
747
306ddc97 748void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
bb3884d8 749{
ff58fced 750 struct io_log *iolog;
bb3884d8 751
ff58fced
JA
752 if (!ddir_rw(ddir))
753 return;
754
755 iolog = agg_io_log[ddir];
306ddc97 756 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
bb3884d8
JA
757}
758
1e97cce9 759void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 760 unsigned long usec, unsigned int bs)
3c39a379 761{
756867bd 762 struct thread_stat *ts = &td->ts;
079ad09b 763
ff58fced
JA
764 if (!ddir_rw(ddir))
765 return;
766
d85f5118 767 add_stat_sample(&ts->clat_stat[ddir], usec);
3c39a379 768
079ad09b 769 if (ts->clat_log)
306ddc97 770 add_log_sample(td, ts->clat_log, usec, ddir, bs);
3c39a379
JA
771}
772
1e97cce9 773void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 774 unsigned long usec, unsigned int bs)
3c39a379 775{
756867bd 776 struct thread_stat *ts = &td->ts;
079ad09b 777
ff58fced
JA
778 if (!ddir_rw(ddir))
779 return;
780
d85f5118 781 add_stat_sample(&ts->slat_stat[ddir], usec);
3c39a379 782
079ad09b 783 if (ts->slat_log)
306ddc97 784 add_log_sample(td, ts->slat_log, usec, ddir, bs);
3c39a379
JA
785}
786
02af0988
JA
787void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
788 unsigned long usec, unsigned int bs)
789{
790 struct thread_stat *ts = &td->ts;
791
ff58fced
JA
792 if (!ddir_rw(ddir))
793 return;
794
02af0988
JA
795 add_stat_sample(&ts->lat_stat[ddir], usec);
796
797 if (ts->lat_log)
798 add_log_sample(td, ts->lat_log, usec, ddir, bs);
799}
800
306ddc97 801void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1e97cce9 802 struct timeval *t)
3c39a379 803{
756867bd 804 struct thread_stat *ts = &td->ts;
ff58fced
JA
805 unsigned long spent, rate;
806
807 if (!ddir_rw(ddir))
808 return;
3c39a379 809
ff58fced 810 spent = mtime_since(&ts->stat_sample_time[ddir], t);
2dc1bbeb 811 if (spent < td->o.bw_avg_time)
3c39a379
JA
812 return;
813
0b9d69ec
JA
814 rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) *
815 1000 / spent / 1024;
079ad09b 816 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 817
079ad09b 818 if (ts->bw_log)
306ddc97 819 add_log_sample(td, ts->bw_log, rate, ddir, bs);
3c39a379 820
079ad09b
JA
821 fio_gettime(&ts->stat_sample_time[ddir], NULL);
822 ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
3c39a379 823}