pRC
multi-purpose Tensor Train library for C++
Loading...
Searching...
No Matches
perfevent.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_PROFILING_PERFEVENT_H
4#define pRC_CORE_PROFILING_PERFEVENT_H
5
6#include <cstdint>
7#include <cstring>
8
9extern "C"
10{
11#include <unistd.h>
12
13#include <linux/perf_event.h>
14#include <sys/ioctl.h>
15#include <sys/syscall.h>
16}
17
18#include <prc/core/log/log.hpp>
20
22{
23 enum class Events
24 {
25 Cycles,
29 };
30
31 template<Events Event>
33 {
34 private:
35 static constexpr auto type()
36 {
37 if constexpr(Event == Events::Cycles ||
41 {
42 return PERF_TYPE_HARDWARE;
43 }
44 }
45
46 static constexpr auto config()
47 {
48 if constexpr(Event == Events::Cycles)
49 {
51 }
52 else if constexpr(Event == Events::Instructions)
53 {
55 }
56 else if constexpr(Event == Events::CacheReferences)
57 {
59 }
60 else if constexpr(Event == Events::CacheMisses)
61 {
63 }
64 }
65
66 public:
68 {
69 struct perf_event_attr pea;
70 std::memset(&pea, 0, sizeof(struct perf_event_attr));
71 pea.type = type();
72 pea.size = sizeof(struct perf_event_attr);
73 pea.config = config();
74 pea.disabled = 1;
75 pea.exclude_kernel = 1;
76 pea.exclude_hv = 1;
77 pea.inherit = 1;
78
79 mFD = syscall(__NR_perf_event_open, &pea, 0, -1, -1, 0);
80 if(mFD == -1)
81 {
82 Logging::warning("Unable to open perf event descriptor.");
83 }
84 }
86 : mFD(other.mFD)
87 , mValue(other.mValue)
88 {
89 other.mFD = -1;
90 }
91
93 {
94 mFD = rhs.mFD;
95 mValue = rhs.mValue;
96 rhs.mFD = -1;
97 }
98
100 {
101 close(mFD);
102 }
103
104 PerfEvent(PerfEvent const &) = delete;
105 PerfEvent &operator=(PerfEvent const &) = delete;
106
107 auto start()
108 {
109 if(mFD != -1)
110 {
113 }
114 return;
115 }
116
117 auto stop()
118 {
119 if(mFD != -1)
120 {
122 std::uint64_t value;
123 if(read(mFD, &value, sizeof(std::uint64_t)) !=
124 sizeof(std::uint64_t))
125 {
126 Logging::error("Unable to read perf event counter.");
127 }
128 mValue += value;
129 }
130 return;
131 }
132
133 auto reset()
134 {
135 if(mFD != -1)
136 {
138 mValue = 0;
139 }
140 }
141
142 auto value() const
143 {
144 return Integer(mValue);
145 }
146
147 private:
148 int mFD = -1;
149 std::uint64_t mValue = 0;
150 };
151}
152#endif // pRC_CORE_PROFILING_PERFEVENT_H
Definition perfevent.hpp:33
~PerfEvent()
Definition perfevent.hpp:99
PerfEvent()
Definition perfevent.hpp:67
PerfEvent(PerfEvent const &)=delete
auto start()
Definition perfevent.hpp:107
auto reset()
Definition perfevent.hpp:133
PerfEvent & operator=(PerfEvent const &)=delete
auto stop()
Definition perfevent.hpp:117
auto value() const
Definition perfevent.hpp:142
PerfEvent & operator=(PerfEvent &&rhs) &
Definition perfevent.hpp:92
PerfEvent(PerfEvent &&other)
Definition perfevent.hpp:85
Top-level class storing a floating point number.
Definition integer.hpp:47
Definition perfevent.hpp:22
Events
Definition perfevent.hpp:24
static void warning(Xs &&...args)
Definition log.hpp:21
static void error(Xs &&...args)
Definition log.hpp:14
static constexpr Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13