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