Skip to content

Commit

Permalink
Merge branch 'linuxkerneltravel:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Monkey857 authored Jul 9, 2024
2 parents 4f0657a + 9faff7b commit c2059d6
Show file tree
Hide file tree
Showing 33 changed files with 3,199 additions and 136 deletions.
17 changes: 11 additions & 6 deletions .github/workflows/ebpf_cpu_watcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@ jobs:
- name: Install dependencies
run: |
sudo apt update
sudo apt install libbpf-dev clang llvm libelf-dev libpcap-dev gcc-multilib build-essential
sudo apt install -y libbpf-dev clang llvm libelf-dev libpcap-dev gcc-multilib build-essential
git submodule update --init --recursive
- name: Run cpu_watcher
- name: Build cpu_watcher
run: |
cd eBPF_Supermarket/CPU_Subsystem/cpu_watcher/
make
sudo ./cpu_watcher
- name: Run test_cpuwatcher

- name: Run cpu_watcher
run: |
sudo ./eBPF_Supermarket/CPU_Subsystem/cpu_watcher/cpu_watcher
- name: Build test_cpuwatcher
run: |
cd eBPF_Supermarket/CPU_Subsystem/cpu_watcher/test
make
./test_cpuwatcher
- name: Run test_cpuwatcher
run: |
./eBPF_Supermarket/CPU_Subsystem/cpu_watcher/test/test_cpuwatcher
11 changes: 8 additions & 3 deletions .github/workflows/ebpf_net_manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ jobs:
cd eBPF_Supermarket/Network_Subsystem/net_manager/
sudo ./configure
sudo make
ifconfig
# run
sudo timeout -s SIGINT 5 ./xdp_loader -d ens33 -S || if [[ $? != 124 && $? != 0 ]];then exit $?;fi
sudo ./xacladm load ens33 ./conf.d/mac_load.conf
sudo xdp-loader unload ens33 --all
cd testenv
sudo ./testenv.sh setup --name veth-basic02
cd ..
cd net_manager
sudo timeout -s SIGINT 5 ./xdp_loader -d eth0 -S || if [[ $? != 124 && $? != 0 ]];then exit $?;fi
sudo ./xdp_loader -d eth0 -S



32 changes: 30 additions & 2 deletions MagicEyes/src/visualization/vscode_ext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@

![](./images/lmp_vscode_ext.gif)

### 2. 相关提示
### 2. 安装与使用

#### 2.1 导入插件

![import_vscode_ext](./images/import_vscode_ext.png)

安装成功如下:

![lmp_ext_install_success](./images/lmp_ext_install_success.png)

#### 2.2 设置

- 启动grafana(可以在docker中启动),启动prometheus与BPF后端采集程序可以看到数据呈现
- 设置IP地址与端口,默认端口是`localhost:3000`
- 设置token

![create_token](./images/create_token.png)

> [grafana官方_创建token](https://grafana.com/docs/grafana/latest/administration/service-accounts/#create-a-service-account-in-grafana)
![set_token](./images/set_token.png)

设置可视化面板存放路径

Expand All @@ -20,7 +40,15 @@

![](./images/error_info.png)

### 3. 开发注意事项
### 3. 插件开发

#### 3.1 开发

安装yarn并且通过`yarn install`安装所需依赖

> tips: 按 F5 开启调试
#### 3.2 开发注意事项

1. yo code生成的框架,vscode最小版本是1.90,需要修改为1.74,不然我当前的版本。1.89无法运行插件
2. tsconfig
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class TreeViewProvider implements TreeDataProvider<TreeItem> {
let command_cpu_watcher = {
title: cpu_watcher_label,
command: 'itemClick',
tooltip: "点击将呈现net_watcher的grafana的可视化面板",
tooltip: "点击将呈现cpu_watcher的grafana的可视化面板",
arguments: [
cpu_watcher_label
]
Expand All @@ -182,7 +182,7 @@ export class TreeViewProvider implements TreeDataProvider<TreeItem> {
let command_proc_image = {
title: proc_iamge_label,
command: 'itemClick',
tooltip: "点击将呈现net_watcher的grafana的可视化面板",
tooltip: "点击将呈现proc_image的grafana的可视化面板",
arguments: [
proc_iamge_label
]
Expand Down Expand Up @@ -214,7 +214,7 @@ export class TreeViewProvider implements TreeDataProvider<TreeItem> {
let command_net_manager = {
title: net_manager_label,
command: 'itemClick',
tooltip: "点击将呈现net_watcher的grafana的可视化面板",
tooltip: "点击将呈现net_manager的grafana的可视化面板",
arguments: [
net_manager_label
]
Expand All @@ -231,7 +231,7 @@ export class TreeViewProvider implements TreeDataProvider<TreeItem> {
let command_mem_watcher = {
title: mem_watcher_label,
command: 'itemClick',
tooltip: "点击将呈现net_watcher的grafana的可视化面板",
tooltip: "点击将呈现mem_watcher的grafana的可视化面板",
arguments: [
mem_watcher_label
]
Expand All @@ -248,7 +248,7 @@ export class TreeViewProvider implements TreeDataProvider<TreeItem> {
let command_stack_analyzer = {
title: stack_analyzer_label,
command: 'itemClick',
tooltip: "点击将呈现net_watcher的grafana的可视化面板",
tooltip: "点击将呈现stack_analyzer的grafana的可视化面板",
arguments: [
stack_analyzer_label
]
Expand All @@ -265,7 +265,7 @@ export class TreeViewProvider implements TreeDataProvider<TreeItem> {
let command_kvm_watcher = {
title: kvm_watcher_label,
command: 'itemClick',
tooltip: "点击将呈现net_watcher的grafana的可视化面板",
tooltip: "点击将呈现kvm_watcher的grafana的可视化面板",
arguments: [
kvm_watcher_label
]
Expand Down Expand Up @@ -390,4 +390,4 @@ export class TreeViewProvider implements TreeDataProvider<TreeItem> {
}
}
*/
*/
2 changes: 1 addition & 1 deletion eBPF_Supermarket/CPU_Subsystem/cpu_watcher/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ INCLUDES := -I$(OUTPUT) -I../../../libbpf/include/uapi -I$(dir $(VMLINUX)) -I$(L
CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

APPS =cs_delay sar sc_delay preempt schedule_delay mq_delay
APPS =cs_delay sar sc_delay preempt schedule_delay mq_delay mutrace
TARGETS=cpu_watcher
CONTROLLER := controller

Expand Down
172 changes: 172 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/bpf/mutrace.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// 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] [email protected] [email protected]

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

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

BPF_HASH(mutex_info_map,u64,struct mutex_info, 1024);

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


/*----------------------------------------------*/
/* 内核态互斥锁 */
/*----------------------------------------------*/

SEC("kprobe/mutex_lock")
int BPF_KPROBE(trace_mutex_lock, struct mutex *lock) {
u64 lock_addr = (u64)lock; // 获取锁地址
u64 ts = bpf_ktime_get_ns();
struct mutex_info *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr);
if (info) {
info->acquire_time = ts; // 保存锁获取时间
} else {
struct mutex_info new_info = {
.locked_total = 0,
.locked_max = 0,
.contended_total = 0,
.last_owner = 0,
.acquire_time = ts,
.ptr = lock_addr
};
bpf_map_update_elem(&mutex_info_map, &lock_addr, &new_info, BPF_ANY);
}
return 0;
}

SEC("kprobe/mutex_trylock")
int BPF_KPROBE(trace_mutex_trylock, struct mutex *lock) {
int ret = PT_REGS_RC(ctx);
if (ret == 0) { // 成功获取锁
u64 lock_addr = (u64)lock; // 获取锁地址
u64 ts = bpf_ktime_get_ns();
struct mutex_info *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr);
if (info) {
info->acquire_time = ts;
} else {
struct mutex_info new_info = {
.locked_total = 0,
.locked_max = 0,
.contended_total = 0,
.last_owner = 0,
.acquire_time = ts,
.ptr = lock_addr
};
bpf_map_update_elem(&mutex_info_map, &lock_addr, &new_info, BPF_ANY);
}
}
return 0;
}

SEC("kprobe/__mutex_lock_slowpath")
int BPF_KPROBE(trace_mutex_lock_slowpath, struct mutex *lock) {
struct mutex_contention_event *e;
struct task_struct *owner_task;
struct task_struct *contender_task;
pid_t pid = bpf_get_current_pid_tgid();
long owner;
u64 lock_addr = (u64)lock;
u64 ts = bpf_ktime_get_ns();
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
if (!e) {
return 0;
}
e->contender_pid = pid;
e->ptr = lock_addr;
bpf_get_current_comm(&e->contender_name, sizeof(e->contender_name));
bpf_probe_read_kernel(&owner, sizeof(owner), &lock->owner);
owner_task = (struct task_struct *)(owner & ~0x1L);
contender_task = (struct task_struct *)bpf_get_current_task();
bpf_probe_read_kernel(&e->contender_prio, sizeof(e->contender_prio), &contender_task->prio);
if (owner_task) {
bpf_probe_read_kernel(&e->owner_pid, sizeof(e->owner_pid), &owner_task->pid);
bpf_probe_read_kernel_str(&e->owner_name, sizeof(e->owner_name), owner_task->comm);
bpf_probe_read_kernel(&e->owner_prio, sizeof(e->owner_prio), &owner_task->prio);
} else {
e->owner_pid = 0;
__builtin_memset(e->owner_name, 0, sizeof(e->owner_name));
}
struct mutex_info *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr);
if (info) {
info->contended_total += ts - info->acquire_time;
} else {
struct mutex_info new_info = {
.locked_total = 0,
.locked_max = 0,
.contended_total = ts,
.last_owner = 0,
.acquire_time = 0,
.ptr = lock_addr
};
bpf_map_update_elem(&mutex_info_map, &lock_addr, &new_info, BPF_ANY);
}
bpf_ringbuf_submit(e, 0);
return 0;
}

SEC("kprobe/mutex_unlock")
int BPF_KPROBE(trace_mutex_unlock, struct mutex *lock) {
u64 lock_addr = (u64)lock;
u64 ts = bpf_ktime_get_ns();
pid_t pid = bpf_get_current_pid_tgid();
struct mutex_info *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr);
if (info) {
u64 held_time = ts - info->acquire_time; // 计算锁被持有的时间
info->locked_total += held_time; // 更新锁被持有的总时间
if (held_time > info->locked_max) {
info->locked_max = held_time; // 更新锁被持有的最长时间
}
info->last_owner = pid; // 更新最后一次持有该锁的线程ID
}
return 0;
}

/*----------------------------------------------*/
/* 用户态互斥锁 */
/*----------------------------------------------*/

// SEC("uprobe")
// int BPF_KPROBE(pthread_mutex_lock_init, pthread_mutex_t *mutex){

// }

// SEC("uprobe")
// int BPF_KPROBE(pthread_mutex_lock,pthread_mutex_t *mutex){

// }

// SEC("uprobe")
// int BPF_KPROBE(pthread_mutex_try, pthread_mutex_t *mutex){

// }

// SEC("uprobe")
// int BPF_KPROBE(pthread_mutex_unlock, pthread_mutex_t *mutex){

// }

// SEC("uprobe")
// int BPF_KPROBE(pthread_mutex_destroy, pthread_mutex_t *mutex){

// }
Loading

0 comments on commit c2059d6

Please sign in to comment.