proc_diskstats.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. #define RRD_TYPE_DISK "disk"
  4. #define PLUGIN_PROC_MODULE_DISKSTATS_NAME "/proc/diskstats"
  5. #define CONFIG_SECTION_PLUGIN_PROC_DISKSTATS "plugin:" PLUGIN_PROC_CONFIG_NAME ":" PLUGIN_PROC_MODULE_DISKSTATS_NAME
  6. #define DISK_TYPE_UNKNOWN 0
  7. #define DISK_TYPE_PHYSICAL 1
  8. #define DISK_TYPE_PARTITION 2
  9. #define DISK_TYPE_VIRTUAL 3
  10. #define DEFAULT_EXCLUDED_DISKS "loop* ram*"
  11. static struct disk {
  12. char *disk; // the name of the disk (sda, sdb, etc, after being looked up)
  13. char *device; // the device of the disk (before being looked up)
  14. unsigned long major;
  15. unsigned long minor;
  16. int sector_size;
  17. int type;
  18. char *mount_point;
  19. // disk options caching
  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. int do_bcache;
  28. int updated;
  29. int device_is_bcache;
  30. char *bcache_filename_dirty_data;
  31. char *bcache_filename_writeback_rate;
  32. char *bcache_filename_cache_congested;
  33. char *bcache_filename_cache_available_percent;
  34. char *bcache_filename_stats_five_minute_cache_hit_ratio;
  35. char *bcache_filename_stats_hour_cache_hit_ratio;
  36. char *bcache_filename_stats_day_cache_hit_ratio;
  37. char *bcache_filename_stats_total_cache_hit_ratio;
  38. char *bcache_filename_stats_total_cache_hits;
  39. char *bcache_filename_stats_total_cache_misses;
  40. char *bcache_filename_stats_total_cache_miss_collisions;
  41. char *bcache_filename_stats_total_cache_bypass_hits;
  42. char *bcache_filename_stats_total_cache_bypass_misses;
  43. char *bcache_filename_stats_total_cache_readaheads;
  44. char *bcache_filename_cache_read_races;
  45. char *bcache_filename_cache_io_errors;
  46. char *bcache_filename_priority_stats;
  47. usec_t bcache_priority_stats_update_every_usec;
  48. usec_t bcache_priority_stats_elapsed_usec;
  49. RRDSET *st_io;
  50. RRDDIM *rd_io_reads;
  51. RRDDIM *rd_io_writes;
  52. RRDSET *st_ops;
  53. RRDDIM *rd_ops_reads;
  54. RRDDIM *rd_ops_writes;
  55. RRDSET *st_qops;
  56. RRDDIM *rd_qops_operations;
  57. RRDSET *st_backlog;
  58. RRDDIM *rd_backlog_backlog;
  59. RRDSET *st_util;
  60. RRDDIM *rd_util_utilization;
  61. RRDSET *st_mops;
  62. RRDDIM *rd_mops_reads;
  63. RRDDIM *rd_mops_writes;
  64. RRDSET *st_iotime;
  65. RRDDIM *rd_iotime_reads;
  66. RRDDIM *rd_iotime_writes;
  67. RRDSET *st_await;
  68. RRDDIM *rd_await_reads;
  69. RRDDIM *rd_await_writes;
  70. RRDSET *st_avgsz;
  71. RRDDIM *rd_avgsz_reads;
  72. RRDDIM *rd_avgsz_writes;
  73. RRDSET *st_svctm;
  74. RRDDIM *rd_svctm_svctm;
  75. RRDSET *st_bcache_size;
  76. RRDDIM *rd_bcache_dirty_size;
  77. RRDSET *st_bcache_usage;
  78. RRDDIM *rd_bcache_available_percent;
  79. RRDSET *st_bcache_hit_ratio;
  80. RRDDIM *rd_bcache_hit_ratio_5min;
  81. RRDDIM *rd_bcache_hit_ratio_1hour;
  82. RRDDIM *rd_bcache_hit_ratio_1day;
  83. RRDDIM *rd_bcache_hit_ratio_total;
  84. RRDSET *st_bcache;
  85. RRDDIM *rd_bcache_hits;
  86. RRDDIM *rd_bcache_misses;
  87. RRDDIM *rd_bcache_miss_collisions;
  88. RRDSET *st_bcache_bypass;
  89. RRDDIM *rd_bcache_bypass_hits;
  90. RRDDIM *rd_bcache_bypass_misses;
  91. RRDSET *st_bcache_rates;
  92. RRDDIM *rd_bcache_rate_congested;
  93. RRDDIM *rd_bcache_readaheads;
  94. RRDDIM *rd_bcache_rate_writeback;
  95. RRDSET *st_bcache_cache_allocations;
  96. RRDDIM *rd_bcache_cache_allocations_unused;
  97. RRDDIM *rd_bcache_cache_allocations_clean;
  98. RRDDIM *rd_bcache_cache_allocations_dirty;
  99. RRDDIM *rd_bcache_cache_allocations_metadata;
  100. RRDDIM *rd_bcache_cache_allocations_unknown;
  101. RRDSET *st_bcache_cache_read_races;
  102. RRDDIM *rd_bcache_cache_read_races;
  103. RRDDIM *rd_bcache_cache_io_errors;
  104. struct disk *next;
  105. } *disk_root = NULL;
  106. #define rrdset_obsolete_and_pointer_null(st) do { if(st) { rrdset_is_obsolete(st); (st) = NULL; } } while(st)
  107. // static char *path_to_get_hw_sector_size = NULL;
  108. // static char *path_to_get_hw_sector_size_partitions = NULL;
  109. static char *path_to_sys_dev_block_major_minor_string = NULL;
  110. static char *path_to_sys_block_device = NULL;
  111. static char *path_to_sys_block_device_bcache = NULL;
  112. static char *path_to_sys_devices_virtual_block_device = NULL;
  113. static char *path_to_device_mapper = NULL;
  114. static char *path_to_device_label = NULL;
  115. static char *path_to_device_id = NULL;
  116. static char *path_to_veritas_volume_groups = NULL;
  117. static int name_disks_by_id = CONFIG_BOOLEAN_NO;
  118. static int global_bcache_priority_stats_update_every = 0; // disabled by default
  119. static int global_enable_new_disks_detected_at_runtime = CONFIG_BOOLEAN_YES,
  120. global_enable_performance_for_physical_disks = CONFIG_BOOLEAN_AUTO,
  121. global_enable_performance_for_virtual_disks = CONFIG_BOOLEAN_AUTO,
  122. global_enable_performance_for_partitions = CONFIG_BOOLEAN_NO,
  123. global_do_io = CONFIG_BOOLEAN_AUTO,
  124. global_do_ops = CONFIG_BOOLEAN_AUTO,
  125. global_do_mops = CONFIG_BOOLEAN_AUTO,
  126. global_do_iotime = CONFIG_BOOLEAN_AUTO,
  127. global_do_qops = CONFIG_BOOLEAN_AUTO,
  128. global_do_util = CONFIG_BOOLEAN_AUTO,
  129. global_do_backlog = CONFIG_BOOLEAN_AUTO,
  130. global_do_bcache = CONFIG_BOOLEAN_AUTO,
  131. globals_initialized = 0,
  132. global_cleanup_removed_disks = 1;
  133. static SIMPLE_PATTERN *excluded_disks = NULL;
  134. static unsigned long long int bcache_read_number_with_units(const char *filename) {
  135. char buffer[50 + 1];
  136. if(read_file(filename, buffer, 50) == 0) {
  137. static int unknown_units_error = 10;
  138. char *end = NULL;
  139. long double value = str2ld(buffer, &end);
  140. if(end && *end) {
  141. if(*end == 'k')
  142. return (unsigned long long int)(value * 1024.0);
  143. else if(*end == 'M')
  144. return (unsigned long long int)(value * 1024.0 * 1024.0);
  145. else if(*end == 'G')
  146. return (unsigned long long int)(value * 1024.0 * 1024.0 * 1024.0);
  147. else if(*end == 'T')
  148. return (unsigned long long int)(value * 1024.0 * 1024.0 * 1024.0 * 1024.0);
  149. else if(unknown_units_error > 0) {
  150. error("bcache file '%s' provides value '%s' with unknown units '%s'", filename, buffer, end);
  151. unknown_units_error--;
  152. }
  153. }
  154. return (unsigned long long int)value;
  155. }
  156. return 0;
  157. }
  158. void bcache_read_priority_stats(struct disk *d, const char *family, int update_every, usec_t dt) {
  159. static procfile *ff = NULL;
  160. static char *separators = " \t:%[]";
  161. static ARL_BASE *arl_base = NULL;
  162. static unsigned long long unused;
  163. static unsigned long long clean;
  164. static unsigned long long dirty;
  165. static unsigned long long metadata;
  166. static unsigned long long unknown;
  167. // check if it is time to update this metric
  168. d->bcache_priority_stats_elapsed_usec += dt;
  169. if(likely(d->bcache_priority_stats_elapsed_usec < d->bcache_priority_stats_update_every_usec)) return;
  170. d->bcache_priority_stats_elapsed_usec = 0;
  171. // initialize ARL
  172. if(unlikely(!arl_base)) {
  173. arl_base = arl_create("bcache/priority_stats", NULL, 60);
  174. arl_expect(arl_base, "Unused", &unused);
  175. arl_expect(arl_base, "Clean", &clean);
  176. arl_expect(arl_base, "Dirty", &dirty);
  177. arl_expect(arl_base, "Metadata", &metadata);
  178. }
  179. ff = procfile_reopen(ff, d->bcache_filename_priority_stats, separators, PROCFILE_FLAG_DEFAULT);
  180. if(likely(ff)) ff = procfile_readall(ff);
  181. if(unlikely(!ff)) {
  182. separators = " \t:%[]";
  183. return;
  184. }
  185. // do not reset the separators on every iteration
  186. separators = NULL;
  187. arl_begin(arl_base);
  188. unused = clean = dirty = metadata = unknown = 0;
  189. size_t lines = procfile_lines(ff), l;
  190. for(l = 0; l < lines ;l++) {
  191. size_t words = procfile_linewords(ff, l);
  192. if(unlikely(words < 2)) {
  193. if(unlikely(words)) error("Cannot read '%s' line %zu. Expected 2 params, read %zu.", d->bcache_filename_priority_stats, l, words);
  194. continue;
  195. }
  196. if(unlikely(arl_check(arl_base,
  197. procfile_lineword(ff, l, 0),
  198. procfile_lineword(ff, l, 1)))) break;
  199. }
  200. unknown = 100 - unused - clean - dirty - metadata;
  201. // create / update the cache allocations chart
  202. {
  203. if(unlikely(!d->st_bcache_cache_allocations)) {
  204. d->st_bcache_cache_allocations = rrdset_create_localhost(
  205. "disk_bcache_cache_alloc"
  206. , d->device
  207. , d->disk
  208. , family
  209. , "disk.bcache_cache_alloc"
  210. , "BCache Cache Allocations"
  211. , "percentage"
  212. , PLUGIN_PROC_NAME
  213. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  214. , NETDATA_CHART_PRIO_BCACHE_CACHE_ALLOC
  215. , update_every
  216. , RRDSET_TYPE_STACKED
  217. );
  218. d->rd_bcache_cache_allocations_unused = rrddim_add(d->st_bcache_cache_allocations, "unused", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  219. d->rd_bcache_cache_allocations_dirty = rrddim_add(d->st_bcache_cache_allocations, "dirty", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  220. d->rd_bcache_cache_allocations_clean = rrddim_add(d->st_bcache_cache_allocations, "clean", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  221. d->rd_bcache_cache_allocations_metadata = rrddim_add(d->st_bcache_cache_allocations, "metadata", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  222. d->rd_bcache_cache_allocations_unknown = rrddim_add(d->st_bcache_cache_allocations, "undefined", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  223. d->bcache_priority_stats_update_every_usec = update_every * USEC_PER_SEC;
  224. }
  225. else rrdset_next(d->st_bcache_cache_allocations);
  226. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_unused, unused);
  227. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_dirty, dirty);
  228. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_clean, clean);
  229. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_metadata, metadata);
  230. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_unknown, unknown);
  231. rrdset_done(d->st_bcache_cache_allocations);
  232. }
  233. }
  234. static inline int is_major_enabled(int major) {
  235. static int8_t *major_configs = NULL;
  236. static size_t major_size = 0;
  237. if(major < 0) return 1;
  238. size_t wanted_size = (size_t)major + 1;
  239. if(major_size < wanted_size) {
  240. major_configs = reallocz(major_configs, wanted_size * sizeof(int8_t));
  241. size_t i;
  242. for(i = major_size; i < wanted_size ; i++)
  243. major_configs[i] = -1;
  244. major_size = wanted_size;
  245. }
  246. if(major_configs[major] == -1) {
  247. char buffer[CONFIG_MAX_NAME + 1];
  248. snprintfz(buffer, CONFIG_MAX_NAME, "performance metrics for disks with major %d", major);
  249. major_configs[major] = (char)config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, buffer, 1);
  250. }
  251. return (int)major_configs[major];
  252. }
  253. static inline int get_disk_name_from_path(const char *path, char *result, size_t result_size, unsigned long major, unsigned long minor, char *disk, char *prefix, int depth) {
  254. //info("DEVICE-MAPPER ('%s', %lu:%lu): examining directory '%s' (allowed depth %d).", disk, major, minor, path, depth);
  255. int found = 0;
  256. DIR *dir = opendir(path);
  257. if (!dir) {
  258. error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot open directory '%s'.", disk, major, minor, path);
  259. goto failed;
  260. }
  261. struct dirent *de = NULL;
  262. while ((de = readdir(dir))) {
  263. if(de->d_type == DT_DIR) {
  264. if((de->d_name[0] == '.' && de->d_name[1] == '\0') || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0'))
  265. continue;
  266. if(depth <= 0) {
  267. error("DEVICE-MAPPER ('%s', %lu:%lu): Depth limit reached for path '%s/%s'. Ignoring path.", disk, major, minor, path, de->d_name);
  268. break;
  269. }
  270. else {
  271. char *path_nested = NULL;
  272. char *prefix_nested = NULL;
  273. {
  274. char buffer[FILENAME_MAX + 1];
  275. snprintfz(buffer, FILENAME_MAX, "%s/%s", path, de->d_name);
  276. path_nested = strdupz(buffer);
  277. snprintfz(buffer, FILENAME_MAX, "%s%s%s", (prefix)?prefix:"", (prefix)?"_":"", de->d_name);
  278. prefix_nested = strdupz(buffer);
  279. }
  280. found = get_disk_name_from_path(path_nested, result, result_size, major, minor, disk, prefix_nested, depth - 1);
  281. freez(path_nested);
  282. freez(prefix_nested);
  283. if(found) break;
  284. }
  285. }
  286. else if(de->d_type == DT_LNK || de->d_type == DT_BLK) {
  287. char filename[FILENAME_MAX + 1];
  288. if(de->d_type == DT_LNK) {
  289. snprintfz(filename, FILENAME_MAX, "%s/%s", path, de->d_name);
  290. ssize_t len = readlink(filename, result, result_size - 1);
  291. if(len <= 0) {
  292. error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot read link '%s'.", disk, major, minor, filename);
  293. continue;
  294. }
  295. result[len] = '\0';
  296. if(result[0] != '/')
  297. snprintfz(filename, FILENAME_MAX, "%s/%s", path, result);
  298. else
  299. strncpyz(filename, result, FILENAME_MAX);
  300. }
  301. else {
  302. snprintfz(filename, FILENAME_MAX, "%s/%s", path, de->d_name);
  303. }
  304. struct stat sb;
  305. if(stat(filename, &sb) == -1) {
  306. error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot stat() file '%s'.", disk, major, minor, filename);
  307. continue;
  308. }
  309. if((sb.st_mode & S_IFMT) != S_IFBLK) {
  310. //info("DEVICE-MAPPER ('%s', %lu:%lu): file '%s' is not a block device.", disk, major, minor, filename);
  311. continue;
  312. }
  313. if(major(sb.st_rdev) != major || minor(sb.st_rdev) != minor) {
  314. //info("DEVICE-MAPPER ('%s', %lu:%lu): filename '%s' does not match %lu:%lu.", disk, major, minor, filename, (unsigned long)major(sb.st_rdev), (unsigned long)minor(sb.st_rdev));
  315. continue;
  316. }
  317. //info("DEVICE-MAPPER ('%s', %lu:%lu): filename '%s' matches.", disk, major, minor, filename);
  318. snprintfz(result, result_size - 1, "%s%s%s", (prefix)?prefix:"", (prefix)?"_":"", de->d_name);
  319. found = 1;
  320. break;
  321. }
  322. }
  323. closedir(dir);
  324. failed:
  325. if(!found)
  326. result[0] = '\0';
  327. return found;
  328. }
  329. static inline char *get_disk_name(unsigned long major, unsigned long minor, char *disk) {
  330. char result[FILENAME_MAX + 1] = "";
  331. if(!path_to_device_mapper || !*path_to_device_mapper || !get_disk_name_from_path(path_to_device_mapper, result, FILENAME_MAX + 1, major, minor, disk, NULL, 0))
  332. if(!path_to_device_label || !*path_to_device_label || !get_disk_name_from_path(path_to_device_label, result, FILENAME_MAX + 1, major, minor, disk, NULL, 0))
  333. if(!path_to_veritas_volume_groups || !*path_to_veritas_volume_groups || !get_disk_name_from_path(path_to_veritas_volume_groups, result, FILENAME_MAX + 1, major, minor, disk, "vx", 2))
  334. if(name_disks_by_id != CONFIG_BOOLEAN_YES || !path_to_device_id || !*path_to_device_id || !get_disk_name_from_path(path_to_device_id, result, FILENAME_MAX + 1, major, minor, disk, NULL, 0))
  335. strncpy(result, disk, FILENAME_MAX);
  336. if(!result[0])
  337. strncpy(result, disk, FILENAME_MAX);
  338. netdata_fix_chart_name(result);
  339. return strdup(result);
  340. }
  341. static void get_disk_config(struct disk *d) {
  342. int def_enable = global_enable_new_disks_detected_at_runtime;
  343. if(def_enable != CONFIG_BOOLEAN_NO && (simple_pattern_matches(excluded_disks, d->device) || simple_pattern_matches(excluded_disks, d->disk)))
  344. def_enable = CONFIG_BOOLEAN_NO;
  345. char var_name[4096 + 1];
  346. snprintfz(var_name, 4096, CONFIG_SECTION_PLUGIN_PROC_DISKSTATS ":%s", d->disk);
  347. def_enable = config_get_boolean_ondemand(var_name, "enable", def_enable);
  348. if(unlikely(def_enable == CONFIG_BOOLEAN_NO)) {
  349. // the user does not want any metrics for this disk
  350. d->do_io = CONFIG_BOOLEAN_NO;
  351. d->do_ops = CONFIG_BOOLEAN_NO;
  352. d->do_mops = CONFIG_BOOLEAN_NO;
  353. d->do_iotime = CONFIG_BOOLEAN_NO;
  354. d->do_qops = CONFIG_BOOLEAN_NO;
  355. d->do_util = CONFIG_BOOLEAN_NO;
  356. d->do_backlog = CONFIG_BOOLEAN_NO;
  357. d->do_bcache = CONFIG_BOOLEAN_NO;
  358. }
  359. else {
  360. // this disk is enabled
  361. // check its direct settings
  362. int def_performance = CONFIG_BOOLEAN_AUTO;
  363. // since this is 'on demand' we can figure the performance settings
  364. // based on the type of disk
  365. if(!d->device_is_bcache) {
  366. switch(d->type) {
  367. default:
  368. case DISK_TYPE_UNKNOWN:
  369. break;
  370. case DISK_TYPE_PHYSICAL:
  371. def_performance = global_enable_performance_for_physical_disks;
  372. break;
  373. case DISK_TYPE_PARTITION:
  374. def_performance = global_enable_performance_for_partitions;
  375. break;
  376. case DISK_TYPE_VIRTUAL:
  377. def_performance = global_enable_performance_for_virtual_disks;
  378. break;
  379. }
  380. }
  381. // check if we have to disable performance for this disk
  382. if(def_performance)
  383. def_performance = is_major_enabled((int)d->major);
  384. // ------------------------------------------------------------
  385. // now we have def_performance and def_space
  386. // to work further
  387. // def_performance
  388. // check the user configuration (this will also show our 'on demand' decision)
  389. def_performance = config_get_boolean_ondemand(var_name, "enable performance metrics", def_performance);
  390. int ddo_io = CONFIG_BOOLEAN_NO,
  391. ddo_ops = CONFIG_BOOLEAN_NO,
  392. ddo_mops = CONFIG_BOOLEAN_NO,
  393. ddo_iotime = CONFIG_BOOLEAN_NO,
  394. ddo_qops = CONFIG_BOOLEAN_NO,
  395. ddo_util = CONFIG_BOOLEAN_NO,
  396. ddo_backlog = CONFIG_BOOLEAN_NO,
  397. ddo_bcache = CONFIG_BOOLEAN_NO;
  398. // we enable individual performance charts only when def_performance is not disabled
  399. if(unlikely(def_performance != CONFIG_BOOLEAN_NO)) {
  400. ddo_io = global_do_io,
  401. ddo_ops = global_do_ops,
  402. ddo_mops = global_do_mops,
  403. ddo_iotime = global_do_iotime,
  404. ddo_qops = global_do_qops,
  405. ddo_util = global_do_util,
  406. ddo_backlog = global_do_backlog,
  407. ddo_bcache = global_do_bcache;
  408. }
  409. d->do_io = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
  410. d->do_ops = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
  411. d->do_mops = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
  412. d->do_iotime = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
  413. d->do_qops = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
  414. d->do_util = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
  415. d->do_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
  416. if(d->device_is_bcache)
  417. d->do_bcache = config_get_boolean_ondemand(var_name, "bcache", ddo_bcache);
  418. else
  419. d->do_bcache = 0;
  420. }
  421. }
  422. static struct disk *get_disk(unsigned long major, unsigned long minor, char *disk) {
  423. static struct mountinfo *disk_mountinfo_root = NULL;
  424. struct disk *d;
  425. // search for it in our RAM list.
  426. // this is sequential, but since we just walk through
  427. // and the number of disks / partitions in a system
  428. // should not be that many, it should be acceptable
  429. for(d = disk_root; d ; d = d->next)
  430. if(unlikely(d->major == major && d->minor == minor))
  431. return d;
  432. // not found
  433. // create a new disk structure
  434. d = (struct disk *)callocz(1, sizeof(struct disk));
  435. d->disk = get_disk_name(major, minor, disk);
  436. d->device = strdupz(disk);
  437. d->major = major;
  438. d->minor = minor;
  439. d->type = DISK_TYPE_UNKNOWN; // Default type. Changed later if not correct.
  440. d->sector_size = 512; // the default, will be changed below
  441. d->next = NULL;
  442. // append it to the list
  443. if(unlikely(!disk_root))
  444. disk_root = d;
  445. else {
  446. struct disk *last;
  447. for(last = disk_root; last->next ;last = last->next);
  448. last->next = d;
  449. }
  450. char buffer[FILENAME_MAX + 1];
  451. // find if it is a physical disk
  452. // by checking if /sys/block/DISK is readable.
  453. snprintfz(buffer, FILENAME_MAX, path_to_sys_block_device, disk);
  454. if(likely(access(buffer, R_OK) == 0)) {
  455. // assign it here, but it will be overwritten if it is not a physical disk
  456. d->type = DISK_TYPE_PHYSICAL;
  457. }
  458. // find if it is a partition
  459. // by checking if /sys/dev/block/MAJOR:MINOR/partition is readable.
  460. snprintfz(buffer, FILENAME_MAX, path_to_sys_dev_block_major_minor_string, major, minor, "partition");
  461. if(likely(access(buffer, R_OK) == 0)) {
  462. d->type = DISK_TYPE_PARTITION;
  463. }
  464. else {
  465. // find if it is a virtual disk
  466. // by checking if /sys/devices/virtual/block/DISK is readable.
  467. snprintfz(buffer, FILENAME_MAX, path_to_sys_devices_virtual_block_device, disk);
  468. if(likely(access(buffer, R_OK) == 0)) {
  469. d->type = DISK_TYPE_VIRTUAL;
  470. }
  471. else {
  472. // find if it is a virtual device
  473. // by checking if /sys/dev/block/MAJOR:MINOR/slaves has entries
  474. snprintfz(buffer, FILENAME_MAX, path_to_sys_dev_block_major_minor_string, major, minor, "slaves/");
  475. DIR *dirp = opendir(buffer);
  476. if (likely(dirp != NULL)) {
  477. struct dirent *dp;
  478. while ((dp = readdir(dirp))) {
  479. // . and .. are also files in empty folders.
  480. if (unlikely(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)) {
  481. continue;
  482. }
  483. d->type = DISK_TYPE_VIRTUAL;
  484. // Stop the loop after we found one file.
  485. break;
  486. }
  487. if (unlikely(closedir(dirp) == -1))
  488. error("Unable to close dir %s", buffer);
  489. }
  490. }
  491. }
  492. // ------------------------------------------------------------------------
  493. // check if we can find its mount point
  494. // mountinfo_find() can be called with NULL disk_mountinfo_root
  495. struct mountinfo *mi = mountinfo_find(disk_mountinfo_root, d->major, d->minor);
  496. if(unlikely(!mi)) {
  497. // mountinfo_free_all can be called with NULL
  498. mountinfo_free_all(disk_mountinfo_root);
  499. disk_mountinfo_root = mountinfo_read(0);
  500. mi = mountinfo_find(disk_mountinfo_root, d->major, d->minor);
  501. }
  502. if(unlikely(mi))
  503. d->mount_point = strdupz(mi->mount_point);
  504. else
  505. d->mount_point = NULL;
  506. // ------------------------------------------------------------------------
  507. // find the disk sector size
  508. /*
  509. * sector size is always 512 bytes inside the kernel #3481
  510. *
  511. {
  512. char tf[FILENAME_MAX + 1], *t;
  513. strncpyz(tf, d->device, FILENAME_MAX);
  514. // replace all / with !
  515. for(t = tf; *t ;t++)
  516. if(unlikely(*t == '/')) *t = '!';
  517. if(likely(d->type == DISK_TYPE_PARTITION))
  518. snprintfz(buffer, FILENAME_MAX, path_to_get_hw_sector_size_partitions, d->major, d->minor, tf);
  519. else
  520. snprintfz(buffer, FILENAME_MAX, path_to_get_hw_sector_size, tf);
  521. FILE *fpss = fopen(buffer, "r");
  522. if(likely(fpss)) {
  523. char buffer2[1024 + 1];
  524. char *tmp = fgets(buffer2, 1024, fpss);
  525. if(likely(tmp)) {
  526. d->sector_size = str2i(tmp);
  527. if(unlikely(d->sector_size <= 0)) {
  528. error("Invalid sector size %d for device %s in %s. Assuming 512.", d->sector_size, d->device, buffer);
  529. d->sector_size = 512;
  530. }
  531. }
  532. else error("Cannot read data for sector size for device %s from %s. Assuming 512.", d->device, buffer);
  533. fclose(fpss);
  534. }
  535. else error("Cannot read sector size for device %s from %s. Assuming 512.", d->device, buffer);
  536. }
  537. */
  538. // ------------------------------------------------------------------------
  539. // check if the device is a bcache
  540. struct stat bcache;
  541. snprintfz(buffer, FILENAME_MAX, path_to_sys_block_device_bcache, disk);
  542. if(unlikely(stat(buffer, &bcache) == 0 && (bcache.st_mode & S_IFMT) == S_IFDIR)) {
  543. // we have the 'bcache' directory
  544. d->device_is_bcache = 1;
  545. char buffer2[FILENAME_MAX + 1];
  546. snprintfz(buffer2, FILENAME_MAX, "%s/cache/congested", buffer);
  547. if(access(buffer2, R_OK) == 0)
  548. d->bcache_filename_cache_congested = strdupz(buffer2);
  549. else
  550. error("bcache file '%s' cannot be read.", buffer2);
  551. snprintfz(buffer2, FILENAME_MAX, "%s/readahead", buffer);
  552. if(access(buffer2, R_OK) == 0)
  553. d->bcache_filename_stats_total_cache_readaheads = strdupz(buffer2);
  554. else
  555. error("bcache file '%s' cannot be read.", buffer2);
  556. snprintfz(buffer2, FILENAME_MAX, "%s/cache/cache0/priority_stats", buffer); // only one cache is supported by bcache
  557. if(access(buffer2, R_OK) == 0)
  558. d->bcache_filename_priority_stats = strdupz(buffer2);
  559. else
  560. error("bcache file '%s' cannot be read.", buffer2);
  561. snprintfz(buffer2, FILENAME_MAX, "%s/cache/internal/cache_read_races", buffer);
  562. if(access(buffer2, R_OK) == 0)
  563. d->bcache_filename_cache_read_races = strdupz(buffer2);
  564. else
  565. error("bcache file '%s' cannot be read.", buffer2);
  566. snprintfz(buffer2, FILENAME_MAX, "%s/cache/cache0/io_errors", buffer);
  567. if(access(buffer2, R_OK) == 0)
  568. d->bcache_filename_cache_io_errors = strdupz(buffer2);
  569. else
  570. error("bcache file '%s' cannot be read.", buffer2);
  571. snprintfz(buffer2, FILENAME_MAX, "%s/dirty_data", buffer);
  572. if(access(buffer2, R_OK) == 0)
  573. d->bcache_filename_dirty_data = strdupz(buffer2);
  574. else
  575. error("bcache file '%s' cannot be read.", buffer2);
  576. snprintfz(buffer2, FILENAME_MAX, "%s/writeback_rate", buffer);
  577. if(access(buffer2, R_OK) == 0)
  578. d->bcache_filename_writeback_rate = strdupz(buffer2);
  579. else
  580. error("bcache file '%s' cannot be read.", buffer2);
  581. snprintfz(buffer2, FILENAME_MAX, "%s/cache/cache_available_percent", buffer);
  582. if(access(buffer2, R_OK) == 0)
  583. d->bcache_filename_cache_available_percent = strdupz(buffer2);
  584. else
  585. error("bcache file '%s' cannot be read.", buffer2);
  586. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_hits", buffer);
  587. if(access(buffer2, R_OK) == 0)
  588. d->bcache_filename_stats_total_cache_hits = strdupz(buffer2);
  589. else
  590. error("bcache file '%s' cannot be read.", buffer2);
  591. snprintfz(buffer2, FILENAME_MAX, "%s/stats_five_minute/cache_hit_ratio", buffer);
  592. if(access(buffer2, R_OK) == 0)
  593. d->bcache_filename_stats_five_minute_cache_hit_ratio = strdupz(buffer2);
  594. else
  595. error("bcache file '%s' cannot be read.", buffer2);
  596. snprintfz(buffer2, FILENAME_MAX, "%s/stats_hour/cache_hit_ratio", buffer);
  597. if(access(buffer2, R_OK) == 0)
  598. d->bcache_filename_stats_hour_cache_hit_ratio = strdupz(buffer2);
  599. else
  600. error("bcache file '%s' cannot be read.", buffer2);
  601. snprintfz(buffer2, FILENAME_MAX, "%s/stats_day/cache_hit_ratio", buffer);
  602. if(access(buffer2, R_OK) == 0)
  603. d->bcache_filename_stats_day_cache_hit_ratio = strdupz(buffer2);
  604. else
  605. error("bcache file '%s' cannot be read.", buffer2);
  606. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_hit_ratio", buffer);
  607. if(access(buffer2, R_OK) == 0)
  608. d->bcache_filename_stats_total_cache_hit_ratio = strdupz(buffer2);
  609. else
  610. error("bcache file '%s' cannot be read.", buffer2);
  611. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_misses", buffer);
  612. if(access(buffer2, R_OK) == 0)
  613. d->bcache_filename_stats_total_cache_misses = strdupz(buffer2);
  614. else
  615. error("bcache file '%s' cannot be read.", buffer2);
  616. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_bypass_hits", buffer);
  617. if(access(buffer2, R_OK) == 0)
  618. d->bcache_filename_stats_total_cache_bypass_hits = strdupz(buffer2);
  619. else
  620. error("bcache file '%s' cannot be read.", buffer2);
  621. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_bypass_misses", buffer);
  622. if(access(buffer2, R_OK) == 0)
  623. d->bcache_filename_stats_total_cache_bypass_misses = strdupz(buffer2);
  624. else
  625. error("bcache file '%s' cannot be read.", buffer2);
  626. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_miss_collisions", buffer);
  627. if(access(buffer2, R_OK) == 0)
  628. d->bcache_filename_stats_total_cache_miss_collisions = strdupz(buffer2);
  629. else
  630. error("bcache file '%s' cannot be read.", buffer2);
  631. }
  632. get_disk_config(d);
  633. return d;
  634. }
  635. int do_proc_diskstats(int update_every, usec_t dt) {
  636. static procfile *ff = NULL;
  637. if(unlikely(!globals_initialized)) {
  638. globals_initialized = 1;
  639. global_enable_new_disks_detected_at_runtime = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "enable new disks detected at runtime", global_enable_new_disks_detected_at_runtime);
  640. global_enable_performance_for_physical_disks = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "performance metrics for physical disks", global_enable_performance_for_physical_disks);
  641. global_enable_performance_for_virtual_disks = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "performance metrics for virtual disks", global_enable_performance_for_virtual_disks);
  642. global_enable_performance_for_partitions = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "performance metrics for partitions", global_enable_performance_for_partitions);
  643. global_do_io = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "bandwidth for all disks", global_do_io);
  644. global_do_ops = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "operations for all disks", global_do_ops);
  645. global_do_mops = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "merged operations for all disks", global_do_mops);
  646. global_do_iotime = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "i/o time for all disks", global_do_iotime);
  647. global_do_qops = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "queued operations for all disks", global_do_qops);
  648. global_do_util = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "utilization percentage for all disks", global_do_util);
  649. global_do_backlog = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "backlog for all disks", global_do_backlog);
  650. global_do_bcache = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "bcache for all disks", global_do_bcache);
  651. global_bcache_priority_stats_update_every = (int)config_get_number(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "bcache priority stats update every", global_bcache_priority_stats_update_every);
  652. global_cleanup_removed_disks = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "remove charts of removed disks" , global_cleanup_removed_disks);
  653. char buffer[FILENAME_MAX + 1];
  654. snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s");
  655. path_to_sys_block_device = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get block device", buffer);
  656. snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s/bcache");
  657. path_to_sys_block_device_bcache = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get block device bcache", buffer);
  658. snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/virtual/block/%s");
  659. path_to_sys_devices_virtual_block_device = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get virtual block device", buffer);
  660. snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/%s");
  661. path_to_sys_dev_block_major_minor_string = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get block device infos", buffer);
  662. //snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s/queue/hw_sector_size");
  663. //path_to_get_hw_sector_size = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get h/w sector size", buffer);
  664. //snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/subsystem/%s/../queue/hw_sector_size");
  665. //path_to_get_hw_sector_size_partitions = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get h/w sector size for partitions", buffer);
  666. snprintfz(buffer, FILENAME_MAX, "%s/dev/mapper", netdata_configured_host_prefix);
  667. path_to_device_mapper = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to device mapper", buffer);
  668. snprintfz(buffer, FILENAME_MAX, "%s/dev/disk/by-label", netdata_configured_host_prefix);
  669. path_to_device_label = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/disk/by-label", buffer);
  670. snprintfz(buffer, FILENAME_MAX, "%s/dev/disk/by-id", netdata_configured_host_prefix);
  671. path_to_device_id = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/disk/by-id", buffer);
  672. snprintfz(buffer, FILENAME_MAX, "%s/dev/vx/dsk", netdata_configured_host_prefix);
  673. path_to_veritas_volume_groups = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/vx/dsk", buffer);
  674. name_disks_by_id = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "name disks by id", name_disks_by_id);
  675. excluded_disks = simple_pattern_create(
  676. config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "exclude disks", DEFAULT_EXCLUDED_DISKS)
  677. , NULL
  678. , SIMPLE_PATTERN_EXACT
  679. );
  680. }
  681. // --------------------------------------------------------------------------
  682. if(unlikely(!ff)) {
  683. char filename[FILENAME_MAX + 1];
  684. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/diskstats");
  685. ff = procfile_open(config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
  686. }
  687. if(unlikely(!ff)) return 0;
  688. ff = procfile_readall(ff);
  689. if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
  690. size_t lines = procfile_lines(ff), l;
  691. collected_number system_read_kb = 0, system_write_kb = 0;
  692. for(l = 0; l < lines ;l++) {
  693. // --------------------------------------------------------------------------
  694. // Read parameters
  695. char *disk;
  696. unsigned long major = 0, minor = 0;
  697. collected_number reads = 0, mreads = 0, readsectors = 0, readms = 0,
  698. writes = 0, mwrites = 0, writesectors = 0, writems = 0,
  699. queued_ios = 0, busy_ms = 0, backlog_ms = 0;
  700. collected_number last_reads = 0, last_readsectors = 0, last_readms = 0,
  701. last_writes = 0, last_writesectors = 0, last_writems = 0,
  702. last_busy_ms = 0;
  703. size_t words = procfile_linewords(ff, l);
  704. if(unlikely(words < 14)) continue;
  705. major = str2ul(procfile_lineword(ff, l, 0));
  706. minor = str2ul(procfile_lineword(ff, l, 1));
  707. disk = procfile_lineword(ff, l, 2);
  708. // # of reads completed # of writes completed
  709. // This is the total number of reads or writes completed successfully.
  710. reads = str2ull(procfile_lineword(ff, l, 3)); // rd_ios
  711. writes = str2ull(procfile_lineword(ff, l, 7)); // wr_ios
  712. // # of reads merged # of writes merged
  713. // Reads and writes which are adjacent to each other may be merged for
  714. // efficiency. Thus two 4K reads may become one 8K read before it is
  715. // ultimately handed to the disk, and so it will be counted (and queued)
  716. mreads = str2ull(procfile_lineword(ff, l, 4)); // rd_merges_or_rd_sec
  717. mwrites = str2ull(procfile_lineword(ff, l, 8)); // wr_merges
  718. // # of sectors read # of sectors written
  719. // This is the total number of sectors read or written successfully.
  720. readsectors = str2ull(procfile_lineword(ff, l, 5)); // rd_sec_or_wr_ios
  721. writesectors = str2ull(procfile_lineword(ff, l, 9)); // wr_sec
  722. // # of milliseconds spent reading # of milliseconds spent writing
  723. // This is the total number of milliseconds spent by all reads or writes (as
  724. // measured from __make_request() to end_that_request_last()).
  725. readms = str2ull(procfile_lineword(ff, l, 6)); // rd_ticks_or_wr_sec
  726. writems = str2ull(procfile_lineword(ff, l, 10)); // wr_ticks
  727. // # of I/Os currently in progress
  728. // The only field that should go to zero. Incremented as requests are
  729. // given to appropriate struct request_queue and decremented as they finish.
  730. queued_ios = str2ull(procfile_lineword(ff, l, 11)); // ios_pgr
  731. // # of milliseconds spent doing I/Os
  732. // This field increases so long as field queued_ios is nonzero.
  733. busy_ms = str2ull(procfile_lineword(ff, l, 12)); // tot_ticks
  734. // weighted # of milliseconds spent doing I/Os
  735. // This field is incremented at each I/O start, I/O completion, I/O
  736. // merge, or read of these stats by the number of I/Os in progress
  737. // (field queued_ios) times the number of milliseconds spent doing I/O since the
  738. // last update of this field. This can provide an easy measure of both
  739. // I/O completion time and the backlog that may be accumulating.
  740. backlog_ms = str2ull(procfile_lineword(ff, l, 13)); // rq_ticks
  741. // --------------------------------------------------------------------------
  742. // remove slashes from disk names
  743. char *s;
  744. for(s = disk; *s ;s++)
  745. if(*s == '/') *s = '_';
  746. // --------------------------------------------------------------------------
  747. // get a disk structure for the disk
  748. struct disk *d = get_disk(major, minor, disk);
  749. d->updated = 1;
  750. // --------------------------------------------------------------------------
  751. // count the global system disk I/O of physical disks
  752. if(unlikely(d->type == DISK_TYPE_PHYSICAL)) {
  753. system_read_kb += readsectors * d->sector_size / 1024;
  754. system_write_kb += writesectors * d->sector_size / 1024;
  755. }
  756. // --------------------------------------------------------------------------
  757. // Set its family based on mount point
  758. char *family = d->mount_point;
  759. if(!family) family = d->disk;
  760. // --------------------------------------------------------------------------
  761. // Do performance metrics
  762. if(d->do_io == CONFIG_BOOLEAN_YES || (d->do_io == CONFIG_BOOLEAN_AUTO && (readsectors || writesectors))) {
  763. d->do_io = CONFIG_BOOLEAN_YES;
  764. if(unlikely(!d->st_io)) {
  765. d->st_io = rrdset_create_localhost(
  766. RRD_TYPE_DISK
  767. , d->device
  768. , d->disk
  769. , family
  770. , "disk.io"
  771. , "Disk I/O Bandwidth"
  772. , "KiB/s"
  773. , PLUGIN_PROC_NAME
  774. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  775. , NETDATA_CHART_PRIO_DISK_IO
  776. , update_every
  777. , RRDSET_TYPE_AREA
  778. );
  779. d->rd_io_reads = rrddim_add(d->st_io, "reads", NULL, d->sector_size, 1024, RRD_ALGORITHM_INCREMENTAL);
  780. d->rd_io_writes = rrddim_add(d->st_io, "writes", NULL, d->sector_size * -1, 1024, RRD_ALGORITHM_INCREMENTAL);
  781. }
  782. else rrdset_next(d->st_io);
  783. last_readsectors = rrddim_set_by_pointer(d->st_io, d->rd_io_reads, readsectors);
  784. last_writesectors = rrddim_set_by_pointer(d->st_io, d->rd_io_writes, writesectors);
  785. rrdset_done(d->st_io);
  786. }
  787. // --------------------------------------------------------------------
  788. if(d->do_ops == CONFIG_BOOLEAN_YES || (d->do_ops == CONFIG_BOOLEAN_AUTO && (reads || writes))) {
  789. d->do_ops = CONFIG_BOOLEAN_YES;
  790. if(unlikely(!d->st_ops)) {
  791. d->st_ops = rrdset_create_localhost(
  792. "disk_ops"
  793. , d->device
  794. , d->disk
  795. , family
  796. , "disk.ops"
  797. , "Disk Completed I/O Operations"
  798. , "operations/s"
  799. , PLUGIN_PROC_NAME
  800. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  801. , NETDATA_CHART_PRIO_DISK_OPS
  802. , update_every
  803. , RRDSET_TYPE_LINE
  804. );
  805. rrdset_flag_set(d->st_ops, RRDSET_FLAG_DETAIL);
  806. d->rd_ops_reads = rrddim_add(d->st_ops, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  807. d->rd_ops_writes = rrddim_add(d->st_ops, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  808. }
  809. else rrdset_next(d->st_ops);
  810. last_reads = rrddim_set_by_pointer(d->st_ops, d->rd_ops_reads, reads);
  811. last_writes = rrddim_set_by_pointer(d->st_ops, d->rd_ops_writes, writes);
  812. rrdset_done(d->st_ops);
  813. }
  814. // --------------------------------------------------------------------
  815. if(d->do_qops == CONFIG_BOOLEAN_YES || (d->do_qops == CONFIG_BOOLEAN_AUTO && queued_ios)) {
  816. d->do_qops = CONFIG_BOOLEAN_YES;
  817. if(unlikely(!d->st_qops)) {
  818. d->st_qops = rrdset_create_localhost(
  819. "disk_qops"
  820. , d->device
  821. , d->disk
  822. , family
  823. , "disk.qops"
  824. , "Disk Current I/O Operations"
  825. , "operations"
  826. , PLUGIN_PROC_NAME
  827. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  828. , NETDATA_CHART_PRIO_DISK_QOPS
  829. , update_every
  830. , RRDSET_TYPE_LINE
  831. );
  832. rrdset_flag_set(d->st_qops, RRDSET_FLAG_DETAIL);
  833. d->rd_qops_operations = rrddim_add(d->st_qops, "operations", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  834. }
  835. else rrdset_next(d->st_qops);
  836. rrddim_set_by_pointer(d->st_qops, d->rd_qops_operations, queued_ios);
  837. rrdset_done(d->st_qops);
  838. }
  839. // --------------------------------------------------------------------
  840. if(d->do_backlog == CONFIG_BOOLEAN_YES || (d->do_backlog == CONFIG_BOOLEAN_AUTO && backlog_ms)) {
  841. d->do_backlog = CONFIG_BOOLEAN_YES;
  842. if(unlikely(!d->st_backlog)) {
  843. d->st_backlog = rrdset_create_localhost(
  844. "disk_backlog"
  845. , d->device
  846. , d->disk
  847. , family
  848. , "disk.backlog"
  849. , "Disk Backlog"
  850. , "milliseconds"
  851. , PLUGIN_PROC_NAME
  852. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  853. , NETDATA_CHART_PRIO_DISK_BACKLOG
  854. , update_every
  855. , RRDSET_TYPE_AREA
  856. );
  857. rrdset_flag_set(d->st_backlog, RRDSET_FLAG_DETAIL);
  858. d->rd_backlog_backlog = rrddim_add(d->st_backlog, "backlog", NULL, 1, 10, RRD_ALGORITHM_INCREMENTAL);
  859. }
  860. else rrdset_next(d->st_backlog);
  861. rrddim_set_by_pointer(d->st_backlog, d->rd_backlog_backlog, backlog_ms);
  862. rrdset_done(d->st_backlog);
  863. }
  864. // --------------------------------------------------------------------
  865. if(d->do_util == CONFIG_BOOLEAN_YES || (d->do_util == CONFIG_BOOLEAN_AUTO && busy_ms)) {
  866. d->do_util = CONFIG_BOOLEAN_YES;
  867. if(unlikely(!d->st_util)) {
  868. d->st_util = rrdset_create_localhost(
  869. "disk_util"
  870. , d->device
  871. , d->disk
  872. , family
  873. , "disk.util"
  874. , "Disk Utilization Time"
  875. , "% of time working"
  876. , PLUGIN_PROC_NAME
  877. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  878. , NETDATA_CHART_PRIO_DISK_UTIL
  879. , update_every
  880. , RRDSET_TYPE_AREA
  881. );
  882. rrdset_flag_set(d->st_util, RRDSET_FLAG_DETAIL);
  883. d->rd_util_utilization = rrddim_add(d->st_util, "utilization", NULL, 1, 10, RRD_ALGORITHM_INCREMENTAL);
  884. }
  885. else rrdset_next(d->st_util);
  886. last_busy_ms = rrddim_set_by_pointer(d->st_util, d->rd_util_utilization, busy_ms);
  887. rrdset_done(d->st_util);
  888. }
  889. // --------------------------------------------------------------------
  890. if(d->do_mops == CONFIG_BOOLEAN_YES || (d->do_mops == CONFIG_BOOLEAN_AUTO && (mreads || mwrites))) {
  891. d->do_mops = CONFIG_BOOLEAN_YES;
  892. if(unlikely(!d->st_mops)) {
  893. d->st_mops = rrdset_create_localhost(
  894. "disk_mops"
  895. , d->device
  896. , d->disk
  897. , family
  898. , "disk.mops"
  899. , "Disk Merged Operations"
  900. , "merged operations/s"
  901. , PLUGIN_PROC_NAME
  902. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  903. , NETDATA_CHART_PRIO_DISK_MOPS
  904. , update_every
  905. , RRDSET_TYPE_LINE
  906. );
  907. rrdset_flag_set(d->st_mops, RRDSET_FLAG_DETAIL);
  908. d->rd_mops_reads = rrddim_add(d->st_mops, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  909. d->rd_mops_writes = rrddim_add(d->st_mops, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  910. }
  911. else rrdset_next(d->st_mops);
  912. rrddim_set_by_pointer(d->st_mops, d->rd_mops_reads, mreads);
  913. rrddim_set_by_pointer(d->st_mops, d->rd_mops_writes, mwrites);
  914. rrdset_done(d->st_mops);
  915. }
  916. // --------------------------------------------------------------------
  917. if(d->do_iotime == CONFIG_BOOLEAN_YES || (d->do_iotime == CONFIG_BOOLEAN_AUTO && (readms || writems))) {
  918. d->do_iotime = CONFIG_BOOLEAN_YES;
  919. if(unlikely(!d->st_iotime)) {
  920. d->st_iotime = rrdset_create_localhost(
  921. "disk_iotime"
  922. , d->device
  923. , d->disk
  924. , family
  925. , "disk.iotime"
  926. , "Disk Total I/O Time"
  927. , "milliseconds/s"
  928. , PLUGIN_PROC_NAME
  929. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  930. , NETDATA_CHART_PRIO_DISK_IOTIME
  931. , update_every
  932. , RRDSET_TYPE_LINE
  933. );
  934. rrdset_flag_set(d->st_iotime, RRDSET_FLAG_DETAIL);
  935. d->rd_iotime_reads = rrddim_add(d->st_iotime, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  936. d->rd_iotime_writes = rrddim_add(d->st_iotime, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  937. }
  938. else rrdset_next(d->st_iotime);
  939. last_readms = rrddim_set_by_pointer(d->st_iotime, d->rd_iotime_reads, readms);
  940. last_writems = rrddim_set_by_pointer(d->st_iotime, d->rd_iotime_writes, writems);
  941. rrdset_done(d->st_iotime);
  942. }
  943. // --------------------------------------------------------------------
  944. // calculate differential charts
  945. // only if this is not the first time we run
  946. if(likely(dt)) {
  947. if( (d->do_iotime == CONFIG_BOOLEAN_YES || (d->do_iotime == CONFIG_BOOLEAN_AUTO && (readms || writems))) &&
  948. (d->do_ops == CONFIG_BOOLEAN_YES || (d->do_ops == CONFIG_BOOLEAN_AUTO && (reads || writes)))) {
  949. if(unlikely(!d->st_await)) {
  950. d->st_await = rrdset_create_localhost(
  951. "disk_await"
  952. , d->device
  953. , d->disk
  954. , family
  955. , "disk.await"
  956. , "Average Completed I/O Operation Time"
  957. , "milliseconds/operation"
  958. , PLUGIN_PROC_NAME
  959. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  960. , NETDATA_CHART_PRIO_DISK_AWAIT
  961. , update_every
  962. , RRDSET_TYPE_LINE
  963. );
  964. rrdset_flag_set(d->st_await, RRDSET_FLAG_DETAIL);
  965. d->rd_await_reads = rrddim_add(d->st_await, "reads", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  966. d->rd_await_writes = rrddim_add(d->st_await, "writes", NULL, -1, 1, RRD_ALGORITHM_ABSOLUTE);
  967. }
  968. else rrdset_next(d->st_await);
  969. rrddim_set_by_pointer(d->st_await, d->rd_await_reads, (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
  970. rrddim_set_by_pointer(d->st_await, d->rd_await_writes, (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
  971. rrdset_done(d->st_await);
  972. }
  973. if( (d->do_io == CONFIG_BOOLEAN_YES || (d->do_io == CONFIG_BOOLEAN_AUTO && (readsectors || writesectors))) &&
  974. (d->do_ops == CONFIG_BOOLEAN_YES || (d->do_ops == CONFIG_BOOLEAN_AUTO && (reads || writes)))) {
  975. if(unlikely(!d->st_avgsz)) {
  976. d->st_avgsz = rrdset_create_localhost(
  977. "disk_avgsz"
  978. , d->device
  979. , d->disk
  980. , family
  981. , "disk.avgsz"
  982. , "Average Completed I/O Operation Bandwidth"
  983. , "KiB/operation"
  984. , PLUGIN_PROC_NAME
  985. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  986. , NETDATA_CHART_PRIO_DISK_AVGSZ
  987. , update_every
  988. , RRDSET_TYPE_AREA
  989. );
  990. rrdset_flag_set(d->st_avgsz, RRDSET_FLAG_DETAIL);
  991. d->rd_avgsz_reads = rrddim_add(d->st_avgsz, "reads", NULL, d->sector_size, 1024, RRD_ALGORITHM_ABSOLUTE);
  992. d->rd_avgsz_writes = rrddim_add(d->st_avgsz, "writes", NULL, d->sector_size * -1, 1024, RRD_ALGORITHM_ABSOLUTE);
  993. }
  994. else rrdset_next(d->st_avgsz);
  995. rrddim_set_by_pointer(d->st_avgsz, d->rd_avgsz_reads, (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
  996. rrddim_set_by_pointer(d->st_avgsz, d->rd_avgsz_writes, (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
  997. rrdset_done(d->st_avgsz);
  998. }
  999. if( (d->do_util == CONFIG_BOOLEAN_YES || (d->do_util == CONFIG_BOOLEAN_AUTO && busy_ms)) &&
  1000. (d->do_ops == CONFIG_BOOLEAN_YES || (d->do_ops == CONFIG_BOOLEAN_AUTO && (reads || writes)))) {
  1001. if(unlikely(!d->st_svctm)) {
  1002. d->st_svctm = rrdset_create_localhost(
  1003. "disk_svctm"
  1004. , d->device
  1005. , d->disk
  1006. , family
  1007. , "disk.svctm"
  1008. , "Average Service Time"
  1009. , "milliseconds/operation"
  1010. , PLUGIN_PROC_NAME
  1011. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1012. , NETDATA_CHART_PRIO_DISK_SVCTM
  1013. , update_every
  1014. , RRDSET_TYPE_LINE
  1015. );
  1016. rrdset_flag_set(d->st_svctm, RRDSET_FLAG_DETAIL);
  1017. d->rd_svctm_svctm = rrddim_add(d->st_svctm, "svctm", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1018. }
  1019. else rrdset_next(d->st_svctm);
  1020. rrddim_set_by_pointer(d->st_svctm, d->rd_svctm_svctm, ((reads - last_reads) + (writes - last_writes)) ? (busy_ms - last_busy_ms) / ((reads - last_reads) + (writes - last_writes)) : 0);
  1021. rrdset_done(d->st_svctm);
  1022. }
  1023. }
  1024. // --------------------------------------------------------------------------
  1025. // read bcache metrics and generate the bcache charts
  1026. if(d->device_is_bcache && d->do_bcache != CONFIG_BOOLEAN_NO) {
  1027. unsigned long long int
  1028. stats_total_cache_bypass_hits = 0,
  1029. stats_total_cache_bypass_misses = 0,
  1030. stats_total_cache_hits = 0,
  1031. stats_total_cache_miss_collisions = 0,
  1032. stats_total_cache_misses = 0,
  1033. stats_five_minute_cache_hit_ratio = 0,
  1034. stats_hour_cache_hit_ratio = 0,
  1035. stats_day_cache_hit_ratio = 0,
  1036. stats_total_cache_hit_ratio = 0,
  1037. cache_available_percent = 0,
  1038. cache_readaheads = 0,
  1039. cache_read_races = 0,
  1040. cache_io_errors = 0,
  1041. cache_congested = 0,
  1042. dirty_data = 0,
  1043. writeback_rate = 0;
  1044. // read the bcache values
  1045. if(d->bcache_filename_dirty_data)
  1046. dirty_data = bcache_read_number_with_units(d->bcache_filename_dirty_data);
  1047. if(d->bcache_filename_writeback_rate)
  1048. writeback_rate = bcache_read_number_with_units(d->bcache_filename_writeback_rate);
  1049. if(d->bcache_filename_cache_congested)
  1050. cache_congested = bcache_read_number_with_units(d->bcache_filename_cache_congested);
  1051. if(d->bcache_filename_cache_available_percent)
  1052. read_single_number_file(d->bcache_filename_cache_available_percent, &cache_available_percent);
  1053. if(d->bcache_filename_stats_five_minute_cache_hit_ratio)
  1054. read_single_number_file(d->bcache_filename_stats_five_minute_cache_hit_ratio, &stats_five_minute_cache_hit_ratio);
  1055. if(d->bcache_filename_stats_hour_cache_hit_ratio)
  1056. read_single_number_file(d->bcache_filename_stats_hour_cache_hit_ratio, &stats_hour_cache_hit_ratio);
  1057. if(d->bcache_filename_stats_day_cache_hit_ratio)
  1058. read_single_number_file(d->bcache_filename_stats_day_cache_hit_ratio, &stats_day_cache_hit_ratio);
  1059. if(d->bcache_filename_stats_total_cache_hit_ratio)
  1060. read_single_number_file(d->bcache_filename_stats_total_cache_hit_ratio, &stats_total_cache_hit_ratio);
  1061. if(d->bcache_filename_stats_total_cache_hits)
  1062. read_single_number_file(d->bcache_filename_stats_total_cache_hits, &stats_total_cache_hits);
  1063. if(d->bcache_filename_stats_total_cache_misses)
  1064. read_single_number_file(d->bcache_filename_stats_total_cache_misses, &stats_total_cache_misses);
  1065. if(d->bcache_filename_stats_total_cache_miss_collisions)
  1066. read_single_number_file(d->bcache_filename_stats_total_cache_miss_collisions, &stats_total_cache_miss_collisions);
  1067. if(d->bcache_filename_stats_total_cache_bypass_hits)
  1068. read_single_number_file(d->bcache_filename_stats_total_cache_bypass_hits, &stats_total_cache_bypass_hits);
  1069. if(d->bcache_filename_stats_total_cache_bypass_misses)
  1070. read_single_number_file(d->bcache_filename_stats_total_cache_bypass_misses, &stats_total_cache_bypass_misses);
  1071. if(d->bcache_filename_stats_total_cache_readaheads)
  1072. cache_readaheads = bcache_read_number_with_units(d->bcache_filename_stats_total_cache_readaheads);
  1073. if(d->bcache_filename_cache_read_races)
  1074. read_single_number_file(d->bcache_filename_cache_read_races, &cache_read_races);
  1075. if(d->bcache_filename_cache_io_errors)
  1076. read_single_number_file(d->bcache_filename_cache_io_errors, &cache_io_errors);
  1077. if(d->bcache_filename_priority_stats && global_bcache_priority_stats_update_every >= 1)
  1078. bcache_read_priority_stats(d, family, global_bcache_priority_stats_update_every, dt);
  1079. // update the charts
  1080. {
  1081. if(unlikely(!d->st_bcache_hit_ratio)) {
  1082. d->st_bcache_hit_ratio = rrdset_create_localhost(
  1083. "disk_bcache_hit_ratio"
  1084. , d->device
  1085. , d->disk
  1086. , family
  1087. , "disk.bcache_hit_ratio"
  1088. , "BCache Cache Hit Ratio"
  1089. , "percentage"
  1090. , PLUGIN_PROC_NAME
  1091. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1092. , NETDATA_CHART_PRIO_BCACHE_HIT_RATIO
  1093. , update_every
  1094. , RRDSET_TYPE_LINE
  1095. );
  1096. d->rd_bcache_hit_ratio_5min = rrddim_add(d->st_bcache_hit_ratio, "5min", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1097. d->rd_bcache_hit_ratio_1hour = rrddim_add(d->st_bcache_hit_ratio, "1hour", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1098. d->rd_bcache_hit_ratio_1day = rrddim_add(d->st_bcache_hit_ratio, "1day", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1099. d->rd_bcache_hit_ratio_total = rrddim_add(d->st_bcache_hit_ratio, "ever", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1100. }
  1101. else rrdset_next(d->st_bcache_hit_ratio);
  1102. rrddim_set_by_pointer(d->st_bcache_hit_ratio, d->rd_bcache_hit_ratio_5min, stats_five_minute_cache_hit_ratio);
  1103. rrddim_set_by_pointer(d->st_bcache_hit_ratio, d->rd_bcache_hit_ratio_1hour, stats_hour_cache_hit_ratio);
  1104. rrddim_set_by_pointer(d->st_bcache_hit_ratio, d->rd_bcache_hit_ratio_1day, stats_day_cache_hit_ratio);
  1105. rrddim_set_by_pointer(d->st_bcache_hit_ratio, d->rd_bcache_hit_ratio_total, stats_total_cache_hit_ratio);
  1106. rrdset_done(d->st_bcache_hit_ratio);
  1107. }
  1108. {
  1109. if(unlikely(!d->st_bcache_rates)) {
  1110. d->st_bcache_rates = rrdset_create_localhost(
  1111. "disk_bcache_rates"
  1112. , d->device
  1113. , d->disk
  1114. , family
  1115. , "disk.bcache_rates"
  1116. , "BCache Rates"
  1117. , "KiB/s"
  1118. , PLUGIN_PROC_NAME
  1119. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1120. , NETDATA_CHART_PRIO_BCACHE_RATES
  1121. , update_every
  1122. , RRDSET_TYPE_AREA
  1123. );
  1124. d->rd_bcache_rate_congested = rrddim_add(d->st_bcache_rates, "congested", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
  1125. d->rd_bcache_rate_writeback = rrddim_add(d->st_bcache_rates, "writeback", NULL, -1, 1024, RRD_ALGORITHM_ABSOLUTE);
  1126. }
  1127. else rrdset_next(d->st_bcache_rates);
  1128. rrddim_set_by_pointer(d->st_bcache_rates, d->rd_bcache_rate_writeback, writeback_rate);
  1129. rrddim_set_by_pointer(d->st_bcache_rates, d->rd_bcache_rate_congested, cache_congested);
  1130. rrdset_done(d->st_bcache_rates);
  1131. }
  1132. {
  1133. if(unlikely(!d->st_bcache_size)) {
  1134. d->st_bcache_size = rrdset_create_localhost(
  1135. "disk_bcache_size"
  1136. , d->device
  1137. , d->disk
  1138. , family
  1139. , "disk.bcache_size"
  1140. , "BCache Cache Sizes"
  1141. , "MiB"
  1142. , PLUGIN_PROC_NAME
  1143. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1144. , NETDATA_CHART_PRIO_BCACHE_SIZE
  1145. , update_every
  1146. , RRDSET_TYPE_AREA
  1147. );
  1148. d->rd_bcache_dirty_size = rrddim_add(d->st_bcache_size, "dirty", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  1149. }
  1150. else rrdset_next(d->st_bcache_size);
  1151. rrddim_set_by_pointer(d->st_bcache_size, d->rd_bcache_dirty_size, dirty_data);
  1152. rrdset_done(d->st_bcache_size);
  1153. }
  1154. {
  1155. if(unlikely(!d->st_bcache_usage)) {
  1156. d->st_bcache_usage = rrdset_create_localhost(
  1157. "disk_bcache_usage"
  1158. , d->device
  1159. , d->disk
  1160. , family
  1161. , "disk.bcache_usage"
  1162. , "BCache Cache Usage"
  1163. , "percentage"
  1164. , PLUGIN_PROC_NAME
  1165. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1166. , NETDATA_CHART_PRIO_BCACHE_USAGE
  1167. , update_every
  1168. , RRDSET_TYPE_AREA
  1169. );
  1170. d->rd_bcache_available_percent = rrddim_add(d->st_bcache_usage, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1171. }
  1172. else rrdset_next(d->st_bcache_usage);
  1173. rrddim_set_by_pointer(d->st_bcache_usage, d->rd_bcache_available_percent, cache_available_percent);
  1174. rrdset_done(d->st_bcache_usage);
  1175. }
  1176. {
  1177. if(unlikely(!d->st_bcache_cache_read_races)) {
  1178. d->st_bcache_cache_read_races = rrdset_create_localhost(
  1179. "disk_bcache_cache_read_races"
  1180. , d->device
  1181. , d->disk
  1182. , family
  1183. , "disk.bcache_cache_read_races"
  1184. , "BCache Cache Read Races"
  1185. , "operations/s"
  1186. , PLUGIN_PROC_NAME
  1187. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1188. , NETDATA_CHART_PRIO_BCACHE_CACHE_READ_RACES
  1189. , update_every
  1190. , RRDSET_TYPE_LINE
  1191. );
  1192. d->rd_bcache_cache_read_races = rrddim_add(d->st_bcache_cache_read_races, "races", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1193. d->rd_bcache_cache_io_errors = rrddim_add(d->st_bcache_cache_read_races, "errors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1194. }
  1195. else rrdset_next(d->st_bcache_cache_read_races);
  1196. rrddim_set_by_pointer(d->st_bcache_cache_read_races, d->rd_bcache_cache_read_races, cache_read_races);
  1197. rrddim_set_by_pointer(d->st_bcache_cache_read_races, d->rd_bcache_cache_io_errors, cache_io_errors);
  1198. rrdset_done(d->st_bcache_cache_read_races);
  1199. }
  1200. if(d->do_bcache == CONFIG_BOOLEAN_YES || (d->do_bcache == CONFIG_BOOLEAN_AUTO && (stats_total_cache_hits != 0 || stats_total_cache_misses != 0 || stats_total_cache_miss_collisions != 0))) {
  1201. if(unlikely(!d->st_bcache)) {
  1202. d->st_bcache = rrdset_create_localhost(
  1203. "disk_bcache"
  1204. , d->device
  1205. , d->disk
  1206. , family
  1207. , "disk.bcache"
  1208. , "BCache Cache I/O Operations"
  1209. , "operations/s"
  1210. , PLUGIN_PROC_NAME
  1211. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1212. , NETDATA_CHART_PRIO_BCACHE_OPS
  1213. , update_every
  1214. , RRDSET_TYPE_LINE
  1215. );
  1216. rrdset_flag_set(d->st_bcache, RRDSET_FLAG_DETAIL);
  1217. d->rd_bcache_hits = rrddim_add(d->st_bcache, "hits", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1218. d->rd_bcache_misses = rrddim_add(d->st_bcache, "misses", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1219. d->rd_bcache_miss_collisions = rrddim_add(d->st_bcache, "collisions", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1220. d->rd_bcache_readaheads = rrddim_add(d->st_bcache, "readaheads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1221. }
  1222. else rrdset_next(d->st_bcache);
  1223. rrddim_set_by_pointer(d->st_bcache, d->rd_bcache_hits, stats_total_cache_hits);
  1224. rrddim_set_by_pointer(d->st_bcache, d->rd_bcache_misses, stats_total_cache_misses);
  1225. rrddim_set_by_pointer(d->st_bcache, d->rd_bcache_miss_collisions, stats_total_cache_miss_collisions);
  1226. rrddim_set_by_pointer(d->st_bcache, d->rd_bcache_readaheads, cache_readaheads);
  1227. rrdset_done(d->st_bcache);
  1228. }
  1229. if(d->do_bcache == CONFIG_BOOLEAN_YES || (d->do_bcache == CONFIG_BOOLEAN_AUTO && (stats_total_cache_bypass_hits != 0 || stats_total_cache_bypass_misses != 0))) {
  1230. if(unlikely(!d->st_bcache_bypass)) {
  1231. d->st_bcache_bypass = rrdset_create_localhost(
  1232. "disk_bcache_bypass"
  1233. , d->device
  1234. , d->disk
  1235. , family
  1236. , "disk.bcache_bypass"
  1237. , "BCache Cache Bypass I/O Operations"
  1238. , "operations/s"
  1239. , PLUGIN_PROC_NAME
  1240. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1241. , NETDATA_CHART_PRIO_BCACHE_BYPASS
  1242. , update_every
  1243. , RRDSET_TYPE_LINE
  1244. );
  1245. rrdset_flag_set(d->st_bcache_bypass, RRDSET_FLAG_DETAIL);
  1246. d->rd_bcache_bypass_hits = rrddim_add(d->st_bcache_bypass, "hits", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1247. d->rd_bcache_bypass_misses = rrddim_add(d->st_bcache_bypass, "misses", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1248. }
  1249. else rrdset_next(d->st_bcache_bypass);
  1250. rrddim_set_by_pointer(d->st_bcache_bypass, d->rd_bcache_bypass_hits, stats_total_cache_bypass_hits);
  1251. rrddim_set_by_pointer(d->st_bcache_bypass, d->rd_bcache_bypass_misses, stats_total_cache_bypass_misses);
  1252. rrdset_done(d->st_bcache_bypass);
  1253. }
  1254. }
  1255. }
  1256. // ------------------------------------------------------------------------
  1257. // update the system total I/O
  1258. if(global_do_io == CONFIG_BOOLEAN_YES || (global_do_io == CONFIG_BOOLEAN_AUTO && (system_read_kb || system_write_kb))) {
  1259. static RRDSET *st_io = NULL;
  1260. static RRDDIM *rd_in = NULL, *rd_out = NULL;
  1261. if(unlikely(!st_io)) {
  1262. st_io = rrdset_create_localhost(
  1263. "system"
  1264. , "io"
  1265. , NULL
  1266. , "disk"
  1267. , NULL
  1268. , "Disk I/O"
  1269. , "KiB/s"
  1270. , PLUGIN_PROC_NAME
  1271. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1272. , NETDATA_CHART_PRIO_SYSTEM_IO
  1273. , update_every
  1274. , RRDSET_TYPE_AREA
  1275. );
  1276. rd_in = rrddim_add(st_io, "in", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1277. rd_out = rrddim_add(st_io, "out", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1278. }
  1279. else rrdset_next(st_io);
  1280. rrddim_set_by_pointer(st_io, rd_in, system_read_kb);
  1281. rrddim_set_by_pointer(st_io, rd_out, system_write_kb);
  1282. rrdset_done(st_io);
  1283. }
  1284. // ------------------------------------------------------------------------
  1285. // cleanup removed disks
  1286. struct disk *d = disk_root, *last = NULL;
  1287. while(d) {
  1288. if(unlikely(global_cleanup_removed_disks && !d->updated)) {
  1289. struct disk *t = d;
  1290. rrdset_obsolete_and_pointer_null(d->st_avgsz);
  1291. rrdset_obsolete_and_pointer_null(d->st_await);
  1292. rrdset_obsolete_and_pointer_null(d->st_backlog);
  1293. rrdset_obsolete_and_pointer_null(d->st_io);
  1294. rrdset_obsolete_and_pointer_null(d->st_iotime);
  1295. rrdset_obsolete_and_pointer_null(d->st_mops);
  1296. rrdset_obsolete_and_pointer_null(d->st_ops);
  1297. rrdset_obsolete_and_pointer_null(d->st_qops);
  1298. rrdset_obsolete_and_pointer_null(d->st_svctm);
  1299. rrdset_obsolete_and_pointer_null(d->st_util);
  1300. rrdset_obsolete_and_pointer_null(d->st_bcache);
  1301. rrdset_obsolete_and_pointer_null(d->st_bcache_bypass);
  1302. rrdset_obsolete_and_pointer_null(d->st_bcache_rates);
  1303. rrdset_obsolete_and_pointer_null(d->st_bcache_size);
  1304. rrdset_obsolete_and_pointer_null(d->st_bcache_usage);
  1305. rrdset_obsolete_and_pointer_null(d->st_bcache_hit_ratio);
  1306. if(d == disk_root) {
  1307. disk_root = d = d->next;
  1308. last = NULL;
  1309. }
  1310. else if(last) {
  1311. last->next = d = d->next;
  1312. }
  1313. freez(t->bcache_filename_dirty_data);
  1314. freez(t->bcache_filename_writeback_rate);
  1315. freez(t->bcache_filename_cache_congested);
  1316. freez(t->bcache_filename_cache_available_percent);
  1317. freez(t->bcache_filename_stats_five_minute_cache_hit_ratio);
  1318. freez(t->bcache_filename_stats_hour_cache_hit_ratio);
  1319. freez(t->bcache_filename_stats_day_cache_hit_ratio);
  1320. freez(t->bcache_filename_stats_total_cache_hit_ratio);
  1321. freez(t->bcache_filename_stats_total_cache_hits);
  1322. freez(t->bcache_filename_stats_total_cache_misses);
  1323. freez(t->bcache_filename_stats_total_cache_miss_collisions);
  1324. freez(t->bcache_filename_stats_total_cache_bypass_hits);
  1325. freez(t->bcache_filename_stats_total_cache_bypass_misses);
  1326. freez(t->bcache_filename_stats_total_cache_readaheads);
  1327. freez(t->bcache_filename_cache_read_races);
  1328. freez(t->bcache_filename_cache_io_errors);
  1329. freez(t->bcache_filename_priority_stats);
  1330. freez(t->disk);
  1331. freez(t->device);
  1332. freez(t->mount_point);
  1333. freez(t);
  1334. }
  1335. else {
  1336. d->updated = 0;
  1337. last = d;
  1338. d = d->next;
  1339. }
  1340. }
  1341. return 0;
  1342. }