| 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 | |
| 18 | #ifndef _UST_CLOCK_H |
| 19 | #define _UST_CLOCK_H |
| 20 | |
| 21 | #include <time.h> |
| 22 | #include <sys/time.h> |
| 23 | #include <ust/kcompat/kcompat.h> |
| 24 | |
| 25 | /* TRACE CLOCK */ |
| 26 | |
| 27 | /* There are two types of clocks that can be used. |
| 28 | - TSC based clock |
| 29 | - gettimeofday() clock |
| 30 | |
| 31 | Microbenchmarks on Linux 2.6.30 on Core2 Duo 3GHz (functions are inlined): |
| 32 | Calls (100000000) to tsc(): 4004035641 cycles or 40 cycles/call |
| 33 | Calls (100000000) to gettimeofday(): 9723158352 cycles or 97 cycles/call |
| 34 | |
| 35 | For merging traces with the kernel, a time source compatible with that of |
| 36 | the kernel is necessary. |
| 37 | |
| 38 | Instead of gettimeofday(), we are now using clock_gettime for better |
| 39 | precision and monotonicity. |
| 40 | */ |
| 41 | |
| 42 | #if __i386__ || __x86_64__ |
| 43 | /* Only available for x86 arch */ |
| 44 | #define CLOCK_TRACE_FREQ 14 |
| 45 | #define CLOCK_TRACE 15 |
| 46 | union lttng_timespec { |
| 47 | struct timespec ts; |
| 48 | u64 lttng_ts; |
| 49 | }; |
| 50 | #endif /* __i386__ || __x86_64__ */ |
| 51 | |
| 52 | static int ust_clock_source; |
| 53 | |
| 54 | /* Choosing correct trace clock */ |
| 55 | #if __PPC__ |
| 56 | static __inline__ u64 trace_clock_read64(void) |
| 57 | { |
| 58 | unsigned long tb_l; |
| 59 | unsigned long tb_h; |
| 60 | unsigned long tb_h2; |
| 61 | u64 tb; |
| 62 | |
| 63 | __asm__ ( |
| 64 | "1:\n\t" |
| 65 | "mftbu %[rhigh]\n\t" |
| 66 | "mftb %[rlow]\n\t" |
| 67 | "mftbu %[rhigh2]\n\t" |
| 68 | "cmpw %[rhigh],%[rhigh2]\n\t" |
| 69 | "bne 1b\n\t" |
| 70 | : [rhigh] "=r" (tb_h), [rhigh2] "=r" (tb_h2), [rlow] "=r" (tb_l)); |
| 71 | |
| 72 | tb = tb_h; |
| 73 | tb <<= 32; |
| 74 | tb |= tb_l; |
| 75 | |
| 76 | return tb; |
| 77 | } |
| 78 | |
| 79 | #else /* !__PPC__ */ |
| 80 | |
| 81 | static __inline__ u64 trace_clock_read64(void) |
| 82 | { |
| 83 | struct timespec ts; |
| 84 | u64 retval; |
| 85 | union lttng_timespec *lts = (union lttng_timespec *) &ts; |
| 86 | |
| 87 | clock_gettime(ust_clock_source, &ts); |
| 88 | /* |
| 89 | * Clock source can change when loading the binary (tracectl.c) |
| 90 | * so we must check if the clock source has changed before |
| 91 | * returning the correct value |
| 92 | */ |
| 93 | if (likely(ust_clock_source == CLOCK_TRACE)) { |
| 94 | retval = lts->lttng_ts; |
| 95 | } else { /* CLOCK_MONOTONIC */ |
| 96 | retval = ts.tv_sec; |
| 97 | retval *= 1000000000; |
| 98 | retval += ts.tv_nsec; |
| 99 | } |
| 100 | |
| 101 | return retval; |
| 102 | } |
| 103 | |
| 104 | #endif /* __PPC__ */ |
| 105 | |
| 106 | static __inline__ u64 trace_clock_frequency(void) |
| 107 | { |
| 108 | struct timespec ts; |
| 109 | union lttng_timespec *lts = (union lttng_timespec *) &ts; |
| 110 | |
| 111 | if (likely(ust_clock_source == CLOCK_TRACE)) { |
| 112 | clock_gettime(CLOCK_TRACE_FREQ, &ts); |
| 113 | return lts->lttng_ts; |
| 114 | } |
| 115 | return 1000000000LL; |
| 116 | } |
| 117 | |
| 118 | static __inline__ u32 trace_clock_freq_scale(void) |
| 119 | { |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | #endif /* _UST_CLOCK_H */ |