Skip to content

Commit

Permalink
add version ctrl
Browse files Browse the repository at this point in the history
Signed-off-by: LiuLingze <[email protected]>
  • Loading branch information
GorilaMond committed Jun 9, 2024
1 parent 1f5a5ab commit f33ba88
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
11 changes: 11 additions & 0 deletions eBPF_Supermarket/Stack_Analyser/include/sa_ebpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define STACK_ANALYZER_EBPF

#include "sa_common.h"
#include <linux/version.h>

#define PF_KTHREAD 0x00200000

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -113,6 +117,7 @@
__ATOMIC_RELAXED, __ATOMIC_RELAXED)) \
return 0; \
}
#endif

#define GET_CURR \
(struct task_struct *)bpf_get_current_task()
Expand All @@ -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)) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <map>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <linux/version.h>

std::string getLocalDateTime(void)
{
Expand Down Expand Up @@ -53,32 +54,52 @@ std::vector<CountItem> *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<CountItem>();
#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<CountItem>();
for (uint32_t i = 0; i < count; i++)
{
CountItem d(keys[i], count_values(vals + val_size * i));
D->insert(std::lower_bound(D->begin(), D->end(), d), d);
}
delete[] keys;
delete[] vals;
#endif
return D;
};

Expand Down

0 comments on commit f33ba88

Please sign in to comment.