Compute Library
 21.02
PMU.cpp
Go to the documentation of this file.
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 #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 {
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 
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);
77  if(result == -1)
78  {
79  ARM_COMPUTE_ERROR_VAR("Failed to enable PMU counter: %d", errno);
80  }
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);
95  if(result == -1)
96  {
97  ARM_COMPUTE_ERROR_VAR("Failed to reset PMU counter: %d", errno);
98  }
99 }
100 } // namespace framework
101 } // namespace test
102 } // namespace arm_compute
void close()
Close the currently open counter.
Definition: PMU.cpp:83
Class provides access to CPU hardware counters.
Definition: PMU.h:43
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
void open(uint64_t config)
Open the specified counter based on the default configuration.
Definition: PMU.cpp:63
Copyright (c) 2017-2021 Arm Limited.
~PMU()
Default destructor.
Definition: PMU.cpp:58
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void reset()
Reset counter.
Definition: PMU.cpp:92
PMU()
Default constructor.
Definition: PMU.cpp:37