stats: Fix computation of summed standard deviation
[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 171
0aacc50c 172 iops = (1000 * (uint64_t)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);
756867bd
JA
477
478 /*
cdcac5cf
YH
479 * Compute new mean and S after the merge
480 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
481 * #Parallel_algorithm>
756867bd
JA
482 */
483 if (nr == 1) {
484 mean = src->mean;
485 S = src->S;
486 } else {
cdcac5cf
YH
487 double delta = src->mean - dst->mean;
488
489 mean = ((src->mean * src->samples) +
490 (dst->mean * dst->samples)) /
491 (dst->samples + src->samples);
492
493 S = src->S + dst->S + pow(delta, 2.0) *
494 (dst->samples * src->samples) /
495 (dst->samples + src->samples);
756867bd
JA
496 }
497
cdcac5cf 498 dst->samples += src->samples;
756867bd
JA
499 dst->mean = mean;
500 dst->S = S;
501}
502
3c39a379
JA
503void show_run_stats(void)
504{
505 struct group_run_stats *runstats, *rs;
506 struct thread_data *td;
756867bd 507 struct thread_stat *threadstats, *ts;
197574e4 508 int i, j, k, l, nr_ts, last_ts, idx;
90fef2d1 509 int kb_base_warned = 0;
3c39a379
JA
510
511 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
512
513 for (i = 0; i < groupid + 1; i++) {
514 rs = &runstats[i];
515
516 memset(rs, 0, sizeof(*rs));
517 rs->min_bw[0] = rs->min_run[0] = ~0UL;
518 rs->min_bw[1] = rs->min_run[1] = ~0UL;
519 }
520
756867bd
JA
521 /*
522 * find out how many threads stats we need. if group reporting isn't
523 * enabled, it's one-per-td.
524 */
525 nr_ts = 0;
526 last_ts = -1;
527 for_each_td(td, i) {
2dc1bbeb 528 if (!td->o.group_reporting) {
756867bd
JA
529 nr_ts++;
530 continue;
531 }
532 if (last_ts == td->groupid)
533 continue;
534
535 last_ts = td->groupid;
536 nr_ts++;
537 }
538
539 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
540
541 for (i = 0; i < nr_ts; i++) {
542 ts = &threadstats[i];
543
544 memset(ts, 0, sizeof(*ts));
de64df05 545 for (j = 0; j <= DDIR_WRITE; j++) {
02af0988 546 ts->lat_stat[j].min_val = -1UL;
197574e4
JA
547 ts->clat_stat[j].min_val = -1UL;
548 ts->slat_stat[j].min_val = -1UL;
549 ts->bw_stat[j].min_val = -1UL;
550 }
7abd0e3a 551 ts->groupid = -1;
756867bd
JA
552 }
553
554 j = 0;
555 last_ts = -1;
197574e4 556 idx = 0;
34572e28 557 for_each_td(td, i) {
2dc1bbeb
JA
558 if (idx && (!td->o.group_reporting ||
559 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
560 idx = 0;
561 j++;
562 }
563
564 last_ts = td->groupid;
565
756867bd
JA
566 ts = &threadstats[j];
567
197574e4 568 idx++;
6586ee89 569 ts->members++;
756867bd 570
7abd0e3a 571 if (ts->groupid == -1) {
2dc84ba7
JA
572 /*
573 * These are per-group shared already
574 */
2dc1bbeb
JA
575 ts->name = td->o.name;
576 ts->description = td->o.description;
756867bd 577 ts->groupid = td->groupid;
2dc84ba7
JA
578
579 /*
580 * first pid in group, not very useful...
581 */
756867bd 582 ts->pid = td->pid;
90fef2d1
JA
583
584 ts->kb_base = td->o.kb_base;
585 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
586 log_info("fio: kb_base differs for jobs in group, using"
587 " %u as the base\n", ts->kb_base);
588 kb_base_warned = 1;
2dc84ba7
JA
589 }
590
f2bba182
RR
591 ts->continue_on_error = td->o.continue_on_error;
592 ts->total_err_count += td->total_err_count;
593 ts->first_error = td->first_error;
594 if (!ts->error) {
595 if (!td->error && td->o.continue_on_error &&
596 td->first_error) {
597 ts->error = td->first_error;
598 ts->verror = td->verror;
599 } else if (td->error) {
600 ts->error = td->error;
601 ts->verror = td->verror;
602 }
756867bd
JA
603 }
604
de64df05 605 for (l = 0; l <= DDIR_WRITE; l++) {
197574e4
JA
606 sum_stat(&ts->clat_stat[l], &td->ts.clat_stat[l], idx);
607 sum_stat(&ts->slat_stat[l], &td->ts.slat_stat[l], idx);
02af0988 608 sum_stat(&ts->lat_stat[l], &td->ts.lat_stat[l], idx);
197574e4 609 sum_stat(&ts->bw_stat[l], &td->ts.bw_stat[l], idx);
756867bd 610
197574e4
JA
611 ts->stat_io_bytes[l] += td->ts.stat_io_bytes[l];
612 ts->io_bytes[l] += td->ts.io_bytes[l];
613
614 if (ts->runtime[l] < td->ts.runtime[l])
615 ts->runtime[l] = td->ts.runtime[l];
616 }
756867bd
JA
617
618 ts->usr_time += td->ts.usr_time;
619 ts->sys_time += td->ts.sys_time;
620 ts->ctx += td->ts.ctx;
e7823a94
JA
621 ts->majf += td->ts.majf;
622 ts->minf += td->ts.minf;
756867bd
JA
623
624 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
625 ts->io_u_map[k] += td->ts.io_u_map[k];
838bc709
JA
626 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
627 ts->io_u_submit[k] += td->ts.io_u_submit[k];
628 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
629 ts->io_u_complete[k] += td->ts.io_u_complete[k];
04a0feae
JA
630 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
631 ts->io_u_lat_u[k] += td->ts.io_u_lat_u[k];
632 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
633 ts->io_u_lat_m[k] += td->ts.io_u_lat_m[k];
634
756867bd 635
0d29de83 636 for (k = 0; k <= 2; k++) {
b3605062 637 ts->total_io_u[k] += td->ts.total_io_u[k];
30061b97
JA
638 ts->short_io_u[k] += td->ts.short_io_u[k];
639 }
756867bd
JA
640
641 ts->total_run_time += td->ts.total_run_time;
838bc709
JA
642 ts->total_submit += td->ts.total_submit;
643 ts->total_complete += td->ts.total_complete;
756867bd
JA
644 }
645
646 for (i = 0; i < nr_ts; i++) {
94370ac4 647 unsigned long long bw;
3c39a379 648
756867bd
JA
649 ts = &threadstats[i];
650 rs = &runstats[ts->groupid];
90fef2d1 651 rs->kb_base = ts->kb_base;
3c39a379 652
de64df05 653 for (j = 0; j <= DDIR_WRITE; j++) {
94370ac4
JA
654 if (!ts->runtime[j])
655 continue;
656 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
657 rs->min_run[j] = ts->runtime[j];
658 if (ts->runtime[j] > rs->max_run[j])
659 rs->max_run[j] = ts->runtime[j];
660
661 bw = 0;
8879fd15
JA
662 if (ts->runtime[j]) {
663 unsigned long runt;
664
90fef2d1 665 runt = ts->runtime[j];
8879fd15
JA
666 bw = ts->io_bytes[j] / runt;
667 }
94370ac4
JA
668 if (bw < rs->min_bw[j])
669 rs->min_bw[j] = bw;
670 if (bw > rs->max_bw[j])
671 rs->max_bw[j] = bw;
672
90fef2d1 673 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
94370ac4 674 }
3c39a379
JA
675 }
676
677 for (i = 0; i < groupid + 1; i++) {
8879fd15
JA
678 unsigned long max_run[2];
679
3c39a379 680 rs = &runstats[i];
90fef2d1
JA
681 max_run[0] = rs->max_run[0];
682 max_run[1] = rs->max_run[1];
3c39a379
JA
683
684 if (rs->max_run[0])
4cf1abc3 685 rs->agg[0] = (rs->io_kb[0] * 1000) / max_run[0];
3c39a379 686 if (rs->max_run[1])
4cf1abc3 687 rs->agg[1] = (rs->io_kb[1] * 1000) / max_run[1];
3c39a379
JA
688 }
689
690 /*
691 * don't overwrite last signal output
692 */
c6ae0a5b 693 if (!terse_output)
4ceb30d4 694 log_info("\n");
3c39a379 695
756867bd
JA
696 for (i = 0; i < nr_ts; i++) {
697 ts = &threadstats[i];
698 rs = &runstats[ts->groupid];
3c39a379 699
c6ae0a5b 700 if (terse_output)
756867bd 701 show_thread_status_terse(ts, rs);
c6ae0a5b 702 else
756867bd 703 show_thread_status(ts, rs);
3c39a379
JA
704 }
705
c6ae0a5b
JA
706 if (!terse_output) {
707 for (i = 0; i < groupid + 1; i++)
708 show_group_stats(&runstats[i], i);
3c39a379 709
c6ae0a5b
JA
710 show_disk_util();
711 }
eecf272f
JA
712
713 free(runstats);
756867bd 714 free(threadstats);
3c39a379
JA
715}
716
68704084 717static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 718{
68704084 719 double val = data;
6660cc67 720 double delta;
68704084
JA
721
722 if (data > is->max_val)
723 is->max_val = data;
724 if (data < is->min_val)
725 is->min_val = data;
726
727 delta = val - is->mean;
ef11d737
JA
728 if (delta) {
729 is->mean += delta / (is->samples + 1.0);
730 is->S += delta * (val - is->mean);
731 }
3c39a379 732
3c39a379
JA
733 is->samples++;
734}
735
bb3884d8 736static void __add_log_sample(struct io_log *iolog, unsigned long val,
306ddc97 737 enum fio_ddir ddir, unsigned int bs,
2b13e716 738 unsigned long t)
3c39a379 739{
306ddc97
JA
740 const int nr_samples = iolog->nr_samples;
741
3c39a379
JA
742 if (iolog->nr_samples == iolog->max_samples) {
743 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
744
745 iolog->log = realloc(iolog->log, new_size);
746 iolog->max_samples <<= 1;
747 }
748
306ddc97 749 iolog->log[nr_samples].val = val;
2b13e716 750 iolog->log[nr_samples].time = t;
306ddc97
JA
751 iolog->log[nr_samples].ddir = ddir;
752 iolog->log[nr_samples].bs = bs;
3c39a379
JA
753 iolog->nr_samples++;
754}
755
bb3884d8 756static void add_log_sample(struct thread_data *td, struct io_log *iolog,
306ddc97
JA
757 unsigned long val, enum fio_ddir ddir,
758 unsigned int bs)
bb3884d8 759{
ff58fced
JA
760 if (!ddir_rw(ddir))
761 return;
762
306ddc97 763 __add_log_sample(iolog, val, ddir, bs, mtime_since_now(&td->epoch));
bb3884d8
JA
764}
765
306ddc97 766void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
bb3884d8 767{
ff58fced 768 struct io_log *iolog;
bb3884d8 769
ff58fced
JA
770 if (!ddir_rw(ddir))
771 return;
772
773 iolog = agg_io_log[ddir];
306ddc97 774 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
bb3884d8
JA
775}
776
1e97cce9 777void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 778 unsigned long usec, unsigned int bs)
3c39a379 779{
756867bd 780 struct thread_stat *ts = &td->ts;
079ad09b 781
ff58fced
JA
782 if (!ddir_rw(ddir))
783 return;
784
d85f5118 785 add_stat_sample(&ts->clat_stat[ddir], usec);
3c39a379 786
079ad09b 787 if (ts->clat_log)
306ddc97 788 add_log_sample(td, ts->clat_log, usec, ddir, bs);
3c39a379
JA
789}
790
1e97cce9 791void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 792 unsigned long usec, unsigned int bs)
3c39a379 793{
756867bd 794 struct thread_stat *ts = &td->ts;
079ad09b 795
ff58fced
JA
796 if (!ddir_rw(ddir))
797 return;
798
d85f5118 799 add_stat_sample(&ts->slat_stat[ddir], usec);
3c39a379 800
079ad09b 801 if (ts->slat_log)
306ddc97 802 add_log_sample(td, ts->slat_log, usec, ddir, bs);
3c39a379
JA
803}
804
02af0988
JA
805void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
806 unsigned long usec, unsigned int bs)
807{
808 struct thread_stat *ts = &td->ts;
809
ff58fced
JA
810 if (!ddir_rw(ddir))
811 return;
812
02af0988
JA
813 add_stat_sample(&ts->lat_stat[ddir], usec);
814
815 if (ts->lat_log)
816 add_log_sample(td, ts->lat_log, usec, ddir, bs);
817}
818
306ddc97 819void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1e97cce9 820 struct timeval *t)
3c39a379 821{
756867bd 822 struct thread_stat *ts = &td->ts;
ff58fced
JA
823 unsigned long spent, rate;
824
825 if (!ddir_rw(ddir))
826 return;
3c39a379 827
ff58fced 828 spent = mtime_since(&ts->stat_sample_time[ddir], t);
2dc1bbeb 829 if (spent < td->o.bw_avg_time)
3c39a379
JA
830 return;
831
0b9d69ec
JA
832 rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) *
833 1000 / spent / 1024;
079ad09b 834 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 835
079ad09b 836 if (ts->bw_log)
306ddc97 837 add_log_sample(td, ts->bw_log, rate, ddir, bs);
3c39a379 838
079ad09b
JA
839 fio_gettime(&ts->stat_sample_time[ddir], NULL);
840 ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
3c39a379 841}