Presentation is loading. Please wait.

Presentation is loading. Please wait.

Time Sources and Timing

Similar presentations


Presentation on theme: "Time Sources and Timing"— Presentation transcript:

1 Time Sources and Timing
David Ferry, Chris Gill, Brian Kocoloski CSE 422S - Operating Systems Organization Washington University in St. Louis St. Louis, MO 63143

2 CSE 422S –Operating Systems Organization
What is meant by Time? … to a computer  CSE 422S –Operating Systems Organization

3 CSE 422S –Operating Systems Organization
What is meant by Time? … to a computer  Q: is time a resource? Q: or, is time a means with which to share other resources? CSE 422S –Operating Systems Organization

4 CSE 422S –Operating Systems Organization
What is meant by Time? … to a computer  Q: is time a resource? Q: or, is time a means with which to share other resources? What time of day is it? ($ date) Wake me up in 5 seconds ($ sleep 5) CSE 422S –Operating Systems Organization

5 CSE 422S –Operating Systems Organization
What is meant by Time? Absolute time since some fixed past event, e.g.: Seconds since start of the Unix Epoch (00:00:00 UTC on 1 January 1970) Seconds since system boot Relative time, E.g.: Seconds between two events Ten seconds into the future (from now) Execution time of a program segment World time, E.g.: January 30th, 9:00 AM An OS must approximate time to provide time-based functions for users. CSE 422S –Operating Systems Organization

6 How does the kernel keep track of time?
Some hardware support Real-time clock: track system time even when machine is off System timer: fires at known periodic intervals Allows the kernel to say to the CPU: stop the currently running process and let me run after some period of time has expired Programs timer interrupts to fire at known points in the future E.g., program the system timer to always interrupt the processor every 10 ms CSE 422S –Operating Systems Organization

7 Timer interrupts ./test CPU core (executing instructions) User space
Kernel space Timer (hardware that supports delivery of interrupts) CSE 422S –Operating Systems Organization

8 Timer interrupts ./test ./test CPU core (executing instructions)
User space Kernel space Timer (hardware that supports delivery of interrupts) CSE 422S –Operating Systems Organization

9 Timer interrupts ./test ./test Interrupt! CPU core
(executing instructions) User space Kernel space Timer (hardware that supports delivery of interrupts) CSE 422S –Operating Systems Organization

10 Timer interrupts ./test ./test Interrupt! CPU core
(executing instructions) User space Kernel space Timer (hardware that supports delivery of interrupts) Timer interrupt Interrupt forces the CPU to enter kernel mode where a special function called the timer interrupt will execute CSE 422S –Operating Systems Organization

11 Timer interrupts ./test ./test Interrupt! CPU core
(executing instructions) User space Kernel space Timer (hardware that supports delivery of interrupts) Timer interrupt Interrupt forces the CPU to enter kernel mode where a special function called the timer interrupt will execute CSE 422S –Operating Systems Organization

12 CSE 422S –Operating Systems Organization
Timer interrupts ./test User space Kernel space Timer interrupt Timer interrupt Timer interrupt CSE 422S –Operating Systems Organization

13 CSE 422S –Operating Systems Organization
Time Sources RTC (Real-Time Clock) Available on most computers (not on RPi 2 or 3 unless you add it) Comparatively low precision (as low as 0.5 seconds) Higher Precision Hardware Timers Might be used to generate interrupts, might be queryable Run at a variety of frequencies Programmable Interval Timer (PIT) High-Performance Event Timer (HPET) Programmable Interrupt Controller (PIC) Advanced Programmable Interrupt Controller (APIC) Processor Cycles Timestamp Counter (TSC) on x86, 64-bit Cycle Counter (CCNT) on ARM, 32-bit, 64-cycle divider, not accessible in user mode Potentially very high accuracy Can generate interrupts on x86 with TSC-deadline CSE 422S –Operating Systems Organization

14 CSE 422S –Operating Systems Organization
Questions How can timer interrupts used to determine an absolute, real-world measurement of time? How can time interrupts be used to implement arbitrary user timers? (e.g., when user executes sleep(5)) CSE 422S –Operating Systems Organization

15 CSE 422S –Operating Systems Organization
Timer Tick Rate: HZ The kernel operates on the granularity of timer interrupts Every 1/HZ seconds, stop the currently running process and figure out what to do next (run another process, run kernel-level services, etc.) What should HZ be set to? High HZ = frequent timer interrupts Low HZ = infrequent timer interrupts Tradeoff between high throughput and low latency CSE 422S –Operating Systems Organization

16 CSE 422S –Operating Systems Organization
Latency vs Throughput Target: low latency, high throughput Throughput Latency CSE 422S –Operating Systems Organization

17 CSE 422S –Operating Systems Organization
Latency vs Throughput Target: low latency, high throughput . Throughput . . . 10 HZ Latency CSE 422S –Operating Systems Organization

18 CSE 422S –Operating Systems Organization
Latency vs Throughput Target: low latency, high throughput . Throughput . Where should 100 HZ go? . . 10 HZ Latency CSE 422S –Operating Systems Organization

19 CSE 422S –Operating Systems Organization
Latency vs Throughput Target: low latency, high throughput . 1 HZ Throughput . . . 10 HZ 100 HZ 1000 HZ Latency CSE 422S –Operating Systems Organization

20 Basic Timer Interrupt in Linux
Historically the OS would run periodically: HZ found in /include/asm-generic/param.h OS function tick_periodic() runs each 1/HZ seconds jiffies variable tracks number of ticks since boot xtime variable tracks wall-clock time Current time since boot: jiffies * 1/HZ Current wall time: boot_time + jiffies * 1/HZ Timers are checked for expiry each tick Concerns with this approach: Unnecessary interrupts when only few processes running Excessive power consumption (can’t really idle processors) CSE 422S –Operating Systems Organization

21 Modern Timer Interrupt in Linux
Two fundamental operating modes: Periodic (CLOCK_EVT_MODE_PERIODIC) One-shot (CLOCK_EVT_MODE_ONESHOT) One-shot mode (CONFIG_NO_HZ): All timer events are independent (not periodic) Next timer interrupt is programmed for the next timer expiration Not rescheduled if no runnable tasks jiffies is maintained as though periodic Big power savings in idle CSE 422S –Operating Systems Organization

22 Measuring Time in Userspace
The timespec struct is declared in <time.h> Stores time info (in seconds and nanoseconds) struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; May need to normalize when adding, subtracting, etc. temp.tv_sec = t2.tv_sec - t1.tv_sec; // ok: monotonically non-decreasing temp.tv_nsec = t2.tv_nsec - t1.tv_nsec; // could give negative value Watch out for overflow (use unsigned long long etc.) Today’s studio will give you experience measuring time (and timer resolution) in user space programs CSE 422S –Operating Systems Organization


Download ppt "Time Sources and Timing"

Similar presentations


Ads by Google