Home | History | Annotate | Line # | Download | only in linux
ktime.h revision 1.9
      1 /*	$NetBSD: ktime.h,v 1.9 2020/02/14 14:34:59 maya 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 union ktime {
     44 	int64_t kt_nsec;
     45 };
     46 
     47 typedef union ktime ktime_t;
     48 
     49 static inline int64_t
     50 ktime_to_ns(ktime_t kt)
     51 {
     52 	return kt.kt_nsec;
     53 }
     54 
     55 static inline int64_t
     56 ktime_to_us(ktime_t kt)
     57 {
     58 	return ktime_to_ns(kt)/1000;
     59 }
     60 
     61 static inline ktime_t
     62 ns_to_ktime(int64_t nsec)
     63 {
     64 	return (ktime_t) { .kt_nsec = nsec };
     65 }
     66 
     67 static inline ktime_t
     68 ktime_add_ns(ktime_t kt, int64_t nsec)
     69 {
     70 	return ns_to_ktime(ktime_to_ns(kt) + nsec);
     71 }
     72 
     73 static inline ktime_t
     74 ktime_sub(ktime_t a, ktime_t b)
     75 {
     76 	return ns_to_ktime(ktime_to_ns(a) - ktime_to_ns(b));
     77 }
     78 
     79 static inline ktime_t
     80 ktime_sub_ns(ktime_t a, int64_t nsec)
     81 {
     82 	return ns_to_ktime(ktime_to_ns(a) - nsec);
     83 }
     84 
     85 static inline struct timespec
     86 ktime_to_timespec(ktime_t kt)
     87 {
     88 	return ns_to_timespec(ktime_to_ns(kt));
     89 }
     90 
     91 static inline struct timeval
     92 ktime_to_timeval(ktime_t kt)
     93 {
     94 	return ns_to_timeval(ktime_to_ns(kt));
     95 }
     96 
     97 static inline ktime_t
     98 timespec_to_ktime(struct timespec ts)
     99 {
    100 	/* XXX Silently truncate?  */
    101 	return ns_to_ktime(1000000000*(int64_t)ts.tv_sec + ts.tv_nsec);
    102 }
    103 
    104 static inline ktime_t
    105 ktime_get(void)
    106 {
    107 	struct timespec ts;
    108 
    109 	nanouptime(&ts);
    110 
    111 	return timespec_to_ktime(ts);
    112 }
    113 
    114 static inline ktime_t
    115 ktime_get_real(void)
    116 {
    117 	struct timespec ts;
    118 
    119 	nanotime(&ts);
    120 
    121 	return timespec_to_ktime(ts);
    122 }
    123 
    124 static inline uint64_t
    125 ktime_get_raw_ns(void)
    126 {
    127 
    128 	/* XXX */
    129 	return ktime_to_ns(ktime_get());
    130 }
    131 
    132 static inline ktime_t
    133 ktime_get_monotonic_offset(void)
    134 {
    135 	struct timespec ts;
    136 
    137 	getnanoboottime(&ts);
    138 
    139 	return timespec_to_ktime(ts);
    140 }
    141 
    142 static inline ktime_t
    143 ktime_mono_to_real(ktime_t kt)
    144 {
    145 	struct timespec ts = ktime_to_timespec(kt);
    146 	struct timespec bts;
    147 
    148 	getnanoboottime(&bts);
    149 	timespecadd(&ts, &bts, &ts);
    150 
    151 	return timespec_to_ktime(ts);
    152 }
    153 
    154 static inline int64_t
    155 ktime_us_delta(ktime_t a, ktime_t b)
    156 {
    157 	return ktime_to_us(ktime_sub(a, b));
    158 }
    159 
    160 static inline bool
    161 time_in_range(unsigned long x, unsigned long a, unsigned long b)
    162 {
    163 	return ((a <= x) && (x <= b));
    164 }
    165 
    166 #endif  /* _LINUX_KTIME_H_ */
    167