linux hrtimer 绑定cpu,Linux hrtimer分析--未配置高精度模式

struct hrtimer_cpu_base {

raw_spinlock_tlock;

struct hrtimer_clock_baseclock_base[HRTIMER_MAX_CLOCK_BASES]; //时钟源 #define HRTIMER_MAX_CLOCK_BASES 2

#ifdef CONFIG_HIGH_RES_TIMERS

ktime_texpires_next;

inthres_active;

inthang_detected;

unsigned longnr_events;

unsigned longnr_retries;

unsigned longnr_hangs;

ktime_tmax_hang_time;

#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_TIMERS

ktime_toffset;

#endif

};

在hrtimer.c中,有为每个CPU具体定义hrtimer_cpu_base的代码:

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 时钟源类型

可以看出,每个CPU都必须定义两个时钟源:REALTIME和MONOTONIC。REALTIME代表实时时钟,MONOTONIC代表单调递增时钟。两者的区别在于,当用户更改系统时间时,REALTIME时钟会收到影响,但MONOTONIC不受影响。这可以从它们两个的get_time函数指针看出来,REALTIME时钟指向的是ktime_get_real,MONOTONIC指向的是ktime_get。

时钟源的结构体定义为struct hrtimer_clock_base,其中有两个域struct rb_node       *first和struct rb_root   active,这两个域维护了hrtimer的红黑树。也就是说,每一个hrtimer_clock_base都维护了自己的一个红黑树。

hrtimer在初始化时,都需要加入到某一个时钟源的红黑树中,这个时钟源要么是REALTIME,要么是MONOTONIC,这个关联通过struct hrtimer的base域实现。

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_STATS

intstart_pid;

void*start_site;

charstart_comm[16];

#endif

};

2. hrtimer的基本操作

Linux的传统定时器通过时间轮算法实现(timer.c),但hrtimer通过红黑树算法实现。在struct hrtimer里面有一个node域,类型为struct rb_node,这个域代表了hrtimer在红黑树中的位置。

2.1 hrtimer_start

hrtimer_start函数将一个hrtimer加入到一个按照到期时间排序的红黑树中,其主要流程为:

int hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode);

// 根据time和mode参数的值计算hrtimer的超时时间,并设置到timer->expire域。

// expire设置的是绝对时间,所以如果参数mode的值为HRTIMER_MODE_REL(即参数tim的值为相对时间),那么需要将tim的值修正为绝对时间:

//     expire = tim + timer->base->get_time()。(注意本文只研究单CPU的情况)

//调用enqueue_hrtimer,将hrtimer加入到红黑树中。

2.2 hrtimer的到期

hrtimer在hrtimer_run_queues函数中判断是否到期执行。hrtimer_run_queues 的调用链为:Linux的系统时钟函数->update_process_times->run_local_timers->hrtimer_run_queues。

void hrtimer_run_queues(void)

// 判断是否是高精度模式,如果是高精度模式,立即返回。本文暂不考虑这种情况。

// 对每一个时钟源(REALTIME和MONOTONIC)的红黑树,按到期先后顺序检查hrtimer,看它们是否到期(将定时器与时钟源的softirq_time比较)。如果到期,就把这个到期的定时器取出,然后按照定时器的具体模式执行相应的操作:

//如果定时器模式为HRTIMER_CB_SOFTIRQ,那么将定时器搬到hrtimer_cpu_base的cb_pending队列

//调用__run_hrtimer,在__run_hrtimer中执行定时器的回调函数。

在没有配置高精度模式时,cb_pending队列中的定时器会在TIMER_SOFTIRQ软中断中执行。调用链为

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)

event_handler,即dev->event_handler = handler;>

2.3 hrtimer_cancel

hrtimer_cancel函数的作用是删除一个正在排队的定时器。这里分三种情况,一种是定时器已到期,并且设置了软中断模式;第二种是没有到期,还在红黑树中;第三种是定时器正在执行。

第一种情况,定时器被挂在hrtimer_cpu_base的cb_pending队列中,所以需要把它从pending队列中移出。

第二种情况,定时器还在红黑树中,那么把它从红黑树中移出。由于本文暂时只考虑高精度没有打开的情况,所以先不研究定时器正好排在红黑树第一个时的情况(即代码中调用hrtimer_force_reprogram函数的部分)。

第三种情况删除失败,hrtimer_cancel函数会循环重试,等到定时器执行完的时候再删除。(这在多CPU系统中可能会发生)

3.  未使能高精度模式时与传统timer的区别

1)传统timer使用时间轮算法,hrtimer使用红黑树算法。

2)传统timer在软中断中执行,hrtimer在硬中断中执行(update_process_times -> run_local_timers -> hrtimer_run_queues)。如果hrtimer设置了HRTIMER_CB_SOFTIRQ模式,那么timer会被移到pending队列,然后再由软中断执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
hrtimerLinux内核中的一个重要功能,可提供高精度的定时器功能。它可以在纳秒级别的精度下进行定时,主要用于实时任务或需要高精度定时的应用。 使用hrtimer高精度的方法如下: 1. 定义hrtimer: 使用hrtimer之前,首先需要定义一个hrtimer对象。可以通过声明struct hrtimer类型的变量来完成,例如: struct hrtimer my_hrtimer; 2. 初始化hrtimer: 初始化hrtimer对象可以使用hrtimer_init函数,该函数有三个参数:hrtimer对象,时钟源和是相对还是绝对时间。根据具体需要,可以选择不同的时钟源,如CLOCK_MONOTONIC或CLOCK_REALTIME。 3. 设置并启动hrtimer: 设置hrtimer对象的定时器周期和回调函数,然后使用hrtimer_start函数启动hrtimer。设置定时器周期可以使用hrtimer_set_periodic或hrtimer_set_expires函数。回调函数会在定时器到期时被调用。 4. 处理hrtimer到期: 当hrtimer到期时,会触发之前设置的回调函数,我们可以在回调函数中执行相应的操作。 5. 停止hrtimer: 如果需要停止hrtimer,可以使用hrtimer_cancel函数。 综上所述,通过使用Linux内核的hrtimer模块,我们可以实现高精度的定时功能。这对于实时任务或需要高精度定时的应用至关重要。通过定义、初始化、设置和启动hrtimer对象,可以实现定时器的定期触发和相应的操作。同时,可以根据需要选择不同的时钟源来满足特定的应用需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值