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