blob: 9fb31bf2be44f6c6e9f997e204a10fb6b7574aa1 [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
105static void disk_util_add(int majdev, int mindev, char *path)
106{
107 struct disk_util *du, *__du;
Jens Axboe01743ee2008-06-02 12:19:19 +0200108 struct flist_head *entry;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200109
Jens Axboecd991b92008-03-07 13:19:35 +0100110 dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
111
Jens Axboe9f8f2062007-04-02 15:08:01 +0200112 du = malloc(sizeof(*du));
113 memset(du, 0, sizeof(*du));
Jens Axboe01743ee2008-06-02 12:19:19 +0200114 INIT_FLIST_HEAD(&du->list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200115 sprintf(du->path, "%s/stat", path);
116 du->name = strdup(basename(path));
Jens Axboee11c4102007-04-03 16:33:04 -0700117 du->sysfs_root = path;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200118 du->major = majdev;
119 du->minor = mindev;
120
Jens Axboe01743ee2008-06-02 12:19:19 +0200121 flist_for_each(entry, &disk_list) {
122 __du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200123
Jens Axboecd991b92008-03-07 13:19:35 +0100124 dprint(FD_DISKUTIL, "found %s in list\n", __du->name);
125
Jens Axboe9f8f2062007-04-02 15:08:01 +0200126 if (!strcmp(du->name, __du->name)) {
127 free(du->name);
128 free(du);
129 return;
130 }
131 }
132
Jens Axboecd991b92008-03-07 13:19:35 +0100133 dprint(FD_DISKUTIL, "add %s to list\n", du->name);
134
Jens Axboe9f8f2062007-04-02 15:08:01 +0200135 fio_gettime(&du->time, NULL);
136 get_io_ticks(du, &du->last_dus);
137
Jens Axboe01743ee2008-06-02 12:19:19 +0200138 flist_add_tail(&du->list, &disk_list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200139}
140
141static int check_dev_match(int majdev, int mindev, char *path)
142{
143 int major, minor;
144 char line[256], *p;
145 FILE *f;
146
147 f = fopen(path, "r");
148 if (!f) {
149 perror("open path");
150 return 1;
151 }
152
153 p = fgets(line, sizeof(line), f);
154 if (!p) {
155 fclose(f);
156 return 1;
157 }
158
159 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
160 fclose(f);
161 return 1;
162 }
163
164 if (majdev == major && mindev == minor) {
165 fclose(f);
166 return 0;
167 }
168
169 fclose(f);
170 return 1;
171}
172
Jens Axboeac0be2a2008-06-06 12:08:04 +0200173static int find_block_dir(int majdev, int mindev, char *path, int link_ok)
Jens Axboe9f8f2062007-04-02 15:08:01 +0200174{
175 struct dirent *dir;
176 struct stat st;
177 int found = 0;
178 DIR *D;
179
180 D = opendir(path);
181 if (!D)
182 return 0;
183
184 while ((dir = readdir(D)) != NULL) {
185 char full_path[256];
186
187 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
188 continue;
189
190 sprintf(full_path, "%s/%s", path, dir->d_name);
191
192 if (!strcmp(dir->d_name, "dev")) {
193 if (!check_dev_match(majdev, mindev, full_path)) {
194 found = 1;
195 break;
196 }
197 }
198
Jens Axboeac0be2a2008-06-06 12:08:04 +0200199 if (link_ok) {
200 if (stat(full_path, &st) == -1) {
201 perror("stat");
202 break;
203 }
204 } else {
205 if (lstat(full_path, &st) == -1) {
206 perror("stat");
207 break;
208 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200209 }
210
211 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
212 continue;
213
Jens Axboeac0be2a2008-06-06 12:08:04 +0200214 found = find_block_dir(majdev, mindev, full_path, 0);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200215 if (found) {
216 strcpy(path, full_path);
217 break;
218 }
219 }
220
221 closedir(D);
222 return found;
223}
224
225static void __init_disk_util(struct thread_data *td, struct fio_file *f)
226{
227 struct stat st;
228 char foo[PATH_MAX], tmp[PATH_MAX];
Jens Axboee11c4102007-04-03 16:33:04 -0700229 struct disk_util *du;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200230 int mindev, majdev;
231 char *p;
232
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200233 if (!lstat(f->file_name, &st)) {
Jens Axboe9f8f2062007-04-02 15:08:01 +0200234 if (S_ISBLK(st.st_mode)) {
235 majdev = major(st.st_rdev);
236 mindev = minor(st.st_rdev);
237 } else if (S_ISCHR(st.st_mode)) {
238 majdev = major(st.st_rdev);
239 mindev = minor(st.st_rdev);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200240 if (fio_lookup_raw(st.st_rdev, &majdev, &mindev))
241 return;
242 } else if (S_ISFIFO(st.st_mode))
243 return;
244 else {
Jens Axboe9f8f2062007-04-02 15:08:01 +0200245 majdev = major(st.st_dev);
246 mindev = minor(st.st_dev);
247 }
248 } else {
249 /*
250 * must be a file, open "." in that path
251 */
252 strncpy(foo, f->file_name, PATH_MAX - 1);
253 p = dirname(foo);
254 if (stat(p, &st)) {
255 perror("disk util stat");
256 return;
257 }
258
259 majdev = major(st.st_dev);
260 mindev = minor(st.st_dev);
261 }
262
Jens Axboecd991b92008-03-07 13:19:35 +0100263 dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", f->file_name,
264 majdev, mindev);
265
Jens Axboee11c4102007-04-03 16:33:04 -0700266 du = disk_util_exists(majdev, mindev);
267 if (du) {
268 if (td->o.ioscheduler && !td->sysfs_root)
269 td->sysfs_root = strdup(du->sysfs_root);
270
Jens Axboe9f8f2062007-04-02 15:08:01 +0200271 return;
Jens Axboee11c4102007-04-03 16:33:04 -0700272 }
Jens Axboe9f8f2062007-04-02 15:08:01 +0200273
274 /*
275 * for an fs without a device, we will repeatedly stat through
276 * sysfs which can take oodles of time for thousands of files. so
277 * cache the last lookup and compare with that before going through
278 * everything again.
279 */
280 if (mindev == last_mindev && majdev == last_majdev)
281 return;
282
283 last_mindev = mindev;
284 last_majdev = majdev;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100285
Jens Axboe9f8f2062007-04-02 15:08:01 +0200286 sprintf(foo, "/sys/block");
Jens Axboeac0be2a2008-06-06 12:08:04 +0200287 if (!find_block_dir(majdev, mindev, foo, 1))
Jens Axboe9f8f2062007-04-02 15:08:01 +0200288 return;
289
290 /*
291 * If there's a ../queue/ directory there, we are inside a partition.
292 * Check if that is the case and jump back. For loop/md/dm etc we
293 * are already in the right spot.
294 */
295 sprintf(tmp, "%s/../queue", foo);
296 if (!stat(tmp, &st)) {
297 p = dirname(foo);
298 sprintf(tmp, "%s/queue", p);
299 if (stat(tmp, &st)) {
300 log_err("unknown sysfs layout\n");
301 return;
302 }
303 strncpy(tmp, p, PATH_MAX - 1);
304 sprintf(foo, "%s", tmp);
305 }
306
307 if (td->o.ioscheduler && !td->sysfs_root)
308 td->sysfs_root = strdup(foo);
309
310 disk_util_add(majdev, mindev, foo);
311}
312
313void init_disk_util(struct thread_data *td)
314{
315 struct fio_file *f;
316 unsigned int i;
317
318 if (!td->o.do_disk_util ||
319 (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
320 return;
321
322 for_each_file(td, f, i)
323 __init_disk_util(td, f);
324}
325
Jens Axboe9f8f2062007-04-02 15:08:01 +0200326void show_disk_util(void)
327{
328 struct disk_util_stat *dus;
Jens Axboe01743ee2008-06-02 12:19:19 +0200329 struct flist_head *entry, *next;
Jens Axboe9f8f2062007-04-02 15:08:01 +0200330 struct disk_util *du;
331 double util;
332
Jens Axboe01743ee2008-06-02 12:19:19 +0200333 if (flist_empty(&disk_list))
Jens Axboe0a839f32007-04-26 09:02:34 +0200334 return;
335
Jens Axboe9f8f2062007-04-02 15:08:01 +0200336 log_info("\nDisk stats (read/write):\n");
337
Jens Axboe01743ee2008-06-02 12:19:19 +0200338 flist_for_each(entry, &disk_list) {
339 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200340 dus = &du->dus;
341
342 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
343 if (util > 100.0)
344 util = 100.0;
345
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100346 log_info(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, "
347 "in_queue=%u, util=%3.2f%%\n", du->name,
348 dus->ios[0], dus->ios[1],
349 dus->merges[0], dus->merges[1],
350 dus->ticks[0], dus->ticks[1],
351 dus->time_in_queue, util);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200352 }
353
354 /*
355 * now free the list
356 */
Jens Axboe01743ee2008-06-02 12:19:19 +0200357 flist_for_each_safe(entry, next, &disk_list) {
358 flist_del(entry);
359 du = flist_entry(entry, struct disk_util, list);
Jens Axboe9f8f2062007-04-02 15:08:01 +0200360 free(du->name);
361 free(du);
362 }
363}