[PATCH] Warn on writable strings
[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"
3c39a379
JA
11
12static struct itimerval itimer;
5c4e1dbc 13static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
3c39a379
JA
14
15static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
16{
17 unsigned in_flight;
18 char line[256];
19 FILE *f;
20 char *p;
21
22 f = fopen(du->path, "r");
23 if (!f)
24 return 1;
25
26 p = fgets(line, sizeof(line), f);
27 if (!p) {
28 fclose(f);
29 return 1;
30 }
31
32 if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) {
33 fclose(f);
34 return 1;
35 }
36
37 fclose(f);
38 return 0;
39}
40
41static void update_io_tick_disk(struct disk_util *du)
42{
43 struct disk_util_stat __dus, *dus, *ldus;
44 struct timeval t;
45
46 if (get_io_ticks(du, &__dus))
47 return;
48
49 dus = &du->dus;
50 ldus = &du->last_dus;
51
52 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
53 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
54 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
55 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
56 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
57 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
58 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
59 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
60 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
61 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
62
63 gettimeofday(&t, NULL);
64 du->msec += mtime_since(&du->time, &t);
65 memcpy(&du->time, &t, sizeof(t));
66 memcpy(ldus, &__dus, sizeof(__dus));
67}
68
69void update_io_ticks(void)
70{
71 struct list_head *entry;
72 struct disk_util *du;
73
74 list_for_each(entry, &disk_list) {
75 du = list_entry(entry, struct disk_util, list);
76 update_io_tick_disk(du);
77 }
78}
79
80static int disk_util_exists(dev_t dev)
81{
82 struct list_head *entry;
83 struct disk_util *du;
84
85 list_for_each(entry, &disk_list) {
86 du = list_entry(entry, struct disk_util, list);
87
88 if (du->dev == dev)
89 return 1;
90 }
91
92 return 0;
93}
94
95static void disk_util_add(dev_t dev, char *path)
96{
97 struct disk_util *du = malloc(sizeof(*du));
98
99 memset(du, 0, sizeof(*du));
100 INIT_LIST_HEAD(&du->list);
101 sprintf(du->path, "%s/stat", path);
102 du->name = strdup(basename(path));
103 du->dev = dev;
104
105 gettimeofday(&du->time, NULL);
106 get_io_ticks(du, &du->last_dus);
107
108 list_add_tail(&du->list, &disk_list);
109}
110
111static int check_dev_match(dev_t dev, char *path)
112{
113 unsigned int major, minor;
114 char line[256], *p;
115 FILE *f;
116
117 f = fopen(path, "r");
118 if (!f) {
119 perror("open path");
120 return 1;
121 }
122
123 p = fgets(line, sizeof(line), f);
124 if (!p) {
125 fclose(f);
126 return 1;
127 }
128
129 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
130 fclose(f);
131 return 1;
132 }
133
134 if (((major << 8) | minor) == dev) {
135 fclose(f);
136 return 0;
137 }
138
139 fclose(f);
140 return 1;
141}
142
143static int find_block_dir(dev_t dev, char *path)
144{
145 struct dirent *dir;
146 struct stat st;
147 int found = 0;
148 DIR *D;
149
150 D = opendir(path);
151 if (!D)
152 return 0;
153
154 while ((dir = readdir(D)) != NULL) {
155 char full_path[256];
156
157 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
158 continue;
3c39a379
JA
159
160 sprintf(full_path, "%s/%s", path, dir->d_name);
161
162 if (!strcmp(dir->d_name, "dev")) {
163 if (!check_dev_match(dev, full_path)) {
164 found = 1;
165 break;
166 }
167 }
168
3307d653 169 if (lstat(full_path, &st) == -1) {
3c39a379
JA
170 perror("stat");
171 break;
172 }
173
174 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
175 continue;
176
177 found = find_block_dir(dev, full_path);
178 if (found) {
179 strcpy(path, full_path);
180 break;
181 }
182 }
183
184 closedir(D);
185 return found;
186}
187
188void init_disk_util(struct thread_data *td)
189{
53cdc686 190 struct fio_file *f;
3c39a379 191 struct stat st;
0bbab0e7 192 char foo[PATH_MAX], tmp[PATH_MAX];
3c39a379
JA
193 dev_t dev;
194 char *p;
195
196 if (!td->do_disk_util)
197 return;
198
53cdc686
JA
199 /*
200 * Just use the same file, they are on the same device.
201 */
202 f = &td->files[0];
203 if (!stat(f->file_name, &st)) {
3c39a379
JA
204 if (S_ISBLK(st.st_mode))
205 dev = st.st_rdev;
206 else
207 dev = st.st_dev;
208 } else {
209 /*
210 * must be a file, open "." in that path
211 */
0bbab0e7 212 strncpy(foo, f->file_name, PATH_MAX - 1);
3c39a379
JA
213 p = dirname(foo);
214 if (stat(p, &st)) {
215 perror("disk util stat");
216 return;
217 }
218
219 dev = st.st_dev;
220 }
221
222 if (disk_util_exists(dev))
223 return;
224
225 sprintf(foo, "/sys/block");
226 if (!find_block_dir(dev, foo))
227 return;
228
229 /*
230 * If there's a ../queue/ directory there, we are inside a partition.
231 * Check if that is the case and jump back. For loop/md/dm etc we
232 * are already in the right spot.
233 */
234 sprintf(tmp, "%s/../queue", foo);
235 if (!stat(tmp, &st)) {
236 p = dirname(foo);
237 sprintf(tmp, "%s/queue", p);
238 if (stat(tmp, &st)) {
3b70d7e5 239 log_err("unknown sysfs layout\n");
3c39a379
JA
240 return;
241 }
0bbab0e7 242 strncpy(tmp, p, PATH_MAX - 1);
84585003 243 sprintf(foo, "%s", tmp);
3c39a379
JA
244 }
245
eecf272f
JA
246 if (td->ioscheduler)
247 td->sysfs_root = strdup(foo);
248
3c39a379
JA
249 disk_util_add(dev, foo);
250}
251
252void disk_util_timer_arm(void)
253{
254 itimer.it_value.tv_sec = 0;
255 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
256 setitimer(ITIMER_REAL, &itimer, NULL);
257}
258
259void update_rusage_stat(struct thread_data *td)
260{
261 if (!(td->runtime[0] + td->runtime[1]))
262 return;
263
264 getrusage(RUSAGE_SELF, &td->ru_end);
265
266 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
267 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
268 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
269
270
271 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
272}
273
274static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
275 double *mean, double *dev)
276{
277 double n;
278
279 if (is->samples == 0)
280 return 0;
281
282 *min = is->min_val;
283 *max = is->max_val;
284
285 n = (double) is->samples;
286 *mean = (double) is->val / n;
287 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
3c39a379
JA
288
289 return 1;
290}
291
292static void show_group_stats(struct group_run_stats *rs, int id)
293{
eb8bbf48 294 fprintf(f_out, "\nRun status group %d (all jobs):\n", id);
3c39a379
JA
295
296 if (rs->max_run[DDIR_READ])
eb8bbf48 297 fprintf(f_out, " READ: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_kb[0] >> 10, rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]);
3c39a379 298 if (rs->max_run[DDIR_WRITE])
eb8bbf48 299 fprintf(f_out, " WRITE: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_kb[1] >> 10, rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]);
3c39a379
JA
300}
301
302static void show_disk_util(void)
303{
304 struct disk_util_stat *dus;
b4a6a59a 305 struct list_head *entry, *next;
3c39a379
JA
306 struct disk_util *du;
307 double util;
308
eb8bbf48 309 fprintf(f_out, "\nDisk stats (read/write):\n");
3c39a379
JA
310
311 list_for_each(entry, &disk_list) {
312 du = list_entry(entry, struct disk_util, list);
313 dus = &du->dus;
314
315 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
316 if (util > 100.0)
317 util = 100.0;
318
eb8bbf48 319 fprintf(f_out, " %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util);
3c39a379 320 }
b4a6a59a
JA
321
322 /*
323 * now free the list
324 */
325 list_for_each_safe(entry, next, &disk_list) {
326 list_del(entry);
327 du = list_entry(entry, struct disk_util, list);
328 free(du->name);
329 free(du);
330 }
3c39a379
JA
331}
332
333static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
334 int ddir)
335{
3c9b60c1 336 const char *ddir_str[] = { "read ", "write" };
3c39a379
JA
337 unsigned long min, max;
338 unsigned long long bw;
339 double mean, dev;
340
341 if (!td->runtime[ddir])
342 return;
343
344 bw = td->io_bytes[ddir] / td->runtime[ddir];
eb8bbf48 345 fprintf(f_out, " %s: io=%6lluMiB, bw=%6lluKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]);
3c39a379
JA
346
347 if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
eb8bbf48 348 fprintf(f_out, " slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
3c39a379
JA
349
350 if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
eb8bbf48 351 fprintf(f_out, " clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
3c39a379
JA
352
353 if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
354 double p_of_agg;
355
356 p_of_agg = mean * 100 / (double) rs->agg[ddir];
eb8bbf48 357 fprintf(f_out, " bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, dev=%5.02f\n", min, max, p_of_agg, mean, dev);
3c39a379
JA
358 }
359}
360
361static void show_thread_status(struct thread_data *td,
362 struct group_run_stats *rs)
363{
364 double usr_cpu, sys_cpu;
365
366 if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error)
367 return;
368
eb8bbf48 369 fprintf(f_out, "%s: (groupid=%d): err=%2d:\n",td->name, td->groupid, td->error);
3c39a379
JA
370
371 show_ddir_status(td, rs, td->ddir);
372 if (td->io_bytes[td->ddir ^ 1])
373 show_ddir_status(td, rs, td->ddir ^ 1);
374
375 if (td->runtime[0] + td->runtime[1]) {
376 double runt = td->runtime[0] + td->runtime[1];
377
378 usr_cpu = (double) td->usr_time * 100 / runt;
379 sys_cpu = (double) td->sys_time * 100 / runt;
380 } else {
381 usr_cpu = 0;
382 sys_cpu = 0;
383 }
384
eb8bbf48 385 fprintf(f_out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
3c39a379
JA
386}
387
c6ae0a5b
JA
388static void show_ddir_status_terse(struct thread_data *td,
389 struct group_run_stats *rs, int ddir)
390{
391 unsigned long min, max;
392 unsigned long long bw;
393 double mean, dev;
394
395 bw = 0;
396 if (td->runtime[ddir])
397 bw = td->io_bytes[ddir] / td->runtime[ddir];
398
399 fprintf(f_out, ",%llu,%llu,%lu", td->io_bytes[ddir] >> 10, bw, td->runtime[ddir]);
400
401 if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
402 fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev);
403 else
404 fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0);
405
406 if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
407 fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev);
408 else
409 fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0);
410
411 if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
412 double p_of_agg;
413
414 p_of_agg = mean * 100 / (double) rs->agg[ddir];
415 fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", min, max, p_of_agg, mean, dev);
416 } else
417 fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", 0UL, 0UL, 0.0, 0.0, 0.0);
418
419}
420
421
422static void show_thread_status_terse(struct thread_data *td,
423 struct group_run_stats *rs)
424{
425 double usr_cpu, sys_cpu;
426
427 fprintf(f_out, "%s,%d,%d",td->name, td->groupid, td->error);
428
429 show_ddir_status_terse(td, rs, 0);
430 show_ddir_status_terse(td, rs, 1);
431
432 if (td->runtime[0] + td->runtime[1]) {
433 double runt = td->runtime[0] + td->runtime[1];
434
435 usr_cpu = (double) td->usr_time * 100 / runt;
436 sys_cpu = (double) td->sys_time * 100 / runt;
437 } else {
438 usr_cpu = 0;
439 sys_cpu = 0;
440 }
441
442 fprintf(f_out, ",%f%%,%f%%,%lu\n", usr_cpu, sys_cpu, td->ctx);
443}
444
3c39a379
JA
445void show_run_stats(void)
446{
447 struct group_run_stats *runstats, *rs;
448 struct thread_data *td;
449 int i;
450
451 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
452
453 for (i = 0; i < groupid + 1; i++) {
454 rs = &runstats[i];
455
456 memset(rs, 0, sizeof(*rs));
457 rs->min_bw[0] = rs->min_run[0] = ~0UL;
458 rs->min_bw[1] = rs->min_run[1] = ~0UL;
459 }
460
34572e28 461 for_each_td(td, i) {
3c39a379
JA
462 unsigned long long rbw, wbw;
463
3c39a379 464 if (td->error) {
eb8bbf48 465 fprintf(f_out, "%s: %s\n", td->name, td->verror);
3c39a379
JA
466 continue;
467 }
468
469 rs = &runstats[td->groupid];
470
471 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
472 rs->min_run[0] = td->runtime[0];
473 if (td->runtime[0] > rs->max_run[0])
474 rs->max_run[0] = td->runtime[0];
475 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
476 rs->min_run[1] = td->runtime[1];
477 if (td->runtime[1] > rs->max_run[1])
478 rs->max_run[1] = td->runtime[1];
479
480 rbw = wbw = 0;
481 if (td->runtime[0])
482 rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0];
483 if (td->runtime[1])
484 wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1];
485
486 if (rbw < rs->min_bw[0])
487 rs->min_bw[0] = rbw;
488 if (wbw < rs->min_bw[1])
489 rs->min_bw[1] = wbw;
490 if (rbw > rs->max_bw[0])
491 rs->max_bw[0] = rbw;
492 if (wbw > rs->max_bw[1])
493 rs->max_bw[1] = wbw;
494
495 rs->io_kb[0] += td->io_bytes[0] >> 10;
496 rs->io_kb[1] += td->io_bytes[1] >> 10;
497 }
498
499 for (i = 0; i < groupid + 1; i++) {
500 rs = &runstats[i];
501
502 if (rs->max_run[0])
503 rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
504 if (rs->max_run[1])
505 rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
506 }
507
508 /*
509 * don't overwrite last signal output
510 */
c6ae0a5b
JA
511 if (!terse_output)
512 printf("\n");
3c39a379 513
34572e28 514 for_each_td(td, i) {
3c39a379
JA
515 rs = &runstats[td->groupid];
516
c6ae0a5b
JA
517 if (terse_output)
518 show_thread_status_terse(td, rs);
519 else
520 show_thread_status(td, rs);
3c39a379
JA
521 }
522
c6ae0a5b
JA
523 if (!terse_output) {
524 for (i = 0; i < groupid + 1; i++)
525 show_group_stats(&runstats[i], i);
3c39a379 526
c6ae0a5b
JA
527 show_disk_util();
528 }
eecf272f
JA
529
530 free(runstats);
3c39a379
JA
531}
532
533static inline void add_stat_sample(struct io_stat *is, unsigned long val)
534{
535 if (val > is->max_val)
536 is->max_val = val;
537 if (val < is->min_val)
538 is->min_val = val;
539
540 is->val += val;
541 is->val_sq += val * val;
542 is->samples++;
543}
544
545static void add_log_sample(struct thread_data *td, struct io_log *iolog,
546 unsigned long val, int ddir)
547{
548 if (iolog->nr_samples == iolog->max_samples) {
549 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
550
551 iolog->log = realloc(iolog->log, new_size);
552 iolog->max_samples <<= 1;
553 }
554
555 iolog->log[iolog->nr_samples].val = val;
556 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch);
557 iolog->log[iolog->nr_samples].ddir = ddir;
558 iolog->nr_samples++;
559}
560
561void add_clat_sample(struct thread_data *td, int ddir, unsigned long msec)
562{
563 add_stat_sample(&td->clat_stat[ddir], msec);
564
565 if (td->clat_log)
566 add_log_sample(td, td->clat_log, msec, ddir);
567}
568
569void add_slat_sample(struct thread_data *td, int ddir, unsigned long msec)
570{
571 add_stat_sample(&td->slat_stat[ddir], msec);
572
573 if (td->slat_log)
574 add_log_sample(td, td->slat_log, msec, ddir);
575}
576
577void add_bw_sample(struct thread_data *td, int ddir)
578{
579 unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]);
580 unsigned long rate;
581
582 if (spent < td->bw_avg_time)
583 return;
584
585 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent;
586 add_stat_sample(&td->bw_stat[ddir], rate);
587
588 if (td->bw_log)
589 add_log_sample(td, td->bw_log, rate, ddir);
590
591 gettimeofday(&td->stat_sample_time[ddir], NULL);
592 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
593}
594
595