Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Arm Limited. |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "PMU.h" |
| 25 | |
| 26 | #include <asm/unistd.h> |
| 27 | #include <cstring> |
| 28 | #include <stdexcept> |
| 29 | #include <sys/ioctl.h> |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace framework |
| 36 | { |
| 37 | PMU::PMU() |
| 38 | : _perf_config() |
| 39 | { |
| 40 | _perf_config.type = PERF_TYPE_HARDWARE; |
| 41 | _perf_config.size = sizeof(perf_event_attr); |
| 42 | |
| 43 | // Start disabled |
| 44 | _perf_config.disabled = 1; |
| 45 | // The inherit bit specifies that this counter should count events of child |
| 46 | // tasks as well as the task specified |
| 47 | _perf_config.inherit = 1; |
| 48 | // Enables saving of event counts on context switch for inherited tasks |
| 49 | _perf_config.inherit_stat = 1; |
| 50 | } |
| 51 | |
| 52 | PMU::PMU(uint64_t config) |
| 53 | : PMU() |
| 54 | { |
| 55 | open(config); |
| 56 | } |
| 57 | |
| 58 | PMU::~PMU() |
| 59 | { |
| 60 | close(); |
| 61 | } |
| 62 | |
| 63 | void PMU::open(uint64_t config) |
| 64 | { |
| 65 | _perf_config.config = config; |
| 66 | open(_perf_config); |
| 67 | } |
| 68 | |
| 69 | void PMU::open(const perf_event_attr &perf_config) |
| 70 | { |
| 71 | // Measure this process/thread (+ children) on any CPU |
| 72 | _fd = syscall(__NR_perf_event_open, &perf_config, 0, -1, -1, 0); |
| 73 | |
| 74 | ARM_COMPUTE_ERROR_ON_MSG(_fd < 0, "perf_event_open failed"); |
| 75 | |
| 76 | const int result = ioctl(_fd, PERF_EVENT_IOC_ENABLE, 0); |
Anthony Barbier | 144d2ff | 2017-09-29 10:46:08 +0100 | [diff] [blame] | 77 | if(result == -1) |
| 78 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 79 | ARM_COMPUTE_ERROR_VAR("Failed to enable PMU counter: %d", errno); |
Anthony Barbier | 144d2ff | 2017-09-29 10:46:08 +0100 | [diff] [blame] | 80 | } |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void PMU::close() |
| 84 | { |
| 85 | if(_fd != -1) |
| 86 | { |
| 87 | ::close(_fd); |
| 88 | _fd = -1; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void PMU::reset() |
| 93 | { |
| 94 | const int result = ioctl(_fd, PERF_EVENT_IOC_RESET, 0); |
Anthony Barbier | 144d2ff | 2017-09-29 10:46:08 +0100 | [diff] [blame] | 95 | if(result == -1) |
| 96 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 97 | ARM_COMPUTE_ERROR_VAR("Failed to reset PMU counter: %d", errno); |
Anthony Barbier | 144d2ff | 2017-09-29 10:46:08 +0100 | [diff] [blame] | 98 | } |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 99 | } |
| 100 | } // namespace framework |
| 101 | } // namespace test |
| 102 | } // namespace arm_compute |