Realization of precise timer for non-real-time system

Source: Internet
Author: User
Tags semaphore

The systems we typically use, whether Linux or Windows, are non-real-time systems. Non-real-time systems can obtain very accurate current time, even by reading some of the CPU's registers to get a CPU cycle count clock, it is estimated that in addition to the GPS system is sufficient to meet our daily can meet the application, but the non-real-time system does not provide accurate timer implementation. If the system timer is used in Linux or Windows, although we can create a timer with a precision of 1ms or even smaller, the actual execution can be found that the timer will always require a few MS or even more than 10 ms to be dispatched once, rather than achieving the desired accuracy.

Typically 10ms levels of timers can meet the needs of the application, but some special scenes of 10ms is too long, such as I have encountered a scene, on a one-way network to drive data to a device, push rate of about 100Mbps magnitude. If every 10ms push, then each push of the amount of data is about 1MB, but the receiving device is older, not so large receive buffer, it is easy to cause loss of packets, and one-way network is no way to carry out data retransmission, if we reduce the amount of push data, Then the overall transmission efficiency will be reduced correspondingly. Therefore, the only feasible way is that we push the data as smoothly as possible based on the IO size of the receiving device, and we need a finer clock to control the push behavior.

For Linux systems, in theory we can recompile the kernel and reduce the call interval of the system clock, but this is too loud, it can affect the operation of many system modules, there is a risk, we still expect to be able to solve the problem at the application layer. After trying, we finally found a solution to the problem:

Thanks to CPU vendors for providing us with multicore systems, we sacrifice a kernel to run a busy waiting check thread on this core, which is the function of constantly reading the system time in a dead loop and notifying the push thread (can be multiple) if the push can be initiated. The push thread usually waits on a semaphore, and when the time expires, the thread is checked to set the semaphore, and the push thread is activated and pushed. In this way, we can implement a timer with almost any precision.

The disadvantage of this scenario is that there is a CPU core wasted, fortunately the server generally has 4, 8 or even 16 cores, the cost of wasting a nuclear is acceptable.

Note: We have patented the program and have been granted the patent name: A method of precise network speed limit on a non-real-time system, the authorization number cn102368729b.

Realization of precise timer for non-real-time system

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.