Timing mechanism and related time functions in Linux

Source: Internet
Author: User

1. Time-related hardware

The time in computer systems is mainly provided by three clock hardware: Real Time
Clock, RTC), programmable interval timer, pit), timestamp
Counter, TSC ). These clock hardware provide clock square wave signal input based on fixed frequency crystal oscillator.

Generally, the Linux kernel requires two types of time:

The first type is the incremental clock in one step without sending interruption. The software needs to actively read its counter register to obtain the time. TSC belongs to this category.

The other is a timer that maintains a fixed cycle to remind the kernel or user that a period of time has elapsed. Periodic sending interruption is used for recording. Pit belongs to this category.

The RTC is powered by a battery on the motherboard. It is maintained on the date and time of shutdown. RTC provides a timing standard for the entire computer. It is the underlying clock data, also known as the hardware clock CMOS clock.

The most typical pit Timing/Counter chip is intel
8253/8254 Programmable Timing/counting chip. The timer/Counter receives the input pulse from RTC, and then starts to decrease the count. When the Count reaches zero, an output pulse is generated, causing real-time interruption of the processing process.
And then start counting from the beginning after the Timer/counter reset. At startup, the operating system initializes the system clock by obtaining the time data in RTC, and then triggers the clock by counting down the timing/counting chip.
The system clock (clock tick) is formed, and the frequency is roughly between-Hz.

TSC is an increasing counter that adds 1 to every clock signal of the CPU. That is to add 1 to the processor frequency. That is, the update frequency on mainstream processors can reach GHz.

2. Time-related data structure

Linux has two important data structures: one is a 32-bit unsigned integer global variable jiffies,
1 (usually 1 ms-10 ms) is added when the clock is interrupted. During Linux running, this time is recorded, such as process time slice update and scheduled service execution, it can be considered as a Linux running
Heartbeat. The other is the global variable xtime, which is a variable in the timeval structure. Used to indicate that the current time is earlier than the Unix time reference
The relative second of 00:00:00. The time precision is nanoseconds (the previous version is microseconds ). Because xtime is mainly used for query, xtime update is put
The second half of timer_interrupt () is executed, unlike jiffies, not every time the clock is interrupted. So it seems to be slower than jiffies, and the two are not synchronized.

3. Time-related system calls

System calls for obtaining time mainly include time () and gettimeofday ()

Time () simply returns TV _sec in xtime to the user space. The Unit is seconds.

Gettimeofday () is used to obtain the current time information and time zone information with the precision of us. First, use the current
Jiffies minus
Jiffies, get a correction value, that is, the difference between the last modified xtime and the present, but this value is accurate in milliseconds. To obtain the US precision, use the current TSC value minus the occurrence of the last clock interruption.
Use these two values to update the xtime and return the value to the user space. In 2.6.20, the main call path is as follows:

Sys_gettimeofday ()->

Do_gettimeofday ()->

Offset = time_interpolator_get_offset ();->

Time_interpolator_get_counter ();->

Time_interpolator_get_cycles ();->

Get_cycles ();->

Rdtscll ();->

Get_scheduled_cycles ();->

Native_read_tsc ();->

ASM volatile ("rdtsc": "= A" (VAL); // read the value of TSC

4. Get a time with higher precision

In Linux, the precision of xtime is NS and getnstimeofday () is available. It is not provided as a system call. You can achieve this by yourself when you need to obtain NS-level data in the user space. See sys_gettimeofday and getnstimeofday ():

Sys_gettimeofday (struct timeval _ User * TV ){

Offset = time_interpolator_get_offset ();

SEC = xtime. TV _sec;

Nsec = xtime. TV _nsec;

USEC = (nsec + offset)/1000;

KTV-> TV _sec = sec;

KTV-> TV _usec = USEC;

Copy_to_user (TV, & KTV, sizeof (KTV ));

}

Getnstimeofday (struct timespec * TV)

{

SEC = xtime. TV _sec;

Nsec = xtime. TV _nsec + time_interpolator_get_offset ();

TV-> TV _sec = sec;

TV-> TV _nsec = nsec;

}

5. Clock Synchronization

In a single processing system, both the system time (updated by clock interrupt) and the TSC (updated by the CLK signal of the processor) are operated by the hardware and run independently, therefore, non-synchronization may occur (for example, jiffies will not be updated when the Linux clock is blocked ). Linux uses TSC to correct the system clock at the appropriate time.

In a multi-processing system, because there are multiple processors, clock interruptions are broadcast in the processor, and one of them processes clock interruptions. However, TSC is owned by each processor and needs to be synchronized between each TSC.

In virtual systems (such as VMware and xen), clock interruptions are generated by software simulation, and TSC cannot be directly read from the hardware. Virtual software must utilize the capabilities provided by the processor, further processing is performed on the real value of TSC.

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.