Linux hrtimer analysis-configure high-precision mode

Source: Internet
Author: User

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

This article introduces the difference between setting the hrtimer in high precision mode and setting the hrtimer in linux2.6.29. This article does not consider the impact of high-precision mode on clock interruption in Linux.

 

When the high-precision mode is not configured, The hrtimer timeout is checked in the cycle of the system clock interruption. Therefore, the timing precision (Jiffy) of the hrtimer is measured in the cycle interval, the accuracy is the same as that of the traditional time wheel timer.

After configuring the high-precision mode, the hrtimer timeout is completed by the timeout interrupt of struct clock_event_device. Clock_event_device generally describes a hardware timer. Its timing accuracy is determined by the hardware timer. For example, the timer parameter on my board is timer at Vir: 0xe0100200 = PHY: 0xe0100200,
Using IRQ: 27, at freq: 250000000, it can be seen that its precision is high: 250 MHz, which is also the origin of the high-precision clock name.

 

struct clock_event_device {const char*name;unsigned intfeatures;u64max_delta_ns;u64min_delta_ns;u32mult;u32shift;intrating;intirq;const struct cpumask*cpumask;int(*set_next_event)(unsigned long evt,  struct clock_event_device *);void(*set_mode)(enum clock_event_mode mode,    struct clock_event_device *);void(*event_handler)(struct clock_event_device *);void(*broadcast)(const struct cpumask *mask);struct list_headlist;enum clock_event_modemode;ktime_tnext_event;unsigned longretries;};

 

The following describes the implementation details of hrtimer after the high-precision mode is configured.

1. Add hrtimer

After configuring the high-precision mode, adding the hrtimer is also completed by the hrtimer_start function, which is the same as when the high-precision mode is not configured. However, in high-precision mode, if the added hrtimer is on the leftmost node of the Red-black tree, that is, when the hrtimer to be added is the first hrtimer to expire, then, the sub-function hrtimer_enqueue_reprogram is called to reset the timeout interrupt of clock_event_device.

Hrtimer_enqueue_reprogram

Hrtimer_reprogram

Tick_program_event

Tick_dev_program_event

Clockevents_program_event

Struct clock_event_device * pdev-> set_next_event

For example, clock_event_device of my platform is defined as follows:

static struct clock_event_device timer0_clockevent = {.name= "my_timer_evt",.features= CLOCK_EVT_FEAT_PERIODIC,.set_mode= my_set_mode,.set_next_event= my_set_next_event,.rating= 300,.cpumask= cpu_all_mask,};

Timer_set_next_event is mainly used to set registers related to hardware devices.

2. Delete hrtimer

The changes made when you delete a hrtimer are the same as when you add a hrtimer. You need to consider that the deleted timer is the leftmost node in the red/black tree. The timeout value of clock_event_device is the timeout value of the timer to be deleted.

3. Expiration of hrtimer

3.1 when high-precision mode is not configured

Hrtimer expiration is checked by the hrtimer_run_queues function. Hrtimer_run_queues is called in run_local_timers, while run_local_timers is called in the interrupt of the system clock (Jiffy. It can be seen from this that, like the timer using the time wheel algorithm, hrtimer uses the round robin method in every system clock interruption when the high-precision mode is not configured to determine whether the hrtimer expires, therefore, the timing accuracy here is the time interval at which the clock is interrupted.

However, at the beginning of the hrtimer_run_queues function, a check is executed:

If (hrtimer_hres_active ())

Return;

Therefore, After configuring the high-precision mode, the hrtimer_run_queues function here is equivalent to an empty function and will return directly.

3.2 after the high-precision mode is configured

Hrtimer is called by the clock_event (a hardware device) device's interrupt processing function hrtimer_interrupt. Note that the traditional round robin method is no longer used to determine whether the timer expires. Instead, it sets the delay interruption of clock_event_device and triggers an interruption at the time point of the first expired timer timeout to perform the timeout operation. Therefore, the timing accuracy here is determined by the timing precision of clock_event_device.

4. Soft Interrupt

If the high-precision mode is not configured, if the hrtimer sets a Soft Interrupt flag, the soft interrupt that triggers the timeout processing is timer_softirq. After configuring the high-precision mode, the system assigns a dedicated Soft Interrupt to hrtimer. The Soft Interrupt number is hrtimer_softirq.

 

This article introduces the difference between setting the hrtimer in high precision mode and setting the hrtimer in linux2.6.29. This article does not consider the impact of high-precision mode on clock interruption in Linux.

 

When the high-precision mode is not configured, The hrtimer timeout is checked in the cycle of the system clock interruption. Therefore, the timing precision (Jiffy) of the hrtimer is measured in the cycle interval, the accuracy is the same as that of the traditional time wheel timer.

After configuring the high-precision mode, the hrtimer timeout is completed by the timeout interrupt of struct clock_event_device. Clock_event_device generally describes a hardware timer. Its timing accuracy is determined by the hardware timer. For example, the timer parameter on my board is timer at Vir: 0xe0100200 = PHY: 0xe0100200,
Using IRQ: 27, at freq: 250000000, it can be seen that its precision is high: 250 MHz, which is also the origin of the high-precision clock name.

 

struct clock_event_device {const char*name;unsigned intfeatures;u64max_delta_ns;u64min_delta_ns;u32mult;u32shift;intrating;intirq;const struct cpumask*cpumask;int(*set_next_event)(unsigned long evt,  struct clock_event_device *);void(*set_mode)(enum clock_event_mode mode,    struct clock_event_device *);void(*event_handler)(struct clock_event_device *);void(*broadcast)(const struct cpumask *mask);struct list_headlist;enum clock_event_modemode;ktime_tnext_event;unsigned longretries;};

 

The following describes the implementation details of hrtimer after the high-precision mode is configured.

1. Add hrtimer

After configuring the high-precision mode, adding the hrtimer is also completed by the hrtimer_start function, which is the same as when the high-precision mode is not configured. However, in high-precision mode, if the added hrtimer is on the leftmost node of the Red-black tree, that is, when the hrtimer to be added is the first hrtimer to expire, then, the sub-function hrtimer_enqueue_reprogram is called to reset the timeout interrupt of clock_event_device.

Hrtimer_enqueue_reprogram

Hrtimer_reprogram

Tick_program_event

Tick_dev_program_event

Clockevents_program_event

Struct clock_event_device * pdev-> set_next_event

For example, clock_event_device of my platform is defined as follows:

static struct clock_event_device timer0_clockevent = {.name= "my_timer_evt",.features= CLOCK_EVT_FEAT_PERIODIC,.set_mode= my_set_mode,.set_next_event= my_set_next_event,.rating= 300,.cpumask= cpu_all_mask,};

Timer_set_next_event is mainly used to set registers related to hardware devices.

2. Delete hrtimer

The changes made when you delete a hrtimer are the same as when you add a hrtimer. You need to consider that the deleted timer is the leftmost node in the red/black tree. The timeout value of clock_event_device is the timeout value of the timer to be deleted.

3. Expiration of hrtimer

3.1 when high-precision mode is not configured

Hrtimer expiration is checked by the hrtimer_run_queues function. Hrtimer_run_queues is called in run_local_timers, while run_local_timers is called in the interrupt of the system clock (Jiffy. It can be seen from this that, like the timer using the time wheel algorithm, hrtimer uses the round robin method in every system clock interruption when the high-precision mode is not configured to determine whether the hrtimer expires, therefore, the timing accuracy here is the time interval at which the clock is interrupted.

However, at the beginning of the hrtimer_run_queues function, a check is executed:

If (hrtimer_hres_active ())

Return;

Therefore, After configuring the high-precision mode, the hrtimer_run_queues function here is equivalent to an empty function and will return directly.

3.2 after the high-precision mode is configured

Hrtimer is called by the clock_event (a hardware device) device's interrupt processing function hrtimer_interrupt. Note that the traditional round robin method is no longer used to determine whether the timer expires. Instead, it sets the delay interruption of clock_event_device and triggers an interruption at the time point of the first expired timer timeout to perform the timeout operation. Therefore, the timing accuracy here is determined by the timing precision of clock_event_device.

4. Soft Interrupt

If the high-precision mode is not configured, if the hrtimer sets a Soft Interrupt flag, the soft interrupt that triggers the timeout processing is timer_softirq. After configuring the high-precision mode, the system assigns a dedicated Soft Interrupt to hrtimer. The Soft Interrupt number is hrtimer_softirq.

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.