From f33ba88d8dc2229c4786f9e61ee4bb0f5fcb2f35 Mon Sep 17 00:00:00 2001 From: LiuLingze Date: Sun, 9 Jun 2024 03:41:11 +0000 Subject: [PATCH] add version ctrl Signed-off-by: LiuLingze --- .../Stack_Analyser/include/sa_ebpf.h | 11 ++++++ .../src/bpf_wapper/eBPFStackCollector.cpp | 37 +++++++++++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/eBPF_Supermarket/Stack_Analyser/include/sa_ebpf.h b/eBPF_Supermarket/Stack_Analyser/include/sa_ebpf.h index 44ba9e125..56ea5549d 100644 --- a/eBPF_Supermarket/Stack_Analyser/include/sa_ebpf.h +++ b/eBPF_Supermarket/Stack_Analyser/include/sa_ebpf.h @@ -20,6 +20,7 @@ #define STACK_ANALYZER_EBPF #include "sa_common.h" +#include #define PF_KTHREAD 0x00200000 @@ -79,6 +80,9 @@ #define TS bpf_ktime_get_ns() +#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0) +#define CHECK_FREQ(_ts) +#else /* 内置函数: bool __atomic_compare_exchange_n ( @@ -113,6 +117,7 @@ __ATOMIC_RELAXED, __ATOMIC_RELAXED)) \ return 0; \ } +#endif #define GET_CURR \ (struct task_struct *)bpf_get_current_task() @@ -129,9 +134,15 @@ #define GET_KNODE(_task) \ BPF_CORE_READ(_task, cgroups, dfl_cgrp, kn) +#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0) +#define CHECK_CGID(_knode) \ + if (target_cgroupid > 0 && BPF_CORE_READ(_knode, id.id) != target_cgroupid) \ + return 0; +#else #define CHECK_CGID(_knode) \ if (target_cgroupid > 0 && BPF_CORE_READ(_knode, id) != target_cgroupid) \ return 0; +#endif #define TRY_SAVE_INFO(_task, _pid, _tgid, _knode) \ if (!bpf_map_lookup_elem(&pid_info_map, &_pid)) \ diff --git a/eBPF_Supermarket/Stack_Analyser/src/bpf_wapper/eBPFStackCollector.cpp b/eBPF_Supermarket/Stack_Analyser/src/bpf_wapper/eBPFStackCollector.cpp index ee599ec51..d3aa81547 100644 --- a/eBPF_Supermarket/Stack_Analyser/src/bpf_wapper/eBPFStackCollector.cpp +++ b/eBPF_Supermarket/Stack_Analyser/src/bpf_wapper/eBPFStackCollector.cpp @@ -24,6 +24,7 @@ #include #include #include +#include std::string getLocalDateTime(void) { @@ -53,25 +54,44 @@ std::vector *StackCollector::sortedCountList(void) auto val_size = bpf_map__value_size(psid_count_map); auto value_fd = bpf_object__find_map_fd_by_name(obj, "psid_count_map"); + auto D = new std::vector(); +#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0) + for (psid prev_key = {0}, curr_key = {0};; prev_key = curr_key) + { + if (bpf_map_get_next_key(value_fd, &prev_key, &curr_key)) + { + if (errno != ENOENT) + perror("map get next key error"); + break; // no more keys, done + } + if (showDelta) + bpf_map_delete_elem(value_fd, &prev_key); + char val[val_size]; + memset(val, 0, val_size); + if (bpf_map_lookup_elem(value_fd, &curr_key, &val)) + { + if (errno != ENOENT) + { + perror("map lookup error"); + break; + } + continue; + } + CountItem d(curr_key, count_values(val)); + D->insert(std::lower_bound(D->begin(), D->end(), d), d); + } +#else auto keys = new psid[MAX_ENTRIES]; auto vals = new char[MAX_ENTRIES * val_size]; uint32_t count = MAX_ENTRIES; psid next_key; int err; if (showDelta) - { err = bpf_map_lookup_and_delete_batch(value_fd, NULL, &next_key, keys, vals, &count, NULL); - } else - { err = bpf_map_lookup_batch(value_fd, NULL, &next_key, keys, vals, &count, NULL); - } if (err == EFAULT) - { return NULL; - } - - auto D = new std::vector(); for (uint32_t i = 0; i < count; i++) { CountItem d(keys[i], count_values(vals + val_size * i)); @@ -79,6 +99,7 @@ std::vector *StackCollector::sortedCountList(void) } delete[] keys; delete[] vals; +#endif return D; };