1、前言
上一篇我们谈到了timer,在详细分析hrtimer的实现之前,我们先追根溯源来谈一下clockevent。先抛开clockevent这个概念,如果硬件要支持定时功能,那么硬件必然要能够支持产生定时时间,通过异步中断的方式通知CPU,你当然不可能让CPU一直去轮询时钟,看时间是不是快到了对吧,linux对能够产生异步事件的clock进行了软件抽象clock event。那么实际上clock event和clock source是硬件时钟的一体两面,clock source提供了读取cycle,计算时间相关的功能,而clock event则提供了产生时间事件相关的功能。
2、clock_event_device数据结构
我们看一下Linux内核是如何用clock_event_device来抽象产生系统时间事件(中断)的设备的,如下:
struct clock_event_device {
void (*event_handler)(struct clock_event_device *);
int (*set_next_event)(unsigned long evt, struct clock_event_device *);
int (*set_next_ktime)(ktime_t expires, struct clock_event_device *);
ktime_t next_event;
u64 max_delta_ns;
u64 min_delta_ns;
u32 mult;
u32 shift;
enum clock_event_state state_use_accessors;
unsigned int features;
unsigned long retries;
int (*set_state_periodic)(struct clock_event_device *);
int (*set_state_oneshot)(struct clock_event_device *);
int (*set_state_oneshot_stopped)(struct clock_event_device *);
int (*set_state_shutdown)(struct clock_event_device *);
int (*tick_resume)(struct clock_event_device *);
void (*broadcast)(const struct cpumask *mask);
void (*suspend)(struct clock_event_device *);
void (*resume)(struct clock_event_device *);
unsigned long min_delta_ticks;
unsigned long max_delta_ticks;
const char *name;
int rating;
int irq;
int bound_on;
const struct cpumask *cpumask;
struct list_head list;
struct module *owner;
} ____cacheline_aligned;
clock_event_device肯定要基于中断,我们先看中断相关的成员
irq:使用的中断号
features