Linux Timers

24
1 Lecture 3 - Timer Timing Measurement (Linux) ¾ Introduction ¾ Timekeeping Architecture ¾ Related System Calls Case study - S3C2410 Timer ¾ Block Diagram ¾ Timer Operations ¾ Registers

Transcript of Linux Timers

Page 1: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 1/24

1

Lecture 3 - Timer 

• Timing Measurement (Linux)

¾ Introduction

¾Timekeeping Architecture

¾Related System Calls

• Case study - S3C2410 Timer ¾Block Diagram

¾Timer Operations¾Registers

Page 2: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 2/24

2

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

Page 3: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 3/24

3

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

Page 4: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 4/24

4

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

Page 5: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 5/24

5

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.

Page 6: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 6/24

6

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()

Page 7: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 7/24

7

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()

Page 8: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 8/24

8

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() inKernel mode.

• update utime field or stime field (ticks spent in User mode or Kernelmode) 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)

Page 9: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 9/24

9

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

Page 10: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 10/24

10

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 elapsedseconds 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.

Page 11: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 11/24

11

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 valueif 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

Page 12: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 12/24

12

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 

Page 13: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 13/24

13

Timer Block Diagram (1/2)

Page 14: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 14/24

14

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 thevalue 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, andthe 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.

Page 15: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 15/24

15

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 

Page 16: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 16/24

16

Timer Operations (2/4)

• Timer Operation

Page 17: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 17/24

17

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

Page 18: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 18/24

18

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 */}

Page 19: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 19/24

19

Registers (1/5)

• TCFG0 (Timer Configuration Register 0)

Page 20: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 20/24

20

Registers (2/5)

• TCFG1 (Timer Configuration Register 1)

Page 21: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 21/24

21

Registers (3/5)

• TCON (Timer Control Register)

Page 22: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 22/24

22

Registers (4/5)

• TCON (Timer Control Register)

R i (5/5)

Page 23: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 23/24

23

Registers (5/5)

• TCNTB0~4 & TCMPB0~4 (Timer Count & Timer Compare Buffer Reg)

• TCNTO0~4 (Timer Count Observation Register)

R f

Page 24: Linux Timers

7/29/2019 Linux Timers

http://slidepdf.com/reader/full/linux-timers 24/24

24

Reference

• “Understanding the Linux Kernel (3rd Edition),” Daniel

P.Bovert & Macro Cesati, O’Reilly, ISBN0-596-00565

• S3C2410X User’s Manual

¾ http://www.samsung.com/Products/Semiconductor/SystemLSI

/MobileSolutions/MobileASSP/MobileComputing/S3C2410/S3

C2410.htm