From b171110fdfa3c85a3f940930295ef5e7883f7796 Mon Sep 17 00:00:00 2001 From: thiagoftsm Date: Mon, 12 Aug 2024 05:12:18 +0000 Subject: [PATCH] Improve memory usage (#79) --- kernel-collector | 2 +- src/cachestat.bpf.c | 39 ++++++++++++++++----------------------- src/cachestat.c | 3 ++- src/dc.bpf.c | 6 +++--- src/netdata_core_common.h | 7 ------- src/process.bpf.c | 1 - src/process.c | 3 +-- src/swap.bpf.c | 4 ++-- 8 files changed, 25 insertions(+), 40 deletions(-) diff --git a/kernel-collector b/kernel-collector index 62f9d87..4a36363 160000 --- a/kernel-collector +++ b/kernel-collector @@ -1 +1 @@ -Subproject commit 62f9d87383ba17809cc09287b17289cc4056e817 +Subproject commit 4a3636354ad0e4abb5855c345d66596774a139d9 diff --git a/src/cachestat.bpf.c b/src/cachestat.bpf.c index b5d67ee..ccb8d06 100644 --- a/src/cachestat.bpf.c +++ b/src/cachestat.bpf.c @@ -38,8 +38,10 @@ struct { * ***********************************************************************************/ -static __always_inline int netdata_cachetat_not_update_apps() +static __always_inline int netdata_cachetat_not_update_apps(__u32 idx) { + libnetdata_update_global(&cstat_global, idx, 1); + __u32 key = NETDATA_CONTROLLER_APPS_ENABLED; __u32 *apps = bpf_map_lookup_elem(&cstat_ctrl ,&key); if (apps && *apps) @@ -52,23 +54,21 @@ static __always_inline int netdata_common_page_cache_lru() { netdata_cachestat_t *fill, data = {}; - libnetdata_update_global(&cstat_global, NETDATA_KEY_MISSES, 1); - - if (netdata_cachetat_not_update_apps()) + if (netdata_cachetat_not_update_apps(NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU)) return 0; __u32 key = 0; __u32 tgid = 0; 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(); - data.tgid = tgid; libnetdata_update_uid_gid(&data.uid, &data.gid); + data.tgid = tgid; bpf_get_current_comm(&data.name, TASK_COMM_LEN); - 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); @@ -80,23 +80,21 @@ static __always_inline int netdata_common_page_cache_lru() static __always_inline int netdata_common_page_accessed() { netdata_cachestat_t *fill, data = {}; - libnetdata_update_global(&cstat_global, NETDATA_KEY_TOTAL, 1); - - if (netdata_cachetat_not_update_apps()) + if (netdata_cachetat_not_update_apps(NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED)) return 0; __u32 key = 0; __u32 tgid = 0; 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(); data.tgid = tgid; libnetdata_update_uid_gid(&data.uid, &data.gid); bpf_get_current_comm(&data.name, TASK_COMM_LEN); - 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); @@ -109,22 +107,21 @@ static __always_inline int netdata_common_page_dirtied() { netdata_cachestat_t *fill, data = {}; - libnetdata_update_sglobal(&cstat_global, NETDATA_KEY_MISSES, -1); - if (netdata_cachetat_not_update_apps()) + if (netdata_cachetat_not_update_apps(NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED)) return 0; __u32 key = 0; __u32 tgid = 0; 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(); data.tgid = tgid; libnetdata_update_uid_gid(&data.uid, &data.gid); bpf_get_current_comm(&data.name, TASK_COMM_LEN); - 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); @@ -137,25 +134,21 @@ static __always_inline int netdata_common_buffer_dirty() { netdata_cachestat_t *fill, data = {}; - libnetdata_update_sglobal(&cstat_global, NETDATA_KEY_TOTAL, -1); - libnetdata_update_global(&cstat_global, NETDATA_KEY_DIRTY, 1); - if (netdata_cachetat_not_update_apps()) + if (netdata_cachetat_not_update_apps(NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY)) return 0; __u32 key = 0; __u32 tgid = 0; fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid); if (fill) { - libnetdata_update_s64(&fill->total, -1); - libnetdata_update_u64(&fill->dirty, 1); + libnetdata_update_u32(&fill->mark_buffer_dirty, 1); } else { data.ct = bpf_ktime_get_ns(); data.tgid = tgid; libnetdata_update_uid_gid(&data.uid, &data.gid); bpf_get_current_comm(&data.name, TASK_COMM_LEN); - 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/src/cachestat.c b/src/cachestat.c index 142f7a5..226fc13 100644 --- a/src/cachestat.c +++ b/src/cachestat.c @@ -203,7 +203,8 @@ static pid_t ebpf_update_tables(int global, int apps) if (ret) fprintf(stderr, "Cannot insert value to global table."); - netdata_cachestat_t stats = { .ct = 0, .uid = 0, .tgid = 0, .total = 1, .misses = 1, .dirty = 1 }; + netdata_cachestat_t stats = { .add_to_page_cache_lru = 1, .mark_page_accessed = 1, + .account_page_dirtied = 1, .mark_buffer_dirty = 1 }; idx = (pid_t)pid; ret = bpf_map_update_elem(apps, &idx, &stats, 0); diff --git a/src/dc.bpf.c b/src/dc.bpf.c index f9dd507..2d67b32 100644 --- a/src/dc.bpf.c +++ b/src/dc.bpf.c @@ -61,7 +61,7 @@ static __always_inline int netdata_common_lookup_fast() 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.references = 1; data.tgid = tgid; @@ -88,7 +88,7 @@ static __always_inline int netdata_common_d_lookup(long ret) 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.slow = 1; data.tgid = tgid; @@ -104,7 +104,7 @@ static __always_inline int netdata_common_d_lookup(long ret) 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/src/netdata_core_common.h b/src/netdata_core_common.h index 4b5f9ba..f7be4cc 100644 --- a/src/netdata_core_common.h +++ b/src/netdata_core_common.h @@ -35,13 +35,6 @@ typedef struct ebpf_specify_name { bool retprobe; } ebpf_specify_name_t; -enum cachestat_counters_user_ring { - 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 -}; - /** * Update names * diff --git a/src/process.bpf.c b/src/process.bpf.c index 2b7061b..1ac60a9 100644 --- a/src/process.bpf.c +++ b/src/process.bpf.c @@ -75,7 +75,6 @@ static __always_inline int netdata_common_release_task() fill = netdata_get_pid_structure(&key, &tgid, &process_ctrl, &tbl_pid_stats); if (fill) { libnetdata_update_u32(&fill->release_call, 1) ; - fill->removeme = 1; libnetdata_update_global(&process_ctrl, NETDATA_CONTROLLER_PID_TABLE_DEL, 1); } diff --git a/src/process.c b/src/process.c index 4f9dc75..23c3bf6 100644 --- a/src/process.c +++ b/src/process.c @@ -146,8 +146,7 @@ static pid_t ebpf_update_tables(int global, int apps) pid_t pid = ebpf_fill_global(global); struct netdata_pid_stat_t stats = { .pid = pid, .tgid = pid, .exit_call = 1, .release_call = 1, - .create_process = 1, .create_thread = 1, .task_err = 1, - .removeme = 0 }; + .create_process = 1, .create_thread = 1, .task_err = 1 }; uint32_t idx; for (idx = 0 ; idx < NETDATA_EBPF_CORE_MIN_STORE; idx++) { diff --git a/src/swap.bpf.c b/src/swap.bpf.c index de18b1f..ac453b5 100644 --- a/src/swap.bpf.c +++ b/src/swap.bpf.c @@ -61,7 +61,7 @@ static __always_inline int common_readpage() 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(); data.tgid = tgid; @@ -90,7 +90,7 @@ static __always_inline int common_writepage() 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(); data.tgid = tgid;