Skip to content

Commit

Permalink
Improve memory usage (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoftsm authored Aug 12, 2024
1 parent 8c8c0f3 commit b171110
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 40 deletions.
39 changes: 16 additions & 23 deletions src/cachestat.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/cachestat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/dc.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/netdata_core_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 0 additions & 1 deletion src/process.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
4 changes: 2 additions & 2 deletions src/swap.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b171110

Please sign in to comment.