Skip to content

Commit

Permalink
revision
Browse files Browse the repository at this point in the history
  • Loading branch information
vvzxy committed Sep 7, 2024
1 parent d36874c commit 4e1d172
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/bpf/cs_delay.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ SEC("kprobe/schedule")
int BPF_KPROBE(schedule)
{
struct cs_ctrl *cs_ctrl = get_cs_ctrl();
if (!cs_ctrl) {
return 0;
}
u64 t1;
t1 = bpf_ktime_get_ns()/1000;
int key =0;
Expand All @@ -54,6 +57,9 @@ SEC("kretprobe/schedule")
int BPF_KRETPROBE(schedule_exit)
{
struct cs_ctrl *cs_ctrl = get_cs_ctrl();
if (!cs_ctrl) {
return 0;
}
u64 t2 = bpf_ktime_get_ns()/1000;
u64 t1,delay;
int key = 0;
Expand Down
21 changes: 21 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/bpf/mq_delay.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ int BPF_KPROBE(mq_timedsend,mqd_t mqdes, const char *u_msg_ptr,
struct timespec64 *ts)
{
struct mq_ctrl *mq_ctrl = get_mq_ctrl();
if (!mq_ctrl) {
return 0;
}
u64 send_enter_time = bpf_ktime_get_ns();//开始发送信息时间;
int pid = bpf_get_current_pid_tgid();//发送端pid

Expand All @@ -95,6 +98,9 @@ int BPF_KPROBE(mq_timedsend,mqd_t mqdes, const char *u_msg_ptr,
SEC("kprobe/load_msg")
int BPF_KPROBE(load_msg_enter,const void *src, size_t len){
struct mq_ctrl *mq_ctrl = get_mq_ctrl();
if (!mq_ctrl) {
return 0;
}
int pid = bpf_get_current_pid_tgid();//发送端pid
/*记录load入参src*/
struct send_events *mq_send_info = bpf_map_lookup_elem(&send_msg1, &pid);
Expand All @@ -110,6 +116,9 @@ int BPF_KPROBE(load_msg_enter,const void *src, size_t len){
SEC("kretprobe/load_msg")
int BPF_KRETPROBE(load_msg_exit,void *ret){
struct mq_ctrl *mq_ctrl = get_mq_ctrl();
if (!mq_ctrl) {
return 0;
}
int pid = bpf_get_current_pid_tgid();//发送端pid
/*构建消息块结构体,作为key*/
struct send_events *mq_send_info = bpf_map_lookup_elem(&send_msg1, &pid);
Expand All @@ -136,6 +145,9 @@ SEC("kretprobe/do_mq_timedsend")
int BPF_KRETPROBE(do_mq_timedsend_exit,void *ret)
{
struct mq_ctrl *mq_ctrl = get_mq_ctrl();
if (!mq_ctrl) {
return 0;
}
bpf_printk("do_mq_timedsend_exit----------------------------------------------------------------\n");
u64 send_exit_time = bpf_ktime_get_ns();//开始发送信息时间;
int pid = bpf_get_current_pid_tgid();//发送端pid
Expand Down Expand Up @@ -164,6 +176,9 @@ int BPF_KPROBE(mq_timedreceive_entry,mqd_t mqdes, const char __user *u_msg_ptr,
struct timespec64 *ts)
{
struct mq_ctrl *mq_ctrl = get_mq_ctrl();
if (!mq_ctrl) {
return 0;
}
u64 rcv_enter_time = bpf_ktime_get_ns();
int pid = bpf_get_current_pid_tgid();

Expand All @@ -182,6 +197,9 @@ SEC("kprobe/store_msg")
int BPF_KPROBE(store_msg,void __user *dest, struct msg_msg *msg, size_t len)
{
struct mq_ctrl *mq_ctrl = get_mq_ctrl();
if (!mq_ctrl) {
return 0;
}
int pid = bpf_get_current_pid_tgid();

/*make key*/
Expand Down Expand Up @@ -210,6 +228,9 @@ int BPF_KPROBE(store_msg,void __user *dest, struct msg_msg *msg, size_t len)
SEC("kretprobe/do_mq_timedreceive")
int BPF_KRETPROBE(do_mq_timedreceive_exit,void *ret){
struct mq_ctrl *mq_ctrl = get_mq_ctrl();
if (!mq_ctrl) {
return 0;
}
u64 rcv_exit_time = bpf_ktime_get_ns();
int pid = bpf_get_current_pid_tgid();
u64 send_enter_time,delay;
Expand Down
24 changes: 24 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/bpf/mutrace.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ static inline void init_mutex_info(struct mutex_info *info, u64 lock_addr, u64 t
SEC("kprobe/mutex_lock")
int BPF_KPROBE(trace_mutex_lock, struct mutex *lock) {
struct mu_ctrl *mu_ctrl = get_mu_ctrl();
if (!mu_ctrl) {
return 0;
}
u64 lock_addr = (u64)lock;
u64 ts = bpf_ktime_get_ns();
struct mutex_info *info = bpf_map_lookup_elem(&kmutex_info_map, &lock_addr);
Expand All @@ -76,6 +79,9 @@ int BPF_KPROBE(trace_mutex_lock, struct mutex *lock) {
SEC("kprobe/mutex_trylock")
int BPF_KPROBE(trace_mutex_trylock, struct mutex *lock) {
struct mu_ctrl *mu_ctrl = get_mu_ctrl();
if (!mu_ctrl) {
return 0;
}
int ret = PT_REGS_RC(ctx);
if (ret != 0) {
u64 lock_addr = (u64)lock;
Expand All @@ -97,6 +103,9 @@ int BPF_KPROBE(trace_mutex_trylock, struct mutex *lock) {
SEC("kprobe/__mutex_lock_slowpath")
int BPF_KPROBE(trace_mutex_lock_slowpath, struct mutex *lock) {
struct mu_ctrl *mu_ctrl = get_mu_ctrl();
if (!mu_ctrl) {
return 0;
}
if (!mu_ctrl) return 0;

u64 lock_addr = (u64)lock;
Expand Down Expand Up @@ -144,6 +153,9 @@ int BPF_KPROBE(trace_mutex_lock_slowpath, struct mutex *lock) {
SEC("kprobe/mutex_unlock")
int BPF_KPROBE(trace_mutex_unlock, struct mutex *lock) {
struct mu_ctrl *mu_ctrl = get_mu_ctrl();
if (!mu_ctrl) {
return 0;
}
u64 lock_addr = (u64)lock;
u64 ts = bpf_ktime_get_ns();
pid_t pid = bpf_get_current_pid_tgid();
Expand Down Expand Up @@ -184,6 +196,9 @@ static inline void handle_user_mutex_lock(void *__mutex, u64 now, pid_t pid) {
SEC("uprobe/pthread_mutex_lock")
int BPF_KPROBE(pthread_mutex_lock, void *__mutex) {
struct mu_ctrl *mu_ctrl = get_mu_ctrl();
if (!mu_ctrl) {
return 0;
}
u64 now = bpf_ktime_get_ns();
pid_t pid = bpf_get_current_pid_tgid() >> 32;
handle_user_mutex_lock(__mutex, now, pid);
Expand All @@ -193,6 +208,9 @@ int BPF_KPROBE(pthread_mutex_lock, void *__mutex) {
SEC("uprobe/__pthread_mutex_trylock")
int BPF_KPROBE(__pthread_mutex_trylock, void *__mutex) {
struct mu_ctrl *mu_ctrl = get_mu_ctrl();
if (!mu_ctrl) {
return 0;
}
u64 pid_tgid = bpf_get_current_pid_tgid();
u64 now = bpf_ktime_get_ns();
struct trylock_info info = {
Expand All @@ -206,6 +224,9 @@ int BPF_KPROBE(__pthread_mutex_trylock, void *__mutex) {
SEC("uretprobe/__pthread_mutex_trylock")
int BPF_KRETPROBE(ret_pthread_mutex_trylock, int ret) {
struct mu_ctrl *mu_ctrl = get_mu_ctrl();
if (!mu_ctrl) {
return 0;
}
u64 pid_tgid = bpf_get_current_pid_tgid();
struct trylock_info *try_info = bpf_map_lookup_elem(&trylock_map, &pid_tgid);
if (!try_info) return 0;
Expand All @@ -220,6 +241,9 @@ int BPF_KRETPROBE(ret_pthread_mutex_trylock, int ret) {
SEC("uprobe/pthread_mutex_unlock")
int BPF_KPROBE(pthread_mutex_unlock, void *__mutex) {
struct mu_ctrl *mu_ctrl = get_mu_ctrl();
if (!mu_ctrl) {
return 0;
}
u64 now = bpf_ktime_get_ns();
pid_t pid = bpf_get_current_pid_tgid() >> 32;
struct mutex_info *info = bpf_map_lookup_elem(&umutex_info_map, &__mutex);
Expand Down
6 changes: 6 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/bpf/preempt.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ static inline struct preempt_ctrl *get_preempt_ctrl(void) {
SEC("tp_btf/sched_switch")
int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, struct task_struct *next) {
struct preempt_ctrl *preempt_ctrl = get_preempt_ctrl();
if (!preempt_ctrl) {
return 0;
}
u64 start_time = bpf_ktime_get_ns();
pid_t prev_pid = BPF_CORE_READ(prev, pid);

Expand All @@ -63,6 +66,9 @@ int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, struct task_s
SEC("kprobe/finish_task_switch.isra.0")
int BPF_KPROBE(finish_task_switch, struct task_struct *prev) {
struct preempt_ctrl *preempt_ctrl = get_preempt_ctrl();
if (!preempt_ctrl) {
return 0;
}
u64 end_time = bpf_ktime_get_ns();
pid_t pid = BPF_CORE_READ(prev, pid);
u64 *val;
Expand Down
30 changes: 30 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/bpf/sar.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ SEC("kprobe/finish_task_switch.isra.0")
int kprobe__finish_task_switch(struct pt_regs *ctx)
{
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
u32 key = 0;
u64 val, *valp = NULL;
unsigned long total_forks;
Expand All @@ -86,6 +89,9 @@ int kprobe__finish_task_switch(struct pt_regs *ctx)
SEC("tracepoint/sched/sched_switch")
int trace_sched_switch2(struct cswch_args *info) {
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
pid_t prev = info->prev_pid, next = info->next_pid;
if (prev != next) {
u32 key = 0;
Expand All @@ -108,6 +114,9 @@ int trace_sched_switch2(struct cswch_args *info) {
SEC("kprobe/finish_task_switch.isra.0")
int BPF_KPROBE(finish_task_switch,struct task_struct *prev){
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
pid_t pid=BPF_CORE_READ(prev,pid);
u64 *val, time = bpf_ktime_get_ns();
u64 delta;
Expand Down Expand Up @@ -139,6 +148,9 @@ int BPF_KPROBE(finish_task_switch,struct task_struct *prev){
SEC("kprobe/update_rq_clock")
int BPF_KPROBE(update_rq_clock,struct rq *rq){
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
u32 key = 0;
u64 val = BPF_CORE_READ(rq,nr_running);
bpf_map_update_elem(&runqlen,&key,&val,BPF_ANY);
Expand All @@ -149,6 +161,9 @@ int BPF_KPROBE(update_rq_clock,struct rq *rq){
SEC("tracepoint/irq/softirq_entry")
int trace_softirq_entry(struct __softirq_info *info) {
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
u32 key = info->vec;
u64 val = bpf_ktime_get_ns();
bpf_map_update_elem(&softirqCpuEnterTime, &key, &val, BPF_ANY);
Expand All @@ -158,6 +173,9 @@ int trace_softirq_entry(struct __softirq_info *info) {
SEC("tracepoint/irq/softirq_exit")
int trace_softirq_exit(struct __softirq_info *info) {
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
u32 key = info->vec;
u64 now = bpf_ktime_get_ns(), *valp = 0;
valp =bpf_map_lookup_elem(&softirqCpuEnterTime, &key);
Expand All @@ -177,6 +195,9 @@ int trace_softirq_exit(struct __softirq_info *info) {
SEC("tracepoint/irq/irq_handler_entry")
int trace_irq_handler_entry(struct __irq_info *info) {
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
u32 key = info->irq;
u64 ts = bpf_ktime_get_ns();
bpf_map_update_elem(&irq_cpu_enter_start, &key, &ts, BPF_ANY);
Expand All @@ -186,6 +207,9 @@ int trace_irq_handler_entry(struct __irq_info *info) {
SEC("tracepoint/irq/irq_handler_exit")
int trace_irq_handler_exit(struct __irq_info *info) {
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
u32 key = info->irq;
u64 now = bpf_ktime_get_ns(), *ts = 0;
ts = bpf_map_lookup_elem(&irq_cpu_enter_start, &key);
Expand All @@ -206,6 +230,9 @@ int trace_irq_handler_exit(struct __irq_info *info) {
SEC("tracepoint/power/cpu_idle")
int trace_cpu_idle(struct idleStruct *pIDLE) {
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}
u64 delta, time = bpf_ktime_get_ns();
u32 key = pIDLE->cpu_id;
if (pIDLE->state == -1) {
Expand Down Expand Up @@ -236,6 +263,9 @@ static __always_inline int user_mode(struct pt_regs *regs)
SEC("perf_event")
int tick_update(struct pt_regs *ctx) {
struct sar_ctrl *sar_ctrl = get_sar_ctrl();
if (!sar_ctrl) {
return 0;
}

// bpf_trace_printk("cs_rpl = %x\n", ctx->cs & 3);
u32 key = 0;
Expand Down
6 changes: 6 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/bpf/sc_delay.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ static inline struct sc_ctrl *get_sc_ctrl(void) {
SEC("tracepoint/raw_syscalls/sys_enter")
int tracepoint__syscalls__sys_enter(struct trace_event_raw_sys_enter *args){
struct sc_ctrl *sc_ctrl = get_sc_ctrl();
if (!sc_ctrl) {
return 0;
}
u64 start_time = bpf_ktime_get_ns()/1000;
pid_t pid = bpf_get_current_pid_tgid();
u64 syscall_id = (u64)args->id;
Expand All @@ -58,6 +61,9 @@ int tracepoint__syscalls__sys_enter(struct trace_event_raw_sys_enter *args){
SEC("tracepoint/raw_syscalls/sys_exit")
int tracepoint__syscalls__sys_exit(struct trace_event_raw_sys_exit *args){
struct sc_ctrl *sc_ctrl = get_sc_ctrl();
if (!sc_ctrl) {
return 0;
}
u64 exit_time = bpf_ktime_get_ns()/1000;
pid_t pid = bpf_get_current_pid_tgid() ;
u64 syscall_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ static inline struct schedule_ctrl *get_schedule_ctrl(void) {
SEC("tp_btf/sched_wakeup")
int BPF_PROG(sched_wakeup, struct task_struct *p) {
struct schedule_ctrl *sched_ctrl = get_schedule_ctrl();
if (!sched_ctrl) {
return 0;
}
pid_t pid = p->pid;
int cpu = bpf_get_smp_processor_id();
struct schedule_event *schedule_event;
Expand Down Expand Up @@ -70,6 +73,9 @@ 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) {
struct schedule_ctrl *sched_ctrl = get_schedule_ctrl();
if (!sched_ctrl) {
return 0;
}
sched_ctrl = bpf_map_lookup_elem(&schedule_ctrl_map,&ctrl_key);
if(!sched_ctrl || !sched_ctrl->schedule_func)
return 0;
Expand All @@ -94,6 +100,9 @@ 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 = get_schedule_ctrl();
if (!sched_ctrl) {
return 0;
}
struct proc_history *history;
struct proc_history new_history;
u64 current_time = bpf_ktime_get_ns();
Expand Down Expand Up @@ -198,6 +207,9 @@ int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, struct task_s
SEC("tracepoint/sched/sched_process_exit")
int sched_process_exit(void *ctx) {
struct schedule_ctrl *sched_ctrl = get_schedule_ctrl();
if (!sched_ctrl) {
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 Down

0 comments on commit 4e1d172

Please sign in to comment.