ebpf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <dlfcn.h>
  6. #include <sys/utsname.h>
  7. #include "../libnetdata.h"
  8. char *ebpf_user_config_dir = CONFIG_DIR;
  9. char *ebpf_stock_config_dir = LIBCONFIG_DIR;
  10. /*
  11. static int clean_kprobe_event(FILE *out, char *filename, char *father_pid, netdata_ebpf_events_t *ptr)
  12. {
  13. int fd = open(filename, O_WRONLY | O_APPEND, 0);
  14. if (fd < 0) {
  15. if (out) {
  16. fprintf(out, "Cannot open %s : %s\n", filename, strerror(errno));
  17. }
  18. return 1;
  19. }
  20. char cmd[1024];
  21. int length = snprintf(cmd, 1023, "-:kprobes/%c_netdata_%s_%s", ptr->type, ptr->name, father_pid);
  22. int ret = 0;
  23. if (length > 0) {
  24. ssize_t written = write(fd, cmd, strlen(cmd));
  25. if (written < 0) {
  26. if (out) {
  27. fprintf(
  28. out, "Cannot remove the event (%d, %d) '%s' from %s : %s\n", getppid(), getpid(), cmd, filename,
  29. strerror((int)errno));
  30. }
  31. ret = 1;
  32. }
  33. }
  34. close(fd);
  35. return ret;
  36. }
  37. int clean_kprobe_events(FILE *out, int pid, netdata_ebpf_events_t *ptr)
  38. {
  39. debug(D_EXIT, "Cleaning parent process events.");
  40. char filename[FILENAME_MAX + 1];
  41. snprintf(filename, FILENAME_MAX, "%s%s", NETDATA_DEBUGFS, "kprobe_events");
  42. char removeme[16];
  43. snprintf(removeme, 15, "%d", pid);
  44. int i;
  45. for (i = 0; ptr[i].name; i++) {
  46. if (clean_kprobe_event(out, filename, removeme, &ptr[i])) {
  47. break;
  48. }
  49. }
  50. return 0;
  51. }
  52. */
  53. //----------------------------------------------------------------------------------------------------------------------
  54. int get_kernel_version(char *out, int size)
  55. {
  56. char major[16], minor[16], patch[16];
  57. char ver[VERSION_STRING_LEN];
  58. char *version = ver;
  59. out[0] = '\0';
  60. int fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
  61. if (fd < 0)
  62. return -1;
  63. ssize_t len = read(fd, ver, sizeof(ver));
  64. if (len < 0) {
  65. close(fd);
  66. return -1;
  67. }
  68. close(fd);
  69. char *move = major;
  70. while (*version && *version != '.')
  71. *move++ = *version++;
  72. *move = '\0';
  73. version++;
  74. move = minor;
  75. while (*version && *version != '.')
  76. *move++ = *version++;
  77. *move = '\0';
  78. if (*version)
  79. version++;
  80. else
  81. return -1;
  82. move = patch;
  83. while (*version && *version != '\n' && *version != '-')
  84. *move++ = *version++;
  85. *move = '\0';
  86. fd = snprintf(out, (size_t)size, "%s.%s.%s", major, minor, patch);
  87. if (fd > size)
  88. error("The buffer to store kernel version is not smaller than necessary.");
  89. return ((int)(str2l(major) * 65536) + (int)(str2l(minor) * 256) + (int)str2l(patch));
  90. }
  91. int get_redhat_release()
  92. {
  93. char buffer[VERSION_STRING_LEN + 1];
  94. int major, minor;
  95. FILE *fp = fopen("/etc/redhat-release", "r");
  96. if (fp) {
  97. major = 0;
  98. minor = -1;
  99. size_t length = fread(buffer, sizeof(char), VERSION_STRING_LEN, fp);
  100. if (length > 4) {
  101. buffer[length] = '\0';
  102. char *end = strchr(buffer, '.');
  103. char *start;
  104. if (end) {
  105. *end = 0x0;
  106. if (end > buffer) {
  107. start = end - 1;
  108. major = strtol(start, NULL, 10);
  109. start = ++end;
  110. end++;
  111. if (end) {
  112. end = 0x00;
  113. minor = strtol(start, NULL, 10);
  114. } else {
  115. minor = -1;
  116. }
  117. }
  118. }
  119. }
  120. fclose(fp);
  121. return ((major * 256) + minor);
  122. } else {
  123. return -1;
  124. }
  125. }
  126. /**
  127. * Check if the kernel is in a list of rejected ones
  128. *
  129. * @return Returns 1 if the kernel is rejected, 0 otherwise.
  130. */
  131. static int kernel_is_rejected()
  132. {
  133. // Get kernel version from system
  134. char version_string[VERSION_STRING_LEN + 1];
  135. int version_string_len = 0;
  136. if (read_file("/proc/version_signature", version_string, VERSION_STRING_LEN)) {
  137. if (read_file("/proc/version", version_string, VERSION_STRING_LEN)) {
  138. struct utsname uname_buf;
  139. if (!uname(&uname_buf)) {
  140. info("Cannot check kernel version");
  141. return 0;
  142. }
  143. version_string_len =
  144. snprintfz(version_string, VERSION_STRING_LEN, "%s %s", uname_buf.release, uname_buf.version);
  145. }
  146. }
  147. if (!version_string_len)
  148. version_string_len = strlen(version_string);
  149. // Open a file with a list of rejected kernels
  150. char *config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  151. if (config_dir == NULL) {
  152. config_dir = CONFIG_DIR;
  153. }
  154. char filename[FILENAME_MAX + 1];
  155. snprintfz(filename, FILENAME_MAX, "%s/ebpf.d/%s", config_dir, EBPF_KERNEL_REJECT_LIST_FILE);
  156. FILE *kernel_reject_list = fopen(filename, "r");
  157. if (!kernel_reject_list) {
  158. // Keep this to have compatibility with old versions
  159. snprintfz(filename, FILENAME_MAX, "%s/%s", config_dir, EBPF_KERNEL_REJECT_LIST_FILE);
  160. kernel_reject_list = fopen(filename, "r");
  161. if (!kernel_reject_list) {
  162. config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  163. if (config_dir == NULL) {
  164. config_dir = LIBCONFIG_DIR;
  165. }
  166. snprintfz(filename, FILENAME_MAX, "%s/ebpf.d/%s", config_dir, EBPF_KERNEL_REJECT_LIST_FILE);
  167. kernel_reject_list = fopen(filename, "r");
  168. if (!kernel_reject_list)
  169. return 0;
  170. }
  171. }
  172. // Find if the kernel is in the reject list
  173. char *reject_string = NULL;
  174. size_t buf_len = 0;
  175. ssize_t reject_string_len;
  176. while ((reject_string_len = getline(&reject_string, &buf_len, kernel_reject_list) - 1) > 0) {
  177. if (version_string_len >= reject_string_len) {
  178. if (!strncmp(version_string, reject_string, reject_string_len)) {
  179. info("A buggy kernel is detected");
  180. fclose(kernel_reject_list);
  181. freez(reject_string);
  182. return 1;
  183. }
  184. }
  185. }
  186. fclose(kernel_reject_list);
  187. freez(reject_string);
  188. return 0;
  189. }
  190. static int has_ebpf_kernel_version(int version)
  191. {
  192. if (kernel_is_rejected())
  193. return 0;
  194. // Kernel 4.11.0 or RH > 7.5
  195. return (version >= NETDATA_MINIMUM_EBPF_KERNEL || get_redhat_release() >= NETDATA_MINIMUM_RH_VERSION);
  196. }
  197. int has_condition_to_run(int version)
  198. {
  199. if (!has_ebpf_kernel_version(version))
  200. return 0;
  201. return 1;
  202. }
  203. //----------------------------------------------------------------------------------------------------------------------
  204. char *ebpf_kernel_suffix(int version, int isrh)
  205. {
  206. if (isrh) {
  207. if (version >= NETDATA_EBPF_KERNEL_4_11)
  208. return "4.18";
  209. else
  210. return "3.10";
  211. } else {
  212. if (version >= NETDATA_EBPF_KERNEL_5_11)
  213. return "5.11";
  214. else if (version >= NETDATA_EBPF_KERNEL_5_10)
  215. return "5.10";
  216. else if (version >= NETDATA_EBPF_KERNEL_4_17)
  217. return "5.4";
  218. else if (version >= NETDATA_EBPF_KERNEL_4_15)
  219. return "4.16";
  220. else if (version >= NETDATA_EBPF_KERNEL_4_11)
  221. return "4.14";
  222. }
  223. return NULL;
  224. }
  225. //----------------------------------------------------------------------------------------------------------------------
  226. int ebpf_update_kernel(ebpf_data_t *ed)
  227. {
  228. char *kernel = ebpf_kernel_suffix(ed->running_on_kernel, (ed->isrh < 0) ? 0 : 1);
  229. size_t length = strlen(kernel);
  230. strncpyz(ed->kernel_string, kernel, length);
  231. ed->kernel_string[length] = '\0';
  232. return 0;
  233. }
  234. static int select_file(char *name, const char *program, size_t length, int mode, char *kernel_string)
  235. {
  236. int ret = -1;
  237. if (!mode)
  238. ret = snprintf(name, length, "rnetdata_ebpf_%s.%s.o", program, kernel_string);
  239. else if (mode == 1)
  240. ret = snprintf(name, length, "dnetdata_ebpf_%s.%s.o", program, kernel_string);
  241. else if (mode == 2)
  242. ret = snprintf(name, length, "pnetdata_ebpf_%s.%s.o", program, kernel_string);
  243. return ret;
  244. }
  245. void ebpf_update_pid_table(ebpf_local_maps_t *pid, ebpf_module_t *em)
  246. {
  247. pid->user_input = em->pid_map_size;
  248. }
  249. void ebpf_update_map_sizes(struct bpf_object *program, ebpf_module_t *em)
  250. {
  251. struct bpf_map *map;
  252. ebpf_local_maps_t *maps = em->maps;
  253. if (!maps)
  254. return;
  255. uint32_t apps_type = NETDATA_EBPF_MAP_PID | NETDATA_EBPF_MAP_RESIZABLE;
  256. bpf_map__for_each(map, program)
  257. {
  258. const char *map_name = bpf_map__name(map);
  259. int i = 0; ;
  260. while (maps[i].name) {
  261. ebpf_local_maps_t *w = &maps[i];
  262. if (w->type & NETDATA_EBPF_MAP_RESIZABLE) {
  263. if (!strcmp(w->name, map_name)) {
  264. if (w->user_input && w->user_input != w->internal_input) {
  265. #ifdef NETDATA_INTERNAL_CHECKS
  266. info("Changing map %s from size %u to %u ", map_name, w->internal_input, w->user_input);
  267. #endif
  268. bpf_map__resize(map, w->user_input);
  269. } else if (((w->type & apps_type) == apps_type) && (!em->apps_charts)) {
  270. w->user_input = ND_EBPF_DEFAULT_MIN_PID;
  271. bpf_map__resize(map, w->user_input);
  272. }
  273. }
  274. }
  275. i++;
  276. }
  277. }
  278. }
  279. size_t ebpf_count_programs(struct bpf_object *obj)
  280. {
  281. size_t tot = 0;
  282. struct bpf_program *prog;
  283. bpf_object__for_each_program(prog, obj)
  284. {
  285. tot++;
  286. }
  287. return tot;
  288. }
  289. static ebpf_specify_name_t *ebpf_find_names(ebpf_specify_name_t *names, const char *prog_name)
  290. {
  291. size_t i = 0;
  292. while (names[i].program_name) {
  293. if (!strcmp(prog_name, names[i].program_name))
  294. return &names[i];
  295. i++;
  296. }
  297. return NULL;
  298. }
  299. static struct bpf_link **ebpf_attach_programs(struct bpf_object *obj, size_t length, ebpf_specify_name_t *names)
  300. {
  301. struct bpf_link **links = callocz(length , sizeof(struct bpf_link *));
  302. size_t i = 0;
  303. struct bpf_program *prog;
  304. bpf_object__for_each_program(prog, obj)
  305. {
  306. links[i] = bpf_program__attach(prog);
  307. if (libbpf_get_error(links[i]) && names) {
  308. const char *name = bpf_program__name(prog);
  309. ebpf_specify_name_t *w = ebpf_find_names(names, name);
  310. if (w) {
  311. enum bpf_prog_type type = bpf_program__get_type(prog);
  312. if (type == BPF_PROG_TYPE_KPROBE)
  313. links[i] = bpf_program__attach_kprobe(prog, w->retprobe, w->optional);
  314. }
  315. }
  316. if (libbpf_get_error(links[i])) {
  317. links[i] = NULL;
  318. }
  319. i++;
  320. }
  321. return links;
  322. }
  323. static void ebpf_update_maps(ebpf_module_t *em, int *map_fd, struct bpf_object *obj)
  324. {
  325. if (!map_fd)
  326. return;
  327. ebpf_local_maps_t *maps = em->maps;
  328. struct bpf_map *map;
  329. size_t i = 0;
  330. bpf_map__for_each(map, obj)
  331. {
  332. int fd = bpf_map__fd(map);
  333. if (maps) {
  334. const char *map_name = bpf_map__name(map);
  335. int j = 0; ;
  336. while (maps[j].name) {
  337. ebpf_local_maps_t *w = &maps[j];
  338. if (w->map_fd == ND_EBPF_MAP_FD_NOT_INITIALIZED && !strcmp(map_name, w->name))
  339. w->map_fd = fd;
  340. j++;
  341. }
  342. }
  343. map_fd[i] = fd;
  344. i++;
  345. }
  346. }
  347. static void ebpf_update_controller(ebpf_module_t *em, struct bpf_object *obj)
  348. {
  349. ebpf_local_maps_t *maps = em->maps;
  350. if (!maps)
  351. return;
  352. struct bpf_map *map;
  353. bpf_map__for_each(map, obj)
  354. {
  355. size_t i = 0;
  356. while (maps[i].name) {
  357. ebpf_local_maps_t *w = &maps[i];
  358. if (w->map_fd != ND_EBPF_MAP_FD_NOT_INITIALIZED && (w->type & NETDATA_EBPF_MAP_CONTROLLER)) {
  359. w->type &= ~NETDATA_EBPF_MAP_CONTROLLER;
  360. w->type |= NETDATA_EBPF_MAP_CONTROLLER_UPDATED;
  361. uint32_t key = NETDATA_CONTROLLER_APPS_ENABLED;
  362. int value = em->apps_charts;
  363. int ret = bpf_map_update_elem(w->map_fd, &key, &value, 0);
  364. if (ret)
  365. error("Add key(%u) for controller table failed.", key);
  366. }
  367. i++;
  368. }
  369. }
  370. }
  371. struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, char *kernel_string,
  372. struct bpf_object **obj, int *map_fd)
  373. {
  374. char lpath[4096];
  375. char lname[128];
  376. int test = select_file(lname, em->thread_name, (size_t)127, em->mode, kernel_string);
  377. if (test < 0 || test > 127)
  378. return NULL;
  379. snprintf(lpath, 4096, "%s/ebpf.d/%s", plugins_dir, lname);
  380. *obj = bpf_object__open_file(lpath, NULL);
  381. if (libbpf_get_error(obj)) {
  382. error("Cannot open BPF object %s", lpath);
  383. bpf_object__close(*obj);
  384. return NULL;
  385. }
  386. ebpf_update_map_sizes(*obj, em);
  387. if (bpf_object__load(*obj)) {
  388. error("ERROR: loading BPF object file failed %s\n", lpath);
  389. bpf_object__close(*obj);
  390. return NULL;
  391. }
  392. ebpf_update_maps(em, map_fd, *obj);
  393. ebpf_update_controller(em, *obj);
  394. size_t count_programs = ebpf_count_programs(*obj);
  395. return ebpf_attach_programs(*obj, count_programs, em->names);
  396. }
  397. static char *ebpf_update_name(char *search)
  398. {
  399. char filename[FILENAME_MAX + 1];
  400. char *ret = NULL;
  401. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, NETDATA_KALLSYMS);
  402. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  403. if(unlikely(!ff)) {
  404. error("Cannot open %s%s", netdata_configured_host_prefix, NETDATA_KALLSYMS);
  405. return ret;
  406. }
  407. ff = procfile_readall(ff);
  408. if(unlikely(!ff))
  409. return ret;
  410. unsigned long i, lines = procfile_lines(ff);
  411. size_t length = strlen(search);
  412. for(i = 0; i < lines ; i++) {
  413. char *cmp = procfile_lineword(ff, i,2);;
  414. if (!strncmp(search, cmp, length)) {
  415. ret = strdupz(cmp);
  416. break;
  417. }
  418. }
  419. procfile_close(ff);
  420. return ret;
  421. }
  422. void ebpf_update_names(ebpf_specify_name_t *opt, ebpf_module_t *em)
  423. {
  424. int mode = em->mode;
  425. em->names = opt;
  426. size_t i = 0;
  427. while (opt[i].program_name) {
  428. opt[i].retprobe = (mode == MODE_RETURN);
  429. opt[i].optional = ebpf_update_name(opt[i].function_to_attach);
  430. i++;
  431. }
  432. }
  433. //----------------------------------------------------------------------------------------------------------------------
  434. void ebpf_mount_config_name(char *filename, size_t length, char *path, const char *config)
  435. {
  436. snprintf(filename, length, "%s/ebpf.d/%s", path, config);
  437. }
  438. int ebpf_load_config(struct config *config, char *filename)
  439. {
  440. return appconfig_load(config, filename, 0, NULL);
  441. }
  442. static netdata_run_mode_t ebpf_select_mode(char *mode)
  443. {
  444. if (!strcasecmp(mode, "return"))
  445. return MODE_RETURN;
  446. else if (!strcasecmp(mode, "dev"))
  447. return MODE_DEVMODE;
  448. return MODE_ENTRY;
  449. }
  450. void ebpf_update_module_using_config(ebpf_module_t *modules)
  451. {
  452. char *mode = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE, EBPF_CFG_LOAD_MODE_DEFAULT);
  453. modules->mode = ebpf_select_mode(mode);
  454. modules->update_time = (int)appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION,
  455. EBPF_CFG_UPDATE_EVERY, modules->update_time);
  456. modules->apps_charts = appconfig_get_boolean(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
  457. modules->apps_charts);
  458. modules->pid_map_size = (uint32_t)appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_PID_SIZE,
  459. modules->pid_map_size);
  460. }
  461. /**
  462. * Update module
  463. *
  464. * When this function is called, it will load the configuration file and after this
  465. * it updates the global information of ebpf_module.
  466. * If the module has specific configuration, this function will load it, but it will not
  467. * update the variables.
  468. *
  469. * @param em the module structure
  470. */
  471. void ebpf_update_module(ebpf_module_t *em)
  472. {
  473. char filename[FILENAME_MAX+1];
  474. ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_user_config_dir, em->config_file);
  475. if (!ebpf_load_config(em->cfg, filename)) {
  476. ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_stock_config_dir, em->config_file);
  477. if (!ebpf_load_config(em->cfg, filename)) {
  478. error("Cannot load the ebpf configuration file %s", em->config_file);
  479. return;
  480. }
  481. }
  482. ebpf_update_module_using_config(em);
  483. }
  484. //----------------------------------------------------------------------------------------------------------------------
  485. /**
  486. * Load Address
  487. *
  488. * Helper used to get address from /proc/kallsym
  489. *
  490. * @param fa address structure
  491. * @param fd file descriptor loaded inside kernel.
  492. */
  493. void ebpf_load_addresses(ebpf_addresses_t *fa, int fd)
  494. {
  495. if (fa->addr)
  496. return ;
  497. procfile *ff = procfile_open("/proc/kallsyms", " \t:", PROCFILE_FLAG_DEFAULT);
  498. if (!ff)
  499. return;
  500. ff = procfile_readall(ff);
  501. if (!ff)
  502. return;
  503. fa->hash = simple_hash(fa->function);
  504. size_t lines = procfile_lines(ff), l;
  505. for(l = 0; l < lines ;l++) {
  506. char *fcnt = procfile_lineword(ff, l, 2);
  507. uint32_t hash = simple_hash(fcnt);
  508. if (fa->hash == hash && !strcmp(fcnt, fa->function)) {
  509. char addr[128];
  510. snprintf(addr, 127, "0x%s", procfile_lineword(ff, l, 0));
  511. fa->addr = (unsigned long) strtoul(addr, NULL, 16);
  512. uint32_t key = 0;
  513. bpf_map_update_elem(fd, &key, &fa->addr, BPF_ANY);
  514. }
  515. }
  516. procfile_close(ff);
  517. }
  518. //----------------------------------------------------------------------------------------------------------------------
  519. /**
  520. * Fill Algorithms
  521. *
  522. * Set one unique dimension for all vector position.
  523. *
  524. * @param algorithms the output vector
  525. * @param length number of elements of algorithms vector
  526. * @param algortihm algorithm used on charts.
  527. */
  528. void ebpf_fill_algorithms(int *algorithms, size_t length, int algorithm)
  529. {
  530. size_t i;
  531. for (i = 0; i < length; i++) {
  532. algorithms[i] = algorithm;
  533. }
  534. }
  535. /**
  536. * Fill Histogram dimension
  537. *
  538. * Fill the histogram dimension with the specified ranges
  539. */
  540. char **ebpf_fill_histogram_dimension(size_t maximum)
  541. {
  542. char *dimensions[] = { "us", "ms", "s"};
  543. int previous_dim = 0, current_dim = 0;
  544. uint32_t previous_level = 1000, current_level = 1000;
  545. uint32_t previous_divisor = 1, current_divisor = 1;
  546. uint32_t current = 1, previous = 0;
  547. uint32_t selector;
  548. char **out = callocz(maximum, sizeof(char *));
  549. char range[128];
  550. size_t end = maximum - 1;
  551. for (selector = 0; selector < end; selector++) {
  552. snprintf(range, 127, "%u%s->%u%s", previous/previous_divisor, dimensions[previous_dim],
  553. current/current_divisor, dimensions[current_dim]);
  554. out[selector] = strdupz(range);
  555. previous = current;
  556. current <<= 1;
  557. if (previous_dim != 2 && previous > previous_level) {
  558. previous_dim++;
  559. previous_divisor *= 1000;
  560. previous_level *= 1000;
  561. }
  562. if (current_dim != 2 && current > current_level) {
  563. current_dim++;
  564. current_divisor *= 1000;
  565. current_level *= 1000;
  566. }
  567. }
  568. snprintf(range, 127, "%u%s->+Inf", previous/previous_divisor, dimensions[previous_dim]);
  569. out[selector] = strdupz(range);
  570. return out;
  571. }
  572. /**
  573. * Histogram dimension cleanup
  574. *
  575. * Cleanup dimensions allocated with function ebpf_fill_histogram_dimension
  576. *
  577. * @param ptr
  578. * @param length
  579. */
  580. void ebpf_histogram_dimension_cleanup(char **ptr, size_t length)
  581. {
  582. size_t i;
  583. for (i = 0; i < length; i++) {
  584. freez(ptr[i]);
  585. }
  586. freez(ptr);
  587. }