Skip to content

Commit

Permalink
Merge pull request #622 from zhangzihengya/develop
Browse files Browse the repository at this point in the history
proc_image项目:在新框架中补充并完善锁功能,解决因版本问题导致无法采集进程内存使用情况的问题
  • Loading branch information
LinkinPF authored Dec 8, 2023
2 parents 59cd978 + 41a90cf commit 19203ed
Show file tree
Hide file tree
Showing 9 changed files with 143,941 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/eBPF_proc_image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
run: |
cd eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/developing
make proc_image
sudo ./proc_image -r -t 1
sudo ./proc_image -a -p 1 -t 1
- name: Run mutex_test
run: |
Expand Down
4 changes: 2 additions & 2 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse @13186379707
/eBPF_Supermarket/Network_Subsystem/net_watcher @helight @LinkinPF @chenamy2017
/eBPF_Supermarket/CPU_Subsystem/cpu_watcher @helight @LinkinPF @chenamy2017
/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image @helight @LinkinPF @chenamy2017
/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image @helight @LinkinPF @chenamy2017 @zhangzihengya
/eBPF_Supermarket/Stack_Analyser @helight @LinkinPF @chenamy2017
/eBPF_Supermarket/kvm_watcher @helight @LinkinPF @chenamy2017
/eBPF_Supermarket/kvm_watcher @helight @LinkinPF @chenamy2017
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
| sed 's/mips.*/mips/' \
| sed 's/riscv64/riscv/' \
| sed 's/loongarch64/loongarch/')
VMLINUX := ../../vmlinux/$(ARCH)/vmlinux.h
# Use our own libbpf API headers and Linux UAPI headers distributed with
# libbpf to avoid dependency on system-wide headers, which could be missing or
# outdated
INCLUDES := -I$(OUTPUT) -I../../libbpf/include/uapi -I$(dir $(VMLINUX)) -I$(LIBBLAZESYM_INC) -I./include
INCLUDES := -I$(OUTPUT) -I../../libbpf/include/uapi -I$(LIBBLAZESYM_INC) -I./include
CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

APPS = resource_image #syscall_image
APPS = resource_image lock_image #syscall_image
TARGETS = proc_image

# Get Clang's default includes on this system. We'll explicitly add these dirs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Copyright 2023 The LMP Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// author: [email protected]
//
// eBPF kernel-mode code that collects holding lock information of processes

#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
#include "proc_image.h"
#include "lock_image.h"

char LICENSE[] SEC("license") = "Dual BSD/GPL";

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, struct proc_flag);
__type(value, u64);
} proc_lock SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, struct proc_flag);
__type(value, u64);
} proc_unlock SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, u64);
__type(value, int);
} locktype SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries,256 * 10240);
} lock_rb SEC(".maps");

// 用户态互斥锁
SEC("uprobe/pthread_mutex_lock")
int BPF_KPROBE(pthread_mutex_lock_enter, void *__mutex)
{
record_lock_enter(1,1,__mutex,&lock_rb,&proc_lock);

return 0;
}

SEC("uretprobe/pthread_mutex_lock")
int BPF_KRETPROBE(pthread_mutex_lock_exit,int ret)
{
record_lock_exit(2,1,ret,&lock_rb,&proc_lock,&locktype);

return 0;
}

SEC("uprobe/__pthread_mutex_trylock")
int BPF_KPROBE(__pthread_mutex_trylock_enter, void *__mutex)
{
record_lock_enter(1,1,__mutex,&lock_rb,&proc_lock);

return 0;
}

SEC("uretprobe/__pthread_mutex_trylock")
int BPF_KRETPROBE(__pthread_mutex_trylock_exit,int ret)
{
record_lock_exit(2,1,ret,&lock_rb,&proc_lock,&locktype);

return 0;
}

SEC("uprobe/pthread_mutex_unlock")
int BPF_KPROBE(pthread_mutex_unlock_enter, void *__rwlock)
{
record_unlock_enter(1,__rwlock,&proc_unlock);

return 0;
}

SEC("uretprobe/pthread_mutex_unlock")
int BPF_KRETPROBE(pthread_mutex_unlock_exit)
{
record_unlock_exit(3,1,&lock_rb,&proc_unlock,&locktype);

return 0;
}

// 用户态读写锁
SEC("uprobe/__pthread_rwlock_rdlock")
int BPF_KPROBE(__pthread_rwlock_rdlock_enter, void *__rwlock)
{
record_lock_enter(4,2,__rwlock,&lock_rb,&proc_lock);

return 0;
}

SEC("uretprobe/__pthread_rwlock_rdlock")
int BPF_KRETPROBE(__pthread_rwlock_rdlock_exit,int ret)
{
record_lock_exit(5,2,ret,&lock_rb,&proc_lock,&locktype);

return 0;
}

SEC("uprobe/__pthread_rwlock_tryrdlock")
int BPF_KPROBE(__pthread_rwlock_tryrdlock_enter, void *__rwlock)
{
record_lock_enter(4,2,__rwlock,&lock_rb,&proc_lock);

return 0;
}

SEC("uretprobe/__pthread_rwlock_tryrdlock")
int BPF_KRETPROBE(__pthread_rwlock_tryrdlock_exit,int ret)
{
record_lock_exit(5,2,ret,&lock_rb,&proc_lock,&locktype);

return 0;
}

SEC("uprobe/__pthread_rwlock_wrlock")
int BPF_KPROBE(__pthread_rwlock_wrlock_enter, void *__rwlock)
{
record_lock_enter(7,2,__rwlock,&lock_rb,&proc_lock);

return 0;
}

SEC("uretprobe/__pthread_rwlock_wrlock")
int BPF_KRETPROBE(__pthread_rwlock_wrlock_exit,int ret)
{
record_lock_exit(8,2,ret,&lock_rb,&proc_lock,&locktype);

return 0;
}

SEC("uprobe/__pthread_rwlock_trywrlock")
int BPF_KPROBE(__pthread_rwlock_trywrlock_enter, void *__rwlock)
{
record_lock_enter(7,2,__rwlock,&lock_rb,&proc_lock);

return 0;
}

SEC("uretprobe/__pthread_rwlock_trywrlock")
int BPF_KRETPROBE(__pthread_rwlock_trywrlock_exit,int ret)
{
record_lock_exit(8,2,ret,&lock_rb,&proc_lock,&locktype);

return 0;
}

SEC("uprobe/__pthread_rwlock_unlock")
int BPF_KPROBE(__pthread_rwlock_unlock_enter, void *__rwlock)
{
record_unlock_enter(2,__rwlock,&proc_unlock);

return 0;
}

SEC("uretprobe/__pthread_rwlock_unlock")
int BPF_KRETPROBE(__pthread_rwlock_unlock_exit)
{
record_unlock_exit(0,2,&lock_rb,&proc_unlock,&locktype);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
// eBPF kernel-mode code that collects process resource usage

#include <vmlinux.h>
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
Expand Down Expand Up @@ -68,18 +68,19 @@ int kprobe__finish_task_switch(struct pt_regs *ctx)
struct total_rsc prev_total = {0};
long unsigned int memused;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)

#else
/*
// #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)
struct percpu_counter *rss;
rss = BPF_CORE_READ(prev,mm,rss_stat);
if(!rss) return 0;
memused = rss[0].count + rss[1].count + rss[3].count;
/* #else
struct mm_rss_stat rss = {};
long long *c;
rss = BPF_CORE_READ(prev, mm, rss_stat);
c = (long long *)(rss.count);
if(!c) return 0;
memused = *c + *(c + 1) + *(c + 3);
*/
#endif
#endif */

prev_total.pid = prev_pd.pid;
prev_total.cpu_id = prev_cpu;
Expand All @@ -97,18 +98,19 @@ int kprobe__finish_task_switch(struct pt_regs *ctx)

long unsigned int memused;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)

#else
/*
//#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)
struct percpu_counter *rss;
rss = BPF_CORE_READ(prev,mm,rss_stat);
if(!rss) return 0;
memused = rss[0].count + rss[1].count + rss[3].count;
/* #else
struct mm_rss_stat rss = {};
long long *c;
rss = BPF_CORE_READ(prev, mm, rss_stat);
c = (long long *)(rss.count);
if(!c) return 0;
memused = *c + *(c + 1) + *(c + 3);
*/
#endif
#endif */

prev_total->cpu_id = prev_cpu;
prev_total->time += bpf_ktime_get_ns() - prev_start->time;
Expand Down
Loading

0 comments on commit 19203ed

Please sign in to comment.