From 9eb7a26dd1fab3b7bf0736262242438a115ca87c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=99=97=E5=8D=9A?= Date: Fri, 21 Jun 2024 18:39:35 +0800 Subject: [PATCH] =?UTF-8?q?migrate=5Fimage=E5=B7=A5=E5=85=B7=E9=87=87?= =?UTF-8?q?=E9=9B=86=E6=9B=B4=E5=A4=9A=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eBPF_proc_image/tools/migrate_image.bpf.c | 78 ++++++++++++------- .../eBPF_proc_image/tools/migrate_image.c | 46 ++++++----- .../eBPF_proc_image/tools/migrate_image.h | 21 ++++- 3 files changed, 93 insertions(+), 52 deletions(-) diff --git a/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.bpf.c b/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.bpf.c index 886481e22..538c0bf83 100644 --- a/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.bpf.c +++ b/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.bpf.c @@ -25,55 +25,73 @@ struct { __uint(type, BPF_MAP_TYPE_HASH); __uint(key_size, sizeof(pid_t)); __uint(value_size, sizeof(struct migrate_event)); - __uint(max_entries, 128); + __uint(max_entries, 1024); } migrate SEC(".maps"); + struct { - __uint(type, BPF_MAP_TYPE_ARRAY); - __uint(key_size, sizeof(int)); - __uint(value_size, sizeof(int)); - __uint(max_entries, 16); -} t SEC(".maps"); + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(struct minfo_key)); + __uint(value_size, sizeof(struct per_migrate)); + __uint(max_entries, 1024); +} migrate_info SEC(".maps"); + + SEC("tracepoint/sched/sched_migrate_task") int tracepoint_sched_migrate_task(struct trace_event_raw_sched_migrate_task *args){ u64 time = bpf_ktime_get_ns();//当前转移时间点; pid_t pid = args->pid; struct migrate_event *migrate_event; + struct task_struct *task = (struct task_struct *)bpf_get_current_task(); migrate_event = bpf_map_lookup_elem(&migrate,&pid); if(!migrate_event){ - int key = 0,*count=bpf_map_lookup_elem(&t,&key); - if(!count){ - int init = 1; - bpf_map_update_elem(&t,&key,&init,BPF_ANY); - } - else *count +=1; - struct migrate_event migrate_event = {}; + struct per_migrate per_migrate = {}; + struct minfo_key mkey = {}; + mkey.pid = pid; + mkey.count = 1; migrate_event.pid = pid; migrate_event.prio = args->prio; - migrate_event.migrate_info[0].time = time; - migrate_event.migrate_info[0].orig_cpu = args->orig_cpu; - migrate_event.migrate_info[0].dest_cpu = args->dest_cpu; migrate_event.count = 1; + migrate_event.rear = 1; + per_migrate.time = time; + per_migrate.orig_cpu = args->orig_cpu; + per_migrate.dest_cpu = args->dest_cpu; + bpf_printk("load_avg:%llu\tutil_avg:%llu\n",BPF_CORE_READ(task,se.avg.load_avg),BPF_CORE_READ(task,se.avg.util_avg)); + per_migrate.pload_avg = BPF_CORE_READ(task,se.avg.load_avg);//进程的量化负载; + per_migrate.putil_avg = BPF_CORE_READ(task,se.avg.util_avg);//进程的实际算力; + per_migrate.mem_usage = BPF_CORE_READ(task,mm,total_vm) << PAGE_SHIFT; + + + per_migrate.read_bytes = BPF_CORE_READ(task,ioac.read_bytes); + per_migrate.write_bytes = BPF_CORE_READ(task,ioac.write_bytes); + + per_migrate.context_switches = BPF_CORE_READ(task,nvcsw) + BPF_CORE_READ(task,nivcsw); + // per_migrate.runtime = BPF_CORE_READ(task,se.sum_exec_runtime); + bpf_map_update_elem(&migrate_info, &mkey, &per_migrate, BPF_ANY); bpf_map_update_elem(&migrate, &pid, &migrate_event, BPF_ANY); } /*&& (migrate_event->migrate_info + migrate_event->count) < (migrate_event->migrate_info + MAX_MIGRATE)*/ - else if(migrate_event->count>0 && migrate_event->countmigrate_info + migrate_event->count) < (migrate_event->migrate_info + MAX_MIGRATE) ) + else if(migrate_event->count>0 && migrate_event->countmigrate_info[migrate_event->count].time = time; - migrate_event->migrate_info[migrate_event->count].orig_cpu = args->orig_cpu; - migrate_event->migrate_info[migrate_event->count++].dest_cpu = args->dest_cpu; - } - else if(migrate_event->count>=MAX_MIGRATE) - { - migrate_event->migrate_info[migrate_event->count % MAX_MIGRATE].time = time; - migrate_event->migrate_info[migrate_event->count % MAX_MIGRATE].orig_cpu = args->orig_cpu; - migrate_event->migrate_info[migrate_event->count % MAX_MIGRATE].dest_cpu = args->dest_cpu; + struct per_migrate per_migrate = {}; + struct minfo_key mkey = {}; migrate_event->count++; - migrate_event->rear ++; - } + mkey.pid = pid; + mkey.count = migrate_event->count; + per_migrate.time = time; + per_migrate.orig_cpu = args->orig_cpu; + per_migrate.dest_cpu = args->dest_cpu; + per_migrate.mem_usage = BPF_CORE_READ(task,mm,total_vm) << PAGE_SHIFT; + - //bpf_printk("Time:%llu\tpid:%d\tcomm:%s\tprio:%d\torig_cpu:%d\tdest_cpu:%d\t\n",time,args->pid,args->comm,args->prio,args->orig_cpu,args->dest_cpu); + per_migrate.read_bytes = BPF_CORE_READ(task,ioac.read_bytes); + per_migrate.write_bytes = BPF_CORE_READ(task,ioac.write_bytes); + + per_migrate.context_switches = BPF_CORE_READ(task,nvcsw) + BPF_CORE_READ(task,nivcsw); + // per_migrate.runtime = BPF_CORE_READ(task,se.sum_exec_runtime); + + bpf_map_update_elem(&migrate_info, &mkey, &per_migrate, BPF_ANY); + } return 0; } \ No newline at end of file diff --git a/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.c b/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.c index ae04ea7e7..4df2ebe15 100644 --- a/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.c +++ b/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.c @@ -128,43 +128,49 @@ static int migrate_print(){ struct tm *localTime = localtime(&now);// 将时间转换为本地时间结构 printf("Time: %02d:%02d:%02d\n",localTime->tm_hour, localTime->tm_min, localTime->tm_sec); printf("---------------------------------------------------------------------------------\n"); - int err,migrate_fd =bpf_map__fd(skel->maps.migrate),t_fd =bpf_map__fd(skel->maps.t); - int key =0,count; - err = bpf_map_lookup_elem(t_fd,&key,&count); - if (err < 0) { - fprintf(stderr, "failed to lookup infos: %d\n", err); - return -1; - } - printf("total process %d\n",count); - count = 0; - bpf_map_update_elem(t_fd,&key,&count,BPF_ANY); + int err,migrate_fd =bpf_map__fd(skel->maps.migrate),migrate_info_fd =bpf_map__fd(skel->maps.migrate_info); pid_t lookup_key = -1 ,next_key; struct migrate_event migrate_event; while(!bpf_map_get_next_key(migrate_fd, &lookup_key, &next_key)){//遍历打印hash map err = bpf_map_lookup_elem(migrate_fd,&next_key,&migrate_event); if (err < 0) { - fprintf(stderr, "failed to lookup infos: %d\n", err); + fprintf(stderr, "failed to lookup infos2: %d\n", err); return -1; } - if(migrate_event.count == migrate_event.rear) { + if(migrate_event.count <= migrate_event.rear) { lookup_key = next_key; continue; } u64 last_time_stamp = 0; printf("\npid:%d\tprio:%d\tcount:%d\trear:%d\n",migrate_event.pid,migrate_event.prio,migrate_event.count,migrate_event.rear); - for(int i=migrate_event.rear;i%d\t", - migrate_event.migrate_info[i%MAX_MIGRATE].time,migrate_event.migrate_info[i%MAX_MIGRATE].orig_cpu,migrate_event.migrate_info[i%MAX_MIGRATE].dest_cpu); + for(int i=migrate_event.rear;i<=migrate_event.count;i++){ + struct per_migrate migrate_info; + struct minfo_key mkey; + mkey.pid = migrate_event.pid; + mkey.count = i; + err = bpf_map_lookup_elem(migrate_info_fd,&mkey,&migrate_info); + if (err < 0) { + fprintf(stderr, "failed to lookup infos err %d mkey_pid: %d mkey_count: %d\n", err,mkey.pid,i); + continue; + } + printf("time_stamp:%llu\t%d->%d\tpload_avg:%llu\tputil_avg:%llu\t", + migrate_info.time,migrate_info.orig_cpu,migrate_info.dest_cpu,migrate_info.pload_avg,migrate_info.putil_avg); + printf("mmem_usage:%llu kb\tread:%llu kb\twite:%llu kb\tcontext_switch:%llu\t", + migrate_info.mem_usage/1024,migrate_info.read_bytes/1024,migrate_info.write_bytes/1024, + migrate_info.context_switches); + if(i==migrate_event.rear && last_time_stamp == 0) { - last_time_stamp = migrate_event.migrate_info[i%MAX_MIGRATE].time; + last_time_stamp = migrate_info.time; printf("delay: /\n"); }else{ - printf("delay: %d us\n",(migrate_event.migrate_info[i%MAX_MIGRATE].time - last_time_stamp)/1000); - last_time_stamp = migrate_event.migrate_info[i%MAX_MIGRATE].time; + printf("delay: %d us\n",(migrate_info.time - last_time_stamp)/1000); + last_time_stamp = migrate_info.time; } + bpf_map_delete_elem(migrate_info_fd,&mkey);//删除已经打印了的数据 + } - migrate_event.rear = migrate_event.count; + migrate_event.rear = migrate_event.count + 1; bpf_map_update_elem(migrate_fd,&next_key,&migrate_event,BPF_ANY); lookup_key = next_key; } @@ -235,7 +241,7 @@ int main(int argc, char **argv) break; } if (err < 0) { - printf("Error polling perf buffer: %d\n", err); + printf("Error: %d\n", err); break; } } diff --git a/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.h b/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.h index 0bb280c34..c89aaecc1 100644 --- a/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.h +++ b/eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/tools/migrate_image.h @@ -6,12 +6,29 @@ typedef unsigned int u32; /*----------------------------------------------*/ /* migrate_event结构体 */ /*----------------------------------------------*/ -#define MAX_MIGRATE 16 +#define MAX_MIGRATE 1024 +#define PAGE_SHIFT 13 // #define ARRY_OVERFLOW -1 +struct minfo_key{ + pid_t pid; + int count; +}; struct per_migrate{//每次迁移,记录该次迁移信息; u64 time; u32 orig_cpu; u32 dest_cpu; + u64 orig_cpu_load; + u64 dest_cpu_load; + u64 pload_avg; + u64 putil_avg; + int on_cpu; + u64 mem_usage; + u64 read_bytes; + u64 write_bytes; + // u64 syscr; + // u64 syscw; + u64 context_switches; + u64 runtime; }; //每个进程的迁移信息; struct migrate_event{ @@ -19,5 +36,5 @@ struct migrate_event{ pid_t pid; int prio; int count,rear;//迁移频率 - struct per_migrate migrate_info[MAX_MIGRATE];//该进程每次迁移信息; + //struct per_migrate *migrate_info;//该进程每次迁移信息; };