[PATCH] Make the libaio fsync fallback really work
[fio.git] / stat.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <dirent.h>
7 #include <libgen.h>
8 #include <math.h>
9
10 #include "fio.h"
11
12 static struct itimerval itimer;
13 static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
14
15 static 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
41 static 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         fio_gettime(&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
69 void 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
80 static 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
95 static void disk_util_add(dev_t dev, char *path)
96 {
97         struct disk_util *du, *__du;
98         struct list_head *entry;
99
100         du = malloc(sizeof(*du));
101         memset(du, 0, sizeof(*du));
102         INIT_LIST_HEAD(&du->list);
103         sprintf(du->path, "%s/stat", path);
104         du->name = strdup(basename(path));
105         du->dev = dev;
106
107         list_for_each(entry, &disk_list) {
108                 __du = list_entry(entry, struct disk_util, list);
109
110                 if (!strcmp(du->name, __du->name)) {
111                         free(du->name);
112                         free(du);
113                         return;
114                 }
115         }
116
117         fio_gettime(&du->time, NULL);
118         get_io_ticks(du, &du->last_dus);
119
120         list_add_tail(&du->list, &disk_list);
121 }
122
123 static int check_dev_match(dev_t dev, char *path)
124 {
125         unsigned int major, minor;
126         char line[256], *p;
127         FILE *f;
128
129         f = fopen(path, "r");
130         if (!f) {
131                 perror("open path");
132                 return 1;
133         }
134
135         p = fgets(line, sizeof(line), f);
136         if (!p) {
137                 fclose(f);
138                 return 1;
139         }
140
141         if (sscanf(p, "%u:%u", &major, &minor) != 2) {
142                 fclose(f);
143                 return 1;
144         }
145
146         if (((major << 8) | minor) == dev) {
147                 fclose(f);
148                 return 0;
149         }
150
151         fclose(f);
152         return 1;
153 }
154
155 static int find_block_dir(dev_t dev, char *path)
156 {
157         struct dirent *dir;
158         struct stat st;
159         int found = 0;
160         DIR *D;
161
162         D = opendir(path);
163         if (!D)
164                 return 0;
165
166         while ((dir = readdir(D)) != NULL) {
167                 char full_path[256];
168
169                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
170                         continue;
171
172                 sprintf(full_path, "%s/%s", path, dir->d_name);
173
174                 if (!strcmp(dir->d_name, "dev")) {
175                         if (!check_dev_match(dev, full_path)) {
176                                 found = 1;
177                                 break;
178                         }
179                 }
180
181                 if (lstat(full_path, &st) == -1) {
182                         perror("stat");
183                         break;
184                 }
185
186                 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
187                         continue;
188
189                 found = find_block_dir(dev, full_path);
190                 if (found) {
191                         strcpy(path, full_path);
192                         break;
193                 }
194         }
195
196         closedir(D);
197         return found;
198 }
199
200 void init_disk_util(struct thread_data *td)
201 {
202         struct fio_file *f;
203         struct stat st;
204         char foo[PATH_MAX], tmp[PATH_MAX];
205         dev_t dev;
206         char *p;
207
208         if (!td->do_disk_util || (td->io_ops->flags & (FIO_NETIO | FIO_NULLIO)))
209                 return;
210
211         /*
212          * Just use the same file, they are on the same device.
213          */
214         f = &td->files[0];
215         if (!stat(f->file_name, &st)) {
216                 if (S_ISBLK(st.st_mode))
217                         dev = st.st_rdev;
218                 else
219                         dev = st.st_dev;
220         } else {
221                 /*
222                  * must be a file, open "." in that path
223                  */
224                 strncpy(foo, f->file_name, PATH_MAX - 1);
225                 p = dirname(foo);
226                 if (stat(p, &st)) {
227                         perror("disk util stat");
228                         return;
229                 }
230
231                 dev = st.st_dev;
232         }
233
234         if (disk_util_exists(dev))
235                 return;
236                 
237         sprintf(foo, "/sys/block");
238         if (!find_block_dir(dev, foo))
239                 return;
240
241         /*
242          * If there's a ../queue/ directory there, we are inside a partition.
243          * Check if that is the case and jump back. For loop/md/dm etc we
244          * are already in the right spot.
245          */
246         sprintf(tmp, "%s/../queue", foo);
247         if (!stat(tmp, &st)) {
248                 p = dirname(foo);
249                 sprintf(tmp, "%s/queue", p);
250                 if (stat(tmp, &st)) {
251                         log_err("unknown sysfs layout\n");
252                         return;
253                 }
254                 strncpy(tmp, p, PATH_MAX - 1);
255                 sprintf(foo, "%s", tmp);
256         }
257
258         if (td->ioscheduler)
259                 td->sysfs_root = strdup(foo);
260
261         disk_util_add(dev, foo);
262 }
263
264 void disk_util_timer_arm(void)
265 {
266         itimer.it_value.tv_sec = 0;
267         itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
268         setitimer(ITIMER_REAL, &itimer, NULL);
269 }
270
271 void update_rusage_stat(struct thread_data *td)
272 {
273         getrusage(RUSAGE_SELF, &td->ru_end);
274
275         td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
276         td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
277         td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
278         
279         memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
280 }
281
282 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
283                     double *mean, double *dev)
284 {
285         double n = is->samples;
286
287         if (is->samples == 0)
288                 return 0;
289
290         *min = is->min_val;
291         *max = is->max_val;
292
293         n = (double) is->samples;
294         *mean = is->mean;
295
296         if (n > 1.0)
297                 *dev = sqrt(is->S / (n - 1.0));
298         else
299                 *dev = -1.0;
300
301         return 1;
302 }
303
304 static void show_group_stats(struct group_run_stats *rs, int id)
305 {
306         fprintf(f_out, "\nRun status group %d (all jobs):\n", id);
307
308         if (rs->max_run[DDIR_READ])
309                 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]);
310         if (rs->max_run[DDIR_WRITE])
311                 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]);
312 }
313
314 static void show_disk_util(void)
315 {
316         struct disk_util_stat *dus;
317         struct list_head *entry, *next;
318         struct disk_util *du;
319         double util;
320
321         fprintf(f_out, "\nDisk stats (read/write):\n");
322
323         list_for_each(entry, &disk_list) {
324                 du = list_entry(entry, struct disk_util, list);
325                 dus = &du->dus;
326
327                 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
328                 if (util > 100.0)
329                         util = 100.0;
330
331                 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);
332         }
333
334         /*
335          * now free the list
336          */
337         list_for_each_safe(entry, next, &disk_list) {
338                 list_del(entry);
339                 du = list_entry(entry, struct disk_util, list);
340                 free(du->name);
341                 free(du);
342         }
343 }
344
345 static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
346                              int ddir)
347 {
348         const char *ddir_str[] = { "read ", "write" };
349         unsigned long min, max;
350         unsigned long long bw;
351         double mean, dev;
352
353         if (!td->runtime[ddir])
354                 return;
355
356         bw = td->io_bytes[ddir] / td->runtime[ddir];
357         fprintf(f_out, "  %s: io=%6lluMiB, bw=%6lluKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]);
358
359         if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
360                 fprintf(f_out, "    slat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
361
362         if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
363                 fprintf(f_out, "    clat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
364
365         if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
366                 double p_of_agg;
367
368                 p_of_agg = mean * 100 / (double) rs->agg[ddir];
369                 fprintf(f_out, "    bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg, mean, dev);
370         }
371 }
372
373 static void show_thread_status(struct thread_data *td,
374                                struct group_run_stats *rs)
375 {
376         double usr_cpu, sys_cpu;
377         unsigned long runtime;
378         double io_u_dist[FIO_IO_U_MAP_NR];
379         int i;
380
381         if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error)
382                 return;
383
384         fprintf(f_out, "%s: (groupid=%d): err=%2d: pid=%d\n",td->name, td->groupid, td->error, td->pid);
385
386         show_ddir_status(td, rs, td->ddir);
387         if (td->io_bytes[td->ddir ^ 1])
388                 show_ddir_status(td, rs, td->ddir ^ 1);
389
390         runtime = mtime_since(&td->epoch, &td->end_time);
391         if (runtime) {
392                 double runt = (double) runtime;
393
394                 usr_cpu = (double) td->usr_time * 100 / runt;
395                 sys_cpu = (double) td->sys_time * 100 / runt;
396         } else {
397                 usr_cpu = 0;
398                 sys_cpu = 0;
399         }
400
401         fprintf(f_out, "  cpu          : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
402
403         /*
404          * Do depth distribution calculations
405          */
406         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
407                 io_u_dist[i] = (double) td->io_u_map[i] / (double) td->total_io_u;
408                 io_u_dist[i] *= 100.0;
409         }
410
411         fprintf(f_out, "  IO depths    : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3], io_u_dist[4], io_u_dist[5], io_u_dist[6]);
412
413         if (td->description)
414                 fprintf(f_out, "%s\n", td->description);
415 }
416
417 static void show_ddir_status_terse(struct thread_data *td,
418                                    struct group_run_stats *rs, int ddir)
419 {
420         unsigned long min, max;
421         unsigned long long bw;
422         double mean, dev;
423
424         bw = 0;
425         if (td->runtime[ddir])
426                 bw = td->io_bytes[ddir] / td->runtime[ddir];
427
428         fprintf(f_out, ",%llu,%llu,%lu", td->io_bytes[ddir] >> 10, bw, td->runtime[ddir]);
429
430         if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
431                 fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev);
432         else
433                 fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0);
434
435         if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
436                 fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev);
437         else
438                 fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0);
439
440         if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
441                 double p_of_agg;
442
443                 p_of_agg = mean * 100 / (double) rs->agg[ddir];
444                 fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", min, max, p_of_agg, mean, dev);
445         } else
446                 fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", 0UL, 0UL, 0.0, 0.0, 0.0);
447                 
448 }
449
450
451 static void show_thread_status_terse(struct thread_data *td,
452                                      struct group_run_stats *rs)
453 {
454         double usr_cpu, sys_cpu;
455
456         fprintf(f_out, "%s,%d,%d",td->name, td->groupid, td->error);
457
458         show_ddir_status_terse(td, rs, 0);
459         show_ddir_status_terse(td, rs, 1);
460
461         if (td->runtime[0] + td->runtime[1]) {
462                 double runt = (double) (td->runtime[0] + td->runtime[1]);
463
464                 usr_cpu = (double) td->usr_time * 100 / runt;
465                 sys_cpu = (double) td->sys_time * 100 / runt;
466         } else {
467                 usr_cpu = 0;
468                 sys_cpu = 0;
469         }
470
471         fprintf(f_out, ",%f%%,%f%%,%lu\n", usr_cpu, sys_cpu, td->ctx);
472 }
473
474 void show_run_stats(void)
475 {
476         struct group_run_stats *runstats, *rs;
477         struct thread_data *td;
478         int i;
479
480         runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
481
482         for (i = 0; i < groupid + 1; i++) {
483                 rs = &runstats[i];
484
485                 memset(rs, 0, sizeof(*rs));
486                 rs->min_bw[0] = rs->min_run[0] = ~0UL;
487                 rs->min_bw[1] = rs->min_run[1] = ~0UL;
488         }
489
490         for_each_td(td, i) {
491                 unsigned long long rbw, wbw;
492
493                 if (td->error) {
494                         fprintf(f_out, "%s: %s\n", td->name, td->verror);
495                         continue;
496                 }
497
498                 rs = &runstats[td->groupid];
499
500                 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
501                         rs->min_run[0] = td->runtime[0];
502                 if (td->runtime[0] > rs->max_run[0])
503                         rs->max_run[0] = td->runtime[0];
504                 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
505                         rs->min_run[1] = td->runtime[1];
506                 if (td->runtime[1] > rs->max_run[1])
507                         rs->max_run[1] = td->runtime[1];
508
509                 rbw = wbw = 0;
510                 if (td->runtime[0])
511                         rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0];
512                 if (td->runtime[1])
513                         wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1];
514
515                 if (rbw < rs->min_bw[0])
516                         rs->min_bw[0] = rbw;
517                 if (wbw < rs->min_bw[1])
518                         rs->min_bw[1] = wbw;
519                 if (rbw > rs->max_bw[0])
520                         rs->max_bw[0] = rbw;
521                 if (wbw > rs->max_bw[1])
522                         rs->max_bw[1] = wbw;
523
524                 rs->io_kb[0] += td->io_bytes[0] >> 10;
525                 rs->io_kb[1] += td->io_bytes[1] >> 10;
526         }
527
528         for (i = 0; i < groupid + 1; i++) {
529                 rs = &runstats[i];
530
531                 if (rs->max_run[0])
532                         rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
533                 if (rs->max_run[1])
534                         rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
535         }
536
537         /*
538          * don't overwrite last signal output
539          */
540         if (!terse_output)
541                 printf("\n");
542
543         for_each_td(td, i) {
544                 rs = &runstats[td->groupid];
545
546                 if (terse_output)
547                         show_thread_status_terse(td, rs);
548                 else
549                         show_thread_status(td, rs);
550         }
551
552         if (!terse_output) {
553                 for (i = 0; i < groupid + 1; i++)
554                         show_group_stats(&runstats[i], i);
555
556                 show_disk_util();
557         }
558
559         free(runstats);
560 }
561
562 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
563 {
564         double val = data;
565         double delta, n;
566
567         if (data > is->max_val)
568                 is->max_val = data;
569         if (data < is->min_val)
570                 is->min_val = data;
571
572         delta = val - is->mean;
573         n = is->samples + 1.0;
574         is->mean += delta / n;
575         is->S += delta * (val - is->mean);
576
577         is->samples++;
578 }
579
580 static void __add_log_sample(struct io_log *iolog, unsigned long val,
581                              enum fio_ddir ddir, unsigned long time)
582 {
583         if (iolog->nr_samples == iolog->max_samples) {
584                 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
585
586                 iolog->log = realloc(iolog->log, new_size);
587                 iolog->max_samples <<= 1;
588         }
589
590         iolog->log[iolog->nr_samples].val = val;
591         iolog->log[iolog->nr_samples].time = time;
592         iolog->log[iolog->nr_samples].ddir = ddir;
593         iolog->nr_samples++;
594 }
595
596 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
597                            unsigned long val, enum fio_ddir ddir)
598 {
599         __add_log_sample(iolog, val, ddir, mtime_since_now(&td->epoch));
600 }
601
602 void add_agg_sample(unsigned long val, enum fio_ddir ddir)
603 {
604         struct io_log *iolog = agg_io_log[ddir];
605
606         __add_log_sample(iolog, val, ddir, mtime_since_genesis());
607 }
608
609 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
610                      unsigned long msec)
611 {
612         add_stat_sample(&td->clat_stat[ddir], msec);
613
614         if (td->clat_log)
615                 add_log_sample(td, td->clat_log, msec, ddir);
616 }
617
618 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
619                      unsigned long msec)
620 {
621         add_stat_sample(&td->slat_stat[ddir], msec);
622
623         if (td->slat_log)
624                 add_log_sample(td, td->slat_log, msec, ddir);
625 }
626
627 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir,
628                    struct timeval *t)
629 {
630         unsigned long spent = mtime_since(&td->stat_sample_time[ddir], t);
631         unsigned long rate;
632
633         if (spent < td->bw_avg_time)
634                 return;
635
636         rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent;
637         add_stat_sample(&td->bw_stat[ddir], rate);
638
639         if (td->bw_log)
640                 add_log_sample(td, td->bw_log, rate, ddir);
641
642         fio_gettime(&td->stat_sample_time[ddir], NULL);
643         td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
644 }
645
646