blob: e29d1c34af24d116f9a601437adb798a8ec194cf [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"
Jens Axboec97bd0f2009-05-27 13:11:20 +020011#include "smalloc.h"
Jens Axboe7c9b1bc2009-06-03 09:25:57 +020012#include "diskutil.h"
Jens Axboe9f8f2062007-04-02 15:08:01 +020013
14static int last_majdev, last_mindev;
Jens Axboec97bd0f2009-05-27 13:11:20 +020015static struct disk_util *last_du;
Jens Axboe9f8f2062007-04-02 15:08:01 +020016
Jens Axboe9ec77792012-08-02 08:27:41 +020017static struct fio_mutex *disk_util_mutex;
Jens Axboe9ec77792012-08-02 08:27:41 +020018
Jens Axboed09a64a2011-10-13 11:38:56 +020019FLIST_HEAD(disk_list);
Jens Axboe9f8f2062007-04-02 15:08:01 +020020
Jens Axboe2db4ecc2009-09-11 22:22:41 +020021static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
22 int majdev, int mindev, char *path);
23
Jens Axboec97bd0f2009-05-27 13:11:20 +020024static void disk_util_free(struct disk_util *du)
25{
26 if (du == last_du)
27 last_du = NULL;
28
Jens Axboeb8d541d2010-06-21 12:40:53 +020029 while (!flist_empty(&du->slaves)) {
30 struct disk_util *slave;
31
32 slave = flist_entry(du->slaves.next, struct disk_util, slavelist);
33 flist_del(&slave->slavelist);
34 slave->users--;
35 }
Bruce Cran93bcfd22012-02-20 20:18:19 +010036
Jens Axboec97bd0f2009-05-27 13:11:20 +020037 fio_mutex_remove(du->lock);
Jens Axboec97bd0f2009-05-27 13:11:20 +020038 sfree(du);
39}
40
Jens Axboe9f8f2062007-04-02 15:08:01 +020041static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
42{
43 unsigned in_flight;
Jens Axboed09a64a2011-10-13 11:38:56 +020044 unsigned long long sectors[2];
Jens Axboe9f8f2062007-04-02 15:08:01 +020045 char line[256];
46 FILE *f;
47 char *p;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010048 int ret;
Jens Axboe9f8f2062007-04-02 15:08:01 +020049
Jens Axboecd991b92008-03-07 13:19:35 +010050 dprint(FD_DISKUTIL, "open stat file: %s\n", du->path);
51
Jens Axboe9f8f2062007-04-02 15:08:01 +020052 f = fopen(du->path, "r");
53 if (!f)
54 return 1;
55
56 p = fgets(line, sizeof(line), f);
57 if (!p) {
58 fclose(f);
59 return 1;
60 }
61
Jens Axboecd991b92008-03-07 13:19:35 +010062 dprint(FD_DISKUTIL, "%s: %s", du->path, p);
63
64 ret = sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0],
Jens Axboed09a64a2011-10-13 11:38:56 +020065 &dus->merges[0], &sectors[0],
Jens Axboe5ec10ea2008-03-06 15:42:00 +010066 &dus->ticks[0], &dus->ios[1],
Jens Axboed09a64a2011-10-13 11:38:56 +020067 &dus->merges[1], &sectors[1],
Jens Axboe5ec10ea2008-03-06 15:42:00 +010068 &dus->ticks[1], &in_flight,
69 &dus->io_ticks, &dus->time_in_queue);
Jens Axboe9f8f2062007-04-02 15:08:01 +020070 fclose(f);
Jens Axboecd991b92008-03-07 13:19:35 +010071 dprint(FD_DISKUTIL, "%s: stat read ok? %d\n", du->path, ret == 1);
Jens Axboed09a64a2011-10-13 11:38:56 +020072 dus->sectors[0] = sectors[0];
73 dus->sectors[1] = sectors[1];
Jens Axboecd991b92008-03-07 13:19:35 +010074 return ret != 11;
Jens Axboe9f8f2062007-04-02 15:08:01 +020075}
76
77static void update_io_tick_disk(struct disk_util *du)
78{
79 struct disk_util_stat __dus, *dus, *ldus;
80 struct timeval t;
81
Jens Axboec97bd0f2009-05-27 13:11:20 +020082 if (!du->users)
83 return;
Jens Axboe90b97ac2010-06-21 12:35:02 +020084 if (get_io_ticks(du, &__dus))
85 return;
Jens Axboe9f8f2062007-04-02 15:08:01 +020086
87 dus = &du->dus;
88 ldus = &du->last_dus;
89
90 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
91 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
92 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
93 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
94 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
95 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
96 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
97 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
98 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
99 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
100
101 fio_gettime(&t, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +0200102 dus->msec += mtime_since(&du->time, &t);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200103 memcpy(&du->time, &t, sizeof(t));
104 memcpy(ldus, &__dus, sizeof(__dus));
105}
106
Jens Axboe9ec77792012-08-02 08:27:41 +0200107int update_io_ticks(void)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200108{
Jens Axboe01743ee2008-06-02 12:19:19 +0200109 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200110 struct disk_util *du;
Jens Axboe9ec77792012-08-02 08:27:41 +0200111 int ret = 0;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200112
Jens Axboecd991b92008-03-07 13:19:35 +0100113 dprint(FD_DISKUTIL, "update io ticks\n");
114
Jens Axboe9ec77792012-08-02 08:27:41 +0200115 fio_mutex_down(disk_util_mutex);
116
117 if (!disk_util_exit) {
118 flist_for_each(entry, &disk_list) {
119 du = flist_entry(entry, struct disk_util, list);
120 update_io_tick_disk(du);
121 }
122 } else
123 ret = 1;
124
125 fio_mutex_up(disk_util_mutex);
126 return ret;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200127}
128
Jens Axboee11c4102007-04-03 16:33:04 -0700129static struct disk_util *disk_util_exists(int major, int minor)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200130{
Jens Axboe01743ee2008-06-02 12:19:19 +0200131 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200132 struct disk_util *du;
133
Jens Axboe9ec77792012-08-02 08:27:41 +0200134 fio_mutex_down(disk_util_mutex);
135
Jens Axboe01743ee2008-06-02 12:19:19 +0200136 flist_for_each(entry, &disk_list) {
137 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200138
Jens Axboe9ec77792012-08-02 08:27:41 +0200139 if (major == du->major && minor == du->minor) {
140 fio_mutex_up(disk_util_mutex);
Jens Axboee11c4102007-04-03 16:33:04 -0700141 return du;
Jens Axboe9ec77792012-08-02 08:27:41 +0200142 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200143 }
144
Jens Axboe9ec77792012-08-02 08:27:41 +0200145 fio_mutex_up(disk_util_mutex);
Jens Axboee11c4102007-04-03 16:33:04 -0700146 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200147}
148
Shehjar Tikoo67423012008-09-30 10:35:44 +1000149static int get_device_numbers(char *file_name, int *maj, int *min)
150{
151 struct stat st;
152 int majdev, mindev;
153 char tempname[PATH_MAX], *p;
154
155 if (!lstat(file_name, &st)) {
156 if (S_ISBLK(st.st_mode)) {
157 majdev = major(st.st_rdev);
158 mindev = minor(st.st_rdev);
159 } else if (S_ISCHR(st.st_mode)) {
160 majdev = major(st.st_rdev);
161 mindev = minor(st.st_rdev);
162 if (fio_lookup_raw(st.st_rdev, &majdev, &mindev))
163 return -1;
164 } else if (S_ISFIFO(st.st_mode))
165 return -1;
166 else {
167 majdev = major(st.st_dev);
168 mindev = minor(st.st_dev);
169 }
170 } else {
171 /*
172 * must be a file, open "." in that path
173 */
174 strncpy(tempname, file_name, PATH_MAX - 1);
175 p = dirname(tempname);
176 if (stat(p, &st)) {
177 perror("disk util stat");
178 return -1;
179 }
180
181 majdev = major(st.st_dev);
182 mindev = minor(st.st_dev);
183 }
184
185 *min = mindev;
186 *maj = majdev;
187
188 return 0;
189}
190
191static int read_block_dev_entry(char *path, int *maj, int *min)
192{
193 char line[256], *p;
194 FILE *f;
195
196 f = fopen(path, "r");
197 if (!f) {
198 perror("open path");
199 return 1;
200 }
201
202 p = fgets(line, sizeof(line), f);
203 fclose(f);
204
205 if (!p)
206 return 1;
207
208 if (sscanf(p, "%u:%u", maj, min) != 2)
209 return 1;
210
211 return 0;
212}
213
Shehjar Tikoo67423012008-09-30 10:35:44 +1000214static void find_add_disk_slaves(struct thread_data *td, char *path,
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200215 struct disk_util *masterdu)
Shehjar Tikoo67423012008-09-30 10:35:44 +1000216{
217 DIR *dirhandle = NULL;
218 struct dirent *dirent = NULL;
219 char slavesdir[PATH_MAX], temppath[PATH_MAX], slavepath[PATH_MAX];
220 struct disk_util *slavedu = NULL;
221 int majdev, mindev;
222 ssize_t linklen;
223
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200224 sprintf(slavesdir, "%s/%s", path, "slaves");
Shehjar Tikoo67423012008-09-30 10:35:44 +1000225 dirhandle = opendir(slavesdir);
226 if (!dirhandle)
227 return;
228
229 while ((dirent = readdir(dirhandle)) != NULL) {
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200230 if (!strcmp(dirent->d_name, ".") ||
231 !strcmp(dirent->d_name, ".."))
Shehjar Tikoo67423012008-09-30 10:35:44 +1000232 continue;
233
Bruce Cranb9fd7882012-02-20 17:01:46 +0000234 sprintf(temppath, "%s%s%s", slavesdir, FIO_OS_PATH_SEPARATOR, dirent->d_name);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000235 /* Can we always assume that the slaves device entries
236 * are links to the real directories for the slave
237 * devices?
238 */
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200239 linklen = readlink(temppath, slavepath, PATH_MAX - 0);
240 if (linklen < 0) {
Shehjar Tikoo67423012008-09-30 10:35:44 +1000241 perror("readlink() for slave device.");
242 return;
243 }
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200244 slavepath[linklen] = '\0';
Shehjar Tikoo67423012008-09-30 10:35:44 +1000245
246 sprintf(temppath, "%s/%s/dev", slavesdir, slavepath);
247 if (read_block_dev_entry(temppath, &majdev, &mindev)) {
248 perror("Error getting slave device numbers.");
249 return;
250 }
251
Alan D. Brunelleb06de822009-09-11 14:57:10 -0400252 /*
253 * See if this maj,min already exists
254 */
255 slavedu = disk_util_exists(majdev, mindev);
256 if (slavedu)
257 continue;
258
Bruce Cranb9fd7882012-02-20 17:01:46 +0000259 sprintf(temppath, "%s%s%s", slavesdir, FIO_OS_PATH_SEPARATOR, slavepath);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000260 __init_per_file_disk_util(td, majdev, mindev, temppath);
261 slavedu = disk_util_exists(majdev, mindev);
262
263 /* Should probably use an assert here. slavedu should
264 * always be present at this point. */
Jens Axboeb8d541d2010-06-21 12:40:53 +0200265 if (slavedu) {
266 slavedu->users++;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000267 flist_add_tail(&slavedu->slavelist, &masterdu->slaves);
Jens Axboeb8d541d2010-06-21 12:40:53 +0200268 }
Shehjar Tikoo67423012008-09-30 10:35:44 +1000269 }
270
271 closedir(dirhandle);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000272}
273
Jens Axboeb8fefc22011-09-08 13:08:05 +0200274static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
Jens Axboec97bd0f2009-05-27 13:11:20 +0200275 int mindev, char *path)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200276{
277 struct disk_util *du, *__du;
Jens Axboe01743ee2008-06-02 12:19:19 +0200278 struct flist_head *entry;
Ken Raeburn4b919f72013-01-29 10:18:13 +0100279 int l;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200280
Jens Axboecd991b92008-03-07 13:19:35 +0100281 dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
282
Jens Axboec97bd0f2009-05-27 13:11:20 +0200283 du = smalloc(sizeof(*du));
Jens Axboefba5c5f2013-01-29 22:29:09 +0100284 if (!du) {
285 log_err("fio: smalloc() pool exhausted\n");
286 return NULL;
287 }
288
Jens Axboe9f8f2062007-04-02 15:08:01 +0200289 memset(du, 0, sizeof(*du));
Jens Axboe01743ee2008-06-02 12:19:19 +0200290 INIT_FLIST_HEAD(&du->list);
Ken Raeburn4b919f72013-01-29 10:18:13 +0100291 l = snprintf(du->path, sizeof(du->path), "%s/stat", path);
292 if (l < 0 || l >= sizeof(du->path)) {
293 log_err("constructed path \"%.100s[...]/stat\" larger than buffer (%zu bytes)\n",
294 path, sizeof(du->path) - 1);
295 sfree(du);
296 return NULL;
297 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200298 strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ);
Jens Axboee11c4102007-04-03 16:33:04 -0700299 du->sysfs_root = path;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200300 du->major = majdev;
301 du->minor = mindev;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000302 INIT_FLIST_HEAD(&du->slavelist);
303 INIT_FLIST_HEAD(&du->slaves);
Jens Axboe521da522012-08-02 11:21:36 +0200304 du->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200305 du->users = 0;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200306
Jens Axboe9ec77792012-08-02 08:27:41 +0200307 fio_mutex_down(disk_util_mutex);
308
Jens Axboe01743ee2008-06-02 12:19:19 +0200309 flist_for_each(entry, &disk_list) {
310 __du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200311
Jens Axboed09a64a2011-10-13 11:38:56 +0200312 dprint(FD_DISKUTIL, "found %s in list\n", __du->dus.name);
Jens Axboecd991b92008-03-07 13:19:35 +0100313
Jens Axboed09a64a2011-10-13 11:38:56 +0200314 if (!strcmp((char *) du->dus.name, (char *) __du->dus.name)) {
Jens Axboec97bd0f2009-05-27 13:11:20 +0200315 disk_util_free(du);
Jens Axboe9ec77792012-08-02 08:27:41 +0200316 fio_mutex_up(disk_util_mutex);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200317 return __du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200318 }
319 }
320
Jens Axboed09a64a2011-10-13 11:38:56 +0200321 dprint(FD_DISKUTIL, "add %s to list\n", du->dus.name);
Jens Axboecd991b92008-03-07 13:19:35 +0100322
Jens Axboe9f8f2062007-04-02 15:08:01 +0200323 fio_gettime(&du->time, NULL);
324 get_io_ticks(du, &du->last_dus);
325
Jens Axboe01743ee2008-06-02 12:19:19 +0200326 flist_add_tail(&du->list, &disk_list);
Jens Axboe9ec77792012-08-02 08:27:41 +0200327 fio_mutex_up(disk_util_mutex);
328
Shehjar Tikoo67423012008-09-30 10:35:44 +1000329 find_add_disk_slaves(td, path, du);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200330 return du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200331}
332
333static int check_dev_match(int majdev, int mindev, char *path)
334{
335 int major, minor;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200336
Shehjar Tikoo67423012008-09-30 10:35:44 +1000337 if (read_block_dev_entry(path, &major, &minor))
Jens Axboe9f8f2062007-04-02 15:08:01 +0200338 return 1;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200339
Jens Axboec97bd0f2009-05-27 13:11:20 +0200340 if (majdev == major && mindev == minor)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200341 return 0;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200342
Jens Axboe9f8f2062007-04-02 15:08:01 +0200343 return 1;
344}
345
Jens Axboeac0be2a2008-06-06 12:08:04 +0200346static int find_block_dir(int majdev, int mindev, char *path, int link_ok)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200347{
348 struct dirent *dir;
349 struct stat st;
350 int found = 0;
351 DIR *D;
352
353 D = opendir(path);
354 if (!D)
355 return 0;
356
357 while ((dir = readdir(D)) != NULL) {
358 char full_path[256];
359
360 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
361 continue;
362
Bruce Cranb9fd7882012-02-20 17:01:46 +0000363 sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200364
365 if (!strcmp(dir->d_name, "dev")) {
366 if (!check_dev_match(majdev, mindev, full_path)) {
367 found = 1;
368 break;
369 }
370 }
371
Jens Axboeac0be2a2008-06-06 12:08:04 +0200372 if (link_ok) {
373 if (stat(full_path, &st) == -1) {
374 perror("stat");
375 break;
376 }
377 } else {
378 if (lstat(full_path, &st) == -1) {
379 perror("stat");
380 break;
381 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200382 }
383
384 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
385 continue;
386
Jens Axboeac0be2a2008-06-06 12:08:04 +0200387 found = find_block_dir(majdev, mindev, full_path, 0);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200388 if (found) {
389 strcpy(path, full_path);
390 break;
391 }
392 }
393
394 closedir(D);
395 return found;
396}
397
Jens Axboec97bd0f2009-05-27 13:11:20 +0200398static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
399 int majdev, int mindev,
400 char *path)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200401{
402 struct stat st;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000403 char tmp[PATH_MAX];
Jens Axboe9f8f2062007-04-02 15:08:01 +0200404 char *p;
405
Shehjar Tikoo67423012008-09-30 10:35:44 +1000406 /*
407 * If there's a ../queue/ directory there, we are inside a partition.
408 * Check if that is the case and jump back. For loop/md/dm etc we
409 * are already in the right spot.
410 */
411 sprintf(tmp, "%s/../queue", path);
412 if (!stat(tmp, &st)) {
413 p = dirname(path);
414 sprintf(tmp, "%s/queue", p);
415 if (stat(tmp, &st)) {
416 log_err("unknown sysfs layout\n");
Jens Axboec97bd0f2009-05-27 13:11:20 +0200417 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200418 }
Shehjar Tikoo67423012008-09-30 10:35:44 +1000419 strncpy(tmp, p, PATH_MAX - 1);
420 sprintf(path, "%s", tmp);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200421 }
422
Shehjar Tikoo67423012008-09-30 10:35:44 +1000423 if (td->o.ioscheduler && !td->sysfs_root)
424 td->sysfs_root = strdup(path);
425
Jens Axboec97bd0f2009-05-27 13:11:20 +0200426 return disk_util_add(td, majdev, mindev, path);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000427}
428
Jens Axboec97bd0f2009-05-27 13:11:20 +0200429static struct disk_util *init_per_file_disk_util(struct thread_data *td,
430 char *filename)
Shehjar Tikoo67423012008-09-30 10:35:44 +1000431{
432
433 char foo[PATH_MAX];
434 struct disk_util *du;
435 int mindev, majdev;
436
Jens Axboec97bd0f2009-05-27 13:11:20 +0200437 if (get_device_numbers(filename, &majdev, &mindev))
438 return NULL;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000439
440 dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", filename, majdev,
441 mindev);
Jens Axboecd991b92008-03-07 13:19:35 +0100442
Jens Axboee11c4102007-04-03 16:33:04 -0700443 du = disk_util_exists(majdev, mindev);
444 if (du) {
445 if (td->o.ioscheduler && !td->sysfs_root)
446 td->sysfs_root = strdup(du->sysfs_root);
447
Jens Axboec97bd0f2009-05-27 13:11:20 +0200448 return du;
Jens Axboee11c4102007-04-03 16:33:04 -0700449 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200450
451 /*
452 * for an fs without a device, we will repeatedly stat through
453 * sysfs which can take oodles of time for thousands of files. so
454 * cache the last lookup and compare with that before going through
455 * everything again.
456 */
457 if (mindev == last_mindev && majdev == last_majdev)
Jens Axboec97bd0f2009-05-27 13:11:20 +0200458 return last_du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200459
460 last_mindev = mindev;
461 last_majdev = majdev;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100462
Jens Axboe9f8f2062007-04-02 15:08:01 +0200463 sprintf(foo, "/sys/block");
Jens Axboeac0be2a2008-06-06 12:08:04 +0200464 if (!find_block_dir(majdev, mindev, foo, 1))
Jens Axboec97bd0f2009-05-27 13:11:20 +0200465 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200466
Jens Axboec97bd0f2009-05-27 13:11:20 +0200467 return __init_per_file_disk_util(td, majdev, mindev, foo);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000468}
Jens Axboe9f8f2062007-04-02 15:08:01 +0200469
Jens Axboec97bd0f2009-05-27 13:11:20 +0200470static struct disk_util *__init_disk_util(struct thread_data *td,
471 struct fio_file *f)
Shehjar Tikoo67423012008-09-30 10:35:44 +1000472{
473 return init_per_file_disk_util(td, f->file_name);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200474}
475
476void init_disk_util(struct thread_data *td)
477{
478 struct fio_file *f;
479 unsigned int i;
480
481 if (!td->o.do_disk_util ||
482 (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
483 return;
484
485 for_each_file(td, f, i)
Jens Axboec97bd0f2009-05-27 13:11:20 +0200486 f->du = __init_disk_util(td, f);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200487}
488
Jens Axboef2f788d2011-10-13 14:03:52 +0200489static void show_agg_stats(struct disk_util_agg *agg, int terse)
Jens Axboed09a64a2011-10-13 11:38:56 +0200490{
491 if (!agg->slavecount)
492 return;
493
Jens Axboef2f788d2011-10-13 14:03:52 +0200494 if (!terse) {
495 log_info(", aggrios=%u/%u, aggrmerge=%u/%u, aggrticks=%u/%u,"
496 " aggrin_queue=%u, aggrutil=%3.2f%%",
497 agg->ios[0] / agg->slavecount,
498 agg->ios[1] / agg->slavecount,
499 agg->merges[0] / agg->slavecount,
500 agg->merges[1] / agg->slavecount,
501 agg->ticks[0] / agg->slavecount,
502 agg->ticks[1] / agg->slavecount,
503 agg->time_in_queue / agg->slavecount,
504 agg->max_util.u.f);
505 } else {
Jens Axboe97b007e2012-02-10 19:21:50 +0100506 log_info(";slaves;%u;%u;%u;%u;%u;%u;%u;%3.2f%%",
Jens Axboef2f788d2011-10-13 14:03:52 +0200507 agg->ios[0] / agg->slavecount,
508 agg->ios[1] / agg->slavecount,
509 agg->merges[0] / agg->slavecount,
510 agg->merges[1] / agg->slavecount,
511 agg->ticks[0] / agg->slavecount,
512 agg->ticks[1] / agg->slavecount,
513 agg->time_in_queue / agg->slavecount,
514 agg->max_util.u.f);
515 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200516}
517
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000518static void aggregate_slaves_stats(struct disk_util *masterdu)
519{
Jens Axboed09a64a2011-10-13 11:38:56 +0200520 struct disk_util_agg *agg = &masterdu->agg;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000521 struct disk_util_stat *dus;
522 struct flist_head *entry;
523 struct disk_util *slavedu;
Jens Axboed09a64a2011-10-13 11:38:56 +0200524 double util;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000525
526 flist_for_each(entry, &masterdu->slaves) {
527 slavedu = flist_entry(entry, struct disk_util, slavelist);
528 dus = &slavedu->dus;
Jens Axboed09a64a2011-10-13 11:38:56 +0200529 agg->ios[0] += dus->ios[0];
530 agg->ios[1] += dus->ios[1];
531 agg->merges[0] += dus->merges[0];
532 agg->merges[1] += dus->merges[1];
533 agg->sectors[0] += dus->sectors[0];
534 agg->sectors[1] += dus->sectors[1];
535 agg->ticks[0] += dus->ticks[0];
536 agg->ticks[1] += dus->ticks[1];
537 agg->time_in_queue += dus->time_in_queue;
538 agg->slavecount++;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000539
Jens Axboed09a64a2011-10-13 11:38:56 +0200540 util = (double) (100 * dus->io_ticks / (double) slavedu->dus.msec);
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000541 /* System utilization is the utilization of the
542 * component with the highest utilization.
543 */
Jens Axboed09a64a2011-10-13 11:38:56 +0200544 if (util > agg->max_util.u.f)
545 agg->max_util.u.f = util;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000546
547 }
548
Jens Axboed09a64a2011-10-13 11:38:56 +0200549 if (agg->max_util.u.f > 100.0)
550 agg->max_util.u.f = 100.0;
551}
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000552
Jens Axboe27357182012-09-27 11:31:50 +0200553void disk_util_prune_entries(void)
Jens Axboed09a64a2011-10-13 11:38:56 +0200554{
Jens Axboe9ec77792012-08-02 08:27:41 +0200555 fio_mutex_down(disk_util_mutex);
556
Jens Axboed09a64a2011-10-13 11:38:56 +0200557 while (!flist_empty(&disk_list)) {
Jens Axboe27357182012-09-27 11:31:50 +0200558 struct disk_util *du;
559
Jens Axboed09a64a2011-10-13 11:38:56 +0200560 du = flist_entry(disk_list.next, struct disk_util, list);
561 flist_del(&du->list);
562 disk_util_free(du);
563 }
564
565 last_majdev = last_mindev = -1;
Jens Axboe9ec77792012-08-02 08:27:41 +0200566 fio_mutex_up(disk_util_mutex);
567 fio_mutex_remove(disk_util_mutex);
Jens Axboed09a64a2011-10-13 11:38:56 +0200568}
569
Jens Axboef2f788d2011-10-13 14:03:52 +0200570void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
571 int terse)
Jens Axboed09a64a2011-10-13 11:38:56 +0200572{
Jens Axboe6914f592012-02-03 12:06:19 +0100573 double util = 0;
Jens Axboed09a64a2011-10-13 11:38:56 +0200574
Jens Axboe6914f592012-02-03 12:06:19 +0100575 if (dus->msec)
576 util = (double) 100 * dus->io_ticks / (double) dus->msec;
Jens Axboed09a64a2011-10-13 11:38:56 +0200577 if (util > 100.0)
578 util = 100.0;
579
Jens Axboef2f788d2011-10-13 14:03:52 +0200580 if (!terse) {
Jens Axboef1d64552012-02-10 19:30:23 +0100581 if (agg->slavecount)
582 log_info(" ");
583
Jens Axboef2f788d2011-10-13 14:03:52 +0200584 log_info(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, "
585 "in_queue=%u, util=%3.2f%%", dus->name,
Jens Axboed09a64a2011-10-13 11:38:56 +0200586 dus->ios[0], dus->ios[1],
587 dus->merges[0], dus->merges[1],
588 dus->ticks[0], dus->ticks[1],
589 dus->time_in_queue, util);
Jens Axboef2f788d2011-10-13 14:03:52 +0200590 } else {
Jens Axboe3d7cd9b2011-10-18 08:31:01 +0200591 log_info(";%s;%u;%u;%u;%u;%u;%u;%u;%3.2f%%",
Jens Axboef2f788d2011-10-13 14:03:52 +0200592 dus->name, dus->ios[0], dus->ios[1],
593 dus->merges[0], dus->merges[1],
594 dus->ticks[0], dus->ticks[1],
595 dus->time_in_queue, util);
596 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200597
598 /*
599 * If the device has slaves, aggregate the stats for
600 * those slave devices also.
601 */
Jens Axboef1d64552012-02-10 19:30:23 +0100602 show_agg_stats(agg, terse);
Jens Axboed09a64a2011-10-13 11:38:56 +0200603
Jens Axboef2f788d2011-10-13 14:03:52 +0200604 if (!terse)
605 log_info("\n");
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000606}
607
Shaohua Licc372b12012-09-17 09:12:18 +0200608static void print_disk_util_json(struct disk_util *du, struct json_array *array)
609{
610 double util = 0;
611 struct disk_util_stat *dus = &du->dus;
612 struct disk_util_agg *agg = &du->agg;
613 struct json_object *obj;
614
615 obj = json_create_object();
616 json_array_add_value_object(array, obj);
617
618 if (dus->msec)
619 util = (double) 100 * dus->io_ticks / (double) dus->msec;
620 if (util > 100.0)
621 util = 100.0;
622
623
624 json_object_add_value_string(obj, "name", dus->name);
625 json_object_add_value_int(obj, "read_ios", dus->ios[0]);
626 json_object_add_value_int(obj, "write_ios", dus->ios[1]);
627 json_object_add_value_int(obj, "read_merges", dus->merges[0]);
628 json_object_add_value_int(obj, "write_merges", dus->merges[1]);
629 json_object_add_value_int(obj, "read_ticks", dus->ticks[0]);
630 json_object_add_value_int(obj, "write_ticks", dus->ticks[1]);
631 json_object_add_value_int(obj, "in_queue", dus->time_in_queue);
632 json_object_add_value_float(obj, "util", util);
633
634 /*
635 * If the device has slaves, aggregate the stats for
636 * those slave devices also.
637 */
638 if (!agg->slavecount)
639 return;
640 json_object_add_value_int(obj, "aggr_read_ios",
641 agg->ios[0] / agg->slavecount);
642 json_object_add_value_int(obj, "aggr_write_ios",
643 agg->ios[1] / agg->slavecount);
644 json_object_add_value_int(obj, "aggr_read_merges",
645 agg->merges[0] / agg->slavecount);
646 json_object_add_value_int(obj, "aggr_write_merge",
647 agg->merges[1] / agg->slavecount);
648 json_object_add_value_int(obj, "aggr_read_ticks",
649 agg->ticks[0] / agg->slavecount);
650 json_object_add_value_int(obj, "aggr_write_ticks",
651 agg->ticks[1] / agg->slavecount);
652 json_object_add_value_int(obj, "aggr_in_queue",
653 agg->time_in_queue / agg->slavecount);
654 json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
655}
656
657void show_disk_util(int terse, struct json_object *parent)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200658{
Jens Axboed09a64a2011-10-13 11:38:56 +0200659 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200660 struct disk_util *du;
Shaohua Licc372b12012-09-17 09:12:18 +0200661 struct json_array *array = NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200662
Jens Axboe9ec77792012-08-02 08:27:41 +0200663 fio_mutex_down(disk_util_mutex);
664
665 if (flist_empty(&disk_list)) {
666 fio_mutex_up(disk_util_mutex);
Jens Axboe0a839f32007-04-26 09:02:34 +0200667 return;
Jens Axboe9ec77792012-08-02 08:27:41 +0200668 }
Jens Axboe0a839f32007-04-26 09:02:34 +0200669
Jens Axboef2f788d2011-10-13 14:03:52 +0200670 if (!terse)
671 log_info("\nDisk stats (read/write):\n");
Jens Axboe9f8f2062007-04-02 15:08:01 +0200672
Jens Axboeaeb32df2012-10-08 13:34:31 -0600673 if (output_format == FIO_OUTPUT_JSON) {
Shaohua Licc372b12012-09-17 09:12:18 +0200674 array = json_create_array();
675 json_object_add_value_array(parent, "disk_util", array);
676 }
677
Jens Axboe01743ee2008-06-02 12:19:19 +0200678 flist_for_each(entry, &disk_list) {
679 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200680
Jens Axboed09a64a2011-10-13 11:38:56 +0200681 aggregate_slaves_stats(du);
Jens Axboeaeb32df2012-10-08 13:34:31 -0600682 if (output_format == FIO_OUTPUT_JSON)
Shaohua Licc372b12012-09-17 09:12:18 +0200683 print_disk_util_json(du, array);
684 else
685 print_disk_util(&du->dus, &du->agg, terse);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200686 }
Jens Axboe9ec77792012-08-02 08:27:41 +0200687
688 fio_mutex_up(disk_util_mutex);
689}
690
691void setup_disk_util(void)
692{
Jens Axboe521da522012-08-02 11:21:36 +0200693 disk_util_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200694}