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