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