xref: /OK3568_Linux_fs/kernel/Documentation/core-api/timekeeping.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyunktime accessors
2*4882a593Smuzhiyun===============
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunDevice drivers can read the current time using ktime_get() and the many
5*4882a593Smuzhiyunrelated functions declared in linux/timekeeping.h. As a rule of thumb,
6*4882a593Smuzhiyunusing an accessor with a shorter name is preferred over one with a longer
7*4882a593Smuzhiyunname if both are equally fit for a particular use case.
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunBasic ktime_t based interfaces
10*4882a593Smuzhiyun------------------------------
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunThe recommended simplest form returns an opaque ktime_t, with variants
13*4882a593Smuzhiyunthat return time for different clock references:
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun.. c:function:: ktime_t ktime_get( void )
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun	CLOCK_MONOTONIC
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun	Useful for reliable timestamps and measuring short time intervals
21*4882a593Smuzhiyun	accurately. Starts at system boot time but stops during suspend.
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun.. c:function:: ktime_t ktime_get_boottime( void )
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun	CLOCK_BOOTTIME
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun	Like ktime_get(), but does not stop when suspended. This can be
28*4882a593Smuzhiyun	used e.g. for key expiration times that need to be synchronized
29*4882a593Smuzhiyun	with other machines across a suspend operation.
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun.. c:function:: ktime_t ktime_get_real( void )
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun	CLOCK_REALTIME
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun	Returns the time in relative to the UNIX epoch starting in 1970
36*4882a593Smuzhiyun	using the Coordinated Universal Time (UTC), same as gettimeofday()
37*4882a593Smuzhiyun	user space. This is used for all timestamps that need to
38*4882a593Smuzhiyun	persist across a reboot, like inode times, but should be avoided
39*4882a593Smuzhiyun	for internal uses, since it can jump backwards due to a leap
40*4882a593Smuzhiyun	second update, NTP adjustment settimeofday() operation from user
41*4882a593Smuzhiyun	space.
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun.. c:function:: ktime_t ktime_get_clocktai( void )
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun	 CLOCK_TAI
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun	Like ktime_get_real(), but uses the International Atomic Time (TAI)
48*4882a593Smuzhiyun	reference instead of UTC to avoid jumping on leap second updates.
49*4882a593Smuzhiyun	This is rarely useful in the kernel.
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun.. c:function:: ktime_t ktime_get_raw( void )
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun	CLOCK_MONOTONIC_RAW
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun	Like ktime_get(), but runs at the same rate as the hardware
56*4882a593Smuzhiyun	clocksource without (NTP) adjustments for clock drift. This is
57*4882a593Smuzhiyun	also rarely needed in the kernel.
58*4882a593Smuzhiyun
59*4882a593Smuzhiyunnanosecond, timespec64, and second output
60*4882a593Smuzhiyun-----------------------------------------
61*4882a593Smuzhiyun
62*4882a593SmuzhiyunFor all of the above, there are variants that return the time in a
63*4882a593Smuzhiyundifferent format depending on what is required by the user:
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun.. c:function:: u64 ktime_get_ns( void )
66*4882a593Smuzhiyun		u64 ktime_get_boottime_ns( void )
67*4882a593Smuzhiyun		u64 ktime_get_real_ns( void )
68*4882a593Smuzhiyun		u64 ktime_get_clocktai_ns( void )
69*4882a593Smuzhiyun		u64 ktime_get_raw_ns( void )
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun	Same as the plain ktime_get functions, but returning a u64 number
72*4882a593Smuzhiyun	of nanoseconds in the respective time reference, which may be
73*4882a593Smuzhiyun	more convenient for some callers.
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun.. c:function:: void ktime_get_ts64( struct timespec64 * )
76*4882a593Smuzhiyun		void ktime_get_boottime_ts64( struct timespec64 * )
77*4882a593Smuzhiyun		void ktime_get_real_ts64( struct timespec64 * )
78*4882a593Smuzhiyun		void ktime_get_clocktai_ts64( struct timespec64 * )
79*4882a593Smuzhiyun		void ktime_get_raw_ts64( struct timespec64 * )
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun	Same above, but returns the time in a 'struct timespec64', split
82*4882a593Smuzhiyun	into seconds and nanoseconds. This can avoid an extra division
83*4882a593Smuzhiyun	when printing the time, or when passing it into an external
84*4882a593Smuzhiyun	interface that expects a 'timespec' or 'timeval' structure.
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun.. c:function:: time64_t ktime_get_seconds( void )
87*4882a593Smuzhiyun		time64_t ktime_get_boottime_seconds( void )
88*4882a593Smuzhiyun		time64_t ktime_get_real_seconds( void )
89*4882a593Smuzhiyun		time64_t ktime_get_clocktai_seconds( void )
90*4882a593Smuzhiyun		time64_t ktime_get_raw_seconds( void )
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun	Return a coarse-grained version of the time as a scalar
93*4882a593Smuzhiyun	time64_t. This avoids accessing the clock hardware and rounds
94*4882a593Smuzhiyun	down the seconds to the full seconds of the last timer tick
95*4882a593Smuzhiyun	using the respective reference.
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunCoarse and fast_ns access
98*4882a593Smuzhiyun-------------------------
99*4882a593Smuzhiyun
100*4882a593SmuzhiyunSome additional variants exist for more specialized cases:
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun.. c:function:: ktime_t ktime_get_coarse( void )
103*4882a593Smuzhiyun		ktime_t ktime_get_coarse_boottime( void )
104*4882a593Smuzhiyun		ktime_t ktime_get_coarse_real( void )
105*4882a593Smuzhiyun		ktime_t ktime_get_coarse_clocktai( void )
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun.. c:function:: u64 ktime_get_coarse_ns( void )
108*4882a593Smuzhiyun		u64 ktime_get_coarse_boottime_ns( void )
109*4882a593Smuzhiyun		u64 ktime_get_coarse_real_ns( void )
110*4882a593Smuzhiyun		u64 ktime_get_coarse_clocktai_ns( void )
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun.. c:function:: void ktime_get_coarse_ts64( struct timespec64 * )
113*4882a593Smuzhiyun		void ktime_get_coarse_boottime_ts64( struct timespec64 * )
114*4882a593Smuzhiyun		void ktime_get_coarse_real_ts64( struct timespec64 * )
115*4882a593Smuzhiyun		void ktime_get_coarse_clocktai_ts64( struct timespec64 * )
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun	These are quicker than the non-coarse versions, but less accurate,
118*4882a593Smuzhiyun	corresponding to CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE
119*4882a593Smuzhiyun	in user space, along with the equivalent boottime/tai/raw
120*4882a593Smuzhiyun	timebase not available in user space.
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun	The time returned here corresponds to the last timer tick, which
123*4882a593Smuzhiyun	may be as much as 10ms in the past (for CONFIG_HZ=100), same as
124*4882a593Smuzhiyun	reading the 'jiffies' variable.  These are only useful when called
125*4882a593Smuzhiyun	in a fast path and one still expects better than second accuracy,
126*4882a593Smuzhiyun	but can't easily use 'jiffies', e.g. for inode timestamps.
127*4882a593Smuzhiyun	Skipping the hardware clock access saves around 100 CPU cycles
128*4882a593Smuzhiyun	on most modern machines with a reliable cycle counter, but
129*4882a593Smuzhiyun	up to several microseconds on older hardware with an external
130*4882a593Smuzhiyun	clocksource.
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun.. c:function:: u64 ktime_get_mono_fast_ns( void )
133*4882a593Smuzhiyun		u64 ktime_get_raw_fast_ns( void )
134*4882a593Smuzhiyun		u64 ktime_get_boot_fast_ns( void )
135*4882a593Smuzhiyun		u64 ktime_get_real_fast_ns( void )
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun	These variants are safe to call from any context, including from
138*4882a593Smuzhiyun	a non-maskable interrupt (NMI) during a timekeeper update, and
139*4882a593Smuzhiyun	while we are entering suspend with the clocksource powered down.
140*4882a593Smuzhiyun	This is useful in some tracing or debugging code as well as
141*4882a593Smuzhiyun	machine check reporting, but most drivers should never call them,
142*4882a593Smuzhiyun	since the time is allowed to jump under certain conditions.
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunDeprecated time interfaces
145*4882a593Smuzhiyun--------------------------
146*4882a593Smuzhiyun
147*4882a593SmuzhiyunOlder kernels used some other interfaces that are now being phased out
148*4882a593Smuzhiyunbut may appear in third-party drivers being ported here. In particular,
149*4882a593Smuzhiyunall interfaces returning a 'struct timeval' or 'struct timespec' have
150*4882a593Smuzhiyunbeen replaced because the tv_sec member overflows in year 2038 on 32-bit
151*4882a593Smuzhiyunarchitectures. These are the recommended replacements:
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun.. c:function:: void ktime_get_ts( struct timespec * )
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun	Use ktime_get() or ktime_get_ts64() instead.
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun.. c:function:: void do_gettimeofday( struct timeval * )
158*4882a593Smuzhiyun		void getnstimeofday( struct timespec * )
159*4882a593Smuzhiyun		void getnstimeofday64( struct timespec64 * )
160*4882a593Smuzhiyun		void ktime_get_real_ts( struct timespec * )
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun	ktime_get_real_ts64() is a direct replacement, but consider using
163*4882a593Smuzhiyun	monotonic time (ktime_get_ts64()) and/or a ktime_t based interface
164*4882a593Smuzhiyun	(ktime_get()/ktime_get_real()).
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun.. c:function:: struct timespec current_kernel_time( void )
167*4882a593Smuzhiyun		struct timespec64 current_kernel_time64( void )
168*4882a593Smuzhiyun		struct timespec get_monotonic_coarse( void )
169*4882a593Smuzhiyun		struct timespec64 get_monotonic_coarse64( void )
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun	These are replaced by ktime_get_coarse_real_ts64() and
172*4882a593Smuzhiyun	ktime_get_coarse_ts64(). However, A lot of code that wants
173*4882a593Smuzhiyun	coarse-grained times can use the simple 'jiffies' instead, while
174*4882a593Smuzhiyun	some drivers may actually want the higher resolution accessors
175*4882a593Smuzhiyun	these days.
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun.. c:function:: struct timespec getrawmonotonic( void )
178*4882a593Smuzhiyun		struct timespec64 getrawmonotonic64( void )
179*4882a593Smuzhiyun		struct timespec timekeeping_clocktai( void )
180*4882a593Smuzhiyun		struct timespec64 timekeeping_clocktai64( void )
181*4882a593Smuzhiyun		struct timespec get_monotonic_boottime( void )
182*4882a593Smuzhiyun		struct timespec64 get_monotonic_boottime64( void )
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun	These are replaced by ktime_get_raw()/ktime_get_raw_ts64(),
185*4882a593Smuzhiyun	ktime_get_clocktai()/ktime_get_clocktai_ts64() as well
186*4882a593Smuzhiyun	as ktime_get_boottime()/ktime_get_boottime_ts64().
187*4882a593Smuzhiyun	However, if the particular choice of clock source is not
188*4882a593Smuzhiyun	important for the user, consider converting to
189*4882a593Smuzhiyun	ktime_get()/ktime_get_ts64() instead for consistency.
190