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

Slides:



Advertisements
Similar presentations
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Advertisements

1 Homework Reading –Intel 8254 Programmable Interval Timer (PIT) Data Sheet Machine Projects –Continue on MP3 Labs –Continue in labs with your assigned.
CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
The Process Control Block From: A Process Control Block (PCB, also called Task Control Block or Task Struct) is.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Chapter 5 Processes and Threads Copyright © 2008.
Interrupts What is an interrupt? What does an interrupt do to the “flow of control” Interrupts used to overlap computation & I/O – Examples would be console.
Interrupts (contd..) Multiple I/O devices may be connected to the processor and the memory via a bus. Some or all of these devices may be capable of generating.
OS2-1 Chapter 2 Computer System Structures. OS2-2 Outlines Computer System Operation I/O Structure Storage Structure Storage Hierarchy Hardware Protection.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
1 Soft Timers: Efficient Microsecond Software Timer Support For Network Processing Mohit Aron and Peter Druschel Rice University Presented By Jonathan.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
1 When to Switch Processes 3 triggers –System call, Interrupt and Trap System call –when a user program invokes a system call. e.g., a system call that.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Computer System Laboratory
General System Architecture and I/O.  I/O devices and the CPU can execute concurrently.  Each device controller is in charge of a particular device.
Introduction to Embedded Systems
System Calls 1.
COMP201 Computer Systems Exceptions and Interrupts.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
CS591 (Spring 2001) The Linux Kernel: Debugging. CS591 (Spring 2001) Accessing the “Black Box” n Kernel code: n Not always executed in context of a process.
CHAPTER 2: COMPUTER-SYSTEM STRUCTURES Computer system operation Computer system operation I/O structure I/O structure Storage structure Storage structure.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Chapter 2: Computer-System Structures
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
1 Chapter 2: Computer-System Structures  Computer System Operation  I/O Structure  Storage Structure  Storage Hierarchy  Hardware Protection  General.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage Structure Storage Hierarchy Hardware Protection Network Structure.
Timer Timer is a device, which counts the input at regular interval (δT) using clock pulses at its input. The counts increment on each pulse and store.
Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.
Lab 13 Department of Computer Science and Information Engineering National Taiwan University Lab13 – Interrupt + Timer 2014/12/23 1 /16.
1 Soft Timers: Efficient Microsecond Software Timer Support For Network Processing Mohit Aron and Peter Druschel Rice University Presented By Oindrila.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
4300 Lines Added 1800 Lines Removed 1500 Lines Modified PER DAY DURING SUSE Lab.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Time Management.  Time management is concerned with OS facilities and services which measure real time.  These services include:  Keeping track of.
Time Sources and Timing David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Ch6. Flow of Time Ch7. Getting Hold of Memory 홍원의.
Timers and Time Management Ok-Kyun Ha
Computer System Structures
Chapter 2: Computer-System Structures(Hardware)
Chapter 2: Computer-System Structures
Processes and threads.
CS 6560: Operating Systems Design
Homework Reading Machine Projects Labs
Time Sources and Timing
Protection of System Resources
Mechanism: Limited Direct Execution
Introduction to the Linux Kernel
Time Sources and Timing
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Module 2: Computer-System Structures
Chapter 2: The Linux System Part 3
Chapter 2: Operating-System Structures
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Wireless Embedded Systems
Time Sources and Timing
CS510 Operating System Foundations
Chapter 2: Computer-System Structures
Chapter 2: Computer-System Structures
Chapter 2: Operating-System Structures
Presentation transcript:

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

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

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

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

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

frequency of the timer interrupt 5

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

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

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

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

jiffies and jiffies_64 10

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

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

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

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

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

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

tick_periodic() 17

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

update_process_times() 19

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

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

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

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

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

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

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

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, – LATCH yields the ratio between CLOCK_TICK_RATE and HZ for programming the PIT. 27

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

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

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

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

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

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

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

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

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

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

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

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

The groups of lists associated with dynamic timers 40

… 日 日 月 月 年 年 2008/06/ PADS 6/3

… 日 日 月 月 年 年 2008/06/ PADS 6/3 系務 會議 7/2 27 後開系務會議 SOC 會議 7/5 30 後開 SOC 會議

… 日 日 月 月 年 年 2008/06/ 系務 會議 7/2 SOC 會議 7/5

… 日 日 月 月 年 年 2008/06/ 系務 會議 7/2 SOC 會議 7/5

光陰飛逝 … 45

光陰飛逝 …… 46

… 日 日 月 月 年 年 2008/06/ 系務 會議 7/2 SOC 會議 7/5

… 日 日 月 月 年 年 2008/07/ 系務 會議 7/2 SOC 會議 7/5

… 日 日 月 月 年 年 2008/07/ 系務 會議 7/2 SOC 會議 7/5

… 日 日 月 月 年 年 2008/07/ SOC 會議 7/5

… 日 日 月 月 年 年 2008/07/ SOC 會議 7/5

… 日 日 月 月 年 年 2008/07/ SOC 會議 7/5

… 日 日 月 月 年 年 2008/07/

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;

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

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

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

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, – 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

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

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

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

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

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

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