debugfs_extfrag.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "debugfs_plugin.h"
  3. #define NETDATA_ORDER_FRAGMENTATION 11
  4. static char *orders[NETDATA_ORDER_FRAGMENTATION] = { "order0", "order1", "order2", "order3", "order4",
  5. "order5", "order6", "order7", "order8", "order9",
  6. "order10"
  7. };
  8. static struct netdata_extrafrag {
  9. char *node_zone;
  10. uint32_t hash;
  11. char *id;
  12. collected_number orders[NETDATA_ORDER_FRAGMENTATION];
  13. struct netdata_extrafrag *next;
  14. } *netdata_extrafrags_root = NULL;
  15. static struct netdata_extrafrag *find_or_create_extrafrag(const char *name)
  16. {
  17. struct netdata_extrafrag *extrafrag;
  18. uint32_t hash = simple_hash(name);
  19. // search it, from beginning to the end
  20. for (extrafrag = netdata_extrafrags_root ; extrafrag ; extrafrag = extrafrag->next) {
  21. if (unlikely(hash == extrafrag->hash && !strcmp(name, extrafrag->node_zone))) {
  22. return extrafrag;
  23. }
  24. }
  25. extrafrag = callocz(1, sizeof(struct netdata_extrafrag));
  26. extrafrag->node_zone = strdupz(name);
  27. extrafrag->hash = hash;
  28. if (netdata_extrafrags_root) {
  29. struct netdata_extrafrag *last_node;
  30. for (last_node = netdata_extrafrags_root; last_node->next ; last_node = last_node->next);
  31. last_node->next = extrafrag;
  32. } else
  33. netdata_extrafrags_root = extrafrag;
  34. return extrafrag;
  35. }
  36. static void extfrag_send_chart(char *chart_id, collected_number *values)
  37. {
  38. int i;
  39. fprintf(stdout, "BEGIN mem.fragmentation_index_%s\n", chart_id);
  40. for (i = 0; i < NETDATA_ORDER_FRAGMENTATION; i++) {
  41. fprintf(stdout, "SET %s = %lld\n", orders[i], values[i]);
  42. }
  43. fprintf(stdout, "END\n");
  44. fflush(stdout);
  45. }
  46. int do_debugfs_extfrag(int update_every, const char *name) {
  47. static procfile *ff = NULL;
  48. static int chart_order = NETDATA_CHART_PRIO_MEM_FRAGMENTATION;
  49. if (unlikely(!ff)) {
  50. char filename[FILENAME_MAX + 1];
  51. snprintfz(filename,
  52. FILENAME_MAX,
  53. "%s%s",
  54. netdata_configured_host_prefix,
  55. "/sys/kernel/debug/extfrag/extfrag_index");
  56. ff = procfile_open(filename, " \t,", PROCFILE_FLAG_DEFAULT);
  57. if (unlikely(!ff)) return 1;
  58. }
  59. ff = procfile_readall(ff);
  60. if (unlikely(!ff)) return 1;
  61. size_t l, i, j, lines = procfile_lines(ff);
  62. for (l = 0; l < lines; l++) {
  63. char chart_id[64];
  64. char zone_lowercase[32];
  65. if (unlikely(procfile_linewords(ff, l) < 15)) continue;
  66. char *zone = procfile_lineword(ff, l, 3);
  67. strncpyz(zone_lowercase, zone, 31);
  68. debugfs2lower(zone_lowercase);
  69. char *id = procfile_lineword(ff, l, 1);
  70. snprintfz(chart_id, 63, "node_%s_%s", id, zone_lowercase);
  71. debugfs2lower(chart_id);
  72. struct netdata_extrafrag *extrafrag = find_or_create_extrafrag(chart_id);
  73. collected_number *line_orders = extrafrag->orders;
  74. for (i = 4, j = 0 ; i < 15; i++, j++) {
  75. NETDATA_DOUBLE value = str2ndd(procfile_lineword(ff, l, i), NULL);
  76. line_orders[j] = (collected_number) (value * 1000.0);
  77. }
  78. if (unlikely(!extrafrag->id)) {
  79. extrafrag->id = extrafrag->node_zone;
  80. fprintf(
  81. stdout,
  82. "CHART mem.fragmentation_index_%s '' 'Memory fragmentation index for each order' 'index' 'fragmentation' 'mem.fragmentation_index_%s' 'line' %d %d '' 'debugfs.plugin' '%s'\n",
  83. extrafrag->node_zone,
  84. zone_lowercase,
  85. chart_order++, // FIXME: the same zones must have the same order
  86. update_every,
  87. name);
  88. for (i = 0; i < NETDATA_ORDER_FRAGMENTATION; i++) {
  89. fprintf(stdout, "DIMENSION '%s' '%s' absolute 1 1000 ''\n", orders[i], orders[i]);
  90. }
  91. fprintf(stdout,
  92. "CLABEL 'numa_node' 'node%s' 1\n"
  93. "CLABEL_COMMIT\n",
  94. id);
  95. }
  96. extfrag_send_chart(chart_id, line_orders);
  97. }
  98. return 0;
  99. }