Linux clock interrupts involve up to two global variables one is xtime and the other is jiffies.
There is a time-related clock: Real time Clock (RTC), which is a hardware clock that is used to keep the system time in place, and the system shuts down and keeps the chronograph on the motherboard's micro battery. When the system starts, the kernel
Initialize the wall by reading the RTC
Time, and is stored in the Xtime variable, that is, Xtime is obtained from the CMOS circuit, usually from a historical moment to the present time, that is, in order to obtain our operating system
Displays the date, and its precision is microseconds. This is the most important role of RTC.
Jiffies is a total number of clock interrupts recorded from the computer to the present. Jiffies in the Linux kernel is much more important than xtime, jiffies depends on the frequency of the system
Rate, Unit is
Hz, which is the reciprocal of the cycle, is usually the number of interrupts produced in a second, so if we need to know the exact time unit of the system, we need to convert it, if the frequency of our system is 200Mhz, then
The interval of one interrupt is 1 seconds/200,000,000hz=0.000 000
005 seconds, so theoretically the accuracy of our system is 5ns. The Linux system clock frequency is determined by a constant Hz,
Usually hz=100 (the Linux kernel starts from 100 to 1000 from the 2.5 kernel), his accuracy is 10ms (milliseconds). Which means that every 10ms
Broken. So the accuracy of Linux in general is 10 milliseconds.
The kernel generally obtains the current time through the jiffies value. Although the value represents the time interval since the last system boot to the current one, the lifetime of the driver is limited to the operating period of the system
(uptime), so it is also feasible. The driver uses the current value of jiffies to calculate the time interval between different events. The hardware provides a system timer for the kernel to calculate and manage time,
The kernel is programmed to preset the frequency of the system timer (that is, the hz=100 mentioned above). Beat Rate (tick
Rate), each cycle is called a tick (beat). Jiffies is a full-scale change in the kernel. The number of beats generated by the system startup. For example, if the calculation system is running for how long
can be calculated using Jiffies/tick rate.
Jiffies defined in the file
If you need more precise time to measure or record something, there is a xtime global variable in the kernel, the type is struct Timespec {time_t tv_sec; long tv_nsec;} According to this data structure, it is the NS level.
There is a current_kernel_time function that allows you to get the value of Xtime. But Xtime is updated in the clock interrupt, and a tick is often
10ms or 100ms, it can only guarantee when the clock interrupts the ISR call time, it returns the value is accurate to the NS level, and does not guarantee that any one call this function of the moment can do so, because
The Xtime update speed is several orders of magnitude worse than it.
Time base type for kernel:
1) System time
A monotonically Increasing value that represents the amount of time the
System has been running. The monotonically increasing system uptime can be achieved by time source,
Xtime and wall_to_monotonic are calculated.
2) Wall Time
A value representing the the human time of day, as seen on a wrist-watch. Realtime time: Xtime.
3) Time Source
A representation of a free running counter running at a known
Frequency, usually in hardware, e.g GPT.
Counter values can be obtained by Clocksource->read ()
4) Tick
A periodic interrupt generated by a hardware-timer, typically with a fixed interval defined by hz:jiffies
These time are interrelated and can be converted to each other.
System_time = Xtime + cyc2ns (Clock->read ()-clock->cycle_last) + wall_to_monotonic;
Real_time = Xtime + cyc2ns (Clock->read ()-Clock->cycle_last)
That is, real time is nanosecond from 1970 to now, and system time is the nanosecond that is booting to the present.
These two are the most important times, so Hrtimer can set an expiration time based on these two time. So the introduction of two clock base:
Clock_Realtime:base in the actual wall time
Clock_Monotonic:base on System Run
Clock_REALTIME Call Ktime_get_real () to get the real time, the function calculates the realtime with the equation mentioned above.
Clock_Monotonic Call Ktime_get(), using the System_time equation to get monotonic time.
Clock API
Clock_GetTime (clockid_t, struct timespec *)
Get the time for the clock
Clock_SetTime (clockid_t, const struct TIMESPEC *)
Set the corresponding clock time
Clock_Nanosleep (clockid_t, int, const struct TIMESPEC *, struct timespec *)
Process Nano Sleep
Clock_Getres (clockid_t, struct timespec *)
Get time accuracy, usually nanosec
The clockid_t defines four clock types:
Clock_REALTIME
System-wide Realtime clock. Setting this clock requires appropriate privileges.
Clock_Monotonic
Clock that cannot is set and represents monotonic time since some unspecified starting point.
Clock_process_cputime_id
High-resolution per-process timer from the CPU.
Clock_thread_cputime_id
Thread-specific cpu-time clock.
The first two mentioned earlier, and the second is related to the process/thread statistics time, the application layer can take advantage of these four clock, improve flexibility and accuracy.
Timer API
Timer can be set up process timer, single or periodic timing.
int Timer_create (clockid_t clockid, struct sigevent *restrict EVP, timer_t *restrict Timerid);
Create a timer.
CLOCKID specifies which clock base the timer is created under.
EVP (sigevent) can specify which signal the kernel sends to the process after the timer expires, and the parameters with the signal; The default is SIGALRM.
Timerid returns the ID number of the timer that was built.
In the signal processing function, you can use the Siginfo_t.si_timerid
Gets the current signal is triggered by which timer expires. Experiment, the maximum number of timer can be created and the pending in Ulimit
Signals has a relationship, and cannot exceed the number of pending signals.
int Timer_gettime (timer_t timerid, struct itimerspec *value);
Gets the time of the timer's next expiration.
int Timer_settime (timer_t timerid, int flags, const struct ITIMERSPEC *restrict value, struct Itimerspec *restrict ovalue) ;
Set the timer expiration time and interval period.
int Timer_delete (timer_t timerid);
Remove the timer.
These system calls will create a Posix_timer hrtimer that sends a signal to the process when it expires.
Linux Time module Three