1 / 10

Linux kernel timers

Linux kernel timers. How can a module cause a function to be invoked at some future moment in time?. Hardware timer-counters. IA32 systems: several hardware timers External devices, or internal to processor Examples: 8254 Programmable Interval Timer (IRQ0)

bernad
Download Presentation

Linux kernel timers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux kernel timers How can a module cause a function to be invoked at some future moment in time?

  2. Hardware timer-counters • IA32 systems: several hardware timers • External devices, or internal to processor • Examples: • 8254 Programmable Interval Timer (IRQ0) • MC146818 Real Time Clock w/CMOS (IRQ8) • Pentium’s 64-bit Time Stamp Counter register • Local APIC’s programmable Counter register • (optional) 8254 Watchdog Timer (NMI) • (optional) Network Interface Card (IRQ varies)

  3. Linux Software Timers • Easy for device-driver programmers to use • Used mainly for detecting device “lockups” • But could also be used for other purposes • The driver-writer merely needs to: 1) define a “customized” timeout-function 2) allocate and initialize a kernel-structure 3) call standard routines for timer-control

  4. Software Timer’s Data-Structure struct timer_list struct list_head list; unsigned long expires; unsigned long data; void (*function)( unsigned long ); Use the ‘init_timer()’ routine to initialize the ‘list’ field

  5. Defining your “timeout” action void my_action( unsigned long dataptr ) { /* …perform your desired actions… */ /* then reschedule timer expiration */ next_timeout = jiffies + 5*HZ; mod_timer( &my_timer, next_timeout ); }

  6. Assigning structure’s attributes struct timer_list my_timer; // declared global int init_module( void ) { init_timer( &my_timer ); // for ‘list’ setup my_timer.data = (unsigned long)&my_data; my_timer.function = my_action; my_timer.expires = jiffies + 5 * HZ; add_timer( &my_timer ); return SUCCESS; }

  7. Removing your timer safely void cleanup_module( void ) { int status = del_timer_sync( &my_timer ); // 1 means ‘my_timer’ was queued // 0 means ‘my_timer’ wasn’t queued }

  8. Kernel’s dynamic-timer handling • Need to deal efficiently with many timers • One big linked list would NOT be efficient • Linux adopts a clever partitioning structure • Uses special structures (called ‘tvec’s) • Timers partitioned by their expiration-times • Result: reduces amount of list-processing

  9. Groups of timer lists Array of ‘tvec’ structures (< 0xFF ) ( < 0x3FFF ) ( < 0xFFFFF ) ( < 0x3FFFFFF ) ( < 0xFFFFFFFF )

  10. In-Class Exercise • Modify the ‘mytimer.c’ demo (on website) • Change expiration-frequency (to 5 secs) • Print timer’s messages on user’s console • (See our ‘announce.c’ demo for how-to)

More Related