Skip to content

Commit

Permalink
Merge pull request #834 from GorilaMond/add_version_ctrl
Browse files Browse the repository at this point in the history
Stack_Analyzer: Add version ctrl
  • Loading branch information
chenamy2017 authored Jun 14, 2024
2 parents a3be13e + f33ba88 commit 68b824c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions eBPF_Supermarket/Stack_Analyser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ $(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT) $(BPF_SKEL):
$(Q)mkdir -p $@

# Build libbpf
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
$(LIBBPF_OBJ): $(LIBBPF_SRC) $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
$(call msg,LIB,$@)
$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \
INCLUDEDIR= LIBDIR= UAPIDIR= \
install

# Build bpftool
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
$(BPFTOOL): $(BPFTOOL_SRC) | $(BPFTOOL_OUTPUT)
$(call msg,BPFTOOL,$@)
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap

Expand Down
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 68b824c

Please sign in to comment.