ktime.h revision 300498
1/*- 2 * Copyright (c) 2014-2015 Mellanox Technologies, Ltd. 3 * Copyright (c) 2015 Fran��ois Tigeot 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/compat/linuxkpi/common/include/linux/ktime.h 300498 2016-05-23 12:10:28Z hselasky $ 28 */ 29#ifndef _LINUX_KTIME_H 30#define _LINUX_KTIME_H 31 32#include <linux/types.h> 33#include <linux/time.h> 34#include <linux/jiffies.h> 35 36#define ktime_get_ts(x) getnanouptime(x) 37 38/* time values in nanoseconds */ 39union ktime { 40 int64_t tv64; 41}; 42 43typedef union ktime ktime_t; 44 45#define KTIME_MAX ((s64)~((u64)1 << 63)) 46#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 47 48static inline int64_t 49ktime_to_ns(ktime_t kt) 50{ 51 return kt.tv64; 52} 53 54static inline int64_t 55ktime_divns(const ktime_t kt, int64_t div) 56{ 57 return kt.tv64 / div; 58} 59 60static inline int64_t 61ktime_to_us(ktime_t kt) 62{ 63 return ktime_divns(kt, NSEC_PER_USEC); 64} 65 66static inline int64_t 67ktime_to_ms(ktime_t kt) 68{ 69 return ktime_divns(kt, NSEC_PER_MSEC); 70} 71 72static inline struct timeval 73ktime_to_timeval(ktime_t kt) 74{ 75 return ns_to_timeval(kt.tv64); 76} 77 78static inline ktime_t 79ktime_add_ns(ktime_t kt, int64_t ns) 80{ 81 ktime_t res; 82 83 res.tv64 = kt.tv64 + ns; 84 return kt; 85} 86 87static inline ktime_t 88ktime_sub_ns(ktime_t kt, int64_t ns) 89{ 90 ktime_t res; 91 92 res.tv64 = kt.tv64 - ns; 93 return kt; 94} 95 96static inline ktime_t 97ktime_set(const long secs, const unsigned long nsecs) 98{ 99 ktime_t retval = { (s64)secs * NSEC_PER_SEC + (s64)nsecs }; 100 return (retval); 101} 102 103static inline ktime_t 104ktime_sub(ktime_t lhs, ktime_t rhs) 105{ 106 lhs.tv64 -= rhs.tv64; 107 return (lhs); 108} 109 110static inline int64_t 111ktime_us_delta(ktime_t later, ktime_t earlier) 112{ 113 ktime_t diff = ktime_sub(later, earlier); 114 return ktime_to_us(diff); 115} 116 117static inline int64_t 118ktime_ms_delta(ktime_t later, ktime_t earlier) 119{ 120 ktime_t diff = ktime_sub(later, earlier); 121 return ktime_to_ms(diff); 122} 123 124static inline ktime_t 125ktime_add(ktime_t lhs, ktime_t rhs) 126{ 127 lhs.tv64 += rhs.tv64; 128 return (lhs); 129} 130 131static inline ktime_t 132timespec_to_ktime(struct timespec ts) 133{ 134 return (ktime_set(ts.tv_sec, ts.tv_nsec)); 135} 136 137static inline ktime_t 138timeval_to_ktime(struct timeval tv) 139{ 140 return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC)); 141} 142 143#define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) 144#define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) 145#define ktime_to_ns(kt) ((kt).tv64) 146 147static inline s64 148ktime_get_ns(void) 149{ 150 struct timespec ts; 151 ktime_t kt; 152 153 ktime_get_ts(&ts); 154 kt = timespec_to_ktime(ts); 155 return (ktime_to_ns(kt)); 156} 157 158static inline ktime_t 159ktime_get(void) 160{ 161 struct timespec ts; 162 163 ktime_get_ts(&ts); 164 return (timespec_to_ktime(ts)); 165} 166 167#endif /* _LINUX_KTIME_H */ 168