blob: 24e17820370dce4b13f584a50eb4aff904dbad30 [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 Axboed09a64a2011-10-13 11:38:56 +020017FLIST_HEAD(disk_list);
Jens Axboe9f8f2062007-04-02 15:08:01 +020018
Jens Axboe2db4ecc2009-09-11 22:22:41 +020019static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
20 int majdev, int mindev, char *path);
21
Jens Axboec97bd0f2009-05-27 13:11:20 +020022static void disk_util_free(struct disk_util *du)
23{
24 if (du == last_du)
25 last_du = NULL;
26
Jens Axboeb8d541d2010-06-21 12:40:53 +020027 while (!flist_empty(&du->slaves)) {
28 struct disk_util *slave;
29
30 slave = flist_entry(du->slaves.next, struct disk_util, slavelist);
31 flist_del(&slave->slavelist);
32 slave->users--;
33 }
34
Jens Axboec97bd0f2009-05-27 13:11:20 +020035 fio_mutex_remove(du->lock);
Jens Axboec97bd0f2009-05-27 13:11:20 +020036 sfree(du);
37}
38
Jens Axboe9f8f2062007-04-02 15:08:01 +020039static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
40{
41 unsigned in_flight;
Jens Axboed09a64a2011-10-13 11:38:56 +020042 unsigned long long sectors[2];
Jens Axboe9f8f2062007-04-02 15:08:01 +020043 char line[256];
44 FILE *f;
45 char *p;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010046 int ret;
Jens Axboe9f8f2062007-04-02 15:08:01 +020047
Jens Axboecd991b92008-03-07 13:19:35 +010048 dprint(FD_DISKUTIL, "open stat file: %s\n", du->path);
49
Jens Axboe9f8f2062007-04-02 15:08:01 +020050 f = fopen(du->path, "r");
51 if (!f)
52 return 1;
53
54 p = fgets(line, sizeof(line), f);
55 if (!p) {
56 fclose(f);
57 return 1;
58 }
59
Jens Axboecd991b92008-03-07 13:19:35 +010060 dprint(FD_DISKUTIL, "%s: %s", du->path, p);
61
62 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 +020063 &dus->merges[0], &sectors[0],
Jens Axboe5ec10ea2008-03-06 15:42:00 +010064 &dus->ticks[0], &dus->ios[1],
Jens Axboed09a64a2011-10-13 11:38:56 +020065 &dus->merges[1], &sectors[1],
Jens Axboe5ec10ea2008-03-06 15:42:00 +010066 &dus->ticks[1], &in_flight,
67 &dus->io_ticks, &dus->time_in_queue);
Jens Axboe9f8f2062007-04-02 15:08:01 +020068 fclose(f);
Jens Axboecd991b92008-03-07 13:19:35 +010069 dprint(FD_DISKUTIL, "%s: stat read ok? %d\n", du->path, ret == 1);
Jens Axboed09a64a2011-10-13 11:38:56 +020070 dus->sectors[0] = sectors[0];
71 dus->sectors[1] = sectors[1];
Jens Axboecd991b92008-03-07 13:19:35 +010072 return ret != 11;
Jens Axboe9f8f2062007-04-02 15:08:01 +020073}
74
75static void update_io_tick_disk(struct disk_util *du)
76{
77 struct disk_util_stat __dus, *dus, *ldus;
78 struct timeval t;
79
Jens Axboec97bd0f2009-05-27 13:11:20 +020080 if (!du->users)
81 return;
Jens Axboe90b97ac2010-06-21 12:35:02 +020082 if (get_io_ticks(du, &__dus))
83 return;
Jens Axboe9f8f2062007-04-02 15:08:01 +020084
85 dus = &du->dus;
86 ldus = &du->last_dus;
87
88 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
89 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
90 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
91 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
92 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
93 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
94 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
95 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
96 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
97 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
98
99 fio_gettime(&t, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +0200100 dus->msec += mtime_since(&du->time, &t);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200101 memcpy(&du->time, &t, sizeof(t));
102 memcpy(ldus, &__dus, sizeof(__dus));
103}
104
105void update_io_ticks(void)
106{
Jens Axboe01743ee2008-06-02 12:19:19 +0200107 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200108 struct disk_util *du;
109
Jens Axboecd991b92008-03-07 13:19:35 +0100110 dprint(FD_DISKUTIL, "update io ticks\n");
111
Jens Axboe01743ee2008-06-02 12:19:19 +0200112 flist_for_each(entry, &disk_list) {
113 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200114 update_io_tick_disk(du);
115 }
116}
117
Jens Axboee11c4102007-04-03 16:33:04 -0700118static struct disk_util *disk_util_exists(int major, int minor)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200119{
Jens Axboe01743ee2008-06-02 12:19:19 +0200120 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200121 struct disk_util *du;
122
Jens Axboe01743ee2008-06-02 12:19:19 +0200123 flist_for_each(entry, &disk_list) {
124 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200125
126 if (major == du->major && minor == du->minor)
Jens Axboee11c4102007-04-03 16:33:04 -0700127 return du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200128 }
129
Jens Axboee11c4102007-04-03 16:33:04 -0700130 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200131}
132
Shehjar Tikoo67423012008-09-30 10:35:44 +1000133static int get_device_numbers(char *file_name, int *maj, int *min)
134{
135 struct stat st;
136 int majdev, mindev;
137 char tempname[PATH_MAX], *p;
138
139 if (!lstat(file_name, &st)) {
140 if (S_ISBLK(st.st_mode)) {
141 majdev = major(st.st_rdev);
142 mindev = minor(st.st_rdev);
143 } else if (S_ISCHR(st.st_mode)) {
144 majdev = major(st.st_rdev);
145 mindev = minor(st.st_rdev);
146 if (fio_lookup_raw(st.st_rdev, &majdev, &mindev))
147 return -1;
148 } else if (S_ISFIFO(st.st_mode))
149 return -1;
150 else {
151 majdev = major(st.st_dev);
152 mindev = minor(st.st_dev);
153 }
154 } else {
155 /*
156 * must be a file, open "." in that path
157 */
158 strncpy(tempname, file_name, PATH_MAX - 1);
159 p = dirname(tempname);
160 if (stat(p, &st)) {
161 perror("disk util stat");
162 return -1;
163 }
164
165 majdev = major(st.st_dev);
166 mindev = minor(st.st_dev);
167 }
168
169 *min = mindev;
170 *maj = majdev;
171
172 return 0;
173}
174
175static int read_block_dev_entry(char *path, int *maj, int *min)
176{
177 char line[256], *p;
178 FILE *f;
179
180 f = fopen(path, "r");
181 if (!f) {
182 perror("open path");
183 return 1;
184 }
185
186 p = fgets(line, sizeof(line), f);
187 fclose(f);
188
189 if (!p)
190 return 1;
191
192 if (sscanf(p, "%u:%u", maj, min) != 2)
193 return 1;
194
195 return 0;
196}
197
Shehjar Tikoo67423012008-09-30 10:35:44 +1000198static void find_add_disk_slaves(struct thread_data *td, char *path,
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200199 struct disk_util *masterdu)
Shehjar Tikoo67423012008-09-30 10:35:44 +1000200{
201 DIR *dirhandle = NULL;
202 struct dirent *dirent = NULL;
203 char slavesdir[PATH_MAX], temppath[PATH_MAX], slavepath[PATH_MAX];
204 struct disk_util *slavedu = NULL;
205 int majdev, mindev;
206 ssize_t linklen;
207
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200208 sprintf(slavesdir, "%s/%s", path, "slaves");
Shehjar Tikoo67423012008-09-30 10:35:44 +1000209 dirhandle = opendir(slavesdir);
210 if (!dirhandle)
211 return;
212
213 while ((dirent = readdir(dirhandle)) != NULL) {
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200214 if (!strcmp(dirent->d_name, ".") ||
215 !strcmp(dirent->d_name, ".."))
Shehjar Tikoo67423012008-09-30 10:35:44 +1000216 continue;
217
218 sprintf(temppath, "%s/%s", slavesdir, dirent->d_name);
219 /* Can we always assume that the slaves device entries
220 * are links to the real directories for the slave
221 * devices?
222 */
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200223 linklen = readlink(temppath, slavepath, PATH_MAX - 0);
224 if (linklen < 0) {
Shehjar Tikoo67423012008-09-30 10:35:44 +1000225 perror("readlink() for slave device.");
226 return;
227 }
Jens Axboe2db4ecc2009-09-11 22:22:41 +0200228 slavepath[linklen] = '\0';
Shehjar Tikoo67423012008-09-30 10:35:44 +1000229
230 sprintf(temppath, "%s/%s/dev", slavesdir, slavepath);
231 if (read_block_dev_entry(temppath, &majdev, &mindev)) {
232 perror("Error getting slave device numbers.");
233 return;
234 }
235
Alan D. Brunelleb06de822009-09-11 14:57:10 -0400236 /*
237 * See if this maj,min already exists
238 */
239 slavedu = disk_util_exists(majdev, mindev);
240 if (slavedu)
241 continue;
242
Shehjar Tikoo67423012008-09-30 10:35:44 +1000243 sprintf(temppath, "%s/%s", slavesdir, slavepath);
244 __init_per_file_disk_util(td, majdev, mindev, temppath);
245 slavedu = disk_util_exists(majdev, mindev);
246
247 /* Should probably use an assert here. slavedu should
248 * always be present at this point. */
Jens Axboeb8d541d2010-06-21 12:40:53 +0200249 if (slavedu) {
250 slavedu->users++;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000251 flist_add_tail(&slavedu->slavelist, &masterdu->slaves);
Jens Axboeb8d541d2010-06-21 12:40:53 +0200252 }
Shehjar Tikoo67423012008-09-30 10:35:44 +1000253 }
254
255 closedir(dirhandle);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000256}
257
Jens Axboeb8fefc22011-09-08 13:08:05 +0200258static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
Jens Axboec97bd0f2009-05-27 13:11:20 +0200259 int mindev, char *path)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200260{
261 struct disk_util *du, *__du;
Jens Axboe01743ee2008-06-02 12:19:19 +0200262 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200263
Jens Axboecd991b92008-03-07 13:19:35 +0100264 dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
265
Jens Axboec97bd0f2009-05-27 13:11:20 +0200266 du = smalloc(sizeof(*du));
Jens Axboe9f8f2062007-04-02 15:08:01 +0200267 memset(du, 0, sizeof(*du));
Jens Axboe01743ee2008-06-02 12:19:19 +0200268 INIT_FLIST_HEAD(&du->list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200269 sprintf(du->path, "%s/stat", path);
Jens Axboed09a64a2011-10-13 11:38:56 +0200270 strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ);
Jens Axboee11c4102007-04-03 16:33:04 -0700271 du->sysfs_root = path;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200272 du->major = majdev;
273 du->minor = mindev;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000274 INIT_FLIST_HEAD(&du->slavelist);
275 INIT_FLIST_HEAD(&du->slaves);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200276 du->lock = fio_mutex_init(1);
277 du->users = 0;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200278
Jens Axboe01743ee2008-06-02 12:19:19 +0200279 flist_for_each(entry, &disk_list) {
280 __du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200281
Jens Axboed09a64a2011-10-13 11:38:56 +0200282 dprint(FD_DISKUTIL, "found %s in list\n", __du->dus.name);
Jens Axboecd991b92008-03-07 13:19:35 +0100283
Jens Axboed09a64a2011-10-13 11:38:56 +0200284 if (!strcmp((char *) du->dus.name, (char *) __du->dus.name)) {
Jens Axboec97bd0f2009-05-27 13:11:20 +0200285 disk_util_free(du);
286 return __du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200287 }
288 }
289
Jens Axboed09a64a2011-10-13 11:38:56 +0200290 dprint(FD_DISKUTIL, "add %s to list\n", du->dus.name);
Jens Axboecd991b92008-03-07 13:19:35 +0100291
Jens Axboe9f8f2062007-04-02 15:08:01 +0200292 fio_gettime(&du->time, NULL);
293 get_io_ticks(du, &du->last_dus);
294
Jens Axboe01743ee2008-06-02 12:19:19 +0200295 flist_add_tail(&du->list, &disk_list);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000296 find_add_disk_slaves(td, path, du);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200297 return du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200298}
299
300static int check_dev_match(int majdev, int mindev, char *path)
301{
302 int major, minor;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200303
Shehjar Tikoo67423012008-09-30 10:35:44 +1000304 if (read_block_dev_entry(path, &major, &minor))
Jens Axboe9f8f2062007-04-02 15:08:01 +0200305 return 1;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200306
Jens Axboec97bd0f2009-05-27 13:11:20 +0200307 if (majdev == major && mindev == minor)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200308 return 0;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200309
Jens Axboe9f8f2062007-04-02 15:08:01 +0200310 return 1;
311}
312
Jens Axboeac0be2a2008-06-06 12:08:04 +0200313static int find_block_dir(int majdev, int mindev, char *path, int link_ok)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200314{
315 struct dirent *dir;
316 struct stat st;
317 int found = 0;
318 DIR *D;
319
320 D = opendir(path);
321 if (!D)
322 return 0;
323
324 while ((dir = readdir(D)) != NULL) {
325 char full_path[256];
326
327 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
328 continue;
329
330 sprintf(full_path, "%s/%s", path, dir->d_name);
331
332 if (!strcmp(dir->d_name, "dev")) {
333 if (!check_dev_match(majdev, mindev, full_path)) {
334 found = 1;
335 break;
336 }
337 }
338
Jens Axboeac0be2a2008-06-06 12:08:04 +0200339 if (link_ok) {
340 if (stat(full_path, &st) == -1) {
341 perror("stat");
342 break;
343 }
344 } else {
345 if (lstat(full_path, &st) == -1) {
346 perror("stat");
347 break;
348 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200349 }
350
351 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
352 continue;
353
Jens Axboeac0be2a2008-06-06 12:08:04 +0200354 found = find_block_dir(majdev, mindev, full_path, 0);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200355 if (found) {
356 strcpy(path, full_path);
357 break;
358 }
359 }
360
361 closedir(D);
362 return found;
363}
364
Jens Axboec97bd0f2009-05-27 13:11:20 +0200365static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
366 int majdev, int mindev,
367 char *path)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200368{
369 struct stat st;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000370 char tmp[PATH_MAX];
Jens Axboe9f8f2062007-04-02 15:08:01 +0200371 char *p;
372
Shehjar Tikoo67423012008-09-30 10:35:44 +1000373 /*
374 * If there's a ../queue/ directory there, we are inside a partition.
375 * Check if that is the case and jump back. For loop/md/dm etc we
376 * are already in the right spot.
377 */
378 sprintf(tmp, "%s/../queue", path);
379 if (!stat(tmp, &st)) {
380 p = dirname(path);
381 sprintf(tmp, "%s/queue", p);
382 if (stat(tmp, &st)) {
383 log_err("unknown sysfs layout\n");
Jens Axboec97bd0f2009-05-27 13:11:20 +0200384 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200385 }
Shehjar Tikoo67423012008-09-30 10:35:44 +1000386 strncpy(tmp, p, PATH_MAX - 1);
387 sprintf(path, "%s", tmp);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200388 }
389
Shehjar Tikoo67423012008-09-30 10:35:44 +1000390 if (td->o.ioscheduler && !td->sysfs_root)
391 td->sysfs_root = strdup(path);
392
Jens Axboec97bd0f2009-05-27 13:11:20 +0200393 return disk_util_add(td, majdev, mindev, path);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000394}
395
Jens Axboec97bd0f2009-05-27 13:11:20 +0200396static struct disk_util *init_per_file_disk_util(struct thread_data *td,
397 char *filename)
Shehjar Tikoo67423012008-09-30 10:35:44 +1000398{
399
400 char foo[PATH_MAX];
401 struct disk_util *du;
402 int mindev, majdev;
403
Jens Axboec97bd0f2009-05-27 13:11:20 +0200404 if (get_device_numbers(filename, &majdev, &mindev))
405 return NULL;
Shehjar Tikoo67423012008-09-30 10:35:44 +1000406
407 dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", filename, majdev,
408 mindev);
Jens Axboecd991b92008-03-07 13:19:35 +0100409
Jens Axboee11c4102007-04-03 16:33:04 -0700410 du = disk_util_exists(majdev, mindev);
411 if (du) {
412 if (td->o.ioscheduler && !td->sysfs_root)
413 td->sysfs_root = strdup(du->sysfs_root);
414
Jens Axboec97bd0f2009-05-27 13:11:20 +0200415 return du;
Jens Axboee11c4102007-04-03 16:33:04 -0700416 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200417
418 /*
419 * for an fs without a device, we will repeatedly stat through
420 * sysfs which can take oodles of time for thousands of files. so
421 * cache the last lookup and compare with that before going through
422 * everything again.
423 */
424 if (mindev == last_mindev && majdev == last_majdev)
Jens Axboec97bd0f2009-05-27 13:11:20 +0200425 return last_du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200426
427 last_mindev = mindev;
428 last_majdev = majdev;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100429
Jens Axboe9f8f2062007-04-02 15:08:01 +0200430 sprintf(foo, "/sys/block");
Jens Axboeac0be2a2008-06-06 12:08:04 +0200431 if (!find_block_dir(majdev, mindev, foo, 1))
Jens Axboec97bd0f2009-05-27 13:11:20 +0200432 return NULL;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200433
Jens Axboec97bd0f2009-05-27 13:11:20 +0200434 return __init_per_file_disk_util(td, majdev, mindev, foo);
Shehjar Tikoo67423012008-09-30 10:35:44 +1000435}
Jens Axboe9f8f2062007-04-02 15:08:01 +0200436
Jens Axboec97bd0f2009-05-27 13:11:20 +0200437static struct disk_util *__init_disk_util(struct thread_data *td,
438 struct fio_file *f)
Shehjar Tikoo67423012008-09-30 10:35:44 +1000439{
440 return init_per_file_disk_util(td, f->file_name);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200441}
442
443void init_disk_util(struct thread_data *td)
444{
445 struct fio_file *f;
446 unsigned int i;
447
448 if (!td->o.do_disk_util ||
449 (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
450 return;
451
452 for_each_file(td, f, i)
Jens Axboec97bd0f2009-05-27 13:11:20 +0200453 f->du = __init_disk_util(td, f);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200454}
455
Jens Axboef2f788d2011-10-13 14:03:52 +0200456static void show_agg_stats(struct disk_util_agg *agg, int terse)
Jens Axboed09a64a2011-10-13 11:38:56 +0200457{
458 if (!agg->slavecount)
459 return;
460
Jens Axboef2f788d2011-10-13 14:03:52 +0200461 if (!terse) {
462 log_info(", aggrios=%u/%u, aggrmerge=%u/%u, aggrticks=%u/%u,"
463 " aggrin_queue=%u, aggrutil=%3.2f%%",
464 agg->ios[0] / agg->slavecount,
465 agg->ios[1] / agg->slavecount,
466 agg->merges[0] / agg->slavecount,
467 agg->merges[1] / agg->slavecount,
468 agg->ticks[0] / agg->slavecount,
469 agg->ticks[1] / agg->slavecount,
470 agg->time_in_queue / agg->slavecount,
471 agg->max_util.u.f);
472 } else {
473 log_info("slaves;%u;%u;%u;%u;%u;%u;%u;%3.2f%%",
474 agg->ios[0] / agg->slavecount,
475 agg->ios[1] / agg->slavecount,
476 agg->merges[0] / agg->slavecount,
477 agg->merges[1] / agg->slavecount,
478 agg->ticks[0] / agg->slavecount,
479 agg->ticks[1] / agg->slavecount,
480 agg->time_in_queue / agg->slavecount,
481 agg->max_util.u.f);
482 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200483}
484
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000485static void aggregate_slaves_stats(struct disk_util *masterdu)
486{
Jens Axboed09a64a2011-10-13 11:38:56 +0200487 struct disk_util_agg *agg = &masterdu->agg;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000488 struct disk_util_stat *dus;
489 struct flist_head *entry;
490 struct disk_util *slavedu;
Jens Axboed09a64a2011-10-13 11:38:56 +0200491 double util;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000492
493 flist_for_each(entry, &masterdu->slaves) {
494 slavedu = flist_entry(entry, struct disk_util, slavelist);
495 dus = &slavedu->dus;
Jens Axboed09a64a2011-10-13 11:38:56 +0200496 agg->ios[0] += dus->ios[0];
497 agg->ios[1] += dus->ios[1];
498 agg->merges[0] += dus->merges[0];
499 agg->merges[1] += dus->merges[1];
500 agg->sectors[0] += dus->sectors[0];
501 agg->sectors[1] += dus->sectors[1];
502 agg->ticks[0] += dus->ticks[0];
503 agg->ticks[1] += dus->ticks[1];
504 agg->time_in_queue += dus->time_in_queue;
505 agg->slavecount++;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000506
Jens Axboed09a64a2011-10-13 11:38:56 +0200507 util = (double) (100 * dus->io_ticks / (double) slavedu->dus.msec);
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000508 /* System utilization is the utilization of the
509 * component with the highest utilization.
510 */
Jens Axboed09a64a2011-10-13 11:38:56 +0200511 if (util > agg->max_util.u.f)
512 agg->max_util.u.f = util;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000513
514 }
515
Jens Axboed09a64a2011-10-13 11:38:56 +0200516 if (agg->max_util.u.f > 100.0)
517 agg->max_util.u.f = 100.0;
518}
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000519
Jens Axboed09a64a2011-10-13 11:38:56 +0200520void free_disk_util(void)
521{
522 struct disk_util *du;
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000523
Jens Axboed09a64a2011-10-13 11:38:56 +0200524 while (!flist_empty(&disk_list)) {
525 du = flist_entry(disk_list.next, struct disk_util, list);
526 flist_del(&du->list);
527 disk_util_free(du);
528 }
529
530 last_majdev = last_mindev = -1;
531}
532
Jens Axboef2f788d2011-10-13 14:03:52 +0200533void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
534 int terse)
Jens Axboed09a64a2011-10-13 11:38:56 +0200535{
536 double util;
537
538 util = (double) 100 * dus->io_ticks / (double) dus->msec;
539 if (util > 100.0)
540 util = 100.0;
541
542 if (agg->slavecount)
543 log_info(" ");
544
Jens Axboef2f788d2011-10-13 14:03:52 +0200545 if (!terse) {
546 log_info(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, "
547 "in_queue=%u, util=%3.2f%%", dus->name,
Jens Axboed09a64a2011-10-13 11:38:56 +0200548 dus->ios[0], dus->ios[1],
549 dus->merges[0], dus->merges[1],
550 dus->ticks[0], dus->ticks[1],
551 dus->time_in_queue, util);
Jens Axboef2f788d2011-10-13 14:03:52 +0200552 } else {
553 log_info(";%s;%u;%u;%u;%u;%lu;%lu;%u;%u;%u;%u;%3.2f%%",
554 dus->name, dus->ios[0], dus->ios[1],
555 dus->merges[0], dus->merges[1],
556 dus->ticks[0], dus->ticks[1],
557 dus->time_in_queue, util);
558 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200559
560 /*
561 * If the device has slaves, aggregate the stats for
562 * those slave devices also.
563 */
564 if (agg->slavecount)
Jens Axboef2f788d2011-10-13 14:03:52 +0200565 show_agg_stats(agg, terse);
Jens Axboed09a64a2011-10-13 11:38:56 +0200566
Jens Axboef2f788d2011-10-13 14:03:52 +0200567 if (!terse)
568 log_info("\n");
Shehjar Tikoo043b74b2008-09-30 11:11:35 +1000569}
570
Jens Axboef2f788d2011-10-13 14:03:52 +0200571void show_disk_util(int terse)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200572{
Jens Axboed09a64a2011-10-13 11:38:56 +0200573 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200574 struct disk_util *du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200575
Jens Axboe01743ee2008-06-02 12:19:19 +0200576 if (flist_empty(&disk_list))
Jens Axboe0a839f32007-04-26 09:02:34 +0200577 return;
578
Jens Axboef2f788d2011-10-13 14:03:52 +0200579 if (!terse)
580 log_info("\nDisk stats (read/write):\n");
Jens Axboe9f8f2062007-04-02 15:08:01 +0200581
Jens Axboe01743ee2008-06-02 12:19:19 +0200582 flist_for_each(entry, &disk_list) {
583 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200584
Jens Axboed09a64a2011-10-13 11:38:56 +0200585 aggregate_slaves_stats(du);
Jens Axboef2f788d2011-10-13 14:03:52 +0200586 print_disk_util(&du->dus, &du->agg, terse);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200587 }
588}