硬件::Linux xxx 5.4.0-65-通用#Ubuntu x86_64 GNU/Linux CPU 1核心环境vwmare
软件线程:POLICY=SCHED_FIFO;参数.SCHED_PRIORITY=90;//最高优先级
unsigned long getUstime()
{
struct timespec start_time;
clock_gettime(CLOCK_MONOTONIC, &start_time);
return start_time.tv_nsec/1000 + start_time.tv_sec*1e6;
}
# if 1
int testclocktime() {
std::cout << "-----test clocktime------"<<std::endl;
unsigned long long start = 0;
unsigned long long end = 0;
const static double LOOP_TIMES = 100000.;
double timesumCall = 0;
for(int j = 0; j < 10; ++j) // loop1 million test;
{
volatile unsigned long long sum = 0;
for(volatile long i= 0; i< LOOP_TIMES; ++i)
{
start = getUstime(); // start time
// test somethings
end = getUstime(); // end time
double now_diff = end - start; //calucate elapsed time
sum += end - start;
if(now_diff > 500) //judge
{
printf("overload 1000 us, now is %fn", now_diff);
}
}
double times= sum / LOOP_TIMES;
timesumCall+=times;
printf("AVG_CYCLE : %lf us n", times);
}
timesumCall = timesumCall/10;
printf("total AVG_CYCLE : %lf us n", timesumCall);
return 0;
}
# endif
输出:
-----testclocktime------
AVG_CYCLE : 1.591390 us
AVG_CYCLE : 1.586720 us
AVG_CYCLE : 1.579920 us
now diff time is 109.000000
now diff time is 104.000000
AVG_CYCLE : 1.580130 us
AVG_CYCLE : 1.581860 us
now diff time is 114.000000
now diff time is 101.000000
now diff time is 104.000000
AVG_CYCLE : 1.584920 us
now diff time is 106.000000
now diff time is 51880.000000
AVG_CYCLE : 2.102190 us
now diff time is 106.000000
AVG_CYCLE : 1.563000 us
AVG_CYCLE : 1.596560 us
AVG_CYCLE : 1.579730 us
total AVG_CYCLE : 1.634642 us
total Maxrdtime : 51880.000000 us
如输出所示,在程序中,正常运行时间约为2us,平均时间在2us以内,但在循环过程中会出现大于100us的时间跳跃。
我已经在执行函数中将线程优先级设置为最高级别,并将线程调度模式设置为FIFO。
为什么会发生这种事?或者这样说,如果我确保在实时线程中执行函数时没有抖动,会怎么样?
1条答案
按热度按时间uyhoqukh1#
为什么会发生这种事?
在Linux、FreeBSD、MacOS和其他通用操作系统中可能会出现抖动。
如果我确保在实时线程中执行函数时没有抖动,会怎么样?
真正确保这一点的唯一方法是使用real time version of Linux。