summaryrefslogtreecommitdiffstats
path: root/collectors/ebpf.plugin/ebpf_shm.c
diff options
context:
space:
mode:
Diffstat (limited to 'collectors/ebpf.plugin/ebpf_shm.c')
-rw-r--r--collectors/ebpf.plugin/ebpf_shm.c149
1 files changed, 97 insertions, 52 deletions
diff --git a/collectors/ebpf.plugin/ebpf_shm.c b/collectors/ebpf.plugin/ebpf_shm.c
index bd928cbdc..f81287d82 100644
--- a/collectors/ebpf.plugin/ebpf_shm.c
+++ b/collectors/ebpf.plugin/ebpf_shm.c
@@ -34,9 +34,16 @@ static ebpf_local_maps_t shm_maps[] = {{.name = "tbl_pid_shm", .internal_input =
.map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
{.name = NULL, .internal_input = 0, .user_input = 0}};
-struct netdata_static_thread shm_threads = {"SHM KERNEL", NULL, NULL, 1,
- NULL, NULL, NULL};
-static enum ebpf_threads_status ebpf_shm_exited = NETDATA_THREAD_EBPF_RUNNING;
+struct netdata_static_thread shm_threads = {
+ .name = "SHM KERNEL",
+ .config_section = NULL,
+ .config_name = NULL,
+ .env_name = NULL,
+ .enabled = 1,
+ .thread = NULL,
+ .init_routine = NULL,
+ .start_routine = NULL
+};
netdata_ebpf_targets_t shm_targets[] = { {.name = "shmget", .mode = EBPF_LOAD_TRAMPOLINE},
{.name = "shmat", .mode = EBPF_LOAD_TRAMPOLINE},
@@ -83,6 +90,7 @@ static void ebpf_disable_probe(struct shm_bpf *obj)
bpf_program__set_autoload(obj->progs.netdata_shmat_probe, false);
bpf_program__set_autoload(obj->progs.netdata_shmdt_probe, false);
bpf_program__set_autoload(obj->progs.netdata_shmctl_probe, false);
+ bpf_program__set_autoload(obj->progs.netdata_shm_release_task_probe, false);
}
/*
@@ -98,6 +106,7 @@ static void ebpf_disable_trampoline(struct shm_bpf *obj)
bpf_program__set_autoload(obj->progs.netdata_shmat_fentry, false);
bpf_program__set_autoload(obj->progs.netdata_shmdt_fentry, false);
bpf_program__set_autoload(obj->progs.netdata_shmctl_fentry, false);
+ bpf_program__set_autoload(obj->progs.netdata_shm_release_task_fentry, false);
}
/**
@@ -130,6 +139,9 @@ static void ebpf_set_trampoline_target(struct shm_bpf *obj)
shm_targets[NETDATA_KEY_SHMCTL_CALL].name, running_on_kernel);
bpf_program__set_attach_target(obj->progs.netdata_shmctl_fentry, 0,
syscall);
+
+ bpf_program__set_attach_target(obj->progs.netdata_shm_release_task_fentry, 0,
+ EBPF_COMMON_FNCT_CLEAN_UP);
}
/**
@@ -177,6 +189,13 @@ static int ebpf_shm_attach_probe(struct shm_bpf *obj)
if (ret)
return -1;
+ obj->links.netdata_shm_release_task_probe = bpf_program__attach_kprobe(obj->progs.netdata_shm_release_task_probe,
+ false, EBPF_COMMON_FNCT_CLEAN_UP);
+ ret = (int)libbpf_get_error(obj->links.netdata_shm_release_task_probe);
+ if (ret)
+ return -1;
+
+
return 0;
}
@@ -193,6 +212,33 @@ static void ebpf_shm_set_hash_tables(struct shm_bpf *obj)
}
/**
+ * Disable Release Task
+ *
+ * Disable release task when apps is not enabled.
+ *
+ * @param obj is the main structure for bpf objects.
+ */
+static void ebpf_shm_disable_release_task(struct shm_bpf *obj)
+{
+ bpf_program__set_autoload(obj->progs.netdata_shm_release_task_probe, false);
+ bpf_program__set_autoload(obj->progs.netdata_shm_release_task_fentry, false);
+}
+
+/**
+ * Adjust Map Size
+ *
+ * Resize maps according input from users.
+ *
+ * @param obj is the main structure for bpf objects.
+ * @param em structure with configuration
+ */
+static void ebpf_shm_adjust_map_size(struct shm_bpf *obj, ebpf_module_t *em)
+{
+ ebpf_update_map_size(obj->maps.tbl_pid_shm, &shm_maps[NETDATA_PID_SHM_TABLE],
+ em, bpf_map__name(obj->maps.tbl_pid_shm));
+}
+
+/**
* Load and attach
*
* Load and attach the eBPF code in kernel.
@@ -221,6 +267,10 @@ static inline int ebpf_shm_load_and_attach(struct shm_bpf *obj, ebpf_module_t *e
ebpf_disable_trampoline(obj);
}
+ ebpf_shm_adjust_map_size(obj, em);
+ if (!em->apps_charts && !em->cgroup_charts)
+ ebpf_shm_disable_release_task(obj);
+
int ret = shm_bpf__load(obj);
if (!ret) {
if (test != EBPF_LOAD_PROBE && test != EBPF_LOAD_RETPROBE)
@@ -240,6 +290,36 @@ static inline int ebpf_shm_load_and_attach(struct shm_bpf *obj, ebpf_module_t *e
*****************************************************************/
/**
+ * SHM Free
+ *
+ * Cleanup variables after child threads to stop
+ *
+ * @param ptr thread data.
+ */
+static void ebpf_shm_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;
+ }
+ pthread_mutex_unlock(&ebpf_exit_cleanup);
+
+ ebpf_cleanup_publish_syscall(shm_publish_aggregated);
+
+ freez(shm_vector);
+ freez(shm_values);
+
+#ifdef LIBBPF_MAJOR_VERSION
+ if (bpf_obj)
+ shm_bpf__destroy(bpf_obj);
+#endif
+
+ em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
+}
+
+/**
* SHM Exit
*
* Cancel child thread.
@@ -249,12 +329,8 @@ static inline int ebpf_shm_load_and_attach(struct shm_bpf *obj, ebpf_module_t *e
static void ebpf_shm_exit(void *ptr)
{
ebpf_module_t *em = (ebpf_module_t *)ptr;
- if (!em->enabled) {
- em->enabled = NETDATA_MAIN_THREAD_EXITED;
- return;
- }
-
- ebpf_shm_exited = NETDATA_THREAD_EBPF_STOPPING;
+ netdata_thread_cancel(*shm_threads.thread);
+ ebpf_shm_free(em);
}
/**
@@ -267,21 +343,7 @@ static void ebpf_shm_exit(void *ptr)
static void ebpf_shm_cleanup(void *ptr)
{
ebpf_module_t *em = (ebpf_module_t *)ptr;
- if (ebpf_shm_exited != NETDATA_THREAD_EBPF_STOPPED)
- return;
-
- ebpf_cleanup_publish_syscall(shm_publish_aggregated);
-
- freez(shm_vector);
- freez(shm_values);
-
-#ifdef LIBBPF_MAJOR_VERSION
- if (bpf_obj)
- shm_bpf__destroy(bpf_obj);
-#endif
-
- shm_threads.enabled = NETDATA_MAIN_THREAD_EXITED;
- em->enabled = NETDATA_MAIN_THREAD_EXITED;
+ ebpf_shm_free(em);
}
/*****************************************************************
@@ -463,17 +525,12 @@ void *ebpf_shm_read_hash(void *ptr)
ebpf_module_t *em = (ebpf_module_t *)ptr;
usec_t step = NETDATA_SHM_SLEEP_MS * em->update_every;
- while (ebpf_shm_exited == NETDATA_THREAD_EBPF_RUNNING) {
- usec_t dt = heartbeat_next(&hb, step);
- (void)dt;
- if (ebpf_shm_exited == NETDATA_THREAD_EBPF_STOPPING)
- break;
+ while (!ebpf_exit_plugin) {
+ (void)heartbeat_next(&hb, step);
read_global_table();
}
- ebpf_shm_exited = NETDATA_THREAD_EBPF_STOPPED;
-
netdata_thread_cleanup_pop(1);
return NULL;
}
@@ -721,20 +778,15 @@ static void ebpf_create_systemd_shm_charts(int update_every)
* Send Systemd charts
*
* Send collected data to Netdata.
- *
- * @return It returns the status for chart creation, if it is necessary to remove a specific dimension, zero is returned
- * otherwise function returns 1 to avoid chart recreation
*/
-static int ebpf_send_systemd_shm_charts()
+static void ebpf_send_systemd_shm_charts()
{
- int ret = 1;
ebpf_cgroup_target_t *ect;
write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_SHMGET_CHART);
for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
if (unlikely(ect->systemd) && unlikely(ect->updated)) {
write_chart_dimension(ect->name, (long long)ect->publish_shm.get);
- } else if (unlikely(ect->systemd))
- ret = 0;
+ }
}
write_end_chart();
@@ -761,8 +813,6 @@ static int ebpf_send_systemd_shm_charts()
}
}
write_end_chart();
-
- return ret;
}
/*
@@ -810,13 +860,11 @@ void ebpf_shm_send_cgroup_data(int update_every)
int has_systemd = shm_ebpf_cgroup.header->systemd_enabled;
if (has_systemd) {
- static int systemd_charts = 0;
- if (!systemd_charts) {
+ if (send_cgroup_chart) {
ebpf_create_systemd_shm_charts(update_every);
- systemd_charts = 1;
}
- systemd_charts = ebpf_send_systemd_shm_charts();
+ ebpf_send_systemd_shm_charts();
}
for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
@@ -1008,7 +1056,9 @@ static void ebpf_create_shm_charts(int update_every)
static int ebpf_shm_load_bpf(ebpf_module_t *em)
{
int ret = 0;
- if (em->load == EBPF_LOAD_LEGACY) {
+
+ ebpf_adjust_apps_cgroup(em, em->targets[NETDATA_KEY_SHMGET_CALL].mode);
+ if (em->load & EBPF_LOAD_LEGACY) {
em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
if (!em->probe_links) {
em->enabled = CONFIG_BOOLEAN_NO;
@@ -1047,15 +1097,11 @@ void *ebpf_shm_thread(void *ptr)
ebpf_update_pid_table(&shm_maps[NETDATA_PID_SHM_TABLE], em);
- if (!em->enabled) {
- goto endshm;
- }
-
#ifdef LIBBPF_MAJOR_VERSION
ebpf_adjust_thread_load(em, default_btf);
#endif
if (ebpf_shm_load_bpf(em)) {
- em->enabled = CONFIG_BOOLEAN_NO;
+ em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
goto endshm;
}
@@ -1084,8 +1130,7 @@ void *ebpf_shm_thread(void *ptr)
shm_collector(em);
endshm:
- if (!em->enabled)
- ebpf_update_disabled_plugin_stats(em);
+ ebpf_update_disabled_plugin_stats(em);
netdata_thread_cleanup_pop(1);
return NULL;