ktime.h revision 335429
1270710Shselasky/*- 2329977Shselasky * Copyright (c) 2018 Limelight Networks, Inc. 3329977Shselasky * Copyright (c) 2014-2018 Mellanox Technologies, Ltd. 4289572Shselasky * Copyright (c) 2015 Fran��ois Tigeot 5270710Shselasky * All rights reserved. 6270710Shselasky * 7270710Shselasky * Redistribution and use in source and binary forms, with or without 8270710Shselasky * modification, are permitted provided that the following conditions 9270710Shselasky * are met: 10270710Shselasky * 1. Redistributions of source code must retain the above copyright 11270710Shselasky * notice unmodified, this list of conditions, and the following 12270710Shselasky * disclaimer. 13270710Shselasky * 2. Redistributions in binary form must reproduce the above copyright 14270710Shselasky * notice, this list of conditions and the following disclaimer in the 15270710Shselasky * documentation and/or other materials provided with the distribution. 16270710Shselasky * 17270710Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18270710Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19270710Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20270710Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21270710Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22270710Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23270710Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24270710Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25270710Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26270710Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27289644Shselasky * 28289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/ktime.h 335429 2018-06-20 06:52:32Z hselasky $ 29270710Shselasky */ 30328653Shselasky 31270710Shselasky#ifndef _LINUX_KTIME_H 32328653Shselasky#define _LINUX_KTIME_H 33270710Shselasky 34270710Shselasky#include <linux/types.h> 35289572Shselasky#include <linux/time.h> 36270710Shselasky#include <linux/jiffies.h> 37270710Shselasky 38329977Shselasky#define ktime_get_ts(x) getnanouptime(x) 39270710Shselasky 40289572Shselasky/* time values in nanoseconds */ 41329977Shselaskytypedef s64 ktime_t; 42270710Shselasky 43331756Semaste#define KTIME_MAX ((s64)~((u64)1 << 63)) 44331756Semaste#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 45270710Shselasky 46289572Shselaskystatic inline int64_t 47289572Shselaskyktime_to_ns(ktime_t kt) 48270710Shselasky{ 49329977Shselasky return (kt); 50270710Shselasky} 51270710Shselasky 52328653Shselaskystatic inline ktime_t 53328653Shselaskyns_to_ktime(uint64_t nsec) 54328653Shselasky{ 55329977Shselasky return (nsec); 56328653Shselasky} 57328653Shselasky 58300498Shselaskystatic inline int64_t 59300498Shselaskyktime_divns(const ktime_t kt, int64_t div) 60300498Shselasky{ 61329977Shselasky return (kt / div); 62300498Shselasky} 63300498Shselasky 64300498Shselaskystatic inline int64_t 65300498Shselaskyktime_to_us(ktime_t kt) 66300498Shselasky{ 67329977Shselasky return (ktime_divns(kt, NSEC_PER_USEC)); 68300498Shselasky} 69300498Shselasky 70300498Shselaskystatic inline int64_t 71300498Shselaskyktime_to_ms(ktime_t kt) 72300498Shselasky{ 73329977Shselasky return (ktime_divns(kt, NSEC_PER_MSEC)); 74300498Shselasky} 75300498Shselasky 76289572Shselaskystatic inline struct timeval 77289572Shselaskyktime_to_timeval(ktime_t kt) 78270710Shselasky{ 79329977Shselasky return (ns_to_timeval(kt)); 80270710Shselasky} 81270710Shselasky 82289572Shselaskystatic inline ktime_t 83289572Shselaskyktime_add_ns(ktime_t kt, int64_t ns) 84270710Shselasky{ 85329977Shselasky return (kt + ns); 86270710Shselasky} 87270710Shselasky 88289572Shselaskystatic inline ktime_t 89289572Shselaskyktime_sub_ns(ktime_t kt, int64_t ns) 90270710Shselasky{ 91329977Shselasky return (kt - ns); 92270710Shselasky} 93270710Shselasky 94289572Shselaskystatic inline ktime_t 95289572Shselaskyktime_set(const long secs, const unsigned long nsecs) 96270710Shselasky{ 97329977Shselasky ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs}; 98329977Shselasky 99289572Shselasky return (retval); 100270710Shselasky} 101270710Shselasky 102289572Shselaskystatic inline ktime_t 103289572Shselaskyktime_sub(ktime_t lhs, ktime_t rhs) 104270710Shselasky{ 105329977Shselasky return (lhs - rhs); 106270710Shselasky} 107270710Shselasky 108300498Shselaskystatic inline int64_t 109300498Shselaskyktime_us_delta(ktime_t later, ktime_t earlier) 110300498Shselasky{ 111329977Shselasky ktime_t diff = ktime_sub(later, earlier); 112329977Shselasky 113329977Shselasky return (ktime_to_us(diff)); 114300498Shselasky} 115300498Shselasky 116300498Shselaskystatic inline int64_t 117300498Shselaskyktime_ms_delta(ktime_t later, ktime_t earlier) 118300498Shselasky{ 119329977Shselasky ktime_t diff = ktime_sub(later, earlier); 120329977Shselasky 121329977Shselasky return (ktime_to_ms(diff)); 122300498Shselasky} 123300498Shselasky 124289572Shselaskystatic inline ktime_t 125289572Shselaskyktime_add(ktime_t lhs, ktime_t rhs) 126270710Shselasky{ 127329977Shselasky return (lhs + rhs); 128270710Shselasky} 129270710Shselasky 130335421Shselaskystatic inline int 131335421Shselaskyktime_compare(const ktime_t cmp1, const ktime_t cmp2) 132335421Shselasky{ 133335421Shselasky 134335421Shselasky if (cmp1 > cmp2) 135335421Shselasky return (1); 136335421Shselasky else if (cmp1 < cmp2) 137335421Shselasky return (-1); 138335421Shselasky else 139335421Shselasky return (0); 140335421Shselasky} 141335421Shselasky 142335421Shselaskystatic inline bool 143335421Shselaskyktime_after(const ktime_t cmp1, const ktime_t cmp2) 144335421Shselasky{ 145335421Shselasky 146335421Shselasky return (ktime_compare(cmp1, cmp2) > 0); 147335421Shselasky} 148335421Shselasky 149289572Shselaskystatic inline ktime_t 150289572Shselaskytimespec_to_ktime(struct timespec ts) 151270710Shselasky{ 152289572Shselasky return (ktime_set(ts.tv_sec, ts.tv_nsec)); 153270710Shselasky} 154270710Shselasky 155289572Shselaskystatic inline ktime_t 156289572Shselaskytimeval_to_ktime(struct timeval tv) 157270710Shselasky{ 158289572Shselasky return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC)); 159270710Shselasky} 160270710Shselasky 161329977Shselasky#define ktime_to_timespec(kt) ns_to_timespec(kt) 162335429Shselasky#define ktime_to_timespec64(kt) ns_to_timespec(kt) 163329977Shselasky#define ktime_to_timeval(kt) ns_to_timeval(kt) 164329977Shselasky#define ktime_to_ns(kt) (kt) 165270710Shselasky 166328653Shselaskystatic inline int64_t 167289572Shselaskyktime_get_ns(void) 168277396Shselasky{ 169277396Shselasky struct timespec ts; 170289572Shselasky 171277396Shselasky ktime_get_ts(&ts); 172329977Shselasky 173329977Shselasky return (ktime_to_ns(timespec_to_ktime(ts))); 174277396Shselasky} 175277396Shselasky 176289572Shselaskystatic inline ktime_t 177289572Shselaskyktime_get(void) 178289572Shselasky{ 179289572Shselasky struct timespec ts; 180289572Shselasky 181289572Shselasky ktime_get_ts(&ts); 182289572Shselasky return (timespec_to_ktime(ts)); 183289572Shselasky} 184289572Shselasky 185328653Shselaskystatic inline ktime_t 186328653Shselaskyktime_get_boottime(void) 187328653Shselasky{ 188328653Shselasky struct timespec ts; 189328653Shselasky 190328653Shselasky nanouptime(&ts); 191328653Shselasky return (timespec_to_ktime(ts)); 192328653Shselasky} 193328653Shselasky 194328653Shselaskystatic inline ktime_t 195328653Shselaskyktime_get_real(void) 196328653Shselasky{ 197328653Shselasky struct timespec ts; 198328653Shselasky 199328653Shselasky nanotime(&ts); 200328653Shselasky return (timespec_to_ktime(ts)); 201328653Shselasky} 202328653Shselasky 203329977Shselaskystatic inline ktime_t 204329977Shselaskyktime_get_real_seconds(void) 205329977Shselasky{ 206329977Shselasky struct timespec ts; 207329977Shselasky 208329977Shselasky nanotime(&ts); 209329977Shselasky return (ts.tv_sec); 210329977Shselasky} 211329977Shselasky 212330848Shselaskystatic inline ktime_t 213330848Shselaskyktime_get_raw(void) 214330848Shselasky{ 215330848Shselasky struct timespec ts; 216330848Shselasky 217330848Shselasky nanotime(&ts); 218330848Shselasky return (timespec_to_ktime(ts)); 219330848Shselasky} 220330848Shselasky 221329977Shselaskystatic inline u64 222329977Shselaskyktime_get_raw_ns(void) 223329977Shselasky{ 224329977Shselasky struct timespec ts; 225329977Shselasky 226329977Shselasky nanouptime(&ts); 227329977Shselasky return (ktime_to_ns(timespec_to_ktime(ts))); 228329977Shselasky} 229329977Shselasky 230328653Shselasky#endif /* _LINUX_KTIME_H */ 231