]> arthur.barton.de Git - netdata.git/blob - src/proc_diskstats.c
fix typo in the installer help
[netdata.git] / src / proc_diskstats.c
1 #include "common.h"
2
3 #define RRD_TYPE_DISK "disk"
4
5 #define DISK_TYPE_PHYSICAL  1
6 #define DISK_TYPE_PARTITION 2
7 #define DISK_TYPE_CONTAINER 3
8
9 static struct disk {
10     char *disk;             // the name of the disk (sda, sdb, etc)
11     unsigned long major;
12     unsigned long minor;
13     int sector_size;
14     int type;
15
16     char *mount_point;
17
18     // disk options caching
19     int configured;
20     int do_io;
21     int do_ops;
22     int do_mops;
23     int do_iotime;
24     int do_qops;
25     int do_util;
26     int do_backlog;
27
28     struct disk *next;
29 } *disk_root = NULL;
30
31 static struct disk *get_disk(unsigned long major, unsigned long minor, char *disk) {
32     static char path_to_get_hw_sector_size[FILENAME_MAX + 1] = "";
33     static char path_to_get_hw_sector_size_partitions[FILENAME_MAX + 1] = "";
34     static char path_find_block_device[FILENAME_MAX + 1] = "";
35     static struct mountinfo *disk_mountinfo_root = NULL;
36
37     struct disk *d;
38
39     // search for it in our RAM list.
40     // this is sequential, but since we just walk through
41     // and the number of disks / partitions in a system
42     // should not be that many, it should be acceptable
43     for(d = disk_root; d ; d = d->next)
44         if(unlikely(d->major == major && d->minor == minor))
45             return d;
46
47     // not found
48     // create a new disk structure
49     d = (struct disk *)mallocz(sizeof(struct disk));
50
51     d->disk = strdupz(disk);
52     d->major = major;
53     d->minor = minor;
54     d->type = DISK_TYPE_PHYSICAL; // Default type. Changed later if not correct.
55     d->configured = 0;
56     d->sector_size = 512; // the default, will be changed below
57     d->next = NULL;
58
59     // append it to the list
60     if(unlikely(!disk_root))
61         disk_root = d;
62     else {
63         struct disk *last;
64         for(last = disk_root; last->next ;last = last->next);
65         last->next = d;
66     }
67
68     // ------------------------------------------------------------------------
69     // find the type of the device
70
71     char buffer[FILENAME_MAX + 1];
72
73     // get the default path for finding info about the block device
74     if(unlikely(!path_find_block_device[0])) {
75         snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/%s");
76         snprintfz(path_find_block_device, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get block device infos", buffer));
77     }
78
79     // find if it is a partition
80     // by checking if /sys/dev/block/MAJOR:MINOR/partition is readable.
81     snprintfz(buffer, FILENAME_MAX, path_find_block_device, major, minor, "partition");
82     if(likely(access(buffer, R_OK) == 0)) {
83         d->type = DISK_TYPE_PARTITION;
84     }
85     else {
86         // find if it is a container
87         // by checking if /sys/dev/block/MAJOR:MINOR/slaves has entries
88         snprintfz(buffer, FILENAME_MAX, path_find_block_device, major, minor, "slaves/");
89         DIR *dirp = opendir(buffer);
90         if(likely(dirp != NULL)) {
91             struct dirent *dp;
92             while( (dp = readdir(dirp)) ) {
93                 // . and .. are also files in empty folders.
94                 if(unlikely(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)) {
95                     continue;
96                 }
97
98                 d->type = DISK_TYPE_CONTAINER;
99
100                 // Stop the loop after we found one file.
101                 break;
102             }
103             if(unlikely(closedir(dirp) == -1))
104                 error("Unable to close dir %s", buffer);
105         }
106     }
107
108     // ------------------------------------------------------------------------
109     // check if we can find its mount point
110
111     // mountinfo_find() can be called with NULL disk_mountinfo_root
112     struct mountinfo *mi = mountinfo_find(disk_mountinfo_root, d->major, d->minor);
113     if(unlikely(!mi)) {
114         // mountinfo_free can be called with NULL
115         mountinfo_free(disk_mountinfo_root);
116         disk_mountinfo_root = mountinfo_read(0);
117         mi = mountinfo_find(disk_mountinfo_root, d->major, d->minor);
118     }
119
120     if(unlikely(mi))
121         d->mount_point = strdupz(mi->mount_point);
122     else
123         d->mount_point = NULL;
124
125     // ------------------------------------------------------------------------
126     // find the disk sector size
127
128     if(unlikely(!path_to_get_hw_sector_size[0])) {
129         snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s/queue/hw_sector_size");
130         snprintfz(path_to_get_hw_sector_size, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get h/w sector size", buffer));
131     }
132     if(unlikely(!path_to_get_hw_sector_size_partitions[0])) {
133         snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/subsystem/%s/../queue/hw_sector_size");
134         snprintfz(path_to_get_hw_sector_size_partitions, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get h/w sector size for partitions", buffer));
135     }
136
137     {
138         char tf[FILENAME_MAX + 1], *t;
139         strncpyz(tf, d->disk, FILENAME_MAX);
140
141         // replace all / with !
142         for(t = tf; *t ;t++)
143             if(unlikely(*t == '/')) *t = '!';
144
145         if(likely(d->type == DISK_TYPE_PARTITION))
146             snprintfz(buffer, FILENAME_MAX, path_to_get_hw_sector_size_partitions, d->major, d->minor, tf);
147         else
148             snprintfz(buffer, FILENAME_MAX, path_to_get_hw_sector_size, tf);
149
150         FILE *fpss = fopen(buffer, "r");
151         if(likely(fpss)) {
152             char buffer2[1024 + 1];
153             char *tmp = fgets(buffer2, 1024, fpss);
154
155             if(likely(tmp)) {
156                 d->sector_size = str2i(tmp);
157                 if(unlikely(d->sector_size <= 0)) {
158                     error("Invalid sector size %d for device %s in %s. Assuming 512.", d->sector_size, d->disk, buffer);
159                     d->sector_size = 512;
160                 }
161             }
162             else error("Cannot read data for sector size for device %s from %s. Assuming 512.", d->disk, buffer);
163
164             fclose(fpss);
165         }
166         else error("Cannot read sector size for device %s from %s. Assuming 512.", d->disk, buffer);
167     }
168
169     return d;
170 }
171
172 static inline int is_major_enabled(int major) {
173     static int8_t *major_configs = NULL;
174     static size_t major_size = 0;
175
176     if(major < 0) return 1;
177
178     size_t wanted_size = (size_t)major + 1;
179
180     if(major_size < wanted_size) {
181         major_configs = reallocz(major_configs, wanted_size * sizeof(int8_t));
182
183         size_t i;
184         for(i = major_size; i < wanted_size ; i++)
185             major_configs[i] = -1;
186
187         major_size = wanted_size;
188     }
189
190     if(major_configs[major] == -1) {
191         char buffer[CONFIG_MAX_NAME + 1];
192         snprintfz(buffer, CONFIG_MAX_NAME, "performance metrics for disks with major %d", major);
193         major_configs[major] = (char)config_get_boolean("plugin:proc:/proc/diskstats", buffer, 1);
194     }
195
196     return (int)major_configs[major];
197 }
198
199 int do_proc_diskstats(int update_every, usec_t dt) {
200     static procfile *ff = NULL;
201     static int  global_enable_new_disks_detected_at_runtime = CONFIG_ONDEMAND_YES,
202                 global_enable_performance_for_physical_disks = CONFIG_ONDEMAND_ONDEMAND,
203                 global_enable_performance_for_virtual_disks = CONFIG_ONDEMAND_ONDEMAND,
204                 global_enable_performance_for_partitions = CONFIG_ONDEMAND_NO,
205                 global_do_io = CONFIG_ONDEMAND_ONDEMAND,
206                 global_do_ops = CONFIG_ONDEMAND_ONDEMAND,
207                 global_do_mops = CONFIG_ONDEMAND_ONDEMAND,
208                 global_do_iotime = CONFIG_ONDEMAND_ONDEMAND,
209                 global_do_qops = CONFIG_ONDEMAND_ONDEMAND,
210                 global_do_util = CONFIG_ONDEMAND_ONDEMAND,
211                 global_do_backlog = CONFIG_ONDEMAND_ONDEMAND,
212                 globals_initialized = 0;
213
214     if(unlikely(!globals_initialized)) {
215         global_enable_new_disks_detected_at_runtime = config_get_boolean("plugin:proc:/proc/diskstats", "enable new disks detected at runtime", global_enable_new_disks_detected_at_runtime);
216
217         global_enable_performance_for_physical_disks = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for physical disks", global_enable_performance_for_physical_disks);
218         global_enable_performance_for_virtual_disks = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for virtual disks", global_enable_performance_for_virtual_disks);
219         global_enable_performance_for_partitions = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for partitions", global_enable_performance_for_partitions);
220
221         global_do_io      = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "bandwidth for all disks", global_do_io);
222         global_do_ops     = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "operations for all disks", global_do_ops);
223         global_do_mops    = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "merged operations for all disks", global_do_mops);
224         global_do_iotime  = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "i/o time for all disks", global_do_iotime);
225         global_do_qops    = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "queued operations for all disks", global_do_qops);
226         global_do_util    = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "utilization percentage for all disks", global_do_util);
227         global_do_backlog = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "backlog for all disks", global_do_backlog);
228
229         globals_initialized = 1;
230     }
231
232     // --------------------------------------------------------------------------
233
234     if(unlikely(!ff)) {
235         char filename[FILENAME_MAX + 1];
236         snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/diskstats");
237         ff = procfile_open(config_get("plugin:proc:/proc/diskstats", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
238     }
239     if(unlikely(!ff)) return 0;
240
241     ff = procfile_readall(ff);
242     if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
243
244     size_t lines = procfile_lines(ff), l;
245
246     for(l = 0; l < lines ;l++) {
247         // --------------------------------------------------------------------------
248         // Read parameters
249
250         char *disk;
251         unsigned long       major = 0, minor = 0;
252
253         collected_number    reads = 0,  mreads = 0,  readsectors = 0,  readms = 0,
254                             writes = 0, mwrites = 0, writesectors = 0, writems = 0,
255                             queued_ios = 0, busy_ms = 0, backlog_ms = 0;
256
257         collected_number    last_reads = 0,  last_readsectors = 0,  last_readms = 0,
258                             last_writes = 0, last_writesectors = 0, last_writems = 0,
259                             last_busy_ms = 0;
260
261         size_t words = procfile_linewords(ff, l);
262         if(unlikely(words < 14)) continue;
263
264         major           = str2ul(procfile_lineword(ff, l, 0));
265         minor           = str2ul(procfile_lineword(ff, l, 1));
266         disk            = procfile_lineword(ff, l, 2);
267
268         // # of reads completed # of writes completed
269         // This is the total number of reads or writes completed successfully.
270         reads           = str2ull(procfile_lineword(ff, l, 3));  // rd_ios
271         writes          = str2ull(procfile_lineword(ff, l, 7));  // wr_ios
272
273         // # of reads merged # of writes merged
274         // Reads and writes which are adjacent to each other may be merged for
275         // efficiency.  Thus two 4K reads may become one 8K read before it is
276         // ultimately handed to the disk, and so it will be counted (and queued)
277         mreads          = str2ull(procfile_lineword(ff, l, 4));  // rd_merges_or_rd_sec
278         mwrites         = str2ull(procfile_lineword(ff, l, 8));  // wr_merges
279
280         // # of sectors read # of sectors written
281         // This is the total number of sectors read or written successfully.
282         readsectors     = str2ull(procfile_lineword(ff, l, 5));  // rd_sec_or_wr_ios
283         writesectors    = str2ull(procfile_lineword(ff, l, 9));  // wr_sec
284
285         // # of milliseconds spent reading # of milliseconds spent writing
286         // This is the total number of milliseconds spent by all reads or writes (as
287         // measured from __make_request() to end_that_request_last()).
288         readms          = str2ull(procfile_lineword(ff, l, 6));  // rd_ticks_or_wr_sec
289         writems         = str2ull(procfile_lineword(ff, l, 10)); // wr_ticks
290
291         // # of I/Os currently in progress
292         // The only field that should go to zero. Incremented as requests are
293         // given to appropriate struct request_queue and decremented as they finish.
294         queued_ios      = str2ull(procfile_lineword(ff, l, 11)); // ios_pgr
295
296         // # of milliseconds spent doing I/Os
297         // This field increases so long as field queued_ios is nonzero.
298         busy_ms         = str2ull(procfile_lineword(ff, l, 12)); // tot_ticks
299
300         // weighted # of milliseconds spent doing I/Os
301         // This field is incremented at each I/O start, I/O completion, I/O
302         // merge, or read of these stats by the number of I/Os in progress
303         // (field queued_ios) times the number of milliseconds spent doing I/O since the
304         // last update of this field.  This can provide an easy measure of both
305         // I/O completion time and the backlog that may be accumulating.
306         backlog_ms      = str2ull(procfile_lineword(ff, l, 13)); // rq_ticks
307
308
309         // --------------------------------------------------------------------------
310         // remove slashes from disk names
311         char *s;
312         for(s = disk; *s ;s++)
313             if(*s == '/') *s = '_';
314
315         // --------------------------------------------------------------------------
316         // get a disk structure for the disk
317
318         struct disk *d = get_disk(major, minor, disk);
319
320
321         // --------------------------------------------------------------------------
322         // Set its family based on mount point
323
324         char *family = d->mount_point;
325         if(!family) family = disk;
326
327
328         // --------------------------------------------------------------------------
329         // Check the configuration for the device
330
331         if(unlikely(!d->configured)) {
332             char var_name[4096 + 1];
333             snprintfz(var_name, 4096, "plugin:proc:/proc/diskstats:%s", disk);
334
335             int def_enable = config_get_boolean_ondemand(var_name, "enable", global_enable_new_disks_detected_at_runtime);
336             if(unlikely(def_enable == CONFIG_ONDEMAND_NO)) {
337                 // the user does not want any metrics for this disk
338                 d->do_io = CONFIG_ONDEMAND_NO;
339                 d->do_ops = CONFIG_ONDEMAND_NO;
340                 d->do_mops = CONFIG_ONDEMAND_NO;
341                 d->do_iotime = CONFIG_ONDEMAND_NO;
342                 d->do_qops = CONFIG_ONDEMAND_NO;
343                 d->do_util = CONFIG_ONDEMAND_NO;
344                 d->do_backlog = CONFIG_ONDEMAND_NO;
345             }
346             else {
347                 // this disk is enabled
348                 // check its direct settings
349
350                 int def_performance = CONFIG_ONDEMAND_ONDEMAND;
351
352                 // since this is 'on demand' we can figure the performance settings
353                 // based on the type of disk
354
355                 switch(d->type) {
356                     case DISK_TYPE_PHYSICAL:
357                         def_performance = global_enable_performance_for_physical_disks;
358                         break;
359
360                     case DISK_TYPE_PARTITION:
361                         def_performance = global_enable_performance_for_partitions;
362                         break;
363
364                     case DISK_TYPE_CONTAINER:
365                         def_performance = global_enable_performance_for_virtual_disks;
366                         break;
367                 }
368
369                 // check if we have to disable performance for this disk
370                 if(def_performance)
371                     def_performance = is_major_enabled((int)major);
372
373                 // ------------------------------------------------------------
374                 // now we have def_performance and def_space
375                 // to work further
376
377                 // def_performance
378                 // check the user configuration (this will also show our 'on demand' decision)
379                 def_performance = config_get_boolean_ondemand(var_name, "enable performance metrics", def_performance);
380
381                 int ddo_io = CONFIG_ONDEMAND_NO,
382                     ddo_ops = CONFIG_ONDEMAND_NO,
383                     ddo_mops = CONFIG_ONDEMAND_NO,
384                     ddo_iotime = CONFIG_ONDEMAND_NO,
385                     ddo_qops = CONFIG_ONDEMAND_NO,
386                     ddo_util = CONFIG_ONDEMAND_NO,
387                     ddo_backlog = CONFIG_ONDEMAND_NO;
388
389                 // we enable individual performance charts only when def_performance is not disabled
390                 if(unlikely(def_performance != CONFIG_ONDEMAND_NO)) {
391                     ddo_io = global_do_io,
392                     ddo_ops = global_do_ops,
393                     ddo_mops = global_do_mops,
394                     ddo_iotime = global_do_iotime,
395                     ddo_qops = global_do_qops,
396                     ddo_util = global_do_util,
397                     ddo_backlog = global_do_backlog;
398                 }
399
400                 d->do_io      = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
401                 d->do_ops     = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
402                 d->do_mops    = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
403                 d->do_iotime  = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
404                 d->do_qops    = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
405                 d->do_util    = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
406                 d->do_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
407             }
408
409             d->configured = 1;
410         }
411
412         RRDSET *st;
413
414         // --------------------------------------------------------------------------
415         // Do performance metrics
416
417         if(d->do_io == CONFIG_ONDEMAND_YES || (d->do_io == CONFIG_ONDEMAND_ONDEMAND && (readsectors || writesectors))) {
418             d->do_io = CONFIG_ONDEMAND_YES;
419
420             st = rrdset_find_bytype(RRD_TYPE_DISK, disk);
421             if(unlikely(!st)) {
422                 st = rrdset_create(RRD_TYPE_DISK, disk, NULL, family, "disk.io", "Disk I/O Bandwidth", "kilobytes/s", 2000, update_every, RRDSET_TYPE_AREA);
423
424                 rrddim_add(st, "reads", NULL, d->sector_size, 1024, RRDDIM_INCREMENTAL);
425                 rrddim_add(st, "writes", NULL, d->sector_size * -1, 1024, RRDDIM_INCREMENTAL);
426             }
427             else rrdset_next(st);
428
429             last_readsectors  = rrddim_set(st, "reads", readsectors);
430             last_writesectors = rrddim_set(st, "writes", writesectors);
431             rrdset_done(st);
432         }
433
434         // --------------------------------------------------------------------
435
436         if(d->do_ops == CONFIG_ONDEMAND_YES || (d->do_ops == CONFIG_ONDEMAND_ONDEMAND && (reads || writes))) {
437             d->do_ops = CONFIG_ONDEMAND_YES;
438
439             st = rrdset_find_bytype("disk_ops", disk);
440             if(unlikely(!st)) {
441                 st = rrdset_create("disk_ops", disk, NULL, family, "disk.ops", "Disk Completed I/O Operations", "operations/s", 2001, update_every, RRDSET_TYPE_LINE);
442                 st->isdetail = 1;
443
444                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
445                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
446             }
447             else rrdset_next(st);
448
449             last_reads  = rrddim_set(st, "reads", reads);
450             last_writes = rrddim_set(st, "writes", writes);
451             rrdset_done(st);
452         }
453
454         // --------------------------------------------------------------------
455
456         if(d->do_qops == CONFIG_ONDEMAND_YES || (d->do_qops == CONFIG_ONDEMAND_ONDEMAND && queued_ios)) {
457             d->do_qops = CONFIG_ONDEMAND_YES;
458
459             st = rrdset_find_bytype("disk_qops", disk);
460             if(unlikely(!st)) {
461                 st = rrdset_create("disk_qops", disk, NULL, family, "disk.qops", "Disk Current I/O Operations", "operations", 2002, update_every, RRDSET_TYPE_LINE);
462                 st->isdetail = 1;
463
464                 rrddim_add(st, "operations", NULL, 1, 1, RRDDIM_ABSOLUTE);
465             }
466             else rrdset_next(st);
467
468             rrddim_set(st, "operations", queued_ios);
469             rrdset_done(st);
470         }
471
472         // --------------------------------------------------------------------
473
474         if(d->do_backlog == CONFIG_ONDEMAND_YES || (d->do_backlog == CONFIG_ONDEMAND_ONDEMAND && backlog_ms)) {
475             d->do_backlog = CONFIG_ONDEMAND_YES;
476
477             st = rrdset_find_bytype("disk_backlog", disk);
478             if(unlikely(!st)) {
479                 st = rrdset_create("disk_backlog", disk, NULL, family, "disk.backlog", "Disk Backlog", "backlog (ms)", 2003, update_every, RRDSET_TYPE_AREA);
480                 st->isdetail = 1;
481
482                 rrddim_add(st, "backlog", NULL, 1, 10, RRDDIM_INCREMENTAL);
483             }
484             else rrdset_next(st);
485
486             rrddim_set(st, "backlog", backlog_ms);
487             rrdset_done(st);
488         }
489
490         // --------------------------------------------------------------------
491
492         if(d->do_util == CONFIG_ONDEMAND_YES || (d->do_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms)) {
493             d->do_util = CONFIG_ONDEMAND_YES;
494
495             st = rrdset_find_bytype("disk_util", disk);
496             if(unlikely(!st)) {
497                 st = rrdset_create("disk_util", disk, NULL, family, "disk.util", "Disk Utilization Time", "% of time working", 2004, update_every, RRDSET_TYPE_AREA);
498                 st->isdetail = 1;
499
500                 rrddim_add(st, "utilization", NULL, 1, 10, RRDDIM_INCREMENTAL);
501             }
502             else rrdset_next(st);
503
504             last_busy_ms = rrddim_set(st, "utilization", busy_ms);
505             rrdset_done(st);
506         }
507
508         // --------------------------------------------------------------------
509
510         if(d->do_mops == CONFIG_ONDEMAND_YES || (d->do_mops == CONFIG_ONDEMAND_ONDEMAND && (mreads || mwrites))) {
511             d->do_mops = CONFIG_ONDEMAND_YES;
512
513             st = rrdset_find_bytype("disk_mops", disk);
514             if(unlikely(!st)) {
515                 st = rrdset_create("disk_mops", disk, NULL, family, "disk.mops", "Disk Merged Operations", "merged operations/s", 2021, update_every, RRDSET_TYPE_LINE);
516                 st->isdetail = 1;
517
518                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
519                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
520             }
521             else rrdset_next(st);
522
523             rrddim_set(st, "reads", mreads);
524             rrddim_set(st, "writes", mwrites);
525             rrdset_done(st);
526         }
527
528         // --------------------------------------------------------------------
529
530         if(d->do_iotime == CONFIG_ONDEMAND_YES || (d->do_iotime == CONFIG_ONDEMAND_ONDEMAND && (readms || writems))) {
531             d->do_iotime = CONFIG_ONDEMAND_YES;
532
533             st = rrdset_find_bytype("disk_iotime", disk);
534             if(unlikely(!st)) {
535                 st = rrdset_create("disk_iotime", disk, NULL, family, "disk.iotime", "Disk Total I/O Time", "milliseconds/s", 2022, update_every, RRDSET_TYPE_LINE);
536                 st->isdetail = 1;
537
538                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
539                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
540             }
541             else rrdset_next(st);
542
543             last_readms  = rrddim_set(st, "reads", readms);
544             last_writems = rrddim_set(st, "writes", writems);
545             rrdset_done(st);
546         }
547
548         // --------------------------------------------------------------------
549         // calculate differential charts
550         // only if this is not the first time we run
551
552         if(likely(dt)) {
553             if( (d->do_iotime == CONFIG_ONDEMAND_YES || (d->do_iotime == CONFIG_ONDEMAND_ONDEMAND && (readms || writems))) &&
554                 (d->do_ops    == CONFIG_ONDEMAND_YES || (d->do_ops    == CONFIG_ONDEMAND_ONDEMAND && (reads || writes)))) {
555                 st = rrdset_find_bytype("disk_await", disk);
556                 if(unlikely(!st)) {
557                     st = rrdset_create("disk_await", disk, NULL, family, "disk.await", "Average Completed I/O Operation Time", "ms per operation", 2005, update_every, RRDSET_TYPE_LINE);
558                     st->isdetail = 1;
559
560                     rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_ABSOLUTE);
561                     rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_ABSOLUTE);
562                 }
563                 else rrdset_next(st);
564
565                 rrddim_set(st, "reads", (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
566                 rrddim_set(st, "writes", (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
567                 rrdset_done(st);
568             }
569
570             if( (d->do_io  == CONFIG_ONDEMAND_YES || (d->do_io  == CONFIG_ONDEMAND_ONDEMAND && (readsectors || writesectors))) &&
571                 (d->do_ops == CONFIG_ONDEMAND_YES || (d->do_ops == CONFIG_ONDEMAND_ONDEMAND && (reads || writes)))) {
572                 st = rrdset_find_bytype("disk_avgsz", disk);
573                 if(unlikely(!st)) {
574                     st = rrdset_create("disk_avgsz", disk, NULL, family, "disk.avgsz", "Average Completed I/O Operation Bandwidth", "kilobytes per operation", 2006, update_every, RRDSET_TYPE_AREA);
575                     st->isdetail = 1;
576
577                     rrddim_add(st, "reads", NULL, d->sector_size, 1024, RRDDIM_ABSOLUTE);
578                     rrddim_add(st, "writes", NULL, d->sector_size * -1, 1024, RRDDIM_ABSOLUTE);
579                 }
580                 else rrdset_next(st);
581
582                 rrddim_set(st, "reads", (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
583                 rrddim_set(st, "writes", (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
584                 rrdset_done(st);
585             }
586
587             if( (d->do_util == CONFIG_ONDEMAND_YES || (d->do_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms)) &&
588                 (d->do_ops  == CONFIG_ONDEMAND_YES || (d->do_ops  == CONFIG_ONDEMAND_ONDEMAND && (reads || writes)))) {
589                 st = rrdset_find_bytype("disk_svctm", disk);
590                 if(unlikely(!st)) {
591                     st = rrdset_create("disk_svctm", disk, NULL, family, "disk.svctm", "Average Service Time", "ms per operation", 2007, update_every, RRDSET_TYPE_LINE);
592                     st->isdetail = 1;
593
594                     rrddim_add(st, "svctm", NULL, 1, 1, RRDDIM_ABSOLUTE);
595                 }
596                 else rrdset_next(st);
597
598                 rrddim_set(st, "svctm", ((reads - last_reads) + (writes - last_writes)) ? (busy_ms - last_busy_ms) / ((reads - last_reads) + (writes - last_writes)) : 0);
599                 rrdset_done(st);
600             }
601         }
602     }
603
604     return 0;
605 }