| 1 | /* |
| 2 | * Copyright (c) 2016 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * as published by the Free Software Foundation; only version 2 |
| 7 | * of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <perfmon/pfmlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | int main(int argc, char **argv) |
| 24 | { |
| 25 | int ret, i; |
| 26 | unsigned int j; |
| 27 | pfm_pmu_info_t pinfo; |
| 28 | |
| 29 | if (argc != 2) { |
| 30 | fprintf(stderr, "Usage: %s <pmu counter to find>\n" |
| 31 | "ex: %s UNHALTED_REFERENCE_CYCLES\n" |
| 32 | "Returns the first occurence it finds with " |
| 33 | "return code 0.\n" |
| 34 | "If not found returns 1, on error returns -1\n", |
| 35 | argv[0], argv[0]); |
| 36 | ret = -1; |
| 37 | goto end; |
| 38 | } |
| 39 | |
| 40 | memset(&pinfo, 0, sizeof(pinfo)); |
| 41 | pinfo.size = sizeof(pinfo); |
| 42 | |
| 43 | ret = pfm_initialize(); |
| 44 | if (ret != PFM_SUCCESS) { |
| 45 | fprintf(stderr, "Failed to initialise libpfm: %s", |
| 46 | pfm_strerror(ret)); |
| 47 | ret = -1; |
| 48 | goto end; |
| 49 | } |
| 50 | |
| 51 | pfm_for_all_pmus(j) { |
| 52 | ret = pfm_get_pmu_info(j, &pinfo); |
| 53 | if (ret != PFM_SUCCESS) { |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | for (i = pinfo.first_event; i != -1; i = pfm_get_event_next(i)) { |
| 58 | pfm_event_info_t info = |
| 59 | { .size = sizeof(pfm_event_info_t) }; |
| 60 | |
| 61 | ret = pfm_get_event_info(i, PFM_OS_NONE, &info); |
| 62 | if (ret != PFM_SUCCESS) { |
| 63 | fprintf(stderr, "Cannot get event info: %s\n", |
| 64 | pfm_strerror(ret)); |
| 65 | ret = -1; |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | if (info.pmu != j) { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | if (strcmp(info.name, argv[1]) == 0) { |
| 74 | fprintf(stdout, "r%" PRIx64 "\n", info.code); |
| 75 | ret = 0; |
| 76 | goto end; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | ret = 1; |
| 82 | |
| 83 | end: |
| 84 | return ret; |
| 85 | } |