Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The basic concept of clock driver in Linux kernel

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the basic concept of Linux kernel clock driver". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the basic concept of Linux kernel clock driver".

1 basic concepts

First of all, it is necessary to clarify some basic concepts in the Linux kernel clock driver.

(1) the frequency of clock cycle (clock cycle): the essence of 8253x8254 PIT is to count the clock cycle generated by the crystal oscillator. The number of clock pulses generated by the crystal oscillator in 1 second is the frequency of the clock cycle.

Linux uses the macro lock _ TICK_RATE to represent the frequency of the input clock pulse of 8254 PIT (this value is usually 1193180HZ on the PC), which is defined in the include/asm-i386/timex.h header file:

# define CLOCK_TICK_RATE 1193180 / * Underlying HZ * /

(2) clock tick (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 in PIT channel 0 determines how many clock cycles it takes to produce a clock interrupt, and therefore the interval length of a clock tick.

(3) the frequency of clock ticking (HZ): that is, the number of clock ticks generated by PIT in 1 second. Similarly, this value is determined by the counter initial value of PIT channel 0 (conversely, the counter initial value of 8254 PIT channel 0 can be determined by determining the frequency value of clock ticking). The Linux kernel uses the macro HZ to represent the frequency of clock ticking, and HZ has different values on different platforms. The value of HZ is 1024 for ALPHA and IA62 platforms, and 1024 for platforms such as SPARC, MIPS, ARM, and i386. The definition of this macro on i386 platform is as follows (include/asm- i386/param.h):

# ifndef HZ # define HZ 100 # endif

According to the value of HZ, we can also know that the specific time interval of a clock tick should be (1000ms/HZ) = 10ms.

(4) interval of clock ticking: Linux uses the global variable tick to represent the interval length of clock ticking. This variable is defined in the kernel/timer.c file, as follows:

Long tick = (1000000 + HZ/2) / HZ; / * timer interrupt period * /

The unit of tick variable is subtle (μ s). Because the value of macro HZ varies on different platforms, the result of the equation tick=1000000 / HZ may be a decimal, so it is rounded to an integer, so Linux defines tick as (1000000+HZ/2) / HZ, in which the function of HZ/2 in the divisor expression is to round the tick value upward into an integer.

In addition, Linux uses the macro trick _ SIZE as the reference alias (alias) of the tick variable, which is defined as follows (arch/i386/kernel/time.c):

# define TICK_SIZE tick

(5) Macro LATCH:Linux uses the macro LATCH to define the value in the counter to be written to PIT channel 0, which indicates that the PIT will generate a clock interrupt every few clock cycles. Obviously, LATCH should be calculated by the following formula:

LATCH= (number of clock cycles within 1 second) / (number of clock interrupts within 1 second) = (CLOCK_TICK_RATE) / (HZ)

Similarly, the result of the above formula may be a decimal and should be rounded. Therefore, Linux defines LATCH as (include/linux/timex.h):

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

Similarly, the HZ/2 in the divisor expression is also used to round the LATCH upward into an integer.

2 represents the kernel data structure of the current time of the system

As a UNIX-like operating system, the Linux kernel clearly uses the third method described at the beginning of this section to represent the current time of the system. The Linux kernel uses three important data structures to represent the current time of the system:

① global variable jiffies: this is a 32-bit unsigned integer that represents the number of clock ticks since the kernel was last started. Every time a clock tick occurs, the kernel's clock interrupt processing function timer_interrupt () increments the global variable jiffies by 1. The variable is defined in the kernel/timer.c source file, as follows:

Unsigned long volatile jiffies

The C language qualifier volatile indicates that jiffies is a variable that can be changed, so the compiler will make access to this variable never through the CPU internal cache.

② global variable xtime: it is a variable of timeval structure type, which is used to represent the relative second value of the current time to the UNIX time benchmark 1970 Murray 01 Murray 01 00:00:00. Structure timeval is a format of time representation in Linux kernel (there are many formats for time representation in Linux kernel, and each format has different time precision), and its time precision is microseconds. This structure is the most commonly used format for the kernel to represent time. It is defined in the header file include/linux/time.h, as shown below:

Struct timeval {time_t tv_sec; / * seconds * / suseconds_t tv_usec; / * microseconds * /}

Where the member tv_sec represents the second value of the current time from the UNIX time base, while the member tv_usec represents the microsecond value within one second, and 1000000 > tv_usec > = 0.

The Linux kernel maintains the current time through the global variable xtime of the timeval structure type, which is defined in the kernel/timer.c file, as follows:

/ * The current time * / volatile struct timeval xtime _ _ attribute__ (aligned (16))

However, the current time maintained by the global variable xtime is usually retrieved and set by the user, while it is rarely used by other kernel modules (jiffies is the most commonly used by other kernel modules), so updating xtime is not an urgent task, so this work is usually delayed to the bottom half of the clock interrupt (bottom half). Because the execution time of bottom half is uncertain, in order to remember when the kernel last updated xtime, the Linux kernel defined a global variable wall_jiffies similar to jiffies to hold the Jiffies value when the kernel last updated xtime. The bottom half of the clock interrupt updates the wall_jiffies to the current jiffies value each time the xtime is updated. 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 timezone structure type that represents the current time zone information of the system. The structure type timezone is defined in the include/linux/time.h header file, as follows:

Struct timezone {int tz_minuteswest; / * minuteswest of Greenwich * / int tz_dsttime; / * type of dst correction * /}

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

Struct timezone sys_tz; thank you for reading, the above is the content of "the basic concept of Linux kernel clock driver". After the study of this article, I believe you have a deeper understanding of the basic concept of Linux kernel clock driver, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report