为什么在容器内部调用需要更多的执行时间?
我们正面临一个问题:系统调用在容器内部执行的时间比在外部容器时要多。
Docker容器设置:
BASE OS Container : Ubuntu Container image CAPS ALLOW : sys_resource sys_nice sys_tty_config ipc_lock
主机操作系统:
RHEL 7.3
在集装箱外面的时间:
时间./a.out
for循环花了8.690000秒执行
for循环花了9.170000秒执行
real 0m9.188s user 0m1.768s sys 0m7.419s
时间在容器里面:
for循环花了64.123517秒执行
for循环花了64.316575秒执行
real 1m4.503s user 0m1.449s sys 1m2.870s
我们运行下面的一段代码来检查系统调用的执行时间
int main() { int key,share_id,num; char *data; int semid; int count= 0; struct sembuf sb={0,-1,0}; clock_t t; t = clock(); if(!fork()) { //Child Porcess for (int i= 0 ;i < 50; i++){ for (int idx =0; idx < 1000000 ; idx++){ sb.sem_op=-1; //Lock semop(share_id,(struct sembuf *)&sb,1); count++; sb.sem_op=1;//Unlock semop(share_id,(struct sembuf *)&sb,1); } } } else { //Parent Process for (int i= 0 ;i < 50; i++){ for (int idx=0; idx < 1000000; idx++){ sb.sem_op=-1; //Lock semop(share_id,(struct sembuf *)&sb,1); count++; sb.sem_op=1;//Unlock semop(share_id,(struct sembuf *)&sb,1); } } } t = clock() - t; double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds printf(" for loop took %f seconds to execute \n", time_taken); return 0; }