Linux hrtimer analysis-high-precision mode not configured

Source: Internet
Author: User

From: http://blog.csdn.net/walkingman321/article/details/6133171

This article analyzes the implementation of hrtimer in linux2.6.29.

 

In linux2.6, a new timer hrtimer is implemented. Unlike the traditional timer's time wheel algorithm, hrtimer uses the red-black tree algorithm. Hrtimer can be configured with high precision and common precision. The implementation of hrtimer in a single CPU system and multiple CPU Systems is also different. Here, we will analyze the simplest configuration into a general precision, single CPU. For more information about how to configure high accuracy, see the subsequent articles.

1. Definition of clock source

To implement hrtimer, Linux defines an hrtimer_cpu_base for each CPU in the system. The struct is defined as follows:

Struct functions {functions; struct functions [functions]; // clock source # define functions 2 # ifdef functions; inthres_active; functions; unsigned longnr_events; unsigned longnr_retries; unsigned longnr_hangs; functions; # endif };

 

struct hrtimer_clock_base {struct hrtimer_cpu_base*cpu_base;clockid_tindex;struct rb_rootactive;struct rb_node*first;ktime_tresolution;ktime_t(*get_time)(void);ktime_tsoftirq_time;#ifdef CONFIG_HIGH_RES_TIMERSktime_toffset;#endif};

In hrtimer. C, hrtimer_cpu_base code is defined for each CPU:

DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) ={.clock_base ={{.index = CLOCK_REALTIME,.get_time = &ktime_get_real,.resolution = KTIME_LOW_RES,},{.index = CLOCK_MONOTONIC,.get_time = &ktime_get,.resolution = KTIME_LOW_RES,},}};

1.1 clock source type

We can see that each CPU must define two clock sources: Realtime and monotonic. Realtime indicates the real-time clock, and monotonic indicates the monotonic increasing clock. The difference between the two is that the realtime clock will be affected when the user changes the system time, But monotonic will not be affected. This can be seen from the get_time function pointers. the realtime clock points to ktime_get_real, and monotonic points to ktime_get.

The struct of the clock source is defined as struct hrtimer_clock_base. There are two fields, struct rb_node * First and struct rb_root active, which maintain the red and black trees of hrtimer. That is to say, each hrtimer_clock_base maintains its own red/black tree.

During initialization, hrtimer must be added to the red/black tree of a clock source. The clock source is either realtime or monotonic. This association is implemented through the base field of struct hrtimer.

struct hrtimer {struct rb_nodenode;ktime_t_expires;ktime_t_softexpires;enum hrtimer_restart(*function)(struct hrtimer *);struct hrtimer_clock_base*base;unsigned longstate;#ifdef CONFIG_TIMER_STATSintstart_pid;void*start_site;charstart_comm[16];#endif};

2. Basic hrtimer operations

Linux's traditional timer is implemented by the time wheel algorithm (timer. c), but hrtimer is implemented by the red-black tree algorithm. In struct hrtimer, there is a node field of the type struct rb_node, which represents the location of hrtimer in the red/black tree.

2.1 hrtimer_start

The hrtimer_start function adds an hrtimer to a red/black tree sorted by expiration time. The main process is as follows:

Int hrtimer_start (struct hrtimer * timer, ktime_t Tim, const Enum hrtimer_mode mode );

// Calculate the hrtimer timeout value based on the value of time and mode, and set it to the timer-> expire field.

// Expire sets the absolute time. Therefore, if the value of the mode parameter is hrtimer_mode_rel (that is, the value of the Tim parameter is relative time), you need to change the value of the Tim to the absolute time:

// Expire = Tim + timer-> base-> get_time (). (Note that this article only studies single CPU)

// Call enqueue_hrtimer to add hrtimer to the red/black tree.

 

2.2 hrtimer expiration

In the hrtimer_run_queues function, hrtimer checks whether the execution has expired. The call chain of hrtimer_run_queues <called from hardirq context every Jiffy> is: Linux system clock function-> update_process_times-> run_local_timers-> hrtimer_run_queues.

Void hrtimer_run_queues (void)

// Determine whether the mode is high-precision. If the mode is high-precision, return immediately. This article does not consider this situation for the moment.

// For the red and black trees of every clock source (Realtime and monotonic), check hrtimer in the order of expiration to see if they have expired (compare the timer with softirq_time of the clock source ). If the expiration date is reached, the expired timer will be taken out and the corresponding operation will be performed according to the specific Timer mode:

//If the timer mode is hrtimer_cb_softirq, move the timer to the cb_pending queue of hrtimer_cpu_base.

//Call _ run_hrtimer to execute the timer callback function in _ run_hrtimer.

 

When the high-precision mode is not configured, the timer in the cb_pending queue is executed in the timer_softirq Soft Interrupt. The call chain is

Run_timer_softirq->
Hrtimer_run_pending (called from timer softirq every Jiffy)->

Hrtimer_switch_to_hres->
Tick_init_highres->
Tick_switch_to_oneshot (hrtimer_interrupt)
<Assign hrtimer_interrupt to Dev-> event_handler, that is, Dev-> event_handler = handler;>

 

2.3 hrtimer_cancel

The hrtimer_cancel function is used to delete a timer in the queue. There are three cases: one is that the timer has expired and the soft interrupt mode is set; the other is that the timer has not expired and is still in the red/black tree; the third is that the timer is being executed.

In the first case, the timer is attached to the cb_pending queue of hrtimer_cpu_base, so it needs to be removed from the pending queue.

In the second case, the timer is still in the red/black tree, so it is removed from the red/black tree. Because this article only considers the situation where high precision is not enabled, we will not study the timing when the timer is the first time in the red/black tree (that is, the part of the Code that calls the hrtimer_force_reprogram function ).

In the third case, the deletion fails. The hrtimer_cancel function will retry cyclically and wait until the timer is executed. (This may happen in multiple CPU Systems)

3. The difference between timer and traditional timer when the high-precision mode is not enabled

1) traditional timer uses the time wheel algorithm and hrtimer uses the red-black tree algorithm.

2) traditional timer is executed in Soft Interrupt mode, while hrtimer is executed in hard interrupt mode (update_process_times-> run_local_timers-> hrtimer_run_queues ). If hrtimer sets the hrtimer_cb_softirq mode, the timer will be moved to the pending queue and then executed by the Soft Interrupt.

 

 

 

 

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.