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
liuslient authored Jun 14, 2024
2 parents 0e2caea + 68b824c commit 6400bf2
Show file tree
Hide file tree
Showing 33 changed files with 4,032 additions and 652 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ebpf_cpu_watcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ jobs:
cd eBPF_Supermarket/CPU_Subsystem/cpu_watcher/
make
sudo ./cpu_watcher
- name: Run test_cpuwatcher

run: |
cd eBPF_Supermarket/CPU_Subsystem/cpu_watcher/test
make
./test_cpuwatcher
2 changes: 2 additions & 0 deletions .github/workflows/ebpf_net_watcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ jobs:
sudo timeout -s SIGINT 5 ./netwatcher -k -T || if [[ $? != 124 && $? != 0 ]];then exit $?;fi
sudo timeout -s SIGINT 5 ./netwatcher -I || if [[ $? != 124 && $? != 0 ]];then exit $?;fi
sudo timeout -s SIGINT 5 ./netwatcher -S || if [[ $? != 124 && $? != 0 ]];then exit $?;fi
sudo timeout -s SIGINT 5 ./netwatcher -D || if [[ $? != 124 && $? != 0 ]];then exit $?;fi
sudo timeout -s SIGINT 5 ./netwatcher -M || if [[ $? != 124 && $? != 0 ]];then exit $?;fi
timeout-minutes: 5
13 changes: 11 additions & 2 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

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

SRC_DIR = ./include

Expand Down Expand Up @@ -81,12 +82,12 @@ $(call allow-override,CC,$(CROSS_COMPILE)cc)
$(call allow-override,LD,$(CROSS_COMPILE)ld)

.PHONY: all
all: $(TARGETS)
all: $(CONTROLLER) $(TARGETS)

.PHONY: clean
clean:
$(call msg,CLEAN)
$(Q)rm -rf $(OUTPUT) $(TARGETS)
$(Q)rm -rf $(OUTPUT) $(TARGETS) $(CONTROLLER)

$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
$(call msg,MKDIR,$@)
Expand Down Expand Up @@ -132,11 +133,19 @@ $(OUTPUT)/%.o: $(SRC_DIR)/%.c | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OUTPUT)/%.o: $(CONTROLLER).c | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OUTPUT)/$(TARGETS).o: $(TARGETS).c $(APPS) | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@

# Build application binary
$(CONTROLLER): %: $(OUTPUT)/%.o $(COMMON_OBJ) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $^ $(ALL_LDFLAGS) -lstdc++ -lelf -lz -o $@

$(TARGETS): %: $(OUTPUT)/%.o $(COMMON_OBJ) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) $^ $(ALL_LDFLAGS) -lstdc++ -lelf -lz -o $@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
#include "cpu_watcher.h"

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

const int ctrl_key = 0;
//记录时间戳;
BPF_ARRAY(start,int,u64,1);
BPF_ARRAY(cs_ctrl_map,int,struct cs_ctrl,1);
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 256 * 1024);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@


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

const int ctrl_key = 0;
BPF_HASH(send_msg1,pid_t,struct send_events,1024);//记录pid->u_msg_ptr的关系;do_mq_timedsend入参
BPF_HASH(send_msg2,u64,struct send_events,1024);//记录msg->time的关系;
BPF_HASH(rcv_msg1,pid_t,struct rcv_events,1024);//记录pid->u_msg_ptr的关系;do_mq_timedsend入参
BPF_ARRAY(mq_ctrl_map,int,struct mq_ctrl,1);
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 256 * 1024);
Expand Down
81 changes: 53 additions & 28 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/bpf/schedule_delay.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@
char LICENSE[] SEC("license") = "Dual BSD/GPL";
#define TASK_RUNNING 0x0000

BPF_HASH(has_scheduled,struct proc_id, bool, 10240);
BPF_HASH(enter_schedule,struct proc_id, struct schedule_event, 10240);
BPF_ARRAY(sys_schedule,int,struct sum_schedule,1);

const int ctrl_key = 0;
BPF_HASH(has_scheduled,struct proc_id, bool, 10240);//记录该进程是否调度过
BPF_HASH(enter_schedule,struct proc_id, struct schedule_event, 10240);//记录该进程上运行队列的时间
BPF_ARRAY(sys_schedule,int,struct sum_schedule,1);//记录整个系统的调度延迟
BPF_ARRAY(threshold_schedule,int,struct proc_schedule,10240);//记录每个进程的调度延迟
BPF_ARRAY(schedule_ctrl_map,int,struct schedule_ctrl,1);

SEC("tp_btf/sched_wakeup")
int BPF_PROG(sched_wakeup, struct task_struct *p) {
pid_t pid = BPF_CORE_READ(p, pid);
struct schedule_ctrl *sched_ctrl;
sched_ctrl = bpf_map_lookup_elem(&schedule_ctrl_map,&ctrl_key);
if(!sched_ctrl || !sched_ctrl->schedule_func)
return 0;
pid_t pid = p->pid;
int cpu = bpf_get_smp_processor_id();
struct schedule_event *schedule_event;
struct proc_id id= {};
Expand All @@ -56,7 +62,11 @@ int BPF_PROG(sched_wakeup, struct task_struct *p) {

SEC("tp_btf/sched_wakeup_new")
int BPF_PROG(sched_wakeup_new, struct task_struct *p) {
pid_t pid = BPF_CORE_READ(p, pid);
struct schedule_ctrl *sched_ctrl;
sched_ctrl = bpf_map_lookup_elem(&schedule_ctrl_map,&ctrl_key);
if(!sched_ctrl || !sched_ctrl->schedule_func)
return 0;
pid_t pid = p->pid;
int cpu = bpf_get_smp_processor_id();
struct proc_id id= {};
u64 current_time = bpf_ktime_get_ns();
Expand All @@ -76,6 +86,11 @@ int BPF_PROG(sched_wakeup_new, struct task_struct *p) {

SEC("tp_btf/sched_switch")
int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, struct task_struct *next) {
struct schedule_ctrl *sched_ctrl;
sched_ctrl = bpf_map_lookup_elem(&schedule_ctrl_map,&ctrl_key);
if(!sched_ctrl || !sched_ctrl->schedule_func)
return 0;

u64 current_time = bpf_ktime_get_ns();
pid_t prev_pid = prev->pid;
unsigned int prev_state = prev->__state;
Expand All @@ -86,17 +101,17 @@ int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, struct task_s
struct schedule_event *schedule_event;
struct sum_schedule *sum_schedule;
int key = 0;
struct proc_id next_id= {};
struct proc_id next_id = {};
u64 delay;
if (prev_state == TASK_RUNNING) {
struct proc_id prev_pd= {};
struct proc_id prev_pd = {};
prev_pd.pid = prev_pid;
if (prev_pid == 0) {
prev_pd.cpu_id = prev_cpu;
}
}
schedule_event = bpf_map_lookup_elem(&enter_schedule, &prev_pd);
if (!schedule_event) {
struct schedule_event schedule_event2 ;
struct schedule_event schedule_event2;
bool issched = false;
schedule_event2.pid = prev_pid;
schedule_event2.count = 1;
Expand All @@ -113,50 +128,60 @@ int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, struct task_s
next_id.cpu_id = next_cpu;
}
schedule_event = bpf_map_lookup_elem(&enter_schedule, &next_id);
if (!schedule_event) return 0;
if (!schedule_event) return 0;
issched = bpf_map_lookup_elem(&has_scheduled, &next_id);
if (!issched) return 0;
if (!issched) return 0;
if (*issched) {
schedule_event->count++;
} else {
*issched = true;
}
}
delay = current_time - schedule_event->enter_time;
struct proc_schedule proc_schedule;
proc_schedule.pid = next_pid;
proc_schedule.delay = delay;
bpf_probe_read_kernel_str(&proc_schedule.proc_name, sizeof(proc_schedule.proc_name), next->comm);
bpf_map_update_elem(&threshold_schedule, &key, &proc_schedule, BPF_ANY);
sum_schedule = bpf_map_lookup_elem(&sys_schedule, &key);
if (!sum_schedule) {
struct sum_schedule sum_schedule= {};
struct sum_schedule sum_schedule = {};
sum_schedule.sum_count++;
sum_schedule.sum_delay += delay;
if (delay > sum_schedule.max_delay){
if (delay > sum_schedule.max_delay) {
sum_schedule.max_delay = delay;
if(next->pid!=0){
sum_schedule.pid_max = next->pid;
if (next->pid != 0) {
bpf_probe_read_kernel_str(&sum_schedule.proc_name_max, sizeof(sum_schedule.proc_name_max), next->comm);
}
}else if (sum_schedule.min_delay == 0 || delay < sum_schedule.min_delay)
} else if (sum_schedule.min_delay == 0 || delay < sum_schedule.min_delay) {
sum_schedule.min_delay = delay;
if(next->pid!=0){
sum_schedule.pid_min = next->pid;
if (next->pid != 0) {
bpf_probe_read_kernel_str(&sum_schedule.proc_name_min, sizeof(sum_schedule.proc_name_min), next->comm);
}
}
bpf_map_update_elem(&sys_schedule, &key, &sum_schedule, BPF_ANY);
} else {
sum_schedule->sum_count++;
sum_schedule->sum_delay += delay;
if (delay > sum_schedule->max_delay){
if (delay > sum_schedule->max_delay) {
sum_schedule->max_delay = delay;
if(next->pid!=0){
sum_schedule->pid_max = next->pid;
}
}else if (sum_schedule->min_delay == 0 || delay < sum_schedule->min_delay)
bpf_probe_read_kernel_str(&sum_schedule->proc_name_max, sizeof(sum_schedule->proc_name_max), next->comm);
} else if (sum_schedule->min_delay == 0 || delay < sum_schedule->min_delay) {
sum_schedule->min_delay = delay;
if(next->pid!=0){
sum_schedule->pid_min = next->pid;
if (next->pid != 0) {
bpf_probe_read_kernel_str(&sum_schedule->proc_name_min, sizeof(sum_schedule->proc_name_min), next->comm);
}
}
}
return 0;
}

SEC("tracepoint/sched/sched_process_exit")
int sched_process_exit(void *ctx) {
struct schedule_ctrl *sched_ctrl;
sched_ctrl = bpf_map_lookup_elem(&schedule_ctrl_map,&ctrl_key);
if(!sched_ctrl || !sched_ctrl->schedule_func)
return 0;

struct task_struct *p = (struct task_struct *)bpf_get_current_task();
pid_t pid = BPF_CORE_READ(p, pid);
int cpu = bpf_get_smp_processor_id();
Expand All @@ -175,4 +200,4 @@ int sched_process_exit(void *ctx) {
bpf_map_delete_elem(&has_scheduled, &id);
}
return 0;
}
}
Loading

0 comments on commit 6400bf2

Please sign in to comment.