Skip to content

Commit

Permalink
modify syscall_delay
Browse files Browse the repository at this point in the history
  • Loading branch information
albertxu216 committed Mar 8, 2024
1 parent 58dc06e commit e65fc60
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 110 deletions.
40 changes: 13 additions & 27 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/cpu_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static const struct argp_option opts[] = {
{ "time", 't', "TIME-SEC", 0, "Max Running Time(0 for infinite)" },
{"libbpf_sar", 's', 0,0,"print sar_info (the data of cpu)"},
{"cs_delay", 'c', 0,0,"print cs_delay (the data of cpu)"},
{"syscall_delay", 'y', 0,0,"print syscall_delay (the data of syscall)"},
{"syscall_delay", 'S', 0,0,"print syscall_delay (the data of syscall)"},
{ NULL, 'h', NULL, OPTION_HIDDEN, "show the full help" },
{0},
};
Expand All @@ -90,7 +90,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
case 'c':
env.CS_DELAY = true;
break;
case 'y':
case 'S':
env.SYSCALL_DELAY = true;
break;
case 'h':
Expand Down Expand Up @@ -385,26 +385,19 @@ static void histogram()
}


static void max_print(){
// static void max_print(){

int sc_average_time = sc_sum_time/sys_call_count;
printf("Average_Syscall_Time: %8d ms\n",sc_average_time);
printf("MAX_Syscall_Time: %8d ms\n",sc_max_time);
printf("MIN_Syscall_Time: %8d ms\n",sc_min_time);
}
// int sc_average_time = sc_sum_time/sys_call_count;
// printf("Average_Syscall_Time: %8d ms\n",sc_average_time);
// printf("MAX_Syscall_Time: %8d ms\n",sc_max_time);
// printf("MIN_Syscall_Time: %8d ms\n",sc_min_time);
// }
static int syscall_delay_print(void *ctx, void *data,unsigned long data_sz)
{

const struct event2 *e = data;
printf("|COMM: %-15s |pid: %-8lu |start_time: %-10lu |exit_time: %-10lu |delay: %-8lu|\n",e->comm,e->pid,e->start_time,e->exit_time,e->delay);
sc_sum_time += e->delay;
if(sc_max_time < e->delay){
sc_max_time = e->delay;
}
else if(sc_min_time > e->delay){
sc_min_time = e->delay;
}
sys_call_count ++;
const struct syscall_events *e = data;
printf("pid: %-8llu comm: %-10s syscall_id: %-8ld delay: %-8llu\n",
e->pid,e->comm,e->syscall_id,e->delay);
return 0;
}

Expand Down Expand Up @@ -563,19 +556,12 @@ int main(int argc, char **argv)
}
time_t now = time(NULL);// 获取当前时间
struct tm *localTime = localtime(&now);// 将时间转换为本地时间结构
if(!syscall_start_print){
syscall_start_print=1;
}else{
printf("----------------------------------------------------------------------------------------------------------\n");
max_print();
}
printf("\n\nTime: %02d:%02d:%02d\n",localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
printf("----------------------------------------------------------------------------------------------------------\n");
sc_sum_time = 0 , sc_max_time = 0 ,sc_min_time = SYSCALL_MIN_TIME, sys_call_count = 0;
sleep(3);
sleep(1);
}
else {
printf("正在开发中......\n-c 打印cs_delay:\t对内核函数schedule()的执行时长进行测试;\n-s sar工具;\n-y 打印sc_delay:\t系统调用运行延迟进行检测; \n");
printf("正在开发中......\n-c 打印cs_delay:\t对内核函数schedule()的执行时长进行测试;\n-s sar工具;\n-S 打印sc_delay:\t系统调用运行延迟进行检测; \n");
break;
}
}
Expand Down
133 changes: 70 additions & 63 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/cpu_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,15 @@
// eBPF map for libbpf sar
#include <asm/types.h>
#include <linux/version.h>
//#include <unordered_map>

typedef long long unsigned int u64;
typedef unsigned int u32;
#define MAX_CPU_NR 128
#define TASK_COMM_LEN 20
#define SYSCALL_MIN_TIME 1E7
/*----------------------------------------------*/
/* cs_delay结构体 */
/*----------------------------------------------*/
#ifndef __CS_DELAY_H
#define __CS_DELAY_H
struct event {
long unsigned int t1;
long unsigned int t2;
long unsigned int delay;
};
#endif /* __CS_DELAY_H */

/*----------------------------------------------*/
/* syscall_delay结构体 */
/*----------------------------------------------*/
struct event2 {
long unsigned int start_time;
long unsigned int exit_time;
long unsigned int delay;
int pid;
char comm[TASK_COMM_LEN];
};

/*----------------------------------------------*/
/* cswch_args结构体 */
/*----------------------------------------------*/
struct cswch_args {
u64 pad;
char prev_comm[16];
pid_t prev_pid;
int prev_prio;
long prev_state;
char next_comm[16];
pid_t next_pid;
int next_prio;
};

/*----------------------------------------------*/
/* 软中断结构体 */
/*----------------------------------------------*/
struct __softirq_info {
u64 pad;
u32 vec;
};

/*----------------------------------------------*/
/* 硬中断结构体 */
/*----------------------------------------------*/
struct __irq_info {
u64 pad;
u32 irq;
};

/*----------------------------------------------*/
/* idlecpu空闲时间所需结构体 */
/*----------------------------------------------*/
struct idleStruct {
u64 pad;
unsigned int state;
unsigned int cpu_id;
};

#define MAX_SYSCALL_COUNT 100
#define MAX_ENTRIES 102400 // map容量

/*----------------------------------------------*/
/* 一些maps结构体的宏定义 */
Expand Down Expand Up @@ -143,4 +84,70 @@ struct idleStruct {
__uint(key_size, sizeof(type1)); \
__uint(value_size, sizeof(type2)); \
__uint(max_entries, MAX_ENTRIES); \
} name SEC(".maps")
} name SEC(".maps")

/*----------------------------------------------*/
/* cs_delay结构体 */
/*----------------------------------------------*/
#ifndef __CS_DELAY_H
#define __CS_DELAY_H
struct event {
long unsigned int t1;
long unsigned int t2;
long unsigned int delay;
};
#endif /* __CS_DELAY_H */

/*----------------------------------------------*/
/* syscall_delay结构体 */
/*----------------------------------------------*/
struct syscall_flags{
long unsigned int start_time;
int syscall_id;
};

struct syscall_events {//每个进程一个
int pid,count;
char comm[TASK_COMM_LEN];
u64 delay;
u64 syscall_id;
};

/*----------------------------------------------*/
/* cswch_args结构体 */
/*----------------------------------------------*/
struct cswch_args {
u64 pad;
char prev_comm[16];
pid_t prev_pid;
int prev_prio;
long prev_state;
char next_comm[16];
pid_t next_pid;
int next_prio;
};

/*----------------------------------------------*/
/* 软中断结构体 */
/*----------------------------------------------*/
struct __softirq_info {
u64 pad;
u32 vec;
};

/*----------------------------------------------*/
/* 硬中断结构体 */
/*----------------------------------------------*/
struct __irq_info {
u64 pad;
u32 irq;
};

/*----------------------------------------------*/
/* idlecpu空闲时间所需结构体 */
/*----------------------------------------------*/
struct idleStruct {
u64 pad;
unsigned int state;
unsigned int cpu_id;
};
57 changes: 37 additions & 20 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/sc_delay.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,64 @@
char LICENSE[] SEC("license") = "Dual BSD/GPL";

// 定义数组映射
BPF_HASH(procStartTime,pid_t,u64,4096);//记录时间戳
//BPF_PERCPU_HASH(SyscallEnterTime,pid_t,struct syscall_flags,512);//记录时间戳
BPF_PERCPU_HASH(SyscallEnterTime,pid_t,u64,512);//记录时间戳
BPF_PERCPU_HASH(Events,pid_t,u64,10);

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 256 * 1024);
} rb SEC(".maps");//环形缓冲区;


SEC("tracepoint/syscalls/sys_enter_execve")//进入系统调用
int tracepoint__syscalls__sys_enter_execve(struct trace_event_raw_sys_enter* ctx){
SEC("tracepoint/raw_syscalls/sys_enter")//进入系统调用
int tracepoint__syscalls__sys_enter(struct trace_event_raw_sys_enter *args){
u64 start_time = bpf_ktime_get_ns()/1000;//ms
pid_t pid = bpf_get_current_pid_tgid() >> 32;//获取到当前进程的pid
bpf_map_update_elem(&procStartTime,&pid,&start_time,BPF_ANY);
return 0;
pid_t pid = bpf_get_current_pid_tgid();//获取到当前进程的pid
u64 syscall_id = (u64)args->id;

//bpf_printk("ID:%ld\n",syscall_id);
bpf_map_update_elem(&Events,&pid,&syscall_id,BPF_ANY);
bpf_map_update_elem(&SyscallEnterTime,&pid,&start_time,BPF_ANY);
return 0;
}

SEC("tracepoint/syscalls/sys_exit_execve")//退出系统调用
int tracepoint__syscalls__sys_exit_execve(struct trace_event_raw_sys_exit* ctx){
SEC("tracepoint/raw_syscalls/sys_exit")//退出系统调用
int tracepoint__syscalls__sys_exit(struct trace_event_raw_sys_exit *args){
u64 exit_time = bpf_ktime_get_ns()/1000;//ms
pid_t pid = bpf_get_current_pid_tgid() ;//获取到当前进程的pid
u64 syscall_id;
u64 start_time, delay;
pid_t pid = bpf_get_current_pid_tgid() >> 32;//获取到当前进程的pid
u64 *val = bpf_map_lookup_elem(&procStartTime, &pid);

u64 *val = bpf_map_lookup_elem(&SyscallEnterTime, &pid);
if(val !=0){
start_time = *val;
delay = exit_time - start_time;
bpf_map_delete_elem(&procStartTime, &pid);
bpf_map_delete_elem(&SyscallEnterTime, &pid);
}else{
return 0;
}

u64 *val2 = bpf_map_lookup_elem(&Events, &pid);
if(val2 !=0){
syscall_id = *val2;
bpf_map_delete_elem(&SyscallEnterTime, &pid);
}else{
return 0;
}

struct event2 *e;

struct syscall_events *e;
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
if (!e) return 0;

e->start_time=start_time;//开始时间
e->exit_time=exit_time;//结束时间
e->delay=delay;//时间间隔
if (!e) return 0;

e->pid = pid;
e->delay = delay;
bpf_get_current_comm(&e->comm, sizeof(e->comm));
/* 成功地将其提交到用户空间进行后期处理 */
e->syscall_id = syscall_id;

bpf_ringbuf_submit(e, 0);



return 0;
}
}

0 comments on commit e65fc60

Please sign in to comment.