Linux hrtimer分析--配置高精度模式

转自:http://blog.csdn.net/walkingman321/article/details/6151172

本文介绍Linux2.6.29中,配置高精度模式的hrtimer与未配置高精度模式时行为的区别。本文暂不考虑高精度模式对Linux系统时钟中断的影响。

 

      在没有配置高精度模式时,hrtimer的超时在系统时钟中断的轮循中检查,所以此时hrtimer的定时精度(jiffy)还是以轮循的间隔为单位,精度与传统的时间轮定时器一样。

      在配置高精度模式后,hrtimer的超时由struct clock_event_device的超时中断完成。clock_event_device一般来说描述的都是一个硬件定时器,其定时精度由硬件定时器决定,比如我的板子上的此定时器参数为:Timer at Vir:0xE0100200 = Phy:0xE0100200, using Irq:27, at Freq:250000000,可见其精度之高:250MHz,这也是高精度时钟这个名称的由来。

 

struct clock_event_device {
	const char		*name;
	unsigned int		features;
	u64			max_delta_ns;
	u64			min_delta_ns;
	u32			mult;
	u32			shift;
	int			rating;
	int			irq;
	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_head	list;
	enum clock_event_mode	mode;
	ktime_t			next_event;
	unsigned long		retries;
};

 

下面简要介绍一下在配置了高精度模式之后hrtimer的实现细节。

1.     添加hrtimer

在配置高精度模式后,添加hrtimer也是由函数hrtimer_start完成,这与没有配置高精度模式时一样。但是,高精度模式时,如果添加的hrtimer在红黑树的最左边一个节点,即要添加的hrtimer是将会第一个到期的hrtimer时,那么这时会调用子函数hrtimer_enqueue_reprogram重新设置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

比如我的平台的clock_event_device的定义如下:

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主要是设置硬件设备的相关registers.

2.  删除hrtimer

删除hrtimer时的改动与添加hrtimer一样,需要考虑删除的定时器正好是红黑树中最左边节点的情况。因为此时clock_event_device的超时中断设置的超时值正好是要删除的定时器的超时值。

3.  hrtimer的到期

3.1 未配置高精度模式时

      hrtimer的到期由函数hrtimer_run_queues检查。hrtimer_run_queues是在run_local_timers中被调用,而run_local_timers又是在系统时钟(jiffy)中断中被调用。从这里可以看出,与传统的使用时间轮算法的定时器一样,hrtimer在未配置高精度模式时采用了在每一个系统时钟中断中轮循的方式来判断hrtimer是否到期,因此,这里的定时精度为时钟中断轮循的时间间隔。

但是,在函数hrtimer_run_queues的开始处,会执行一项检查:

       if (hrtimer_hres_active())

              return;

所以在配置高精度模式后,这里的hrtimer_run_queues函数相当于空函数,会直接返回。

3.2 配置了高精度模式之后

       hrtimer的到期由clock_event(是一个硬件设备)设备的中断处理来调用,处理函数为hrtimer_interrupt。注意这里不再采用传统的轮循方式判断定时器是否到期,而是通过设置clock_event_device的延时中断,在第一个到期的定时器超时的时间点触发一个中断来执行超时操作。所以,这里的定时精度由clock_event_device的计时精度决定。

4.     软中断

     未配置高精度模式时,如果hrtimer设置了软中断标记位,触发其超时处理的软中断为TIMER_SOFTIRQ。配置高精度模式后,系统为hrtimer分配了一个专用的软中断,软中断编号为HRTIMER_SOFTIRQ。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值