Skip to content

Commit

Permalink
Fix syscount error
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Nov 19, 2023
1 parent e10fb4f commit 706e03d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 47 deletions.
34 changes: 4 additions & 30 deletions example/libbpf-tools/syscount/syscount.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ const volatile bool filter_failed = false;
const volatile int filter_errno = false;
const volatile pid_t filter_pid = 0;

struct {
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
__type(key, u32);
__type(value, u32);
__uint(max_entries, 1);
} cgroup_map SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, MAX_ENTRIES);
Expand All @@ -37,18 +30,6 @@ struct {
__type(value, struct data_t);
} data SEC(".maps");

static __always_inline
void save_proc_name(struct data_t *val)
{
struct task_struct *current = (void *)bpf_get_current_task();

/* We should save the process name every time because it can be
* changed (e.g., by exec). This can be optimized later by managing
* this field with the help of tp/sched/sched_process_exec and
* raw_tp/task_rename. */
BPF_CORE_READ_STR_INTO(&val->comm, current, group_leader, comm);
}

SEC("tracepoint/raw_syscalls/sys_enter")
int sys_enter(struct trace_event_raw_sys_enter *args)
{
Expand All @@ -57,9 +38,6 @@ int sys_enter(struct trace_event_raw_sys_enter *args)
u32 tid = id;
u64 ts;

if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;

if (filter_pid && pid != filter_pid)
return 0;

Expand All @@ -71,9 +49,6 @@ int sys_enter(struct trace_event_raw_sys_enter *args)
SEC("tracepoint/raw_syscalls/sys_exit")
int sys_exit(struct trace_event_raw_sys_exit *args)
{
if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;

u64 id = bpf_get_current_pid_tgid();
static const struct data_t zero;
pid_t pid = id >> 32;
Expand Down Expand Up @@ -103,11 +78,10 @@ int sys_exit(struct trace_event_raw_sys_exit *args)
key = (count_by_process) ? pid : args->id;
val = bpf_map_lookup_or_try_init(&data, &key, &zero);
if (val) {
__sync_fetch_and_add(&val->count, 1);
if (count_by_process)
save_proc_name(val);
if (measure_latency)
__sync_fetch_and_add(&val->total_ns, lat);
val->count = val->count + 1;
if (measure_latency) {
val->total_ns = val->total_ns + lat;
}
}
return 0;
}
Expand Down
17 changes: 0 additions & 17 deletions example/libbpf-tools/syscount/syscount.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,6 @@ int main(int argc, char **argv)
int seconds = 0;
__u32 count;
int err;
int idx, cg_map_fd;
int cgfd = -1;

init_syscall_names();

Expand Down Expand Up @@ -430,21 +428,6 @@ int main(int argc, char **argv)
goto cleanup_obj;
}

/* update cgroup path fd to map */
if (env.cg) {
idx = 0;
cg_map_fd = bpf_map__fd(obj->maps.cgroup_map);
cgfd = open(env.cgroupspath, O_RDONLY);
if (cgfd < 0) {
fprintf(stderr, "Failed opening Cgroup path: %s", env.cgroupspath);
goto cleanup_obj;
}
if (bpf_map_update_elem(cg_map_fd, &idx, &cgfd, BPF_ANY)) {
fprintf(stderr, "Failed adding target cgroup to map");
goto cleanup_obj;
}
}

obj->links.sys_exit = bpf_program__attach(obj->progs.sys_exit);
if (!obj->links.sys_exit) {
err = -errno;
Expand Down

0 comments on commit 706e03d

Please sign in to comment.