summaryrefslogtreecommitdiffstats
path: root/collectors/ebpf.plugin/ebpf_swap.c
diff options
context:
space:
mode:
Diffstat (limited to 'collectors/ebpf.plugin/ebpf_swap.c')
-rw-r--r--collectors/ebpf.plugin/ebpf_swap.c138
1 files changed, 84 insertions, 54 deletions
diff --git a/collectors/ebpf.plugin/ebpf_swap.c b/collectors/ebpf.plugin/ebpf_swap.c
index 71d0c402b..8199573a9 100644
--- a/collectors/ebpf.plugin/ebpf_swap.c
+++ b/collectors/ebpf.plugin/ebpf_swap.c
@@ -34,9 +34,16 @@ static ebpf_local_maps_t swap_maps[] = {{.name = "tbl_pid_swap", .internal_input
.map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
{.name = NULL, .internal_input = 0, .user_input = 0}};
-struct netdata_static_thread swap_threads = {"SWAP KERNEL", NULL, NULL, 1,
- NULL, NULL, NULL};
-static enum ebpf_threads_status ebpf_swap_exited = NETDATA_THREAD_EBPF_RUNNING;
+struct netdata_static_thread swap_threads = {
+ .name = "SWAP KERNEL",
+ .config_section = NULL,
+ .config_name = NULL,
+ .env_name = NULL,
+ .enabled = 1,
+ .thread = NULL,
+ .init_routine = NULL,
+ .start_routine = NULL
+};
netdata_ebpf_targets_t swap_targets[] = { {.name = "swap_readpage", .mode = EBPF_LOAD_TRAMPOLINE},
{.name = "swap_writepage", .mode = EBPF_LOAD_TRAMPOLINE},
@@ -71,6 +78,7 @@ static void ebpf_swap_disable_trampoline(struct swap_bpf *obj)
{
bpf_program__set_autoload(obj->progs.netdata_swap_readpage_fentry, false);
bpf_program__set_autoload(obj->progs.netdata_swap_writepage_fentry, false);
+ bpf_program__set_autoload(obj->progs.netdata_release_task_fentry, false);
}
/**
@@ -87,6 +95,9 @@ static void ebpf_swap_set_trampoline_target(struct swap_bpf *obj)
bpf_program__set_attach_target(obj->progs.netdata_swap_writepage_fentry, 0,
swap_targets[NETDATA_KEY_SWAP_WRITEPAGE_CALL].name);
+
+ bpf_program__set_attach_target(obj->progs.netdata_release_task_fentry, 0,
+ EBPF_COMMON_FNCT_CLEAN_UP);
}
/**
@@ -114,6 +125,13 @@ static int ebpf_swap_attach_kprobe(struct swap_bpf *obj)
if (ret)
return -1;
+ obj->links.netdata_release_task_probe = bpf_program__attach_kprobe(obj->progs.netdata_release_task_probe,
+ false,
+ EBPF_COMMON_FNCT_CLEAN_UP);
+ ret = libbpf_get_error(obj->links.netdata_swap_writepage_probe);
+ if (ret)
+ return -1;
+
return 0;
}
@@ -146,6 +164,19 @@ static void ebpf_swap_adjust_map_size(struct swap_bpf *obj, ebpf_module_t *em)
}
/**
+ * Disable Release Task
+ *
+ * Disable release task when apps is not enabled.
+ *
+ * @param obj is the main structure for bpf objects.
+ */
+static void ebpf_swap_disable_release_task(struct swap_bpf *obj)
+{
+ bpf_program__set_autoload(obj->progs.netdata_release_task_fentry, false);
+ bpf_program__set_autoload(obj->progs.netdata_release_task_probe, false);
+}
+
+/**
* Load and attach
*
* Load and attach the eBPF code in kernel.
@@ -168,13 +199,16 @@ static inline int ebpf_swap_load_and_attach(struct swap_bpf *obj, ebpf_module_t
ebpf_swap_disable_trampoline(obj);
}
+ ebpf_swap_adjust_map_size(obj, em);
+
+ if (!em->apps_charts && !em->cgroup_charts)
+ ebpf_swap_disable_release_task(obj);
+
int ret = swap_bpf__load(obj);
if (ret) {
return ret;
}
- ebpf_swap_adjust_map_size(obj, em);
-
ret = (test == EBPF_LOAD_TRAMPOLINE) ? swap_bpf__attach(obj) : ebpf_swap_attach_kprobe(obj);
if (!ret) {
ebpf_swap_set_hash_tables(obj);
@@ -193,6 +227,38 @@ static inline int ebpf_swap_load_and_attach(struct swap_bpf *obj, ebpf_module_t
*****************************************************************/
/**
+ * Cachestat Free
+ *
+ * Cleanup variables after child threads to stop
+ *
+ * @param ptr thread data.
+ */
+static void ebpf_swap_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(swap_publish_aggregated);
+
+ freez(swap_vector);
+ freez(swap_values);
+ freez(swap_threads.thread);
+
+#ifdef LIBBPF_MAJOR_VERSION
+ if (bpf_obj)
+ swap_bpf__destroy(bpf_obj);
+#endif
+ pthread_mutex_lock(&ebpf_exit_cleanup);
+ em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
+ pthread_mutex_unlock(&ebpf_exit_cleanup);
+}
+
+/**
* Swap exit
*
* Cancel thread and exit.
@@ -202,12 +268,8 @@ static inline int ebpf_swap_load_and_attach(struct swap_bpf *obj, ebpf_module_t
static void ebpf_swap_exit(void *ptr)
{
ebpf_module_t *em = (ebpf_module_t *)ptr;
- if (!em->enabled) {
- em->enabled = NETDATA_MAIN_THREAD_EXITED;
- return;
- }
-
- ebpf_swap_exited = NETDATA_THREAD_EBPF_STOPPING;
+ netdata_thread_cancel(*swap_threads.thread);
+ ebpf_swap_free(em);
}
/**
@@ -220,21 +282,7 @@ static void ebpf_swap_exit(void *ptr)
static void ebpf_swap_cleanup(void *ptr)
{
ebpf_module_t *em = (ebpf_module_t *)ptr;
- if (ebpf_swap_exited != NETDATA_THREAD_EBPF_STOPPED)
- return;
-
- ebpf_cleanup_publish_syscall(swap_publish_aggregated);
-
- freez(swap_vector);
- freez(swap_values);
- freez(swap_threads.thread);
-
-#ifdef LIBBPF_MAJOR_VERSION
- if (bpf_obj)
- swap_bpf__destroy(bpf_obj);
-#endif
- swap_threads.enabled = NETDATA_MAIN_THREAD_EXITED;
- em->enabled = NETDATA_MAIN_THREAD_EXITED;
+ ebpf_swap_free(em);
}
/*****************************************************************
@@ -401,17 +449,12 @@ void *ebpf_swap_read_hash(void *ptr)
ebpf_module_t *em = (ebpf_module_t *)ptr;
usec_t step = NETDATA_SWAP_SLEEP_MS * em->update_every;
- while (ebpf_swap_exited == NETDATA_THREAD_EBPF_RUNNING) {
- usec_t dt = heartbeat_next(&hb, step);
- (void)dt;
- if (ebpf_swap_exited == NETDATA_THREAD_EBPF_STOPPING)
- break;
+ while (!ebpf_exit_plugin) {
+ (void)heartbeat_next(&hb, step);
read_global_table();
}
- ebpf_swap_exited = NETDATA_THREAD_EBPF_STOPPED;
-
netdata_thread_cleanup_pop(1);
return NULL;
}
@@ -505,20 +548,15 @@ static void ebpf_swap_sum_cgroup_pids(netdata_publish_swap_t *swap, struct pid_o
* 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_swap_charts()
+static void ebpf_send_systemd_swap_charts()
{
- int ret = 1;
ebpf_cgroup_target_t *ect;
write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_MEM_SWAP_READ_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_systemd_swap.read);
- } else if (unlikely(ect->systemd))
- ret = 0;
+ }
}
write_end_chart();
@@ -529,8 +567,6 @@ static int ebpf_send_systemd_swap_charts()
}
}
write_end_chart();
-
- return ret;
}
/**
@@ -644,14 +680,11 @@ void ebpf_swap_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_swap_charts(update_every);
- systemd_charts = 1;
fflush(stdout);
}
-
- systemd_charts = ebpf_send_systemd_swap_charts();
+ ebpf_send_systemd_swap_charts();
}
for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
@@ -812,7 +845,8 @@ static void ebpf_create_swap_charts(int update_every)
static int ebpf_swap_load_bpf(ebpf_module_t *em)
{
int ret = 0;
- if (em->load == EBPF_LOAD_LEGACY) {
+ ebpf_adjust_apps_cgroup(em, em->targets[NETDATA_KEY_SWAP_READPAGE_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) {
ret = -1;
@@ -852,14 +886,11 @@ void *ebpf_swap_thread(void *ptr)
ebpf_update_pid_table(&swap_maps[NETDATA_PID_SWAP_TABLE], em);
- if (!em->enabled)
- goto endswap;
-
#ifdef LIBBPF_MAJOR_VERSION
ebpf_adjust_thread_load(em, default_btf);
#endif
if (ebpf_swap_load_bpf(em)) {
- em->enabled = CONFIG_BOOLEAN_NO;
+ em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
goto endswap;
}
@@ -877,8 +908,7 @@ void *ebpf_swap_thread(void *ptr)
swap_collector(em);
endswap:
- if (!em->enabled)
- ebpf_update_disabled_plugin_stats(em);
+ ebpf_update_disabled_plugin_stats(em);
netdata_thread_cleanup_pop(1);
return NULL;