linux get the current time

Kernel code can always be captured represent a current time. Often, this value represents the time since the last time from the start by looking at the value of jifies, the fact that the drive is irrelevant because its life is limited to the system Uptime., the driving current value of jiffies may be used if they visit interval calculated time between events (e.g., the input drive from the click or double-click distinguishing calculated timeout). Briefly, see jiffies is almost always sufficient when you need to measure time intervals. If you need to measure short-term loss for a very precise, processor-specific registers come to the rescue (although they bring in serious portability problems).

 

It is very unlikely that a driver will need to know the wall clock time, in months, days, hours and expression; this information is often only require the user program, such as cron and syslogd time to deal with the real world is often best left. user space, where the C library offers better support; in addition, this code is often too policy related to belong kernel has a kernel function transform a wall clock time to a jiffies value, however:

 

#include <linux/time.h>

unsigned long mktime (unsigned int year, unsigned int mon, unsigned int day, unsigned int hour,

unsigned int min, unsigned int sec);

 

Repeat: Processing wall clock time directly in the drive signal is often achieved in a policy, and should therefore be questioned.

 

While you will not necessarily deal with human-readable representation of the time, sometimes you even need to deal with the absolute time in kernel space. To this end, <linux / time.h> do_gettimeofday output function. When called, it fills a struct timeval pointer - and used in the same system call gettimeofday - using similar seconds and milliseconds do_gettimeofday prototype is:

 

#include <linux/time.h>

void do_gettimeofday(struct timeval *tv);

 

This source code declares do_gettimeofday has "near microsecond precision," because it asks the timing hardware current jiffy what proportion has been lost. The accuracy of each system is different, however, because it depends on the actual hardware mechanisms in use. For example, some m68knommu processor, Sun3 systems, and other systems can not provide m68k accuracy than jiffy. the Pentium system, on the other hand, provides very fast and accurate measurement is less than tick timestamp counter through described earlier in this reading.

 

The current time may also be (although jiffy granularity) from xtime variable struct timespec a value directly discouraged this variable, because it is difficult to access these two fields atomically Thus, the kernel function provides a practical current_kernel_time..:

 

#include <linux/time.h>

struct timespec current_kernel_time(void);

 

The code used to get the current time in various ways, from jit source files on the FTP site provided by O 'Reilly ( "just in time") module to obtain. Jit creates a file called / proc / currentime, when read, it returns the entry to the following ASCII codes:

 

  • The current jiffies and jiffies_64 value, in the form of hexadecimal numbers.
    • Do_gettimeofday same time as the current return.
    • Returned by current_kernel_time of timespec.

 

We chose to use a dynamic / proc file to keep the boilerplate code to a minimum - it's not worth creating a whole device just to return a little textual information.

 

This file returns text lines continuously as long as the module is loaded; each read system call to collect and return a set of data, in order to better read and organized in two lines whenever you read in less than one clock tick more data. set, you will see the difference between do_gettimeofday, it queries the hardware, and other values ​​are updated only when the clock tick.

 

phon% head -8 /proc/currentime

0x00bdbc1f 0x0000000100bdbc1f 1062370899.630126

1062370899.629161488

0x00bdbc1f 0x0000000100bdbc1f 1062370899.630150

1062370899.629161488

0x00bdbc20 0x0000000100bdbc20 1062370899.630208

1062370899.630161336

0x00bdbc20 0x0000000100bdbc20 1062370899.630233

1062370899.630161336

 

In the screenshot above, the two interesting things to note first of all that this current_kernel_time value, though expressed in nanoseconds, only the clock tick granularity;. Do_gettimeofday continued to report a later time, but no later than the next clock tick. second, the 64-bit counter has the least significant bit jiffies high 32-bit word set. this is due to the default value of INITIAL_JIFFIES, to initialize the counter at the starting time, within a few minutes after the start time impose a low word overflow detection and this is just to help overflow-related issues. this initial bias in the counter has no effect, because jiffies nothing to do with the wall clock time. in the / proc / uptime, the kernel extracts the uptime here from the counter, initial bias is removed before the conversion.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11141990.html