splice: update to new vmsplice-to-user interface
[fio.git] / diskutil.c
1 #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
12 static int last_majdev, last_mindev;
13 static struct itimerval itimer;
14
15 static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
16
17 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
18 {
19         unsigned in_flight;
20         char line[256];
21         FILE *f;
22         char *p;
23
24         f = fopen(du->path, "r");
25         if (!f)
26                 return 1;
27
28         p = fgets(line, sizeof(line), f);
29         if (!p) {
30                 fclose(f);
31                 return 1;
32         }
33
34         if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) {
35                 fclose(f);
36                 return 1;
37         }
38
39         fclose(f);
40         return 0;
41 }
42
43 static void update_io_tick_disk(struct disk_util *du)
44 {
45         struct disk_util_stat __dus, *dus, *ldus;
46         struct timeval t;
47
48         if (get_io_ticks(du, &__dus))
49                 return;
50
51         dus = &du->dus;
52         ldus = &du->last_dus;
53
54         dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
55         dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
56         dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
57         dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
58         dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
59         dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
60         dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
61         dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
62         dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
63         dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
64
65         fio_gettime(&t, NULL);
66         du->msec += mtime_since(&du->time, &t);
67         memcpy(&du->time, &t, sizeof(t));
68         memcpy(ldus, &__dus, sizeof(__dus));
69 }
70
71 void update_io_ticks(void)
72 {
73         struct list_head *entry;
74         struct disk_util *du;
75
76         list_for_each(entry, &disk_list) {
77                 du = list_entry(entry, struct disk_util, list);
78                 update_io_tick_disk(du);
79         }
80 }
81
82 static struct disk_util *disk_util_exists(int major, int minor)
83 {
84         struct list_head *entry;
85         struct disk_util *du;
86
87         list_for_each(entry, &disk_list) {
88                 du = list_entry(entry, struct disk_util, list);
89
90                 if (major == du->major && minor == du->minor)
91                         return du;
92         }
93
94         return NULL;
95 }
96
97 static void disk_util_add(int majdev, int mindev, char *path)
98 {
99         struct disk_util *du, *__du;
100         struct list_head *entry;
101
102         du = malloc(sizeof(*du));
103         memset(du, 0, sizeof(*du));
104         INIT_LIST_HEAD(&du->list);
105         sprintf(du->path, "%s/stat", path);
106         du->name = strdup(basename(path));
107         du->sysfs_root = path;
108         du->major = majdev;
109         du->minor = mindev;
110
111         list_for_each(entry, &disk_list) {
112                 __du = list_entry(entry, struct disk_util, list);
113
114                 if (!strcmp(du->name, __du->name)) {
115                         free(du->name);
116                         free(du);
117                         return;
118                 }
119         }
120
121         fio_gettime(&du->time, NULL);
122         get_io_ticks(du, &du->last_dus);
123
124         list_add_tail(&du->list, &disk_list);
125 }
126
127 static int check_dev_match(int majdev, int mindev, char *path)
128 {
129         int major, minor;
130         char line[256], *p;
131         FILE *f;
132
133         f = fopen(path, "r");
134         if (!f) {
135                 perror("open path");
136                 return 1;
137         }
138
139         p = fgets(line, sizeof(line), f);
140         if (!p) {
141                 fclose(f);
142                 return 1;
143         }
144
145         if (sscanf(p, "%u:%u", &major, &minor) != 2) {
146                 fclose(f);
147                 return 1;
148         }
149
150         if (majdev == major && mindev == minor) {
151                 fclose(f);
152                 return 0;
153         }
154
155         fclose(f);
156         return 1;
157 }
158
159 static int find_block_dir(int majdev, int mindev, char *path)
160 {
161         struct dirent *dir;
162         struct stat st;
163         int found = 0;
164         DIR *D;
165
166         D = opendir(path);
167         if (!D)
168                 return 0;
169
170         while ((dir = readdir(D)) != NULL) {
171                 char full_path[256];
172
173                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
174                         continue;
175
176                 sprintf(full_path, "%s/%s", path, dir->d_name);
177
178                 if (!strcmp(dir->d_name, "dev")) {
179                         if (!check_dev_match(majdev, mindev, full_path)) {
180                                 found = 1;
181                                 break;
182                         }
183                 }
184
185                 if (lstat(full_path, &st) == -1) {
186                         perror("stat");
187                         break;
188                 }
189
190                 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
191                         continue;
192
193                 found = find_block_dir(majdev, mindev, full_path);
194                 if (found) {
195                         strcpy(path, full_path);
196                         break;
197                 }
198         }
199
200         closedir(D);
201         return found;
202 }
203
204 static void __init_disk_util(struct thread_data *td, struct fio_file *f)
205 {
206         struct stat st;
207         char foo[PATH_MAX], tmp[PATH_MAX];
208         struct disk_util *du;
209         int mindev, majdev;
210         char *p;
211
212         if (!lstat(f->file_name, &st)) {
213                 if (S_ISBLK(st.st_mode)) {
214                         majdev = major(st.st_rdev);
215                         mindev = minor(st.st_rdev);
216                 } else if (S_ISCHR(st.st_mode)) {
217                         majdev = major(st.st_rdev);
218                         mindev = minor(st.st_rdev);
219                         if (fio_lookup_raw(st.st_rdev, &majdev, &mindev))
220                                 return;
221                 } else if (S_ISFIFO(st.st_mode))
222                         return;
223                 else {
224                         majdev = major(st.st_dev);
225                         mindev = minor(st.st_dev);
226                 }
227         } else {
228                 /*
229                  * must be a file, open "." in that path
230                  */
231                 strncpy(foo, f->file_name, PATH_MAX - 1);
232                 p = dirname(foo);
233                 if (stat(p, &st)) {
234                         perror("disk util stat");
235                         return;
236                 }
237
238                 majdev = major(st.st_dev);
239                 mindev = minor(st.st_dev);
240         }
241
242         du = disk_util_exists(majdev, mindev);
243         if (du) {
244                 if (td->o.ioscheduler && !td->sysfs_root)
245                         td->sysfs_root = strdup(du->sysfs_root);
246
247                 return;
248         }
249
250         /*
251          * for an fs without a device, we will repeatedly stat through
252          * sysfs which can take oodles of time for thousands of files. so
253          * cache the last lookup and compare with that before going through
254          * everything again.
255          */
256         if (mindev == last_mindev && majdev == last_majdev)
257                 return;
258
259         last_mindev = mindev;
260         last_majdev = majdev;
261                 
262         sprintf(foo, "/sys/block");
263         if (!find_block_dir(majdev, mindev, foo))
264                 return;
265
266         /*
267          * If there's a ../queue/ directory there, we are inside a partition.
268          * Check if that is the case and jump back. For loop/md/dm etc we
269          * are already in the right spot.
270          */
271         sprintf(tmp, "%s/../queue", foo);
272         if (!stat(tmp, &st)) {
273                 p = dirname(foo);
274                 sprintf(tmp, "%s/queue", p);
275                 if (stat(tmp, &st)) {
276                         log_err("unknown sysfs layout\n");
277                         return;
278                 }
279                 strncpy(tmp, p, PATH_MAX - 1);
280                 sprintf(foo, "%s", tmp);
281         }
282
283         if (td->o.ioscheduler && !td->sysfs_root)
284                 td->sysfs_root = strdup(foo);
285
286         disk_util_add(majdev, mindev, foo);
287 }
288
289 void init_disk_util(struct thread_data *td)
290 {
291         struct fio_file *f;
292         unsigned int i;
293
294         if (!td->o.do_disk_util ||
295             (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
296                 return;
297
298         for_each_file(td, f, i)
299                 __init_disk_util(td, f);
300 }
301
302 void disk_util_timer_arm(void)
303 {
304         itimer.it_value.tv_sec = 0;
305         itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
306         setitimer(ITIMER_REAL, &itimer, NULL);
307 }
308
309 void show_disk_util(void)
310 {
311         struct disk_util_stat *dus;
312         struct list_head *entry, *next;
313         struct disk_util *du;
314         double util;
315
316         if (list_empty(&disk_list))
317                 return;
318
319         log_info("\nDisk stats (read/write):\n");
320
321         list_for_each(entry, &disk_list) {
322                 du = list_entry(entry, struct disk_util, list);
323                 dus = &du->dus;
324
325                 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
326                 if (util > 100.0)
327                         util = 100.0;
328
329                 log_info("  %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util);
330         }
331
332         /*
333          * now free the list
334          */
335         list_for_each_safe(entry, next, &disk_list) {
336                 list_del(entry);
337                 du = list_entry(entry, struct disk_util, list);
338                 free(du->name);
339                 free(du);
340         }
341 }