Linux Timing Architecture

Source: Internet
Author: User
Tags time and date

1 Basic Concepts

The timing mechanism, along with some more visible kernel activity (such as check timeouts), drives the process to switch.

Two main timing measurements:

    • Save the current time and date so that they can be returned to the user program through the Ftime () and gettimeofday () system calls.
    • The maintenance timer, which can tell the kernel or user program that a time interval has passed.

Timing measurements are performed by several hardware circuits based on fixed-frequency oscillators and counters.

2: and Timer circuit

Clock circuits are used to track the current time and to produce accurate time measurements.

Timer circuits are programmed by the kernel, so they interrupt with Udingde, pre-defined frequencies.

Classification of clock circuits

    • Used to track the current time
      • Real-Time Clock RTC
      • Time Stamp counter TSC
    • Generates periodic clock interrupts for timing
      • Programmable Interval Timer Pit

2.1 Interrupt generated on real-time clock rtc--irq8

When the PC is powered off, the RTC also continues to work.

The kernel accesses the RTC via the 0x70 and 0X71I/O ports.

can emit periodic interrupts on the IRQ8, frequency between 2hz~8192hz, programmable


2.2 Timestamp counter TSC

In the 80x86 microprocessor, there is a CLK input lead that receives the clock signal from the external oscillator. The TSC adds 1 when each clock signal arrives.

TSC is a 64-bit timestamp counter Register, assembly instruction RDTSC read this register. Linux must determine the frequency of the clock signal when initializing the system.

Get the TSC clock frequency: the CALIBRATE_TSC () function calculates the actual CPU frequency by calculating the number of clock signals generated by a time interval of approximately 5ms.

Linux uses RDTSCLL () or RDTSCL () to read the TSC.

Compared to the programmable interval timer, TSC can obtain a more accurate clock.

2.3 Programmable Interval Timer pit

Using the I/O port 0x40~0x43

Linux programmed the first pit of the PC to make clock interrupts to the IRQ0 at a frequency greater than 1000Hz, i.e. one clock interrupt per 1ms, which is called a tick, and its length is stored in nanoseconds in tick_nsec variables.

Initialized by Setup_pit_timer (). Initializes the clock interrupt frequency in Init_pit_timer ().

Macro definitions related to the system clock signal:

(1) macro definition Hz

The frequency of programmable timer interrupts required by the system clock, that is, the number of ticks per second, under different architectures

(2) macro definition Clock_tick_rate

Recorded input clock frequency for driving programmable timer operation under different architectures

(3) macro definition latch

The ratios of the above two macro definitions are recorded to set the initial value of the Counter register counter in the programmable timer during kernel initialization.

3 Linux Timing Architecture

The timing architecture of Linux is a set of time-flow-related kernel data structures and functions.

Function:

    • The time elapsed since the system startup was updated
    • Update Time and date
    • Determine the execution time of the current process, and consider whether you want to preempt
    • Update Resource Usage Statistics count
    • Check for expired soft timers

The kernel has two basic timing functions:

    • Keep current up-to-date time
    • Calculates the number of nanoseconds traversed within the current second

In a single-processor system, all timed activities are triggered by a clock interrupt on the IRQ0, including:

    • The part that executes immediately in the interrupt
    • Deferred execution as part of the lower half

3.1 Timing architecture Data Structure 3.1.1 Timer object (clock source)

In order to use a uniform method to handle possible timer resources, the kernel uses a "timer object", which is a descriptor for the timer_opts type. The two most important of these methods:

Mark_offset: Called by the clock interrupt handler and records the exact time at which each beat arrives with the appropriate data structure.

Get_offset: Uses the recorded value to calculate the elapsed time since the last clock interrupt (beat).

Both of these methods enable the LINUXD timing architecture to hit the sub-beat resolution, which means that the kernel can measure the current time with a higher precision than the cycle period, which is called "timed interpolation".

During kernel initialization, the Select_timer () function sets the address of Cur_timer to the appropriate timer object (clock source). The variable timer_cur stores the address of the corresponding timer, which is the best timer resource available to the system.

3.1.2jiffies variables

A counter that is used to record the total number of beats that have been generated since the system started.

Because the number of system clock interrupts generated in a second equals the value of the macro definition Hz, the value of the variable jiffies increments by Hz within one second.

3.1.3xtime variables

The Xtime variable holds the current time and date, which is a timespec type of data structure. This allows the kernel to mark certain objects and events in time, such as when the file was created, modified, last accessed, or used by the user process through a system transfer.

Basic each tick is updated once.

3.2 Timing architecture on single-processor systems

Test Center: Functions of the Tick_handle_periodic function (functions of the Linux clocking architecture)

Tick_init calls Clockevents_tegister_notifier to register tick_notifier on Clockevents_chain.

Update_wall_time () completes the update of the variable xtime.

Time_init_hook () to set the system clock interrupt handler.

In the clock interrupt handler function:

The Tick_init function is called, and the functions in many of the processes in the book are eventually called by this function, and the flow is as follows:

4 Soft timers and delay functions

Soft timer:

    • Dynamic Timer (Kernel)
    • Interval timer (can user)

Dynamic timer: Dynamically created and revoked, there is no limit to the number of dynamic timers currently active

A timer is a software feature that allows a function to be invoked at a given time interval at some point in the future. Each timer contains a field that indicates how long the timer will take to expire. The initial value of this field is the current jiffies, plus the appropriate number of Beats.

Note that timers are not suitable for real-time applications that must strictly adhere to timing, because the timer check is always performed by a deferred function.

4.1 Create and activate a dynamic timer--init_timer initialize a Time_list object

    • To create a new Timer_list object
    • Call Init_timer initialization and set the function and parameters to be processed by the timer
    • Set timing Time
    • Use Add_timer to add to the appropriate list
Specific steps:

The data structure of the 4.2 dynamic timer is used to compare with the system core variable jiffies.
    • Member variable functions: This function pointer variable holds the function to be executed after the kernel timer expires, that is, the timer timeout handler function.
    • Member variable data: The unsigned long variable is used as the parameter of the timer timeout handler.
    • Member variable base: this pointer variable indicates which processor in the system the kernel timer node belongs to, and in the process of initializing the kernel timer node using function Init_timer (), point the pointer to a member variable t_base for each processor variable tvec_bases.
4.3 Maintenance of dynamic timers

Main functions of Run_timer

    • Timer time indication parameter plus one
    • Timer removal for processing
    • Process the expiration timer in turn

Delayed work of dynamic timer application

Dynamic Timer Application Schedule_timeout:setup_time_on_stack (&timer, Process_timeout, (unsigned long) current); When the timer time is up, the Process_timeout function changes the current process to a waiting state.

4.4 Delay Function:

When the kernel waits for a short time interval, such as a few milliseconds, the device driver typically waits for a predefined entire microsecond until the hardware completes certain operations. In these cases, the kernel uses the Udelay () and Ndelay () functions: The former receives a microsecond interval as its argument and returns after the specified delay, which is similar to the former, but the parameter specifying the delay is nanosecond.

Http://www.cnblogs.com/suzhou/archive/2013/06/04/3638986.html

Linux Timing Architecture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.