1/* $OpenBSD: ktime.h,v 1.8 2024/03/28 02:36:38 jsg Exp $ */ 2/* 3 * Copyright (c) 2013, 2014, 2015 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18#ifndef _LINUX_KTIME_H 19#define _LINUX_KTIME_H 20 21#include <sys/time.h> 22#include <linux/time.h> 23#include <linux/jiffies.h> 24 25typedef int64_t ktime_t; 26#define KTIME_MAX INT64_MAX 27 28static inline ktime_t 29ktime_get(void) 30{ 31 struct timespec ts; 32 nanouptime(&ts); 33 return TIMESPEC_TO_NSEC(&ts); 34} 35 36static inline ktime_t 37ktime_get_raw(void) 38{ 39 struct timespec ts; 40 nanouptime(&ts); 41 return TIMESPEC_TO_NSEC(&ts); 42} 43 44static inline int64_t 45ktime_to_ms(ktime_t k) 46{ 47 return k / NSEC_PER_MSEC; 48} 49 50static inline int64_t 51ktime_to_us(ktime_t k) 52{ 53 return k / NSEC_PER_USEC; 54} 55 56static inline int64_t 57ktime_to_ns(ktime_t k) 58{ 59 return k; 60} 61 62static inline int64_t 63ktime_get_raw_ns(void) 64{ 65 return ktime_to_ns(ktime_get_raw()); 66} 67 68static inline int64_t 69ktime_get_raw_fast_ns(void) 70{ 71 return ktime_to_ns(ktime_get_raw()); 72} 73 74static inline struct timespec64 75ktime_to_timespec64(ktime_t k) 76{ 77 struct timespec64 ts; 78 ts.tv_sec = k / NSEC_PER_SEC; 79 ts.tv_nsec = k % NSEC_PER_SEC; 80 if (ts.tv_nsec < 0) { 81 ts.tv_sec--; 82 ts.tv_nsec += NSEC_PER_SEC; 83 } 84 return ts; 85} 86 87static inline ktime_t 88ktime_sub(ktime_t a, ktime_t b) 89{ 90 return a - b; 91} 92 93static inline ktime_t 94ktime_add(ktime_t a, ktime_t b) 95{ 96 return a + b; 97} 98 99static inline ktime_t 100ktime_add_us(ktime_t k, uint64_t us) 101{ 102 return k + (us * NSEC_PER_USEC); 103} 104 105static inline ktime_t 106ktime_add_ms(ktime_t k, uint64_t ms) 107{ 108 return k + (ms * NSEC_PER_MSEC); 109} 110 111static inline ktime_t 112ktime_add_ns(ktime_t k, int64_t ns) 113{ 114 return k + ns; 115} 116 117static inline ktime_t 118ktime_sub_ns(ktime_t k, int64_t ns) 119{ 120 return k - ns; 121} 122 123static inline int64_t 124ktime_us_delta(ktime_t a, ktime_t b) 125{ 126 return ktime_to_us(ktime_sub(a, b)); 127} 128 129static inline int64_t 130ktime_ms_delta(ktime_t a, ktime_t b) 131{ 132 return ktime_to_ms(ktime_sub(a, b)); 133} 134 135static inline bool 136ktime_before(ktime_t a, ktime_t b) 137{ 138 return a < b; 139} 140 141static inline bool 142ktime_after(ktime_t a, ktime_t b) 143{ 144 return a > b; 145} 146 147static inline ktime_t 148ns_to_ktime(uint64_t ns) 149{ 150 return ns; 151} 152 153static inline ktime_t 154ms_to_ktime(uint64_t ms) 155{ 156 return ms * NSEC_PER_MSEC; 157} 158 159static inline int64_t 160ktime_divns(ktime_t a, int64_t ns) 161{ 162 return a / ns; 163} 164 165static inline ktime_t 166ktime_set(time_t s, long ns) 167{ 168 struct timespec ts; 169 ts.tv_sec = s; 170 ts.tv_nsec = ns; 171 return TIMESPEC_TO_NSEC(&ts); 172} 173 174#include <linux/timekeeping.h> 175 176#endif 177