Time in linux

Source: Internet
Author: User

Generally,Linux operating systemYou can use three methods to represent the currentTimeAnd date:
① The simplest method is to use a 64-bit counter to count the tick of the clock.
② The second method is to use a 32-bit counter to count the second, and also use a 32-bit auxiliary counter to counter the tick of the clock. The child accumulates until one second. Since 232 and later than 136, this method can make the system work well until the 22nd century.
③ The third method is to count by clock tick, but it is relative to the number of tick answers since the system was started, rather than relative to a certain external time; when reading an external backup clock such as RTC) or when you enter the actual time, the current time of the system is calculated based on the current tick answer count.
UNIX operating systems usually use the third method to maintain the system's time and date.
1. Basic Concepts
First, it is necessary to clarify some basic concepts of Linux kernel clock drivers.
1) clock cycle) frequency: 8253/8254 the essence of PIT is to count the clock cycle generated by the crystal oscillator, the number of clock pulses generated by a crystal oscillator in one second is the frequency of the clock cycle.
Linux uses the macro CLOCK_TICK_RATE to indicate the frequency of the 8254 PIT input Clock Pulse in PC, which is usually 1193180 HZ), which is defined in the include/asm-i386/timex. h header file:

 
 
  1. #define CLOCK_TICK_RATE 1193180 /* Underlying HZ */ 


2) clock tick): We know that when the counter of PIT channel 0 is reduced to 0, it generates a clock interrupt on IRQ0, that is, a clock tick. The initial value of the counter of PIT channel 0 determines how many clock cycles will be used to generate a clock interrupt. Therefore, it determines the time interval of a clock tick.
3) clock tick frequency HZ): that is, the number of times the PIT generates a tick in one second. Similarly, this value is determined by the initial values of the PIT channel 0 counter. After determining the frequency value of the clock tick, you can determine the initial values of the 8254 PIT channel 0 counter ). The Linux kernel uses macro HZ to indicate the frequency of clock ticking, and HZ has different defined values on different platforms. The HZ value for ALPHA and IA62 platforms is 1024, And the HZ value for those platforms such as Linux, MIPS, ARM, and i386 is 100. The macro definition on the i386 platform is as follows: include/asm-i386/param. h ):

 
 
  1. #ifndef HZ   
  2. #define HZ 100   
  3. #endif   

Based on the HZ value, we can also know that the specific time interval of a tick answer should be 1000 ms/HZ) = 10 ms.
4) interval of clock tick answer: Linux uses the global variable tick to indicate the Interval Length of the clock tick answer. This variable is defined in the kernel/timer. c file, as follows:
Long tick = (1000000 + HZ/2)/HZ;/* timer interrupt period */
The unit of the tick variable is microμs. Because the macro HZ values on different platforms are different, the result of the equation tick = 1000000 HZ may be decimal, therefore, it is rounded to an integer, so Linux defines tick as 1000000 + HZ/2)/HZ, HZ/2 in the divisor expression is used to round up the tick value into an integer.
In addition, Linux uses the macro TICK_SIZE as the alias for referencing the tick variable alias. Its definition is as follows: arch/i386/kernel/time. c ):

 
 
  1. #define TICK_SIZE tick   

5) macro LATCH: in Linux, macro LATCH is used to define the value of the counter to be written to PIT channel 0. It indicates that PIT will generate no clock interruption for many clock cycles. Obviously LATCH should be calculated by the following formula:
LATCH = the number of clock cycles within 1 second) ÷ the number of clock interruptions within 1 second) = CLOCK_TICK_RATE) ÷ HZ)
Similarly, the result of the above formula may be a decimal number, which should be rounded down. Therefore, Linux defines LATCH as include/linux/timex. h ):

 
 
  1. /* LATCH is used in the interval timer and ftape setup. */   
  2. #define LATCH ((CLOCK_TICK_RATE + HZ/2) / HZ) /* For divider */   

Similarly, HZ/2 in the divisor expression is used to round up LATCH into an integer.
2 indicates the kernel data structure of the current system time
As a UNIX operating system, the Linux kernel clearly uses the third method described at the beginning of this section to indicate the current time of the system. The Linux kernel uses three important data structures to indicate the current time of the system:
① Global variable jiffies: A 32-bit unsigned integer used to indicate the number of times the clock has been ticking since the last kernel startup. Every time a clock tick occurs, the kernel's clock interrupt processing function timer_interrupt must add 1 to the global variable jiffies. This variable is defined in the kernel/timer. c source file, as follows:

 
 
  1. unsigned long volatile jiffies;   

The C language qualifier volatile indicates that jiffies is a variable that is easy to change. Therefore, the compiler will never access this variable through the internal CPU cache.
② Global variable xtime: it is a timeval structure variable used to represent the relative second value of the current time from the UNIX time reference 00:00:00. The timeval structure is a time format in the Linux kernel. the Linux kernel has multiple time formats, each of which has different time precision. The time precision is microseconds. This structure is the most common format used when the kernel represents time. It is defined in the header file include/linux/time. h, as shown below:

 
 
  1. struct timeval {   
  2. time_t tv_sec; /* seconds */   
  3. suseconds_t tv_usec; /* microseconds */   
  4. };   

The member TV _sec indicates the second value of the current time from the UNIX time benchmark, and the member TV _usec indicates the microsecond value within one second, and the value is 1000000> TV _usec> = 0.
The Linux kernel uses the global variable xtime of the timeval structure to maintain the current time. This variable is defined in the kernel/timer. c file, as shown below:

 
 
  1. /* The current time */   
  2. volatile struct timeval xtime __attribute__ ((aligned (16)));   

However, the current time maintained by the global variable xtime is usually used for retrieval and setup by users, while other kernel modules usually seldom use jiffies, which is the most used by other kernel modules ), therefore, updating xtime is not an urgent task. Therefore, this task is usually delayed to the half-bottom half of the clock interruption. Because the execution time of bottom half is uncertain, to remember when the last xtime kernel was updated, the Linux kernel defined a global variable wall_jiffies similar to jiffies, to save the jiffies value of the last kernel update xtime. The bottom half of the clock interrupt is updated to the current jiffies value every time xtime. The global variable wall_jiffies is defined in the kernel/timer. c file:
/* Jiffies at the most recent update of wall time */
Unsigned long wall_jiffies;
③ Global variable sys_tz: it is a global variable of the timezone structure type, indicating the current time zone of the system. The structure type timezone is defined in the include/linux/time. h header file, as follows:

 
 
  1. struct timezone {   
  2. int tz_minuteswest; /* minutes west of Greenwich */   
  3. int tz_dsttime; /* type of dst correction */   
  4. };   

Based on the above structure, Linux defines the global variable sys_tz in the kernel/time. c file to indicate the current time zone of the system, as shown below:

 
 
  1. struct timezone sys_tz;  

Through understanding of the above article, we have a deep understanding of the time management in Linux, and have their own experiences in the time composition and operation in Linux.

  1. 1996-2010: Important Linux system viruses
  2. Run MeeGo SDK on mainstream linux systems
  3. Install the system log server in Linux
  4. Five methods available for installing linux
  5. Summary of Embedded linux System Development
  6. Linux porting

Related Article

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.