Summary of the use of TIMERFD

Source: Internet
Author: User

TIMERFD is a timer interface that Linux provides for user programs. This interface, which is based on a file descriptor, is timed out by a readable event of the file descriptor, so it can be used for select/poll scenarios. TIMERFD is an excuse to add to the Linux kernel version 2.6.25. TIMERFD, EVENTFD, SIGNALFD with epoll use, you can construct a 0 polling program, but the program does not handle events, the program is blocked. In this case, the program is more power-saving on some mobile devices.
The Clock_gettime function can obtain the system clock, accurate to nanosecond. Libraries need to be specified at compile time:-LRT. You can get two types of events: Clock_realtime: Relative time, from 1970.1.1 to the current time. Changing the system time changes the obtained value. That is, it is in the system time coordinates. Clock_monotonic: In contrast to Clock_realtime, it is based on absolute time, the time it takes to reboot the system to the present time, and the change in system time alignment has no effect.
Timerfd_create: Generates a Timer object that returns the file descriptor associated with it. Receive two incoming parameters, one is Clockid, fill in clock_realtime or clock_monotonic, the parameter meaning ditto. The second can pass the control flag: Tfd_nonblock (non-blocking), tfd_cloexec (same o_cloexec)
Note: The progress of TIMERFD is higher than usleep.
Timerfd_settime: Ability to start and stop timers; You can set a second parameter: flags,0 is a relative timer, Tfd_timer_abstime is an absolute timer. The third parameter sets the timeout time, and if 0 indicates the stop timer. Timer Set Timeout method: 1, set timeout time is required to call Clock_gettime to get the current time, if it is an absolute timer, then need to get clock_realtime, in addition to the time to timeout. If it's a relative timer, get clock_monotonic time.                2, data structure: struct TIMESPEC {time_t tv_sec; * Seconds * *
Long tv_nsec; * nanoseconds * *
};

struct Itimerspec {
struct Timespec it_interval; /* Interval for periodic timer */
struct Timespec it_value; /* Initial Expiration * *
}; It_value is the first timeout, the time required to fill out from Clock_gettime, plus the time to timeout.      It_interval is the subsequent periodic timeout and how much time is required.            Pay attention to a place that is easy to make mistakes: tv_nsec plus go after must determine whether to exceed 1000000000 (if more than a second plus one), or set a failure.      A it_interval of not 0 indicates a periodic timer. Both It_value and It_interval are 0 to stop the timer.
Note: The first parameter of Timerfd_create and Clock_gettime is the second parameter of Clock_realtime or clock_monotonic,timerfd_settime is 0 (relative timer) or TFD _timer_abstime, the relationship between the three: 1, if the timerfd_settime is set to Tfd_timer_abstime (determines the time), then the time after must be obtained by Clock_gettime, and set clock_ when getting Realtime or clock_monotonic depends on the value of the Timerfd_create setting.     2, if the Timerfd_settime set to 0 (relative timer), then the time must be followed by relative time, that is: New_value.it_value.tv_nsec = 500000000;     New_value.it_value.tv_sec = 3;     new_value.it_interval.tv_sec = 0; New_value.it_interval.tv_nsec = 10000000;
The read function reads Timerfd, reads as uint_64, and indicates the number of timeouts.
TIMERFD Simple performance test: Apply for 1000 timers, super time positioning 1s, timeout once per second, found CPU occupancy rate in 3.0G CPU on about 1%, 10,000 timers and then 7%, and will not occur at the same time timeout of two cases, if there is printf to the foreground , the timer timeout is generally repeated (3-5) before callback.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.