events cycle count fix, reverse byte order fix
[lttv.git] / ltt / branches / poly / ltt / time.h
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 #include <ltt/ltt-types.h>
27
28 typedef struct _LttTime {
29 unsigned long tv_sec;
30 unsigned long tv_nsec;
31 } LttTime;
32
33
34 #define NANOSECONDS_PER_SECOND 1000000000
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 */
38 /* 2^30/1.07374182400631629848 = 1000000000.0 */
39 #define DOUBLE_SHIFT_CONST_DIV 1.07374182400631629848
40 #define DOUBLE_SHIFT 30
41
42 /* 2^30*0.93132257461547851562 = 1000000000.0000000000 */
43 #define DOUBLE_SHIFT_CONST_MUL 0.93132257461547851562
44
45
46 /* 1953125 * 2^9 = NANOSECONDS_PER_SECOND */
47 #define LTT_TIME_UINT_SHIFT_CONST 1953125
48 #define LTT_TIME_UINT_SHIFT 9
49
50
51 static const LttTime ltt_time_zero = { 0, 0 };
52
53 static const LttTime ltt_time_one = { 0, 1 };
54
55 static const LttTime ltt_time_infinite = { G_MAXUINT, NANOSECONDS_PER_SECOND };
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;
61 res.tv_nsec = t1.tv_nsec - t2.tv_nsec;
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)) {
67 res.tv_sec--;
68 res.tv_nsec += NANOSECONDS_PER_SECOND;
69 }
70 return res;
71 }
72
73
74 static inline LttTime ltt_time_add(LttTime t1, LttTime t2)
75 {
76 LttTime res;
77 res.tv_nsec = t1.tv_nsec + t2.tv_nsec;
78 res.tv_sec = t1.tv_sec + t2.tv_sec;
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)) {
84 res.tv_sec++;
85 res.tv_nsec -= NANOSECONDS_PER_SECOND;
86 }
87 return res;
88 }
89
90 /* Fastest comparison : t1 > t2 */
91 static inline int ltt_time_compare(LttTime t1, LttTime t2)
92 {
93 int ret=0;
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;
98
99 return ret;
100 }
101
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
105 #define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF
106 static inline double ltt_time_to_double(LttTime t1)
107 {
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 * */
116 #ifdef EXTRA_CHECK
117 g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE);
118 if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE)
119 g_warning("Precision loss in conversion LttTime to double");
120 #endif //EXTRA_CHECK
121 return ((double)((guint64)t1.tv_sec<<DOUBLE_SHIFT)
122 * (double)DOUBLE_SHIFT_CONST_MUL)
123 + (double)t1.tv_nsec;
124 }
125
126
127 static inline LttTime ltt_time_from_double(double t1)
128 {
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 * */
137 #ifdef EXTRA_CHECK
138 g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE);
139 if(t1 > MAX_TV_SEC_TO_DOUBLE)
140 g_warning("Conversion from non precise double to LttTime");
141 #endif //EXTRA_CHECK
142 LttTime res;
143 //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND;
144 res.tv_sec = (guint64)(t1 * DOUBLE_SHIFT_CONST_DIV) >> DOUBLE_SHIFT;
145 res.tv_nsec = (t1 - (((guint64)res.tv_sec<<LTT_TIME_UINT_SHIFT))
146 * LTT_TIME_UINT_SHIFT_CONST);
147 return res;
148 }
149
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
215
216 static inline guint64 ltt_time_to_uint64(LttTime t1)
217 {
218 return (((guint64)t1.tv_sec*LTT_TIME_UINT_SHIFT_CONST) << LTT_TIME_UINT_SHIFT)
219 + (guint64)t1.tv_nsec;
220 }
221
222
223 #define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL
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 */
228 static inline LttTime ltt_time_from_uint64(guint64 t1)
229 {
230 /* We lose precision if tv_sec is > than (2^62)-1
231 * */
232 #ifdef EXTRA_CHECK
233 g_assert(t1 <= MAX_TV_SEC_TO_UINT64);
234 if(t1 > MAX_TV_SEC_TO_UINT64)
235 g_warning("Conversion from uint64 to non precise LttTime");
236 #endif //EXTRA_CHECK
237 LttTime res;
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
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 }
248 return res;
249 }
250
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
264 #endif // LTT_TIME_H
This page took 0.0399 seconds and 4 git commands to generate.