Home | History | Annotate | Line # | Download | only in linux
      1 /*	$NetBSD: ktime.h,v 1.21 2021/12/19 12:30:23 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _LINUX_KTIME_H_
     33 #define _LINUX_KTIME_H_
     34 
     35 #include <sys/types.h>
     36 #include <sys/endian.h>
     37 #include <sys/kernel.h>
     38 #include <sys/time.h>
     39 #include <sys/timevar.h>
     40 
     41 #include <linux/jiffies.h>
     42 #include <linux/time.h>
     43 
     44 typedef int64_t	ktime_t;
     45 
     46 static inline int64_t
     47 ktime_to_ns(ktime_t kt)
     48 {
     49 	return kt;
     50 }
     51 
     52 static inline int64_t
     53 ktime_to_us(ktime_t kt)
     54 {
     55 	return ktime_to_ns(kt)/1000;
     56 }
     57 
     58 static inline int64_t
     59 ktime_to_ms(ktime_t kt)
     60 {
     61 	return ktime_to_ns(kt)/1000000;
     62 }
     63 
     64 static inline ktime_t
     65 ns_to_ktime(int64_t nsec)
     66 {
     67 	return nsec;
     68 }
     69 
     70 static inline ktime_t
     71 ktime_add(ktime_t a, ktime_t b)
     72 {
     73 	return ns_to_ktime(ktime_to_ns(a) + ktime_to_ns(b));
     74 }
     75 
     76 static inline ktime_t
     77 ktime_add_ns(ktime_t kt, int64_t nsec)
     78 {
     79 	return ns_to_ktime(ktime_to_ns(kt) + nsec);
     80 }
     81 
     82 static inline ktime_t
     83 ktime_add_us(ktime_t kt, int64_t usec)
     84 {
     85 	return ktime_add_ns(kt, 1000 * usec);
     86 }
     87 
     88 static inline ktime_t
     89 ktime_sub(ktime_t a, ktime_t b)
     90 {
     91 	return ns_to_ktime(ktime_to_ns(a) - ktime_to_ns(b));
     92 }
     93 
     94 static inline ktime_t
     95 ktime_sub_ns(ktime_t a, int64_t nsec)
     96 {
     97 	return ns_to_ktime(ktime_to_ns(a) - nsec);
     98 }
     99 
    100 static inline struct timespec
    101 ktime_to_timespec(ktime_t kt)
    102 {
    103 	return ns_to_timespec(ktime_to_ns(kt));
    104 }
    105 #define	ktime_to_timespec64	ktime_to_timespec
    106 
    107 static inline struct timeval
    108 ktime_to_timeval(ktime_t kt)
    109 {
    110 	return ns_to_timeval(ktime_to_ns(kt));
    111 }
    112 
    113 static inline ktime_t
    114 timespec_to_ktime(struct timespec ts)
    115 {
    116 	/* XXX Silently truncate?  */
    117 	return ns_to_ktime(1000000000*(int64_t)ts.tv_sec + ts.tv_nsec);
    118 }
    119 #define	timespec64_to_ktime	timespec_to_ktime
    120 
    121 static inline ktime_t
    122 ktime_get(void)
    123 {
    124 	struct timespec ts;
    125 
    126 	nanouptime(&ts);
    127 
    128 	return timespec_to_ktime(ts);
    129 }
    130 
    131 static inline ktime_t
    132 ktime_get_real(void)
    133 {
    134 	struct timespec ts;
    135 
    136 	nanotime(&ts);
    137 
    138 	return timespec_to_ktime(ts);
    139 }
    140 
    141 static inline ktime_t
    142 ktime_get_boottime(void)
    143 {
    144 	/* XXX include time spent in suspend */
    145 	return ktime_get();
    146 }
    147 
    148 static inline ktime_t
    149 ktime_get_raw(void)
    150 {
    151 	/* XXX */
    152 	return ktime_get();
    153 }
    154 
    155 static inline uint64_t
    156 ktime_get_ns(void)
    157 {
    158 	return ktime_to_ns(ktime_get());
    159 }
    160 
    161 static inline uint64_t
    162 ktime_get_raw_ns(void)
    163 {
    164 	return ktime_to_ns(ktime_get_raw());
    165 }
    166 
    167 static inline uint64_t
    168 ktime_get_mono_fast_ns(void)
    169 {
    170 	return ktime_get_raw_ns();
    171 }
    172 
    173 static inline ktime_t
    174 ktime_get_monotonic_offset(void)
    175 {
    176 	struct timespec ts;
    177 
    178 	getnanoboottime(&ts);
    179 
    180 	return timespec_to_ktime(ts);
    181 }
    182 
    183 static inline ktime_t
    184 ktime_mono_to_real(ktime_t kt)
    185 {
    186 	struct timespec ts = ktime_to_timespec(kt);
    187 	struct timespec bts;
    188 
    189 	getnanoboottime(&bts);
    190 	timespecadd(&ts, &bts, &ts);
    191 
    192 	return timespec_to_ktime(ts);
    193 }
    194 
    195 static inline int64_t
    196 ktime_us_delta(ktime_t a, ktime_t b)
    197 {
    198 	return ktime_to_us(ktime_sub(a, b));
    199 }
    200 
    201 static inline int64_t
    202 ktime_ms_delta(ktime_t a, ktime_t b)
    203 {
    204 	return ktime_to_ms(ktime_sub(a, b));
    205 }
    206 
    207 
    208 static inline bool
    209 time_in_range(unsigned long x, unsigned long a, unsigned long b)
    210 {
    211 	return ((a <= x) && (x <= b));
    212 }
    213 
    214 static inline bool
    215 ktime_after(ktime_t a, ktime_t b)
    216 {
    217 	return ktime_to_ns(a) > ktime_to_ns(b);
    218 }
    219 
    220 static inline time_t
    221 ktime_get_real_seconds(void)
    222 {
    223 	return time_second;
    224 }
    225 
    226 static inline void
    227 ktime_get_ts64(struct timespec64 *ts)
    228 {
    229 
    230 	nanotime(ts);
    231 }
    232 
    233 #endif  /* _LINUX_KTIME_H_ */
    234