ktime.h revision 289572
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 28#ifndef _LINUX_KTIME_H 29#define _LINUX_KTIME_H 30 31#include <linux/types.h> 32#include <linux/time.h> 33#include <linux/jiffies.h> 34 35#define ktime_get_ts(x) getnanouptime(x) 36 37/* time values in nanoseconds */ 38union ktime { 39 int64_t tv64; 40}; 41 42typedef union ktime ktime_t; 43 44#define KTIME_MAX ((s64)~((u64)1 << 63)) 45#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 46 47static inline int64_t 48ktime_to_ns(ktime_t kt) 49{ 50 return kt.tv64; 51} 52 53static inline struct timeval 54ktime_to_timeval(ktime_t kt) 55{ 56 return ns_to_timeval(kt.tv64); 57} 58 59static inline ktime_t 60ktime_add_ns(ktime_t kt, int64_t ns) 61{ 62 ktime_t res; 63 64 res.tv64 = kt.tv64 + ns; 65 return kt; 66} 67 68static inline ktime_t 69ktime_sub_ns(ktime_t kt, int64_t ns) 70{ 71 ktime_t res; 72 73 res.tv64 = kt.tv64 - ns; 74 return kt; 75} 76 77static inline ktime_t 78ktime_set(const long secs, const unsigned long nsecs) 79{ 80 ktime_t retval = { (s64)secs * NSEC_PER_SEC + (s64)nsecs }; 81 return (retval); 82} 83 84static inline ktime_t 85ktime_sub(ktime_t lhs, ktime_t rhs) 86{ 87 lhs.tv64 -= rhs.tv64; 88 return (lhs); 89} 90 91static inline ktime_t 92ktime_add(ktime_t lhs, ktime_t rhs) 93{ 94 lhs.tv64 += rhs.tv64; 95 return (lhs); 96} 97 98static inline ktime_t 99timespec_to_ktime(struct timespec ts) 100{ 101 return (ktime_set(ts.tv_sec, ts.tv_nsec)); 102} 103 104static inline ktime_t 105timeval_to_ktime(struct timeval tv) 106{ 107 return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC)); 108} 109 110#define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) 111#define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) 112#define ktime_to_ns(kt) ((kt).tv64) 113 114static inline s64 115ktime_get_ns(void) 116{ 117 struct timespec ts; 118 ktime_t kt; 119 120 ktime_get_ts(&ts); 121 kt = timespec_to_ktime(ts); 122 return (ktime_to_ns(kt)); 123} 124 125static inline ktime_t 126ktime_get(void) 127{ 128 struct timespec ts; 129 130 ktime_get_ts(&ts); 131 return (timespec_to_ktime(ts)); 132} 133 134#endif /* _LINUX_KTIME_H */ 135