Linux驅動技術(七) _內核定時器與延遲工作


內核定時器

軟件上的定時器最終要依靠硬件時鍾來實現,簡單的說,內核會在時鍾中斷發生后檢測各個注冊到內核的定時器是否到期,如果到期,就回調相應的注冊函數,將其作為中斷底半部來執行。實際上,時鍾中斷處理程序會觸發TIMER_SOFTIRQ軟中斷,運行當前處理器上到期的所有定時器。
設備驅動程序如要獲得時間信息以及需要定時服務,都可以使用內核定時器。

jiffies

要說內核定時器,首先就得說說內核中關於時間的一個重要的概念:jiffies變量,作為內核時鍾的基礎,jiffies每隔一個固定的時間就會增加1,稱為增加一個節拍,這個固定間隔由定時器中斷來實現,每秒中產生多少個定時器中斷,由在<linux/param.h>中定義的HZ宏來確定,如此,可以通過jiffies獲取一段時間,比如jiffies/HZ表示自系統啟動的秒數。下兩秒就是(jiffies/HZ+2),內核中用jiffies來計時,秒轉換成的jiffies:seconds*HZ,所以以jiffiy為單位,以當前時刻為基准計時2秒:(jiffies/HZ+2)*HZ=jiffies+2*HZ如果要獲取當前時間,可以使用do_gettimeofday(),該函數填充一個struct timeval結構,有着接近微妙的分辨率。

//kernel/time/timekeeping.c
 473 /**
 474  * do_gettimeofday - Returns the time of day in a timeval
 475  * @tv:         pointer to the timeval to be set
 476  *
 477  * NOTE: Users should be converted to using getnstimeofday()
 478  */
 479 void do_gettimeofday(struct timeval *tv)   

驅動程序為了讓硬件有足夠的時間完成一些任務,常常需要將特定的代碼延后一段時間來執行,根據延時的長短,內核開發中使用長延時短延時兩個概念。長延時的定義為:延時時間>多個jiffies,實現長延時可以用查詢jiffies的方法:

time_before(jiffies, new_jiffies);
time_after(new_jiffiesmjiffies);

**短延時的定義為:延遲事件接近或短於一個jiffy,實現短延時可以調用

udelay();
mdelay();

這兩個函數都是忙等待函數,大量消耗CPU時間,前者使用軟件循環來延遲指定數目的微妙數,后者使用前者的嵌套來實現毫秒級的延時。

定時器

驅動可以注冊一個內核定時器,來指定一個函數在未來某個時間來執行。定時器從注冊到內核開始計時,達到指定的時間后會執行注冊的函數。即超時值是一個jiffies值,當jiffies值大於timer->expires時,timer->function就會被執行。API如下

//定一個定時器
struct timer_list my_timer;

//初始化定時器
void init_timer(struct timer_list *timer);
mytimer.function = my_function;
mytimer.expires = jiffies +HZ;

//增加定時器
void add_timer(struct timer_list *timer);

//刪除定時器
int del_tiemr(struct timer_list *timer);

實例

static struct timer_list tm;
struct timeval oldtv;

void callback(unsigned long arg)
{
	struct timeval tv;
	char *strp = (char*)arg;
	do_gettimeofday(&tv);
	printk("%s: %ld, %ld\n", __func__,
		tv.tv_sec - oldtv.tv_sec,
		tv.tv_usec- oldtv.tv_usec);
	oldtv = tv;
	tm.expires = jiffies+1*HZ;
	add_timer(&tm);
}

static int __init demo_init(void)
{
	init_timer(&tm);
	do_gettimeofday(&oldtv);
	tm.function= callback;
	tm.data    = (unsigned long)"hello world";
	tm.expires = jiffies+1*HZ;
	add_timer(&tm);
	return 0;
}

延遲工作

除了使用內核定時器完成定時延遲工作,Linux內核還提供了一套封裝好的"快捷方式"-delayed_work,和內核定時器類似,其本質也是利用工作隊列和定時器實現,

//include/linux/workqueue.h
100 struct work_struct {           
101         atomic_long_t data;
102         struct list_head entry;
103         work_func_t func;
104 #ifdef CONFIG_LOCKDEP
105         struct lockdep_map lockdep_map;
106 #endif
107 };
113 struct delayed_work {              
114         struct work_struct work;
115         struct timer_list timer;
116 
117         /* target workqueue and CPU ->timer uses to queue ->work */
118         struct workqueue_struct *wq;
119         int cpu;
120 };

struct work_struct
--103-->需要延遲執行的函數, typedef void (*work_func_t)(struct work_struct *work);

至此,我們可以使用一個delayed_work對象以及相應的調度API實現對指定任務的延時執行

//注冊一個延遲執行
591 static inline bool schedule_delayed_work(struct delayed_work *dwork,unsigned long delay)
//注銷一個延遲執行
2975 bool cancel_delayed_work(struct delayed_work *dwork)    

和內核定時器一樣,延遲執行只會在超時的時候執行一次,如果要實現循環延遲,只需要在注冊的函數中再次注冊一個延遲執行函數。

schedule_delayed_work(&work,msecs_to_jiffies(poll_interval));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM