Home | History | Annotate | Line # | Download | only in linux
ktime.h revision 1.17
      1 /*	$NetBSD: ktime.h,v 1.17 2021/12/19 11:38:04 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 
     40 #include <linux/jiffies.h>
     41 #include <linux/time.h>
     42 
     43 typedef int64_t	ktime_t;
     44 
     45 static inline int64_t
     46 ktime_to_ns(ktime_t kt)
     47 {
     48 	return kt;
     49 }
     50 
     51 static inline int64_t
     52 ktime_to_us(ktime_t kt)
     53 {
     54 	return ktime_to_ns(kt)/1000;
     55 }
     56 
     57 static inline int64_t
     58 ktime_to_ms(ktime_t kt)
     59 {
     60 	return ktime_to_ns(kt)/1000000;
     61 }
     62 
     63 static inline ktime_t
     64 ns_to_ktime(int64_t nsec)
     65 {
     66 	return nsec;
     67 }
     68 
     69 static inline ktime_t
     70 ktime_add(ktime_t a, ktime_t b)
     71 {
     72 	return ns_to_ktime(ktime_to_ns(a) + ktime_to_ns(b));
     73 }
     74 
     75 static inline ktime_t
     76 ktime_add_ns(ktime_t kt, int64_t nsec)
     77 {
     78 	return ns_to_ktime(ktime_to_ns(kt) + nsec);
     79 }
     80 
     81 static inline ktime_t
     82 ktime_sub(ktime_t a, ktime_t b)
     83 {
     84 	return ns_to_ktime(ktime_to_ns(a) - ktime_to_ns(b));
     85 }
     86 
     87 static inline ktime_t
     88 ktime_sub_ns(ktime_t a, int64_t nsec)
     89 {
     90 	return ns_to_ktime(ktime_to_ns(a) - nsec);
     91 }
     92 
     93 static inline struct timespec
     94 ktime_to_timespec(ktime_t kt)
     95 {
     96 	return ns_to_timespec(ktime_to_ns(kt));
     97 }
     98 #define	ktime_to_timespec64	ktime_to_timespec
     99 
    100 static inline struct timeval
    101 ktime_to_timeval(ktime_t kt)
    102 {
    103 	return ns_to_timeval(ktime_to_ns(kt));
    104 }
    105 
    106 static inline ktime_t
    107 timespec_to_ktime(struct timespec ts)
    108 {
    109 	/* XXX Silently truncate?  */
    110 	return ns_to_ktime(1000000000*(int64_t)ts.tv_sec + ts.tv_nsec);
    111 }
    112 #define	timespec64_to_ktime	timespec_to_ktime
    113 
    114 static inline ktime_t
    115 ktime_get(void)
    116 {
    117 	struct timespec ts;
    118 
    119 	nanouptime(&ts);
    120 
    121 	return timespec_to_ktime(ts);
    122 }
    123 
    124 static inline ktime_t
    125 ktime_get_real(void)
    126 {
    127 	struct timespec ts;
    128 
    129 	nanotime(&ts);
    130 
    131 	return timespec_to_ktime(ts);
    132 }
    133 
    134 static inline ktime_t
    135 ktime_get_boottime(void)
    136 {
    137 	/* XXX include time spent in suspend */
    138 	return ktime_get();
    139 }
    140 
    141 static inline ktime_t
    142 ktime_get_raw(void)
    143 {
    144 	/* XXX */
    145 	return ktime_get();
    146 }
    147 
    148 static inline uint64_t
    149 ktime_get_raw_ns(void)
    150 {
    151 	return ktime_to_ns(ktime_get_raw());
    152 }
    153 
    154 static inline uint64_t
    155 ktime_get_mono_fast_ns(void)
    156 {
    157 	return ktime_get_raw_ns();
    158 }
    159 
    160 static inline ktime_t
    161 ktime_get_monotonic_offset(void)
    162 {
    163 	struct timespec ts;
    164 
    165 	getnanoboottime(&ts);
    166 
    167 	return timespec_to_ktime(ts);
    168 }
    169 
    170 static inline ktime_t
    171 ktime_mono_to_real(ktime_t kt)
    172 {
    173 	struct timespec ts = ktime_to_timespec(kt);
    174 	struct timespec bts;
    175 
    176 	getnanoboottime(&bts);
    177 	timespecadd(&ts, &bts, &ts);
    178 
    179 	return timespec_to_ktime(ts);
    180 }
    181 
    182 static inline int64_t
    183 ktime_us_delta(ktime_t a, ktime_t b)
    184 {
    185 	return ktime_to_us(ktime_sub(a, b));
    186 }
    187 
    188 static inline int64_t
    189 ktime_ms_delta(ktime_t a, ktime_t b)
    190 {
    191 	return ktime_to_ms(ktime_sub(a, b));
    192 }
    193 
    194 
    195 static inline bool
    196 time_in_range(unsigned long x, unsigned long a, unsigned long b)
    197 {
    198 	return ((a <= x) && (x <= b));
    199 }
    200 
    201 static inline bool
    202 ktime_after(ktime_t a, ktime_t b)
    203 {
    204 	return ktime_to_ns(a) > ktime_to_ns(b);
    205 }
    206 
    207 #endif  /* _LINUX_KTIME_H_ */
    208