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.c114
1 files changed, 55 insertions, 59 deletions
diff --git a/collectors/ebpf.plugin/ebpf_swap.c b/collectors/ebpf.plugin/ebpf_swap.c
index 7d842335..71d0c402 100644
--- a/collectors/ebpf.plugin/ebpf_swap.c
+++ b/collectors/ebpf.plugin/ebpf_swap.c
@@ -7,7 +7,6 @@ static char *swap_dimension_name[NETDATA_SWAP_END] = { "read", "write" };
static netdata_syscall_stat_t swap_aggregated_data[NETDATA_SWAP_END];
static netdata_publish_syscall_t swap_publish_aggregated[NETDATA_SWAP_END];
-static int read_thread_closed = 1;
netdata_publish_swap_t *swap_vector = NULL;
static netdata_idx_t swap_hash_values[NETDATA_SWAP_END];
@@ -35,11 +34,9 @@ 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}};
-static struct bpf_link **probe_links = NULL;
-static struct bpf_object *objects = NULL;
-
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;
netdata_ebpf_targets_t swap_targets[] = { {.name = "swap_readpage", .mode = EBPF_LOAD_TRAMPOLINE},
{.name = "swap_writepage", .mode = EBPF_LOAD_TRAMPOLINE},
@@ -196,54 +193,48 @@ static inline int ebpf_swap_load_and_attach(struct swap_bpf *obj, ebpf_module_t
*****************************************************************/
/**
- * Clean swap structure
+ * Swap exit
+ *
+ * Cancel thread and exit.
+ *
+ * @param ptr thread data.
*/
-void clean_swap_pid_structures() {
- struct pid_stat *pids = root_of_pids;
- while (pids) {
- freez(swap_pid[pids->pid]);
-
- pids = pids->next;
+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;
}
/**
- * Clean up the main thread.
+ * Swap cleanup
+ *
+ * Clean up allocated memory.
*
* @param ptr thread data.
*/
static void ebpf_swap_cleanup(void *ptr)
{
ebpf_module_t *em = (ebpf_module_t *)ptr;
- if (!em->enabled)
+ if (ebpf_swap_exited != NETDATA_THREAD_EBPF_STOPPED)
return;
- heartbeat_t hb;
- heartbeat_init(&hb);
- uint32_t tick = 2 * USEC_PER_MS;
- while (!read_thread_closed) {
- usec_t dt = heartbeat_next(&hb, tick);
- UNUSED(dt);
- }
-
ebpf_cleanup_publish_syscall(swap_publish_aggregated);
freez(swap_vector);
freez(swap_values);
+ freez(swap_threads.thread);
- if (probe_links) {
- struct bpf_program *prog;
- size_t i = 0 ;
- bpf_object__for_each_program(prog, objects) {
- bpf_link__destroy(probe_links[i]);
- i++;
- }
- bpf_object__close(objects);
- }
#ifdef LIBBPF_MAJOR_VERSION
- else if (bpf_obj)
+ if (bpf_obj)
swap_bpf__destroy(bpf_obj);
#endif
+ swap_threads.enabled = NETDATA_MAIN_THREAD_EXITED;
+ em->enabled = NETDATA_MAIN_THREAD_EXITED;
}
/*****************************************************************
@@ -404,21 +395,24 @@ static void read_global_table()
*/
void *ebpf_swap_read_hash(void *ptr)
{
- read_thread_closed = 0;
-
+ netdata_thread_cleanup_push(ebpf_swap_cleanup, ptr);
heartbeat_t hb;
heartbeat_init(&hb);
ebpf_module_t *em = (ebpf_module_t *)ptr;
usec_t step = NETDATA_SWAP_SLEEP_MS * em->update_every;
- while (!close_ebpf_plugin) {
+ 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;
read_global_table();
}
- read_thread_closed = 1;
+ ebpf_swap_exited = NETDATA_THREAD_EBPF_STOPPED;
+
+ netdata_thread_cleanup_pop(1);
return NULL;
}
@@ -523,7 +517,7 @@ static int ebpf_send_systemd_swap_charts()
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
+ } else if (unlikely(ect->systemd))
ret = 0;
}
write_end_chart();
@@ -690,37 +684,38 @@ static void swap_collector(ebpf_module_t *em)
swap_threads.thread = mallocz(sizeof(netdata_thread_t));
swap_threads.start_routine = ebpf_swap_read_hash;
- netdata_thread_create(swap_threads.thread, swap_threads.name, NETDATA_THREAD_OPTION_JOINABLE,
+ netdata_thread_create(swap_threads.thread, swap_threads.name, NETDATA_THREAD_OPTION_DEFAULT,
ebpf_swap_read_hash, em);
- int apps = em->apps_charts;
int cgroup = em->cgroup_charts;
int update_every = em->update_every;
- int counter = update_every - 1;
- while (!close_ebpf_plugin) {
- pthread_mutex_lock(&collect_data_mutex);
- pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
+ heartbeat_t hb;
+ heartbeat_init(&hb);
+ usec_t step = update_every * USEC_PER_SEC;
+ while (!ebpf_exit_plugin) {
+ (void)heartbeat_next(&hb, step);
+ if (ebpf_exit_plugin)
+ break;
- if (++counter == update_every) {
- counter = 0;
- if (apps)
- read_apps_table();
+ netdata_apps_integration_flags_t apps = em->apps_charts;
+ pthread_mutex_lock(&collect_data_mutex);
+ if (apps)
+ read_apps_table();
- if (cgroup)
- ebpf_update_swap_cgroup();
+ if (cgroup)
+ ebpf_update_swap_cgroup();
- pthread_mutex_lock(&lock);
+ pthread_mutex_lock(&lock);
- swap_send_global();
+ swap_send_global();
- if (apps)
- ebpf_swap_send_apps_data(apps_groups_root_target);
+ if (apps & NETDATA_EBPF_APPS_FLAG_CHART_CREATED)
+ ebpf_swap_send_apps_data(apps_groups_root_target);
- if (cgroup)
- ebpf_swap_send_cgroup_data(update_every);
+ if (cgroup)
+ ebpf_swap_send_cgroup_data(update_every);
- pthread_mutex_unlock(&lock);
- }
+ pthread_mutex_unlock(&lock);
pthread_mutex_unlock(&collect_data_mutex);
}
}
@@ -758,6 +753,7 @@ void ebpf_swap_create_apps_charts(struct ebpf_module *em, void *ptr)
20192,
ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
root, em->update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
+ em->apps_charts |= NETDATA_EBPF_APPS_FLAG_CHART_CREATED;
}
/**
@@ -817,8 +813,8 @@ static int ebpf_swap_load_bpf(ebpf_module_t *em)
{
int ret = 0;
if (em->load == EBPF_LOAD_LEGACY) {
- probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &objects);
- if (!probe_links) {
+ em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
+ if (!em->probe_links) {
ret = -1;
}
}
@@ -849,7 +845,7 @@ static int ebpf_swap_load_bpf(ebpf_module_t *em)
*/
void *ebpf_swap_thread(void *ptr)
{
- netdata_thread_cleanup_push(ebpf_swap_cleanup, ptr);
+ netdata_thread_cleanup_push(ebpf_swap_exit, ptr);
ebpf_module_t *em = (ebpf_module_t *)ptr;
em->maps = swap_maps;