1 /*
2 * Copyright (c) 2017-2019 Arm Limited.
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>
30 #include <errno.h>
31 #include <linux/perf_event.h>
32 #include <stdexcept>
33 #include <sys/syscall.h>
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
67 /** Open the specified counter based on the default configuration.
68 *
69 * @param[in] config The default configuration.
70 */
71 void open(uint64_t config);
72
73 /** Open the specified configuration.
74 *
75 * @param[in] perf_config The specified configuration.
76 */
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>
get_value()91 T PMU::get_value() const
92 {
93 T value{};
94 const ssize_t result = read(_fd, &value, sizeof(T));
95
96 if(result == -1)
97 {
98 ARM_COMPUTE_ERROR_VAR("Can't get PMU counter value: %d", errno);
99 }
100
101 return value;
102 }
103 } // namespace framework
104 } // namespace test
105 } // namespace arm_compute
106 #endif /* ARM_COMPUTE_TEST_PMU */
107