Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 26 | pmbaty | 1 | /* ACC -- Automatic Compiler Configuration | 
| 2 | |||
| 3 | Copyright (C) 1996-2004 Markus Franz Xaver Johannes Oberhumer | ||
| 4 | All Rights Reserved. | ||
| 5 | |||
| 6 | This software is a copyrighted work licensed under the terms of | ||
| 7 | the GNU General Public License. Please consult the file "ACC_LICENSE" | ||
| 8 | for details. | ||
| 9 | |||
| 10 | Markus F.X.J. Oberhumer | ||
| 11 | <markus@oberhumer.com> | ||
| 12 | http://www.oberhumer.com/ | ||
| 13 | */ | ||
| 14 | |||
| 15 | |||
| 16 | #define __ACCLIB_PERFCTR_CH_INCLUDED 1 | ||
| 17 | #if !defined(ACCLIB_PUBLIC) | ||
| 18 | # define ACCLIB_PUBLIC(r,f) r __ACCLIB_FUNCNAME(f) | ||
| 19 | #endif | ||
| 20 | |||
| 21 | |||
| 22 | #if (ACC_OS_POSIX_LINUX) | ||
| 23 | /* see http://user.it.uu.se/~mikpe/linux/perfctr/ */ | ||
| 24 | #if defined(__cplusplus) | ||
| 25 | extern "C" { | ||
| 26 | #include <libperfctr.h> | ||
| 27 | } | ||
| 28 | #else | ||
| 29 | #include <libperfctr.h> | ||
| 30 | #endif | ||
| 31 | #endif | ||
| 32 | |||
| 33 | |||
| 34 | /************************************************************************* | ||
| 35 | // | ||
| 36 | **************************************************************************/ | ||
| 37 | |||
| 38 | ACCLIB_PUBLIC(int, acc_perfctr_open) (acc_perfctr_handle_p h) | ||
| 39 | { | ||
| 40 | memset(h, 0, sizeof(*h)); | ||
| 41 | #if (ACC_OS_POSIX_LINUX) | ||
| 42 |     { | ||
| 43 | struct vperfctr* handle; | ||
| 44 | struct perfctr_info info; | ||
| 45 | struct vperfctr_control control; | ||
| 46 | struct perfctr_cpu_control* const cc = &control.cpu_control; | ||
| 47 | /* open */ | ||
| 48 | handle = vperfctr_open(); | ||
| 49 | if (!handle) goto error; | ||
| 50 | /* get info */ | ||
| 51 | if (vperfctr_info(handle, &info) < 0) goto error; | ||
| 52 | h->cpu_type = info.cpu_type; | ||
| 53 | h->cpu_features = info.cpu_features; | ||
| 54 | h->cpu_khz = info.cpu_khz; | ||
| 55 | h->cpu_nrctrs = perfctr_info_nrctrs(&info); | ||
| 56 | h->cpu_name = perfctr_info_cpu_name(&info); | ||
| 57 | /* setup control */ | ||
| 58 | memset(&control, 0, sizeof(control)); | ||
| 59 |     switch (h->cpu_type) { | ||
| 60 | #if (ACC_ARCH_IA32) | ||
| 61 | case PERFCTR_X86_WINCHIP_C6: | ||
| 62 | case PERFCTR_X86_WINCHIP_2: | ||
| 63 | break; /* no working TSC available */ | ||
| 64 | case PERFCTR_X86_AMD_K7: | ||
| 65 | #endif | ||
| 66 | #if (ACC_ARCH_AMD64 || ACC_ARCH_IA32) | ||
| 67 | case PERFCTR_X86_AMD_K8: | ||
| 68 | case PERFCTR_X86_AMD_K8C: | ||
| 69 | cc->tsc_on = 1; cc->nractrs = 2; | ||
| 70 | /* event 0xC0 (RETIRED_INSNS), count at CPL > 0, Enable */ | ||
| 71 | cc->pmc_map[0] = 0; | ||
| 72 | cc->evntsel[0] = 0xC0 | (1 << 16) | (1 << 22); | ||
| 73 | /* event 0xC1 (RETIRED_OPS), count at CPL > 0, Enable */ | ||
| 74 | cc->pmc_map[1] = 1; | ||
| 75 | cc->evntsel[1] = 0xC1 | (1 << 16) | (1 << 22); | ||
| 76 | break; | ||
| 77 | #endif | ||
| 78 | default: | ||
| 79 | cc->tsc_on = 1; | ||
| 80 | break; | ||
| 81 | } | ||
| 82 | if (cc->nractrs > h->cpu_nrctrs) cc->nractrs = h->cpu_nrctrs; | ||
| 83 | if (vperfctr_control(handle, &control) < 0) goto error; | ||
| 84 | /* success */ | ||
| 85 | h->h = (void*) handle; | ||
| 86 | return 0; | ||
| 87 | error: | ||
| 88 |     if (handle) { | ||
| 89 | vperfctr_stop(handle); | ||
| 90 | vperfctr_close(handle); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | #endif | ||
| 94 | return -1; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | ACCLIB_PUBLIC(int, acc_perfctr_close) (acc_perfctr_handle_p h) | ||
| 99 | { | ||
| 100 |     if (h->h) { | ||
| 101 | #if (ACC_OS_POSIX_LINUX) | ||
| 102 | struct vperfctr* handle = (struct vperfctr*) h->h; | ||
| 103 | vperfctr_stop(handle); | ||
| 104 | vperfctr_close(handle); | ||
| 105 | #endif | ||
| 106 | h->h = 0; | ||
| 107 | } | ||
| 108 | return 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | /************************************************************************* | ||
| 113 | // | ||
| 114 | **************************************************************************/ | ||
| 115 | |||
| 116 | ACCLIB_PUBLIC(void, acc_perfctr_read) (acc_perfctr_handle_p h, acc_perfctr_clock_p c) | ||
| 117 | { | ||
| 118 |     if (h->h) { | ||
| 119 | #if (ACC_OS_POSIX_LINUX) | ||
| 120 | struct vperfctr* handle = (struct vperfctr*) h->h; | ||
| 121 | vperfctr_read_ctrs(handle, (struct perfctr_sum_ctrs*) c); | ||
| 122 | #else | ||
| 123 | memset(c, 0, sizeof(*c)); | ||
| 124 | #endif | ||
| 125 | } else | ||
| 126 | memset(c, 0, sizeof(*c)); | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | /************************************************************************* | ||
| 131 | // | ||
| 132 | **************************************************************************/ | ||
| 133 | |||
| 134 | ACCLIB_PUBLIC(double, acc_perfctr_get_elapsed) (acc_perfctr_handle_p h, const acc_perfctr_clock_p start, const acc_perfctr_clock_p stop) | ||
| 135 | { | ||
| 136 | #if (ACC_OS_POSIX_LINUX) | ||
| 137 | acc_uint64l_t tsc = stop->tsc - start->tsc; | ||
| 138 | return ((double)tsc / h->cpu_khz) / 1000.0; | ||
| 139 | #else | ||
| 140 | ACC_UNUSED(h); ACC_UNUSED(start); ACC_UNUSED(stop); return 0; | ||
| 141 | #endif | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | ACCLIB_PUBLIC(double, acc_perfctr_get_elapsed_tsc) (acc_perfctr_handle_p h, acc_uint64l_t tsc) | ||
| 146 | { | ||
| 147 | #if (ACC_OS_POSIX_LINUX) | ||
| 148 | return ((double)tsc / h->cpu_khz) / 1000.0; | ||
| 149 | #else | ||
| 150 | ACC_UNUSED(h); ACC_UNUSED(tsc); return 0; | ||
| 151 | #endif | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | /* | ||
| 156 | vi:ts=4:et | ||
| 157 | */ |