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