Docsity

Understanding Intel Hardware Clocks and Software Timers in Linux OS, Study notes of Operating Systems

Toggle sidebar
bg1
Timing and Timers
โ€ข Accurate timing crucial for many aspects of OS
โ€“ Device-related timeouts
โ€“ File timestamps (created, accessed, written)
โ€“ Time-of-day (gettimeofday()
โ€“ High-precision timers (code profiling, etc.)
โ€“ Scheduling, cpu usage, etc.
โ€ข Intel timer hardware
โ€“ RTC: Real Time Clock
โ€“ PIT: Programmable Interrupt Timer
โ€“ TSC: TimeStamp Counter (cycle counter)
โ€“ Local APIC Timer: per-cpu alarms
โ€ข Timer implementations
โ€“ Kernel timers (dynamic timers)
โ€“ User โ€œintervalโ€ timers (alarm(), setitimer())
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Intel Hardware Clocks and Software Timers in Linux OS and more Study notes Operating Systems in PDF only on Docsity!

Timing and Timers

Accurate timing crucial for many aspects of OS

  • Device-related timeouts โ€“ File timestamps (created, accessed, written) โ€“ Time-of-day (gettimeofday() โ€“ High-precision timers (code profiling, etc.) โ€“ Scheduling, cpu usage, etc.

    Intel timer hardware - RTC: Real Time Clock โ€“ PIT: Programmable Interrupt Timer โ€“ TSC: TimeStamp Counter (cycle counter) โ€“ Local APIC Timer: per-cpu alarms - Timer implementations - Kernel timers (dynamic timers) โ€“ User โ€œintervalโ€ timers (alarm(), setitimer())

Hardware Clocks (Intel)

RTC

  • battery backed (packaged with CMOS RAM) โ€“ registers to access current date/time (ports 0x70, 0x71) โ€“ includes programmable timer (2-8192Hz) โ€“ accessible as /dev/rtc โ€“ sampled by kernel (only) on startup โ€“ set by โ€œclockโ€ command (synched at shutdown)

TSC (aka MSR: microprocessor specific register)

  • 64 bit counter increments at CPU cycle speed โ€“ accessible via userspace assembly instruction rdtsc โ€“ provides high-resolution timing capability โ€“ kernel determines frequency at boot (calibrate_tsc())

Uniprocessor Architecture

sample RTC on startup to init xtime

timer_interrupt() // IRQ0 interrupt handler

  • executes with interrupts disabled โ€“ if TSC
    • sample TSC (last_tsc_low) โ€ข sample PIT register to determine usecs since interrupt (delay_since_last_interrupt) - call do_timer_interrupt()

do_timer_interrupt()

  • calls do_timer() โ€“ updates kernel profiling info if activated โ€“ performs gradual clock adjustment (adjustex())

Uniprocessor Architecture

do_timer()

  • update jiffies
    • jiffies wraps after 467 days but kernel handles it
      • check for quantum expiration โ€“ mark TIMER_BH for execution

timer_bh()

  • executes with interrupts enabled โ€“ possibly delayed for several top-half execution
    • BHs execute only when last stacked interrupt completes
      • update_times() // xtime, load average โ€“ run_timer_list() // dynamic timers

CPU Time Sharing (Slicing)

current->counter

  • ticks remaining in current quantum โ€“ initialized by default to 21 (210 ms โ€“ 1/5 second)

    update_process_times() - executed by PIT or APIC timer if (current->pid) { --current->counter; if (current->counter <= 0) { current->counter = 0; current->need_resched = 1; } } - why outer if? โ€“ why โ€œ<= 0โ€?

Updating Date/Time

xtime.tv_sec, xtime.tv_usec

  • seconds since Jan 1, 1970

    update_times() - wall_jiffies: time of last xtime update โ€“ update_wall_time(ticks) // handles usec wrap โ€“ calc_load(ticks) // load average โ€“ void update_times(void) { unsigned long ticks; write_lock_irq(&xtime_lock); ticks = jiffies โ€“ wall_jiffies; if (ticks) { jiffies += wall_jiffies; update_wall_time(ticks); } write_unlock_irq(&xtime_lock); calc_load(ticks); }

Software Timers

kernel requires lots of high-performance timers

  • particularly networking code

simple implementation

  • linked-list sorted by expiry (in jiffies) โ€“ look at head of list on each timer interrupt โ€“ too expensive for longish lists

caution:

  • handlers executed at some point AFTER expiry โ€“ canโ€™t really bound delay (up to 100s of milliseconds) โ€“ not suitable for real-time processing

Dynamic Timers

struct timer_list

  • linkage โ€“ expires (jiffies) โ€“ function (handler) โ€“ data (int or pointer)

each timer linked into one of 512 doubly linked lists

various functions and macros

  • add_timer(), mod_timer(), del_timer() โ€“ synch versions to avoid SMP problems

Dynamic Timers Handling

very clever data structure

  • avoid O(log N) heap insert (priority queue)

    struct timer_vec (tv1) - array of linked lists (0..255) + โ€œcurrentโ€ list index โ€“ list of timers that will expire in next 256 ticks โ€“ increment index on each tick, execute handlers - what happens when index reaches end of array? - โ€œreplenishโ€ with timers expiring in next 256 seconds โ€“ these are maintained in the NEXT vector (tv2) - each element: timers that expire in next 256 ticks - includes itโ€™s own โ€œcurrentโ€™ index - one โ€œglobalโ€ array of pointers to five vectors of lists - tv1 replenish every 2.5 seconds; tv2 every 3 minutes โ€“ tv3 every 3 hours; tv4 every 7 days!; tv5 never replenished

Dynamic Timer Example

sleep for 2 seconds (in kernel)

  • set_current_state(TASK_INTERRUPTIBLE); โ€“ remaing = schedule_timeout(2 * HZ);

    schedule_timeout() - struct timer_list timer; - expire = timeout + jiffies; - timer.expires = expire; - timer.data = (unsigned long) current; - timer.function = process_timeout; - add_timer(&timer); - schedule(); - del_timer_sync(&timer); - timeout = expire โ€“ jiffies; - return ( timeout < 0? 0 : timeout); - void process_timeout(unsigned long data) - struct task_struct *p = (struct task_struct *) data; โ€“ wake_up_process(p);

User Mode Interval Timers

setitimer(): 3 distinct user mode interval timers

  • can set for one-time or periodic alarm โ€“ sends signals on timer expiry

    three timers: - ITIMER_REAL: elapsed time (SIGALRM) โ€“ ITIMER_VIRT: user (SIGVTALRM) โ€“ ITIMER_PROF: user + kernel (SIGPROF) - implementations: - VIRT, PROF โ€“ updates by PIT or APIC โ€“ REAL โ€“ requires kernel timer - may need to deliver to blocked process - current->real_timer - shared by alarm() API so canโ€™t use both