| 1 | /* This file is part of the Linux Trace Toolkit trace reading library |
| 2 | * Copyright (C) 2003-2004 Michel Dagenais |
| 3 | * 2005 Mathieu Desnoyers |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License Version 2.1 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifndef LTT_TIME_H |
| 21 | #define LTT_TIME_H |
| 22 | |
| 23 | #include <glib.h> |
| 24 | #include <ltt/compiler.h> |
| 25 | #include <math.h> |
| 26 | |
| 27 | typedef struct _LttTime { |
| 28 | unsigned long tv_sec; |
| 29 | unsigned long tv_nsec; |
| 30 | } LttTime; |
| 31 | |
| 32 | |
| 33 | #define NANOSECONDS_PER_SECOND 1000000000 |
| 34 | |
| 35 | /* We give the DIV and MUL constants so we can always multiply, for a |
| 36 | * division as well as a multiplication of NANOSECONDS_PER_SECOND */ |
| 37 | /* 2^30/1.07374182400631629848 = 1000000000.0 */ |
| 38 | #define DOUBLE_SHIFT_CONST_DIV 1.07374182400631629848 |
| 39 | #define DOUBLE_SHIFT 30 |
| 40 | |
| 41 | /* 2^30*0.93132257461547851562 = 1000000000.0000000000 */ |
| 42 | #define DOUBLE_SHIFT_CONST_MUL 0.93132257461547851562 |
| 43 | |
| 44 | |
| 45 | /* 1953125 * 2^9 = NANOSECONDS_PER_SECOND */ |
| 46 | #define LTT_TIME_UINT_SHIFT_CONST 1953125 |
| 47 | #define LTT_TIME_UINT_SHIFT 9 |
| 48 | |
| 49 | |
| 50 | static const LttTime ltt_time_zero = { 0, 0 }; |
| 51 | |
| 52 | static const LttTime ltt_time_one = { 0, 1 }; |
| 53 | |
| 54 | static const LttTime ltt_time_infinite = { G_MAXUINT, NANOSECONDS_PER_SECOND }; |
| 55 | |
| 56 | static inline LttTime ltt_time_sub(LttTime t1, LttTime t2) |
| 57 | { |
| 58 | LttTime res; |
| 59 | res.tv_sec = t1.tv_sec - t2.tv_sec; |
| 60 | res.tv_nsec = t1.tv_nsec - t2.tv_nsec; |
| 61 | /* unlikely : given equal chance to be anywhere in t1.tv_nsec, and |
| 62 | * higher probability of low value for t2.tv_sec, we will habitually |
| 63 | * not wrap. |
| 64 | */ |
| 65 | if(unlikely(t1.tv_nsec < t2.tv_nsec)) { |
| 66 | res.tv_sec--; |
| 67 | res.tv_nsec += NANOSECONDS_PER_SECOND; |
| 68 | } |
| 69 | return res; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static inline LttTime ltt_time_add(LttTime t1, LttTime t2) |
| 74 | { |
| 75 | LttTime res; |
| 76 | res.tv_nsec = t1.tv_nsec + t2.tv_nsec; |
| 77 | res.tv_sec = t1.tv_sec + t2.tv_sec; |
| 78 | /* unlikely : given equal chance to be anywhere in t1.tv_nsec, and |
| 79 | * higher probability of low value for t2.tv_sec, we will habitually |
| 80 | * not wrap. |
| 81 | */ |
| 82 | if(unlikely(res.tv_nsec >= NANOSECONDS_PER_SECOND)) { |
| 83 | res.tv_sec++; |
| 84 | res.tv_nsec -= NANOSECONDS_PER_SECOND; |
| 85 | } |
| 86 | return res; |
| 87 | } |
| 88 | |
| 89 | /* Fastest comparison : t1 > t2 */ |
| 90 | static inline int ltt_time_compare(LttTime t1, LttTime t2) |
| 91 | { |
| 92 | int ret=0; |
| 93 | if(likely(t1.tv_sec > t2.tv_sec)) ret = 1; |
| 94 | else if(unlikely(t1.tv_sec < t2.tv_sec)) ret = -1; |
| 95 | else if(likely(t1.tv_nsec > t2.tv_nsec)) ret = 1; |
| 96 | else if(unlikely(t1.tv_nsec < t2.tv_nsec)) ret = -1; |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | #define LTT_TIME_MIN(a,b) ((ltt_time_compare((a),(b)) < 0) ? (a) : (b)) |
| 102 | #define LTT_TIME_MAX(a,b) ((ltt_time_compare((a),(b)) > 0) ? (a) : (b)) |
| 103 | |
| 104 | #define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF |
| 105 | static inline double ltt_time_to_double(LttTime t1) |
| 106 | { |
| 107 | /* We lose precision if tv_sec is > than (2^23)-1 |
| 108 | * |
| 109 | * Max values that fits in a double (53 bits precision on normalised |
| 110 | * mantissa): |
| 111 | * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30 |
| 112 | * |
| 113 | * So we have 53-30 = 23 bits left for tv_sec. |
| 114 | * */ |
| 115 | #ifdef EXTRA_CHECK |
| 116 | g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE); |
| 117 | if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE) |
| 118 | g_warning("Precision loss in conversion LttTime to double"); |
| 119 | #endif //EXTRA_CHECK |
| 120 | return ((double)((guint64)t1.tv_sec<<DOUBLE_SHIFT) |
| 121 | * (double)DOUBLE_SHIFT_CONST_MUL) |
| 122 | + (double)t1.tv_nsec; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static inline LttTime ltt_time_from_double(double t1) |
| 127 | { |
| 128 | /* We lose precision if tv_sec is > than (2^23)-1 |
| 129 | * |
| 130 | * Max values that fits in a double (53 bits precision on normalised |
| 131 | * mantissa): |
| 132 | * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30 |
| 133 | * |
| 134 | * So we have 53-30 = 23 bits left for tv_sec. |
| 135 | * */ |
| 136 | #ifdef EXTRA_CHECK |
| 137 | g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE); |
| 138 | if(t1 > MAX_TV_SEC_TO_DOUBLE) |
| 139 | g_warning("Conversion from non precise double to LttTime"); |
| 140 | #endif //EXTRA_CHECK |
| 141 | LttTime res; |
| 142 | //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND; |
| 143 | res.tv_sec = (guint64)(t1 * DOUBLE_SHIFT_CONST_DIV) >> DOUBLE_SHIFT; |
| 144 | res.tv_nsec = (t1 - (((guint64)res.tv_sec<<LTT_TIME_UINT_SHIFT)) |
| 145 | * LTT_TIME_UINT_SHIFT_CONST); |
| 146 | return res; |
| 147 | } |
| 148 | |
| 149 | /* Use ltt_time_to_double and ltt_time_from_double to check for lack |
| 150 | * of precision. |
| 151 | */ |
| 152 | static inline LttTime ltt_time_mul(LttTime t1, double d) |
| 153 | { |
| 154 | LttTime res; |
| 155 | |
| 156 | double time_double = ltt_time_to_double(t1); |
| 157 | |
| 158 | time_double = time_double * d; |
| 159 | |
| 160 | res = ltt_time_from_double(time_double); |
| 161 | |
| 162 | return res; |
| 163 | |
| 164 | #if 0 |
| 165 | /* What is that ? (Mathieu) */ |
| 166 | if(f == 0.0){ |
| 167 | res.tv_sec = 0; |
| 168 | res.tv_nsec = 0; |
| 169 | }else{ |
| 170 | double d; |
| 171 | d = 1.0/f; |
| 172 | sec = t1.tv_sec / (double)d; |
| 173 | res.tv_sec = sec; |
| 174 | res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) * |
| 175 | NANOSECONDS_PER_SECOND; |
| 176 | res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND; |
| 177 | res.tv_nsec %= NANOSECONDS_PER_SECOND; |
| 178 | } |
| 179 | return res; |
| 180 | #endif //0 |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* Use ltt_time_to_double and ltt_time_from_double to check for lack |
| 185 | * of precision. |
| 186 | */ |
| 187 | static inline LttTime ltt_time_div(LttTime t1, double d) |
| 188 | { |
| 189 | LttTime res; |
| 190 | |
| 191 | double time_double = ltt_time_to_double(t1); |
| 192 | |
| 193 | time_double = time_double / d; |
| 194 | |
| 195 | res = ltt_time_from_double(time_double); |
| 196 | |
| 197 | return res; |
| 198 | |
| 199 | |
| 200 | #if 0 |
| 201 | double sec; |
| 202 | LttTime res; |
| 203 | |
| 204 | sec = t1.tv_sec / (double)f; |
| 205 | res.tv_sec = sec; |
| 206 | res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) * |
| 207 | NANOSECONDS_PER_SECOND; |
| 208 | res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND; |
| 209 | res.tv_nsec %= NANOSECONDS_PER_SECOND; |
| 210 | return res; |
| 211 | #endif //0 |
| 212 | } |
| 213 | |
| 214 | |
| 215 | static inline guint64 ltt_time_to_uint64(LttTime t1) |
| 216 | { |
| 217 | return (((guint64)t1.tv_sec*LTT_TIME_UINT_SHIFT_CONST) << LTT_TIME_UINT_SHIFT) |
| 218 | + (guint64)t1.tv_nsec; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | #define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL |
| 223 | |
| 224 | /* The likely branch is with sec != 0, because most events in a bloc |
| 225 | * will be over 1s from the block start. (see tracefile.c) |
| 226 | */ |
| 227 | static inline LttTime ltt_time_from_uint64(guint64 t1) |
| 228 | { |
| 229 | /* We lose precision if tv_sec is > than (2^62)-1 |
| 230 | * */ |
| 231 | #ifdef EXTRA_CHECK |
| 232 | g_assert(t1 <= MAX_TV_SEC_TO_UINT64); |
| 233 | if(t1 > MAX_TV_SEC_TO_UINT64) |
| 234 | g_warning("Conversion from uint64 to non precise LttTime"); |
| 235 | #endif //EXTRA_CHECK |
| 236 | LttTime res; |
| 237 | //if(unlikely(t1 >= NANOSECONDS_PER_SECOND)) { |
| 238 | if(likely(t1>>LTT_TIME_UINT_SHIFT >= LTT_TIME_UINT_SHIFT_CONST)) { |
| 239 | //res.tv_sec = t1/NANOSECONDS_PER_SECOND; |
| 240 | res.tv_sec = (t1>>LTT_TIME_UINT_SHIFT) |
| 241 | /LTT_TIME_UINT_SHIFT_CONST; // acceleration |
| 242 | res.tv_nsec = (t1 - res.tv_sec*NANOSECONDS_PER_SECOND); |
| 243 | } else { |
| 244 | res.tv_sec = 0; |
| 245 | res.tv_nsec = (guint32)t1; |
| 246 | } |
| 247 | return res; |
| 248 | } |
| 249 | |
| 250 | #endif // LTT_TIME_H |