blob: c392a0ac66087b7a1ff7b8d40f2004612cc79ef3 [file] [log] [blame]
Moritz Pflanzer09e4f982017-08-30 12:47:06 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 Arm Limited.
Moritz Pflanzer09e4f982017-08-30 12:47:06 +01003 *
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 Barbier503cec82018-01-25 15:49:23 +000030#include <errno.h>
Moritz Pflanzer09e4f982017-08-30 12:47:06 +010031#include <linux/perf_event.h>
32#include <stdexcept>
Moritz Pflanzer29088d52017-09-21 15:06:59 +010033#include <sys/syscall.h>
Moritz Pflanzer09e4f982017-08-30 12:47:06 +010034#include <unistd.h>
35
36namespace arm_compute
37{
38namespace test
39{
40namespace framework
41{
42/** Class provides access to CPU hardware counters. */
43class PMU
44{
45public:
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 Gildayc357c472018-03-21 13:54:09 +000067 /** Open the specified counter based on the default configuration.
68 *
69 * @param[in] config The default configuration.
70 */
Moritz Pflanzer09e4f982017-08-30 12:47:06 +010071 void open(uint64_t config);
72
Alex Gildayc357c472018-03-21 13:54:09 +000073 /** Open the specified configuration.
74 *
75 * @param[in] perf_config The specified configuration.
76 */
Moritz Pflanzer09e4f982017-08-30 12:47:06 +010077 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
85private:
86 perf_event_attr _perf_config;
87 long _fd{ -1 };
88};
89
90template <typename T>
91T PMU::get_value() const
92{
93 T value{};
94 const ssize_t result = read(_fd, &value, sizeof(T));
95
Anthony Barbier144d2ff2017-09-29 10:46:08 +010096 if(result == -1)
97 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +010098 ARM_COMPUTE_ERROR_VAR("Can't get PMU counter value: %d", errno);
Anthony Barbier144d2ff2017-09-29 10:46:08 +010099 }
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100100
101 return value;
102}
103} // namespace framework
104} // namespace test
105} // namespace arm_compute
106#endif /* ARM_COMPUTE_TEST_PMU */