blob: bb2c70e2151814f0b5b992fc5de09e225e4c080a [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#include "PMU.h"
25
26#include <asm/unistd.h>
27#include <cstring>
28#include <stdexcept>
29#include <sys/ioctl.h>
30
31namespace arm_compute
32{
33namespace test
34{
35namespace framework
36{
37PMU::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
52PMU::PMU(uint64_t config)
53 : PMU()
54{
55 open(config);
56}
57
58PMU::~PMU()
59{
60 close();
61}
62
63void PMU::open(uint64_t config)
64{
65 _perf_config.config = config;
66 open(_perf_config);
67}
68
69void 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 Barbier144d2ff2017-09-29 10:46:08 +010077 if(result == -1)
78 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +010079 ARM_COMPUTE_ERROR_VAR("Failed to enable PMU counter: %d", errno);
Anthony Barbier144d2ff2017-09-29 10:46:08 +010080 }
Moritz Pflanzer09e4f982017-08-30 12:47:06 +010081}
82
83void PMU::close()
84{
85 if(_fd != -1)
86 {
87 ::close(_fd);
88 _fd = -1;
89 }
90}
91
92void PMU::reset()
93{
94 const int result = ioctl(_fd, PERF_EVENT_IOC_RESET, 0);
Anthony Barbier144d2ff2017-09-29 10:46:08 +010095 if(result == -1)
96 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +010097 ARM_COMPUTE_ERROR_VAR("Failed to reset PMU counter: %d", errno);
Anthony Barbier144d2ff2017-09-29 10:46:08 +010098 }
Moritz Pflanzer09e4f982017-08-30 12:47:06 +010099}
100} // namespace framework
101} // namespace test
102} // namespace arm_compute