Skip to content

Commit

Permalink
Monitor elements hash table (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoftsm authored May 12, 2023
1 parent 6e87f96 commit af86416
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 250 deletions.
11 changes: 10 additions & 1 deletion includes/netdata_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static __always_inline void *netdata_get_pid_structure(__u32 *store_pid, void *c
{
__u32 pid, key = NETDATA_CONTROLLER_APPS_LEVEL;

__u32 *level = bpf_map_lookup_elem(ctrl_tbl ,&key);
__u64 *level = bpf_map_lookup_elem(ctrl_tbl ,&key);
if (level) {
if (*level == NETDATA_APPS_LEVEL_REAL_PARENT)
pid = netdata_get_real_parent_pid();
Expand All @@ -179,6 +179,15 @@ static __always_inline void *netdata_get_pid_structure(__u32 *store_pid, void *c
return bpf_map_lookup_elem(pid_tbl, store_pid);
}

static __always_inline __u32 monitor_apps(void *ctrl_tbl)
{
__u32 apps_key = NETDATA_CONTROLLER_APPS_ENABLED;
__u64 *apps = bpf_map_lookup_elem(ctrl_tbl ,&apps_key);
if (!apps || (apps && *apps == 0))
return 0;

return 1;
}

#endif /* _NETDATA_COMMON_ */

23 changes: 23 additions & 0 deletions kernel/btrfs_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ struct {
__uint(max_entries, 4192);
} tmp_btrfs SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, __u64);
__uint(max_entries, NETDATA_CONTROLLER_END);
} btrfs_ctrl SEC(".maps");
#else

struct bpf_map_def SEC("maps") tbl_btrfs = {
Expand All @@ -73,6 +79,13 @@ struct bpf_map_def SEC("maps") tmp_btrfs = {
.value_size = sizeof(__u64),
.max_entries = 4192
};

struct bpf_map_def SEC("maps") btrfs_ctrl = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(__u64),
.max_entries = NETDATA_CONTROLLER_END
};
#endif

/************************************************************************************
Expand All @@ -89,6 +102,8 @@ static __always_inline int netdata_btrfs_entry()

bpf_map_update_elem(&tmp_btrfs, &pid, &ts, BPF_ANY);

libnetdata_update_global(&btrfs_ctrl, NETDATA_CONTROLLER_TEMP_TABLE_ADD, 1);

return 0;
}

Expand Down Expand Up @@ -182,6 +197,8 @@ int netdata_ret_generic_file_read_iter(struct pt_regs *ctx)
data = bpf_ktime_get_ns() - *fill;
bpf_map_delete_elem(&tmp_btrfs, &pid);

libnetdata_update_global(&btrfs_ctrl, NETDATA_CONTROLLER_TEMP_TABLE_DEL, 1);

// Skip entries with backward time
if ( (s64)data < 0)
return 0;
Expand All @@ -208,6 +225,8 @@ int netdata_ret_btrfs_file_write_iter(struct pt_regs *ctx)
data = bpf_ktime_get_ns() - *fill;
bpf_map_delete_elem(&tmp_btrfs, &pid);

libnetdata_update_global(&btrfs_ctrl, NETDATA_CONTROLLER_TEMP_TABLE_DEL, 1);

// Skip entries with backward time
if ( (s64)data < 0)
return 0;
Expand All @@ -234,6 +253,8 @@ int netdata_ret_btrfs_file_open(struct pt_regs *ctx)
data = bpf_ktime_get_ns() - *fill;
bpf_map_delete_elem(&tmp_btrfs, &pid);

libnetdata_update_global(&btrfs_ctrl, NETDATA_CONTROLLER_TEMP_TABLE_DEL, 1);

// Skip entries with backward time
if ( (s64)data < 0)
return 0;
Expand All @@ -260,6 +281,8 @@ int netdata_ret_btrfs_sync_file(struct pt_regs *ctx)
data = bpf_ktime_get_ns() - *fill;
bpf_map_delete_elem(&tmp_btrfs, &pid);

libnetdata_update_global(&btrfs_ctrl, NETDATA_CONTROLLER_TEMP_TABLE_DEL, 1);

// Skip entries with backward time
if ( (s64)data < 0)
return 0;
Expand Down
64 changes: 31 additions & 33 deletions kernel/cachestat_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct {
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, __u32);
__type(value, __u64);
__uint(max_entries, NETDATA_CONTROLLER_END);
} cstat_ctrl SEC(".maps");

Expand All @@ -57,7 +57,7 @@ struct bpf_map_def SEC("maps") cstat_pid = {
struct bpf_map_def SEC("maps") cstat_ctrl = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(__u32),
.value_size = sizeof(__u64),
.max_entries = NETDATA_CONTROLLER_END
};

Expand All @@ -75,18 +75,18 @@ int netdata_add_to_page_cache_lru(struct pt_regs* ctx)
netdata_cachestat_t *fill, data = {};
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU, 1);

__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&cstat_ctrl ,&key);
if (apps)
if (*apps == 0)
return 0;
__u32 key = 0;
if (!monitor_apps(&cstat_ctrl))
return 0;

fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->add_to_page_cache_lru, 1);
} else {
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);
}

return 0;
Expand All @@ -98,18 +98,18 @@ int netdata_mark_page_accessed(struct pt_regs* ctx)
netdata_cachestat_t *fill, data = {};
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED, 1);

__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&cstat_ctrl ,&key);
if (apps)
if (*apps == 0)
return 0;
__u32 key = 0;
if (!monitor_apps(&cstat_ctrl))
return 0;

fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->mark_page_accessed, 1);
} else {
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);
}

return 0;
Expand Down Expand Up @@ -140,18 +140,18 @@ int netdata_set_page_dirty(struct pt_regs* ctx)
netdata_cachestat_t *fill, data = {};
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED, 1);

__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&cstat_ctrl ,&key);
if (apps)
if (*apps == 0)
return 0;
__u32 key = 0;
if (!monitor_apps(&cstat_ctrl))
return 0;

fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->account_page_dirtied, 1);
} else {
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);
}

return 0;
Expand All @@ -167,18 +167,18 @@ int netdata_account_page_dirtied(struct pt_regs* ctx)
netdata_cachestat_t *fill, data = {};
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED, 1);

__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&cstat_ctrl ,&key);
if (apps)
if (*apps == 0)
return 0;
__u32 key = 0;
if (!monitor_apps(&cstat_ctrl))
return 0;

fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->account_page_dirtied, 1);
} else {
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);
}

return 0;
Expand All @@ -191,18 +191,18 @@ int netdata_mark_buffer_dirty(struct pt_regs* ctx)
netdata_cachestat_t *fill, data = {};
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY, 1);

__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&cstat_ctrl ,&key);
if (apps)
if (*apps == 0)
return 0;
__u32 key = 0;
if (!monitor_apps(&cstat_ctrl))
return 0;

fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->mark_buffer_dirty, 1);
} else {
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);
}

return 0;
Expand All @@ -221,17 +221,15 @@ SEC("kprobe/release_task")
int netdata_release_task_dc(struct pt_regs* ctx)
{
netdata_cachestat_t *removeme;
__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&cstat_ctrl ,&key);
if (apps) {
if (*apps == 0)
return 0;
} else
__u32 key = 0;
if (!monitor_apps(&cstat_ctrl))
return 0;

removeme = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
if (removeme) {
bpf_map_delete_elem(&cstat_pid, &key);

libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_DEL, 1);
}

return 0;
Expand Down
61 changes: 29 additions & 32 deletions kernel/dc_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct {
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__type(key, __u32);
__type(value, __u32);
__type(value, __u64);
__uint(max_entries, NETDATA_CONTROLLER_END);
} dcstat_ctrl SEC(".maps");

Expand All @@ -60,7 +60,7 @@ struct bpf_map_def SEC("maps") dcstat_pid = {
struct bpf_map_def SEC("maps") dcstat_ctrl = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(__u32),
.value_size = sizeof(__u64),
.max_entries = NETDATA_CONTROLLER_END
};

Expand All @@ -78,18 +78,18 @@ int netdata_lookup_fast(struct pt_regs* ctx)
netdata_dc_stat_t *fill, data = {};
libnetdata_update_global(&dcstat_global, NETDATA_KEY_DC_REFERENCE, 1);

__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&dcstat_ctrl ,&key);
if (apps)
if (*apps == 0)
return 0;
__u32 key = 0;
if (!monitor_apps(&dcstat_ctrl))
return 0;

fill = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
if (fill) {
libnetdata_update_u64(&fill->references, 1);
} else {
data.references = 1;
bpf_map_update_elem(&dcstat_pid, &key, &data, BPF_ANY);

libnetdata_update_global(&dcstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1);
}

return 0;
Expand All @@ -103,32 +103,31 @@ int netdata_d_lookup(struct pt_regs* ctx)

int ret = PT_REGS_RC(ctx);

__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&dcstat_ctrl ,&key);
if (!apps)
__u32 key = 0;
if (!monitor_apps(&dcstat_ctrl))
return 0;

if (*apps == 1) {
fill = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
if (fill) {
libnetdata_update_u64(&fill->slow, 1);
} else {
data.slow = 1;
bpf_map_update_elem(&dcstat_pid, &key, &data, BPF_ANY);
}
fill = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
if (fill) {
libnetdata_update_u64(&fill->slow, 1);
} else {
data.slow = 1;
bpf_map_update_elem(&dcstat_pid, &key, &data, BPF_ANY);

libnetdata_update_global(&dcstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1);
}

// file not found
if (ret == 0) {
libnetdata_update_global(&dcstat_global, NETDATA_KEY_DC_MISS, 1);
if (*apps == 1) {
fill = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
if (fill) {
libnetdata_update_u64(&fill->missed, 1);
} else {
data.missed = 1;
bpf_map_update_elem(&dcstat_pid, &key, &data, BPF_ANY);
}
fill = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
if (fill) {
libnetdata_update_u64(&fill->missed, 1);
} else {
data.missed = 1;
bpf_map_update_elem(&dcstat_pid, &key, &data, BPF_ANY);

libnetdata_update_global(&dcstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1);
}
}

Expand All @@ -148,17 +147,15 @@ SEC("kprobe/release_task")
int netdata_release_task_dc(struct pt_regs* ctx)
{
netdata_dc_stat_t *removeme;
__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 *apps = bpf_map_lookup_elem(&dcstat_ctrl ,&key);
if (apps) {
if (*apps == 0)
return 0;
} else
__u32 key = 0;
if (!monitor_apps(&dcstat_ctrl))
return 0;

removeme = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
if (removeme) {
bpf_map_delete_elem(&dcstat_pid, &key);

libnetdata_update_global(&dcstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_DEL, 1);
}

return 0;
Expand Down
Loading

0 comments on commit af86416

Please sign in to comment.