]> arthur.barton.de Git - netdata.git/blob - src/proc_diskstats.c
fix apps.plugin values on 32bit machines
[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", global_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", global_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", global_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 char *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);
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 major_configs[major];
197 }
198
199 int do_proc_diskstats(int update_every, usec_t dt) {
200     (void)dt;
201
202     static procfile *ff = NULL;
203     static int  global_enable_new_disks_detected_at_runtime = CONFIG_ONDEMAND_YES,
204                 global_enable_performance_for_physical_disks = CONFIG_ONDEMAND_ONDEMAND,
205                 global_enable_performance_for_virtual_disks = CONFIG_ONDEMAND_ONDEMAND,
206                 global_enable_performance_for_partitions = CONFIG_ONDEMAND_NO,
207                 global_do_io = CONFIG_ONDEMAND_ONDEMAND,
208                 global_do_ops = CONFIG_ONDEMAND_ONDEMAND,
209                 global_do_mops = CONFIG_ONDEMAND_ONDEMAND,
210                 global_do_iotime = CONFIG_ONDEMAND_ONDEMAND,
211                 global_do_qops = CONFIG_ONDEMAND_ONDEMAND,
212                 global_do_util = CONFIG_ONDEMAND_ONDEMAND,
213                 global_do_backlog = CONFIG_ONDEMAND_ONDEMAND,
214                 globals_initialized = 0;
215
216     if(unlikely(!globals_initialized)) {
217         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);
218
219         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);
220         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);
221         global_enable_performance_for_partitions = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for partitions", global_enable_performance_for_partitions);
222
223         global_do_io      = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "bandwidth for all disks", global_do_io);
224         global_do_ops     = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "operations for all disks", global_do_ops);
225         global_do_mops    = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "merged operations for all disks", global_do_mops);
226         global_do_iotime  = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "i/o time for all disks", global_do_iotime);
227         global_do_qops    = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "queued operations for all disks", global_do_qops);
228         global_do_util    = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "utilization percentage for all disks", global_do_util);
229         global_do_backlog = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "backlog for all disks", global_do_backlog);
230
231         globals_initialized = 1;
232     }
233
234     // --------------------------------------------------------------------------
235
236     if(unlikely(!ff)) {
237         char filename[FILENAME_MAX + 1];
238         snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/diskstats");
239         ff = procfile_open(config_get("plugin:proc:/proc/diskstats", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
240     }
241     if(unlikely(!ff)) return 0;
242
243     ff = procfile_readall(ff);
244     if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
245
246     size_t lines = procfile_lines(ff), l;
247
248     for(l = 0; l < lines ;l++) {
249         // --------------------------------------------------------------------------
250         // Read parameters
251
252         char *disk;
253         unsigned long       major = 0, minor = 0;
254
255         collected_number    reads = 0,  mreads = 0,  readsectors = 0,  readms = 0,
256                             writes = 0, mwrites = 0, writesectors = 0, writems = 0,
257                             queued_ios = 0, busy_ms = 0, backlog_ms = 0;
258
259         collected_number    last_reads = 0,  last_readsectors = 0,  last_readms = 0,
260                             last_writes = 0, last_writesectors = 0, last_writems = 0,
261                             last_busy_ms = 0;
262
263         size_t words = procfile_linewords(ff, l);
264         if(unlikely(words < 14)) continue;
265
266         major           = str2ul(procfile_lineword(ff, l, 0));
267         minor           = str2ul(procfile_lineword(ff, l, 1));
268         disk            = procfile_lineword(ff, l, 2);
269
270         // # of reads completed # of writes completed
271         // This is the total number of reads or writes completed successfully.
272         reads           = str2ull(procfile_lineword(ff, l, 3));  // rd_ios
273         writes          = str2ull(procfile_lineword(ff, l, 7));  // wr_ios
274
275         // # of reads merged # of writes merged
276         // Reads and writes which are adjacent to each other may be merged for
277         // efficiency.  Thus two 4K reads may become one 8K read before it is
278         // ultimately handed to the disk, and so it will be counted (and queued)
279         mreads          = str2ull(procfile_lineword(ff, l, 4));  // rd_merges_or_rd_sec
280         mwrites         = str2ull(procfile_lineword(ff, l, 8));  // wr_merges
281
282         // # of sectors read # of sectors written
283         // This is the total number of sectors read or written successfully.
284         readsectors     = str2ull(procfile_lineword(ff, l, 5));  // rd_sec_or_wr_ios
285         writesectors    = str2ull(procfile_lineword(ff, l, 9));  // wr_sec
286
287         // # of milliseconds spent reading # of milliseconds spent writing
288         // This is the total number of milliseconds spent by all reads or writes (as
289         // measured from __make_request() to end_that_request_last()).
290         readms          = str2ull(procfile_lineword(ff, l, 6));  // rd_ticks_or_wr_sec
291         writems         = str2ull(procfile_lineword(ff, l, 10)); // wr_ticks
292
293         // # of I/Os currently in progress
294         // The only field that should go to zero. Incremented as requests are
295         // given to appropriate struct request_queue and decremented as they finish.
296         queued_ios      = str2ull(procfile_lineword(ff, l, 11)); // ios_pgr
297
298         // # of milliseconds spent doing I/Os
299         // This field increases so long as field queued_ios is nonzero.
300         busy_ms         = str2ull(procfile_lineword(ff, l, 12)); // tot_ticks
301
302         // weighted # of milliseconds spent doing I/Os
303         // This field is incremented at each I/O start, I/O completion, I/O
304         // merge, or read of these stats by the number of I/Os in progress
305         // (field queued_ios) times the number of milliseconds spent doing I/O since the
306         // last update of this field.  This can provide an easy measure of both
307         // I/O completion time and the backlog that may be accumulating.
308         backlog_ms      = str2ull(procfile_lineword(ff, l, 13)); // rq_ticks
309
310
311         // --------------------------------------------------------------------------
312         // remove slashes from disk names
313         char *s;
314         for(s = disk; *s ;s++)
315             if(*s == '/') *s = '_';
316
317         // --------------------------------------------------------------------------
318         // get a disk structure for the disk
319
320         struct disk *d = get_disk(major, minor, disk);
321
322
323         // --------------------------------------------------------------------------
324         // Set its family based on mount point
325
326         char *family = d->mount_point;
327         if(!family) family = disk;
328
329
330         // --------------------------------------------------------------------------
331         // Check the configuration for the device
332
333         if(unlikely(!d->configured)) {
334             char var_name[4096 + 1];
335             snprintfz(var_name, 4096, "plugin:proc:/proc/diskstats:%s", disk);
336
337             int def_enable = config_get_boolean_ondemand(var_name, "enable", global_enable_new_disks_detected_at_runtime);
338             if(unlikely(def_enable == CONFIG_ONDEMAND_NO)) {
339                 // the user does not want any metrics for this disk
340                 d->do_io = CONFIG_ONDEMAND_NO;
341                 d->do_ops = CONFIG_ONDEMAND_NO;
342                 d->do_mops = CONFIG_ONDEMAND_NO;
343                 d->do_iotime = CONFIG_ONDEMAND_NO;
344                 d->do_qops = CONFIG_ONDEMAND_NO;
345                 d->do_util = CONFIG_ONDEMAND_NO;
346                 d->do_backlog = CONFIG_ONDEMAND_NO;
347             }
348             else {
349                 // this disk is enabled
350                 // check its direct settings
351
352                 int def_performance = CONFIG_ONDEMAND_ONDEMAND;
353
354                 // since this is 'on demand' we can figure the performance settings
355                 // based on the type of disk
356
357                 switch(d->type) {
358                     case DISK_TYPE_PHYSICAL:
359                         def_performance = global_enable_performance_for_physical_disks;
360                         break;
361
362                     case DISK_TYPE_PARTITION:
363                         def_performance = global_enable_performance_for_partitions;
364                         break;
365
366                     case DISK_TYPE_CONTAINER:
367                         def_performance = global_enable_performance_for_virtual_disks;
368                         break;
369                 }
370
371                 // check if we have to disable performance for this disk
372                 if(def_performance)
373                     def_performance = is_major_enabled((int)major);
374
375                 // ------------------------------------------------------------
376                 // now we have def_performance and def_space
377                 // to work further
378
379                 // def_performance
380                 // check the user configuration (this will also show our 'on demand' decision)
381                 def_performance = config_get_boolean_ondemand(var_name, "enable performance metrics", def_performance);
382
383                 int ddo_io = CONFIG_ONDEMAND_NO,
384                     ddo_ops = CONFIG_ONDEMAND_NO,
385                     ddo_mops = CONFIG_ONDEMAND_NO,
386                     ddo_iotime = CONFIG_ONDEMAND_NO,
387                     ddo_qops = CONFIG_ONDEMAND_NO,
388                     ddo_util = CONFIG_ONDEMAND_NO,
389                     ddo_backlog = CONFIG_ONDEMAND_NO;
390
391                 // we enable individual performance charts only when def_performance is not disabled
392                 if(unlikely(def_performance != CONFIG_ONDEMAND_NO)) {
393                     ddo_io = global_do_io,
394                     ddo_ops = global_do_ops,
395                     ddo_mops = global_do_mops,
396                     ddo_iotime = global_do_iotime,
397                     ddo_qops = global_do_qops,
398                     ddo_util = global_do_util,
399                     ddo_backlog = global_do_backlog;
400                 }
401
402                 d->do_io      = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
403                 d->do_ops     = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
404                 d->do_mops    = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
405                 d->do_iotime  = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
406                 d->do_qops    = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
407                 d->do_util    = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
408                 d->do_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
409             }
410
411             d->configured = 1;
412         }
413
414         RRDSET *st;
415
416         // --------------------------------------------------------------------------
417         // Do performance metrics
418
419         if(d->do_io == CONFIG_ONDEMAND_YES || (d->do_io == CONFIG_ONDEMAND_ONDEMAND && (readsectors || writesectors))) {
420             d->do_io = CONFIG_ONDEMAND_YES;
421
422             st = rrdset_find_bytype(RRD_TYPE_DISK, disk);
423             if(unlikely(!st)) {
424                 st = rrdset_create(RRD_TYPE_DISK, disk, NULL, family, "disk.io", "Disk I/O Bandwidth", "kilobytes/s", 2000, update_every, RRDSET_TYPE_AREA);
425
426                 rrddim_add(st, "reads", NULL, d->sector_size, 1024, RRDDIM_INCREMENTAL);
427                 rrddim_add(st, "writes", NULL, d->sector_size * -1, 1024, RRDDIM_INCREMENTAL);
428             }
429             else rrdset_next(st);
430
431             last_readsectors  = rrddim_set(st, "reads", readsectors);
432             last_writesectors = rrddim_set(st, "writes", writesectors);
433             rrdset_done(st);
434         }
435
436         // --------------------------------------------------------------------
437
438         if(d->do_ops == CONFIG_ONDEMAND_YES || (d->do_ops == CONFIG_ONDEMAND_ONDEMAND && (reads || writes))) {
439             d->do_ops = CONFIG_ONDEMAND_YES;
440
441             st = rrdset_find_bytype("disk_ops", disk);
442             if(unlikely(!st)) {
443                 st = rrdset_create("disk_ops", disk, NULL, family, "disk.ops", "Disk Completed I/O Operations", "operations/s", 2001, update_every, RRDSET_TYPE_LINE);
444                 st->isdetail = 1;
445
446                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
447                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_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_ONDEMAND_YES || (d->do_qops == CONFIG_ONDEMAND_ONDEMAND && queued_ios)) {
459             d->do_qops = CONFIG_ONDEMAND_YES;
460
461             st = rrdset_find_bytype("disk_qops", disk);
462             if(unlikely(!st)) {
463                 st = rrdset_create("disk_qops", disk, NULL, family, "disk.qops", "Disk Current I/O Operations", "operations", 2002, update_every, RRDSET_TYPE_LINE);
464                 st->isdetail = 1;
465
466                 rrddim_add(st, "operations", NULL, 1, 1, RRDDIM_ABSOLUTE);
467             }
468             else rrdset_next(st);
469
470             rrddim_set(st, "operations", queued_ios);
471             rrdset_done(st);
472         }
473
474         // --------------------------------------------------------------------
475
476         if(d->do_backlog == CONFIG_ONDEMAND_YES || (d->do_backlog == CONFIG_ONDEMAND_ONDEMAND && backlog_ms)) {
477             d->do_backlog = CONFIG_ONDEMAND_YES;
478
479             st = rrdset_find_bytype("disk_backlog", disk);
480             if(unlikely(!st)) {
481                 st = rrdset_create("disk_backlog", disk, NULL, family, "disk.backlog", "Disk Backlog", "backlog (ms)", 2003, update_every, RRDSET_TYPE_AREA);
482                 st->isdetail = 1;
483
484                 rrddim_add(st, "backlog", NULL, 1, 10, RRDDIM_INCREMENTAL);
485             }
486             else rrdset_next(st);
487
488             rrddim_set(st, "backlog", backlog_ms);
489             rrdset_done(st);
490         }
491
492         // --------------------------------------------------------------------
493
494         if(d->do_util == CONFIG_ONDEMAND_YES || (d->do_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms)) {
495             d->do_util = CONFIG_ONDEMAND_YES;
496
497             st = rrdset_find_bytype("disk_util", disk);
498             if(unlikely(!st)) {
499                 st = rrdset_create("disk_util", disk, NULL, family, "disk.util", "Disk Utilization Time", "% of time working", 2004, update_every, RRDSET_TYPE_AREA);
500                 st->isdetail = 1;
501
502                 rrddim_add(st, "utilization", NULL, 1, 10, RRDDIM_INCREMENTAL);
503             }
504             else rrdset_next(st);
505
506             last_busy_ms = rrddim_set(st, "utilization", busy_ms);
507             rrdset_done(st);
508         }
509
510         // --------------------------------------------------------------------
511
512         if(d->do_mops == CONFIG_ONDEMAND_YES || (d->do_mops == CONFIG_ONDEMAND_ONDEMAND && (mreads || mwrites))) {
513             d->do_mops = CONFIG_ONDEMAND_YES;
514
515             st = rrdset_find_bytype("disk_mops", disk);
516             if(unlikely(!st)) {
517                 st = rrdset_create("disk_mops", disk, NULL, family, "disk.mops", "Disk Merged Operations", "merged operations/s", 2021, update_every, RRDSET_TYPE_LINE);
518                 st->isdetail = 1;
519
520                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
521                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
522             }
523             else rrdset_next(st);
524
525             rrddim_set(st, "reads", mreads);
526             rrddim_set(st, "writes", mwrites);
527             rrdset_done(st);
528         }
529
530         // --------------------------------------------------------------------
531
532         if(d->do_iotime == CONFIG_ONDEMAND_YES || (d->do_iotime == CONFIG_ONDEMAND_ONDEMAND && (readms || writems))) {
533             d->do_iotime = CONFIG_ONDEMAND_YES;
534
535             st = rrdset_find_bytype("disk_iotime", disk);
536             if(unlikely(!st)) {
537                 st = rrdset_create("disk_iotime", disk, NULL, family, "disk.iotime", "Disk Total I/O Time", "milliseconds/s", 2022, update_every, RRDSET_TYPE_LINE);
538                 st->isdetail = 1;
539
540                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
541                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
542             }
543             else rrdset_next(st);
544
545             last_readms  = rrddim_set(st, "reads", readms);
546             last_writems = rrddim_set(st, "writes", writems);
547             rrdset_done(st);
548         }
549
550         // --------------------------------------------------------------------
551         // calculate differential charts
552         // only if this is not the first time we run
553
554         if(likely(dt)) {
555             if( (d->do_iotime == CONFIG_ONDEMAND_YES || (d->do_iotime == CONFIG_ONDEMAND_ONDEMAND && (readms || writems))) &&
556                 (d->do_ops    == CONFIG_ONDEMAND_YES || (d->do_ops    == CONFIG_ONDEMAND_ONDEMAND && (reads || writes)))) {
557                 st = rrdset_find_bytype("disk_await", disk);
558                 if(unlikely(!st)) {
559                     st = rrdset_create("disk_await", disk, NULL, family, "disk.await", "Average Completed I/O Operation Time", "ms per operation", 2005, update_every, RRDSET_TYPE_LINE);
560                     st->isdetail = 1;
561
562                     rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_ABSOLUTE);
563                     rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_ABSOLUTE);
564                 }
565                 else rrdset_next(st);
566
567                 rrddim_set(st, "reads", (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
568                 rrddim_set(st, "writes", (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
569                 rrdset_done(st);
570             }
571
572             if( (d->do_io  == CONFIG_ONDEMAND_YES || (d->do_io  == CONFIG_ONDEMAND_ONDEMAND && (readsectors || writesectors))) &&
573                 (d->do_ops == CONFIG_ONDEMAND_YES || (d->do_ops == CONFIG_ONDEMAND_ONDEMAND && (reads || writes)))) {
574                 st = rrdset_find_bytype("disk_avgsz", disk);
575                 if(unlikely(!st)) {
576                     st = rrdset_create("disk_avgsz", disk, NULL, family, "disk.avgsz", "Average Completed I/O Operation Bandwidth", "kilobytes per operation", 2006, update_every, RRDSET_TYPE_AREA);
577                     st->isdetail = 1;
578
579                     rrddim_add(st, "reads", NULL, d->sector_size, 1024, RRDDIM_ABSOLUTE);
580                     rrddim_add(st, "writes", NULL, d->sector_size * -1, 1024, RRDDIM_ABSOLUTE);
581                 }
582                 else rrdset_next(st);
583
584                 rrddim_set(st, "reads", (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
585                 rrddim_set(st, "writes", (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
586                 rrdset_done(st);
587             }
588
589             if( (d->do_util == CONFIG_ONDEMAND_YES || (d->do_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms)) &&
590                 (d->do_ops  == CONFIG_ONDEMAND_YES || (d->do_ops  == CONFIG_ONDEMAND_ONDEMAND && (reads || writes)))) {
591                 st = rrdset_find_bytype("disk_svctm", disk);
592                 if(unlikely(!st)) {
593                     st = rrdset_create("disk_svctm", disk, NULL, family, "disk.svctm", "Average Service Time", "ms per operation", 2007, update_every, RRDSET_TYPE_LINE);
594                     st->isdetail = 1;
595
596                     rrddim_add(st, "svctm", NULL, 1, 1, RRDDIM_ABSOLUTE);
597                 }
598                 else rrdset_next(st);
599
600                 rrddim_set(st, "svctm", ((reads - last_reads) + (writes - last_writes)) ? (busy_ms - last_busy_ms) / ((reads - last_reads) + (writes - last_writes)) : 0);
601                 rrdset_done(st);
602             }
603         }
604     }
605
606     return 0;
607 }