| 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 <sys/time.h> |
| 22 | #include <ust/kcompat/kcompat.h> |
| 23 | |
| 24 | /* TRACE CLOCK */ |
| 25 | |
| 26 | /* There are two types of clocks that can be used. |
| 27 | - TSC based clock |
| 28 | - gettimeofday() clock |
| 29 | |
| 30 | Microbenchmarks on Linux 2.6.30 on Core2 Duo 3GHz (functions are inlined): |
| 31 | Calls (100000000) to tsc(): 4004035641 cycles or 40 cycles/call |
| 32 | Calls (100000000) to gettimeofday(): 9723158352 cycles or 97 cycles/call |
| 33 | |
| 34 | For merging traces with the kernel, a time source compatible with that of |
| 35 | the kernel is necessary. |
| 36 | |
| 37 | */ |
| 38 | |
| 39 | #define TRACE_CLOCK_GENERIC |
| 40 | #ifdef TRACE_CLOCK_GENERIC |
| 41 | |
| 42 | static __inline__ u64 trace_clock_read64(void) |
| 43 | { |
| 44 | struct timeval tv; |
| 45 | u64 retval; |
| 46 | |
| 47 | gettimeofday(&tv, NULL); |
| 48 | retval = tv.tv_sec; |
| 49 | retval *= 1000000; |
| 50 | retval += tv.tv_usec; |
| 51 | |
| 52 | return retval; |
| 53 | } |
| 54 | |
| 55 | #else |
| 56 | |
| 57 | #if __i386 || __x86_64 |
| 58 | |
| 59 | /* WARNING: Make sure to set frequency and scaling functions that will not |
| 60 | * result in lttv timestamps (sec.nsec) with seconds greater than 2**32-1. |
| 61 | */ |
| 62 | static __inline__ u64 trace_clock_read64(void) |
| 63 | { |
| 64 | uint32_t low; |
| 65 | uint32_t high; |
| 66 | uint64_t retval; |
| 67 | __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high)); |
| 68 | |
| 69 | retval = high; |
| 70 | retval <<= 32; |
| 71 | return retval | low; |
| 72 | } |
| 73 | |
| 74 | #endif /* __i386 || __x86_64 */ |
| 75 | |
| 76 | #ifdef __PPC__ |
| 77 | |
| 78 | static __inline__ u64 trace_clock_read64(void) |
| 79 | { |
| 80 | unsigned long tb_l; |
| 81 | unsigned long tb_h; |
| 82 | unsigned long tb_h2; |
| 83 | u64 tb; |
| 84 | |
| 85 | __asm__ ( |
| 86 | "1:\n\t" |
| 87 | "mftbu %[rhigh]\n\t" |
| 88 | "mftb %[rlow]\n\t" |
| 89 | "mftbu %[rhigh2]\n\t" |
| 90 | "cmpw %[rhigh],%[rhigh2]\n\t" |
| 91 | "bne 1b\n\t" |
| 92 | : [rhigh] "=r" (tb_h), [rhigh2] "=r" (tb_h2), [rlow] "=r" (tb_l)); |
| 93 | |
| 94 | tb = tb_h; |
| 95 | tb <<= 32; |
| 96 | tb |= tb_l; |
| 97 | |
| 98 | return tb; |
| 99 | } |
| 100 | |
| 101 | #endif /* __PPC__ */ |
| 102 | |
| 103 | #endif /* ! UST_TRACE_CLOCK_GENERIC */ |
| 104 | |
| 105 | static __inline__ u64 trace_clock_frequency(void) |
| 106 | { |
| 107 | return 1000000LL; |
| 108 | } |
| 109 | |
| 110 | static __inline__ u32 trace_clock_freq_scale(void) |
| 111 | { |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | #endif /* UST_CLOCK_H */ |