Get a higher time value "Go" in the Linux kernel than jiffies precision

Source: Internet
Author: User

Transferred from: http://blog.chinaunix.net/uid-20672257-id-2831219.html

The kernel generally obtains the current time through the jiffies value. Although this value represents the time interval since the last system boot to the current one, it is possible because the driver's lifetime is limited to the operating period of the system (uptime). 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, and the kernel programmatically presets the frequency of the system timer, which is the tick rate, which is called a tick (the beat) for each cycle. The Linux kernel starts with the 2.5 kernel to turn the frequency from 100 to 1000 (which of course brings many advantages,
Also have some drawbacks). Jiffies is a global variable in the kernel that is used to record the number of Beats generated from the start of the system. For example, if the calculation system is running for how long, it 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 also 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 the clock interrupt ISR call time, it returns the value is accurate to the NS level, and does not guarantee any call this function of the moment can be like this, The reason for this is that Xtime's update speed is several orders of magnitude worse than it.

If you need to be precise to a subtle level, you can use the Do_gettimeofday function. The function does not return today is the week of the week or similar information; it uses a microsecond value to populate a pointer variable that points to the struct timeval. The corresponding prototypes are as follows:

#include

void Do_gettimeofday (struct timeval *tv);

The do_gettimeofday declared in the source code has a "near-microsecond resolution" on the architecture outside of Alpha and SPARC, as well as the resolution of the Jiffies value on Alpha and SPARC. The ported version of SPARC is upgraded in the kernel of version 2.1.34, which supports finer granularity of time measurement.

void Do_gettimeofday (struct timeval *tv)
{
unsigned long flags;
unsigned long seq;
unsigned long nsec, sec, lost;

do {
Seq = Read_seqbegin_irqsave (&xtime_lock, flags);
USEC = System_timer->offset ();

lost = Jiffies-wall_jiffies;
if (lost)
USEC + = lost * Usecs_per_jiffy;

SEC = xtime.tv_sec;
Nsec + = xtime.tv_nsec;
} while (Read_seqretry_irqrestore (&xtime_lock, SEQ, flags));

/* USEC May has gone up a lot:be safe */
while (nsec >= 1000000000) {
Nsec-= 1000000000;
sec++;
}

Tv->tv_sec = sec;
Tv->tv_usec = USEC;
}


FromCan be found, we can only modify the code a little bit to achieve the accuracy of nanosecond

void Do_gettimeofday_nsec (struct timespec *tv)
{
unsigned long flags;
unsigned long seq;
unsigned long usec, sec, lost;

do {
Seq = Read_seqbegin_irqsave (&xtime_lock, flags);
USEC = System_timer->offset ();

lost = Jiffies-wall_jiffies;
if (lost)
USEC + = lost * Usecs_per_jiffy;

SEC = xtime.tv_sec;
USEC + = xtime.tv_nsec/1000;
} while (Read_seqretry_irqrestore (&xtime_lock, SEQ, flags));

/* USEC May has gone up a lot:be safe */
while (usec >= 1000000) {
USEC-= 1000000;
sec++;
}

Tv->tv_sec = sec;
Tv->tv_usec = USEC;
}

Note that different time structures are used here.

Get a higher time value "Go" in the Linux kernel than jiffies precision

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.