歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> Linux內核系統定時器TIMER實現過程分析

Linux內核系統定時器TIMER實現過程分析

日期:2017/2/28 15:55:59   编辑:Linux內核

Linux系統定時器,在內核中扮演著重要角色。內核的許多重要實現如任務調度,工作隊列等均以系統定時器關系密切。系統定時器能以可編程的頻率中斷處理,這一中斷叫做軟中斷。此頻率即為每秒的定時器節拍數HZ。HZ的越大,說明定時器節拍越小,線程調度的准確性會越高。但HZ設得過大,對一個系統來說並不好,會導CPU開銷過大,反而造成任務調度效率降低。滴答jiffies 變量記錄系統啟動以來,系統定時器已經觸發的次數。也就是說每過一秒jiffies的增量為HZ,一般HZ=100,HZ是可以配置的,在S3C2440 arm linux中配置為200.

下面基於Linux2.6.30.4源碼來探討其實現原理及其過程。

要理解系統定時器實現原理,先來看看關系到系統定時器的各種數據結構,其具體的描述參數。

結構體structtimer_list來描述timer的參數,其數據結構如下:

[cpp]
  1. struct timer_list {
  2. structlist_head entry; //timer雙向鏈表
  3. unsignedlong expires; //timer超時變量
  4. void(*function)(unsigned long); //timer超時回調函數 www.linuxidc.com
  5. unsignedlong data; //傳遞給回調函數的數據,也就是定時器數據
  6. struct tvec_base *base; //timer base向量表用於timer_list的掛載和鏈表管理
  7. //timer的一些擴展參數
  8. #ifdef CONFIG_TIMER_STATS
  9. void*start_site;
  10. charstart_comm[16];
  11. intstart_pid;
  12. #endif
  13. #ifdef CONFIG_LOCKDEP
  14. structlockdep_map lockdep_map;
  15. #endif
  16. };

其中:

[cpp]
  1. list_entry結構:
  2. struct list_head {
  3. structlist_head *next, *prev;
  4. };
  5. tevc_base的結構:
  6. struct tvec_base {
  7. spinlock_tlock; //自旋鎖lock
  8. structtimer_list *running_timer; //指向已經掛載進來的timer_list
  9. unsignedlong timer_jiffies; //timer jiffies用於記錄定時器當前jiffies
  10. structtvec_root tv1; //5組tvec_base,從tv1~tv5,成員數各不相同
  11. structtvec tv2; //其成員數TVR_SIZE,TVN_SIZE決定
  12. structtvec tv3;
  13. structtvec tv4;
  14. structtvec tv5;
  15. } ____cacheline_aligned;
  16. #define TVN_BITS (CONFIG_BASE_SMALL ? 4 :6)
  17. #define TVR_BITS (CONFIG_BASE_SMALL ? 6 :8)
  18. #define TVN_SIZE (1 << TVN_BITS)
  19. #define TVR_SIZE (1 << TVR_BITS)
  20. #define TVN_MASK (TVN_SIZE - 1)
  21. #define TVR_MASK (TVR_SIZE - 1)
  22. struct tvec {
  23. structlist_head vec[TVN_SIZE]; // tv2~t5個數為64的數組
  24. };
  25. struct tvec_root {
  26. structlist_head vec[TVR_SIZE]; //tv1個數為256的數組
  27. };
Copyright © Linux教程網 All Rights Reserved