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