slabinfo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "daemon/common.h"
  3. #include "libnetdata/required_dummies.h"
  4. #define PLUGIN_SLABINFO_NAME "slabinfo.plugin"
  5. #define PLUGIN_SLABINFO_PROCFILE "/proc/slabinfo"
  6. #define CHART_TYPE "mem"
  7. #define CHART_FAMILY "slab"
  8. #define CHART_PRIO 3000
  9. // #define slabdebug(...) if (debug) { fprintf(stderr, __VA_ARGS__); }
  10. #define slabdebug(args...) if (debug) { \
  11. fprintf(stderr, "slabinfo.plugin DEBUG (%04d@%-10.10s:%-15.15s)::", __LINE__, __FILE__, __FUNCTION__); \
  12. fprintf(stderr, ##args); \
  13. fprintf(stderr, "\n"); }
  14. int running = 1;
  15. int debug = 0;
  16. size_t lines_discovered = 0;
  17. int redraw_chart = 0;
  18. // ----------------------------------------------------------------------------
  19. // Slabinfo format :
  20. // format 2.1 Was provided by 57ed3eda977a215f054102b460ab0eb5d8d112e6 (2.6.24-rc6) as:
  21. // seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
  22. // seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  23. // seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  24. //
  25. // With max values:
  26. // seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  27. // cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size, sinfo.objects_per_slab, (1 << sinfo.cache_order));
  28. // seq_printf(m, " : tunables %4u %4u %4u",
  29. // sinfo.limit, sinfo.batchcount, sinfo.shared);
  30. // seq_printf(m, " : slabdata %6lu %6lu %6lu",
  31. // sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  32. //
  33. // If CONFIG_DEBUG_SLAB is set, it will also add columns from slabinfo_show_stats (for SLAB only):
  34. // seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu %4lu %4lu %4lu %4lu %4lu",
  35. // allocs, high, grown, reaped, errors, max_freeable, node_allocs, node_frees, overflows);
  36. // seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  37. // allochit, allocmiss, freehit, freemiss);
  38. //
  39. // Implementation choices:
  40. // - Iterates through a linked list of kmem_cache.
  41. // - Name is a char* from struct kmem_cache (mm/slab.h).
  42. // - max name size found is 24:
  43. // grep -roP 'kmem_cache_create\(".+"'| awk '{split($0,a,"\""); print a[2],length(a[2]); }' | sort -k2 -n
  44. // - Using uint64 everywhere, as types fits and allows to use standard helpers
  45. struct slabinfo {
  46. // procfile fields
  47. const char *name;
  48. uint64_t active_objs;
  49. uint64_t num_objs;
  50. uint64_t obj_size;
  51. uint64_t obj_per_slab;
  52. uint64_t pages_per_slab;
  53. uint64_t tune_limit;
  54. uint64_t tune_batchcnt;
  55. uint64_t tune_shared_factor;
  56. uint64_t data_active_slabs;
  57. uint64_t data_num_slabs;
  58. uint64_t data_shared_avail;
  59. // Calculated fields
  60. uint64_t mem_usage;
  61. uint64_t mem_waste;
  62. uint8_t obj_filling;
  63. uint32_t hash;
  64. struct slabinfo *next;
  65. } *slabinfo_root = NULL, *slabinfo_next = NULL, *slabinfo_last_used = NULL;
  66. // The code is very inspired from "proc_net_dev.c" and "perf_plugin.c"
  67. // Get the existing object, or create a new one
  68. static struct slabinfo *get_slabstruct(const char *name) {
  69. struct slabinfo *s;
  70. slabdebug("--> Requested slabstruct %s", name);
  71. uint32_t hash = simple_hash(name);
  72. // Search it, from the next to the end
  73. for (s = slabinfo_next; s; s = s->next) {
  74. if ((hash = s->hash) && !strcmp(name, s->name)) {
  75. slabdebug("<-- Found existing slabstruct after %s", slabinfo_last_used->name);
  76. // Prepare the next run
  77. slabinfo_next = s->next;
  78. slabinfo_last_used = s;
  79. return s;
  80. }
  81. }
  82. // Search it from the beginning to the last position we used
  83. for (s = slabinfo_root; s != slabinfo_last_used; s = s->next) {
  84. if (hash == s->hash && !strcmp(name, s->name)) {
  85. slabdebug("<-- Found existing slabstruct after root %s", slabinfo_root->name);
  86. slabinfo_next = s->next;
  87. slabinfo_last_used = s;
  88. return s;
  89. }
  90. }
  91. // Create a new one
  92. s = callocz(1, sizeof(struct slabinfo));
  93. s->name = strdupz(name);
  94. s->hash = hash;
  95. // Add it to the current position
  96. if (slabinfo_root) {
  97. slabdebug("<-- Creating new slabstruct after %s", slabinfo_last_used->name);
  98. s->next = slabinfo_last_used->next;
  99. slabinfo_last_used->next = s;
  100. slabinfo_last_used = s;
  101. }
  102. else {
  103. slabdebug("<-- Creating new slabstruct as root");
  104. slabinfo_root = slabinfo_last_used = s;
  105. }
  106. return s;
  107. }
  108. // Read a full pass of slabinfo to update the structs
  109. struct slabinfo *read_file_slabinfo() {
  110. slabdebug("-> Reading procfile %s", PLUGIN_SLABINFO_PROCFILE);
  111. static procfile *ff = NULL;
  112. static long slab_pagesize = 0;
  113. if (unlikely(!slab_pagesize)) {
  114. slab_pagesize = sysconf(_SC_PAGESIZE);
  115. slabdebug(" Discovered pagesize: %ld", slab_pagesize);
  116. }
  117. if(unlikely(!ff)) {
  118. ff = procfile_reopen(ff, PLUGIN_SLABINFO_PROCFILE, " ,:" , PROCFILE_FLAG_DEFAULT);
  119. if(unlikely(!ff)) {
  120. collector_error("<- Cannot open file '%s", PLUGIN_SLABINFO_PROCFILE);
  121. exit(1);
  122. }
  123. }
  124. ff = procfile_readall(ff);
  125. if(unlikely(!ff)) {
  126. collector_error("<- Cannot read file '%s'", PLUGIN_SLABINFO_PROCFILE);
  127. exit(0);
  128. }
  129. // Iterate on all lines to populate / update the slabinfo struct
  130. size_t lines = procfile_lines(ff), l;
  131. if (unlikely(lines != lines_discovered)) {
  132. lines_discovered = lines;
  133. redraw_chart = 1;
  134. }
  135. slabdebug(" Read %lu lines from procfile", (unsigned long)lines);
  136. for(l = 2; l < lines; l++) {
  137. if (unlikely(procfile_linewords(ff, l) < 14)) {
  138. slabdebug(" Line %zu has only %zu words, skipping", l, procfile_linewords(ff,l));
  139. continue;
  140. }
  141. char *name = procfile_lineword(ff, l, 0);
  142. struct slabinfo *s = get_slabstruct(name);
  143. s->active_objs = str2uint64_t(procfile_lineword(ff, l, 1), NULL);
  144. s->num_objs = str2uint64_t(procfile_lineword(ff, l, 2), NULL);
  145. s->obj_size = str2uint64_t(procfile_lineword(ff, l, 3), NULL);
  146. s->obj_per_slab = str2uint64_t(procfile_lineword(ff, l, 4), NULL);
  147. s->pages_per_slab = str2uint64_t(procfile_lineword(ff, l, 5), NULL);
  148. s->tune_limit = str2uint64_t(procfile_lineword(ff, l, 7), NULL);
  149. s->tune_batchcnt = str2uint64_t(procfile_lineword(ff, l, 8), NULL);
  150. s->tune_shared_factor = str2uint64_t(procfile_lineword(ff, l, 9), NULL);
  151. s->data_active_slabs = str2uint64_t(procfile_lineword(ff, l, 11), NULL);
  152. s->data_num_slabs = str2uint64_t(procfile_lineword(ff, l, 12), NULL);
  153. s->data_shared_avail = str2uint64_t(procfile_lineword(ff, l, 13), NULL);
  154. uint32_t memperslab = s->pages_per_slab * slab_pagesize;
  155. // Internal fragmentation: loss per slab, due to objects not being a multiple of pagesize
  156. //uint32_t lossperslab = memperslab - s->obj_per_slab * s->obj_size;
  157. // Total usage = slabs * pages per slab * page size
  158. s->mem_usage = (uint64_t)(s->data_num_slabs * memperslab);
  159. // Wasted memory (filling): slabs allocated but not filled: sum total slab - sum total objects
  160. s->mem_waste = s->mem_usage - (uint64_t)(s->active_objs * s->obj_size);
  161. //if (s->data_num_slabs > 1)
  162. // s->mem_waste += s->data_num_slabs * lossperslab;
  163. // Slab filling efficiency
  164. if (s->num_objs > 0)
  165. s->obj_filling = 100 * s->active_objs / s->num_objs;
  166. else
  167. s->obj_filling = 0;
  168. slabdebug(" Updated slab %s: %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" / %"PRIu64" %"PRIu64" %"PRIu64" / %"PRIu64" %"PRIu64" %"PRIu64" / %"PRIu64" %"PRIu64" %hhu",
  169. name, s->active_objs, s->num_objs, s->obj_size, s->obj_per_slab, s->pages_per_slab,
  170. s->tune_limit, s->tune_batchcnt, s->tune_shared_factor,
  171. s->data_active_slabs, s->data_num_slabs, s->data_shared_avail,
  172. s->mem_usage, s->mem_waste, s->obj_filling);
  173. }
  174. return slabinfo_root;
  175. }
  176. unsigned int do_slab_stats(int update_every) {
  177. static unsigned int loops = 0;
  178. struct slabinfo *sactive = NULL, *s = NULL;
  179. // Main processing loop
  180. while (running) {
  181. sactive = read_file_slabinfo();
  182. // Init Charts
  183. if (unlikely(redraw_chart)) {
  184. redraw_chart = 0;
  185. // Memory Usage
  186. printf("CHART %s.%s '' 'Memory Usage' 'B' '%s' '' line %d %d %s\n"
  187. , CHART_TYPE
  188. , "slabmemory"
  189. , CHART_FAMILY
  190. , CHART_PRIO
  191. , update_every
  192. , PLUGIN_SLABINFO_NAME
  193. );
  194. for (s = sactive; s; s = s->next) {
  195. printf("DIMENSION %s '' absolute 1 1\n", s->name);
  196. }
  197. // Slab active usage (filling)
  198. printf("CHART %s.%s '' 'Object Filling' '%%' '%s' '' line %d %d %s\n"
  199. , CHART_TYPE
  200. , "slabfilling"
  201. , CHART_FAMILY
  202. , CHART_PRIO + 1
  203. , update_every
  204. , PLUGIN_SLABINFO_NAME
  205. );
  206. for (s = sactive; s; s = s->next) {
  207. printf("DIMENSION %s '' absolute 1 1\n", s->name);
  208. }
  209. // Memory waste
  210. printf("CHART %s.%s '' 'Memory waste' 'B' '%s' '' line %d %d %s\n"
  211. , CHART_TYPE
  212. , "slabwaste"
  213. , CHART_FAMILY
  214. , CHART_PRIO + 2
  215. , update_every
  216. , PLUGIN_SLABINFO_NAME
  217. );
  218. for (s = sactive; s; s = s->next) {
  219. printf("DIMENSION %s '' absolute 1 1\n", s->name);
  220. }
  221. }
  222. //
  223. // Memory usage
  224. //
  225. printf("BEGIN %s.%s\n"
  226. , CHART_TYPE
  227. , "slabmemory"
  228. );
  229. for (s = sactive; s; s = s->next) {
  230. printf("SET %s = %"PRIu64"\n"
  231. , s->name
  232. , s->mem_usage
  233. );
  234. }
  235. printf("END\n");
  236. //
  237. // Slab active usage
  238. //
  239. printf("BEGIN %s.%s\n"
  240. , CHART_TYPE
  241. , "slabfilling"
  242. );
  243. for (s = sactive; s; s = s->next) {
  244. printf("SET %s = %u\n"
  245. , s->name
  246. , s->obj_filling
  247. );
  248. }
  249. printf("END\n");
  250. //
  251. // Memory waste
  252. //
  253. printf("BEGIN %s.%s\n"
  254. , CHART_TYPE
  255. , "slabwaste"
  256. );
  257. for (s = sactive; s; s = s->next) {
  258. printf("SET %s = %"PRIu64"\n"
  259. , s->name
  260. , s->mem_waste
  261. );
  262. }
  263. printf("END\n");
  264. loops++;
  265. sleep(update_every);
  266. }
  267. return loops;
  268. }
  269. // ----------------------------------------------------------------------------
  270. // main
  271. void usage(void) {
  272. fprintf(stderr, "%s\n", program_name);
  273. exit(1);
  274. }
  275. int main(int argc, char **argv) {
  276. stderror = stderr;
  277. clocks_init();
  278. program_name = argv[0];
  279. program_version = "0.1";
  280. error_log_syslog = 0;
  281. int update_every = 1, i, n, freq = 0;
  282. for (i = 1; i < argc; i++) {
  283. // Frequency parsing
  284. if(isdigit(*argv[i]) && !freq) {
  285. n = (int) str2l(argv[i]);
  286. if (n > 0) {
  287. if (n >= UPDATE_EVERY_MAX) {
  288. collector_error("Invalid interval value: %s", argv[i]);
  289. exit(1);
  290. }
  291. freq = n;
  292. }
  293. }
  294. else if (strcmp("debug", argv[i]) == 0) {
  295. debug = 1;
  296. continue;
  297. }
  298. else {
  299. fprintf(stderr,
  300. "netdata slabinfo.plugin %s\n"
  301. "This program is a data collector plugin for netdata.\n"
  302. "\n"
  303. "Available command line options:\n"
  304. "\n"
  305. " COLLECTION_FREQUENCY data collection frequency in seconds\n"
  306. " minimum: %d\n"
  307. "\n"
  308. " debug enable verbose output\n"
  309. " default: disabled\n"
  310. "\n",
  311. program_version,
  312. update_every
  313. );
  314. exit(1);
  315. }
  316. }
  317. if(freq >= update_every)
  318. update_every = freq;
  319. else if(freq)
  320. collector_error("update frequency %d seconds is too small for slabinfo. Using %d.", freq, update_every);
  321. // Call the main function. Time drift to be added
  322. do_slab_stats(update_every);
  323. return 0;
  324. }