A word about the Linux kernel timer

  • 2020-06-19 12:27:30
  • OfStack

The timer in the sense of software ultimately depends on the hardware timer to be realized. The kernel detects whether each timer expires after the clock interrupt occurs, and the timer processing function after expiration will be executed in the bottom half as soft interrupt. In essence, the clock interrupt handler will replace the TIMER_SOFTIRQ softinterrupt and run all the expired timers on the current processor.

It still sums up the process of soft interrupts

a. Registers the softirq handler


/*/linux/kernel.timer.c*/
void __init init_timers(void)
  -->open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);

b. Add timer_list to a linked list

void add_timer (struct timer_list *timer);

c. Triggers the softirq handler function


void irq_exit(void)
  -->tick_nohz_stop_sched_tick();
    -->raise_softirq_irqoff(TIMER_SOFTIRQ);

d. Calls the softirq handler

static void run_timer_softirq(struct softirq_action *h)
-- > __run_timers(base);
-- > Iterate through the timer handler in timer_list where the execution time arrives
In Linux device driver programming, one set of functions and data structures provided in the Linux kernel can be used to complete timed triggers or complete some kind of periodic transaction. This set of functions and data structures allows the driver to largely ignore the kernel and hardware behavior of the specific software timer.

1) An instance of timer_list structure corresponds to a timer, which is defined as follows:


struct timer_list {

   struct list_head entry, /* Timer list */
   unsigned long expires, /* Timer expiration time */
   void (*function) (unsigned long), /* Timer handler functions */
   unsigned long data,/* Is passed in as a parameter to the timer handler function */
   struct timer_base_s *base,
   ...

};

Instantiate struct timer_list my_timer;

2) Initialize the timer


void init_timer (struct timer_list *timer);

TIMER_INITIALIZER (_function, _expires, _data)

DEFINE_TIMER (_name, _function, _expires, _data)

setup_timer ();

3) Increase the timer

void add_timer (struct timer_list *timer);

4) Delete the timer

int del_timer (struct timer_list *timer);

5) Modify expire of the timer

int mod_timer (struct timer_list *timer, unsigned long expires);


Related articles: