proc_diskstats.c 72 KB

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