Skip to content

Commit

Permalink
Synchronization (#158)
Browse files Browse the repository at this point in the history
Fix synchronization between threads when summing values.
  • Loading branch information
thiagoftsm authored Jun 8, 2020
1 parent 979844d commit 69fbd0a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
42 changes: 24 additions & 18 deletions kernel/network_viewer_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,23 @@ struct bpf_map_def SEC("maps") tbl_sock_total_stats = {
/**
* Function used to update 64 bit values and avoid overflow
*/
static void netdata_update_u64(u64 *res, u64 value)
static void netdata_update_u64(__u64 *res, __u64 value)
{
if ( (0xFFFFFFFFFFFFFFFF - *res) <= value)
if (!value)
return;

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

/*
* Update hash table tbl_sock_total_stats
*/
static void netdata_update_global(__u32 key, __u64 value)
{
u64 *res;
__u64 *res;
res = bpf_map_lookup_elem(&tbl_sock_total_stats, &key);
if (res) {
netdata_update_u64(res, value) ;
Expand Down Expand Up @@ -307,18 +310,18 @@ int netdata_tcp_sendmsg(struct pt_regs* ctx)
__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = (__u32)(pid_tgid >> 32);
__u32 tgid = (__u32)( 0x00000000FFFFFFFF & pid_tgid);
size_t sent = (size_t)PT_REGS_PARM3(ctx);
__u64 sent = (__u64)PT_REGS_PARM3(ctx);
struct inet_sock *is = inet_sk((struct sock *)PT_REGS_PARM1(ctx));

netdata_update_global(NETDATA_KEY_CALLS_TCP_SENDMSG, 1);

family = set_idx_value(&idx, is, pid);
tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats;

netdata_update_global(NETDATA_KEY_BYTES_TCP_SENDMSG, (__u64)sent);
update_socket_table(tbl, &idx, (__u64) sent, 0, pid_tgid, 6);
update_socket_table(tbl, &idx, sent, 0, pid_tgid, 6);

update_pid_stats(pid, tgid, (__u64) sent, 0);
netdata_update_global(NETDATA_KEY_BYTES_TCP_SENDMSG, sent);
update_pid_stats(pid, tgid, sent, 0);

#if NETDATASEL < 2
if (ret < 0) {
Expand Down Expand Up @@ -351,13 +354,14 @@ int netdata_tcp_cleanup_rbuf(struct pt_regs* ctx)
return 0;
}

__u64 received = (__u64) PT_REGS_PARM2(ctx);

family = set_idx_value(&idx, is, pid);
tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats;

netdata_update_global(NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF, (__u64) copied);
update_socket_table(tbl, &idx, 0, (__u64) copied, pid_tgid, 6);

update_pid_stats(pid, tgid, 0, (__u64) copied);
netdata_update_global(NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF, received);
update_socket_table(tbl, &idx, 0, received, pid_tgid, 6);
update_pid_stats(pid, tgid, 0, received);

return 0;
}
Expand All @@ -377,7 +381,7 @@ int netdata_tcp_close(struct pt_regs* ctx)
__u32 pid = (__u32)(pid_tgid >> 32);
struct inet_sock *is = inet_sk((struct sock *)PT_REGS_PARM1(ctx));

netdata_update_global(NETDATA_KEY_CALLS_TCP_CLOSE, 1);
netdata_update_global(NETDATA_KEY_CALLS_TCP_CLOSE, 1);

family = set_idx_value(&idx, is, pid);
tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats;
Expand Down Expand Up @@ -445,15 +449,17 @@ int trace_udp_ret_recvmsg(struct pt_regs* ctx)
return 0;
}

__u64 received = (__u64) PT_REGS_RC(ctx);

bpf_map_delete_elem(&tbl_nv_udp_conn_stats, &pid_tgid);

family = set_idx_value(&idx, is, pid);
tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats;

netdata_update_global(NETDATA_KEY_BYTES_UDP_RECVMSG, (__u64) copied);
update_socket_table(tbl, &idx, 0, (__u64) copied, pid_tgid, 17);
netdata_update_global(NETDATA_KEY_BYTES_UDP_RECVMSG, received);
update_socket_table(tbl, &idx, 0, received, pid_tgid, 17);

update_pid_stats(pid, tgid, 0, (__u64) copied);
update_pid_stats(pid, tgid, 0, received);

return 0;
}
Expand Down Expand Up @@ -487,9 +493,9 @@ int trace_udp_sendmsg(struct pt_regs* ctx)
tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats;

update_socket_table(tbl, &idx, (__u64) sent, 0, pid_tgid, 17);
update_pid_stats(pid, tgid, (__u64) sent, 0);

netdata_update_global(NETDATA_KEY_BYTES_UDP_SENDMSG, (__u64) sent);
update_pid_stats(pid, tgid, (__u64) sent, 0);

#if NETDATASEL < 2
if (ret < 0) {
Expand Down
34 changes: 20 additions & 14 deletions kernel/process_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,29 @@ static unsigned int log2l(unsigned long v)

static void netdata_update_u32(u32 *res, u32 value)
{
if ( (0xFFFFFFFF - *res) <= value)
if (!value)
return;

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

static void netdata_update_u64(u64 *res, u64 value)
static void netdata_update_u64(__u64 *res, __u64 value)
{
if ( (0x7FFFFFFFFFFFFFFF - *res) <= value)
if (!value)
return;

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

static void netdata_update_global(__u32 key, __u64 value)
{
u64 *res;
__u64 *res;
res = bpf_map_lookup_elem(&tbl_total_stats, &key);
if (res) {
netdata_update_u64(res, value) ;
Expand Down Expand Up @@ -181,7 +187,7 @@ int netdata_sys_write(struct pt_regs* ctx)
tot = 0;
#endif
netdata_update_global(NETDATA_KEY_BYTES_VFS_WRITE, tot);
netdata_update_u64(&fill->write_bytes, (u64) tot);
netdata_update_u64(&fill->write_bytes, tot);
#if NETDATASEL < 2
}
#endif
Expand All @@ -199,7 +205,7 @@ int netdata_sys_write(struct pt_regs* ctx)
tot = 0;
#endif
netdata_update_global(NETDATA_KEY_BYTES_VFS_WRITE, tot);
data.write_bytes = (u64)tot;
data.write_bytes = tot;
#if NETDATASEL < 2
}
#endif
Expand Down Expand Up @@ -249,7 +255,7 @@ int netdata_sys_writev(struct pt_regs* ctx)
tot = 0;
#endif
netdata_update_global(NETDATA_KEY_BYTES_VFS_WRITEV, tot);
netdata_update_u64(&fill->writev_bytes, (u64) tot);
netdata_update_u64(&fill->writev_bytes, tot);
#if NETDATASEL < 2
}
#endif
Expand Down Expand Up @@ -317,7 +323,7 @@ int netdata_sys_read(struct pt_regs* ctx)
tot = 0;
#endif
netdata_update_global(NETDATA_KEY_BYTES_VFS_READ, tot);
netdata_update_u64(&fill->read_bytes, (u64) tot);
netdata_update_u64(&fill->read_bytes, tot);
#if NETDATASEL < 2
}
#endif
Expand Down Expand Up @@ -385,7 +391,7 @@ int netdata_sys_readv(struct pt_regs* ctx)
tot = 0;
#endif
netdata_update_global(NETDATA_KEY_BYTES_VFS_READV, tot);
netdata_update_u64(&fill->readv_bytes, (u64) tot);
netdata_update_u64(&fill->readv_bytes, tot);
#if NETDATASEL < 2
}
#endif
Expand Down Expand Up @@ -701,7 +707,7 @@ int netdata_clone(struct pt_regs* ctx)
__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = (__u32)(pid_tgid >> 32);
__u32 tgid = (__u32)( 0x00000000FFFFFFFF & pid_tgid);
u64 arg1 = (u64)PT_REGS_PARM2(ctx);
__u64 arg1 = (__u64)PT_REGS_PARM2(ctx);

arg1 &= CLONE_THREAD|CLONE_VM;
if(!arg1)
Expand Down

0 comments on commit 69fbd0a

Please sign in to comment.