【转】nanosleep的精度与调度算法的关系 来自:bean.blog.chinaunix.net

分类: LINUX

    Heartwork前辈在我前一篇博文多线程条件下的计数器(2)中的回复中提到,
 
nanosleep的问题很好解释,看这里……

The nanosleep() function shall cause the current thread to be suspended from execution until either the time interval specified by the rqtp argument has elapsed or a signal is delivered to the calling thread, and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested because the argument value is rounded up to an integer multiple of the sleep resolution or because of the scheduling of other activity by the system. But, except for the case of being interrupted by a signal, the suspension time shall not be less than the time specified by rqtp, as measured by the system clock CLOCK_REALTIME.

The use of the nanosleep() function has no effect on the action or blockage of any signal.

简单来说,这个误差是与调度器的时间片和调度策略有关的,也就是可以通过减小时间片大小和使用实时性更好的调度策略(比如SCHED_RR)来获得更小的分辨率。

 
下来我写了段测试代码,对默认情况和设置调度算法后的nanosleep做了测量。
 
  • #include<stdio.h>
  • #include<stdlib.h>
  • #include<unistd.h>
  • #include<sys/time.h>
  • #include<sched.h>
  • #define COUNT 10000
  • #define MILLION 1000000L
  • #define NANOSECOND 1000
  • int main(int argc,char* argv[])
  • {
  • int i;
  • struct timespec sleeptm;
  • long interval;
  • struct timeval tend,tstart;
  • struct sched_param param;
  • if(argc != 2)
  • {
  • fprintf(stderr,"usage:./test sched_method\n");
  • return -1;
  • }
  • int sched = atoi(argv[1]);
  • param.sched_priority = 1;
  • sched_setscheduler(getpid(),sched,&param);
  • int scheduler = sched_getscheduler(getpid());
  • fprintf(stderr,"default scheduler is %d\n",scheduler);
  • sleeptm.tv_sec = 0;
  • sleeptm.tv_nsec = NANOSECOND;
  • if(gettimeofday(&tstart,NULL)!=0)
  • {
  • fprintf(stderr,"get start time failed \n");
  • return -2;
  • }
  • for(i = 0;i<COUNT;i++)
  • {
  • if(nanosleep(&sleeptm,NULL) != 0)
  • {
  • fprintf(stderr,"the %d sleep failed\n",i);
  • return -3;
  • }
  • }
  • if(gettimeofday(&tend,NULL)!=0)
  • {
  • fprintf(stderr,"get end time failed \n");
  • return -4;
  • }
  • interval = MILLION*(tend.tv_sec - tstart.tv_sec)
  • +(tend.tv_usec-tstart.tv_usec);
  • fprintf(stderr,"the expected time is %d us,but real                                   time cost is %lu us\n",COUNT,interval);
  • return 0;
  • }
  • root@libin:~/program/C/timer# ./test 0
  • default scheduler is 0
  • the expected time is 10000 us,but real time cost is 630624 us
  • root@libin:~/program/C/timer# ./test 1
  • default scheduler is 1
  • the expected time is 10000 us,but real time cost is 67252 us
  • root@libin:~/program/C/timer# ./test 2
  • default scheduler is 2
  • the expected time is 10000 us,but real time cost is 82449 us
 
所谓调度算法值 0 1  2 分别是什么呢?bit/sched.h中定义:
 
  • #define SCHED_OTHER 0
  • #define SCHED_FIFO 1
  • #define SCHED_RR 2
  • #ifdef __USE_GNU
  • # define SCHED_BATCH 3
  • #endif
从结果上看,采用默认的算法是SCHED_OTHER nanosleep是最不准确的,理想情况下,每次睡1us,睡10000次,耗时因该是10ms,实际上耗时是630ms。采用时间片轮转的SCHED_RR nanosleep的计时 要准确一些,耗时为82ms.依次类推。
 
 
至于调度算法如何影响nanosleep,我还不是很清楚,里面还有很多东西需要我继续学习。好久没写博客了,先将这篇不成熟的博文拿出,抛砖引玉。
 
参考文献:
1 UNIX 系统编程
2 Heartwork的回复
上一篇:IO调度算法


下一篇:摘:数据结构各种算法实现(C++模板)