清单 6. hrtimer 切换到高精度模式的核心函数
/*
* Switch to high resolution mode
*/
static int hrtimer_switch_to_hres(void)
{
int cpu = smp_processor_id();
struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
unsigned long flags;
if (base->hres_active)
return 1;
local_irq_save(flags);
if (tick_init_highres()) {
local_irq_restore(flags);
printk(KERN_WARNING "Could not switch to high resolution "
"mode on CPU %d\n", cpu);
return 0;
}
base->hres_active = 1;
base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
tick_setup_sched_timer();
/* "Retrigger" the interrupt to get things going */
retrigger_next_event(NULL);
local_irq_restore(flags);
return 1;
}
|
在这个函数中,首先使用 tick_init_highres 更新与原来的 tick device 绑定的时钟事件设备的 event handler,例如将在低精度模式下的工作函数 tick_handle_periodic / tick_handle_periodic_broadcast 换成 hrtimer_interrupt(它是 hrtimer 在高精度模式下的 timer 中断处理函数),同时将 tick device 的触发模式变为 one-shot,即单次触发模式,这是使用 dynamic tick 或者 hrtimer 时 tick device 的工作模式。由于 dynamic tick 可以随时停止和开始,以不规律的速度产生 tick,因此支持 one-shot 模式的时钟事件设备是必须的;对于 hrtimer,由于 hrtimer 采用事件机制驱动 timer 前进,因此使用 one-shot 的触发模式也是顺理成章的。不过这样一来,原本 tick device 每次执行中断时需要完成的周期性任务如更新 jiffies / wall time (do_timer) 以及更新 process 的使用时间(update_process_times)等工作在切换到高精度模式之后就没有了,因此在执行完 tick_init_highres 之后紧接着会调用 tick_setup_sched_timer 函数来完成这部分设置工作,如清单 7 所示:
清单 7. hrtimer 高精度模式下模拟周期运行的 tick device 的简化实现
void tick_setup_sched_timer(void)
{
struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
ktime_t now = ktime_get();
u64 offset;
/*
* Emulate tick processing via per-CPU hrtimers:
*/
hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
ts->sched_timer.function = tick_sched_timer;
. . . .
for (;;) {
hrtimer_forward(&ts->sched_timer, now, tick_period);
hrtimer_start_expires(&ts->sched_timer,
HRTIMER_MODE_ABS_PINNED);
/* Check, if the timer was already in the past */
if (hrtimer_active(&ts->sched_timer))
break;
now = ktime_get();
}
. . . .
}
|
这个函数使用 tick_cpu_sched 这个 per-CPU 变量来模拟原来 tick device 的功能。tick_cpu_sched 本身绑定了一个 hrtimer,这个 hrtimer 的超时值为下一个 tick,回调函数为 tick_sched_timer。因此,每过一个 tick,tick_sched_timer 就会被调用一次,在这个回调函数中首先完成原来 tick device 的工作,然后设置下一次的超时值为再下一个 tick,从而达到了模拟周期运行的 tick device 的功能。如果所有的 CPU 在同一时间点被唤醒,并发执行 tick 时可能会出现 lock 竞争以及 cache-line 冲突,为此 Linux 内核做了特别处理:如果假设 CPU 的个数为 N,则所有的 CPU 都在 tick_period 前 1/2 的时间内执行 tick 工作,并且每个 CPU 的执行间隔是 tick_period / (2N),见清单 8 所示:
