具体做做法是:
在父子进程之间切换,来计算context switch的时间,于是写了一段代码(Linux only 的程序)
1.通过sched_setaffinity将进程绑定在某一个CPU上(如果是多核的话,比如CPU0),fork出来的子进程也会继承这个属性
2. 通过sched_setscheduler讲进程设置成SCHED_FIFO(这步需要root权限),即实时进程调度,同时,将优先级调到最高(最高优先级可以通过sched_get_priority_max系统调用得到,fork出来的子进程也会继承这个属性,这么做是为了保证CPU0上父进程和子进程优先级最高,父进程切换出来内核调度到的一定是子进程)
3.创建管道,用于在fork之后,父进程和子进程利用管道循环读写n次,读写一个int,用此来进行context switch
4. fork,,父进程先获取当前时间戳(gettimeofday),然后往管道中写,。这时子进程应该能从read中返回,然后子进程写一个int,父进程读,如此循环n次。结束之后,子进程获取当前时间戳。这两个时间戳之差就是大致n*2次的context switch的时间
附上实际的代码:
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sched.h> #include <time.h> #include <sys/time.h> #include <pthread.h> #include <sys/wait.h> int main(int argc, char* argv[]) { int prio_max,data,i,iter_count; int pfd1[2],pfd2[2],time_pipe[2]; pid_t pid,child; struct timeval start,end; struct sched_param prio_param; cpu_set_t cpuset; if( argc > 1 ) iter_count = atoi(argv[1]); else iter_count = 10000; CPU_ZERO(&cpuset); CPU_SET(0,&cpuset); memset(&prio_param,0,sizeof(struct sched_param)); pid = getpid(); if( sched_setaffinity(pid,sizeof(cpu_set_t),&cpuset) < 0 ){ perror("sched_setaffinity"); exit(EXIT_FAILURE); } if( (prio_max = sched_get_priority_max(SCHED_FIFO)) < 0 ){ perror("sched_get_priority_max"); exit(EXIT_FAILURE); } printf("prio_max: %d\n",prio_max); prio_param.sched_priority = prio_max; if( sched_setscheduler(pid,SCHED_FIFO,&prio_param) < 0 ){ perror("sched_setscheduler"); exit(EXIT_FAILURE); } if( pipe(pfd1) < 0 ){ perror("pipe"); exit(EXIT_FAILURE); } if( pipe(pfd2) < 0 ){ perror("pipe"); exit(EXIT_FAILURE); } if( pipe(time_pipe) < 0 ){ perror("pipe"); exit(EXIT_FAILURE); } if( (child = fork()) < 0 ){ perror("fork"); exit(EXIT_FAILURE); }else if( child == 0 ){ int n = sizeof(data); close(pfd1[1]); close(pfd2[0]); close(time_pipe[0]); for( i = 0; i < iter_count; i++ ){ if( read(pfd1[0],&data,sizeof(data)) != n ){ perror("read at child process"); exit(EXIT_FAILURE); } if( write(pfd2[1],&data,sizeof(data)) != n){ perror("write at child process"); exit(EXIT_FAILURE); } } gettimeofday(&end,NULL); n = sizeof(struct timeval); if( write(time_pipe[1],&end,sizeof(struct timeval)) != n ){ perror("write at child process"); exit(EXIT_FAILURE); } close(pfd1[0]); close(pfd2[1]); close(time_pipe[1]); exit(EXIT_SUCCESS); }else{ double switch_time,yield_time; struct timeval yield; int n; close(pfd1[0]); close(pfd2[1]); close(time_pipe[1]); data = 1; n = sizeof(data); gettimeofday(&start,NULL); for( i = 0; i < iter_count;i++){ if( write(pfd1[1],&data,sizeof(data)) != n ){ perror("write at parent process"); exit(EXIT_FAILURE); } if( read(pfd2[0],&data,sizeof(data)) != n ){ perror("write at parent process"); exit(EXIT_FAILURE); } } n = sizeof(struct timeval); if( read(time_pipe[0],&end,sizeof(struct timeval)) != n ){ perror("read at parent"); exit(EXIT_FAILURE); } close(pfd1[1]); close(pfd2[0]); close(time_pipe[0]); wait(NULL); switch_time = ((end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec))/1000.0; printf("context switch between two processes: %0.6lfms\n",switch_time/(iter_count*2)); } return 0; }
实际在一个Intel(R) Xeon(R) CPU E5310 @ 1.60GHz 8核4GB内存的机器上,测出来一次context switch的时间大约在3.706us,所花费的指令数大致是5000~6000条指令的样子,实际的时间应该要比这个快一些吧。不过如果循环的n比较小的话,测出来的误差挺大的,比如iter_count取1,测出来的context switch时间貌似接近100us,相差了近100倍啊啊啊啊