Linux Timers
Linux Timers
Linux Timers
Introduction
Within a system, timers are used for lots of functions
system reset synchronization, bus-ready gating, refresh counters, driving other circuits, operating system housekeeping, timing external interrupt events.
Introduction
Periodic, time-related activities in Linux
Updates the time elapsed since system startup Updates the time and date Determines, for every CPU, how long the current process has been running,
preempts it if it has exceeded the time allocated to it
Updates resource usage statistics Checks whether the interval of time associated with each software timer has elapsed.
If so, invoke the proper function
In a Multiprocessor System,
all general activities (like handling of software timers) are triggered by the interrupts raised by the PIT, while CPU-specific activities (like monitoring the execution time of the currently running process) are triggered by the interrupts raised by the local APIC timers.
The Global timer interrupt timer_interrupt() differs from the UP version as follows
acknowledge the interrupt on APIC update_process_times() and profile_tick() are NOT invoked
check how long (in ticks) current process has been running
call account_user_time() in User mode, or account_system_time() in Kernel mode. update utime field or stime field (ticks spent in User mode or Kernel mode) of current process descriptor
check whether the total CPU time limit has been reached
If so, sends SIGXCPU and SIGKILL signals to current process.
can identify hot spots in Kernel, User Mode app, and system library
9
10
setitimer()
it has the following policies: ITIMER_REAL: The actual elapsed time; SIGALRM signals. ITIMER_VIRTUAL: The time spent by the process in User Mode; SIGVTALRM signals. ITIMER_PROF: The time spent by the process both in User and in Kernel Mode; SIGPROF signals.
alarm()
sends a SIGALRM signal to calling process. cannot use at the same time as setitimer() with ITIMER_REAL
11
13
Prescaler
Timer 0,1 share a presacler, and timer 2,3,4 share the other The 8-bit prescaler is programmable and divides the PCLK according to the value stored in TCFG0 and TCFG1 registers.
Period
period = TCNTBn * (prescaler + 1) * divider / PCLK frequency TCNTBn = period * PCLK frequency / ((prescaler+1)* divider )
where prescaler: 0~255 divider: 2,4,8,16
16
17
Programming example
void set_timer4(int mode, int second)
{ TCON &= 0xffefffff; /* clear start bit */ TCNTB4 = PCLK_frequency * second / ((prescaler+1) * divider); if (mode == ONE_SHOT) TCON &= 0xffbfffff; /* set mode bit */ else if (mode == AUTO_RELOAD) TCON |= 0x00400000; /* set mode bit */ TCON |= 0x00200000; /* set manual update bit */ TCON = TCON & 0xffdfffff | 0x00100000; /* clear manaul update bit */ TCON |= 0x00100000; /* set start bit */ }
18
Registers (1/5)
TCFG0 (Timer Configuration Register 0)
19
Registers (2/5)
TCFG1 (Timer Configuration Register 1)
20
Registers (3/5)
TCON (Timer Control Register)
21
Registers (4/5)
TCON (Timer Control Register)
22
Registers (5/5)
TCNTB0~4 & TCMPB0~4 (Timer Count & Timer Compare Buffer Reg)
23
Reference
Understanding the Linux Kernel (3rd Edition), Daniel P.Bovert & Macro Cesati, OReilly, ISBN0-596-00565 S3C2410X Users Manual http://www.samsung.com/Products/Semiconductor/SystemLSI /MobileSolutions/MobileASSP/MobileComputing/S3C2410/S3 C2410.htm
24