【发布时间】:2020-03-06 11:11:59
【问题描述】:
我正在使用 Linux 定期计时器,特别是 timerfd,我将其设置为定期到期,例如每 200 毫秒。
然而,我注意到,有时计时器似乎比我设置的超时时间早一点。
特别是,我使用以下 C 代码来执行一个简单的测试:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <poll.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/timerfd.h>
#include <sys/time.h>
#define NO_FLAGS_TIMER 0
#define NUM_TESTS 10
// Function to perform the difference between two struct timeval.
// The operation which is performed is out = out - in
static inline int timevalSub(struct timeval *in, struct timeval *out) {
time_t original_out_tv_sec=out->tv_sec;
if (out->tv_usec < in->tv_usec) {
int nsec = (in->tv_usec - out->tv_usec) / 1000000 + 1;
in->tv_usec -= 1000000 * nsec;
in->tv_sec += nsec;
}
if (out->tv_usec - in->tv_usec > 1000000) {
int nsec = (out->tv_usec - in->tv_usec) / 1000000;
in->tv_usec += 1000000 * nsec;
in->tv_sec -= nsec;
}
out->tv_sec-=in->tv_sec;
out->tv_usec-=in->tv_usec;
// '1' is returned when the result is negative
return original_out_tv_sec < in->tv_sec;
}
// Function to create a timerfd and set it with a periodic timeout of 'time_ms', in milliseconds
int timerCreateAndSet(struct pollfd *timerMon,int *clockFd,uint64_t time_ms) {
struct itimerspec new_value;
time_t sec;
long nanosec;
// Create monotonic (increasing) timer
*clockFd=timerfd_create(CLOCK_MONOTONIC,NO_FLAGS_TIMER);
if(*clockFd==-1) {
return -1;
}
// Convert time, in ms, to seconds and nanoseconds
sec=(time_t) ((time_ms)/1000);
nanosec=1000000*time_ms-sec*1000000000;
new_value.it_value.tv_nsec=nanosec;
new_value.it_value.tv_sec=sec;
new_value.it_interval.tv_nsec=nanosec;
new_value.it_interval.tv_sec=sec;
// Fill pollfd structure
timerMon->fd=*clockFd;
timerMon->revents=0;
timerMon->events=POLLIN;
// Start timer
if(timerfd_settime(*clockFd,NO_FLAGS_TIMER,&new_value,NULL)==-1) {
close(*clockFd);
return -2;
}
return 0;
}
int main(void) {
struct timeval tv,tv_prev,tv_curr;
int clockFd;
struct pollfd timerMon;
unsigned long long junk;
gettimeofday(&tv,NULL);
timerCreateAndSet(&timerMon,&clockFd,200); // 200 ms periodic expiration time
tv_prev=tv;
for(int a=0;a<NUM_TESTS;a++) {
// No error check on poll() just for the sake of brevity...
// The final code should contain a check on the return value of poll()
poll(&timerMon,1,-1);
(void) read(clockFd,&junk,sizeof(junk));
gettimeofday(&tv,NULL);
tv_curr=tv;
if(timevalSub(&tv_prev,&tv_curr)) {
fprintf(stdout,"Error! Negative timestamps. The test will be interrupted now.\n");
break;
}
printf("Iteration: %d - curr. timestamp: %lu.%lu - elapsed after %f ms - real est. delta_t %f ms\n",a,tv.tv_sec,tv.tv_usec,200.0,
(tv_curr.tv_sec*1000000+tv_curr.tv_usec)/1000.0);
tv_prev=tv;
}
return 0;
}
用gcc编译后:
gcc -o timertest_stackoverflow timertest_stackoverflow.c
我得到以下输出:
Iteration: 0 - curr. timestamp: 1583491102.833748 - elapsed after 200.000000 ms - real est. delta_t 200.112000 ms
Iteration: 1 - curr. timestamp: 1583491103.33690 - elapsed after 200.000000 ms - real est. delta_t 199.942000 ms
Iteration: 2 - curr. timestamp: 1583491103.233687 - elapsed after 200.000000 ms - real est. delta_t 199.997000 ms
Iteration: 3 - curr. timestamp: 1583491103.433737 - elapsed after 200.000000 ms - real est. delta_t 200.050000 ms
Iteration: 4 - curr. timestamp: 1583491103.633737 - elapsed after 200.000000 ms - real est. delta_t 200.000000 ms
Iteration: 5 - curr. timestamp: 1583491103.833701 - elapsed after 200.000000 ms - real est. delta_t 199.964000 ms
Iteration: 6 - curr. timestamp: 1583491104.33686 - elapsed after 200.000000 ms - real est. delta_t 199.985000 ms
Iteration: 7 - curr. timestamp: 1583491104.233745 - elapsed after 200.000000 ms - real est. delta_t 200.059000 ms
Iteration: 8 - curr. timestamp: 1583491104.433737 - elapsed after 200.000000 ms - real est. delta_t 199.992000 ms
Iteration: 9 - curr. timestamp: 1583491104.633736 - elapsed after 200.000000 ms - real est. delta_t 199.999000 ms
我希望使用gettimeofday() 估计的实际时间差永远不会小于 200 毫秒(也是由于使用 read() 清除事件所需的时间),但也有一些值是小于 200 毫秒,例如 199.942000 ms。
你知道我为什么会观察到这种行为吗?
这可能是因为我正在使用gettimeofday() 并且有时tv_prev 会稍晚一点(由于调用read() 或gettimeofday() 本身时的延迟不同)和@987654332 @,在下一次迭代中,是不是,导致估计时间小于200毫秒,而定时器实际上是精确到每200毫秒到期一次?
非常感谢您。
【问题讨论】:
-
我认为你的猜测是正确的。请注意,timerfd 不会在下一个计时器开始之前等待您
read它。 -
Linux 不是实时操作系统。预计时间是“ish” - 200ish ms。在特殊情况下,我不会感到惊讶,因为它会出现更多。它比 Windows 好太多了。
标签: c linux timer gettimeofday