
explains how the date command retrieves the current date and time, including the time zone. demonstrates running the command to display the current date, time, and time zone.
Explain how the uptime command reports system up time, current users, and 1/5/15 minute load averages, and how to interpret these values relative to CPU cores.
Discover epoch time, or Unix time, as the number of seconds since January 1, 1970, accessible via the time system call. Watch a five-second sleep and a time check.
Discover how to use ctime to convert a time value into a human-readable string, displaying seconds or other units, via the time library.
use gettimeofday to capture wall clock time with microsecond resolution via the timeval structure, convert seconds and microseconds to milliseconds, and pass a null timezone pointer since timezone is obsolete.
This lecture shows that gettimeofday() cannot reliably measure elapsed time when system clock is changed by NTP. It demonstrates the need for a timer that remains accurate despite clock adjustments.
Learn how the monotonic clock measures elapsed time safely, unaffected by system time changes, by reading clock monotonic and comparing it to wall clock time for accurate timing in code.
Compare CLOCK_MONOTONIC and CLOCK_BOOTTIME to see how they handle suspend time. MONOTONIC excludes suspend, while CLOCK_BOOTTIME includes it for elapsed time measurements.
Learn how CLOCK_MONOTONIC_RAW avoids time adjustments, unlike CLOCK_MONOTONIC, to measure very short intervals, while CLOCK_MONOTONIC suits longer durations such as minutes, hours, or days.
Explore CLOCK_REALTIME, the real-time clock that provides time of day and system time for stamping and displaying the actual time to users.
Clock process cputime id measures the CPU time consumed by a process, showing whether your program uses CPU time or waits on I/O; it excludes time waiting for user input.
Calculate the time a micro CPU takes to increment a variable using clock time, starting and stopping the measurement to capture the elapsed duration and display it.
Compare two methods for dividing by 256—division and a night shift—and time them with clock_gettime to show the night shift is faster due to direct assembly instruction support.
Compare clock_monotonic and CLOCK_PROCESS_CPUTIME_ID by measuring elapsed time between user input and function calls. Monotonic time advances continuously, while CPU time reflects actual processor usage.
Explore how CLOCK_THREAD_CPUTIME_ID measures CPU time for a thread, compare it with real-time clocks, and interpret timing output that shows per-thread versus wall-clock duration.
Use clock_settime to set the clock and adjust time, with examples of getting current time and adding minutes, and note that monotonic clocks cannot be set for the wall clock.
Demonstrates how clock_getres retrieves the resolution of a specified clock, including alarm clock, monotonic clock, and time clock, revealing nanosecond precision.
Learn how the times function reveals user and system time for a process and its children, measure clock ticks per second, and use spend time to illustrate runtime durations.
Explore the getrusage interface to measure timing and resource usage for self and child processes, reporting seconds and microseconds with various clocks in user space.
The timing subsystem in the Linux kernel manages current time and elapsed time, timestamps for files and packets, alarms and timers, and two hardware types: clocks and fixed-frequency timers.
Explore how the RTC hardware clock, battery-backed, initializes the system clock and runs independently from the software clock; learn to read and set RTC values via system interfaces.
Learn how a kernel module reads the real-time clock to obtain hours, minutes, and seconds. See how a time value such as 9:31 is produced.
Explore how the RTC subsystem uses a clock to increment time, generate interrupts for periodic events, alarms, and timers, and configure frequencies to trigger actions at specific moments.
Understand the rtc ioctl interface, enabling per-second interrupts, reading and setting the time, and user-space access via a symbolic link with permission considerations.
Configure RTC periodic interrupts by setting the frequency (how many interrupts per second), enabling and disabling the stream, and verifying operation up to a 64 Hz maximum.
Configure an RTC alarm interrupt by setting the alarm time, enabling the interrupt, and waiting for the alarm to trigger after a chosen interval such as 30 seconds.
Explore why the real-time clock, RTC, is used only at boot because reading RTC registers is slow, while a system clock runs during the operating system's uptime.
The lecture explains the timestamp counter, a 64-bit register that increments with every clock cycle, providing high-resolution timing to measure instruction costs and processor frequency.
Read the TSC from kernel space using the same clock frequency and logic as in user space.
Explore how out-of-order execution can reorder instructions and affect cycle timing, and learn how the iDesk copy instruction helps serialize execution to maintain in-order processing in the RDTSCP lecture.
Explore the issues of timestamped measurements in multiprocessor systems, including per-core time stamps and single-call synchronization. Learn how Constant BFC and DC keep clocks synchronized despite frequency changes.
Explore kernel functions for the time stamp counter (TSC) and learn architecture independent, portable functions that read cycles across platforms, enabling a universal driver.
the programmable interval timer generates an output when the programmed value is reached, supports one-shot or periodic modes, and serves as a three-channel memory-mapped system timer in early Linux.
Explore how each CPU's local apic provides a 32-bit timer configurable in one-shot, periodic, or deadline mode to generate interrupts for CPU, with deadline mode using CPU timestamp for precision.
Present the high precision event timer, a 64-bit counter at least 10 MHz, developed by Intel and Microsoft, with one-shot or periodic interrupts.
Explore Linux clock source abstraction, listing and selecting available clock sources, and understand how timer devices, events, and event handlers manage hardware timing across cores.
Explore how the timing subsystem uses a configured interrupt frequency to drive system time, with 250 interrupts per second equating to about four milliseconds and architecture-specific definitions.
Explore how a global volatile unsigned long variable named fees increments over time during a four-second busy loop, illustrating timer ticks and the jiffies timing subsystem in practice.
Explore how the size of unsigned long changes across 32- and 64-bit architectures by writing a small program that prints its size on the system.
Explain why jiffies isn't declared as 64-bit on x86 due to non-atomic 64-bit access, and describe the role of 32-bit/64-bit reads, synchronization, and sequence locks.
Explore how jiffies wraparound occurs with unsigned long time variables, causing overflow; learn macro-based time_after and time_before logic to safely compare times.
Explore the initial value of jiffies on boot, defined by a preprocessor as minus 300, not zero. Examine how unsigned overflow after five minutes affects behavior and testing.
Explore the timing subsystem in depth by examining how an initial jiffies value, such as minus three hundred, becomes zero after five minutes due to interrupts.
Explore why the INITIAL_JIFFIES macro uses double casting to handle negative values, unsigned and signed extensions, and architecture differences between 32-bit and 64-bit systems.
Explore converting jiffies to seconds, milliseconds, microseconds, and nanoseconds using conversion functions, with examples showing input arguments, returned values, and wraparound behavior.
Measure the module load time using jiffies by recording jiffies at load and unload. Compute the difference and divide to convert to seconds, revealing how long the model stays loaded.
Explore updating jiffies in the timing subsystem, showing an unsigned long write to zero, then warn that the scheduler relies on this value and modifying it risks bad outcomes.
Explore timer interrupt processing where architecture-dependent event handlers execute clock events, call architecture-independent code, increment time and base statistics, update data processing times, and run the scheduler.
Explore busy looping with jiffies, calculating delays by time multiples, and compare spinning in a loop with using the scheduler to yield to more important tasks in a process context.
Explore how to convert between time representations in the timing subsystem, including milliseconds, microseconds, and nanoseconds, using timespec structures and bidirectional conversions with practical examples.
Explore how the timing subsystem implements busy loops to produce microsecond, nanosecond, and millisecond delays, using a calibrator to determine loops per variable and approximate timing.
Periodic interrupts raise power use in the Linux kernel by waking the processor regularly; dynamic ticks or tickless timers fire only when needed, reducing wakeups and extending battery life.
Explore dynamic clock interrupt configurations, including periodic ticks and aperiodic options, to balance real-time performance, heavy workloads with short bursts, and power consumption for CPUs with varying tasks.
Learn how timers schedule a function to run in the future, for timeouts, by initializing, setting expiration, providing a callback, and activating the timer, focusing on low resolution timers.
Learn how to implement low resolution timers in a timing subsystem using a linked list of timer structures with callbacks, expiration times, and flags, including static and dynamic initialization.
Learn how to specify an expiration time for a timer by providing the time argument; the timer handler executes when time reaches expiration, with conversions among seconds, nanoseconds, and milliseconds.
Determine whether a timer handler runs in interrupt or process context using a macro, then compile and load the module to observe the five-second timer in the right context.
Explore timing subsystem and timer implementation, showing how a function checks for expired timers on the current processor, handles soft outcalls, and triggers a timeout function after a 10-second delay.
Investigate whether interrupts are enabled or disabled on the local processor while the timer handler runs, using the status function and noting the 10-second delay.
Examine whether a timer handler runs in softirq context or hardirq context. The example demonstrates that it runs in softirq context and not in hardirq context.
Examine whether the kernel checks all timer entries on each softirq raise or only a prioritized first partition of timers grouped by expiration values.
Deactivate a timer presents a function that returns whether the timer is activated, and demonstrates the impact of removing a module without cleanup on timing.
Explain del_timer_sync and its use on assembly machines and multiprocessor systems to wait for the time handler to finish. Contrast with the non-sync variant that does not wait.
Determine whether a given time is pending using the timer_pending function, which returns 1 while pending and 0 when not, as expiration times like 10 seconds are evaluated.
Explore how periodic timers in the timing subsystem work by setting an initial 10-second delay, after which the timer's function runs again every second until the module is removed.
Explore how to synchronize a global structure between the process context and a timer handler in the timing subsystem, using spin locks instead of mutexes or semaphores.
Run a timer handler that executes a function every one second, demonstrating multimodality by using milliseconds and headset input to achieve the same behavior.
Learn how timer flags in the timing subsystem work, including static initialization and the deferrable flag, which delays timer services until the CPU is idle and a timer expires.
Explore how schedule_timeout implements timeout by suspending a task with a timer, scheduling expiry, and resuming the process when the timer expires.
Explore the problem of low resolution timers, where timing precision is coarse and causes video and audio playback skips, and preview the need for high-resolution timers.
Understand why low resolution timelines are inadequate for real-time work and how to enable high resolution timers. Learn to verify support, read the file, and compare nanosecond versus millisecond resolutions.
The ktime_t type stores a time value in nanoseconds as a signed 64-bit integer, with seconds and nanoseconds arranged by endianness to form a single 64-bit value.
Explore the macros and functions for ktime_t, including static initialization, converting seconds to nanoseconds, and computing time differences in nanoseconds using practical examples.
Use macros and functions for add and subtract operations on ktime_t values, combining 64-bit parts, handling seconds and nanoseconds without modifying inputs.
Explore ktime_t conversion functions to convert between seconds, nanoseconds, and microseconds, using timespec representations and existing macros, with practical examples of forward and reverse conversions.
Demonstrate time conversion functions that translate nanoseconds and microseconds into time spent, compare monotonic and wall clocks for accurate elapsed time, and explain elapsed time during suspend.
Explore the timing subsystem with ktime accessors, demonstrating how to read monotonic time for measuring elapsed intervals that remain stable despite system clock changes, using a busy loop example.
Explore ktime accessors part 2 to compare monotonic clocks with clocks that continue during suspend operations, learn to measure expiration times across suspend operations, and ensure time keeps ticking.
Explore ktime accessors part 3 for clocks, current time, and time stamps, highlighting how to measure elapsed time, handle leap seconds, and obtain monotonic time for userspace.
Measure time with nanoseconds using a 64-bit unsigned value, showing a busy loop of one second and capturing nanosecond values to compare with milliseconds.
Explore using timespec64 to output realtime timestamps in seconds and nanoseconds, avoiding manual division by nanoseconds and printing results from repeated get realtime specs calls.
Use a monotonic clock to measure time in seconds and nanoseconds, calculate elapsed duration, and track how long a model remains loaded. Verify timings with simple outputs and time differences.
This lecture explains how uptime is calculated and measured, examining code that creates a time profile and uses the show function to display uptime in seconds and nanoseconds.
Learn how gettimeofday is implemented to obtain the time of day, using a clock with nanosecond timing and dividing nanoseconds to produce microseconds for a second-based resolution.
Explore how clock_gettime implements time retrieval by selecting a clock, passing its id, and filling a timespec structure via a system call, then copying the result to user space.
Explore coarse variants in the timing subsystem, showing they are quicker but less accurate than non-coarse versions. Compare cycles and nanoseconds to evaluate performance and trade-offs.
Explore why coarse variants run faster by contrasting hardware clock reads with a globally updated timekeeping structure that gets updated every time.
Introduces a timing subsystem using a time-sorted list with the next timer at the head and a red-black tree for fast insertions, plus initialization and absolute versus relative expiry settings.
Learn how to cancel a timer to prevent crashes when modules are removed, handle active or expired timers, and use the timer state for debugging and return values.
Understand how the return value of a callback timer sets the expiration and restarts it for periodic triggers every 10 seconds using a monotonic clock, current time, and new time.
Use hrtimer_forward_now to advance time and simplify code by forwarding the current time base, and view a periodic timer that fires every 10 seconds with forward calls and callbacks.
Measure clock cycles with a monotonic clock, verify frequency, and compare absolute versus relative timer modes to schedule events, such as firing after ten seconds or one nanosecond.
Explore how clocks and modes like absolute, monotonic, and relative affect timers and alarms. Learn why POSIX compliance often requires converting timers to monotonic for reliable timing.
Learn to verify a timer's callback with a helper that returns 1 when running and 0 when not. Initialize the clock and start the timer to see the callback status.
Learn how to restart a high-resolution timer using hrtimer_restart, manage timer cancellation, and ensure the callback executes when the timer expires.
Learn how the hrtimer_get_remaining function reports the time left before a timer expires, including positive and negative values, and debug absolute mode to avoid confusion.
Examine how a high-resolution timer triggers a callback in a context, with interrupt states, and how to verify execution via context dumps and delays in 250 ms or microseconds.
Timing Measurements in Linux Kernel
Many computer activities are based on timing measurements.
E.g. Your Computer display is turned off, if you have not pressed a key or moved your mouse for a particular time.
Linux timing subsystem mainly handles two types of timing activities
1. Keeping the current time and date
a. time() , gettimeofday() and clock_gettime()
b. Time stamps for files and network packets
2. Maintaining Timers
a. Mechanisms to notify kernel and user space (ex. alarm()) that a certain interval of time has elapsed.
Hardware Devices
Linux depends on hardware devices to maintain time. These devices can be basically classified into two types:
1. Clocks/Counters: provide precise time measurements
Used to keep track of current time of day
2. Timers: Issue interrupts at fixed, predefined frequency.
Used for implementing software timers
What will you learn from this course?
Various timing commands: date, uptime
POSIX Clocks: CLOCK_BOOTTIME, CLOCK_MONOTONIC_RAW, CLOCK_REALTIME, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID
Timing System calls: ctime, gettimeofday, clock_gettime, clock_settime, clock_getres, times, getrusage
Setting time from userspace
How to measure time for a particular instruction
Hardware Devices used in timing measurement: RTC, TSC, Programmable Interval Timer, APIC, High Precision Event Timer
Jiffies
Low Resolution and High Resolution Timers
This course comes with a 30 day money back guaranteed!. If you are not satisfied with the course, you'll get your money back
So what are you waiting for, enroll now and take the next step in learning Timing subsystem in Linux Kernel