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