Вы находитесь на странице: 1из 24

Lecture 3 - Timer

Timing Measurement (Linux)


Introduction Timekeeping Architecture Related System Calls

Case study - S3C2410 Timer


Block Diagram Timer Operations Registers

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.

For user applications, timers are useful in:


performance monitoring general time keeping timing external events generate timed interrupts
2

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

Linux Timekeeping Architecture


Data Structures
The cur_timer object (the timer object of type timer_opts)
Consist a timer name and four methods to handle timer source. name: identifying the timer source mark_offset: records the exact time of last tick get_offset: returns the time elapsed since the last tick monotonic_clock: returns the number of ns since kernel initialization delay: waits for a given number of loops

The xtime variable (type timespec)


Stores the current time and date. tv_sec: stores number of sec elapsed since midnight of Jan.1, 1970. tv_nsec: stores number of nsec elapsed within the last sec.

The jiffies variable


stores the number of elapsed ticks since the system was started
4

Linux Timekeeping Architecture


In a Uniprocessor System,
All time-keeping activities are triggered by interrupts raised by the Programmable Interval Timer (PIT) on IRQ line 0.

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.

Linux Timekeeping Architecture


In Uniprocessor Systems
Initialization time_init()
Initialize xtime by reading RTC Invoke select_timer() to select the best timer source Invoke setup_irq() to setup interrupt gate to IRQ0 (PIT or HPET)

The timer interrupt handler timer_interrupt()


Executes mark_offset method of cur_timer to record current value of timer counter. Invokes do_timer_interrupt() > update jiffies > Invoke update_times() to update xtime and compute system load > Invoke update_process_times() to update local CPU statistics > Invoke the profile_tick()
6

Linux Timekeeping Architecture


In Multiprocessor Systems
Initialization time_init() differs from the UP version as follows
Invoke apic_intr_init() to setup local timer interrupt handler Invoke calibrate_APIC_clock() and then program local APICs

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

The Local time interrupt apic_timer_interrupt() for per-CPU timekeeping activities


acknowledge the interrupt on local APIC invoke smp_local_timer_interrupt() Invoke update_process_times() and profile_tick()
7

Linux Timekeeping Architecture


update_process_times()
is invoked once every tick,
either by the timer interrupt handler in uniprocessor systems, or by the local timer interrupt handler in multiprocessor systems.

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.

Invoke the scheduler_tick(),


which decreases the time slice counter of the current process, and check whether its quantum is exhausted. If so, preempt current process (depends on scheduling policy)
8

Linux Timekeeping Architecture


profile_tick()
To identify the hot spots of the kernel code
At every timer interrupt occurrence, the kernel identify whether the interrupt occurred in Kernel Mode
If so, kernel fetches eip value before the interruption from the stack, and uses it to discover what the kernel was doing before interrupt. In the long run, the samples accumulate on the hot spots.

To enable the code profiler,


the kernel must be booted by passing as the parameter the profile= N, where 2N denotes the size of the code fragment to be profiled.

To access the profiled data


The collected data are stored in /proc/profile file The readprofile system command The oprofile command in Linux 2.6 kernel

can identify hot spots in Kernel, User Mode app, and system library
9

Related System Calls


Time and Date
Several system call allow User Mode processes to read and modify time and date and to create timer. time() : Returns the number of elapsed seconds since midnight at the start of January 1, 1970 (UTC). gettimeofday() : Returns the, in timeval, the number of elapsed seconds since midnight of Jan. 1, 1970, and the number of elapsed microseconds in the last second. Adjtimex(): The system call changes the time gradually at each tick. It will call the update_wall_time_one_tick() to slightly adjust the number of microseconds at each tick.

10

Related System Calls


Interval Timers
Linux allows User Mode processes to activate Interval Timers Cause signals to be sent periodically to the process. Each interval timer is characterized by: The frequency at which the signals must be emitted, or a null value if just one signal has to be generated. The time remaining until the next signal is to be generated.

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

Case Study- S3C2410 Timer


S3C2410 (SAMSUNG)
Use ARM920T CPU core up to 200 MHz Internal Advanced Microcontroller Bus Architecture (AMBATM) (AMBA2.0, AHB/APB) Enhanced ARM architecture MMU to support WinCE, EPOC32, and Linux.

The S3C2410 Timer


Five 16-bit timers Two 8-bit pre-scalers and two 4-bit divider Programmable duty control of output waveform (PWM) Auto reload mode or one-shot pulse mode Dead Zone Generator
12

Timer Block Diagram (1/2)

13

Timer Block Diagram (2/2)


Clock Divider
Each timer has a clock divider with 5 different divided signals (1/2,1/4,1/8, 1/16, and TCLK). The clock divider receives the clock from the corresponding prescaler.

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.

TCNTB (Timer Count Buffer Register)


TCNTBn has an initial value which is loaded into the down-counter (double buffering) when the timer is enabled. Each timer has its own down counter, which is driven by the timer clock. When the down-counter reaches zero, the timer interrupt is generated, and the value of TCNTBn is loaded to the down-counter to continue next cycle.

TCMPB (Timer Compare Buffer Register)


For pulse width modulation (PWM). The value of TCMPBn is loaded into the compare register (double buffering). The timer control logic changes the output level when the down-counter value matches the value of compare register.
14

Timer Operations (1/4)


Timer Operation
Prescaler & Divider

Period
period = TCNTBn * (prescaler + 1) * divider / PCLK frequency TCNTBn = period * PCLK frequency / ((prescaler+1)* divider )
where prescaler: 0~255 divider: 2,4,8,16

e.g. PCLK is 50MHz, prescaler is 15, divider MUX is 1/2


We must set TCNTBn 15625 if we want to get a 10 milisecond timer
15

Timer Operations (2/4)


Timer Operation

16

Timer Operations (3/4)


Dead Zone Generator
This enables the insertion of the time gap between a turn-off of a switching device and a turn on of another switching device, to prohibit the two devices from being turned on simultaneously.

nTOUT0 is the inversion of TOUT0

17

Timer Operations (4/4)


Timer Initialization
Write the initial value into TCNTBn and TCMPBn. Set the manual update bit of the corresponding timer. Set start bit of the corresponding timer to start the timer (and then clear the manual update bit).

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)

TCNTO0~4 (Timer Count Observation Register)

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

Вам также может понравиться