Skip to content

Commit

Permalink
add cswtch && runqlen
Browse files Browse the repository at this point in the history
  • Loading branch information
albertxu216 committed Nov 3, 2023
1 parent 538a555 commit 120a990
Show file tree
Hide file tree
Showing 26 changed files with 434,993 additions and 43 deletions.
2 changes: 1 addition & 1 deletion eBPF_Supermarket/CPU_Subsystem/BCC_sar/src/sar/sar.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct rq {
unsigned int has_blocked_load;
};

struct curState {
struct curState {//当前进程状态
long int task_state;
u32 task_pid;
u32 pad;
Expand Down
11 changes: 8 additions & 3 deletions eBPF_Supermarket/CPU_Subsystem/BCC_sar/src/sar/sar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from bpfutil import colorize

def parse_arg():
#使用argparse库来解析命令行参数。它定义了一些命令行选项,如输出间隔、显示方式、输出计数以及需要附加到的进程等,然后返回这些参数的值。
parser = argparse.ArgumentParser(description="CPU subsystem parameter display")
parser.add_argument("-i", "--interval", default=1, type=int,
help="interval between two output, in second")
Expand All @@ -20,7 +21,7 @@ def parse_arg():
return parser.parse_args()

def attach_probe():
# load BPF program
# load BPF program 一些BPF probe(探针)来监视系统事件,有对应的
bpf = BPF(src_file="sar.bpf.c")
bpf.attach_tracepoint(tp="sched:sched_switch", fn_name="trace_sched_switch")
bpf.attach_tracepoint(tp="irq:softirq_entry", fn_name="trace_softirq_entry")
Expand Down Expand Up @@ -104,6 +105,7 @@ def exit_app(signum, frame):
_line = line = 0

while 1:
##检查命令行参数 args.interval 是否大于0,如果是,则等待指定的时间间隔;否则,默认等待1秒。
if args.interval > 0:
time.sleep(args.interval)
else:
Expand All @@ -112,11 +114,14 @@ def exit_app(signum, frame):
# 上下文切换数
# bpf['countMap'][0] 的类型是 ctypes.c_ulong,要得到其值需要.value
cswch_s = bpf["countMap"][0].value
cswch_s, cswch = cswch_s - cswch, cswch_s
cswch_s, cswch = cswch_s - cswch, cswch_s
#cswch_s=cswch_s - cswch;
#cswch=cswch_s(原始值)

# 每秒新建进程数(fork)
proc_s = bpf["countMap"][1].value
proc_s, proc = proc_s - proc, proc_s
#逻辑同上;

# 运行队列长度
runqlen_list = bpf['runqlen'][0]
Expand Down Expand Up @@ -200,7 +205,7 @@ def exit_app(signum, frame):
bpfCount = bpf["countMap"][2].value
dtaBpfCount = bpfCount - _bpfCount

# 按照类型输出信息
# 按照类型输出信息
if args.type == "time": # 输出时间型
print("%8s %6d %7d %7d %10d %10d %7d %10d %7d %8d %6d %6d" %
(timeStr, proc_s, cswch_s, runqlen, dtaIrq / 1000, dtaSoft / 1000,
Expand Down
15 changes: 8 additions & 7 deletions eBPF_Supermarket/CPU_Subsystem/cs_delay/BCC_cs_delay/cs_delay.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <uapi/linux/ptrace.h>

BPF_ARRAY(start, u64,1);
BPF_HISTOGRAM(dist);
BPF_ARRAY(start, u64,1);//定义了一个名为start的ebpf数组,元素类型为u64,数组大小为1;存储进入内核函数的时间戳
BPF_HISTOGRAM(dist);//定义了一个直方图,名为dist;用于存储不同时间间隔的计数

int do_entry(struct pt_regs *ctx) //pt_regs结构定义了在系统调用或其他内核条目期间将寄存器存储在内核堆栈上的方式
int do_entry(struct pt_regs *ctx) //pt_regs结构定义了在系统调用或其他内核条目期间,将寄存器存储在内核堆栈上的方式
{
u64 t1= bpf_ktime_get_ns()/1000; //bpf_ktime_get_ns返回自系统启动以来所经过的时间(以纳秒为单位)。不包括系统挂起的时间
u64 t1= bpf_ktime_get_ns()/1000;//bpf_ktime_get_ns返回自系统启动以来所经过的时间(以纳秒为单位)。不包括系统挂起的时间
int key=0;
start.update(&key,&t1);
start.update(&key,&t1);//记录开始时间;

return 0;
}

int do_return(struct pt_regs *ctx)
{
u64 t2= bpf_ktime_get_ns()/1000;
u64 t2= bpf_ktime_get_ns()/1000;//当前时间;
u64 *tsp, delay;

int key=0;
Expand All @@ -24,7 +24,8 @@ int do_return(struct pt_regs *ctx)
{
delay = t2 - *tsp;
start.delete(&key);
dist.increment(bpf_log2l(delay));
dist.increment(bpf_log2l(delay));//将时间间隔添加到dist直方图中
//使用bpf_log2l()来确定时间间隔的对数值,以便记录不同时间间隔的计数。
}

return 0;
Expand Down
26 changes: 26 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/cs_delay/libbpf_cs_delay/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
| sed 's/arm.*/arm/' \
| sed 's/aarch64/arm64/' \
| sed 's/ppc64le/powerpc/' \
| sed 's/mips.*/mips/' \
| sed 's/riscv64/riscv/' \
| sed 's/loongarch64/loongarch/')
APP = cs_delay

ifeq ($(wildcard ./vmlinux.h),)
all:
bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
clang -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) -I/usr/include/x86_64-linux-gnu -I. -c $(APP).bpf.c -o $(APP).bpf.o
bpftool gen skeleton $(APP).bpf.o > $(APP).skel.h
clang -g -O2 -Wall -I . -c $(APP).c -o $(APP).o
clang -Wall -O2 -g $(APP).o -static -lbpf -lelf -lz -o $(APP)
endif

other:
clang -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) -I/usr/include/x86_64-linux-gnu -I. -c $(APP).bpf.c -o $(APP).bpf.o
bpftool gen skeleton $(APP).bpf.o > $(APP).skel.h
clang -g -O2 -Wall -I . -c $(APP).c -o $(APP).o
clang -Wall -O2 -g $(APP).o -static -lbpf -lelf -lz -o $(APP)

clean:
rm *.o *.skel.h $(APP)
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h> //包含了BPF 辅助函数
#include <bpf/bpf_helpers.h> //包含了BPF 辅助函数
#include <bpf/bpf_tracing.h>
#include "cs_delay.h"

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

// 定义数组映射
// 定义数组映射
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, int);
__type(value, u64);
} start SEC(".maps");
} start SEC(".maps");//记录时间戳;

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

SEC("kprobe/schedule")
int BPF_KPROBE(schedule)
{
u64 t1;
t1 = bpf_ktime_get_ns()/1000; //bpf_ktime_get_ns返回自系统启动以来所经过的时间(以纳秒为单位)。不包括系统挂起的时间。
int key=0;
t1 = bpf_ktime_get_ns()/1000; //bpf_ktime_get_ns返回自系统启动以来所经过的时间(以纳秒为单位)。不包括系统挂起的时间。
int key =0;
bpf_map_update_elem(&start,&key,&t1,BPF_ANY);

return 0;
Expand All @@ -40,20 +40,21 @@ int BPF_KRETPROBE(schedule_exit)
{
t1 = *val;
delay = t2 - t1;
bpf_map_delete_elem(&start, &key);
}else{
return 0;
}
bpf_map_delete_elem(&start, &key);


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

e->t1=t1;
e->t2=t2;
e->delay=delay;
e->t1=t1;//开始时间
e->t2=t2;//结束时间
e->delay=delay;//时间间隔

/* 成功地将其提交到用户空间进行后期处理 */
/* 成功地将其提交到用户空间进行后期处理 */
bpf_ringbuf_submit(e, 0);

return 0;
Expand Down
Binary file not shown.
68 changes: 66 additions & 2 deletions eBPF_Supermarket/CPU_Subsystem/cs_delay/libbpf_cs_delay/cs_delay.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <stdio.h>
#include <math.h>//用于对数运算
#include <unistd.h>
#include <sys/resource.h>
#include <bpf/libbpf.h>
Expand All @@ -13,20 +14,80 @@

static volatile bool exiting = false;

int count[25]={0};//定义一个count数组,用于汇总schedul()调度时间,以log2(时间间隔)为统计依据;

static void sig_handler(int sig)
{
exiting = true;
}


static int handle_event(void *ctx, void *data,unsigned long data_sz)
{
const struct event *e = data;
printf("t1:%lu t2:%lu delay:%lu\n",e->t1,e->t2,e->delay);


int dly=(int)(e->delay),i=0;
while (dly > 1){
dly /= 2;
i ++;
}
count[i]++;//记录时间间隔次数;
return 0;
}
static int print_hstgram(int i,int max,int per_len)
{
int cnt=count[i];
if(per_len==1){
while(cnt>0){//打印
printf("*");
cnt--;
}
}
while(cnt-per_len>=0){//打印
printf("*");
cnt-=per_len;
}
printf("\n");
return per_len;
}
static void histogram()
{
int log10[15]={0},max=0,per_len=1;
for(int i=0;i<10;i++){//log10(count[i]);
int tmp=count[i],cnt=0;
while (tmp >= 10){
tmp /= 10;
cnt ++;//幂次
}
log10[cnt]++;
}

for(int i=0;i<10;i++){//找log10里的最大值;
if(max<log10[i])
max=i;
}

while(max>0){//pow(10,max);
per_len *=10 ;
max--;
}

printf("\n%-24s \t%-12s \t%-12s \n","cs_delay","Count","Distribution");
printf("%d\t=>\t%-8d \t%-12d \t|",0,1,count[0]);
print_hstgram(0,max,per_len);
printf("%d\t=>\t%-8d \t%-12d \t|",2,3,count[1]);
print_hstgram(1,max,per_len);
for(int i=2;i<20;i++){
printf("%d\t=>\t%-8d \t%-12d \t|",(int)pow(2,i),(int)pow(2,(i+1))-1,count[i]);
print_hstgram(i,max,per_len);
}
printf("per_len = %d\n",per_len);
}




static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
return vfprintf(stderr, format, args);
Expand Down Expand Up @@ -94,7 +155,10 @@ int main(int argc, char **argv)
exiting = true; //使用该程序时,将该行代码注释掉

}

/*睡眠*/
//sleep(99999999);
/*打印直方图*/
histogram();
/* 卸载BPF程序 */
cleanup:
ring_buffer__free(rb);
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion eBPF_Supermarket/CPU_Subsystem/libbpf
Loading

0 comments on commit 120a990

Please sign in to comment.