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