[Linux kernel] [Linux]linux Core Time Management Basics

Source: Internet
Author: User

One, Linux time management basics
http://blog.csdn.net/droidphone/article/details/7975694
http://blog.csdn.net/smilingjames/article/details/6205540
Linux All-time basis is based on low-level hardware, low-level hardware has GPT and CPU local timer, such as the GPT clock source for the 13M hzlinux low-level time architecture is divided into the clock Source,clock event Device,clock The source layer is divided into xtimer and Hrtimer,xtimer mainly refers to the wall time (when booting from the RTC Register to read the wall time), the Hrtimer is mainly high-precision timers, precision can be to the NS level, Clock event Device provides the concept of jiffies and time wheel to the upper layer, such as the minimum precision of process switching is 10ms.

struct Clocksource, which defines the basic properties and behavior of a clock device, which are generally counted, timed, and interrupt capable, such as GPT
The main function of the struct Clock_event_device clock event is to distribute clock events and set the next trigger condition. The clock interrupt is generated periodically, known as jiffies and Hz, before it is no clock event.

II: The concept of jiffies and Hz
The size of the Hz on the arm system is generally 100, indicating that there are 100 beats in 1s, jiffies represents the total number of Beats since the start of the system, Jiffies is generally unsigned long type, so it may overflow. For example: unsigned long jiffies; unsigned long timeout=jiffies+hz/2; the 0.5s of the future.
Jiffies wrap-Around problem:

[CPP]View Plaincopy
    1. 01.unsigned long jiffies;
    2. 02.unsigned Long timeout = jiffies + hz/2;
    3. The.//...
    4. if (Timeout > jiffies) {
    5. //Not timed out, very good
    6. 06.}
    7. .else {
    8. A. //Timeout, error occurred
    9. 09.}

Where Jiffies is a constantly increasing unsigned long,timeout can be seen as a unsigned long than jiffies "not much." Overflow occurs when jiffies becomes larger than 2^32-1, "wrap around" around 0.
At this point, the judgment statement is true, although it actually timed out, but was judged not to have timed out.
The Linux kernel provides a set of macros to solve this problem. where macro Time_after (A, B) is considered a possible overflow condition after determining whether time A is after time B (i.e. "B < a").

[CPP]View Plaincopy
      1. #define Time_after (A, B) ((long) (b)-(Long) (a) < 0)

[Linux kernel] [Linux]linux Core Time Management Basics

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.