1 /*- 2 * Copyright (c) 2014-2015 François Tigeot 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 #ifndef _LINUXKPI_LINUX_TIME_H_ 29 #define _LINUXKPI_LINUX_TIME_H_ 30 31 #define MSEC_PER_SEC 1000L 32 33 #define NSEC_PER_USEC 1000L 34 #define NSEC_PER_MSEC 1000000L 35 #define NSEC_PER_SEC 1000000000L 36 37 #define USEC_PER_MSEC 1000L 38 #define USEC_PER_SEC 1000000L 39 40 #define timespec64 timespec 41 42 #include <sys/time.h> 43 #include <sys/stdint.h> 44 45 #include <linux/math64.h> 46 47 static inline struct timeval 48 ns_to_timeval(const int64_t nsec) 49 { 50 struct timeval tv; 51 long rem; 52 53 if (nsec == 0) { 54 tv.tv_sec = 0; 55 tv.tv_usec = 0; 56 return (tv); 57 } 58 59 tv.tv_sec = nsec / NSEC_PER_SEC; 60 rem = nsec % NSEC_PER_SEC; 61 if (rem < 0) { 62 tv.tv_sec--; 63 rem += NSEC_PER_SEC; 64 } 65 tv.tv_usec = rem / 1000; 66 return (tv); 67 } 68 69 static inline int64_t 70 timeval_to_ns(const struct timeval *tv) 71 { 72 return ((int64_t)tv->tv_sec * NSEC_PER_SEC) + 73 tv->tv_usec * NSEC_PER_USEC; 74 } 75 76 #define getrawmonotonic(ts) nanouptime(ts) 77 78 static inline struct timespec 79 timespec_sub(struct timespec lhs, struct timespec rhs) 80 { 81 struct timespec ts; 82 83 timespecsub(&lhs, &rhs, &ts); 84 85 return ts; 86 } 87 88 static inline void 89 set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec) 90 { 91 /* XXX: this doesn't actually normalize anything */ 92 ts->tv_sec = sec; 93 ts->tv_nsec = nsec; 94 } 95 96 static inline int64_t 97 timespec_to_ns(const struct timespec *ts) 98 { 99 return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec); 100 } 101 102 static inline struct timespec 103 ns_to_timespec(const int64_t nsec) 104 { 105 struct timespec ts; 106 int32_t rem; 107 108 if (nsec == 0) { 109 ts.tv_sec = 0; 110 ts.tv_nsec = 0; 111 return (ts); 112 } 113 114 ts.tv_sec = nsec / NSEC_PER_SEC; 115 rem = nsec % NSEC_PER_SEC; 116 if (rem < 0) { 117 ts.tv_sec--; 118 rem += NSEC_PER_SEC; 119 } 120 ts.tv_nsec = rem; 121 return (ts); 122 } 123 124 #define ns_to_timespec64(_x) ns_to_timespec(_x) 125 126 static inline int 127 timespec_valid(const struct timespec *ts) 128 { 129 if (ts->tv_sec < 0 || ts->tv_sec > 100000000 || 130 ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 131 return (0); 132 return (1); 133 } 134 135 static inline unsigned long 136 get_seconds(void) 137 { 138 return time_uptime; 139 } 140 141 #endif /* _LINUXKPI_LINUX_TIME_H_ */ 142