Shell's Home

Nov 6, 2014 - 1 minute read - Comments

context切换测试——C语言协程有关部分请求review

setjmp/longjmp测试

使用s_jmp程序来测试setjmp的性能。1G次循环。下面是结论:

5.77,0.00,29,0,0,0,99%
5.70,0.00,30,0,0,0,99%
5.71,0.00,22,0,0,0,99%
5.71,0.00,23,0,0,0,99%
5.70,0.00,30,0,0,0,100%
5.70,0.00,23,0,0,0,99%

统计结果如下:

  • time mean = 5.715
  • time var = 0.000625

单次调度开销只有5.7ns,在所有测试中性能最优。(glibc-2.19/sysdeps/x86_64/setjmp.S)

getcontext/setcontext测试

使用s_context程序来测试setcontext的性能。100M次循环。下面是结论:

12.96,5.88,79,0,0,0,99%
13.13,5.94,105,0,0,0,99%
12.95,6.18,57,0,0,0,99%
13.13,5.90,64,0,0,0,99%
12.95,5.88,82,0,0,0,99%
12.96,5.80,51,0,0,0,99%

统计结果如下:

  • time mean = 13.01
  • time var = 0.0068

单次调度开销高达130ns,仅比系统的sched在高线程下略快。这事很奇怪,因为根据我看到的源码(glibc-2.19/sysdeps/unix/sysv/linux/x86_64/setcontext.S),getcontext/setcontext在glibc中是用汇编实现的。其中陷入内核只是为了设定signal mask。