Skip to content

Commit

Permalink
Common library (#204)
Browse files Browse the repository at this point in the history
Move common code to header creating a standard library.
  • Loading branch information
thiagoftsm authored Mar 9, 2021
1 parent c4f568c commit 7ec1e7f
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 181 deletions.
9 changes: 1 addition & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ _LIBC ?= glibc
EXTRA_CFLAGS += -fno-stack-protector

all: $(KERNEL_PROGRAM)
cp $(KERNEL_DIR)rprocess_kern.o rnetdata_ebpf_process.$(VER_MAJOR).$(VER_MINOR).o
cp $(KERNEL_DIR)pprocess_kern.o pnetdata_ebpf_process.$(VER_MAJOR).$(VER_MINOR).o
cp $(KERNEL_DIR)rnetwork_viewer_kern.o rnetdata_ebpf_socket.$(VER_MAJOR).$(VER_MINOR).o
cp $(KERNEL_DIR)pnetwork_viewer_kern.o pnetdata_ebpf_socket.$(VER_MAJOR).$(VER_MINOR).o
cp $(KERNEL_DIR)rcachestat_kern.o rnetdata_ebpf_cachestat.$(VER_MAJOR).$(VER_MINOR).o
cp $(KERNEL_DIR)pcachestat_kern.o pnetdata_ebpf_cachestat.$(VER_MAJOR).$(VER_MINOR).o
cp $(KERNEL_DIR)rsync_kern.o rnetdata_ebpf_sync.$(VER_MAJOR).$(VER_MINOR).o
cp $(KERNEL_DIR)psync_kern.o pnetdata_ebpf_sync.$(VER_MAJOR).$(VER_MINOR).o
sh rename_binaries.sh "$(VER_MAJOR)" "$(VER_MINOR)"
if [ -f pnetdata_ebpf_process.$(VER_MAJOR).$(VER_MINOR).o ]; then tar -cf artifacts/netdata_ebpf-$(FIRST_KERNEL_VERSION)_$(VER_MAJOR).$(VER_MINOR)-$(_LIBC).tar [pr]netdata_ebpf_*.$(VER_MAJOR).$(VER_MINOR).o; else echo "ERROR: Cannot find BPF programs"; exit 1; fi
if [ "$${DEBUG:-0}" -eq 1 ]; then tar -uvf artifacts/netdata_ebpf-$(FIRST_KERNEL_VERSION)_$(VER_MAJOR).$(VER_MINOR)-$(_LIBC).tar tools/check-kernel-config.sh; fi
xz artifacts/netdata_ebpf-$(FIRST_KERNEL_VERSION)_$(VER_MAJOR).$(VER_MINOR)-$(_LIBC).tar
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions includes/netdata_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum cachestat_counters {
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
};

Expand Down
31 changes: 31 additions & 0 deletions includes/netdata_ebpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ struct netdata_error_report_t {
int err;
};

// Use __always_inline instead inline to keep compatiblity with old kernels
// https://docs.cilium.io/en/v1.8/bpf/
// The condition to test kernel was added, because __always_inline broke the epbf.plugin
// on CentOS 7 and Ubuntu 18.04 (kernel 4.18)
#if (LINUX_VERSION_CODE > KERNEL_VERSION(4,19,0))
static __always_inline void libnetdata_update_u64(__u64 *res, __u64 value)
#else
static inline void libnetdata_update_u64(__u64 *res, __u64 value)
#endif
{
__sync_fetch_and_add(res, value);
if ( (0xFFFFFFFFFFFFFFFF - *res) <= value) {
*res = value;
}
}

#if (LINUX_VERSION_CODE > KERNEL_VERSION(4,19,0))
static __always_inline void libnetdata_update_global(struct bpf_map_def *tbl,__u32 key, __u64 value)
#else
static inline void libnetdata_update_global(struct bpf_map_def *tbl,__u32 key, __u64 value)
#endif
{
__u64 *res;
res = bpf_map_lookup_elem(tbl, &key);
if (res)
libnetdata_update_u64(res, value) ;
else
bpf_map_update_elem(tbl, &key, &value, BPF_NOEXIST);
}


// Copied from linux/samples/bpf/tracex1_kern.c
#define _(P) \
({ \
Expand Down
1 change: 1 addition & 0 deletions includes/netdata_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum socket_counters {

NETDATA_KEY_TCP_RETRANSMIT,

// Keep this as last and don't skip numbers as it is used as element counter
NETDATA_SOCKET_COUNTER
};

Expand Down
1 change: 1 addition & 0 deletions includes/netdata_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ enum process_counters {
NETDATA_KEY_ERROR_VFS_READV,
NETDATA_KEY_BYTES_VFS_READV,

// Keep this as last and don't skip numbers as it is used as element counter
NETDATA_GLOBAL_COUNTER
};

Expand Down
1 change: 0 additions & 1 deletion includes/netdata_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

enum sync_counters {
NETDATA_KEY_SYNC_CALL,
NETDATA_KEY_SYNC_ERROR,

// Keep this as last and don't skip numbers as it is used as element counter
NETDATA_SYNC_END
Expand Down
6 changes: 3 additions & 3 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ all: process_kern.o network_viewer_kern.o cachestat_kern.o sync_kern.o
-Wno-tautological-compare \
-DNETDATASEL=0 \
-D__BPF_TRACING__ \
-include netdata_asm_goto.h \
-include ../includes/netdata_asm_goto.h \
-O2 -emit-llvm -c $<
$(LLC) -march=bpf -filetype=obj -o r$@ $(<:.c=.ll)
$(CLANG) $(EXTRA_CFLAGS) -S -nostdinc $(LINUXINCLUDE) $(LLVM_INCLUDES) \
Expand All @@ -58,7 +58,7 @@ all: process_kern.o network_viewer_kern.o cachestat_kern.o sync_kern.o
-Wno-tautological-compare \
-DNETDATASEL=1 \
-D__BPF_TRACING__ \
-include netdata_asm_goto.h \
-include ../includes/netdata_asm_goto.h \
-O2 -emit-llvm -c $<
$(LLC) -march=bpf -filetype=obj -o d$@ $(<:.c=.ll)
$(CLANG) $(EXTRA_CFLAGS) -S -nostdinc $(LINUXINCLUDE) $(LLVM_INCLUDES) \
Expand All @@ -68,7 +68,7 @@ all: process_kern.o network_viewer_kern.o cachestat_kern.o sync_kern.o
-Wno-tautological-compare \
-DNETDATASEL=2 \
-D__BPF_TRACING__ \
-include netdata_asm_goto.h \
-include ../includes/netdata_asm_goto.h \
-O2 -emit-llvm -c $<
$(LLC) -march=bpf -filetype=obj -o p$@ $(<:.c=.ll)

Expand Down
40 changes: 8 additions & 32 deletions kernel/cachestat_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ struct bpf_map_def SEC("maps") cstat_pid = {
.max_entries = 100000
};

/************************************************************************************
*
* COMMON Section
*
***********************************************************************************/

static inline void netdata_update_u64(__u64 *res, __u64 value)
{
__sync_fetch_and_add(res, value);
if ( (0xFFFFFFFFFFFFFFFF - *res) <= value) {
*res = value;
}
}

static inline void netdata_update_global(__u32 key, __u64 value)
{
__u64 *res;
res = bpf_map_lookup_elem(&cstat_global, &key);
if (res)
netdata_update_u64(res, value) ;
else
bpf_map_update_elem(&cstat_global, &key, &value, BPF_NOEXIST);
}

/************************************************************************************
*
* Probe Section
Expand All @@ -61,13 +37,13 @@ SEC("kprobe/add_to_page_cache_lru")
int netdata_add_to_page_cache_lru(struct pt_regs* ctx)
{
netdata_cachestat_t *fill, data = {};
netdata_update_global(NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU, 1);
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU, 1);

__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = (__u32)(pid_tgid >> 32);
fill = bpf_map_lookup_elem(&cstat_pid ,&pid);
if (fill) {
netdata_update_u64(&fill->add_to_page_cache_lru, 1);
libnetdata_update_u64(&fill->add_to_page_cache_lru, 1);
} else {
data.add_to_page_cache_lru = 1;
bpf_map_update_elem(&cstat_pid, &pid, &data, BPF_ANY);
Expand All @@ -80,13 +56,13 @@ SEC("kprobe/mark_page_accessed")
int netdata_mark_page_accessed(struct pt_regs* ctx)
{
netdata_cachestat_t *fill, data = {};
netdata_update_global(NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED, 1);
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED, 1);

__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = (__u32)(pid_tgid >> 32);
fill = bpf_map_lookup_elem(&cstat_pid ,&pid);
if (fill) {
netdata_update_u64(&fill->mark_page_accessed, 1);
libnetdata_update_u64(&fill->mark_page_accessed, 1);
} else {
data.mark_page_accessed = 1;
bpf_map_update_elem(&cstat_pid, &pid, &data, BPF_ANY);
Expand All @@ -99,13 +75,13 @@ SEC("kprobe/account_page_dirtied")
int netdata_account_page_dirtied(struct pt_regs* ctx)
{
netdata_cachestat_t *fill, data = {};
netdata_update_global(NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED, 1);
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED, 1);

__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = (__u32)(pid_tgid >> 32);
fill = bpf_map_lookup_elem(&cstat_pid ,&pid);
if (fill) {
netdata_update_u64(&fill->account_page_dirtied, 1);
libnetdata_update_u64(&fill->account_page_dirtied, 1);
} else {
data.account_page_dirtied = 1;
bpf_map_update_elem(&cstat_pid, &pid, &data, BPF_ANY);
Expand All @@ -118,13 +94,13 @@ SEC("kprobe/mark_buffer_dirty")
int netdata_mark_buffer_dirty(struct pt_regs* ctx)
{
netdata_cachestat_t *fill, data = {};
netdata_update_global(NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY, 1);
libnetdata_update_global(&cstat_global, NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY, 1);

__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = (__u32)(pid_tgid >> 32);
fill = bpf_map_lookup_elem(&cstat_pid ,&pid);
if (fill) {
netdata_update_u64(&fill->mark_buffer_dirty, 1);
libnetdata_update_u64(&fill->mark_buffer_dirty, 1);
} else {
data.mark_buffer_dirty = 1;
bpf_map_update_elem(&cstat_pid, &pid, &data, BPF_ANY);
Expand Down
15 changes: 15 additions & 0 deletions kernel/network_viewer_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,19 @@ struct bpf_map_def SEC("maps") tbl_used_ports = {
/**
* Function used to update 64 bit values and avoid overflow
*/
#if (LINUX_VERSION_CODE > KERNEL_VERSION(4,19,0))
static __always_inline void netdata_update_u64(__u64 *res, __u64 value)
#else
static inline void netdata_update_u64(__u64 *res, __u64 value)
#endif
{
if (!value)
return;

__sync_fetch_and_add(res, value);
if ( (0xFFFFFFFFFFFFFFFF - *res) <= value) {
*res = value;
}
}

/*
Expand All @@ -191,7 +198,11 @@ static void netdata_update_global(__u32 key, __u64 value)
*
* Read information from socket to update the index.
*/
#if (LINUX_VERSION_CODE > KERNEL_VERSION(4,19,0))
static __always_inline __u16 set_idx_value(netdata_socket_idx_t *nsi, struct inet_sock *is)
#else
static inline __u16 set_idx_value(netdata_socket_idx_t *nsi, struct inet_sock *is)
#endif
{
__u16 family;

Expand Down Expand Up @@ -233,7 +244,11 @@ static inline __u16 set_idx_value(netdata_socket_idx_t *nsi, struct inet_sock *i
/**
* Update time and bytes sent and received
*/
#if (LINUX_VERSION_CODE > KERNEL_VERSION(4,19,0))
static __always_inline void update_socket_stats(netdata_socket_t *ptr, __u64 sent, __u64 received, __u16 retransmitted)
#else
static inline void update_socket_stats(netdata_socket_t *ptr, __u64 sent, __u64 received, __u16 retransmitted)
#endif
{
ptr->ct = bpf_ktime_get_ns();

Expand Down
Loading

0 comments on commit 7ec1e7f

Please sign in to comment.