Skip to content

Commit

Permalink
migrate_image工具采集更多数据
Browse files Browse the repository at this point in the history
  • Loading branch information
albertxu216 committed Jun 21, 2024
1 parent becb393 commit 9eb7a26
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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->count<MAX_MIGRATE
&& (migrate_event->migrate_info + migrate_event->count) < (migrate_event->migrate_info + MAX_MIGRATE) )
else if(migrate_event->count>0 && migrate_event->count<MAX_MIGRATE)
{
migrate_event->migrate_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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<migrate_event.count;i++){
printf("time_stamp:%llu\t%d->%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;
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,35 @@ 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{
int erro;
pid_t pid;
int prio;
int count,rear;//迁移频率
struct per_migrate migrate_info[MAX_MIGRATE];//该进程每次迁移信息;
//struct per_migrate *migrate_info;//该进程每次迁移信息;
};

0 comments on commit 9eb7a26

Please sign in to comment.