diff options
Diffstat (limited to 'collectors/ebpf.plugin/ebpf_fd.c')
-rw-r--r-- | collectors/ebpf.plugin/ebpf_fd.c | 145 |
1 files changed, 61 insertions, 84 deletions
diff --git a/collectors/ebpf.plugin/ebpf_fd.c b/collectors/ebpf.plugin/ebpf_fd.c index 30b7f22ce..79537066c 100644 --- a/collectors/ebpf.plugin/ebpf_fd.c +++ b/collectors/ebpf.plugin/ebpf_fd.c @@ -6,6 +6,9 @@ static char *fd_dimension_names[NETDATA_FD_SYSCALL_END] = { "open", "close" }; static char *fd_id_names[NETDATA_FD_SYSCALL_END] = { "do_sys_open", "__close_fd" }; +static char *close_targets[NETDATA_EBPF_MAX_FD_TARGETS] = {"close_fd", "__close_fd"}; +static char *open_targets[NETDATA_EBPF_MAX_FD_TARGETS] = {"do_sys_openat2", "do_sys_open"}; + static netdata_syscall_stat_t fd_aggregated_data[NETDATA_FD_SYSCALL_END]; static netdata_publish_syscall_t fd_publish_aggregated[NETDATA_FD_SYSCALL_END]; @@ -29,15 +32,6 @@ struct config fd_config = { .first_section = NULL, .last_section = NULL, .mutex .index = {.avl_tree = { .root = NULL, .compar = appconfig_section_compare }, .rwlock = AVL_LOCK_INITIALIZER } }; -struct netdata_static_thread fd_thread = {"FD KERNEL", - .config_section = NULL, - .config_name = NULL, - .env_name = NULL, - .enabled = 1, - .thread = NULL, - .init_routine = NULL, - .start_routine = NULL}; - static netdata_idx_t fd_hash_values[NETDATA_FD_COUNTER]; static netdata_idx_t *fd_values = NULL; @@ -65,7 +59,7 @@ static inline void ebpf_fd_disable_probes(struct fd_bpf *obj) bpf_program__set_autoload(obj->progs.netdata_sys_open_kprobe, false); bpf_program__set_autoload(obj->progs.netdata_sys_open_kretprobe, false); bpf_program__set_autoload(obj->progs.netdata_release_task_fd_kprobe, false); - if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_11) { + if (!strcmp(fd_targets[NETDATA_FD_SYSCALL_CLOSE].name, close_targets[NETDATA_FD_CLOSE_FD])) { bpf_program__set_autoload(obj->progs.netdata___close_fd_kretprobe, false); bpf_program__set_autoload(obj->progs.netdata___close_fd_kprobe, false); bpf_program__set_autoload(obj->progs.netdata_close_fd_kprobe, false); @@ -85,7 +79,7 @@ static inline void ebpf_fd_disable_probes(struct fd_bpf *obj) */ static inline void ebpf_disable_specific_probes(struct fd_bpf *obj) { - if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_11) { + if (!strcmp(fd_targets[NETDATA_FD_SYSCALL_CLOSE].name, close_targets[NETDATA_FD_CLOSE_FD])) { bpf_program__set_autoload(obj->progs.netdata___close_fd_kretprobe, false); bpf_program__set_autoload(obj->progs.netdata___close_fd_kprobe, false); } else { @@ -121,7 +115,7 @@ static inline void ebpf_disable_trampoline(struct fd_bpf *obj) */ static inline void ebpf_disable_specific_trampoline(struct fd_bpf *obj) { - if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_11) { + if (!strcmp(fd_targets[NETDATA_FD_SYSCALL_CLOSE].name, close_targets[NETDATA_FD_CLOSE_FD])) { bpf_program__set_autoload(obj->progs.netdata___close_fd_fentry, false); bpf_program__set_autoload(obj->progs.netdata___close_fd_fexit, false); } else { @@ -143,7 +137,7 @@ static void ebpf_set_trampoline_target(struct fd_bpf *obj) bpf_program__set_attach_target(obj->progs.netdata_sys_open_fexit, 0, fd_targets[NETDATA_FD_SYSCALL_OPEN].name); bpf_program__set_attach_target(obj->progs.netdata_release_task_fd_fentry, 0, EBPF_COMMON_FNCT_CLEAN_UP); - if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_11) { + if (!strcmp(fd_targets[NETDATA_FD_SYSCALL_CLOSE].name, close_targets[NETDATA_FD_CLOSE_FD])) { bpf_program__set_attach_target( obj->progs.netdata_close_fd_fentry, 0, fd_targets[NETDATA_FD_SYSCALL_CLOSE].name); bpf_program__set_attach_target(obj->progs.netdata_close_fd_fexit, 0, fd_targets[NETDATA_FD_SYSCALL_CLOSE].name); @@ -185,7 +179,7 @@ static int ebpf_fd_attach_probe(struct fd_bpf *obj) if (ret) return -1; - if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_11) { + if (!strcmp(fd_targets[NETDATA_FD_SYSCALL_CLOSE].name, close_targets[NETDATA_FD_CLOSE_FD])) { obj->links.netdata_close_fd_kretprobe = bpf_program__attach_kprobe(obj->progs.netdata_close_fd_kretprobe, true, fd_targets[NETDATA_FD_SYSCALL_CLOSE].name); ret = libbpf_get_error(obj->links.netdata_close_fd_kretprobe); @@ -217,24 +211,50 @@ static int ebpf_fd_attach_probe(struct fd_bpf *obj) } /** - * Set target values + * FD Fill Address * - * Set pointers used to laod data. + * Fill address value used to load probes/trampoline. */ -static void ebpf_fd_set_target_values() +static inline void ebpf_fd_fill_address(ebpf_addresses_t *address, char **targets) { - static char *close_targets[] = {"close_fd", "__close_fd"}; - static char *open_targets[] = {"do_sys_openat2", "do_sys_open"}; - if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_11) { - fd_targets[NETDATA_FD_SYSCALL_OPEN].name = open_targets[0]; - fd_targets[NETDATA_FD_SYSCALL_CLOSE].name = close_targets[0]; - } else { - fd_targets[NETDATA_FD_SYSCALL_OPEN].name = open_targets[1]; - fd_targets[NETDATA_FD_SYSCALL_CLOSE].name = close_targets[1]; + int i; + for (i = 0; i < NETDATA_EBPF_MAX_FD_TARGETS; i++) { + address->function = targets[i]; + ebpf_load_addresses(address, -1); + if (address->addr) + break; } } /** + * Set target values + * + * Set pointers used to load data. + * + * @return It returns 0 on success and -1 otherwise. + */ +static int ebpf_fd_set_target_values() +{ + ebpf_addresses_t address = {.function = NULL, .hash = 0, .addr = 0}; + ebpf_fd_fill_address(&address, close_targets); + + if (!address.addr) + return -1; + + fd_targets[NETDATA_FD_SYSCALL_CLOSE].name = address.function; + + address.addr = 0; + ebpf_fd_fill_address(&address, open_targets); + + if (!address.addr) + return -1; + + fd_targets[NETDATA_FD_SYSCALL_OPEN].name = address.function; + + return 0; +} + +/** * Set hash tables * * Set the values for maps according the value given by kernel. @@ -283,14 +303,18 @@ static void ebpf_fd_disable_release_task(struct fd_bpf *obj) * @param obj is the main structure for bpf objects. * @param em structure with configuration * - * @return it returns 0 on succes and -1 otherwise + * @return it returns 0 on success and -1 otherwise */ static inline int ebpf_fd_load_and_attach(struct fd_bpf *obj, ebpf_module_t *em) { netdata_ebpf_targets_t *mt = em->targets; netdata_ebpf_program_loaded_t test = mt[NETDATA_FD_SYSCALL_OPEN].mode; - ebpf_fd_set_target_values(); + if (ebpf_fd_set_target_values()) { + error("%s file descriptor.", NETDATA_EBPF_DEFAULT_FNT_NOT_FOUND); + return -1; + } + if (test == EBPF_LOAD_TRAMPOLINE) { ebpf_fd_disable_probes(obj); ebpf_disable_specific_trampoline(obj); @@ -340,15 +364,10 @@ static inline int ebpf_fd_load_and_attach(struct fd_bpf *obj, ebpf_module_t *em) static void ebpf_fd_free(ebpf_module_t *em) { pthread_mutex_lock(&ebpf_exit_cleanup); - if (em->thread->enabled == NETDATA_THREAD_EBPF_RUNNING) { - em->thread->enabled = NETDATA_THREAD_EBPF_STOPPING; - pthread_mutex_unlock(&ebpf_exit_cleanup); - return; - } + em->thread->enabled = NETDATA_THREAD_EBPF_STOPPING; pthread_mutex_unlock(&ebpf_exit_cleanup); ebpf_cleanup_publish_syscall(fd_publish_aggregated); - freez(fd_thread.thread); freez(fd_values); freez(fd_vector); @@ -372,18 +391,6 @@ static void ebpf_fd_free(ebpf_module_t *em) static void ebpf_fd_exit(void *ptr) { ebpf_module_t *em = (ebpf_module_t *)ptr; - netdata_thread_cancel(*fd_thread.thread); - ebpf_fd_free(em); -} - -/** - * Clean up the main thread. - * - * @param ptr thread data. - */ -static void ebpf_fd_cleanup(void *ptr) -{ - ebpf_module_t *em = (ebpf_module_t *)ptr; ebpf_fd_free(em); } @@ -420,7 +427,7 @@ static void ebpf_fd_send_data(ebpf_module_t *em) * * Read the table with number of calls for all functions */ -static void read_global_table() +static void ebpf_fd_read_global_table() { uint32_t idx; netdata_idx_t *val = fd_hash_values; @@ -441,34 +448,6 @@ static void read_global_table() } /** - * File descriptor read hash - * - * This is the thread callback. - * This thread is necessary, because we cannot freeze the whole plugin to read the data. - * - * @param ptr It is a NULL value for this thread. - * - * @return It always returns NULL. - */ -void *ebpf_fd_read_hash(void *ptr) -{ - netdata_thread_cleanup_push(ebpf_fd_cleanup, ptr); - heartbeat_t hb; - heartbeat_init(&hb); - - ebpf_module_t *em = (ebpf_module_t *)ptr; - usec_t step = NETDATA_FD_SLEEP_MS * em->update_every; - while (!ebpf_exit_plugin) { - (void)heartbeat_next(&hb, step); - - read_global_table(); - } - - netdata_thread_cleanup_pop(1); - return NULL; -} - -/** * Apps Accumulator * * Sum all values read from kernel and store in the first address. @@ -942,22 +921,20 @@ static void ebpf_fd_send_cgroup_data(ebpf_module_t *em) */ static void fd_collector(ebpf_module_t *em) { - fd_thread.thread = mallocz(sizeof(netdata_thread_t)); - fd_thread.start_routine = ebpf_fd_read_hash; - - netdata_thread_create(fd_thread.thread, fd_thread.name, NETDATA_THREAD_OPTION_DEFAULT, - ebpf_fd_read_hash, em); - int cgroups = em->cgroup_charts; heartbeat_t hb; heartbeat_init(&hb); - usec_t step = em->update_every * USEC_PER_SEC; + int update_every = em->update_every; + int counter = update_every - 1; while (!ebpf_exit_plugin) { - (void)heartbeat_next(&hb, step); - if (ebpf_exit_plugin) - break; + (void)heartbeat_next(&hb, USEC_PER_SEC); + + if (ebpf_exit_plugin || ++counter != update_every) + continue; + counter = 0; netdata_apps_integration_flags_t apps = em->apps_charts; + ebpf_fd_read_global_table(); pthread_mutex_lock(&collect_data_mutex); if (apps) read_apps_table(); |