blob: a78ae74b61646eb9d4a49d295d0b411dcecc6ef9 [file] [log] [blame]
Jens Axboe9f8f2062007-04-02 15:08:01 +02001#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
12static int last_majdev, last_mindev;
Jens Axboe9f8f2062007-04-02 15:08:01 +020013
Jens Axboe01743ee2008-06-02 12:19:19 +020014static struct flist_head disk_list = FLIST_HEAD_INIT(disk_list);
Jens Axboe9f8f2062007-04-02 15:08:01 +020015
16static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
17{
18 unsigned in_flight;
19 char line[256];
20 FILE *f;
21 char *p;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010022 int ret;
Jens Axboe9f8f2062007-04-02 15:08:01 +020023
Jens Axboecd991b92008-03-07 13:19:35 +010024 dprint(FD_DISKUTIL, "open stat file: %s\n", du->path);
25
Jens Axboe9f8f2062007-04-02 15:08:01 +020026 f = fopen(du->path, "r");
27 if (!f)
28 return 1;
29
30 p = fgets(line, sizeof(line), f);
31 if (!p) {
32 fclose(f);
33 return 1;
34 }
35
Jens Axboecd991b92008-03-07 13:19:35 +010036 dprint(FD_DISKUTIL, "%s: %s", du->path, p);
37
38 ret = sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0],
Jens Axboe5ec10ea2008-03-06 15:42:00 +010039 &dus->merges[0], &dus->sectors[0],
40 &dus->ticks[0], &dus->ios[1],
41 &dus->merges[1], &dus->sectors[1],
42 &dus->ticks[1], &in_flight,
43 &dus->io_ticks, &dus->time_in_queue);
Jens Axboe9f8f2062007-04-02 15:08:01 +020044 fclose(f);
Jens Axboecd991b92008-03-07 13:19:35 +010045 dprint(FD_DISKUTIL, "%s: stat read ok? %d\n", du->path, ret == 1);
46 return ret != 11;
Jens Axboe9f8f2062007-04-02 15:08:01 +020047}
48
49static void update_io_tick_disk(struct disk_util *du)
50{
51 struct disk_util_stat __dus, *dus, *ldus;
52 struct timeval t;
53
54 if (get_io_ticks(du, &__dus))
55 return;
56
57 dus = &du->dus;
58 ldus = &du->last_dus;
59
60 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
61 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
62 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
63 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
64 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
65 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
66 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
67 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
68 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
69 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
70
71 fio_gettime(&t, NULL);
72 du->msec += mtime_since(&du->time, &t);
73 memcpy(&du->time, &t, sizeof(t));
74 memcpy(ldus, &__dus, sizeof(__dus));
75}
76
77void update_io_ticks(void)
78{
Jens Axboe01743ee2008-06-02 12:19:19 +020079 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +020080 struct disk_util *du;
81
Jens Axboecd991b92008-03-07 13:19:35 +010082 dprint(FD_DISKUTIL, "update io ticks\n");
83
Jens Axboe01743ee2008-06-02 12:19:19 +020084 flist_for_each(entry, &disk_list) {
85 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +020086 update_io_tick_disk(du);
87 }
88}
89
Jens Axboee11c4102007-04-03 16:33:04 -070090static struct disk_util *disk_util_exists(int major, int minor)
Jens Axboe9f8f2062007-04-02 15:08:01 +020091{
Jens Axboe01743ee2008-06-02 12:19:19 +020092 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +020093 struct disk_util *du;
94
Jens Axboe01743ee2008-06-02 12:19:19 +020095 flist_for_each(entry, &disk_list) {
96 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +020097
98 if (major == du->major && minor == du->minor)
Jens Axboee11c4102007-04-03 16:33:04 -070099 return du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200100 }
101
Jens Axboee11c4102007-04-03 16:33:04 -0700102 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200103}
104
Shehjar Tikoo67423012008-09-30 10:35:44 +1000105static int get_device_numbers(char *file_name, int *maj, int *min)
106{
107 struct stat st;
108 int majdev, mindev;
109 char tempname[PATH_MAX], *p;
110
111 if (!lstat(file_name, &st)) {
112 if (S_ISBLK(st.st_mode)) {
113 majdev = major(st.st_rdev);
114 mindev = minor(st.st_rdev);
115 } else if (S_ISCHR(st.st_mode)) {
116 majdev = major(st.st_rdev);
117 mindev = minor(st.st_rdev);
118 if (fio_lookup_raw(st.st_rdev, &majdev, &mindev))
119 return -1;
120 } else if (S_ISFIFO(st.st_mode))
121 return -1;
122 else {
123 majdev = major(st.st_dev);
124 mindev = minor(st.st_dev);
125 }
126 } else {
127 /*
128 * must be a file, open "." in that path
129 */
130 strncpy(tempname, file_name, PATH_MAX - 1);
131 p = dirname(tempname);
132 if (stat(p, &st)) {
133 perror("disk util stat");
134 return -1;
135 }
136
137 majdev = major(st.st_dev);
138 mindev = minor(st.st_dev);
139 }
140
141 *min = mindev;
142 *maj = majdev;
143
144 return 0;
145}
146
147static int read_block_dev_entry(char *path, int *maj, int *min)
148{
149 char line[256], *p;
150 FILE *f;
151
152 f = fopen(path, "r");
153 if (!f) {
154 perror("open path");
155 return 1;
156 }
157
158 p = fgets(line, sizeof(line), f);
159 fclose(f);
160
161 if (!p)
162 return 1;
163
164 if (sscanf(p, "%u:%u", maj, min) != 2)
165 return 1;
166
167 return 0;
168}
169
170
171static void __init_per_file_disk_util(struct thread_data *td, int majdev,
172 int mindev, char * path);
173
174static void find_add_disk_slaves(struct thread_data *td, char *path,
175 struct disk_util *masterdu)
176{
177 DIR *dirhandle = NULL;
178 struct dirent *dirent = NULL;
179 char slavesdir[PATH_MAX], temppath[PATH_MAX], slavepath[PATH_MAX];
180 struct disk_util *slavedu = NULL;
181 int majdev, mindev;
182 ssize_t linklen;
183
184 sprintf(slavesdir, "%s/%s",path, "slaves");
185 dirhandle = opendir(slavesdir);
186 if (!dirhandle)
187 return;
188
189 while ((dirent = readdir(dirhandle)) != NULL) {
190 if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, ".."))
191 continue;
192
193 sprintf(temppath, "%s/%s", slavesdir, dirent->d_name);
194 /* Can we always assume that the slaves device entries
195 * are links to the real directories for the slave
196 * devices?
197 */
198 if ((linklen = readlink(temppath, slavepath, PATH_MAX-1)) < 0) {
199 perror("readlink() for slave device.");
200 return;
201 }
202 slavepath[linklen]='\0';
203
204 sprintf(temppath, "%s/%s/dev", slavesdir, slavepath);
205 if (read_block_dev_entry(temppath, &majdev, &mindev)) {
206 perror("Error getting slave device numbers.");
207 return;
208 }
209
210 sprintf(temppath, "%s/%s", slavesdir, slavepath);
211 __init_per_file_disk_util(td, majdev, mindev, temppath);
212 slavedu = disk_util_exists(majdev, mindev);
213
214 /* Should probably use an assert here. slavedu should
215 * always be present at this point. */
216 if (slavedu)
217 flist_add_tail(&slavedu->slavelist, &masterdu->slaves);
218 }
219
220 closedir(dirhandle);
221 return;
222}
223
224
225
226static void disk_util_add(struct thread_data * td, int majdev, int mindev,
227 char *path)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200228{
229 struct disk_util *du, *__du;
Jens Axboe01743ee2008-06-02 12:19:19 +0200230 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200231
Jens Axboecd991b92008-03-07 13:19:35 +0100232 dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
233
Jens Axboe9f8f2062007-04-02 15:08:01 +0200234 du = malloc(sizeof(*du));
235 memset(du, 0, sizeof(*du));
Jens Axboe01743ee2008-06-02 12:19:19 +0200236 INIT_FLIST_HEAD(&du->list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200237 sprintf(du->path, "%s/stat", path);
238 du->name = strdup(basename(path));
Jens Axboee11c4102007-04-03 16:33:04 -0700239 du->sysfs_root = path;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200240 du->major = majdev;
241 du->minor = mindev;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000242 INIT_FLIST_HEAD(&du->slavelist);
243 INIT_FLIST_HEAD(&du->slaves);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200244
Jens Axboe01743ee2008-06-02 12:19:19 +0200245 flist_for_each(entry, &disk_list) {
246 __du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200247
Jens Axboecd991b92008-03-07 13:19:35 +0100248 dprint(FD_DISKUTIL, "found %s in list\n", __du->name);
249
Jens Axboe9f8f2062007-04-02 15:08:01 +0200250 if (!strcmp(du->name, __du->name)) {
251 free(du->name);
252 free(du);
253 return;
254 }
255 }
256
Jens Axboecd991b92008-03-07 13:19:35 +0100257 dprint(FD_DISKUTIL, "add %s to list\n", du->name);
258
Jens Axboe9f8f2062007-04-02 15:08:01 +0200259 fio_gettime(&du->time, NULL);
260 get_io_ticks(du, &du->last_dus);
261
Jens Axboe01743ee2008-06-02 12:19:19 +0200262 flist_add_tail(&du->list, &disk_list);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000263 find_add_disk_slaves(td, path, du);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200264}
265
Shehjar Tikoo67423012008-09-30 10:35:44 +1000266
Jens Axboe9f8f2062007-04-02 15:08:01 +0200267static int check_dev_match(int majdev, int mindev, char *path)
268{
269 int major, minor;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200270
Shehjar Tikoo67423012008-09-30 10:35:44 +1000271 if (read_block_dev_entry(path, &major, &minor))
Jens Axboe9f8f2062007-04-02 15:08:01 +0200272 return 1;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200273
274 if (majdev == major && mindev == minor) {
Jens Axboe9f8f2062007-04-02 15:08:01 +0200275 return 0;
276 }
277
Jens Axboe9f8f2062007-04-02 15:08:01 +0200278 return 1;
279}
280
Jens Axboeac0be2a2008-06-06 12:08:04 +0200281static int find_block_dir(int majdev, int mindev, char *path, int link_ok)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200282{
283 struct dirent *dir;
284 struct stat st;
285 int found = 0;
286 DIR *D;
287
288 D = opendir(path);
289 if (!D)
290 return 0;
291
292 while ((dir = readdir(D)) != NULL) {
293 char full_path[256];
294
295 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
296 continue;
297
298 sprintf(full_path, "%s/%s", path, dir->d_name);
299
300 if (!strcmp(dir->d_name, "dev")) {
301 if (!check_dev_match(majdev, mindev, full_path)) {
302 found = 1;
303 break;
304 }
305 }
306
Jens Axboeac0be2a2008-06-06 12:08:04 +0200307 if (link_ok) {
308 if (stat(full_path, &st) == -1) {
309 perror("stat");
310 break;
311 }
312 } else {
313 if (lstat(full_path, &st) == -1) {
314 perror("stat");
315 break;
316 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200317 }
318
319 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
320 continue;
321
Jens Axboeac0be2a2008-06-06 12:08:04 +0200322 found = find_block_dir(majdev, mindev, full_path, 0);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200323 if (found) {
324 strcpy(path, full_path);
325 break;
326 }
327 }
328
329 closedir(D);
330 return found;
331}
332
Shehjar Tikoo67423012008-09-30 10:35:44 +1000333
334static void __init_per_file_disk_util(struct thread_data *td, int majdev,
335 int mindev, char * path)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200336{
337 struct stat st;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000338 char tmp[PATH_MAX];
Jens Axboe9f8f2062007-04-02 15:08:01 +0200339 char *p;
340
Shehjar Tikoo67423012008-09-30 10:35:44 +1000341 /*
342 * If there's a ../queue/ directory there, we are inside a partition.
343 * Check if that is the case and jump back. For loop/md/dm etc we
344 * are already in the right spot.
345 */
346 sprintf(tmp, "%s/../queue", path);
347 if (!stat(tmp, &st)) {
348 p = dirname(path);
349 sprintf(tmp, "%s/queue", p);
350 if (stat(tmp, &st)) {
351 log_err("unknown sysfs layout\n");
Jens Axboe9f8f2062007-04-02 15:08:01 +0200352 return;
353 }
Shehjar Tikoo67423012008-09-30 10:35:44 +1000354 strncpy(tmp, p, PATH_MAX - 1);
355 sprintf(path, "%s", tmp);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200356 }
357
Shehjar Tikoo67423012008-09-30 10:35:44 +1000358 if (td->o.ioscheduler && !td->sysfs_root)
359 td->sysfs_root = strdup(path);
360
361 disk_util_add(td, majdev, mindev, path);
362}
363
364
365
366static void init_per_file_disk_util(struct thread_data *td, char * filename)
367{
368
369 char foo[PATH_MAX];
370 struct disk_util *du;
371 int mindev, majdev;
372
373 if(get_device_numbers(filename, &majdev, &mindev))
374 return;
375
376 dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", filename, majdev,
377 mindev);
Jens Axboecd991b92008-03-07 13:19:35 +0100378
Jens Axboee11c4102007-04-03 16:33:04 -0700379 du = disk_util_exists(majdev, mindev);
380 if (du) {
381 if (td->o.ioscheduler && !td->sysfs_root)
382 td->sysfs_root = strdup(du->sysfs_root);
383
Jens Axboe9f8f2062007-04-02 15:08:01 +0200384 return;
Jens Axboee11c4102007-04-03 16:33:04 -0700385 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200386
387 /*
388 * for an fs without a device, we will repeatedly stat through
389 * sysfs which can take oodles of time for thousands of files. so
390 * cache the last lookup and compare with that before going through
391 * everything again.
392 */
393 if (mindev == last_mindev && majdev == last_majdev)
394 return;
395
396 last_mindev = mindev;
397 last_majdev = majdev;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100398
Jens Axboe9f8f2062007-04-02 15:08:01 +0200399 sprintf(foo, "/sys/block");
Jens Axboeac0be2a2008-06-06 12:08:04 +0200400 if (!find_block_dir(majdev, mindev, foo, 1))
Jens Axboe9f8f2062007-04-02 15:08:01 +0200401 return;
402
Shehjar Tikoo67423012008-09-30 10:35:44 +1000403 __init_per_file_disk_util(td, majdev, mindev, foo);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200404
Shehjar Tikoo67423012008-09-30 10:35:44 +1000405}
Jens Axboe9f8f2062007-04-02 15:08:01 +0200406
Shehjar Tikoo67423012008-09-30 10:35:44 +1000407static void __init_disk_util(struct thread_data *td, struct fio_file *f)
408{
409 return init_per_file_disk_util(td, f->file_name);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200410}
411
412void init_disk_util(struct thread_data *td)
413{
414 struct fio_file *f;
415 unsigned int i;
416
417 if (!td->o.do_disk_util ||
418 (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
419 return;
420
421 for_each_file(td, f, i)
422 __init_disk_util(td, f);
423}
424
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000425static void aggregate_slaves_stats(struct disk_util *masterdu)
426{
427 struct disk_util_stat *dus;
428 struct flist_head *entry;
429 struct disk_util *slavedu;
430 double util, max_util = 0;
431 int slavecount = 0;
432
Jens Axboe5e28df32008-09-30 08:39:17 +0200433 unsigned merges[2] = { 0, };
434 unsigned ticks[2] = { 0, };
435 unsigned time_in_queue = { 0, };
436 unsigned long long sectors[2] = { 0, };
437 unsigned ios[2] = { 0, };
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000438
439 flist_for_each(entry, &masterdu->slaves) {
440 slavedu = flist_entry(entry, struct disk_util, slavelist);
441 dus = &slavedu->dus;
442 ios[0] += dus->ios[0];
443 ios[1] += dus->ios[1];
444 merges[0] += dus->merges[0];
445 merges[1] += dus->merges[1];
446 sectors[0] += dus->sectors[0];
447 sectors[1] += dus->sectors[1];
448 ticks[0] += dus->ticks[0];
449 ticks[1] += dus->ticks[1];
450 time_in_queue += dus->time_in_queue;
451 ++slavecount;
452
453 util = (double) (100 * dus->io_ticks / (double) slavedu->msec);
454 /* System utilization is the utilization of the
455 * component with the highest utilization.
456 */
457 if (util > max_util)
458 max_util = util;
459
460 }
461
462 if (max_util > 100.0)
463 max_util = 100.0;
464
465 log_info(", aggrios=%u/%u, aggrmerge=%u/%u, aggrticks=%u/%u,"
466 " aggrin_queue=%u, aggrutil=%3.2f%%",
467 ios[0]/slavecount, ios[1]/slavecount,
468 merges[0]/slavecount, merges[1]/slavecount,
469 ticks[0]/slavecount, ticks[1]/slavecount,
470 time_in_queue/slavecount, max_util);
471
472}
473
474
475
Jens Axboe9f8f2062007-04-02 15:08:01 +0200476void show_disk_util(void)
477{
478 struct disk_util_stat *dus;
Jens Axboe01743ee2008-06-02 12:19:19 +0200479 struct flist_head *entry, *next;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200480 struct disk_util *du;
481 double util;
482
Jens Axboe01743ee2008-06-02 12:19:19 +0200483 if (flist_empty(&disk_list))
Jens Axboe0a839f32007-04-26 09:02:34 +0200484 return;
485
Jens Axboe9f8f2062007-04-02 15:08:01 +0200486 log_info("\nDisk stats (read/write):\n");
487
Jens Axboe01743ee2008-06-02 12:19:19 +0200488 flist_for_each(entry, &disk_list) {
489 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200490 dus = &du->dus;
491
492 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
493 if (util > 100.0)
494 util = 100.0;
495
Shehjar Tikoo67423012008-09-30 10:35:44 +1000496 /* If this node is the slave of a master device, as
497 * happens in case of software RAIDs, inward-indent
498 * this stats line to reflect a master-slave
499 * relationship. Because the master device gets added
500 * before the slave devices, we can safely assume that
501 * the master's stats line has been displayed in a
502 * previous iteration of this loop.
503 */
504 if(!flist_empty(&du->slavelist))
505 log_info(" ");
506
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100507 log_info(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, "
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000508 "in_queue=%u, util=%3.2f%%", du->name,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100509 dus->ios[0], dus->ios[1],
510 dus->merges[0], dus->merges[1],
511 dus->ticks[0], dus->ticks[1],
512 dus->time_in_queue, util);
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000513
514 /* If the device has slaves, aggregate the stats for
515 * those slave devices also.
516 */
517 if(!flist_empty(&du->slaves))
518 aggregate_slaves_stats(du);
519
520 log_info("\n");
Jens Axboe9f8f2062007-04-02 15:08:01 +0200521 }
522
523 /*
524 * now free the list
525 */
Jens Axboe01743ee2008-06-02 12:19:19 +0200526 flist_for_each_safe(entry, next, &disk_list) {
527 flist_del(entry);
528 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200529 free(du->name);
530 free(du);
531 }
532}