sys_block_zram.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. #define PLUGIN_PROC_MODULE_ZRAM_NAME "/sys/block/zram"
  4. #define rrdset_obsolete_and_pointer_null(st) do { if(st) { rrdset_is_obsolete(st); (st) = NULL; } } while(st)
  5. typedef struct mm_stat {
  6. unsigned long long orig_data_size;
  7. unsigned long long compr_data_size;
  8. unsigned long long mem_used_total;
  9. unsigned long long mem_limit;
  10. unsigned long long mem_used_max;
  11. unsigned long long same_pages;
  12. unsigned long long pages_compacted;
  13. } MM_STAT;
  14. typedef struct zram_device {
  15. procfile *file;
  16. RRDSET *st_usage;
  17. RRDDIM *rd_compr_data_size;
  18. RRDDIM *rd_metadata_size;
  19. RRDSET *st_savings;
  20. RRDDIM *rd_original_size;
  21. RRDDIM *rd_savings_size;
  22. RRDSET *st_comp_ratio;
  23. RRDDIM *rd_comp_ratio;
  24. RRDSET *st_alloc_efficiency;
  25. RRDDIM *rd_alloc_efficiency;
  26. } ZRAM_DEVICE;
  27. // --------------------------------------------------------------------
  28. static int try_get_zram_major_number(procfile *file) {
  29. size_t i;
  30. unsigned int lines = procfile_lines(file);
  31. int id = -1;
  32. char *name = NULL;
  33. for (i = 0; i < lines; i++)
  34. {
  35. if (procfile_linewords(file, i) < 2)
  36. continue;
  37. name = procfile_lineword(file, i, 1);
  38. if (strcmp(name, "zram") == 0)
  39. {
  40. id = str2i(procfile_lineword(file, i, 0));
  41. if (id == 0)
  42. return -1;
  43. return id;
  44. }
  45. }
  46. return -1;
  47. }
  48. static inline void init_rrd(const char *name, ZRAM_DEVICE *d, int update_every) {
  49. char chart_name[RRD_ID_LENGTH_MAX + 1];
  50. snprintfz(chart_name, RRD_ID_LENGTH_MAX, "zram_usage.%s", name);
  51. d->st_usage = rrdset_create_localhost(
  52. "mem"
  53. , chart_name
  54. , chart_name
  55. , name
  56. , "mem.zram_usage"
  57. , "ZRAM Memory Usage"
  58. , "MiB"
  59. , PLUGIN_PROC_NAME
  60. , PLUGIN_PROC_MODULE_ZRAM_NAME
  61. , NETDATA_CHART_PRIO_MEM_ZRAM
  62. , update_every
  63. , RRDSET_TYPE_AREA);
  64. d->rd_compr_data_size = rrddim_add(d->st_usage, "compressed", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  65. d->rd_metadata_size = rrddim_add(d->st_usage, "metadata", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  66. snprintfz(chart_name, RRD_ID_LENGTH_MAX, "zram_savings.%s", name);
  67. d->st_savings = rrdset_create_localhost(
  68. "mem"
  69. , chart_name
  70. , chart_name
  71. , name
  72. , "mem.zram_savings"
  73. , "ZRAM Memory Savings"
  74. , "MiB"
  75. , PLUGIN_PROC_NAME
  76. , PLUGIN_PROC_MODULE_ZRAM_NAME
  77. , NETDATA_CHART_PRIO_MEM_ZRAM_SAVINGS
  78. , update_every
  79. , RRDSET_TYPE_AREA);
  80. d->rd_savings_size = rrddim_add(d->st_savings, "savings", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  81. d->rd_original_size = rrddim_add(d->st_savings, "original", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  82. snprintfz(chart_name, RRD_ID_LENGTH_MAX, "zram_ratio.%s", name);
  83. d->st_comp_ratio = rrdset_create_localhost(
  84. "mem"
  85. , chart_name
  86. , chart_name
  87. , name
  88. , "mem.zram_ratio"
  89. , "ZRAM Compression Ratio (original to compressed)"
  90. , "ratio"
  91. , PLUGIN_PROC_NAME
  92. , PLUGIN_PROC_MODULE_ZRAM_NAME
  93. , NETDATA_CHART_PRIO_MEM_ZRAM_RATIO
  94. , update_every
  95. , RRDSET_TYPE_LINE);
  96. d->rd_comp_ratio = rrddim_add(d->st_comp_ratio, "ratio", NULL, 1, 100, RRD_ALGORITHM_ABSOLUTE);
  97. snprintfz(chart_name, RRD_ID_LENGTH_MAX, "zram_efficiency.%s", name);
  98. d->st_alloc_efficiency = rrdset_create_localhost(
  99. "mem"
  100. , chart_name
  101. , chart_name
  102. , name
  103. , "mem.zram_efficiency"
  104. , "ZRAM Efficiency"
  105. , "percentage"
  106. , PLUGIN_PROC_NAME
  107. , PLUGIN_PROC_MODULE_ZRAM_NAME
  108. , NETDATA_CHART_PRIO_MEM_ZRAM_EFFICIENCY
  109. , update_every
  110. , RRDSET_TYPE_LINE);
  111. d->rd_alloc_efficiency = rrddim_add(d->st_alloc_efficiency, "percent", NULL, 1, 10000, RRD_ALGORITHM_ABSOLUTE);
  112. }
  113. static int init_devices(DICTIONARY *devices, unsigned int zram_id, int update_every) {
  114. int count = 0;
  115. DIR *dir = opendir("/dev");
  116. struct dirent *de;
  117. struct stat st;
  118. char filename[FILENAME_MAX + 1];
  119. procfile *ff = NULL;
  120. ZRAM_DEVICE device;
  121. if (unlikely(!dir))
  122. return 0;
  123. while ((de = readdir(dir)))
  124. {
  125. snprintfz(filename, FILENAME_MAX, "/dev/%s", de->d_name);
  126. if (unlikely(stat(filename, &st) != 0))
  127. {
  128. error("ZRAM : Unable to stat %s: %s", filename, strerror(errno));
  129. continue;
  130. }
  131. if (major(st.st_rdev) == zram_id)
  132. {
  133. info("ZRAM : Found device %s", filename);
  134. snprintfz(filename, FILENAME_MAX, "/sys/block/%s/mm_stat", de->d_name);
  135. ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  136. if (ff == NULL)
  137. {
  138. error("ZRAM : Failed to open %s: %s", filename, strerror(errno));
  139. continue;
  140. }
  141. device.file = ff;
  142. init_rrd(de->d_name, &device, update_every);
  143. dictionary_set(devices, de->d_name, &device, sizeof(ZRAM_DEVICE));
  144. count++;
  145. }
  146. }
  147. closedir(dir);
  148. return count;
  149. }
  150. static void free_device(DICTIONARY *dict, const char *name)
  151. {
  152. ZRAM_DEVICE *d = (ZRAM_DEVICE*)dictionary_get(dict, name);
  153. info("ZRAM : Disabling monitoring of device %s", name);
  154. rrdset_obsolete_and_pointer_null(d->st_usage);
  155. rrdset_obsolete_and_pointer_null(d->st_savings);
  156. rrdset_obsolete_and_pointer_null(d->st_alloc_efficiency);
  157. rrdset_obsolete_and_pointer_null(d->st_comp_ratio);
  158. dictionary_del_having_write_lock(dict, name);
  159. }
  160. // --------------------------------------------------------------------
  161. static inline int read_mm_stat(procfile *ff, MM_STAT *stats) {
  162. ff = procfile_readall(ff);
  163. if (!ff)
  164. return -1;
  165. if (procfile_lines(ff) < 1) {
  166. procfile_close(ff);
  167. return -1;
  168. }
  169. if (procfile_linewords(ff, 0) < 7) {
  170. procfile_close(ff);
  171. return -1;
  172. }
  173. stats->orig_data_size = str2ull(procfile_word(ff, 0));
  174. stats->compr_data_size = str2ull(procfile_word(ff, 1));
  175. stats->mem_used_total = str2ull(procfile_word(ff, 2));
  176. stats->mem_limit = str2ull(procfile_word(ff, 3));
  177. stats->mem_used_max = str2ull(procfile_word(ff, 4));
  178. stats->same_pages = str2ull(procfile_word(ff, 5));
  179. stats->pages_compacted = str2ull(procfile_word(ff, 6));
  180. return 0;
  181. }
  182. static inline int _collect_zram_metrics(const char* name, ZRAM_DEVICE *d, int advance, DICTIONARY* dict) {
  183. MM_STAT mm;
  184. int value;
  185. if (unlikely(read_mm_stat(d->file, &mm) < 0))
  186. {
  187. free_device(dict, name);
  188. return -1;
  189. }
  190. if (likely(advance))
  191. {
  192. rrdset_next(d->st_usage);
  193. rrdset_next(d->st_savings);
  194. rrdset_next(d->st_comp_ratio);
  195. rrdset_next(d->st_alloc_efficiency);
  196. }
  197. // zram_usage
  198. rrddim_set_by_pointer(d->st_usage, d->rd_compr_data_size, mm.compr_data_size);
  199. rrddim_set_by_pointer(d->st_usage, d->rd_metadata_size, mm.mem_used_total - mm.compr_data_size);
  200. rrdset_done(d->st_usage);
  201. // zram_savings
  202. rrddim_set_by_pointer(d->st_savings, d->rd_savings_size, mm.compr_data_size - mm.orig_data_size);
  203. rrddim_set_by_pointer(d->st_savings, d->rd_original_size, mm.orig_data_size);
  204. rrdset_done(d->st_savings);
  205. // zram_ratio
  206. value = mm.compr_data_size == 0 ? 1 : mm.orig_data_size * 100 / mm.compr_data_size;
  207. rrddim_set_by_pointer(d->st_comp_ratio, d->rd_comp_ratio, value);
  208. rrdset_done(d->st_comp_ratio);
  209. // zram_efficiency
  210. value = mm.mem_used_total == 0 ? 100 : (mm.compr_data_size * 1000000 / mm.mem_used_total);
  211. rrddim_set_by_pointer(d->st_alloc_efficiency, d->rd_alloc_efficiency, value);
  212. rrdset_done(d->st_alloc_efficiency);
  213. return 0;
  214. }
  215. static int collect_first_zram_metrics(const char *name, void *entry, void *data) {
  216. // collect without calling rrdset_next (init only)
  217. return _collect_zram_metrics(name, (ZRAM_DEVICE *)entry, 0, (DICTIONARY *)data);
  218. }
  219. static int collect_zram_metrics(const char *name, void *entry, void *data) {
  220. (void)name;
  221. // collect with calling rrdset_next
  222. return _collect_zram_metrics(name, (ZRAM_DEVICE *)entry, 1, (DICTIONARY *)data);
  223. }
  224. // --------------------------------------------------------------------
  225. int do_sys_block_zram(int update_every, usec_t dt) {
  226. (void)dt;
  227. static procfile *ff = NULL;
  228. static DICTIONARY *devices = NULL;
  229. static int initialized = 0;
  230. static int device_count = 0;
  231. int zram_id = -1;
  232. if (unlikely(!initialized))
  233. {
  234. initialized = 1;
  235. ff = procfile_open("/proc/devices", " \t:", PROCFILE_FLAG_DEFAULT);
  236. if (ff == NULL)
  237. {
  238. error("Cannot read /proc/devices");
  239. return 1;
  240. }
  241. ff = procfile_readall(ff);
  242. if (!ff)
  243. return 1;
  244. zram_id = try_get_zram_major_number(ff);
  245. if (zram_id == -1)
  246. {
  247. if (ff != NULL)
  248. procfile_close(ff);
  249. return 1;
  250. }
  251. procfile_close(ff);
  252. devices = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
  253. device_count = init_devices(devices, (unsigned int)zram_id, update_every);
  254. if (device_count < 1)
  255. return 1;
  256. dictionary_walkthrough_write(devices, collect_first_zram_metrics, devices);
  257. }
  258. else
  259. {
  260. if (unlikely(device_count < 1))
  261. return 1;
  262. dictionary_walkthrough_write(devices, collect_zram_metrics, devices);
  263. }
  264. return 0;
  265. }