blob: fbc4268e800f9068d6bfcf5f3a6bb7a4a54cb4cc [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;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200279
Jens Axboecd991b92008-03-07 13:19:35 +0100280 dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
281
Jens Axboec97bd0f2009-05-27 13:11:20 +0200282 du = smalloc(sizeof(*du));
Jens Axboe9f8f2062007-04-02 15:08:01 +0200283 memset(du, 0, sizeof(*du));
Jens Axboe01743ee2008-06-02 12:19:19 +0200284 INIT_FLIST_HEAD(&du->list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200285 sprintf(du->path, "%s/stat", path);
Jens Axboed09a64a2011-10-13 11:38:56 +0200286 strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ);
Jens Axboee11c4102007-04-03 16:33:04 -0700287 du->sysfs_root = path;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200288 du->major = majdev;
289 du->minor = mindev;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000290 INIT_FLIST_HEAD(&du->slavelist);
291 INIT_FLIST_HEAD(&du->slaves);
Jens Axboe521da522012-08-02 11:21:36 +0200292 du->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200293 du->users = 0;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200294
Jens Axboe9ec77792012-08-02 08:27:41 +0200295 fio_mutex_down(disk_util_mutex);
296
Jens Axboe01743ee2008-06-02 12:19:19 +0200297 flist_for_each(entry, &disk_list) {
298 __du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200299
Jens Axboed09a64a2011-10-13 11:38:56 +0200300 dprint(FD_DISKUTIL, "found %s in list\n", __du->dus.name);
Jens Axboecd991b92008-03-07 13:19:35 +0100301
Jens Axboed09a64a2011-10-13 11:38:56 +0200302 if (!strcmp((char *) du->dus.name, (char *) __du->dus.name)) {
Jens Axboec97bd0f2009-05-27 13:11:20 +0200303 disk_util_free(du);
Jens Axboe9ec77792012-08-02 08:27:41 +0200304 fio_mutex_up(disk_util_mutex);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200305 return __du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200306 }
307 }
308
Jens Axboed09a64a2011-10-13 11:38:56 +0200309 dprint(FD_DISKUTIL, "add %s to list\n", du->dus.name);
Jens Axboecd991b92008-03-07 13:19:35 +0100310
Jens Axboe9f8f2062007-04-02 15:08:01 +0200311 fio_gettime(&du->time, NULL);
312 get_io_ticks(du, &du->last_dus);
313
Jens Axboe01743ee2008-06-02 12:19:19 +0200314 flist_add_tail(&du->list, &disk_list);
Jens Axboe9ec77792012-08-02 08:27:41 +0200315 fio_mutex_up(disk_util_mutex);
316
Shehjar Tikoo67423012008-09-30 10:35:44 +1000317 find_add_disk_slaves(td, path, du);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200318 return du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200319}
320
321static int check_dev_match(int majdev, int mindev, char *path)
322{
323 int major, minor;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200324
Shehjar Tikoo67423012008-09-30 10:35:44 +1000325 if (read_block_dev_entry(path, &major, &minor))
Jens Axboe9f8f2062007-04-02 15:08:01 +0200326 return 1;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200327
Jens Axboec97bd0f2009-05-27 13:11:20 +0200328 if (majdev == major && mindev == minor)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200329 return 0;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200330
Jens Axboe9f8f2062007-04-02 15:08:01 +0200331 return 1;
332}
333
Jens Axboeac0be2a2008-06-06 12:08:04 +0200334static int find_block_dir(int majdev, int mindev, char *path, int link_ok)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200335{
336 struct dirent *dir;
337 struct stat st;
338 int found = 0;
339 DIR *D;
340
341 D = opendir(path);
342 if (!D)
343 return 0;
344
345 while ((dir = readdir(D)) != NULL) {
346 char full_path[256];
347
348 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
349 continue;
350
Bruce Cranb9fd7882012-02-20 17:01:46 +0000351 sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200352
353 if (!strcmp(dir->d_name, "dev")) {
354 if (!check_dev_match(majdev, mindev, full_path)) {
355 found = 1;
356 break;
357 }
358 }
359
Jens Axboeac0be2a2008-06-06 12:08:04 +0200360 if (link_ok) {
361 if (stat(full_path, &st) == -1) {
362 perror("stat");
363 break;
364 }
365 } else {
366 if (lstat(full_path, &st) == -1) {
367 perror("stat");
368 break;
369 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200370 }
371
372 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
373 continue;
374
Jens Axboeac0be2a2008-06-06 12:08:04 +0200375 found = find_block_dir(majdev, mindev, full_path, 0);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200376 if (found) {
377 strcpy(path, full_path);
378 break;
379 }
380 }
381
382 closedir(D);
383 return found;
384}
385
Jens Axboec97bd0f2009-05-27 13:11:20 +0200386static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
387 int majdev, int mindev,
388 char *path)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200389{
390 struct stat st;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000391 char tmp[PATH_MAX];
Jens Axboe9f8f2062007-04-02 15:08:01 +0200392 char *p;
393
Shehjar Tikoo67423012008-09-30 10:35:44 +1000394 /*
395 * If there's a ../queue/ directory there, we are inside a partition.
396 * Check if that is the case and jump back. For loop/md/dm etc we
397 * are already in the right spot.
398 */
399 sprintf(tmp, "%s/../queue", path);
400 if (!stat(tmp, &st)) {
401 p = dirname(path);
402 sprintf(tmp, "%s/queue", p);
403 if (stat(tmp, &st)) {
404 log_err("unknown sysfs layout\n");
Jens Axboec97bd0f2009-05-27 13:11:20 +0200405 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200406 }
Shehjar Tikoo67423012008-09-30 10:35:44 +1000407 strncpy(tmp, p, PATH_MAX - 1);
408 sprintf(path, "%s", tmp);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200409 }
410
Shehjar Tikoo67423012008-09-30 10:35:44 +1000411 if (td->o.ioscheduler && !td->sysfs_root)
412 td->sysfs_root = strdup(path);
413
Jens Axboec97bd0f2009-05-27 13:11:20 +0200414 return disk_util_add(td, majdev, mindev, path);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000415}
416
Jens Axboec97bd0f2009-05-27 13:11:20 +0200417static struct disk_util *init_per_file_disk_util(struct thread_data *td,
418 char *filename)
Shehjar Tikoo67423012008-09-30 10:35:44 +1000419{
420
421 char foo[PATH_MAX];
422 struct disk_util *du;
423 int mindev, majdev;
424
Jens Axboec97bd0f2009-05-27 13:11:20 +0200425 if (get_device_numbers(filename, &majdev, &mindev))
426 return NULL;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000427
428 dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", filename, majdev,
429 mindev);
Jens Axboecd991b92008-03-07 13:19:35 +0100430
Jens Axboee11c4102007-04-03 16:33:04 -0700431 du = disk_util_exists(majdev, mindev);
432 if (du) {
433 if (td->o.ioscheduler && !td->sysfs_root)
434 td->sysfs_root = strdup(du->sysfs_root);
435
Jens Axboec97bd0f2009-05-27 13:11:20 +0200436 return du;
Jens Axboee11c4102007-04-03 16:33:04 -0700437 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200438
439 /*
440 * for an fs without a device, we will repeatedly stat through
441 * sysfs which can take oodles of time for thousands of files. so
442 * cache the last lookup and compare with that before going through
443 * everything again.
444 */
445 if (mindev == last_mindev && majdev == last_majdev)
Jens Axboec97bd0f2009-05-27 13:11:20 +0200446 return last_du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200447
448 last_mindev = mindev;
449 last_majdev = majdev;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100450
Jens Axboe9f8f2062007-04-02 15:08:01 +0200451 sprintf(foo, "/sys/block");
Jens Axboeac0be2a2008-06-06 12:08:04 +0200452 if (!find_block_dir(majdev, mindev, foo, 1))
Jens Axboec97bd0f2009-05-27 13:11:20 +0200453 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200454
Jens Axboec97bd0f2009-05-27 13:11:20 +0200455 return __init_per_file_disk_util(td, majdev, mindev, foo);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000456}
Jens Axboe9f8f2062007-04-02 15:08:01 +0200457
Jens Axboec97bd0f2009-05-27 13:11:20 +0200458static struct disk_util *__init_disk_util(struct thread_data *td,
459 struct fio_file *f)
Shehjar Tikoo67423012008-09-30 10:35:44 +1000460{
461 return init_per_file_disk_util(td, f->file_name);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200462}
463
464void init_disk_util(struct thread_data *td)
465{
466 struct fio_file *f;
467 unsigned int i;
468
469 if (!td->o.do_disk_util ||
470 (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
471 return;
472
473 for_each_file(td, f, i)
Jens Axboec97bd0f2009-05-27 13:11:20 +0200474 f->du = __init_disk_util(td, f);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200475}
476
Jens Axboef2f788d2011-10-13 14:03:52 +0200477static void show_agg_stats(struct disk_util_agg *agg, int terse)
Jens Axboed09a64a2011-10-13 11:38:56 +0200478{
479 if (!agg->slavecount)
480 return;
481
Jens Axboef2f788d2011-10-13 14:03:52 +0200482 if (!terse) {
483 log_info(", aggrios=%u/%u, aggrmerge=%u/%u, aggrticks=%u/%u,"
484 " aggrin_queue=%u, aggrutil=%3.2f%%",
485 agg->ios[0] / agg->slavecount,
486 agg->ios[1] / agg->slavecount,
487 agg->merges[0] / agg->slavecount,
488 agg->merges[1] / agg->slavecount,
489 agg->ticks[0] / agg->slavecount,
490 agg->ticks[1] / agg->slavecount,
491 agg->time_in_queue / agg->slavecount,
492 agg->max_util.u.f);
493 } else {
Jens Axboe97b007e2012-02-10 19:21:50 +0100494 log_info(";slaves;%u;%u;%u;%u;%u;%u;%u;%3.2f%%",
Jens Axboef2f788d2011-10-13 14:03:52 +0200495 agg->ios[0] / agg->slavecount,
496 agg->ios[1] / agg->slavecount,
497 agg->merges[0] / agg->slavecount,
498 agg->merges[1] / agg->slavecount,
499 agg->ticks[0] / agg->slavecount,
500 agg->ticks[1] / agg->slavecount,
501 agg->time_in_queue / agg->slavecount,
502 agg->max_util.u.f);
503 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200504}
505
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000506static void aggregate_slaves_stats(struct disk_util *masterdu)
507{
Jens Axboed09a64a2011-10-13 11:38:56 +0200508 struct disk_util_agg *agg = &masterdu->agg;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000509 struct disk_util_stat *dus;
510 struct flist_head *entry;
511 struct disk_util *slavedu;
Jens Axboed09a64a2011-10-13 11:38:56 +0200512 double util;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000513
514 flist_for_each(entry, &masterdu->slaves) {
515 slavedu = flist_entry(entry, struct disk_util, slavelist);
516 dus = &slavedu->dus;
Jens Axboed09a64a2011-10-13 11:38:56 +0200517 agg->ios[0] += dus->ios[0];
518 agg->ios[1] += dus->ios[1];
519 agg->merges[0] += dus->merges[0];
520 agg->merges[1] += dus->merges[1];
521 agg->sectors[0] += dus->sectors[0];
522 agg->sectors[1] += dus->sectors[1];
523 agg->ticks[0] += dus->ticks[0];
524 agg->ticks[1] += dus->ticks[1];
525 agg->time_in_queue += dus->time_in_queue;
526 agg->slavecount++;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000527
Jens Axboed09a64a2011-10-13 11:38:56 +0200528 util = (double) (100 * dus->io_ticks / (double) slavedu->dus.msec);
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000529 /* System utilization is the utilization of the
530 * component with the highest utilization.
531 */
Jens Axboed09a64a2011-10-13 11:38:56 +0200532 if (util > agg->max_util.u.f)
533 agg->max_util.u.f = util;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000534
535 }
536
Jens Axboed09a64a2011-10-13 11:38:56 +0200537 if (agg->max_util.u.f > 100.0)
538 agg->max_util.u.f = 100.0;
539}
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000540
Jens Axboe27357182012-09-27 11:31:50 +0200541void disk_util_prune_entries(void)
Jens Axboed09a64a2011-10-13 11:38:56 +0200542{
Jens Axboe9ec77792012-08-02 08:27:41 +0200543 fio_mutex_down(disk_util_mutex);
544
Jens Axboed09a64a2011-10-13 11:38:56 +0200545 while (!flist_empty(&disk_list)) {
Jens Axboe27357182012-09-27 11:31:50 +0200546 struct disk_util *du;
547
Jens Axboed09a64a2011-10-13 11:38:56 +0200548 du = flist_entry(disk_list.next, struct disk_util, list);
549 flist_del(&du->list);
550 disk_util_free(du);
551 }
552
553 last_majdev = last_mindev = -1;
Jens Axboe9ec77792012-08-02 08:27:41 +0200554 fio_mutex_up(disk_util_mutex);
555 fio_mutex_remove(disk_util_mutex);
Jens Axboed09a64a2011-10-13 11:38:56 +0200556}
557
Jens Axboef2f788d2011-10-13 14:03:52 +0200558void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
559 int terse)
Jens Axboed09a64a2011-10-13 11:38:56 +0200560{
Jens Axboe6914f592012-02-03 12:06:19 +0100561 double util = 0;
Jens Axboed09a64a2011-10-13 11:38:56 +0200562
Jens Axboe6914f592012-02-03 12:06:19 +0100563 if (dus->msec)
564 util = (double) 100 * dus->io_ticks / (double) dus->msec;
Jens Axboed09a64a2011-10-13 11:38:56 +0200565 if (util > 100.0)
566 util = 100.0;
567
Jens Axboef2f788d2011-10-13 14:03:52 +0200568 if (!terse) {
Jens Axboef1d64552012-02-10 19:30:23 +0100569 if (agg->slavecount)
570 log_info(" ");
571
Jens Axboef2f788d2011-10-13 14:03:52 +0200572 log_info(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, "
573 "in_queue=%u, util=%3.2f%%", dus->name,
Jens Axboed09a64a2011-10-13 11:38:56 +0200574 dus->ios[0], dus->ios[1],
575 dus->merges[0], dus->merges[1],
576 dus->ticks[0], dus->ticks[1],
577 dus->time_in_queue, util);
Jens Axboef2f788d2011-10-13 14:03:52 +0200578 } else {
Jens Axboe3d7cd9b2011-10-18 08:31:01 +0200579 log_info(";%s;%u;%u;%u;%u;%u;%u;%u;%3.2f%%",
Jens Axboef2f788d2011-10-13 14:03:52 +0200580 dus->name, dus->ios[0], dus->ios[1],
581 dus->merges[0], dus->merges[1],
582 dus->ticks[0], dus->ticks[1],
583 dus->time_in_queue, util);
584 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200585
586 /*
587 * If the device has slaves, aggregate the stats for
588 * those slave devices also.
589 */
Jens Axboef1d64552012-02-10 19:30:23 +0100590 show_agg_stats(agg, terse);
Jens Axboed09a64a2011-10-13 11:38:56 +0200591
Jens Axboef2f788d2011-10-13 14:03:52 +0200592 if (!terse)
593 log_info("\n");
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000594}
595
Shaohua Licc372b12012-09-17 09:12:18 +0200596static void print_disk_util_json(struct disk_util *du, struct json_array *array)
597{
598 double util = 0;
599 struct disk_util_stat *dus = &du->dus;
600 struct disk_util_agg *agg = &du->agg;
601 struct json_object *obj;
602
603 obj = json_create_object();
604 json_array_add_value_object(array, obj);
605
606 if (dus->msec)
607 util = (double) 100 * dus->io_ticks / (double) dus->msec;
608 if (util > 100.0)
609 util = 100.0;
610
611
612 json_object_add_value_string(obj, "name", dus->name);
613 json_object_add_value_int(obj, "read_ios", dus->ios[0]);
614 json_object_add_value_int(obj, "write_ios", dus->ios[1]);
615 json_object_add_value_int(obj, "read_merges", dus->merges[0]);
616 json_object_add_value_int(obj, "write_merges", dus->merges[1]);
617 json_object_add_value_int(obj, "read_ticks", dus->ticks[0]);
618 json_object_add_value_int(obj, "write_ticks", dus->ticks[1]);
619 json_object_add_value_int(obj, "in_queue", dus->time_in_queue);
620 json_object_add_value_float(obj, "util", util);
621
622 /*
623 * If the device has slaves, aggregate the stats for
624 * those slave devices also.
625 */
626 if (!agg->slavecount)
627 return;
628 json_object_add_value_int(obj, "aggr_read_ios",
629 agg->ios[0] / agg->slavecount);
630 json_object_add_value_int(obj, "aggr_write_ios",
631 agg->ios[1] / agg->slavecount);
632 json_object_add_value_int(obj, "aggr_read_merges",
633 agg->merges[0] / agg->slavecount);
634 json_object_add_value_int(obj, "aggr_write_merge",
635 agg->merges[1] / agg->slavecount);
636 json_object_add_value_int(obj, "aggr_read_ticks",
637 agg->ticks[0] / agg->slavecount);
638 json_object_add_value_int(obj, "aggr_write_ticks",
639 agg->ticks[1] / agg->slavecount);
640 json_object_add_value_int(obj, "aggr_in_queue",
641 agg->time_in_queue / agg->slavecount);
642 json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
643}
644
645void show_disk_util(int terse, struct json_object *parent)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200646{
Jens Axboed09a64a2011-10-13 11:38:56 +0200647 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200648 struct disk_util *du;
Shaohua Licc372b12012-09-17 09:12:18 +0200649 struct json_array *array = NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200650
Jens Axboe9ec77792012-08-02 08:27:41 +0200651 fio_mutex_down(disk_util_mutex);
652
653 if (flist_empty(&disk_list)) {
654 fio_mutex_up(disk_util_mutex);
Jens Axboe0a839f32007-04-26 09:02:34 +0200655 return;
Jens Axboe9ec77792012-08-02 08:27:41 +0200656 }
Jens Axboe0a839f32007-04-26 09:02:34 +0200657
Jens Axboef2f788d2011-10-13 14:03:52 +0200658 if (!terse)
659 log_info("\nDisk stats (read/write):\n");
Jens Axboe9f8f2062007-04-02 15:08:01 +0200660
Jens Axboeaeb32df2012-10-08 13:34:31 -0600661 if (output_format == FIO_OUTPUT_JSON) {
Shaohua Licc372b12012-09-17 09:12:18 +0200662 array = json_create_array();
663 json_object_add_value_array(parent, "disk_util", array);
664 }
665
Jens Axboe01743ee2008-06-02 12:19:19 +0200666 flist_for_each(entry, &disk_list) {
667 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200668
Jens Axboed09a64a2011-10-13 11:38:56 +0200669 aggregate_slaves_stats(du);
Jens Axboeaeb32df2012-10-08 13:34:31 -0600670 if (output_format == FIO_OUTPUT_JSON)
Shaohua Licc372b12012-09-17 09:12:18 +0200671 print_disk_util_json(du, array);
672 else
673 print_disk_util(&du->dus, &du->agg, terse);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200674 }
Jens Axboe9ec77792012-08-02 08:27:41 +0200675
676 fio_mutex_up(disk_util_mutex);
677}
678
679void setup_disk_util(void)
680{
Jens Axboe521da522012-08-02 11:21:36 +0200681 disk_util_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200682}