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 | #ifndef ARM_COMPUTE_TEST_PMU |
| 25 | #define ARM_COMPUTE_TEST_PMU |
| 26 | |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | |
| 29 | #include <cstdint> |
Anthony Barbier | 503cec8 | 2018-01-25 15:49:23 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 31 | #include <linux/perf_event.h> |
| 32 | #include <stdexcept> |
Moritz Pflanzer | 29088d5 | 2017-09-21 15:06:59 +0100 | [diff] [blame] | 33 | #include <sys/syscall.h> |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace framework |
| 41 | { |
| 42 | /** Class provides access to CPU hardware counters. */ |
| 43 | class PMU |
| 44 | { |
| 45 | public: |
| 46 | /** Default constructor. */ |
| 47 | PMU(); |
| 48 | |
| 49 | /** Create PMU with specified counter. |
| 50 | * |
| 51 | * This constructor automatically calls @ref open with the default |
| 52 | * configuration. |
| 53 | * |
| 54 | * @param[in] config Counter identifier. |
| 55 | */ |
| 56 | explicit PMU(uint64_t config); |
| 57 | |
| 58 | /** Default destructor. */ |
| 59 | ~PMU(); |
| 60 | |
| 61 | /** Get the counter value. |
| 62 | * |
| 63 | * @return Counter value casted to the specified type. */ |
| 64 | template <typename T> |
| 65 | T get_value() const; |
| 66 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 67 | /** Open the specified counter based on the default configuration. |
| 68 | * |
| 69 | * @param[in] config The default configuration. |
| 70 | */ |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 71 | void open(uint64_t config); |
| 72 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 73 | /** Open the specified configuration. |
| 74 | * |
| 75 | * @param[in] perf_config The specified configuration. |
| 76 | */ |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 77 | void open(const perf_event_attr &perf_config); |
| 78 | |
| 79 | /** Close the currently open counter. */ |
| 80 | void close(); |
| 81 | |
| 82 | /** Reset counter. */ |
| 83 | void reset(); |
| 84 | |
| 85 | private: |
| 86 | perf_event_attr _perf_config; |
| 87 | long _fd{ -1 }; |
| 88 | }; |
| 89 | |
| 90 | template <typename T> |
| 91 | T PMU::get_value() const |
| 92 | { |
| 93 | T value{}; |
| 94 | const ssize_t result = read(_fd, &value, sizeof(T)); |
| 95 | |
Anthony Barbier | 144d2ff | 2017-09-29 10:46:08 +0100 | [diff] [blame] | 96 | if(result == -1) |
| 97 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 98 | ARM_COMPUTE_ERROR_VAR("Can't get PMU counter value: %d", errno); |
Anthony Barbier | 144d2ff | 2017-09-29 10:46:08 +0100 | [diff] [blame] | 99 | } |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 100 | |
| 101 | return value; |
| 102 | } |
| 103 | } // namespace framework |
| 104 | } // namespace test |
| 105 | } // namespace arm_compute |
| 106 | #endif /* ARM_COMPUTE_TEST_PMU */ |