Presentation is loading. Please wait.

Presentation is loading. Please wait.

Timing Measurements 國立中正大學 資訊工程研究所 羅習五 老師 0. Chapter 11 – Timing Measurements kernel notion of time the tick rate: HZ jiffes hardware clocks and timers.

Similar presentations


Presentation on theme: "Timing Measurements 國立中正大學 資訊工程研究所 羅習五 老師 0. Chapter 11 – Timing Measurements kernel notion of time the tick rate: HZ jiffes hardware clocks and timers."— Presentation transcript:

1 Timing Measurements 國立中正大學 資訊工程研究所 羅習五 老師 0

2 Chapter 11 – Timing Measurements kernel notion of time the tick rate: HZ jiffes hardware clocks and timers timers delaying execution conclusion 1

3 Kernel notion of time When the hardware timer goes off, it issues an interrupt that the kernel handles via a special interrupt handler. This period is called a tick and is equal to 1/(tick rate) seconds. A family of system calls provides the date and time of day to user-space. 2

4 Kernel notion of time Some of the work executed periodically by the timer interrupt includes – Updating the system uptime – Updating the time of day – ensuring that the scheduler runqueues are balanced – Running any dynamic timers that have expired – Updating resource usage and processor time statistics 3

5 The Tick Rate: HZ The frequency of the system timer is programmed on system boot based on a static preprocessor define, HZ. – by default the x86 architecture defines HZ to be 100. It is configurable! – Other common values for HZ are 250 and 1000 4

6 frequency of the timer interrupt 5

7 The ideal HZ value Increasing the tick rate means the timer interrupt runs more frequently. This has the following benefits: – The timer interrupt has a higher resolution – The accuracy of timed events improves 6

8 Advantages with a Larger HZ Kernel timers execute with finer resolution and increased accuracy. System calls such as poll() and select() that optionally employ a timeout value execute with improved precision. Measurements, such as resource usage or the system uptime, are recorded with a finer resolution. Process preemption occurs more accurately. 7

9 Disadvantages with a Larger HZ A higher tick rate implies more frequent timer interrupts, which implies higher overhead 8

10 Jiffies The global variable jiffies holds the number of ticks that have occurred since the system booted – On boot, the kernel initializes the variable to zero – it is incremented by one during each timer interrupt – The system uptime is therefore jiffies/HZ seconds. 9

11 jiffies and jiffies_64 10

12 Hardware clocks and timers Real-Time Clock – The real-time clock (RTC) provides a nonvolatile device for storing the system time. – On boot, the kernel reads the RTC and uses it to initialize the wall time, which is stored in the xtime variable. – The kernel does not typically read the value again 11

13 System timer The idea behind the system timer is to provide a mechanism for driving an interrupt at a periodic rate. On x86, the primary system timer is the programmable interrupt timer (PIT). – The PIT exists on all PC machines and has been driving interrupts since the days of DOS. – It is a simple device with limited functionality, but it gets the job done. 12

14 x86 time sources Real Time Clock Time Stamp Counter ACPI power management timer Programmable Interval Timer High precision event timer (HPET) CPU Local Timers 13

15 The Timer Interrupt Handler The timer interrupt is broken into two pieces: an architecture-dependent and an architecture-independent routine. 14

16 The Timer Interrupt Handler (architecture-dependent) Obtain the xtime_lock lock, which protects access to jiffies_64 and the wall time value, xtime. Acknowledge or reset the system timer as required. Periodically save the updated wall time to the real time clock Call the architecture-independent timer routine, tick_periodic() 15

17 tick_periodic() ( architecture-independent) Increment the jiffies_64 count by one Update resource usages, such as consumed system and user time, for the currently running process Run any dynamic timers that have expired Execute scheduler_tick() Update the wall time, which is stored in xtime Calculate the infamous load average 16

18 tick_periodic() 17

19 do_timer() 18 update_wall_time() updates the wall time in accordance with the elapsed ticks calc_global_load() updates the system’s load average statistics

20 update_process_times() 19

21 update_process_times() Recall from tick_periodic() that the value of user_tick is set by looking at the system’s registers: update_process_times(user_mode(get_irq_regs())); 20

22 account_process_tick() the kernel credits a process for running the entire previous tick in whatever mode the processor was in when the timer interrupt occurred. 21

23 The time of Day To update xtime, a write seqlock is required: 22

24 The time of Day Reading xtime requires the use of the read_seqbegin() and read_seqretry() functions: 23

25 Hardware Clocks Two main kinds of timing measurement that must be performed : – Keeping the current time and date. Be returned to user programs through system calls. – Maintain timers. Be able to notify the kernel or a user program that a certain interval of time has elapsed. The kernel interacts with four clocks: – Real Time Clock – Time Stamp Counter ACPI power management timer – Programmable Interval Timer High precision event timer (HPET) ( 高精確度事件計時器 /Vista) – CPU Local Timers 24

26 Real Time Clock All PCs include a Real Time Clock (RTC). – independent of the CPU and all other chips. Continues to tick even when the PC is switched off. It is capable of issuing periodic interrupts on IRQ8 at frequencies ranging between 2Hz and 8192Hz. 500~0.12ms In Linux: – Acts on the /dev/rtc device file. – Accesses the RTC through ports 0x70 and 0x71. – /sbin/clock 25

27 Time Stamp Counter On a Pentium, you are able to get the number of clock cycles since the CPU was powered up or reset. A 64-bit Time Stamp Counter (TSC) register. – It is a counter. – For instance, 400MHz↔2.5ns calibrate_tsc () 26

28 Programmable Interval Timer (PIT) PIT makes the user aware that the time interval has elapsed. The PIT issues timer interrupts on the IRQ0 at a 100~1k Hz - called a tick. A few macros in the Linux: – HZ yields the number of timer interrupts per second. – CLOCK_TICK_RATE yields 8254 chip ’ s internal oscillator frequency, 1193180. – LATCH yields the ratio between CLOCK_TICK_RATE and HZ for programming the PIT. 27

29 CPU Local Timers The APIC's timer counter is 32-bits long – while the PIT's timer counter is 16-bits long The local APIC timer sends an interrupt only to its processor – while the PIT raises a global interrupt, which may be handled by any CPU in the system The APIC's timer is based on the bus clock signal – the timer counter can be decremented every 1, 2, 4, 8, 16, 32, 64, or 128 bus clock signals – the PIT has its own internal clock oscillator. 28

30 Linux Timekeeping Architecture Updates the time elapsed since system startup. Updates the time and date. Determines how long the current process has been running, and preempts it if it has exceeded the time allocated to it. (RR) Updates resource usage statistics. Checks whether the interval of time associated with each software timer has elapsed. 29

31 Linux Timekeeping Architecture In a uniprocessor system, – all time-keeping activities are triggered by interrupts raised by the Programmable Interval Timer (PIT). In a multiprocessor system, – All general activities (like handling of software timers) are triggered by the interrupts raised by the PIT, – CPU-specific activities (like monitoring the execution time of the currently running process) are triggered by the interrupts raised by the local APIC timers. 30

32 Timekeeping Architecture in Uniprocessor Systems All time-related activities are triggered by the interrupts raised by the Programmable Interval Timer (PIT) on IRQ line 0. The time_init ( ) function sets up the interrupt gate corresponding to IRQ 0 during kernel setup. 31

33 PIT’s Interrupt Service Routine (1) The handler field of IRQ ’ s irqaction descriptor contains the address of the timer_interrupt () function. It performs the following steps: – If the CPU has a TSC. Stores the value of the TSC in the last_tsc_low variable. Reads the state of the 8254 and computes the delay between the timer interrupt occurrence and the execution of the interrupt service rountine. Stores the delay in the delay_at_last_interrupt variable. 32

34 PIT’s Interrupt Service Routine (2) – It invokes do_timer_interrupt (). Invokes the do_timer () function, updates the time elapsed from system startup and delegates all remaining activities to two bottom halves. x86_do_profile If an adjtimex () system call has been issued, it invokes the set_rtc_mmss () function once every 660 seconds (11 milliseconds). Helps systems on a network synchronize their clocks. 33

35 The TIMER_BH Bottom Half Functions The timer_bh () function associated with the TIMER_BH bottom half – update_times () Updates the system date and time and computes the current system load – run_timer_list () Takes care of software timers handling 34

36 Updating the Time and Date The xtime variable stores the current time and date The xtime variable of type sturct timeval – Where user programs get the current time and date. The time_init () function is invoked to set up the time and date. The update_times () function updates xtime. 35

37 Updating system statistics Checking the current process CPU resource limit – Updates the number of ticks which the process has been running in kernel/user mode Keeping track of system load – calc_load counts # of processes in the TASK_RUNNING or TASK_UNINTERRUPTIBLE Profiling the kernel code – The profiler identifies the hot spots of the kernel. 36

38 CPU’s Time Sharing The counter field of the process descriptor: – Specifies how many ticks of CPU time are left to the process. – Be updated at every tick by update_process_times () as follows: if (current->pid) { --current->counter; if (current->counter < 0) { current->counter = 0; current->need_resched = 1; } 37

39 Software Timers A timer is a software facility. – Allow functions to be invoked at some future moment. – Most drives make use of timers to detect anomalous conditions. Ex: floppy disk drivers and parallel printer drivers. – Each timer contains a field that is calculated by adding the number of ticks to the current value of jiffies. 38

40 Dynamic Timers A dynamic timer is stored in the following timer_list structure: 39

41 The groups of lists associated with dynamic timers 40

42 41 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/06/02 262728 PADS 6/3

43 42 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/06/03 262728 PADS 6/3 系務 會議 7/2 27 後開系務會議 SOC 會議 7/5 30 後開 SOC 會議

44 43 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/06/04 262728 系務 會議 7/2 SOC 會議 7/5

45 44 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/06/05 262728 系務 會議 7/2 SOC 會議 7/5

46 光陰飛逝 … 45

47 光陰飛逝 …… 46

48 47 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/06/28 262728 系務 會議 7/2 SOC 會議 7/5

49 48 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/07/01 262728 系務 會議 7/2 SOC 會議 7/5

50 49 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/07/2 262728 系務 會議 7/2 SOC 會議 7/5

51 50 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/07/03 262728 SOC 會議 7/5

52 51 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/07/04 262728 SOC 會議 7/5

53 52 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/07/05 262728 SOC 會議 7/5

54 53 123456… 123456789101112 0607080910 日 日 月 月 年 年 2008/07/06 262728

55 Dynamic timer handling The run_timer_softirq( ) function is a deferrable function. Algorithm: 1.Computes the index of the list in base->tv1 that holds the next timers to be handled: 2.If index is zero,… 54 if (!index && (!cascade(base, &base->tv2, (base->timer_jiffies>> 8)&63)) && (!cascade(base, &base->tv3, (base->timer_jiffies>>14)&63)) && (!cascade(base, &base->tv4, (base->timer_jiffies>>20)&63))) cascade(base, &base->tv5, (base->timer_jiffies>>26)&63); index = base->timer_jiffies & 255;

56 Dynamic timer handling Algorithm (cont.): 3.Increases by one base->timer_jiffies. 4.For each dynamic timer in the base- >tv1.vec[index] list, executes the corresponding timer function. 55

57 An Application of Dynamic Timers (1) To suspend the current process for two seconds, it does this by executing the code: timeout = 2 * HZ; current->state = TASK_INTERRUPTIBLE; timeout = schedule_timeout (timeout); When the time-out expires, the kernel executes the function: void process_timeout(unsigned long data) { struct task_struct * p = (struct task_struct *) data; wake_up_process(p); } 56

58 An Application of Dynamic Timers (2) The schedule_timeout() function executes the following statements: struct timer_list timer; expire = timeout + jiffies; init_timer(&timer); timer.expires = expire; timer.data = (unsigned ling) current; /*TCB*/ timer.function = process_timeout; add_timer(&timer); schedule(); /* process suspended until timer expires */ del_timer(&timer); timeout = expire – jiffies; return (timeout < 0 ? 0 : timeout); 57

59 System Calls Related to Timing Measurements Several system call allow User Mode processes to read and modify the time and date and to create timer. – time() - Returns the number of elapsed seconds since midnight at the start of January 1, 1970. – ftime() - Returns the number of elapsed seconds and the number of elapsed milliseconds in a data structure of type timeb. – gettimeofday() - Returns the same information as ftime() in two data structures named timeval and timezone. 58

60 The adjtimex () System Call The system call changes the time gradually at each tick. The update_wall_time_one_tick () slightly adjusts the number of microseconds at each tick. 59

61 Interval Timers Linux allows User Mode processes to activate special timer – Called Interval timers – Cause Unix signals to be sent periodically to the process. – Be activate by means of the POSIX setitimer () system call. 60

62 The setitimer () System Call The setittimer () system call 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. 61

63 62 A Lightweight Implementation of Time Service (BSD) Idea: using a linked list to describe waiting events – The list is sorted in time order – The time for each event is kept as a difference from the time of the previous event in the linked list 3ticks (3) 2ticks (5) 4ticks (9) 1ticks (10) NIL

64 63 Example INT32U TestNewTimer (void) { OSTimeDly(16);/*at time 16*/ } 12 ticks (12) 2 ticks (14) 4 ticks (18) 1 ticks (19) NILbefore 12 ticks (12) 2 ticks (14) 2 ticks (16) 2 ticks (18) 1 ticks (19) NIL after

65 64 Enhancement The hardware timer can trigger the software timer until the time of the next time event. 3ticks (3) 2ticks (5) 4ticks (9) 1ticks (10) NIL Set H/W timer = 3 Set H/W timer = 2 Set H/W timer = 4 Set H/W timer = 1


Download ppt "Timing Measurements 國立中正大學 資訊工程研究所 羅習五 老師 0. Chapter 11 – Timing Measurements kernel notion of time the tick rate: HZ jiffes hardware clocks and timers."

Similar presentations


Ads by Google