From 4a3636354ad0e4abb5855c345d66596774a139d9 Mon Sep 17 00:00:00 2001 From: thiagoftsm Date: Mon, 12 Aug 2024 04:33:29 +0000 Subject: [PATCH] Improve eBPF memory usage (#397) --- includes/netdata_cache.h | 14 ++++++++------ includes/netdata_dc.h | 6 +++--- includes/netdata_process.h | 2 -- includes/netdata_swap.h | 4 ++-- kernel/cachestat_kern.c | 34 ++++++++++++++++------------------ kernel/dc_kern.c | 6 +++--- kernel/swap_kern.c | 4 ++-- 7 files changed, 34 insertions(+), 36 deletions(-) diff --git a/includes/netdata_cache.h b/includes/netdata_cache.h index 80abf180..6bccf6f0 100644 --- a/includes/netdata_cache.h +++ b/includes/netdata_cache.h @@ -10,15 +10,17 @@ typedef struct netdata_cachestat { __u32 gid; char name[TASK_COMM_LEN]; - __s64 total; - __s64 misses; - __u64 dirty; + __u32 add_to_page_cache_lru; + __u32 mark_page_accessed; + __u32 account_page_dirtied; + __u32 mark_buffer_dirty; } netdata_cachestat_t; enum cachestat_counters { - NETDATA_KEY_TOTAL, - NETDATA_KEY_MISSES, - NETDATA_KEY_DIRTY, + NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU, + NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED, + NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED, + NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY, // Keep this as last and don't skip numbers as it is used as element counter NETDATA_CACHESTAT_END diff --git a/includes/netdata_dc.h b/includes/netdata_dc.h index 8b64c687..52d08443 100644 --- a/includes/netdata_dc.h +++ b/includes/netdata_dc.h @@ -10,9 +10,9 @@ typedef struct netdata_dc_stat { __u32 gid; char name[TASK_COMM_LEN]; - __u64 references; - __u64 slow; - __u64 missed; + __u32 references; + __u32 slow; + __u32 missed; } netdata_dc_stat_t; enum directory_cache_counters { diff --git a/includes/netdata_process.h b/includes/netdata_process.h index d538a255..7f3d9561 100644 --- a/includes/netdata_process.h +++ b/includes/netdata_process.h @@ -44,8 +44,6 @@ struct netdata_pid_stat_t { __u32 create_thread; //Start syscall (fork, clone, forkv) __u32 task_err; - - __u8 removeme; }; enum process_counters { diff --git a/includes/netdata_swap.h b/includes/netdata_swap.h index 0876a511..2e5a79e9 100644 --- a/includes/netdata_swap.h +++ b/includes/netdata_swap.h @@ -10,8 +10,8 @@ typedef struct netdata_swap_access { __u32 gid; char name[TASK_COMM_LEN]; - __u64 read; - __u64 write; + __u32 read; + __u32 write; } netdata_swap_access_t; enum swap_counters { diff --git a/kernel/cachestat_kern.c b/kernel/cachestat_kern.c index 58628f3c..861d2e4a 100644 --- a/kernel/cachestat_kern.c +++ b/kernel/cachestat_kern.c @@ -74,7 +74,7 @@ SEC("kprobe/add_to_page_cache_lru") int netdata_add_to_page_cache_lru(struct pt_regs* ctx) { netdata_cachestat_t *fill, data = {}; - libnetdata_update_global(&cstat_global, NETDATA_KEY_MISSES, 1); + libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU, 1); __u32 key = 0; __u32 tgid = 0; @@ -83,7 +83,7 @@ int netdata_add_to_page_cache_lru(struct pt_regs* ctx) fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid); if (fill) { - libnetdata_update_s64(&fill->misses, 1); + libnetdata_update_u32(&fill->add_to_page_cache_lru, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid); @@ -94,7 +94,7 @@ int netdata_add_to_page_cache_lru(struct pt_regs* ctx) data.name[0] = '\0'; #endif - data.misses = 1; + data.add_to_page_cache_lru = 1; bpf_map_update_elem(&cstat_pid, &key, &data, BPF_ANY); libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1); @@ -107,7 +107,7 @@ SEC("kprobe/mark_page_accessed") int netdata_mark_page_accessed(struct pt_regs* ctx) { netdata_cachestat_t *fill, data = {}; - libnetdata_update_global(&cstat_global, NETDATA_KEY_TOTAL, 1); + libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED, 1); __u32 key = 0; __u32 tgid = 0; @@ -116,18 +116,19 @@ int netdata_mark_page_accessed(struct pt_regs* ctx) fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid); if (fill) { - libnetdata_update_s64(&fill->total, 1); + libnetdata_update_u32(&fill->mark_page_accessed, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid); data.tgid = tgid; + #if (LINUX_VERSION_CODE > KERNEL_VERSION(4,11,0)) bpf_get_current_comm(&data.name, TASK_COMM_LEN); #else data.name[0] = '\0'; #endif - data.total = 1; + data.mark_page_accessed = 1; bpf_map_update_elem(&cstat_pid, &key, &data, BPF_ANY); libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1); @@ -159,7 +160,7 @@ int netdata_set_page_dirty(struct pt_regs* ctx) #endif netdata_cachestat_t *fill, data = {}; - libnetdata_update_sglobal(&cstat_global, NETDATA_KEY_MISSES, -1); + libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED, 1); __u32 key = 0; __u32 tgid = 0; @@ -168,7 +169,7 @@ int netdata_set_page_dirty(struct pt_regs* ctx) fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid); if (fill) { - libnetdata_update_s64(&fill->misses, -1); + libnetdata_update_u32(&fill->account_page_dirtied, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid); @@ -179,7 +180,7 @@ int netdata_set_page_dirty(struct pt_regs* ctx) data.name[0] = '\0'; #endif - data.misses = -1; + data.account_page_dirtied = 1; bpf_map_update_elem(&cstat_pid, &key, &data, BPF_ANY); libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1); @@ -196,7 +197,7 @@ SEC("kprobe/account_page_dirtied") int netdata_account_page_dirtied(struct pt_regs* ctx) { netdata_cachestat_t *fill, data = {}; - libnetdata_update_sglobal(&cstat_global, NETDATA_KEY_MISSES, -1); + libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED, 1); __u32 key = 0; __u32 tgid = 0; @@ -205,7 +206,7 @@ int netdata_account_page_dirtied(struct pt_regs* ctx) fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid); if (fill) { - libnetdata_update_s64(&fill->misses, -1); + libnetdata_update_u32(&fill->account_page_dirtied, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid); @@ -216,7 +217,7 @@ int netdata_account_page_dirtied(struct pt_regs* ctx) data.name[0] = '\0'; #endif - data.misses = -1; + data.account_page_dirtied = 1; bpf_map_update_elem(&cstat_pid, &key, &data, BPF_ANY); libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1); @@ -230,8 +231,7 @@ SEC("kprobe/mark_buffer_dirty") int netdata_mark_buffer_dirty(struct pt_regs* ctx) { netdata_cachestat_t *fill, data = {}; - libnetdata_update_sglobal(&cstat_global, NETDATA_KEY_TOTAL, -1); - libnetdata_update_global(&cstat_global, NETDATA_KEY_DIRTY, 1); + libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY, 1); __u32 key = 0; __u32 tgid = 0; @@ -240,8 +240,7 @@ int netdata_mark_buffer_dirty(struct pt_regs* ctx) fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid); if (fill) { - libnetdata_update_u64(&fill->total, -1); - libnetdata_update_u64(&fill->dirty, 1); + libnetdata_update_u32(&fill->mark_buffer_dirty, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid); @@ -252,8 +251,7 @@ int netdata_mark_buffer_dirty(struct pt_regs* ctx) data.name[0] = '\0'; #endif - data.dirty = 1; - data.total = -1; + data.mark_buffer_dirty = 1; bpf_map_update_elem(&cstat_pid, &key, &data, BPF_ANY); libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1); diff --git a/kernel/dc_kern.c b/kernel/dc_kern.c index e15cda88..d76b4683 100644 --- a/kernel/dc_kern.c +++ b/kernel/dc_kern.c @@ -85,7 +85,7 @@ int netdata_lookup_fast(struct pt_regs* ctx) fill = netdata_get_pid_structure(&key, &tgid, &dcstat_ctrl, &dcstat_pid); if (fill) { - libnetdata_update_u64(&fill->references, 1); + libnetdata_update_u32(&fill->references, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid); @@ -120,7 +120,7 @@ int netdata_d_lookup(struct pt_regs* ctx) fill = netdata_get_pid_structure(&key, &tgid, &dcstat_ctrl, &dcstat_pid); if (fill) { - libnetdata_update_u64(&fill->slow, 1); + libnetdata_update_u32(&fill->slow, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid); @@ -142,7 +142,7 @@ int netdata_d_lookup(struct pt_regs* ctx) libnetdata_update_global(&dcstat_global, NETDATA_KEY_DC_MISS, 1); fill = netdata_get_pid_structure(&key, &tgid, &dcstat_ctrl, &dcstat_pid); if (fill) { - libnetdata_update_u64(&fill->missed, 1); + libnetdata_update_u32(&fill->missed, 1); } } diff --git a/kernel/swap_kern.c b/kernel/swap_kern.c index 3daafefb..031d8b84 100644 --- a/kernel/swap_kern.c +++ b/kernel/swap_kern.c @@ -89,7 +89,7 @@ int netdata_swap_readpage(struct pt_regs* ctx) netdata_swap_access_t *fill = netdata_get_pid_structure(&key, &tgid, &swap_ctrl, &tbl_pid_swap); if (fill) { - libnetdata_update_u64(&fill->read, 1); + libnetdata_update_u32(&fill->read, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid); @@ -123,7 +123,7 @@ int netdata_swap_writepage(struct pt_regs* ctx) netdata_swap_access_t *fill = netdata_get_pid_structure(&key, &tgid, &swap_ctrl, &tbl_pid_swap); if (fill) { - libnetdata_update_u64(&fill->write, 1); + libnetdata_update_u32(&fill->write, 1); } else { data.ct = bpf_ktime_get_ns(); libnetdata_update_uid_gid(&data.uid, &data.gid);