diff options
Diffstat (limited to 'collectors/ebpf.plugin/ebpf_shm.c')
-rw-r--r-- | collectors/ebpf.plugin/ebpf_shm.c | 74 |
1 files changed, 56 insertions, 18 deletions
diff --git a/collectors/ebpf.plugin/ebpf_shm.c b/collectors/ebpf.plugin/ebpf_shm.c index f81c01964..093d65b60 100644 --- a/collectors/ebpf.plugin/ebpf_shm.c +++ b/collectors/ebpf.plugin/ebpf_shm.c @@ -21,15 +21,27 @@ struct config shm_config = { .first_section = NULL, static ebpf_local_maps_t shm_maps[] = {{.name = "tbl_pid_shm", .internal_input = ND_EBPF_DEFAULT_PID_SIZE, .user_input = 0, .type = NETDATA_EBPF_MAP_RESIZABLE | NETDATA_EBPF_MAP_PID, - .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED}, + .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED, +#ifdef LIBBPF_MAJOR_VERSION + .map_type = BPF_MAP_TYPE_PERCPU_HASH +#endif + }, {.name = "shm_ctrl", .internal_input = NETDATA_CONTROLLER_END, .user_input = 0, .type = NETDATA_EBPF_MAP_CONTROLLER, - .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED}, + .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED, +#ifdef LIBBPF_MAJOR_VERSION + .map_type = BPF_MAP_TYPE_PERCPU_ARRAY +#endif + }, {.name = "tbl_shm", .internal_input = NETDATA_SHM_END, .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC, - .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED}, + .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED, +#ifdef LIBBPF_MAJOR_VERSION + .map_type = BPF_MAP_TYPE_PERCPU_ARRAY +#endif + }, {.name = NULL, .internal_input = 0, .user_input = 0}}; netdata_ebpf_targets_t shm_targets[] = { {.name = "shmget", .mode = EBPF_LOAD_TRAMPOLINE}, @@ -215,10 +227,14 @@ static void ebpf_shm_disable_release_task(struct shm_bpf *obj) * @param obj is the main structure for bpf objects. * @param em structure with configuration */ -static void ebpf_shm_adjust_map_size(struct shm_bpf *obj, ebpf_module_t *em) +static void ebpf_shm_adjust_map(struct shm_bpf *obj, ebpf_module_t *em) { ebpf_update_map_size(obj->maps.tbl_pid_shm, &shm_maps[NETDATA_PID_SHM_TABLE], em, bpf_map__name(obj->maps.tbl_pid_shm)); + + ebpf_update_map_type(obj->maps.tbl_shm, &shm_maps[NETDATA_SHM_GLOBAL_TABLE]); + ebpf_update_map_type(obj->maps.tbl_pid_shm, &shm_maps[NETDATA_PID_SHM_TABLE]); + ebpf_update_map_type(obj->maps.shm_ctrl, &shm_maps[NETDATA_SHM_CONTROLLER]); } /** @@ -250,7 +266,7 @@ static inline int ebpf_shm_load_and_attach(struct shm_bpf *obj, ebpf_module_t *e ebpf_disable_trampoline(obj); } - ebpf_shm_adjust_map_size(obj, em); + ebpf_shm_adjust_map(obj, em); if (!em->apps_charts && !em->cgroup_charts) ebpf_shm_disable_release_task(obj); @@ -312,10 +328,11 @@ static void ebpf_shm_exit(void *ptr) * Sum all values read from kernel and store in the first address. * * @param out the vector with read values. + * @param maps_per_core do I need to read all cores? */ -static void shm_apps_accumulator(netdata_publish_shm_t *out) +static void shm_apps_accumulator(netdata_publish_shm_t *out, int maps_per_core) { - int i, end = (running_on_kernel >= NETDATA_KERNEL_V4_15) ? ebpf_nprocs : 1; + int i, end = (maps_per_core) ? ebpf_nprocs : 1; netdata_publish_shm_t *total = &out[0]; for (i = 1; i < end; i++) { netdata_publish_shm_t *w = &out[i]; @@ -349,12 +366,17 @@ static void shm_fill_pid(uint32_t current_pid, netdata_publish_shm_t *publish) * Update cgroup * * Update cgroup data based in + * + * @param maps_per_core do I need to read all cores? */ -static void ebpf_update_shm_cgroup() +static void ebpf_update_shm_cgroup(int maps_per_core) { netdata_publish_shm_t *cv = shm_vector; int fd = shm_maps[NETDATA_PID_SHM_TABLE].map_fd; - size_t length = sizeof(netdata_publish_shm_t) * ebpf_nprocs; + size_t length = sizeof(netdata_publish_shm_t); + if (maps_per_core) + length *= ebpf_nprocs; + ebpf_cgroup_target_t *ect; memset(cv, 0, length); @@ -371,7 +393,7 @@ static void ebpf_update_shm_cgroup() memcpy(out, in, sizeof(netdata_publish_shm_t)); } else { if (!bpf_map_lookup_elem(fd, &pid, cv)) { - shm_apps_accumulator(cv); + shm_apps_accumulator(cv, maps_per_core); memcpy(out, cv, sizeof(netdata_publish_shm_t)); @@ -389,14 +411,19 @@ static void ebpf_update_shm_cgroup() * Read APPS table * * Read the apps table and store data inside the structure. + * + * @param maps_per_core do I need to read all cores? */ -static void read_apps_table() +static void read_shm_apps_table(int maps_per_core) { netdata_publish_shm_t *cv = shm_vector; uint32_t key; struct ebpf_pid_stat *pids = ebpf_root_of_pids; int fd = shm_maps[NETDATA_PID_SHM_TABLE].map_fd; - size_t length = sizeof(netdata_publish_shm_t)*ebpf_nprocs; + size_t length = sizeof(netdata_publish_shm_t); + if (maps_per_core) + length *= ebpf_nprocs; + while (pids) { key = pids->pid; @@ -405,7 +432,7 @@ static void read_apps_table() continue; } - shm_apps_accumulator(cv); + shm_apps_accumulator(cv, maps_per_core); shm_fill_pid(key, cv); @@ -446,23 +473,29 @@ static void shm_send_global() * Read global counter * * Read the table with number of calls for all functions + * + * @param maps_per_core do I need to read all cores? */ -static void ebpf_shm_read_global_table() +static void ebpf_shm_read_global_table(int maps_per_core) { netdata_idx_t *stored = shm_values; netdata_idx_t *val = shm_hash_values; int fd = shm_maps[NETDATA_SHM_GLOBAL_TABLE].map_fd; + size_t length = sizeof(netdata_idx_t); + if (maps_per_core) + length *= ebpf_nprocs; uint32_t i, end = NETDATA_SHM_END; for (i = NETDATA_KEY_SHMGET_CALL; i < end; i++) { if (!bpf_map_lookup_elem(fd, &i, stored)) { int j; - int last = ebpf_nprocs; + int last = (maps_per_core) ? ebpf_nprocs : 1; netdata_idx_t total = 0; for (j = 0; j < last; j++) total += stored[j]; val[i] = total; + memset(stored, 0 , length); } } } @@ -831,6 +864,7 @@ static void shm_collector(ebpf_module_t *em) heartbeat_t hb; heartbeat_init(&hb); int counter = update_every - 1; + int maps_per_core = em->maps_per_core; while (!ebpf_exit_plugin) { (void)heartbeat_next(&hb, USEC_PER_SEC); if (ebpf_exit_plugin || ++counter != update_every) @@ -838,14 +872,14 @@ static void shm_collector(ebpf_module_t *em) counter = 0; netdata_apps_integration_flags_t apps = em->apps_charts; - ebpf_shm_read_global_table(); + ebpf_shm_read_global_table(maps_per_core); pthread_mutex_lock(&collect_data_mutex); if (apps) { - read_apps_table(); + read_shm_apps_table(maps_per_core); } if (cgroups) { - ebpf_update_shm_cgroup(); + ebpf_update_shm_cgroup(maps_per_core); } pthread_mutex_lock(&lock); @@ -984,6 +1018,10 @@ static void ebpf_create_shm_charts(int update_every) */ static int ebpf_shm_load_bpf(ebpf_module_t *em) { +#ifdef LIBBPF_MAJOR_VERSION + ebpf_define_map_type(em->maps, em->maps_per_core, running_on_kernel); +#endif + int ret = 0; ebpf_adjust_apps_cgroup(em, em->targets[NETDATA_KEY_SHMGET_CALL].mode); |