Compute Library
 21.08
PMU Class Reference

Class provides access to CPU hardware counters. More...

#include <PMU.h>

Public Member Functions

 PMU ()
 Default constructor. More...
 
 PMU (uint64_t config)
 Create PMU with specified counter. More...
 
 ~PMU ()
 Default destructor. More...
 
template<typename T >
get_value () const
 Get the counter value. More...
 
void open (uint64_t config)
 Open the specified counter based on the default configuration. More...
 
void open (const perf_event_attr &perf_config)
 Open the specified configuration. More...
 
void close ()
 Close the currently open counter. More...
 
void reset ()
 Reset counter. More...
 

Detailed Description

Class provides access to CPU hardware counters.

Definition at line 43 of file PMU.h.

Constructor & Destructor Documentation

◆ PMU() [1/2]

PMU ( )

Default constructor.

Definition at line 37 of file PMU.cpp.

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 }

◆ PMU() [2/2]

PMU ( uint64_t  config)
explicit

Create PMU with specified counter.

This constructor automatically calls open with the default configuration.

Parameters
[in]configCounter identifier.

Definition at line 52 of file PMU.cpp.

References PMU::open().

53  : PMU()
54 {
55  open(config);
56 }
void open(uint64_t config)
Open the specified counter based on the default configuration.
Definition: PMU.cpp:63
PMU()
Default constructor.
Definition: PMU.cpp:37

◆ ~PMU()

~PMU ( )

Default destructor.

Definition at line 58 of file PMU.cpp.

References PMU::close().

59 {
60  close();
61 }
void close()
Close the currently open counter.
Definition: PMU.cpp:83

Member Function Documentation

◆ close()

void close ( )

Close the currently open counter.

Definition at line 83 of file PMU.cpp.

Referenced by PMU::~PMU().

84 {
85  if(_fd != -1)
86  {
87  ::close(_fd);
88  _fd = -1;
89  }
90 }
void close()
Close the currently open counter.
Definition: PMU.cpp:83

◆ get_value()

T get_value ( ) const

Get the counter value.

Returns
Counter value casted to the specified type.

Definition at line 91 of file PMU.h.

References ARM_COMPUTE_ERROR_VAR.

Referenced by PMUCounter::stop().

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 }
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346

◆ open() [1/2]

void open ( uint64_t  config)

Open the specified counter based on the default configuration.

Parameters
[in]configThe default configuration.

Definition at line 63 of file PMU.cpp.

Referenced by PMU::PMU().

64 {
65  _perf_config.config = config;
66  open(_perf_config);
67 }
void open(uint64_t config)
Open the specified counter based on the default configuration.
Definition: PMU.cpp:63

◆ open() [2/2]

void open ( const perf_event_attr &  perf_config)

Open the specified configuration.

Parameters
[in]perf_configThe specified configuration.

Definition at line 69 of file PMU.cpp.

References ARM_COMPUTE_ERROR_ON_MSG, and ARM_COMPUTE_ERROR_VAR.

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 }
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456

◆ reset()

void reset ( )

Reset counter.

Definition at line 92 of file PMU.cpp.

References ARM_COMPUTE_ERROR_VAR.

Referenced by PMUCounter::start().

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 }
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346

The documentation for this class was generated from the following files: