Linux system Timers

Source: Internet
Author: User

There are many types of Linux clocks, roughly divided into two categories: periodic clocks that provide interrupts (RTC, Pit, and so on) that provide a count of incremental clocks (such as TSC)

Here is a brief list of several common.

(1) RTC

(2) TSC

(3) Kvm_clock

(4) ACPI_PM

View current system-supported clocks

Cat/sys/devices/system/clocksource/clocksource0/available_clocksource

View the currently used clock

Cat/sys/devices/system/clocksource/clocksource0/current_clocksource

Rtc

The RTC is powered by a separate battery and the system can read the time information from the RTC to ensure continuity of time running after the power outage. In the kernel, the RTC driver can be divided into two tiers, the first layer is the abstraction layer (hardware independent) to manage the RTC device, device node, attribute node registration and operation. The other layer is the underlying drive layer (hardware dependent)

Abstract layer program in the/DRIVERS/RTC directory, mainly related to the following several files:

    • ClassC is used to manage and register RTC Device Structures, SYSFS, PROCFS, and RTC classes;

    • RTC-DEV.C is used to register and manage the RTC device node , providing DEVFS operation interface for user space, the main operation is Rtc_read,rtc_ioctl;

    • RTC-PROC.C is used to manage the Procfs attribute node of RTC, and provides some interrupt status and flag query.

    • RTC-SYSFS.C is used to manage the Sysfs properties of the RTC device, such as obtaining the RTC device name, date, time and other attribute information;

    • INTERFACE.C provides operation interface for RTC-DEV.C and RTC low-level drives;

The RTC device driver registers the RTC device with the system via Rtc_device_register and generates the corresponding property file in the directory of proc, SYS, and so on. When the user initiates the operation through the/DEV/RTCX device node, it is necessary to access the real device driver through the interface interface.

In the low-frequency business scenario, the user process reads the/DEV/RTC through read (2), select (2) to obtain these interrupts. When the call is interrupted, the process blocks or exits until the next interrupt arrives. In a high-frequency business scenario, the user process checks for a number of interrupts to determine whether there are currently unhandled interrupts.

Interrupt frequency can be modified by/proc/sys/dev/rtc/max-user-freq file (default is 64hz)

RTC Bottom Operating interface

These operating interfaces are encapsulated in the

Int (*open) (struct device *); Turn on the device

void (*release) (struct device *); Releasing the device

Int (*ioctl) (struct device *, unsigned int, unsigned long);

Int (*read_time) (struct device *, struct rtc_time *); Read RTC time;

Int (*set_time) (struct device *, struct rtc_time *); Set RTC time;

Int (*read_alarm) (struct device *, struct RTC_WKALRM *); Read RTC alarm time;

Int (*set_alarm) (struct device *, struct RTC_WKALRM *); Set RTC alarm time;

Int (*proc) (struct device *, struct seq_file *); Used to provide PROCFS query RTC State interface;

Int (*SET_MMSS) (struct device *, unsigned long secs); Set the RTC time interface in S;

Int (*read_callback) (struct device *, int data);

Int (*alarm_irq_enable) (struct device *, unsigned int enabled); Interrupt enable interface;


The system clock and hardware clock information can be synchronized via Hwclock, or the RTC can be accessed via 0x70, 0x71 ports

System time synchronization to hardware time: hwclock–w, Hwclock--SYSTOHC

In the Hwclock display, set the RTC hardware time before the clock tick synchronization, the synchronization method is to open 1S an update interrupt, every 1S RTC will produce an alarm interrupt, and update the corresponding interrupt data, Use select at the application layer to monitor if RTC data is readable and continue to display or set hardware time operations if there is data


Tsc

TSC is a 64-bit TSC register located inside the CPU that records the number of clock cycles consumed by the processor since it was started, plus one per CPU clock cycle. TSC is highly accurate because the TSC varies with the processor cycle rate.

Can be read by RDTSC instructions.

[SEC = TSC value/cpu Clock rate]

    • To read the TSC value via the RDTSC instruction

Taticinline unsigned longlong native_read_tsc (void) {

unsigned Long LongVal;

ASM Volatile("RDTSC": "=a" (Val));

return Val;

}

This function places the TSC value in the EAX and edx registers and then into the Val variable.

Kvm_clock

The Kvm_clock is the KVM semi-virtualized default clock source. The approximate principle is to implement a Kvmclock driver on the client, and then the client uses this driver to query the VMM for time.

Workflow: The client allocates a memory page, which is communicated to VMM,VMM via the MSR Register to write the machine system time to the memory page, and the client obtains the time by reading the memory page.

The CPU steal time refers to how long the Vcpus waits for the CPU, and the Vcpus enters VMM through Vm-exit and then enters guest from VMM, which is the CPU steal. This can be used to measure virtual machine performance.


Linux system Timers

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.