| 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 | |
| 43 | #define TRACE_CLOCK_GENERIC |
| 44 | #ifdef TRACE_CLOCK_GENERIC |
| 45 | |
| 46 | static __inline__ u64 trace_clock_read64(void) |
| 47 | { |
| 48 | struct timespec ts; |
| 49 | u64 retval; |
| 50 | |
| 51 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 52 | retval = ts.tv_sec; |
| 53 | retval *= 1000000000; |
| 54 | retval += ts.tv_nsec; |
| 55 | |
| 56 | return retval; |
| 57 | } |
| 58 | |
| 59 | #else |
| 60 | |
| 61 | #if __i386 || __x86_64 |
| 62 | |
| 63 | /* WARNING: Make sure to set frequency and scaling functions that will not |
| 64 | * result in lttv timestamps (sec.nsec) with seconds greater than 2**32-1. |
| 65 | */ |
| 66 | static __inline__ u64 trace_clock_read64(void) |
| 67 | { |
| 68 | uint32_t low; |
| 69 | uint32_t high; |
| 70 | uint64_t retval; |
| 71 | __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high)); |
| 72 | |
| 73 | retval = high; |
| 74 | retval <<= 32; |
| 75 | return retval | low; |
| 76 | } |
| 77 | |
| 78 | #endif /* __i386 || __x86_64 */ |
| 79 | |
| 80 | #ifdef __PPC__ |
| 81 | |
| 82 | static __inline__ u64 trace_clock_read64(void) |
| 83 | { |
| 84 | unsigned long tb_l; |
| 85 | unsigned long tb_h; |
| 86 | unsigned long tb_h2; |
| 87 | u64 tb; |
| 88 | |
| 89 | __asm__ ( |
| 90 | "1:\n\t" |
| 91 | "mftbu %[rhigh]\n\t" |
| 92 | "mftb %[rlow]\n\t" |
| 93 | "mftbu %[rhigh2]\n\t" |
| 94 | "cmpw %[rhigh],%[rhigh2]\n\t" |
| 95 | "bne 1b\n\t" |
| 96 | : [rhigh] "=r" (tb_h), [rhigh2] "=r" (tb_h2), [rlow] "=r" (tb_l)); |
| 97 | |
| 98 | tb = tb_h; |
| 99 | tb <<= 32; |
| 100 | tb |= tb_l; |
| 101 | |
| 102 | return tb; |
| 103 | } |
| 104 | |
| 105 | #endif /* __PPC__ */ |
| 106 | |
| 107 | #endif /* ! UST_TRACE_CLOCK_GENERIC */ |
| 108 | |
| 109 | static __inline__ u64 trace_clock_frequency(void) |
| 110 | { |
| 111 | return 1000000000LL; |
| 112 | } |
| 113 | |
| 114 | static __inline__ u32 trace_clock_freq_scale(void) |
| 115 | { |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | #endif /* UST_CLOCK_H */ |