summaryrefslogtreecommitdiffstats
path: root/collectors/ebpf.plugin/ebpf_cachestat.c
diff options
context:
space:
mode:
Diffstat (limited to 'collectors/ebpf.plugin/ebpf_cachestat.c')
-rw-r--r--collectors/ebpf.plugin/ebpf_cachestat.c139
1 files changed, 53 insertions, 86 deletions
diff --git a/collectors/ebpf.plugin/ebpf_cachestat.c b/collectors/ebpf.plugin/ebpf_cachestat.c
index 4c410647d..b21cc6103 100644
--- a/collectors/ebpf.plugin/ebpf_cachestat.c
+++ b/collectors/ebpf.plugin/ebpf_cachestat.c
@@ -15,15 +15,6 @@ netdata_cachestat_pid_t *cachestat_vector = NULL;
static netdata_idx_t cachestat_hash_values[NETDATA_CACHESTAT_END];
static netdata_idx_t *cachestat_values = NULL;
-struct netdata_static_thread cachestat_threads = {.name = "CACHESTAT KERNEL",
- .config_section = NULL,
- .config_name = NULL,
- .env_name = NULL,
- .enabled = 1,
- .thread = NULL,
- .init_routine = NULL,
- .start_routine = NULL};
-
ebpf_local_maps_t cachestat_maps[] = {{.name = "cstat_global", .internal_input = NETDATA_CACHESTAT_END,
.user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
.map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
@@ -51,6 +42,9 @@ netdata_ebpf_targets_t cachestat_targets[] = { {.name = "add_to_page_cache_lru",
{.name = "mark_buffer_dirty", .mode = EBPF_LOAD_TRAMPOLINE},
{.name = NULL, .mode = EBPF_LOAD_TRAMPOLINE}};
+static char *account_page[NETDATA_CACHESTAT_ACCOUNT_DIRTY_END] ={ "account_page_dirtied",
+ "__set_page_dirty", "__folio_mark_dirty" };
+
#ifdef LIBBPF_MAJOR_VERSION
#include "includes/cachestat.skel.h" // BTF code
@@ -83,10 +77,12 @@ static void ebpf_cachestat_disable_probe(struct cachestat_bpf *obj)
*/
static void ebpf_cachestat_disable_specific_probe(struct cachestat_bpf *obj)
{
- if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_16) {
+ if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
+ account_page[NETDATA_CACHESTAT_FOLIO_DIRTY])) {
bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_kprobe, false);
bpf_program__set_autoload(obj->progs.netdata_set_page_dirty_kprobe, false);
- } else if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_15) {
+ } else if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
+ account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY])) {
bpf_program__set_autoload(obj->progs.netdata_folio_mark_dirty_kprobe, false);
bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_kprobe, false);
} else {
@@ -122,10 +118,12 @@ static void ebpf_cachestat_disable_trampoline(struct cachestat_bpf *obj)
*/
static void ebpf_cachestat_disable_specific_trampoline(struct cachestat_bpf *obj)
{
- if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_16) {
+ if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
+ account_page[NETDATA_CACHESTAT_FOLIO_DIRTY])) {
bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_fentry, false);
bpf_program__set_autoload(obj->progs.netdata_set_page_dirty_fentry, false);
- } else if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_15) {
+ } else if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
+ account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY])) {
bpf_program__set_autoload(obj->progs.netdata_folio_mark_dirty_fentry, false);
bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_fentry, false);
} else {
@@ -149,10 +147,12 @@ static inline void netdata_set_trampoline_target(struct cachestat_bpf *obj)
bpf_program__set_attach_target(obj->progs.netdata_mark_page_accessed_fentry, 0,
cachestat_targets[NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED].name);
- if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_16) {
+ if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
+ account_page[NETDATA_CACHESTAT_FOLIO_DIRTY])) {
bpf_program__set_attach_target(obj->progs.netdata_folio_mark_dirty_fentry, 0,
cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
- } else if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_15) {
+ } else if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
+ account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY])) {
bpf_program__set_attach_target(obj->progs.netdata_set_page_dirty_fentry, 0,
cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
} else {
@@ -192,12 +192,14 @@ static int ebpf_cachestat_attach_probe(struct cachestat_bpf *obj)
if (ret)
return -1;
- if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_16) {
+ if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
+ account_page[NETDATA_CACHESTAT_FOLIO_DIRTY])) {
obj->links.netdata_folio_mark_dirty_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_folio_mark_dirty_kprobe,
false,
cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
ret = libbpf_get_error(obj->links.netdata_folio_mark_dirty_kprobe);
- } else if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_15) {
+ } else if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
+ account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY])) {
obj->links.netdata_set_page_dirty_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_set_page_dirty_kprobe,
false,
cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
@@ -278,7 +280,7 @@ static void ebpf_cachestat_disable_release_task(struct cachestat_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_cachestat_load_and_attach(struct cachestat_bpf *obj, ebpf_module_t *em)
{
@@ -331,18 +333,13 @@ static inline int ebpf_cachestat_load_and_attach(struct cachestat_bpf *obj, ebpf
static void ebpf_cachestat_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(cachestat_counter_publish_aggregated);
freez(cachestat_vector);
freez(cachestat_values);
- freez(cachestat_threads.thread);
#ifdef LIBBPF_MAJOR_VERSION
if (bpf_obj)
@@ -363,20 +360,7 @@ static void ebpf_cachestat_free(ebpf_module_t *em)
static void ebpf_cachestat_exit(void *ptr)
{
ebpf_module_t *em = (ebpf_module_t *)ptr;
- netdata_thread_cancel(*cachestat_threads.thread);
- ebpf_cachestat_free(em);
-}
-/**
- * Cachestat cleanup
- *
- * Clean up allocated addresses.
- *
- * @param ptr thread data.
- */
-static void ebpf_cachestat_cleanup(void *ptr)
-{
- ebpf_module_t *em = (ebpf_module_t *)ptr;
ebpf_cachestat_free(em);
}
@@ -656,7 +640,7 @@ void ebpf_cachestat_create_apps_charts(struct ebpf_module *em, void *ptr)
*
* Read the table with number of calls for all functions
*/
-static void read_global_table()
+static void ebpf_cachestat_read_global_table()
{
uint32_t idx;
netdata_idx_t *val = cachestat_hash_values;
@@ -677,35 +661,6 @@ static void read_global_table()
}
/**
- * Socket read hash
- *
- * This is the thread callback.
- * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
- *
- * @param ptr It is a NULL value for this thread.
- *
- * @return It always returns NULL.
- */
-void *ebpf_cachestat_read_hash(void *ptr)
-{
- netdata_thread_cleanup_push(ebpf_cachestat_cleanup, ptr);
- heartbeat_t hb;
- heartbeat_init(&hb);
-
- ebpf_module_t *em = (ebpf_module_t *)ptr;
-
- usec_t step = NETDATA_LATENCY_CACHESTAT_SLEEP_MS * em->update_every;
- while (!ebpf_exit_plugin) {
- (void)heartbeat_next(&hb, step);
-
- read_global_table();
- }
-
- netdata_thread_cleanup_pop(1);
- return NULL;
-}
-
-/**
* Send global
*
* Send global charts to Netdata
@@ -1106,26 +1061,23 @@ void ebpf_cachestat_send_cgroup_data(int update_every)
*/
static void cachestat_collector(ebpf_module_t *em)
{
- cachestat_threads.thread = callocz(1, sizeof(netdata_thread_t));
- cachestat_threads.start_routine = ebpf_cachestat_read_hash;
-
- netdata_thread_create(cachestat_threads.thread, cachestat_threads.name, NETDATA_THREAD_OPTION_DEFAULT,
- ebpf_cachestat_read_hash, em);
-
netdata_publish_cachestat_t publish;
memset(&publish, 0, sizeof(publish));
int cgroups = em->cgroup_charts;
int update_every = em->update_every;
heartbeat_t hb;
heartbeat_init(&hb);
- usec_t step = update_every * USEC_PER_SEC;
+ int counter = update_every - 1;
//This will be cancelled by its parent
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_cachestat_read_global_table();
pthread_mutex_lock(&collect_data_mutex);
if (apps)
read_apps_table();
@@ -1237,16 +1189,28 @@ static void ebpf_cachestat_allocate_global_vectors(int apps)
* Update Internal value
*
* Update values used during runtime.
+ *
+ * @return It returns 0 when one of the functions is present and -1 otherwise.
*/
-static void ebpf_cachestat_set_internal_value()
+static int ebpf_cachestat_set_internal_value()
{
- static char *account_page[] = { "account_page_dirtied", "__set_page_dirty", "__folio_mark_dirty" };
- if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_16)
- cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name = account_page[NETDATA_CACHESTAT_FOLIO_DIRTY];
- else if (running_on_kernel >= NETDATA_EBPF_KERNEL_5_15)
- cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name = account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY];
- else
- cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name = account_page[NETDATA_CACHESTAT_ACCOUNT_PAGE_DIRTY];
+ ebpf_addresses_t address = {.function = NULL, .hash = 0, .addr = 0};
+ int i;
+ for (i = 0; i < NETDATA_CACHESTAT_ACCOUNT_DIRTY_END ; i++) {
+ address.function = account_page[i];
+ ebpf_load_addresses(&address, -1);
+ if (address.addr)
+ break;
+ }
+
+ if (!address.addr) {
+ error("%s cachestat.", NETDATA_EBPF_DEFAULT_FNT_NOT_FOUND);
+ return -1;
+ }
+
+ cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name = address.function;
+
+ return 0;
}
/*
@@ -1300,7 +1264,10 @@ void *ebpf_cachestat_thread(void *ptr)
ebpf_update_pid_table(&cachestat_maps[NETDATA_CACHESTAT_PID_STATS], em);
- ebpf_cachestat_set_internal_value();
+ if (ebpf_cachestat_set_internal_value()) {
+ em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
+ goto endcachestat;
+ }
#ifdef LIBBPF_MAJOR_VERSION
ebpf_adjust_thread_load(em, default_btf);