12 static struct itimerval itimer;
13 static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
15 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
22 f = fopen(du->path, "r");
26 p = fgets(line, sizeof(line), f);
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) {
41 static void update_io_tick_disk(struct disk_util *du)
43 struct disk_util_stat __dus, *dus, *ldus;
46 if (get_io_ticks(du, &__dus))
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);
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));
69 void update_io_ticks(void)
71 struct list_head *entry;
74 list_for_each(entry, &disk_list) {
75 du = list_entry(entry, struct disk_util, list);
76 update_io_tick_disk(du);
80 static int disk_util_exists(dev_t dev)
82 struct list_head *entry;
85 list_for_each(entry, &disk_list) {
86 du = list_entry(entry, struct disk_util, list);
95 static void disk_util_add(dev_t dev, char *path)
97 struct disk_util *du = malloc(sizeof(*du));
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));
105 gettimeofday(&du->time, NULL);
106 get_io_ticks(du, &du->last_dus);
108 list_add_tail(&du->list, &disk_list);
111 static int check_dev_match(dev_t dev, char *path)
113 unsigned int major, minor;
117 f = fopen(path, "r");
123 p = fgets(line, sizeof(line), f);
129 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
134 if (((major << 8) | minor) == dev) {
143 static int find_block_dir(dev_t dev, char *path)
154 while ((dir = readdir(D)) != NULL) {
157 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
159 if (!strcmp(dir->d_name, "device"))
162 sprintf(full_path, "%s/%s", path, dir->d_name);
164 if (!strcmp(dir->d_name, "dev")) {
165 if (!check_dev_match(dev, full_path)) {
171 if (stat(full_path, &st) == -1) {
176 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
179 found = find_block_dir(dev, full_path);
181 strcpy(path, full_path);
190 void init_disk_util(struct thread_data *td)
193 char foo[256], tmp[256];
197 if (!td->do_disk_util)
200 if (!stat(td->file_name, &st)) {
201 if (S_ISBLK(st.st_mode))
207 * must be a file, open "." in that path
209 strcpy(foo, td->file_name);
212 perror("disk util stat");
219 if (disk_util_exists(dev))
222 sprintf(foo, "/sys/block");
223 if (!find_block_dir(dev, foo))
227 * If there's a ../queue/ directory there, we are inside a partition.
228 * Check if that is the case and jump back. For loop/md/dm etc we
229 * are already in the right spot.
231 sprintf(tmp, "%s/../queue", foo);
232 if (!stat(tmp, &st)) {
234 sprintf(tmp, "%s/queue", p);
235 if (stat(tmp, &st)) {
236 fprintf(stderr, "unknown sysfs layout\n");
239 sprintf(foo, "%s", p);
242 td->sysfs_root = strdup(foo);
243 disk_util_add(dev, foo);
246 void disk_util_timer_arm(void)
248 itimer.it_value.tv_sec = 0;
249 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
250 setitimer(ITIMER_REAL, &itimer, NULL);
253 void update_rusage_stat(struct thread_data *td)
255 if (!(td->runtime[0] + td->runtime[1]))
258 getrusage(RUSAGE_SELF, &td->ru_end);
260 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
261 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
262 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
265 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
268 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
269 double *mean, double *dev)
273 if (is->samples == 0)
279 n = (double) is->samples;
280 *mean = (double) is->val / n;
281 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
282 if (!(*min + *max) && !(*mean + *dev))
288 static void show_group_stats(struct group_run_stats *rs, int id)
290 printf("\nRun status group %d (all jobs):\n", id);
292 if (rs->max_run[DDIR_READ])
293 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]);
294 if (rs->max_run[DDIR_WRITE])
295 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]);
298 static void show_disk_util(void)
300 struct disk_util_stat *dus;
301 struct list_head *entry;
302 struct disk_util *du;
305 printf("\nDisk stats (read/write):\n");
307 list_for_each(entry, &disk_list) {
308 du = list_entry(entry, struct disk_util, list);
311 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
315 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);
319 static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
322 char *ddir_str[] = { "read ", "write" };
323 unsigned long min, max;
324 unsigned long long bw;
327 if (!td->runtime[ddir])
330 bw = td->io_bytes[ddir] / td->runtime[ddir];
331 printf(" %s: io=%6lluMiB, bw=%6lluKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]);
333 if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
334 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
336 if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
337 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
339 if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
342 p_of_agg = mean * 100 / (double) rs->agg[ddir];
343 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);
347 static void show_thread_status(struct thread_data *td,
348 struct group_run_stats *rs)
350 double usr_cpu, sys_cpu;
352 if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error)
355 printf("%s: (groupid=%d): err=%2d:\n",td->name, td->groupid, td->error);
357 show_ddir_status(td, rs, td->ddir);
358 if (td->io_bytes[td->ddir ^ 1])
359 show_ddir_status(td, rs, td->ddir ^ 1);
361 if (td->runtime[0] + td->runtime[1]) {
362 double runt = td->runtime[0] + td->runtime[1];
364 usr_cpu = (double) td->usr_time * 100 / runt;
365 sys_cpu = (double) td->sys_time * 100 / runt;
371 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
374 void show_run_stats(void)
376 struct group_run_stats *runstats, *rs;
377 struct thread_data *td;
380 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
382 for (i = 0; i < groupid + 1; i++) {
385 memset(rs, 0, sizeof(*rs));
386 rs->min_bw[0] = rs->min_run[0] = ~0UL;
387 rs->min_bw[1] = rs->min_run[1] = ~0UL;
390 for (i = 0; i < thread_number; i++) {
391 unsigned long long rbw, wbw;
396 printf("%s: %s\n", td->name, td->verror);
400 rs = &runstats[td->groupid];
402 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
403 rs->min_run[0] = td->runtime[0];
404 if (td->runtime[0] > rs->max_run[0])
405 rs->max_run[0] = td->runtime[0];
406 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
407 rs->min_run[1] = td->runtime[1];
408 if (td->runtime[1] > rs->max_run[1])
409 rs->max_run[1] = td->runtime[1];
413 rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0];
415 wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1];
417 if (rbw < rs->min_bw[0])
419 if (wbw < rs->min_bw[1])
421 if (rbw > rs->max_bw[0])
423 if (wbw > rs->max_bw[1])
426 rs->io_kb[0] += td->io_bytes[0] >> 10;
427 rs->io_kb[1] += td->io_bytes[1] >> 10;
430 for (i = 0; i < groupid + 1; i++) {
434 rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
436 rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
440 * don't overwrite last signal output
444 for (i = 0; i < thread_number; i++) {
446 rs = &runstats[td->groupid];
448 show_thread_status(td, rs);
451 for (i = 0; i < groupid + 1; i++)
452 show_group_stats(&runstats[i], i);
457 static inline void add_stat_sample(struct io_stat *is, unsigned long val)
459 if (val > is->max_val)
461 if (val < is->min_val)
465 is->val_sq += val * val;
469 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
470 unsigned long val, int ddir)
472 if (iolog->nr_samples == iolog->max_samples) {
473 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
475 iolog->log = realloc(iolog->log, new_size);
476 iolog->max_samples <<= 1;
479 iolog->log[iolog->nr_samples].val = val;
480 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch);
481 iolog->log[iolog->nr_samples].ddir = ddir;
485 void add_clat_sample(struct thread_data *td, int ddir, unsigned long msec)
487 add_stat_sample(&td->clat_stat[ddir], msec);
490 add_log_sample(td, td->clat_log, msec, ddir);
493 void add_slat_sample(struct thread_data *td, int ddir, unsigned long msec)
495 add_stat_sample(&td->slat_stat[ddir], msec);
498 add_log_sample(td, td->slat_log, msec, ddir);
501 void add_bw_sample(struct thread_data *td, int ddir)
503 unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]);
506 if (spent < td->bw_avg_time)
509 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent;
510 add_stat_sample(&td->bw_stat[ddir], rate);
513 add_log_sample(td, td->bw_log, rate, ddir);
515 gettimeofday(&td->stat_sample_time[ddir], NULL);
516 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];