proc_diskstats.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  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_PREFERRED_IDS "*"
  11. #define DEFAULT_EXCLUDED_DISKS "loop* ram*"
  12. static struct disk {
  13. char *disk; // the name of the disk (sda, sdb, etc, after being looked up)
  14. char *device; // the device of the disk (before being looked up)
  15. char *disk_by_id;
  16. char *model;
  17. char *serial;
  18. // bool rotational;
  19. // bool removable;
  20. uint32_t hash;
  21. unsigned long major;
  22. unsigned long minor;
  23. int sector_size;
  24. int type;
  25. char *mount_point;
  26. char *chart_id;
  27. // disk options caching
  28. int do_io;
  29. int do_ops;
  30. int do_mops;
  31. int do_iotime;
  32. int do_qops;
  33. int do_util;
  34. int do_ext;
  35. int do_backlog;
  36. int do_bcache;
  37. int updated;
  38. int device_is_bcache;
  39. char *bcache_filename_dirty_data;
  40. char *bcache_filename_writeback_rate;
  41. char *bcache_filename_cache_congested;
  42. char *bcache_filename_cache_available_percent;
  43. char *bcache_filename_stats_five_minute_cache_hit_ratio;
  44. char *bcache_filename_stats_hour_cache_hit_ratio;
  45. char *bcache_filename_stats_day_cache_hit_ratio;
  46. char *bcache_filename_stats_total_cache_hit_ratio;
  47. char *bcache_filename_stats_total_cache_hits;
  48. char *bcache_filename_stats_total_cache_misses;
  49. char *bcache_filename_stats_total_cache_miss_collisions;
  50. char *bcache_filename_stats_total_cache_bypass_hits;
  51. char *bcache_filename_stats_total_cache_bypass_misses;
  52. char *bcache_filename_stats_total_cache_readaheads;
  53. char *bcache_filename_cache_read_races;
  54. char *bcache_filename_cache_io_errors;
  55. char *bcache_filename_priority_stats;
  56. usec_t bcache_priority_stats_update_every_usec;
  57. usec_t bcache_priority_stats_elapsed_usec;
  58. RRDSET *st_io;
  59. RRDDIM *rd_io_reads;
  60. RRDDIM *rd_io_writes;
  61. RRDSET *st_ext_io;
  62. RRDDIM *rd_io_discards;
  63. RRDSET *st_ops;
  64. RRDDIM *rd_ops_reads;
  65. RRDDIM *rd_ops_writes;
  66. RRDSET *st_ext_ops;
  67. RRDDIM *rd_ops_discards;
  68. RRDDIM *rd_ops_flushes;
  69. RRDSET *st_qops;
  70. RRDDIM *rd_qops_operations;
  71. RRDSET *st_backlog;
  72. RRDDIM *rd_backlog_backlog;
  73. RRDSET *st_busy;
  74. RRDDIM *rd_busy_busy;
  75. RRDSET *st_util;
  76. RRDDIM *rd_util_utilization;
  77. RRDSET *st_mops;
  78. RRDDIM *rd_mops_reads;
  79. RRDDIM *rd_mops_writes;
  80. RRDSET *st_ext_mops;
  81. RRDDIM *rd_mops_discards;
  82. RRDSET *st_iotime;
  83. RRDDIM *rd_iotime_reads;
  84. RRDDIM *rd_iotime_writes;
  85. RRDSET *st_ext_iotime;
  86. RRDDIM *rd_iotime_discards;
  87. RRDDIM *rd_iotime_flushes;
  88. RRDSET *st_await;
  89. RRDDIM *rd_await_reads;
  90. RRDDIM *rd_await_writes;
  91. RRDSET *st_ext_await;
  92. RRDDIM *rd_await_discards;
  93. RRDDIM *rd_await_flushes;
  94. RRDSET *st_avgsz;
  95. RRDDIM *rd_avgsz_reads;
  96. RRDDIM *rd_avgsz_writes;
  97. RRDSET *st_ext_avgsz;
  98. RRDDIM *rd_avgsz_discards;
  99. RRDSET *st_svctm;
  100. RRDDIM *rd_svctm_svctm;
  101. RRDSET *st_bcache_size;
  102. RRDDIM *rd_bcache_dirty_size;
  103. RRDSET *st_bcache_usage;
  104. RRDDIM *rd_bcache_available_percent;
  105. RRDSET *st_bcache_hit_ratio;
  106. RRDDIM *rd_bcache_hit_ratio_5min;
  107. RRDDIM *rd_bcache_hit_ratio_1hour;
  108. RRDDIM *rd_bcache_hit_ratio_1day;
  109. RRDDIM *rd_bcache_hit_ratio_total;
  110. RRDSET *st_bcache;
  111. RRDDIM *rd_bcache_hits;
  112. RRDDIM *rd_bcache_misses;
  113. RRDDIM *rd_bcache_miss_collisions;
  114. RRDSET *st_bcache_bypass;
  115. RRDDIM *rd_bcache_bypass_hits;
  116. RRDDIM *rd_bcache_bypass_misses;
  117. RRDSET *st_bcache_rates;
  118. RRDDIM *rd_bcache_rate_congested;
  119. RRDDIM *rd_bcache_readaheads;
  120. RRDDIM *rd_bcache_rate_writeback;
  121. RRDSET *st_bcache_cache_allocations;
  122. RRDDIM *rd_bcache_cache_allocations_unused;
  123. RRDDIM *rd_bcache_cache_allocations_clean;
  124. RRDDIM *rd_bcache_cache_allocations_dirty;
  125. RRDDIM *rd_bcache_cache_allocations_metadata;
  126. RRDDIM *rd_bcache_cache_allocations_unknown;
  127. RRDSET *st_bcache_cache_read_races;
  128. RRDDIM *rd_bcache_cache_read_races;
  129. RRDDIM *rd_bcache_cache_io_errors;
  130. struct disk *next;
  131. } *disk_root = NULL;
  132. #define rrdset_obsolete_and_pointer_null(st) do { if(st) { rrdset_is_obsolete(st); (st) = NULL; } } while(st)
  133. // static char *path_to_get_hw_sector_size = NULL;
  134. // static char *path_to_get_hw_sector_size_partitions = NULL;
  135. static char *path_to_sys_dev_block_major_minor_string = NULL;
  136. static char *path_to_sys_block_device = NULL;
  137. static char *path_to_sys_block_device_bcache = NULL;
  138. static char *path_to_sys_devices_virtual_block_device = NULL;
  139. static char *path_to_device_mapper = NULL;
  140. static char *path_to_dev_disk = NULL;
  141. static char *path_to_sys_block = NULL;
  142. static char *path_to_device_label = NULL;
  143. static char *path_to_device_id = NULL;
  144. static char *path_to_veritas_volume_groups = NULL;
  145. static int name_disks_by_id = CONFIG_BOOLEAN_NO;
  146. static int global_bcache_priority_stats_update_every = 0; // disabled by default
  147. static int global_enable_new_disks_detected_at_runtime = CONFIG_BOOLEAN_YES,
  148. global_enable_performance_for_physical_disks = CONFIG_BOOLEAN_AUTO,
  149. global_enable_performance_for_virtual_disks = CONFIG_BOOLEAN_AUTO,
  150. global_enable_performance_for_partitions = CONFIG_BOOLEAN_NO,
  151. global_do_io = CONFIG_BOOLEAN_AUTO,
  152. global_do_ops = CONFIG_BOOLEAN_AUTO,
  153. global_do_mops = CONFIG_BOOLEAN_AUTO,
  154. global_do_iotime = CONFIG_BOOLEAN_AUTO,
  155. global_do_qops = CONFIG_BOOLEAN_AUTO,
  156. global_do_util = CONFIG_BOOLEAN_AUTO,
  157. global_do_ext = CONFIG_BOOLEAN_AUTO,
  158. global_do_backlog = CONFIG_BOOLEAN_AUTO,
  159. global_do_bcache = CONFIG_BOOLEAN_AUTO,
  160. globals_initialized = 0,
  161. global_cleanup_removed_disks = 1;
  162. static SIMPLE_PATTERN *preferred_ids = NULL;
  163. static SIMPLE_PATTERN *excluded_disks = NULL;
  164. static unsigned long long int bcache_read_number_with_units(const char *filename) {
  165. char buffer[50 + 1];
  166. if(read_file(filename, buffer, 50) == 0) {
  167. static int unknown_units_error = 10;
  168. char *end = NULL;
  169. NETDATA_DOUBLE value = str2ndd(buffer, &end);
  170. if(end && *end) {
  171. if(*end == 'k')
  172. return (unsigned long long int)(value * 1024.0);
  173. else if(*end == 'M')
  174. return (unsigned long long int)(value * 1024.0 * 1024.0);
  175. else if(*end == 'G')
  176. return (unsigned long long int)(value * 1024.0 * 1024.0 * 1024.0);
  177. else if(*end == 'T')
  178. return (unsigned long long int)(value * 1024.0 * 1024.0 * 1024.0 * 1024.0);
  179. else if(unknown_units_error > 0) {
  180. collector_error("bcache file '%s' provides value '%s' with unknown units '%s'", filename, buffer, end);
  181. unknown_units_error--;
  182. }
  183. }
  184. return (unsigned long long int)value;
  185. }
  186. return 0;
  187. }
  188. void bcache_read_priority_stats(struct disk *d, const char *family, int update_every, usec_t dt) {
  189. static procfile *ff = NULL;
  190. static char *separators = " \t:%[]";
  191. static ARL_BASE *arl_base = NULL;
  192. static unsigned long long unused;
  193. static unsigned long long clean;
  194. static unsigned long long dirty;
  195. static unsigned long long metadata;
  196. static unsigned long long unknown;
  197. // check if it is time to update this metric
  198. d->bcache_priority_stats_elapsed_usec += dt;
  199. if(likely(d->bcache_priority_stats_elapsed_usec < d->bcache_priority_stats_update_every_usec)) return;
  200. d->bcache_priority_stats_elapsed_usec = 0;
  201. // initialize ARL
  202. if(unlikely(!arl_base)) {
  203. arl_base = arl_create("bcache/priority_stats", NULL, 60);
  204. arl_expect(arl_base, "Unused", &unused);
  205. arl_expect(arl_base, "Clean", &clean);
  206. arl_expect(arl_base, "Dirty", &dirty);
  207. arl_expect(arl_base, "Metadata", &metadata);
  208. }
  209. ff = procfile_reopen(ff, d->bcache_filename_priority_stats, separators, PROCFILE_FLAG_DEFAULT);
  210. if(likely(ff)) ff = procfile_readall(ff);
  211. if(unlikely(!ff)) {
  212. separators = " \t:%[]";
  213. return;
  214. }
  215. // do not reset the separators on every iteration
  216. separators = NULL;
  217. arl_begin(arl_base);
  218. unused = clean = dirty = metadata = unknown = 0;
  219. size_t lines = procfile_lines(ff), l;
  220. for(l = 0; l < lines ;l++) {
  221. size_t words = procfile_linewords(ff, l);
  222. if(unlikely(words < 2)) {
  223. if(unlikely(words)) collector_error("Cannot read '%s' line %zu. Expected 2 params, read %zu.", d->bcache_filename_priority_stats, l, words);
  224. continue;
  225. }
  226. if(unlikely(arl_check(arl_base,
  227. procfile_lineword(ff, l, 0),
  228. procfile_lineword(ff, l, 1)))) break;
  229. }
  230. unknown = 100 - unused - clean - dirty - metadata;
  231. // create / update the cache allocations chart
  232. {
  233. if(unlikely(!d->st_bcache_cache_allocations)) {
  234. d->st_bcache_cache_allocations = rrdset_create_localhost(
  235. "disk_bcache_cache_alloc"
  236. , d->chart_id
  237. , d->disk
  238. , family
  239. , "disk.bcache_cache_alloc"
  240. , "BCache Cache Allocations"
  241. , "percentage"
  242. , PLUGIN_PROC_NAME
  243. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  244. , NETDATA_CHART_PRIO_BCACHE_CACHE_ALLOC
  245. , update_every
  246. , RRDSET_TYPE_STACKED
  247. );
  248. d->rd_bcache_cache_allocations_unused = rrddim_add(d->st_bcache_cache_allocations, "unused", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  249. d->rd_bcache_cache_allocations_dirty = rrddim_add(d->st_bcache_cache_allocations, "dirty", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  250. d->rd_bcache_cache_allocations_clean = rrddim_add(d->st_bcache_cache_allocations, "clean", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  251. d->rd_bcache_cache_allocations_metadata = rrddim_add(d->st_bcache_cache_allocations, "metadata", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  252. d->rd_bcache_cache_allocations_unknown = rrddim_add(d->st_bcache_cache_allocations, "undefined", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  253. d->bcache_priority_stats_update_every_usec = update_every * USEC_PER_SEC;
  254. }
  255. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_unused, unused);
  256. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_dirty, dirty);
  257. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_clean, clean);
  258. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_metadata, metadata);
  259. rrddim_set_by_pointer(d->st_bcache_cache_allocations, d->rd_bcache_cache_allocations_unknown, unknown);
  260. rrdset_done(d->st_bcache_cache_allocations);
  261. }
  262. }
  263. static inline int is_major_enabled(int major) {
  264. static int8_t *major_configs = NULL;
  265. static size_t major_size = 0;
  266. if(major < 0) return 1;
  267. size_t wanted_size = (size_t)major + 1;
  268. if(major_size < wanted_size) {
  269. major_configs = reallocz(major_configs, wanted_size * sizeof(int8_t));
  270. size_t i;
  271. for(i = major_size; i < wanted_size ; i++)
  272. major_configs[i] = -1;
  273. major_size = wanted_size;
  274. }
  275. if(major_configs[major] == -1) {
  276. char buffer[CONFIG_MAX_NAME + 1];
  277. snprintfz(buffer, CONFIG_MAX_NAME, "performance metrics for disks with major %d", major);
  278. major_configs[major] = (char)config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, buffer, 1);
  279. }
  280. return (int)major_configs[major];
  281. }
  282. 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) {
  283. //collector_info("DEVICE-MAPPER ('%s', %lu:%lu): examining directory '%s' (allowed depth %d).", disk, major, minor, path, depth);
  284. int found = 0, preferred = 0;
  285. char *first_result = mallocz(result_size + 1);
  286. DIR *dir = opendir(path);
  287. if (!dir) {
  288. collector_error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot open directory '%s'.", disk, major, minor, path);
  289. goto failed;
  290. }
  291. struct dirent *de = NULL;
  292. while ((de = readdir(dir))) {
  293. if(de->d_type == DT_DIR) {
  294. if((de->d_name[0] == '.' && de->d_name[1] == '\0') || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0'))
  295. continue;
  296. if(depth <= 0) {
  297. collector_error("DEVICE-MAPPER ('%s', %lu:%lu): Depth limit reached for path '%s/%s'. Ignoring path.", disk, major, minor, path, de->d_name);
  298. break;
  299. }
  300. else {
  301. char *path_nested = NULL;
  302. char *prefix_nested = NULL;
  303. {
  304. char buffer[FILENAME_MAX + 1];
  305. snprintfz(buffer, FILENAME_MAX, "%s/%s", path, de->d_name);
  306. path_nested = strdupz(buffer);
  307. snprintfz(buffer, FILENAME_MAX, "%s%s%s", (prefix)?prefix:"", (prefix)?"_":"", de->d_name);
  308. prefix_nested = strdupz(buffer);
  309. }
  310. found = get_disk_name_from_path(path_nested, result, result_size, major, minor, disk, prefix_nested, depth - 1);
  311. freez(path_nested);
  312. freez(prefix_nested);
  313. if(found) break;
  314. }
  315. }
  316. else if(de->d_type == DT_LNK || de->d_type == DT_BLK) {
  317. char filename[FILENAME_MAX + 1];
  318. if(de->d_type == DT_LNK) {
  319. snprintfz(filename, FILENAME_MAX, "%s/%s", path, de->d_name);
  320. ssize_t len = readlink(filename, result, result_size - 1);
  321. if(len <= 0) {
  322. collector_error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot read link '%s'.", disk, major, minor, filename);
  323. continue;
  324. }
  325. result[len] = '\0';
  326. if(result[0] != '/')
  327. snprintfz(filename, FILENAME_MAX, "%s/%s", path, result);
  328. else
  329. strncpyz(filename, result, FILENAME_MAX);
  330. }
  331. else {
  332. snprintfz(filename, FILENAME_MAX, "%s/%s", path, de->d_name);
  333. }
  334. struct stat sb;
  335. if(stat(filename, &sb) == -1) {
  336. collector_error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot stat() file '%s'.", disk, major, minor, filename);
  337. continue;
  338. }
  339. if((sb.st_mode & S_IFMT) != S_IFBLK) {
  340. //collector_info("DEVICE-MAPPER ('%s', %lu:%lu): file '%s' is not a block device.", disk, major, minor, filename);
  341. continue;
  342. }
  343. if(major(sb.st_rdev) != major || minor(sb.st_rdev) != minor || strcmp(basename(filename), disk)) {
  344. //collector_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));
  345. continue;
  346. }
  347. //collector_info("DEVICE-MAPPER ('%s', %lu:%lu): filename '%s' matches.", disk, major, minor, filename);
  348. snprintfz(result, result_size - 1, "%s%s%s", (prefix)?prefix:"", (prefix)?"_":"", de->d_name);
  349. if(!found) {
  350. strncpyz(first_result, result, result_size);
  351. found = 1;
  352. }
  353. if(simple_pattern_matches(preferred_ids, result)) {
  354. preferred = 1;
  355. break;
  356. }
  357. }
  358. }
  359. closedir(dir);
  360. failed:
  361. if(!found)
  362. result[0] = '\0';
  363. else if(!preferred)
  364. strncpyz(result, first_result, result_size);
  365. freez(first_result);
  366. return found;
  367. }
  368. static inline char *get_disk_name(unsigned long major, unsigned long minor, char *disk) {
  369. char result[FILENAME_MAX + 2] = "";
  370. 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))
  371. 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))
  372. 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))
  373. 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))
  374. strncpy(result, disk, FILENAME_MAX);
  375. if(!result[0])
  376. strncpy(result, disk, FILENAME_MAX);
  377. netdata_fix_chart_name(result);
  378. return strdup(result);
  379. }
  380. static inline bool ends_with(const char *str, const char *suffix) {
  381. if (!str || !suffix)
  382. return false;
  383. size_t len_str = strlen(str);
  384. size_t len_suffix = strlen(suffix);
  385. if (len_suffix > len_str)
  386. return false;
  387. return strncmp(str + len_str - len_suffix, suffix, len_suffix) == 0;
  388. }
  389. static inline char *get_disk_by_id(char *device) {
  390. char pathname[256 + 1];
  391. snprintfz(pathname, 256, "%s/by-id", path_to_dev_disk);
  392. struct dirent *entry;
  393. DIR *dp = opendir(pathname);
  394. if (dp == NULL) {
  395. internal_error(true, "Cannot open '%s'", pathname);
  396. return NULL;
  397. }
  398. while ((entry = readdir(dp))) {
  399. // We ignore the '.' and '..' entries
  400. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  401. continue;
  402. if(strncmp(entry->d_name, "md-uuid-", 8) == 0 ||
  403. strncmp(entry->d_name, "dm-uuid-", 8) == 0 ||
  404. strncmp(entry->d_name, "nvme-eui.", 9) == 0 ||
  405. strncmp(entry->d_name, "wwn-", 4) == 0 ||
  406. strncmp(entry->d_name, "lvm-pv-uuid-", 12) == 0)
  407. continue;
  408. char link_target[256 + 1];
  409. char full_path[256 + 1];
  410. snprintfz(full_path, 256, "%s/%s", pathname, entry->d_name);
  411. ssize_t len = readlink(full_path, link_target, 256);
  412. if (len == -1)
  413. continue;
  414. link_target[len] = '\0';
  415. if (ends_with(link_target, device)) {
  416. char *s = strdupz(entry->d_name);
  417. closedir(dp);
  418. return s;
  419. }
  420. }
  421. closedir(dp);
  422. return NULL;
  423. }
  424. static inline char *get_disk_model(char *device) {
  425. char path[256 + 1];
  426. char buffer[256 + 1];
  427. snprintfz(path, 256, "%s/%s/device/model", path_to_sys_block, device);
  428. if(read_file(path, buffer, 256) != 0) {
  429. snprintfz(path, 256, "%s/%s/device/name", path_to_sys_block, device);
  430. if(read_file(path, buffer, 256) != 0)
  431. return NULL;
  432. }
  433. return strdupz(buffer);
  434. }
  435. static inline char *get_disk_serial(char *device) {
  436. char path[256 + 1];
  437. char buffer[256 + 1];
  438. snprintfz(path, 256, "%s/%s/device/serial", path_to_sys_block, device);
  439. if(read_file(path, buffer, 256) != 0)
  440. return NULL;
  441. return strdupz(buffer);
  442. }
  443. //static inline bool get_disk_rotational(char *device) {
  444. // char path[256 + 1];
  445. // char buffer[256 + 1];
  446. //
  447. // snprintfz(path, 256, "%s/%s/queue/rotational", path_to_sys_block, device);
  448. // if(read_file(path, buffer, 256) != 0)
  449. // return false;
  450. //
  451. // return buffer[0] == '1';
  452. //}
  453. //
  454. //static inline bool get_disk_removable(char *device) {
  455. // char path[256 + 1];
  456. // char buffer[256 + 1];
  457. //
  458. // snprintfz(path, 256, "%s/%s/removable", path_to_sys_block, device);
  459. // if(read_file(path, buffer, 256) != 0)
  460. // return false;
  461. //
  462. // return buffer[0] == '1';
  463. //}
  464. static void get_disk_config(struct disk *d) {
  465. int def_enable = global_enable_new_disks_detected_at_runtime;
  466. if(def_enable != CONFIG_BOOLEAN_NO && (simple_pattern_matches(excluded_disks, d->device) || simple_pattern_matches(excluded_disks, d->disk)))
  467. def_enable = CONFIG_BOOLEAN_NO;
  468. char var_name[4096 + 1];
  469. snprintfz(var_name, 4096, CONFIG_SECTION_PLUGIN_PROC_DISKSTATS ":%s", d->disk);
  470. def_enable = config_get_boolean_ondemand(var_name, "enable", def_enable);
  471. if(unlikely(def_enable == CONFIG_BOOLEAN_NO)) {
  472. // the user does not want any metrics for this disk
  473. d->do_io = CONFIG_BOOLEAN_NO;
  474. d->do_ops = CONFIG_BOOLEAN_NO;
  475. d->do_mops = CONFIG_BOOLEAN_NO;
  476. d->do_iotime = CONFIG_BOOLEAN_NO;
  477. d->do_qops = CONFIG_BOOLEAN_NO;
  478. d->do_util = CONFIG_BOOLEAN_NO;
  479. d->do_ext = CONFIG_BOOLEAN_NO;
  480. d->do_backlog = CONFIG_BOOLEAN_NO;
  481. d->do_bcache = CONFIG_BOOLEAN_NO;
  482. }
  483. else {
  484. // this disk is enabled
  485. // check its direct settings
  486. int def_performance = CONFIG_BOOLEAN_AUTO;
  487. // since this is 'on demand' we can figure the performance settings
  488. // based on the type of disk
  489. if(!d->device_is_bcache) {
  490. switch(d->type) {
  491. default:
  492. case DISK_TYPE_UNKNOWN:
  493. break;
  494. case DISK_TYPE_PHYSICAL:
  495. def_performance = global_enable_performance_for_physical_disks;
  496. break;
  497. case DISK_TYPE_PARTITION:
  498. def_performance = global_enable_performance_for_partitions;
  499. break;
  500. case DISK_TYPE_VIRTUAL:
  501. def_performance = global_enable_performance_for_virtual_disks;
  502. break;
  503. }
  504. }
  505. // check if we have to disable performance for this disk
  506. if(def_performance)
  507. def_performance = is_major_enabled((int)d->major);
  508. // ------------------------------------------------------------
  509. // now we have def_performance and def_space
  510. // to work further
  511. // def_performance
  512. // check the user configuration (this will also show our 'on demand' decision)
  513. def_performance = config_get_boolean_ondemand(var_name, "enable performance metrics", def_performance);
  514. int ddo_io = CONFIG_BOOLEAN_NO,
  515. ddo_ops = CONFIG_BOOLEAN_NO,
  516. ddo_mops = CONFIG_BOOLEAN_NO,
  517. ddo_iotime = CONFIG_BOOLEAN_NO,
  518. ddo_qops = CONFIG_BOOLEAN_NO,
  519. ddo_util = CONFIG_BOOLEAN_NO,
  520. ddo_ext = CONFIG_BOOLEAN_NO,
  521. ddo_backlog = CONFIG_BOOLEAN_NO,
  522. ddo_bcache = CONFIG_BOOLEAN_NO;
  523. // we enable individual performance charts only when def_performance is not disabled
  524. if(unlikely(def_performance != CONFIG_BOOLEAN_NO)) {
  525. ddo_io = global_do_io,
  526. ddo_ops = global_do_ops,
  527. ddo_mops = global_do_mops,
  528. ddo_iotime = global_do_iotime,
  529. ddo_qops = global_do_qops,
  530. ddo_util = global_do_util,
  531. ddo_ext = global_do_ext,
  532. ddo_backlog = global_do_backlog,
  533. ddo_bcache = global_do_bcache;
  534. }
  535. d->do_io = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
  536. d->do_ops = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
  537. d->do_mops = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
  538. d->do_iotime = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
  539. d->do_qops = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
  540. d->do_util = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
  541. d->do_ext = config_get_boolean_ondemand(var_name, "extended operations", ddo_ext);
  542. d->do_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
  543. if(d->device_is_bcache)
  544. d->do_bcache = config_get_boolean_ondemand(var_name, "bcache", ddo_bcache);
  545. else
  546. d->do_bcache = 0;
  547. }
  548. }
  549. static struct disk *get_disk(unsigned long major, unsigned long minor, char *disk) {
  550. static struct mountinfo *disk_mountinfo_root = NULL;
  551. struct disk *d;
  552. uint32_t hash = simple_hash(disk);
  553. // search for it in our RAM list.
  554. // this is sequential, but since we just walk through
  555. // and the number of disks / partitions in a system
  556. // should not be that many, it should be acceptable
  557. for(d = disk_root; d ; d = d->next){
  558. if (unlikely(
  559. d->major == major && d->minor == minor && d->hash == hash && !strcmp(d->device, disk)))
  560. return d;
  561. }
  562. // not found
  563. // create a new disk structure
  564. d = (struct disk *)callocz(1, sizeof(struct disk));
  565. d->disk = get_disk_name(major, minor, disk);
  566. d->device = strdupz(disk);
  567. d->disk_by_id = get_disk_by_id(disk);
  568. d->model = get_disk_model(disk);
  569. d->serial = get_disk_serial(disk);
  570. // d->rotational = get_disk_rotational(disk);
  571. // d->removable = get_disk_removable(disk);
  572. d->hash = simple_hash(d->device);
  573. d->major = major;
  574. d->minor = minor;
  575. d->type = DISK_TYPE_UNKNOWN; // Default type. Changed later if not correct.
  576. d->sector_size = 512; // the default, will be changed below
  577. d->next = NULL;
  578. // append it to the list
  579. if(unlikely(!disk_root))
  580. disk_root = d;
  581. else {
  582. struct disk *last;
  583. for(last = disk_root; last->next ;last = last->next);
  584. last->next = d;
  585. }
  586. d->chart_id = strdupz(d->device);
  587. // read device uuid if it is an LVM volume
  588. if (!strncmp(d->device, "dm-", 3)) {
  589. char uuid_filename[FILENAME_MAX + 1];
  590. int size = snprintfz(uuid_filename, FILENAME_MAX, path_to_sys_devices_virtual_block_device, disk);
  591. strncat(uuid_filename, "/dm/uuid", FILENAME_MAX - size);
  592. char device_uuid[RRD_ID_LENGTH_MAX + 1];
  593. if (!read_file(uuid_filename, device_uuid, RRD_ID_LENGTH_MAX) && !strncmp(device_uuid, "LVM-", 4)) {
  594. trim(device_uuid);
  595. char chart_id[RRD_ID_LENGTH_MAX + 1];
  596. snprintf(chart_id, RRD_ID_LENGTH_MAX, "%s-%s", d->device, device_uuid + 4);
  597. freez(d->chart_id);
  598. d->chart_id = strdupz(chart_id);
  599. }
  600. }
  601. char buffer[FILENAME_MAX + 1];
  602. // find if it is a physical disk
  603. // by checking if /sys/block/DISK is readable.
  604. snprintfz(buffer, FILENAME_MAX, path_to_sys_block_device, disk);
  605. if(likely(access(buffer, R_OK) == 0)) {
  606. // assign it here, but it will be overwritten if it is not a physical disk
  607. d->type = DISK_TYPE_PHYSICAL;
  608. }
  609. // find if it is a partition
  610. // by checking if /sys/dev/block/MAJOR:MINOR/partition is readable.
  611. snprintfz(buffer, FILENAME_MAX, path_to_sys_dev_block_major_minor_string, major, minor, "partition");
  612. if(likely(access(buffer, R_OK) == 0)) {
  613. d->type = DISK_TYPE_PARTITION;
  614. }
  615. else {
  616. // find if it is a virtual disk
  617. // by checking if /sys/devices/virtual/block/DISK is readable.
  618. snprintfz(buffer, FILENAME_MAX, path_to_sys_devices_virtual_block_device, disk);
  619. if(likely(access(buffer, R_OK) == 0)) {
  620. d->type = DISK_TYPE_VIRTUAL;
  621. }
  622. else {
  623. // find if it is a virtual device
  624. // by checking if /sys/dev/block/MAJOR:MINOR/slaves has entries
  625. snprintfz(buffer, FILENAME_MAX, path_to_sys_dev_block_major_minor_string, major, minor, "slaves/");
  626. DIR *dirp = opendir(buffer);
  627. if (likely(dirp != NULL)) {
  628. struct dirent *dp;
  629. while ((dp = readdir(dirp))) {
  630. // . and .. are also files in empty folders.
  631. if (unlikely(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)) {
  632. continue;
  633. }
  634. d->type = DISK_TYPE_VIRTUAL;
  635. // Stop the loop after we found one file.
  636. break;
  637. }
  638. if (unlikely(closedir(dirp) == -1))
  639. collector_error("Unable to close dir %s", buffer);
  640. }
  641. }
  642. }
  643. // ------------------------------------------------------------------------
  644. // check if we can find its mount point
  645. // mountinfo_find() can be called with NULL disk_mountinfo_root
  646. struct mountinfo *mi = mountinfo_find(disk_mountinfo_root, d->major, d->minor, d->device);
  647. if(unlikely(!mi)) {
  648. // mountinfo_free_all can be called with NULL
  649. mountinfo_free_all(disk_mountinfo_root);
  650. disk_mountinfo_root = mountinfo_read(0);
  651. mi = mountinfo_find(disk_mountinfo_root, d->major, d->minor, d->device);
  652. }
  653. if(unlikely(mi))
  654. d->mount_point = strdupz(mi->mount_point);
  655. else
  656. d->mount_point = NULL;
  657. // ------------------------------------------------------------------------
  658. // find the disk sector size
  659. /*
  660. * sector size is always 512 bytes inside the kernel #3481
  661. *
  662. {
  663. char tf[FILENAME_MAX + 1], *t;
  664. strncpyz(tf, d->device, FILENAME_MAX);
  665. // replace all / with !
  666. for(t = tf; *t ;t++)
  667. if(unlikely(*t == '/')) *t = '!';
  668. if(likely(d->type == DISK_TYPE_PARTITION))
  669. snprintfz(buffer, FILENAME_MAX, path_to_get_hw_sector_size_partitions, d->major, d->minor, tf);
  670. else
  671. snprintfz(buffer, FILENAME_MAX, path_to_get_hw_sector_size, tf);
  672. FILE *fpss = fopen(buffer, "r");
  673. if(likely(fpss)) {
  674. char buffer2[1024 + 1];
  675. char *tmp = fgets(buffer2, 1024, fpss);
  676. if(likely(tmp)) {
  677. d->sector_size = str2i(tmp);
  678. if(unlikely(d->sector_size <= 0)) {
  679. collector_error("Invalid sector size %d for device %s in %s. Assuming 512.", d->sector_size, d->device, buffer);
  680. d->sector_size = 512;
  681. }
  682. }
  683. else collector_error("Cannot read data for sector size for device %s from %s. Assuming 512.", d->device, buffer);
  684. fclose(fpss);
  685. }
  686. else collector_error("Cannot read sector size for device %s from %s. Assuming 512.", d->device, buffer);
  687. }
  688. */
  689. // ------------------------------------------------------------------------
  690. // check if the device is a bcache
  691. struct stat bcache;
  692. snprintfz(buffer, FILENAME_MAX, path_to_sys_block_device_bcache, disk);
  693. if(unlikely(stat(buffer, &bcache) == 0 && (bcache.st_mode & S_IFMT) == S_IFDIR)) {
  694. // we have the 'bcache' directory
  695. d->device_is_bcache = 1;
  696. char buffer2[FILENAME_MAX + 1];
  697. snprintfz(buffer2, FILENAME_MAX, "%s/cache/congested", buffer);
  698. if(access(buffer2, R_OK) == 0)
  699. d->bcache_filename_cache_congested = strdupz(buffer2);
  700. else
  701. collector_error("bcache file '%s' cannot be read.", buffer2);
  702. snprintfz(buffer2, FILENAME_MAX, "%s/readahead", buffer);
  703. if(access(buffer2, R_OK) == 0)
  704. d->bcache_filename_stats_total_cache_readaheads = strdupz(buffer2);
  705. else
  706. collector_error("bcache file '%s' cannot be read.", buffer2);
  707. snprintfz(buffer2, FILENAME_MAX, "%s/cache/cache0/priority_stats", buffer); // only one cache is supported by bcache
  708. if(access(buffer2, R_OK) == 0)
  709. d->bcache_filename_priority_stats = strdupz(buffer2);
  710. else
  711. collector_error("bcache file '%s' cannot be read.", buffer2);
  712. snprintfz(buffer2, FILENAME_MAX, "%s/cache/internal/cache_read_races", buffer);
  713. if(access(buffer2, R_OK) == 0)
  714. d->bcache_filename_cache_read_races = strdupz(buffer2);
  715. else
  716. collector_error("bcache file '%s' cannot be read.", buffer2);
  717. snprintfz(buffer2, FILENAME_MAX, "%s/cache/cache0/io_errors", buffer);
  718. if(access(buffer2, R_OK) == 0)
  719. d->bcache_filename_cache_io_errors = strdupz(buffer2);
  720. else
  721. collector_error("bcache file '%s' cannot be read.", buffer2);
  722. snprintfz(buffer2, FILENAME_MAX, "%s/dirty_data", buffer);
  723. if(access(buffer2, R_OK) == 0)
  724. d->bcache_filename_dirty_data = strdupz(buffer2);
  725. else
  726. collector_error("bcache file '%s' cannot be read.", buffer2);
  727. snprintfz(buffer2, FILENAME_MAX, "%s/writeback_rate", buffer);
  728. if(access(buffer2, R_OK) == 0)
  729. d->bcache_filename_writeback_rate = strdupz(buffer2);
  730. else
  731. collector_error("bcache file '%s' cannot be read.", buffer2);
  732. snprintfz(buffer2, FILENAME_MAX, "%s/cache/cache_available_percent", buffer);
  733. if(access(buffer2, R_OK) == 0)
  734. d->bcache_filename_cache_available_percent = strdupz(buffer2);
  735. else
  736. collector_error("bcache file '%s' cannot be read.", buffer2);
  737. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_hits", buffer);
  738. if(access(buffer2, R_OK) == 0)
  739. d->bcache_filename_stats_total_cache_hits = strdupz(buffer2);
  740. else
  741. collector_error("bcache file '%s' cannot be read.", buffer2);
  742. snprintfz(buffer2, FILENAME_MAX, "%s/stats_five_minute/cache_hit_ratio", buffer);
  743. if(access(buffer2, R_OK) == 0)
  744. d->bcache_filename_stats_five_minute_cache_hit_ratio = strdupz(buffer2);
  745. else
  746. collector_error("bcache file '%s' cannot be read.", buffer2);
  747. snprintfz(buffer2, FILENAME_MAX, "%s/stats_hour/cache_hit_ratio", buffer);
  748. if(access(buffer2, R_OK) == 0)
  749. d->bcache_filename_stats_hour_cache_hit_ratio = strdupz(buffer2);
  750. else
  751. collector_error("bcache file '%s' cannot be read.", buffer2);
  752. snprintfz(buffer2, FILENAME_MAX, "%s/stats_day/cache_hit_ratio", buffer);
  753. if(access(buffer2, R_OK) == 0)
  754. d->bcache_filename_stats_day_cache_hit_ratio = strdupz(buffer2);
  755. else
  756. collector_error("bcache file '%s' cannot be read.", buffer2);
  757. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_hit_ratio", buffer);
  758. if(access(buffer2, R_OK) == 0)
  759. d->bcache_filename_stats_total_cache_hit_ratio = strdupz(buffer2);
  760. else
  761. collector_error("bcache file '%s' cannot be read.", buffer2);
  762. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_misses", buffer);
  763. if(access(buffer2, R_OK) == 0)
  764. d->bcache_filename_stats_total_cache_misses = strdupz(buffer2);
  765. else
  766. collector_error("bcache file '%s' cannot be read.", buffer2);
  767. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_bypass_hits", buffer);
  768. if(access(buffer2, R_OK) == 0)
  769. d->bcache_filename_stats_total_cache_bypass_hits = strdupz(buffer2);
  770. else
  771. collector_error("bcache file '%s' cannot be read.", buffer2);
  772. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_bypass_misses", buffer);
  773. if(access(buffer2, R_OK) == 0)
  774. d->bcache_filename_stats_total_cache_bypass_misses = strdupz(buffer2);
  775. else
  776. collector_error("bcache file '%s' cannot be read.", buffer2);
  777. snprintfz(buffer2, FILENAME_MAX, "%s/stats_total/cache_miss_collisions", buffer);
  778. if(access(buffer2, R_OK) == 0)
  779. d->bcache_filename_stats_total_cache_miss_collisions = strdupz(buffer2);
  780. else
  781. collector_error("bcache file '%s' cannot be read.", buffer2);
  782. }
  783. get_disk_config(d);
  784. return d;
  785. }
  786. static void add_labels_to_disk(struct disk *d, RRDSET *st) {
  787. rrdlabels_add(st->rrdlabels, "device", d->disk, RRDLABEL_SRC_AUTO);
  788. rrdlabels_add(st->rrdlabels, "mount_point", d->mount_point, RRDLABEL_SRC_AUTO);
  789. rrdlabels_add(st->rrdlabels, "id", d->disk_by_id, RRDLABEL_SRC_AUTO);
  790. rrdlabels_add(st->rrdlabels, "model", d->model, RRDLABEL_SRC_AUTO);
  791. rrdlabels_add(st->rrdlabels, "serial", d->serial, RRDLABEL_SRC_AUTO);
  792. // rrdlabels_add(st->rrdlabels, "rotational", d->rotational ? "true" : "false", RRDLABEL_SRC_AUTO);
  793. // rrdlabels_add(st->rrdlabels, "removable", d->removable ? "true" : "false", RRDLABEL_SRC_AUTO);
  794. switch (d->type) {
  795. default:
  796. case DISK_TYPE_UNKNOWN:
  797. rrdlabels_add(st->rrdlabels, "device_type", "unknown", RRDLABEL_SRC_AUTO);
  798. break;
  799. case DISK_TYPE_PHYSICAL:
  800. rrdlabels_add(st->rrdlabels, "device_type", "physical", RRDLABEL_SRC_AUTO);
  801. break;
  802. case DISK_TYPE_PARTITION:
  803. rrdlabels_add(st->rrdlabels, "device_type", "partition", RRDLABEL_SRC_AUTO);
  804. break;
  805. case DISK_TYPE_VIRTUAL:
  806. rrdlabels_add(st->rrdlabels, "device_type", "virtual", RRDLABEL_SRC_AUTO);
  807. break;
  808. }
  809. }
  810. int do_proc_diskstats(int update_every, usec_t dt) {
  811. static procfile *ff = NULL;
  812. if(unlikely(!globals_initialized)) {
  813. globals_initialized = 1;
  814. 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);
  815. 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);
  816. 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);
  817. global_enable_performance_for_partitions = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "performance metrics for partitions", global_enable_performance_for_partitions);
  818. global_do_io = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "bandwidth for all disks", global_do_io);
  819. global_do_ops = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "operations for all disks", global_do_ops);
  820. global_do_mops = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "merged operations for all disks", global_do_mops);
  821. global_do_iotime = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "i/o time for all disks", global_do_iotime);
  822. global_do_qops = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "queued operations for all disks", global_do_qops);
  823. global_do_util = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "utilization percentage for all disks", global_do_util);
  824. global_do_ext = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "extended operations for all disks", global_do_ext);
  825. global_do_backlog = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "backlog for all disks", global_do_backlog);
  826. global_do_bcache = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "bcache for all disks", global_do_bcache);
  827. 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);
  828. global_cleanup_removed_disks = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "remove charts of removed disks" , global_cleanup_removed_disks);
  829. char buffer[FILENAME_MAX + 1];
  830. snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s");
  831. path_to_sys_block_device = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get block device", buffer);
  832. snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s/bcache");
  833. path_to_sys_block_device_bcache = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get block device bcache", buffer);
  834. snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/virtual/block/%s");
  835. path_to_sys_devices_virtual_block_device = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get virtual block device", buffer);
  836. snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/%s");
  837. path_to_sys_dev_block_major_minor_string = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get block device infos", buffer);
  838. //snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s/queue/hw_sector_size");
  839. //path_to_get_hw_sector_size = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get h/w sector size", buffer);
  840. //snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/subsystem/%s/../queue/hw_sector_size");
  841. //path_to_get_hw_sector_size_partitions = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to get h/w sector size for partitions", buffer);
  842. snprintfz(buffer, FILENAME_MAX, "%s/dev/mapper", netdata_configured_host_prefix);
  843. path_to_device_mapper = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to device mapper", buffer);
  844. snprintfz(buffer, FILENAME_MAX, "%s/dev/disk", netdata_configured_host_prefix);
  845. path_to_dev_disk = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/disk", buffer);
  846. snprintfz(buffer, FILENAME_MAX, "%s/sys/block", netdata_configured_host_prefix);
  847. path_to_sys_block = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /sys/block", buffer);
  848. snprintfz(buffer, FILENAME_MAX, "%s/dev/disk/by-label", netdata_configured_host_prefix);
  849. path_to_device_label = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/disk/by-label", buffer);
  850. snprintfz(buffer, FILENAME_MAX, "%s/dev/disk/by-id", netdata_configured_host_prefix);
  851. path_to_device_id = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/disk/by-id", buffer);
  852. snprintfz(buffer, FILENAME_MAX, "%s/dev/vx/dsk", netdata_configured_host_prefix);
  853. path_to_veritas_volume_groups = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/vx/dsk", buffer);
  854. name_disks_by_id = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "name disks by id", name_disks_by_id);
  855. preferred_ids = simple_pattern_create(
  856. config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "preferred disk ids", DEFAULT_PREFERRED_IDS), NULL,
  857. SIMPLE_PATTERN_EXACT, true);
  858. excluded_disks = simple_pattern_create(
  859. config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "exclude disks", DEFAULT_EXCLUDED_DISKS), NULL,
  860. SIMPLE_PATTERN_EXACT, true);
  861. }
  862. // --------------------------------------------------------------------------
  863. if(unlikely(!ff)) {
  864. char filename[FILENAME_MAX + 1];
  865. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/diskstats");
  866. ff = procfile_open(config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
  867. }
  868. if(unlikely(!ff)) return 0;
  869. ff = procfile_readall(ff);
  870. if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
  871. size_t lines = procfile_lines(ff), l;
  872. collected_number system_read_kb = 0, system_write_kb = 0;
  873. int do_dc_stats = 0, do_fl_stats = 0;
  874. for(l = 0; l < lines ;l++) {
  875. // --------------------------------------------------------------------------
  876. // Read parameters
  877. char *disk;
  878. unsigned long major = 0, minor = 0;
  879. collected_number reads = 0, mreads = 0, readsectors = 0, readms = 0,
  880. writes = 0, mwrites = 0, writesectors = 0, writems = 0,
  881. queued_ios = 0, busy_ms = 0, backlog_ms = 0,
  882. discards = 0, mdiscards = 0, discardsectors = 0, discardms = 0,
  883. flushes = 0, flushms = 0;
  884. collected_number last_reads = 0, last_readsectors = 0, last_readms = 0,
  885. last_writes = 0, last_writesectors = 0, last_writems = 0,
  886. last_busy_ms = 0,
  887. last_discards = 0, last_discardsectors = 0, last_discardms = 0,
  888. last_flushes = 0, last_flushms = 0;
  889. size_t words = procfile_linewords(ff, l);
  890. if(unlikely(words < 14)) continue;
  891. major = str2ul(procfile_lineword(ff, l, 0));
  892. minor = str2ul(procfile_lineword(ff, l, 1));
  893. disk = procfile_lineword(ff, l, 2);
  894. // # of reads completed # of writes completed
  895. // This is the total number of reads or writes completed successfully.
  896. reads = str2ull(procfile_lineword(ff, l, 3), NULL); // rd_ios
  897. writes = str2ull(procfile_lineword(ff, l, 7), NULL); // wr_ios
  898. // # of reads merged # of writes merged
  899. // Reads and writes which are adjacent to each other may be merged for
  900. // efficiency. Thus two 4K reads may become one 8K read before it is
  901. // ultimately handed to the disk, and so it will be counted (and queued)
  902. mreads = str2ull(procfile_lineword(ff, l, 4), NULL); // rd_merges_or_rd_sec
  903. mwrites = str2ull(procfile_lineword(ff, l, 8), NULL); // wr_merges
  904. // # of sectors read # of sectors written
  905. // This is the total number of sectors read or written successfully.
  906. readsectors = str2ull(procfile_lineword(ff, l, 5), NULL); // rd_sec_or_wr_ios
  907. writesectors = str2ull(procfile_lineword(ff, l, 9), NULL); // wr_sec
  908. // # of milliseconds spent reading # of milliseconds spent writing
  909. // This is the total number of milliseconds spent by all reads or writes (as
  910. // measured from __make_request() to end_that_request_last()).
  911. readms = str2ull(procfile_lineword(ff, l, 6), NULL); // rd_ticks_or_wr_sec
  912. writems = str2ull(procfile_lineword(ff, l, 10), NULL); // wr_ticks
  913. // # of I/Os currently in progress
  914. // The only field that should go to zero. Incremented as requests are
  915. // given to appropriate struct request_queue and decremented as they finish.
  916. queued_ios = str2ull(procfile_lineword(ff, l, 11), NULL); // ios_pgr
  917. // # of milliseconds spent doing I/Os
  918. // This field increases so long as field queued_ios is nonzero.
  919. busy_ms = str2ull(procfile_lineword(ff, l, 12), NULL); // tot_ticks
  920. // weighted # of milliseconds spent doing I/Os
  921. // This field is incremented at each I/O start, I/O completion, I/O
  922. // merge, or read of these stats by the number of I/Os in progress
  923. // (field queued_ios) times the number of milliseconds spent doing I/O since the
  924. // last update of this field. This can provide an easy measure of both
  925. // I/O completion time and the backlog that may be accumulating.
  926. backlog_ms = str2ull(procfile_lineword(ff, l, 13), NULL); // rq_ticks
  927. if (unlikely(words > 13)) {
  928. do_dc_stats = 1;
  929. // # of discards completed
  930. // This is the total number of discards completed successfully.
  931. discards = str2ull(procfile_lineword(ff, l, 14), NULL); // dc_ios
  932. // # of discards merged
  933. // See the description of mreads/mwrites
  934. mdiscards = str2ull(procfile_lineword(ff, l, 15), NULL); // dc_merges
  935. // # of sectors discarded
  936. // This is the total number of sectors discarded successfully.
  937. discardsectors = str2ull(procfile_lineword(ff, l, 16), NULL); // dc_sec
  938. // # of milliseconds spent discarding
  939. // This is the total number of milliseconds spent by all discards (as
  940. // measured from __make_request() to end_that_request_last()).
  941. discardms = str2ull(procfile_lineword(ff, l, 17), NULL); // dc_ticks
  942. }
  943. if (unlikely(words > 17)) {
  944. do_fl_stats = 1;
  945. // number of flush I/Os processed
  946. // These values increment when an flush I/O request completes.
  947. // Block layer combines flush requests and executes at most one at a time.
  948. // This counts flush requests executed by disk. Not tracked for partitions.
  949. flushes = str2ull(procfile_lineword(ff, l, 18), NULL); // fl_ios
  950. // total wait time for flush requests
  951. flushms = str2ull(procfile_lineword(ff, l, 19), NULL); // fl_ticks
  952. }
  953. // --------------------------------------------------------------------------
  954. // get a disk structure for the disk
  955. struct disk *d = get_disk(major, minor, disk);
  956. d->updated = 1;
  957. // --------------------------------------------------------------------------
  958. // count the global system disk I/O of physical disks
  959. if(unlikely(d->type == DISK_TYPE_PHYSICAL)) {
  960. system_read_kb += readsectors * d->sector_size / 1024;
  961. system_write_kb += writesectors * d->sector_size / 1024;
  962. }
  963. // --------------------------------------------------------------------------
  964. // Set its family based on mount point
  965. char *family = d->mount_point;
  966. if(!family) family = d->disk;
  967. // --------------------------------------------------------------------------
  968. // Do performance metrics
  969. if(d->do_io == CONFIG_BOOLEAN_YES || (d->do_io == CONFIG_BOOLEAN_AUTO &&
  970. (readsectors || writesectors || discardsectors ||
  971. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  972. d->do_io = CONFIG_BOOLEAN_YES;
  973. if(unlikely(!d->st_io)) {
  974. d->st_io = rrdset_create_localhost(
  975. RRD_TYPE_DISK
  976. , d->chart_id
  977. , d->disk
  978. , family
  979. , "disk.io"
  980. , "Disk I/O Bandwidth"
  981. , "KiB/s"
  982. , PLUGIN_PROC_NAME
  983. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  984. , NETDATA_CHART_PRIO_DISK_IO
  985. , update_every
  986. , RRDSET_TYPE_AREA
  987. );
  988. d->rd_io_reads = rrddim_add(d->st_io, "reads", NULL, d->sector_size, 1024, RRD_ALGORITHM_INCREMENTAL);
  989. d->rd_io_writes = rrddim_add(d->st_io, "writes", NULL, d->sector_size * -1, 1024, RRD_ALGORITHM_INCREMENTAL);
  990. add_labels_to_disk(d, d->st_io);
  991. }
  992. last_readsectors = rrddim_set_by_pointer(d->st_io, d->rd_io_reads, readsectors);
  993. last_writesectors = rrddim_set_by_pointer(d->st_io, d->rd_io_writes, writesectors);
  994. rrdset_done(d->st_io);
  995. }
  996. if (do_dc_stats && d->do_io == CONFIG_BOOLEAN_YES && d->do_ext != CONFIG_BOOLEAN_NO) {
  997. if (unlikely(!d->st_ext_io)) {
  998. d->st_ext_io = rrdset_create_localhost(
  999. "disk_ext"
  1000. , d->chart_id
  1001. , d->disk
  1002. , family
  1003. , "disk_ext.io"
  1004. , "Amount of Discarded Data"
  1005. , "KiB/s"
  1006. , PLUGIN_PROC_NAME
  1007. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1008. , NETDATA_CHART_PRIO_DISK_IO + 1
  1009. , update_every
  1010. , RRDSET_TYPE_AREA
  1011. );
  1012. d->rd_io_discards = rrddim_add(d->st_ext_io, "discards", NULL, d->sector_size, 1024, RRD_ALGORITHM_INCREMENTAL);
  1013. add_labels_to_disk(d, d->st_ext_io);
  1014. }
  1015. last_discardsectors = rrddim_set_by_pointer(d->st_ext_io, d->rd_io_discards, discardsectors);
  1016. rrdset_done(d->st_ext_io);
  1017. }
  1018. if(d->do_ops == CONFIG_BOOLEAN_YES || (d->do_ops == CONFIG_BOOLEAN_AUTO &&
  1019. (reads || writes || discards || flushes ||
  1020. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1021. d->do_ops = CONFIG_BOOLEAN_YES;
  1022. if(unlikely(!d->st_ops)) {
  1023. d->st_ops = rrdset_create_localhost(
  1024. "disk_ops"
  1025. , d->chart_id
  1026. , d->disk
  1027. , family
  1028. , "disk.ops"
  1029. , "Disk Completed I/O Operations"
  1030. , "operations/s"
  1031. , PLUGIN_PROC_NAME
  1032. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1033. , NETDATA_CHART_PRIO_DISK_OPS
  1034. , update_every
  1035. , RRDSET_TYPE_LINE
  1036. );
  1037. rrdset_flag_set(d->st_ops, RRDSET_FLAG_DETAIL);
  1038. d->rd_ops_reads = rrddim_add(d->st_ops, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1039. d->rd_ops_writes = rrddim_add(d->st_ops, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1040. add_labels_to_disk(d, d->st_ops);
  1041. }
  1042. last_reads = rrddim_set_by_pointer(d->st_ops, d->rd_ops_reads, reads);
  1043. last_writes = rrddim_set_by_pointer(d->st_ops, d->rd_ops_writes, writes);
  1044. rrdset_done(d->st_ops);
  1045. }
  1046. if (do_dc_stats && d->do_ops == CONFIG_BOOLEAN_YES && d->do_ext != CONFIG_BOOLEAN_NO) {
  1047. if (unlikely(!d->st_ext_ops)) {
  1048. d->st_ext_ops = rrdset_create_localhost(
  1049. "disk_ext_ops"
  1050. , d->chart_id
  1051. , d->disk
  1052. , family
  1053. , "disk_ext.ops"
  1054. , "Disk Completed Extended I/O Operations"
  1055. , "operations/s"
  1056. , PLUGIN_PROC_NAME
  1057. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1058. , NETDATA_CHART_PRIO_DISK_OPS + 1
  1059. , update_every
  1060. , RRDSET_TYPE_LINE
  1061. );
  1062. rrdset_flag_set(d->st_ext_ops, RRDSET_FLAG_DETAIL);
  1063. d->rd_ops_discards = rrddim_add(d->st_ext_ops, "discards", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1064. if (do_fl_stats)
  1065. d->rd_ops_flushes = rrddim_add(d->st_ext_ops, "flushes", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1066. add_labels_to_disk(d, d->st_ext_ops);
  1067. }
  1068. last_discards = rrddim_set_by_pointer(d->st_ext_ops, d->rd_ops_discards, discards);
  1069. if (do_fl_stats)
  1070. last_flushes = rrddim_set_by_pointer(d->st_ext_ops, d->rd_ops_flushes, flushes);
  1071. rrdset_done(d->st_ext_ops);
  1072. }
  1073. if(d->do_qops == CONFIG_BOOLEAN_YES || (d->do_qops == CONFIG_BOOLEAN_AUTO &&
  1074. (queued_ios || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1075. d->do_qops = CONFIG_BOOLEAN_YES;
  1076. if(unlikely(!d->st_qops)) {
  1077. d->st_qops = rrdset_create_localhost(
  1078. "disk_qops"
  1079. , d->chart_id
  1080. , d->disk
  1081. , family
  1082. , "disk.qops"
  1083. , "Disk Current I/O Operations"
  1084. , "operations"
  1085. , PLUGIN_PROC_NAME
  1086. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1087. , NETDATA_CHART_PRIO_DISK_QOPS
  1088. , update_every
  1089. , RRDSET_TYPE_LINE
  1090. );
  1091. rrdset_flag_set(d->st_qops, RRDSET_FLAG_DETAIL);
  1092. d->rd_qops_operations = rrddim_add(d->st_qops, "operations", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1093. add_labels_to_disk(d, d->st_qops);
  1094. }
  1095. rrddim_set_by_pointer(d->st_qops, d->rd_qops_operations, queued_ios);
  1096. rrdset_done(d->st_qops);
  1097. }
  1098. if(d->do_backlog == CONFIG_BOOLEAN_YES || (d->do_backlog == CONFIG_BOOLEAN_AUTO &&
  1099. (backlog_ms || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1100. d->do_backlog = CONFIG_BOOLEAN_YES;
  1101. if(unlikely(!d->st_backlog)) {
  1102. d->st_backlog = rrdset_create_localhost(
  1103. "disk_backlog"
  1104. , d->chart_id
  1105. , d->disk
  1106. , family
  1107. , "disk.backlog"
  1108. , "Disk Backlog"
  1109. , "milliseconds"
  1110. , PLUGIN_PROC_NAME
  1111. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1112. , NETDATA_CHART_PRIO_DISK_BACKLOG
  1113. , update_every
  1114. , RRDSET_TYPE_AREA
  1115. );
  1116. rrdset_flag_set(d->st_backlog, RRDSET_FLAG_DETAIL);
  1117. d->rd_backlog_backlog = rrddim_add(d->st_backlog, "backlog", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1118. add_labels_to_disk(d, d->st_backlog);
  1119. }
  1120. rrddim_set_by_pointer(d->st_backlog, d->rd_backlog_backlog, backlog_ms);
  1121. rrdset_done(d->st_backlog);
  1122. }
  1123. if(d->do_util == CONFIG_BOOLEAN_YES || (d->do_util == CONFIG_BOOLEAN_AUTO &&
  1124. (busy_ms || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1125. d->do_util = CONFIG_BOOLEAN_YES;
  1126. if(unlikely(!d->st_busy)) {
  1127. d->st_busy = rrdset_create_localhost(
  1128. "disk_busy"
  1129. , d->chart_id
  1130. , d->disk
  1131. , family
  1132. , "disk.busy"
  1133. , "Disk Busy Time"
  1134. , "milliseconds"
  1135. , PLUGIN_PROC_NAME
  1136. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1137. , NETDATA_CHART_PRIO_DISK_BUSY
  1138. , update_every
  1139. , RRDSET_TYPE_AREA
  1140. );
  1141. rrdset_flag_set(d->st_busy, RRDSET_FLAG_DETAIL);
  1142. d->rd_busy_busy = rrddim_add(d->st_busy, "busy", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1143. add_labels_to_disk(d, d->st_busy);
  1144. }
  1145. last_busy_ms = rrddim_set_by_pointer(d->st_busy, d->rd_busy_busy, busy_ms);
  1146. rrdset_done(d->st_busy);
  1147. if(unlikely(!d->st_util)) {
  1148. d->st_util = rrdset_create_localhost(
  1149. "disk_util"
  1150. , d->chart_id
  1151. , d->disk
  1152. , family
  1153. , "disk.util"
  1154. , "Disk Utilization Time"
  1155. , "% of time working"
  1156. , PLUGIN_PROC_NAME
  1157. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1158. , NETDATA_CHART_PRIO_DISK_UTIL
  1159. , update_every
  1160. , RRDSET_TYPE_AREA
  1161. );
  1162. rrdset_flag_set(d->st_util, RRDSET_FLAG_DETAIL);
  1163. d->rd_util_utilization = rrddim_add(d->st_util, "utilization", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1164. add_labels_to_disk(d, d->st_util);
  1165. }
  1166. collected_number disk_utilization = (busy_ms - last_busy_ms) / (10 * update_every);
  1167. if (disk_utilization > 100)
  1168. disk_utilization = 100;
  1169. rrddim_set_by_pointer(d->st_util, d->rd_util_utilization, disk_utilization);
  1170. rrdset_done(d->st_util);
  1171. }
  1172. if(d->do_mops == CONFIG_BOOLEAN_YES || (d->do_mops == CONFIG_BOOLEAN_AUTO &&
  1173. (mreads || mwrites || mdiscards ||
  1174. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1175. d->do_mops = CONFIG_BOOLEAN_YES;
  1176. if(unlikely(!d->st_mops)) {
  1177. d->st_mops = rrdset_create_localhost(
  1178. "disk_mops"
  1179. , d->chart_id
  1180. , d->disk
  1181. , family
  1182. , "disk.mops"
  1183. , "Disk Merged Operations"
  1184. , "merged operations/s"
  1185. , PLUGIN_PROC_NAME
  1186. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1187. , NETDATA_CHART_PRIO_DISK_MOPS
  1188. , update_every
  1189. , RRDSET_TYPE_LINE
  1190. );
  1191. rrdset_flag_set(d->st_mops, RRDSET_FLAG_DETAIL);
  1192. d->rd_mops_reads = rrddim_add(d->st_mops, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1193. d->rd_mops_writes = rrddim_add(d->st_mops, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1194. add_labels_to_disk(d, d->st_mops);
  1195. }
  1196. rrddim_set_by_pointer(d->st_mops, d->rd_mops_reads, mreads);
  1197. rrddim_set_by_pointer(d->st_mops, d->rd_mops_writes, mwrites);
  1198. rrdset_done(d->st_mops);
  1199. }
  1200. if(do_dc_stats && d->do_mops == CONFIG_BOOLEAN_YES && d->do_ext != CONFIG_BOOLEAN_NO) {
  1201. d->do_mops = CONFIG_BOOLEAN_YES;
  1202. if(unlikely(!d->st_ext_mops)) {
  1203. d->st_ext_mops = rrdset_create_localhost(
  1204. "disk_ext_mops"
  1205. , d->chart_id
  1206. , d->disk
  1207. , family
  1208. , "disk_ext.mops"
  1209. , "Disk Merged Discard Operations"
  1210. , "merged operations/s"
  1211. , PLUGIN_PROC_NAME
  1212. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1213. , NETDATA_CHART_PRIO_DISK_MOPS + 1
  1214. , update_every
  1215. , RRDSET_TYPE_LINE
  1216. );
  1217. rrdset_flag_set(d->st_ext_mops, RRDSET_FLAG_DETAIL);
  1218. d->rd_mops_discards = rrddim_add(d->st_ext_mops, "discards", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1219. add_labels_to_disk(d, d->st_ext_mops);
  1220. }
  1221. rrddim_set_by_pointer(d->st_ext_mops, d->rd_mops_discards, mdiscards);
  1222. rrdset_done(d->st_ext_mops);
  1223. }
  1224. if(d->do_iotime == CONFIG_BOOLEAN_YES || (d->do_iotime == CONFIG_BOOLEAN_AUTO &&
  1225. (readms || writems || discardms || flushms || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1226. d->do_iotime = CONFIG_BOOLEAN_YES;
  1227. if(unlikely(!d->st_iotime)) {
  1228. d->st_iotime = rrdset_create_localhost(
  1229. "disk_iotime"
  1230. , d->chart_id
  1231. , d->disk
  1232. , family
  1233. , "disk.iotime"
  1234. , "Disk Total I/O Time"
  1235. , "milliseconds/s"
  1236. , PLUGIN_PROC_NAME
  1237. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1238. , NETDATA_CHART_PRIO_DISK_IOTIME
  1239. , update_every
  1240. , RRDSET_TYPE_LINE
  1241. );
  1242. rrdset_flag_set(d->st_iotime, RRDSET_FLAG_DETAIL);
  1243. d->rd_iotime_reads = rrddim_add(d->st_iotime, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1244. d->rd_iotime_writes = rrddim_add(d->st_iotime, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1245. add_labels_to_disk(d, d->st_iotime);
  1246. }
  1247. last_readms = rrddim_set_by_pointer(d->st_iotime, d->rd_iotime_reads, readms);
  1248. last_writems = rrddim_set_by_pointer(d->st_iotime, d->rd_iotime_writes, writems);
  1249. rrdset_done(d->st_iotime);
  1250. }
  1251. if(do_dc_stats && d->do_iotime == CONFIG_BOOLEAN_YES && d->do_ext != CONFIG_BOOLEAN_NO) {
  1252. if(unlikely(!d->st_ext_iotime)) {
  1253. d->st_ext_iotime = rrdset_create_localhost(
  1254. "disk_ext_iotime"
  1255. , d->chart_id
  1256. , d->disk
  1257. , family
  1258. , "disk_ext.iotime"
  1259. , "Disk Total I/O Time for Extended Operations"
  1260. , "milliseconds/s"
  1261. , PLUGIN_PROC_NAME
  1262. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1263. , NETDATA_CHART_PRIO_DISK_IOTIME + 1
  1264. , update_every
  1265. , RRDSET_TYPE_LINE
  1266. );
  1267. rrdset_flag_set(d->st_ext_iotime, RRDSET_FLAG_DETAIL);
  1268. d->rd_iotime_discards = rrddim_add(d->st_ext_iotime, "discards", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1269. if (do_fl_stats)
  1270. d->rd_iotime_flushes = rrddim_add(d->st_ext_iotime, "flushes", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1271. add_labels_to_disk(d, d->st_ext_iotime);
  1272. }
  1273. last_discardms = rrddim_set_by_pointer(d->st_ext_iotime, d->rd_iotime_discards, discardms);
  1274. if (do_fl_stats)
  1275. last_flushms = rrddim_set_by_pointer(d->st_ext_iotime, d->rd_iotime_flushes, flushms);
  1276. rrdset_done(d->st_ext_iotime);
  1277. }
  1278. // calculate differential charts
  1279. // only if this is not the first time we run
  1280. if(likely(dt)) {
  1281. if( (d->do_iotime == CONFIG_BOOLEAN_YES || (d->do_iotime == CONFIG_BOOLEAN_AUTO &&
  1282. (readms || writems ||
  1283. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) &&
  1284. (d->do_ops == CONFIG_BOOLEAN_YES || (d->do_ops == CONFIG_BOOLEAN_AUTO &&
  1285. (reads || writes ||
  1286. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))) {
  1287. if(unlikely(!d->st_await)) {
  1288. d->st_await = rrdset_create_localhost(
  1289. "disk_await"
  1290. , d->chart_id
  1291. , d->disk
  1292. , family
  1293. , "disk.await"
  1294. , "Average Completed I/O Operation Time"
  1295. , "milliseconds/operation"
  1296. , PLUGIN_PROC_NAME
  1297. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1298. , NETDATA_CHART_PRIO_DISK_AWAIT
  1299. , update_every
  1300. , RRDSET_TYPE_LINE
  1301. );
  1302. rrdset_flag_set(d->st_await, RRDSET_FLAG_DETAIL);
  1303. d->rd_await_reads = rrddim_add(d->st_await, "reads", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1304. d->rd_await_writes = rrddim_add(d->st_await, "writes", NULL, -1, 1, RRD_ALGORITHM_ABSOLUTE);
  1305. add_labels_to_disk(d, d->st_await);
  1306. }
  1307. rrddim_set_by_pointer(d->st_await, d->rd_await_reads, (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
  1308. rrddim_set_by_pointer(d->st_await, d->rd_await_writes, (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
  1309. rrdset_done(d->st_await);
  1310. }
  1311. if (do_dc_stats && d->do_iotime == CONFIG_BOOLEAN_YES && d->do_ops == CONFIG_BOOLEAN_YES && d->do_ext != CONFIG_BOOLEAN_NO) {
  1312. if(unlikely(!d->st_ext_await)) {
  1313. d->st_ext_await = rrdset_create_localhost(
  1314. "disk_ext_await"
  1315. , d->chart_id
  1316. , d->disk
  1317. , family
  1318. , "disk_ext.await"
  1319. , "Average Completed Extended I/O Operation Time"
  1320. , "milliseconds/operation"
  1321. , PLUGIN_PROC_NAME
  1322. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1323. , NETDATA_CHART_PRIO_DISK_AWAIT + 1
  1324. , update_every
  1325. , RRDSET_TYPE_LINE
  1326. );
  1327. rrdset_flag_set(d->st_ext_await, RRDSET_FLAG_DETAIL);
  1328. d->rd_await_discards = rrddim_add(d->st_ext_await, "discards", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1329. if (do_fl_stats)
  1330. d->rd_await_flushes = rrddim_add(d->st_ext_await, "flushes", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1331. add_labels_to_disk(d, d->st_ext_await);
  1332. }
  1333. rrddim_set_by_pointer(
  1334. d->st_ext_await, d->rd_await_discards,
  1335. (discards - last_discards) ? (discardms - last_discardms) / (discards - last_discards) : 0);
  1336. if (do_fl_stats)
  1337. rrddim_set_by_pointer(
  1338. d->st_ext_await, d->rd_await_flushes,
  1339. (flushes - last_flushes) ? (flushms - last_flushms) / (flushes - last_flushes) : 0);
  1340. rrdset_done(d->st_ext_await);
  1341. }
  1342. if( (d->do_io == CONFIG_BOOLEAN_YES || (d->do_io == CONFIG_BOOLEAN_AUTO &&
  1343. (readsectors || writesectors || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) &&
  1344. (d->do_ops == CONFIG_BOOLEAN_YES || (d->do_ops == CONFIG_BOOLEAN_AUTO &&
  1345. (reads || writes || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))) {
  1346. if(unlikely(!d->st_avgsz)) {
  1347. d->st_avgsz = rrdset_create_localhost(
  1348. "disk_avgsz"
  1349. , d->chart_id
  1350. , d->disk
  1351. , family
  1352. , "disk.avgsz"
  1353. , "Average Completed I/O Operation Bandwidth"
  1354. , "KiB/operation"
  1355. , PLUGIN_PROC_NAME
  1356. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1357. , NETDATA_CHART_PRIO_DISK_AVGSZ
  1358. , update_every
  1359. , RRDSET_TYPE_AREA
  1360. );
  1361. rrdset_flag_set(d->st_avgsz, RRDSET_FLAG_DETAIL);
  1362. d->rd_avgsz_reads = rrddim_add(d->st_avgsz, "reads", NULL, d->sector_size, 1024, RRD_ALGORITHM_ABSOLUTE);
  1363. d->rd_avgsz_writes = rrddim_add(d->st_avgsz, "writes", NULL, d->sector_size * -1, 1024, RRD_ALGORITHM_ABSOLUTE);
  1364. add_labels_to_disk(d, d->st_avgsz);
  1365. }
  1366. rrddim_set_by_pointer(d->st_avgsz, d->rd_avgsz_reads, (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
  1367. rrddim_set_by_pointer(d->st_avgsz, d->rd_avgsz_writes, (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
  1368. rrdset_done(d->st_avgsz);
  1369. }
  1370. if(do_dc_stats && d->do_io == CONFIG_BOOLEAN_YES && d->do_ops == CONFIG_BOOLEAN_YES && d->do_ext != CONFIG_BOOLEAN_NO) {
  1371. if(unlikely(!d->st_ext_avgsz)) {
  1372. d->st_ext_avgsz = rrdset_create_localhost(
  1373. "disk_ext_avgsz"
  1374. , d->chart_id
  1375. , d->disk
  1376. , family
  1377. , "disk_ext.avgsz"
  1378. , "Average Amount of Discarded Data"
  1379. , "KiB/operation"
  1380. , PLUGIN_PROC_NAME
  1381. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1382. , NETDATA_CHART_PRIO_DISK_AVGSZ
  1383. , update_every
  1384. , RRDSET_TYPE_AREA
  1385. );
  1386. rrdset_flag_set(d->st_ext_avgsz, RRDSET_FLAG_DETAIL);
  1387. d->rd_avgsz_discards = rrddim_add(d->st_ext_avgsz, "discards", NULL, d->sector_size, 1024, RRD_ALGORITHM_ABSOLUTE);
  1388. add_labels_to_disk(d, d->st_ext_avgsz);
  1389. }
  1390. rrddim_set_by_pointer(
  1391. d->st_ext_avgsz, d->rd_avgsz_discards,
  1392. (discards - last_discards) ? (discardsectors - last_discardsectors) / (discards - last_discards) :
  1393. 0);
  1394. rrdset_done(d->st_ext_avgsz);
  1395. }
  1396. if( (d->do_util == CONFIG_BOOLEAN_YES || (d->do_util == CONFIG_BOOLEAN_AUTO &&
  1397. (busy_ms ||
  1398. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) &&
  1399. (d->do_ops == CONFIG_BOOLEAN_YES || (d->do_ops == CONFIG_BOOLEAN_AUTO &&
  1400. (reads || writes ||
  1401. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))) {
  1402. if(unlikely(!d->st_svctm)) {
  1403. d->st_svctm = rrdset_create_localhost(
  1404. "disk_svctm"
  1405. , d->chart_id
  1406. , d->disk
  1407. , family
  1408. , "disk.svctm"
  1409. , "Average Service Time"
  1410. , "milliseconds/operation"
  1411. , PLUGIN_PROC_NAME
  1412. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1413. , NETDATA_CHART_PRIO_DISK_SVCTM
  1414. , update_every
  1415. , RRDSET_TYPE_LINE
  1416. );
  1417. rrdset_flag_set(d->st_svctm, RRDSET_FLAG_DETAIL);
  1418. d->rd_svctm_svctm = rrddim_add(d->st_svctm, "svctm", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1419. add_labels_to_disk(d, d->st_svctm);
  1420. }
  1421. 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);
  1422. rrdset_done(d->st_svctm);
  1423. }
  1424. }
  1425. // read bcache metrics and generate the bcache charts
  1426. if(d->device_is_bcache && d->do_bcache != CONFIG_BOOLEAN_NO) {
  1427. unsigned long long int
  1428. stats_total_cache_bypass_hits = 0,
  1429. stats_total_cache_bypass_misses = 0,
  1430. stats_total_cache_hits = 0,
  1431. stats_total_cache_miss_collisions = 0,
  1432. stats_total_cache_misses = 0,
  1433. stats_five_minute_cache_hit_ratio = 0,
  1434. stats_hour_cache_hit_ratio = 0,
  1435. stats_day_cache_hit_ratio = 0,
  1436. stats_total_cache_hit_ratio = 0,
  1437. cache_available_percent = 0,
  1438. cache_readaheads = 0,
  1439. cache_read_races = 0,
  1440. cache_io_errors = 0,
  1441. cache_congested = 0,
  1442. dirty_data = 0,
  1443. writeback_rate = 0;
  1444. // read the bcache values
  1445. if(d->bcache_filename_dirty_data)
  1446. dirty_data = bcache_read_number_with_units(d->bcache_filename_dirty_data);
  1447. if(d->bcache_filename_writeback_rate)
  1448. writeback_rate = bcache_read_number_with_units(d->bcache_filename_writeback_rate);
  1449. if(d->bcache_filename_cache_congested)
  1450. cache_congested = bcache_read_number_with_units(d->bcache_filename_cache_congested);
  1451. if(d->bcache_filename_cache_available_percent)
  1452. read_single_number_file(d->bcache_filename_cache_available_percent, &cache_available_percent);
  1453. if(d->bcache_filename_stats_five_minute_cache_hit_ratio)
  1454. read_single_number_file(d->bcache_filename_stats_five_minute_cache_hit_ratio, &stats_five_minute_cache_hit_ratio);
  1455. if(d->bcache_filename_stats_hour_cache_hit_ratio)
  1456. read_single_number_file(d->bcache_filename_stats_hour_cache_hit_ratio, &stats_hour_cache_hit_ratio);
  1457. if(d->bcache_filename_stats_day_cache_hit_ratio)
  1458. read_single_number_file(d->bcache_filename_stats_day_cache_hit_ratio, &stats_day_cache_hit_ratio);
  1459. if(d->bcache_filename_stats_total_cache_hit_ratio)
  1460. read_single_number_file(d->bcache_filename_stats_total_cache_hit_ratio, &stats_total_cache_hit_ratio);
  1461. if(d->bcache_filename_stats_total_cache_hits)
  1462. read_single_number_file(d->bcache_filename_stats_total_cache_hits, &stats_total_cache_hits);
  1463. if(d->bcache_filename_stats_total_cache_misses)
  1464. read_single_number_file(d->bcache_filename_stats_total_cache_misses, &stats_total_cache_misses);
  1465. if(d->bcache_filename_stats_total_cache_miss_collisions)
  1466. read_single_number_file(d->bcache_filename_stats_total_cache_miss_collisions, &stats_total_cache_miss_collisions);
  1467. if(d->bcache_filename_stats_total_cache_bypass_hits)
  1468. read_single_number_file(d->bcache_filename_stats_total_cache_bypass_hits, &stats_total_cache_bypass_hits);
  1469. if(d->bcache_filename_stats_total_cache_bypass_misses)
  1470. read_single_number_file(d->bcache_filename_stats_total_cache_bypass_misses, &stats_total_cache_bypass_misses);
  1471. if(d->bcache_filename_stats_total_cache_readaheads)
  1472. cache_readaheads = bcache_read_number_with_units(d->bcache_filename_stats_total_cache_readaheads);
  1473. if(d->bcache_filename_cache_read_races)
  1474. read_single_number_file(d->bcache_filename_cache_read_races, &cache_read_races);
  1475. if(d->bcache_filename_cache_io_errors)
  1476. read_single_number_file(d->bcache_filename_cache_io_errors, &cache_io_errors);
  1477. if(d->bcache_filename_priority_stats && global_bcache_priority_stats_update_every >= 1)
  1478. bcache_read_priority_stats(d, family, global_bcache_priority_stats_update_every, dt);
  1479. // update the charts
  1480. {
  1481. if(unlikely(!d->st_bcache_hit_ratio)) {
  1482. d->st_bcache_hit_ratio = rrdset_create_localhost(
  1483. "disk_bcache_hit_ratio"
  1484. , d->chart_id
  1485. , d->disk
  1486. , family
  1487. , "disk.bcache_hit_ratio"
  1488. , "BCache Cache Hit Ratio"
  1489. , "percentage"
  1490. , PLUGIN_PROC_NAME
  1491. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1492. , NETDATA_CHART_PRIO_BCACHE_HIT_RATIO
  1493. , update_every
  1494. , RRDSET_TYPE_LINE
  1495. );
  1496. d->rd_bcache_hit_ratio_5min = rrddim_add(d->st_bcache_hit_ratio, "5min", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1497. d->rd_bcache_hit_ratio_1hour = rrddim_add(d->st_bcache_hit_ratio, "1hour", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1498. d->rd_bcache_hit_ratio_1day = rrddim_add(d->st_bcache_hit_ratio, "1day", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1499. d->rd_bcache_hit_ratio_total = rrddim_add(d->st_bcache_hit_ratio, "ever", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1500. add_labels_to_disk(d, d->st_bcache_hit_ratio);
  1501. }
  1502. rrddim_set_by_pointer(d->st_bcache_hit_ratio, d->rd_bcache_hit_ratio_5min, stats_five_minute_cache_hit_ratio);
  1503. rrddim_set_by_pointer(d->st_bcache_hit_ratio, d->rd_bcache_hit_ratio_1hour, stats_hour_cache_hit_ratio);
  1504. rrddim_set_by_pointer(d->st_bcache_hit_ratio, d->rd_bcache_hit_ratio_1day, stats_day_cache_hit_ratio);
  1505. rrddim_set_by_pointer(d->st_bcache_hit_ratio, d->rd_bcache_hit_ratio_total, stats_total_cache_hit_ratio);
  1506. rrdset_done(d->st_bcache_hit_ratio);
  1507. }
  1508. {
  1509. if(unlikely(!d->st_bcache_rates)) {
  1510. d->st_bcache_rates = rrdset_create_localhost(
  1511. "disk_bcache_rates"
  1512. , d->chart_id
  1513. , d->disk
  1514. , family
  1515. , "disk.bcache_rates"
  1516. , "BCache Rates"
  1517. , "KiB/s"
  1518. , PLUGIN_PROC_NAME
  1519. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1520. , NETDATA_CHART_PRIO_BCACHE_RATES
  1521. , update_every
  1522. , RRDSET_TYPE_AREA
  1523. );
  1524. d->rd_bcache_rate_congested = rrddim_add(d->st_bcache_rates, "congested", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
  1525. d->rd_bcache_rate_writeback = rrddim_add(d->st_bcache_rates, "writeback", NULL, -1, 1024, RRD_ALGORITHM_ABSOLUTE);
  1526. add_labels_to_disk(d, d->st_bcache_rates);
  1527. }
  1528. rrddim_set_by_pointer(d->st_bcache_rates, d->rd_bcache_rate_writeback, writeback_rate);
  1529. rrddim_set_by_pointer(d->st_bcache_rates, d->rd_bcache_rate_congested, cache_congested);
  1530. rrdset_done(d->st_bcache_rates);
  1531. }
  1532. {
  1533. if(unlikely(!d->st_bcache_size)) {
  1534. d->st_bcache_size = rrdset_create_localhost(
  1535. "disk_bcache_size"
  1536. , d->chart_id
  1537. , d->disk
  1538. , family
  1539. , "disk.bcache_size"
  1540. , "BCache Cache Sizes"
  1541. , "MiB"
  1542. , PLUGIN_PROC_NAME
  1543. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1544. , NETDATA_CHART_PRIO_BCACHE_SIZE
  1545. , update_every
  1546. , RRDSET_TYPE_AREA
  1547. );
  1548. d->rd_bcache_dirty_size = rrddim_add(d->st_bcache_size, "dirty", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  1549. add_labels_to_disk(d, d->st_bcache_size);
  1550. }
  1551. rrddim_set_by_pointer(d->st_bcache_size, d->rd_bcache_dirty_size, dirty_data);
  1552. rrdset_done(d->st_bcache_size);
  1553. }
  1554. {
  1555. if(unlikely(!d->st_bcache_usage)) {
  1556. d->st_bcache_usage = rrdset_create_localhost(
  1557. "disk_bcache_usage"
  1558. , d->chart_id
  1559. , d->disk
  1560. , family
  1561. , "disk.bcache_usage"
  1562. , "BCache Cache Usage"
  1563. , "percentage"
  1564. , PLUGIN_PROC_NAME
  1565. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1566. , NETDATA_CHART_PRIO_BCACHE_USAGE
  1567. , update_every
  1568. , RRDSET_TYPE_AREA
  1569. );
  1570. d->rd_bcache_available_percent = rrddim_add(d->st_bcache_usage, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1571. add_labels_to_disk(d, d->st_bcache_usage);
  1572. }
  1573. rrddim_set_by_pointer(d->st_bcache_usage, d->rd_bcache_available_percent, cache_available_percent);
  1574. rrdset_done(d->st_bcache_usage);
  1575. }
  1576. {
  1577. if(unlikely(!d->st_bcache_cache_read_races)) {
  1578. d->st_bcache_cache_read_races = rrdset_create_localhost(
  1579. "disk_bcache_cache_read_races"
  1580. , d->chart_id
  1581. , d->disk
  1582. , family
  1583. , "disk.bcache_cache_read_races"
  1584. , "BCache Cache Read Races"
  1585. , "operations/s"
  1586. , PLUGIN_PROC_NAME
  1587. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1588. , NETDATA_CHART_PRIO_BCACHE_CACHE_READ_RACES
  1589. , update_every
  1590. , RRDSET_TYPE_LINE
  1591. );
  1592. d->rd_bcache_cache_read_races = rrddim_add(d->st_bcache_cache_read_races, "races", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1593. d->rd_bcache_cache_io_errors = rrddim_add(d->st_bcache_cache_read_races, "errors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1594. add_labels_to_disk(d, d->st_bcache_cache_read_races);
  1595. }
  1596. rrddim_set_by_pointer(d->st_bcache_cache_read_races, d->rd_bcache_cache_read_races, cache_read_races);
  1597. rrddim_set_by_pointer(d->st_bcache_cache_read_races, d->rd_bcache_cache_io_errors, cache_io_errors);
  1598. rrdset_done(d->st_bcache_cache_read_races);
  1599. }
  1600. if(d->do_bcache == CONFIG_BOOLEAN_YES || (d->do_bcache == CONFIG_BOOLEAN_AUTO &&
  1601. (stats_total_cache_hits ||
  1602. stats_total_cache_misses ||
  1603. stats_total_cache_miss_collisions ||
  1604. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1605. if(unlikely(!d->st_bcache)) {
  1606. d->st_bcache = rrdset_create_localhost(
  1607. "disk_bcache"
  1608. , d->chart_id
  1609. , d->disk
  1610. , family
  1611. , "disk.bcache"
  1612. , "BCache Cache I/O Operations"
  1613. , "operations/s"
  1614. , PLUGIN_PROC_NAME
  1615. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1616. , NETDATA_CHART_PRIO_BCACHE_OPS
  1617. , update_every
  1618. , RRDSET_TYPE_LINE
  1619. );
  1620. rrdset_flag_set(d->st_bcache, RRDSET_FLAG_DETAIL);
  1621. d->rd_bcache_hits = rrddim_add(d->st_bcache, "hits", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1622. d->rd_bcache_misses = rrddim_add(d->st_bcache, "misses", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1623. d->rd_bcache_miss_collisions = rrddim_add(d->st_bcache, "collisions", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1624. d->rd_bcache_readaheads = rrddim_add(d->st_bcache, "readaheads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1625. add_labels_to_disk(d, d->st_bcache);
  1626. }
  1627. rrddim_set_by_pointer(d->st_bcache, d->rd_bcache_hits, stats_total_cache_hits);
  1628. rrddim_set_by_pointer(d->st_bcache, d->rd_bcache_misses, stats_total_cache_misses);
  1629. rrddim_set_by_pointer(d->st_bcache, d->rd_bcache_miss_collisions, stats_total_cache_miss_collisions);
  1630. rrddim_set_by_pointer(d->st_bcache, d->rd_bcache_readaheads, cache_readaheads);
  1631. rrdset_done(d->st_bcache);
  1632. }
  1633. if(d->do_bcache == CONFIG_BOOLEAN_YES || (d->do_bcache == CONFIG_BOOLEAN_AUTO &&
  1634. (stats_total_cache_bypass_hits ||
  1635. stats_total_cache_bypass_misses ||
  1636. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1637. if(unlikely(!d->st_bcache_bypass)) {
  1638. d->st_bcache_bypass = rrdset_create_localhost(
  1639. "disk_bcache_bypass"
  1640. , d->chart_id
  1641. , d->disk
  1642. , family
  1643. , "disk.bcache_bypass"
  1644. , "BCache Cache Bypass I/O Operations"
  1645. , "operations/s"
  1646. , PLUGIN_PROC_NAME
  1647. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1648. , NETDATA_CHART_PRIO_BCACHE_BYPASS
  1649. , update_every
  1650. , RRDSET_TYPE_LINE
  1651. );
  1652. rrdset_flag_set(d->st_bcache_bypass, RRDSET_FLAG_DETAIL);
  1653. d->rd_bcache_bypass_hits = rrddim_add(d->st_bcache_bypass, "hits", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1654. d->rd_bcache_bypass_misses = rrddim_add(d->st_bcache_bypass, "misses", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1655. add_labels_to_disk(d, d->st_bcache_bypass);
  1656. }
  1657. rrddim_set_by_pointer(d->st_bcache_bypass, d->rd_bcache_bypass_hits, stats_total_cache_bypass_hits);
  1658. rrddim_set_by_pointer(d->st_bcache_bypass, d->rd_bcache_bypass_misses, stats_total_cache_bypass_misses);
  1659. rrdset_done(d->st_bcache_bypass);
  1660. }
  1661. }
  1662. }
  1663. // update the system total I/O
  1664. if(global_do_io == CONFIG_BOOLEAN_YES || (global_do_io == CONFIG_BOOLEAN_AUTO &&
  1665. (system_read_kb || system_write_kb ||
  1666. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  1667. static RRDSET *st_io = NULL;
  1668. static RRDDIM *rd_in = NULL, *rd_out = NULL;
  1669. if(unlikely(!st_io)) {
  1670. st_io = rrdset_create_localhost(
  1671. "system"
  1672. , "io"
  1673. , NULL
  1674. , "disk"
  1675. , NULL
  1676. , "Disk I/O"
  1677. , "KiB/s"
  1678. , PLUGIN_PROC_NAME
  1679. , PLUGIN_PROC_MODULE_DISKSTATS_NAME
  1680. , NETDATA_CHART_PRIO_SYSTEM_IO
  1681. , update_every
  1682. , RRDSET_TYPE_AREA
  1683. );
  1684. rd_in = rrddim_add(st_io, "in", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1685. rd_out = rrddim_add(st_io, "out", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  1686. }
  1687. rrddim_set_by_pointer(st_io, rd_in, system_read_kb);
  1688. rrddim_set_by_pointer(st_io, rd_out, system_write_kb);
  1689. rrdset_done(st_io);
  1690. }
  1691. // cleanup removed disks
  1692. struct disk *d = disk_root, *last = NULL;
  1693. while(d) {
  1694. if(unlikely(global_cleanup_removed_disks && !d->updated)) {
  1695. struct disk *t = d;
  1696. rrdset_obsolete_and_pointer_null(d->st_avgsz);
  1697. rrdset_obsolete_and_pointer_null(d->st_ext_avgsz);
  1698. rrdset_obsolete_and_pointer_null(d->st_await);
  1699. rrdset_obsolete_and_pointer_null(d->st_ext_await);
  1700. rrdset_obsolete_and_pointer_null(d->st_backlog);
  1701. rrdset_obsolete_and_pointer_null(d->st_busy);
  1702. rrdset_obsolete_and_pointer_null(d->st_io);
  1703. rrdset_obsolete_and_pointer_null(d->st_ext_io);
  1704. rrdset_obsolete_and_pointer_null(d->st_iotime);
  1705. rrdset_obsolete_and_pointer_null(d->st_ext_iotime);
  1706. rrdset_obsolete_and_pointer_null(d->st_mops);
  1707. rrdset_obsolete_and_pointer_null(d->st_ext_mops);
  1708. rrdset_obsolete_and_pointer_null(d->st_ops);
  1709. rrdset_obsolete_and_pointer_null(d->st_ext_ops);
  1710. rrdset_obsolete_and_pointer_null(d->st_qops);
  1711. rrdset_obsolete_and_pointer_null(d->st_svctm);
  1712. rrdset_obsolete_and_pointer_null(d->st_util);
  1713. rrdset_obsolete_and_pointer_null(d->st_bcache);
  1714. rrdset_obsolete_and_pointer_null(d->st_bcache_bypass);
  1715. rrdset_obsolete_and_pointer_null(d->st_bcache_rates);
  1716. rrdset_obsolete_and_pointer_null(d->st_bcache_size);
  1717. rrdset_obsolete_and_pointer_null(d->st_bcache_usage);
  1718. rrdset_obsolete_and_pointer_null(d->st_bcache_hit_ratio);
  1719. rrdset_obsolete_and_pointer_null(d->st_bcache_cache_allocations);
  1720. rrdset_obsolete_and_pointer_null(d->st_bcache_cache_read_races);
  1721. if(d == disk_root) {
  1722. disk_root = d = d->next;
  1723. last = NULL;
  1724. }
  1725. else if(last) {
  1726. last->next = d = d->next;
  1727. }
  1728. freez(t->bcache_filename_dirty_data);
  1729. freez(t->bcache_filename_writeback_rate);
  1730. freez(t->bcache_filename_cache_congested);
  1731. freez(t->bcache_filename_cache_available_percent);
  1732. freez(t->bcache_filename_stats_five_minute_cache_hit_ratio);
  1733. freez(t->bcache_filename_stats_hour_cache_hit_ratio);
  1734. freez(t->bcache_filename_stats_day_cache_hit_ratio);
  1735. freez(t->bcache_filename_stats_total_cache_hit_ratio);
  1736. freez(t->bcache_filename_stats_total_cache_hits);
  1737. freez(t->bcache_filename_stats_total_cache_misses);
  1738. freez(t->bcache_filename_stats_total_cache_miss_collisions);
  1739. freez(t->bcache_filename_stats_total_cache_bypass_hits);
  1740. freez(t->bcache_filename_stats_total_cache_bypass_misses);
  1741. freez(t->bcache_filename_stats_total_cache_readaheads);
  1742. freez(t->bcache_filename_cache_read_races);
  1743. freez(t->bcache_filename_cache_io_errors);
  1744. freez(t->bcache_filename_priority_stats);
  1745. freez(t->disk);
  1746. freez(t->device);
  1747. freez(t->disk_by_id);
  1748. freez(t->model);
  1749. freez(t->serial);
  1750. freez(t->mount_point);
  1751. freez(t->chart_id);
  1752. freez(t);
  1753. }
  1754. else {
  1755. d->updated = 0;
  1756. last = d;
  1757. d = d->next;
  1758. }
  1759. }
  1760. return 0;
  1761. }