现在的位置: 首页 > 综合 > 正文

时钟中断处理程序

2014年01月29日 ⁄ 综合 ⁄ 共 5461字 ⁄ 字号 评论关闭

   时钟中断处理程序可以划分为两个部分:体系结构相关部分和体系结构无关部分。

 

 与体系结构相关的例程作为系统定时器的中断处理程序而注册到内核中,以便在产生时钟中断时,它都能够相应地运行。这部分处理程序主要完成:

获得xtime_lock锁,以便对访问jiffies_64墙上时间xtime进行保护

需要时应答或重新设置系统时钟

周期性地使用墙上时间更新实时时钟

调用体系结构无关的时钟例程:do_timer()

  1. 在<Time.h(incluce/linux)>中
  2. extern struct timespec xtime;
  3. #ifndef _STRUCT_TIMESPEC
  4. #define _STRUCT_TIMESPEC
  5. struct timespec {
  6.     time_t  tv_sec;     /* seconds */
  7.     long    tv_nsec;    /* nanoseconds 十亿分之一秒*/
  8. };
  9. #endif

  中断服务程序主要通过调用与体系结构无关的例程do_timer()执行下面的工作:

给jiffies_64变量增加1

更新资源消耗的统计值,比如当前进程所消耗的系统时间和用户时间

执行已经到期的动态定时器

执行scheduler_tick()函数

更新墙上时间,该时间存放在xtime变量中

计算平均负载

 

  1. 在<Time.c(kernel)>中
  2. /*
  3.  * Called by the timer interrupt. xtime_lock must already be taken
  4.  * by the timer IRQ!
  5.  */
  6. static inline void update_times(unsigned long ticks)
  7. {
  8.     update_wall_time(); /* 更新墙上时间 */
  9.     calc_load(ticks); /* 计算平均负载值*/
  10. }
  11.   
  12. /*
  13.  * The 64-bit jiffies value is not atomic - you MUST NOT read it
  14.  * without sampling the sequence number in xtime_lock.
  15.  * jiffies is defined in the linker script...
  16.  */
  17. void do_timer(unsigned long ticks)
  18. {
  19.     jiffies_64 += ticks;/* 给jiffies_64变量加1: */
  20.     update_times(ticks);
  21. }
  22. /**
  23.  * update_wall_time - Uses the current clocksource to increment the wall time
  24.  *
  25.  * Called from the timer interrupt, must hold a write on xtime_lock.
  26.  */
  27. static void update_wall_time(void)
  28. {
  29.     cycle_t offset;
  30.     /* Make sure we're fully resumed: */
  31.     if (unlikely(timekeeping_suspended))
  32.         return;
  33. #ifdef CONFIG_GENERIC_TIME
  34.     offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
  35. #else
  36.     offset = clock->cycle_interval;
  37. #endif
  38.     clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
  39.     /* normally this loop will run just once, however in the
  40.      * case of lost or late ticks, it will accumulate correctly.
  41.      */
  42.     while (offset >= clock->cycle_interval) {
  43.         /* accumulate one interval */
  44.         clock->xtime_nsec += clock->xtime_interval;
  45.         clock->cycle_last += clock->cycle_interval;
  46.         offset -= clock->cycle_interval;
  47.         if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
  48.             clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
  49.             xtime.tv_sec++;
  50.             second_overflow();
  51.         }
  52.         /* interpolator bits */
  53.         time_interpolator_update(clock->xtime_interval
  54.                         >> clock->shift);
  55.         /* accumulate error between NTP and clock interval */
  56.         clock->error += current_tick_length();
  57.         clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
  58.     }
  59.     /* correct the clock when NTP error is too big */
  60.     clocksource_adjust(clock, offset);
  61.     /* store full nanoseconds into xtime */
  62.     xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
  63.     clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
  64.     /* check to see if there is a new clocksource to use */
  65.     change_clocksource();
  66.     update_vsyscall(&xtime, clock);
  67. }
  68. /*
  69.  * calc_load - given tick count, update the avenrun load estimates.
  70.  * This is called while holding a write_lock on xtime_lock.
  71.  */
  72. static inline void calc_load(unsigned long ticks)
  73. {
  74.     unsigned long active_tasks; /* fixed-point */
  75.     static int count = LOAD_FREQ;
  76.     count -= ticks;
  77.     if (unlikely(count < 0)) {
  78.         active_tasks = count_active_tasks();
  79.         do {
  80.             CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  81.             CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  82.             CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  83.             count += LOAD_FREQ;
  84.         } while (count < 0);
  85.     }
  86. }

  do_timer()执行完毕后返回与体系结构相关的中断处理程序,继续执行后面的工作,释放xtime_lock锁,然后退出。

  以上全部工作每1/Hz秒都要发生一次。

  相关数据类型如下:

 

  1. 在<Clocksource.h(include/linux)>中
  2. /* clocksource cycle base type */
  3. typedef u64 cycle_t;
  4. struct clocksource;
  5. /**
  6.  * struct clocksource - hardware abstraction for a free running counter
  7.  *  Provides mostly state-free accessors to the underlying hardware.
  8.  *
  9.  * @name:       ptr to clocksource name
  10.  * @list:       list head for registration
  11.  * @rating:     rating value for selection (higher is better)
  12.  *          To avoid rating inflation the following
  13.  *          list should give you a guide as to how
  14.  *          to assign your clocksource a rating
  15.  *          1-99: Unfit for real use
  16.  *              Only available for bootup and testing purposes.
  17.  *          100-199: Base level usability.
  18.  *              Functional for real use, but not desired.
  19.  *          200-299: Good.
  20.  *              A correct and usable clocksource.
  21.  *          300-399: Desired.
  22.  *              A reasonably fast and accurate clocksource.
  23.  *          400-499: Perfect
  24.  *              The ideal clocksource. A must-use where
  25.  *              available.
  26.  * @read:       returns a cycle value
  27.  * @mask:       bitmask for two's complement
  28.  *          subtraction of non 64 bit counters
  29.  * @mult:       cycle to nanosecond multiplier
  30.  * @shift:      cycle to nanosecond divisor (power of two)
  31.  * @flags:      flags describing special properties
  32.  * @vread:      vsyscall based read
  33.  * @cycle_interval: Used internally by timekeeping core, please ignore.
  34.  * @xtime_interval: Used internally by timekeeping core, please ignore.
  35.  */
  36. struct clocksource {
  37.     char *name;
  38.     struct list_head list;
  39.     int rating;
  40.     cycle_t (*read)(void);
  41.     cycle_t mask;
  42.     u32 mult;
  43.     u32 shift;
  44.     unsigned long flags;
  45.     cycle_t (*vread)(void);
  46.     /* timekeeping specific data, ignore */
  47.     cycle_t cycle_last, cycle_interval;
  48.     u64 xtime_nsec, xtime_interval;
  49.     s64 error;
  50. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  51.     /* Watchdog related data, used by the framework */
  52.     struct list_head wd_list;
  53.     cycle_t wd_last;
  54. #endif

呵呵

抱歉!评论已关闭.