Commit | Line | Data |
---|---|---|
518d7abb PMF |
1 | /* Copyright (C) 2010 Pierre-Marc Fournier |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
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 Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
9c6bb081 JD |
18 | #ifndef _UST_CLOCK_H |
19 | #define _UST_CLOCK_H | |
518d7abb | 20 | |
ca4525b5 | 21 | #include <time.h> |
518d7abb | 22 | #include <sys/time.h> |
9edd34bd MD |
23 | #include <stdint.h> |
24 | #include <stddef.h> | |
25 | #include <ust/core.h> | |
518d7abb PMF |
26 | |
27 | /* TRACE CLOCK */ | |
28 | ||
29 | /* There are two types of clocks that can be used. | |
30 | - TSC based clock | |
31 | - gettimeofday() clock | |
32 | ||
33 | Microbenchmarks on Linux 2.6.30 on Core2 Duo 3GHz (functions are inlined): | |
9c6bb081 JD |
34 | Calls (100000000) to tsc(): 4004035641 cycles or 40 cycles/call |
35 | Calls (100000000) to gettimeofday(): 9723158352 cycles or 97 cycles/call | |
518d7abb PMF |
36 | |
37 | For merging traces with the kernel, a time source compatible with that of | |
38 | the kernel is necessary. | |
39 | ||
ca4525b5 PMF |
40 | Instead of gettimeofday(), we are now using clock_gettime for better |
41 | precision and monotonicity. | |
518d7abb PMF |
42 | */ |
43 | ||
9c6bb081 JD |
44 | /* Only available for x86 arch */ |
45 | #define CLOCK_TRACE_FREQ 14 | |
46 | #define CLOCK_TRACE 15 | |
47 | union lttng_timespec { | |
ca4525b5 | 48 | struct timespec ts; |
9edd34bd | 49 | uint64_t lttng_ts; |
9c6bb081 | 50 | }; |
518d7abb | 51 | |
08b070db | 52 | extern int ust_clock_source; |
518d7abb | 53 | |
9c6bb081 | 54 | /* Choosing correct trace clock */ |
518d7abb | 55 | |
9edd34bd | 56 | static __inline__ uint64_t trace_clock_read64(void) |
9c6bb081 JD |
57 | { |
58 | struct timespec ts; | |
9edd34bd | 59 | uint64_t retval; |
9c6bb081 JD |
60 | union lttng_timespec *lts = (union lttng_timespec *) &ts; |
61 | ||
62 | clock_gettime(ust_clock_source, &ts); | |
63 | /* | |
64 | * Clock source can change when loading the binary (tracectl.c) | |
65 | * so we must check if the clock source has changed before | |
66 | * returning the correct value | |
67 | */ | |
68 | if (likely(ust_clock_source == CLOCK_TRACE)) { | |
69 | retval = lts->lttng_ts; | |
70 | } else { /* CLOCK_MONOTONIC */ | |
71 | retval = ts.tv_sec; | |
72 | retval *= 1000000000; | |
73 | retval += ts.tv_nsec; | |
74 | } | |
75 | ||
76 | return retval; | |
77 | } | |
78 | ||
772d939d | 79 | #if __i386__ || __x86_64__ |
9edd34bd | 80 | static __inline__ uint64_t trace_clock_frequency(void) |
518d7abb | 81 | { |
9c6bb081 JD |
82 | struct timespec ts; |
83 | union lttng_timespec *lts = (union lttng_timespec *) &ts; | |
84 | ||
85 | if (likely(ust_clock_source == CLOCK_TRACE)) { | |
86 | clock_gettime(CLOCK_TRACE_FREQ, &ts); | |
87 | return lts->lttng_ts; | |
88 | } | |
ca4525b5 | 89 | return 1000000000LL; |
518d7abb | 90 | } |
772d939d | 91 | #else /* #if __i386__ || __x86_64__ */ |
9edd34bd | 92 | static __inline__ uint64_t trace_clock_frequency(void) |
772d939d MD |
93 | { |
94 | return 1000000000LL; | |
95 | } | |
96 | #endif /* #else #if __i386__ || __x86_64__ */ | |
518d7abb | 97 | |
9edd34bd | 98 | static __inline__ uint32_t trace_clock_freq_scale(void) |
518d7abb PMF |
99 | { |
100 | return 1; | |
101 | } | |
102 | ||
9c6bb081 | 103 | #endif /* _UST_CLOCK_H */ |