ktime.h revision 329977
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 329977 2018-02-25 10:44:47Z 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 43329977Shselasky#define KTIME_MAX ((s64)~((u64)1 << 63)) 44329977Shselasky#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 130289572Shselaskystatic inline ktime_t 131289572Shselaskytimespec_to_ktime(struct timespec ts) 132270710Shselasky{ 133289572Shselasky return (ktime_set(ts.tv_sec, ts.tv_nsec)); 134270710Shselasky} 135270710Shselasky 136289572Shselaskystatic inline ktime_t 137289572Shselaskytimeval_to_ktime(struct timeval tv) 138270710Shselasky{ 139289572Shselasky return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC)); 140270710Shselasky} 141270710Shselasky 142329977Shselasky#define ktime_to_timespec(kt) ns_to_timespec(kt) 143329977Shselasky#define ktime_to_timeval(kt) ns_to_timeval(kt) 144329977Shselasky#define ktime_to_ns(kt) (kt) 145270710Shselasky 146328653Shselaskystatic inline int64_t 147289572Shselaskyktime_get_ns(void) 148277396Shselasky{ 149277396Shselasky struct timespec ts; 150289572Shselasky 151277396Shselasky ktime_get_ts(&ts); 152329977Shselasky 153329977Shselasky return (ktime_to_ns(timespec_to_ktime(ts))); 154277396Shselasky} 155277396Shselasky 156289572Shselaskystatic inline ktime_t 157289572Shselaskyktime_get(void) 158289572Shselasky{ 159289572Shselasky struct timespec ts; 160289572Shselasky 161289572Shselasky ktime_get_ts(&ts); 162289572Shselasky return (timespec_to_ktime(ts)); 163289572Shselasky} 164289572Shselasky 165328653Shselaskystatic inline ktime_t 166328653Shselaskyktime_get_boottime(void) 167328653Shselasky{ 168328653Shselasky struct timespec ts; 169328653Shselasky 170328653Shselasky nanouptime(&ts); 171328653Shselasky return (timespec_to_ktime(ts)); 172328653Shselasky} 173328653Shselasky 174328653Shselaskystatic inline ktime_t 175328653Shselaskyktime_get_real(void) 176328653Shselasky{ 177328653Shselasky struct timespec ts; 178328653Shselasky 179328653Shselasky nanotime(&ts); 180328653Shselasky return (timespec_to_ktime(ts)); 181328653Shselasky} 182328653Shselasky 183329977Shselaskystatic inline ktime_t 184329977Shselaskyktime_get_real_seconds(void) 185329977Shselasky{ 186329977Shselasky struct timespec ts; 187329977Shselasky 188329977Shselasky nanotime(&ts); 189329977Shselasky return (ts.tv_sec); 190329977Shselasky} 191329977Shselasky 192329977Shselaskystatic inline u64 193329977Shselaskyktime_get_raw_ns(void) 194329977Shselasky{ 195329977Shselasky struct timespec ts; 196329977Shselasky 197329977Shselasky nanouptime(&ts); 198329977Shselasky return (ktime_to_ns(timespec_to_ktime(ts))); 199329977Shselasky} 200329977Shselasky 201328653Shselasky#endif /* _LINUX_KTIME_H */ 202