Skip to content

Commit

Permalink
Update algorithms (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoftsm authored Oct 8, 2023
1 parent 1245d0c commit b0be1dc
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 58 deletions.
52 changes: 33 additions & 19 deletions src/cachestat.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ struct {
*
***********************************************************************************/

static __always_inline int netdata_cachetat_not_update_apps(__u32 idx)
static __always_inline int netdata_cachetat_not_update_apps()
{
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 @@ -54,18 +52,22 @@ static __always_inline int netdata_common_page_cache_lru()
{
netdata_cachestat_t *fill, data = {};

if (netdata_cachetat_not_update_apps(NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU))
libnetdata_update_global(&cstat_global, NETDATA_KEY_MISSES, 1);

if (netdata_cachetat_not_update_apps())
return 0;

__u32 key = 0;
fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
__u32 tgid = 0;
fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->add_to_page_cache_lru, 1);
libnetdata_update_s64(&fill->misses, 1);
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);

data.add_to_page_cache_lru = 1;
data.misses = 1;
bpf_map_update_elem(&cstat_pid, &key, &data, BPF_ANY);

libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1);
Expand All @@ -77,19 +79,22 @@ 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(NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED))
if (netdata_cachetat_not_update_apps())
return 0;

__u32 key = 0;
fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
__u32 tgid = 0;
fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->mark_page_accessed, 1);
libnetdata_update_s64(&fill->total, 1);
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);

data.mark_page_accessed = 1;
data.total = 1;
bpf_map_update_elem(&cstat_pid, &key, &data, BPF_ANY);

libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1);
Expand All @@ -102,18 +107,21 @@ static __always_inline int netdata_common_page_dirtied()
{
netdata_cachestat_t *fill, data = {};

if (netdata_cachetat_not_update_apps(NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED))
libnetdata_update_sglobal(&cstat_global, NETDATA_KEY_MISSES, -1);
if (netdata_cachetat_not_update_apps())
return 0;

__u32 key = 0;
fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
__u32 tgid = 0;
fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->account_page_dirtied, 1);
libnetdata_update_s64(&fill->misses, -1);
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);

data.account_page_dirtied = 1;
data.misses = -1;
bpf_map_update_elem(&cstat_pid, &key, &data, BPF_ANY);

libnetdata_update_global(&cstat_ctrl, NETDATA_CONTROLLER_PID_TABLE_ADD, 1);
Expand All @@ -126,18 +134,24 @@ static __always_inline int netdata_common_buffer_dirty()
{
netdata_cachestat_t *fill, data = {};

if (netdata_cachetat_not_update_apps(NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY))
libnetdata_update_sglobal(&cstat_global, NETDATA_KEY_TOTAL, -1);
libnetdata_update_global(&cstat_global, NETDATA_KEY_DIRTY, 1);
if (netdata_cachetat_not_update_apps())
return 0;

__u32 key = 0;
fill = netdata_get_pid_structure(&key, &cstat_ctrl, &cstat_pid);
__u32 tgid = 0;
fill = netdata_get_pid_structure(&key, &tgid, &cstat_ctrl, &cstat_pid);
if (fill) {
libnetdata_update_u64(&fill->mark_buffer_dirty, 1);
libnetdata_update_s64(&fill->total, -1);
libnetdata_update_u64(&fill->dirty, 1);
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);

data.mark_buffer_dirty = 1;
data.dirty = 1;
data.total = -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: 1 addition & 2 deletions src/cachestat.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ static pid_t ebpf_update_tables(int global, int apps)
if (ret)
fprintf(stderr, "Cannot insert value to global table.");

netdata_cachestat_t stats = { .add_to_page_cache_lru = 1, .mark_page_accessed = 1,
.account_page_dirtied = 1, .mark_buffer_dirty = 1 };
netdata_cachestat_t stats = { .ct = 0, .uid = 0, .tgid = 0, .total = 1, .misses = 1, .dirty = 1 };

idx = (pid_t)pid;
ret = bpf_map_update_elem(apps, &idx, &stats, 0);
Expand Down
16 changes: 7 additions & 9 deletions src/dc.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ static __always_inline int netdata_common_lookup_fast()
{
netdata_dc_stat_t *fill, data = {};
__u32 key = 0;
__u32 tgid = 0;

libnetdata_update_global(&dcstat_global, NETDATA_KEY_DC_REFERENCE, 1);

if (netdata_dc_not_update_apps())
return 0;

fill = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
fill = netdata_get_pid_structure(&key, &tgid, &dcstat_ctrl, &dcstat_pid);
if (fill) {
libnetdata_update_u64(&fill->references, 1);
} else {
data.references = 1;
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);
bpf_map_update_elem(&dcstat_pid, &key, &data, BPF_ANY);

Expand All @@ -76,17 +78,19 @@ static __always_inline int netdata_common_d_lookup(long ret)
{
netdata_dc_stat_t *fill, data = {};
__u32 key = 0;
__u32 tgid = 0;

libnetdata_update_global(&dcstat_global, NETDATA_KEY_DC_SLOW, 1);

if (netdata_dc_not_update_apps())
return 0;

fill = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
fill = netdata_get_pid_structure(&key, &tgid, &dcstat_ctrl, &dcstat_pid);
if (fill) {
libnetdata_update_u64(&fill->slow, 1);
} else {
data.slow = 1;
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);
bpf_map_update_elem(&dcstat_pid, &key, &data, BPF_ANY);

Expand All @@ -96,15 +100,9 @@ static __always_inline int netdata_common_d_lookup(long ret)
// file not found
if (!ret) {
libnetdata_update_global(&dcstat_global, NETDATA_KEY_DC_MISS, 1);
fill = netdata_get_pid_structure(&key, &dcstat_ctrl, &dcstat_pid);
fill = netdata_get_pid_structure(&key, &tgid, &dcstat_ctrl, &dcstat_pid);
if (fill) {
libnetdata_update_u64(&fill->missed, 1);
} else {
data.missed = 1;
bpf_get_current_comm(&data.name, TASK_COMM_LEN);
bpf_map_update_elem(&dcstat_pid, &key, &data, BPF_ANY);

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

Expand Down
12 changes: 8 additions & 4 deletions src/fd.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ static __always_inline int netdata_apps_do_sys_openat2(long ret)
if (!netdata_are_apps_enabled())
return 0;

__u32 key;
fill = netdata_get_pid_structure(&key, &fd_ctrl, &tbl_fd_pid);
__u32 key = 0;
__u32 tgid = 0;
fill = netdata_get_pid_structure(&key, &tgid, &fd_ctrl, &tbl_fd_pid);
if (fill) {
libnetdata_update_u32(&fill->open_call, 1) ;
if (ret < 0)
libnetdata_update_u32(&fill->open_err, 1) ;
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);
data.open_call = 1;
if (ret < 0)
Expand Down Expand Up @@ -100,14 +102,16 @@ static __always_inline int netdata_apps_close_fd(int ret)
if (!netdata_are_apps_enabled())
return 0;

__u32 key;
fill = netdata_get_pid_structure(&key, &fd_ctrl, &tbl_fd_pid);
__u32 key = 0;
__u32 tgid = 0;
fill = netdata_get_pid_structure(&key, &tgid, &fd_ctrl, &tbl_fd_pid);
if (fill) {
libnetdata_update_u32(&fill->close_call, 1) ;
if (ret < 0)
libnetdata_update_u32(&fill->close_err, 1) ;
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);
data.close_call = 1;
if (ret < 0)
Expand Down
7 changes: 7 additions & 0 deletions src/netdata_core_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ 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
25 changes: 15 additions & 10 deletions src/process.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ static __always_inline int netdata_process_not_update_apps()
static __always_inline int netdata_common_release_task()
{
struct netdata_pid_stat_t *fill;
__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 key = 0;
__u32 tgid = 0;

libnetdata_update_global(&tbl_total_stats, NETDATA_KEY_CALLS_RELEASE_TASK, 1);
if (netdata_process_not_update_apps())
return 0;

fill = netdata_get_pid_structure(&key, &process_ctrl, &tbl_pid_stats);
fill = netdata_get_pid_structure(&key, &tgid, &process_ctrl, &tbl_pid_stats);
if (fill) {
libnetdata_update_u32(&fill->release_call, 1) ;
fill->removeme = 1;
Expand All @@ -84,7 +85,8 @@ static __always_inline int netdata_common_release_task()

static __always_inline int netdata_common_fork_clone(int ret)
{
__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 key = 0;
__u32 tgid = 0;
struct netdata_pid_stat_t data = { };
struct netdata_pid_stat_t *fill;

Expand All @@ -95,7 +97,7 @@ static __always_inline int netdata_common_fork_clone(int ret)
if (netdata_process_not_update_apps())
return 0;

fill = netdata_get_pid_structure(&key, &process_ctrl, &tbl_pid_stats);
fill = netdata_get_pid_structure(&key, &tgid, &process_ctrl, &tbl_pid_stats);
if (fill) {
fill->release_call = 0;

Expand Down Expand Up @@ -126,13 +128,14 @@ SEC("tracepoint/sched/sched_process_exit")
int netdata_tracepoint_sched_process_exit(struct netdata_sched_process_exit *ptr)
{
struct netdata_pid_stat_t *fill;
__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 key = 0;
__u32 tgid = 0;

libnetdata_update_global(&tbl_total_stats, NETDATA_KEY_CALLS_DO_EXIT, 1);
if (netdata_process_not_update_apps())
return 0;

fill = netdata_get_pid_structure(&key, &process_ctrl, &tbl_pid_stats);
fill = netdata_get_pid_structure(&key, &tgid, &process_ctrl, &tbl_pid_stats);
if (fill) {
libnetdata_update_u32(&fill->exit_call, 1) ;
}
Expand All @@ -146,15 +149,16 @@ int netdata_tracepoint_sched_process_exec(struct netdata_sched_process_exec *ptr
{
struct netdata_pid_stat_t data = { };
struct netdata_pid_stat_t *fill;
__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 key = 0;
__u32 tgid = 0;
// This is necessary, because it represents the main function to start a thread
libnetdata_update_global(&tbl_total_stats, NETDATA_KEY_CALLS_PROCESS, 1);

libnetdata_update_global(&tbl_total_stats, NETDATA_KEY_CALLS_DO_EXIT, 1);
if (netdata_process_not_update_apps())
return 0;

fill = netdata_get_pid_structure(&key, &process_ctrl, &tbl_pid_stats);
fill = netdata_get_pid_structure(&key, &tgid, &process_ctrl, &tbl_pid_stats);
if (fill) {
fill->release_call = 0;
libnetdata_update_u32(&fill->create_process, 1) ;
Expand All @@ -176,7 +180,8 @@ int netdata_tracepoint_sched_process_fork(struct netdata_sched_process_fork *ptr
{
struct netdata_pid_stat_t data = { };
struct netdata_pid_stat_t *fill;
__u32 key = NETDATA_CONTROLLER_APPS_ENABLED;
__u32 key = 0;
__u32 tgid = 0;

libnetdata_update_global(&tbl_total_stats, NETDATA_KEY_CALLS_PROCESS, 1);

Expand All @@ -190,7 +195,7 @@ int netdata_tracepoint_sched_process_fork(struct netdata_sched_process_fork *ptr
if (netdata_process_not_update_apps())
return 0;

fill = netdata_get_pid_structure(&key, &process_ctrl, &tbl_pid_stats);
fill = netdata_get_pid_structure(&key, &tgid, &process_ctrl, &tbl_pid_stats);
if (fill) {
fill->release_call = 0;
libnetdata_update_u32(&fill->create_process, 1);
Expand Down
6 changes: 4 additions & 2 deletions src/shm.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ static __always_inline int netdata_update_apps(__u32 idx)
{
netdata_shm_t data = {};

__u32 key;
netdata_shm_t *fill = netdata_get_pid_structure(&key, &shm_ctrl, &tbl_pid_shm);
__u32 key = 0;
__u32 tgid = 0;
netdata_shm_t *fill = netdata_get_pid_structure(&key, &tgid, &shm_ctrl, &tbl_pid_shm);
if (fill) {
netdata_update_stored_data(fill, idx);
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);

netdata_set_structure_value(&data, idx);
Expand Down
3 changes: 2 additions & 1 deletion src/socket.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ static __always_inline short unsigned int set_idx_value(netdata_socket_idx_t *ns
if (nsi->dport == 0)
return AF_UNSPEC;

nsi->pid = netdata_get_pid(&socket_ctrl);
__u32 tgid = 0;
nsi->pid = netdata_get_pid(&socket_ctrl, &tgid);

return family;
}
Expand Down
8 changes: 6 additions & 2 deletions src/swap.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ static __always_inline int common_readpage()
libnetdata_update_global(&tbl_swap, NETDATA_KEY_SWAP_READPAGE_CALL, 1);

__u32 key = 0;
__u32 tgid = 0;
if (netdata_swap_not_update_apps())
return 0;

netdata_swap_access_t *fill = netdata_get_pid_structure(&key, &swap_ctrl, &tbl_pid_swap);
netdata_swap_access_t *fill = netdata_get_pid_structure(&key, &tgid, &swap_ctrl, &tbl_pid_swap);
if (fill) {
libnetdata_update_u64(&fill->read, 1);
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);
data.read = 1;

Expand All @@ -81,14 +83,16 @@ static __always_inline int common_writepage()
libnetdata_update_global(&tbl_swap, NETDATA_KEY_SWAP_WRITEPAGE_CALL, 1);

__u32 key = 0;
__u32 tgid = 0;
if (netdata_swap_not_update_apps())
return 0;

netdata_swap_access_t *fill = netdata_get_pid_structure(&key, &swap_ctrl, &tbl_pid_swap);
netdata_swap_access_t *fill = netdata_get_pid_structure(&key, &tgid, &swap_ctrl, &tbl_pid_swap);
if (fill) {
libnetdata_update_u64(&fill->write, 1);
} else {
data.ct = bpf_ktime_get_ns();
libnetdata_update_uid_gid(&data.uid, &data.gid);
bpf_get_current_comm(&data.name, TASK_COMM_LEN);
data.write = 1;

Expand Down
Loading

0 comments on commit b0be1dc

Please sign in to comment.