-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperf_event.hpp
More file actions
89 lines (70 loc) · 1.92 KB
/
perf_event.hpp
File metadata and controls
89 lines (70 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
#include <linux/perf_event.h>
#include <sys/types.h>
#include <cstdint>
#include <optional>
#include <system_error>
namespace cpuscope
{
struct PerfEventOpenResult;
class PerfEvent
{
public:
enum class Scope
{
CPU,
Process
};
enum class Event
{
CPU_CYCLES,
HW_Instructions,
HW_Cache_Misses
};
struct Config
{
pid_t pid = 0;
int cpu = -1;
Scope scope = Scope::CPU;
Event event = Event::CPU_CYCLES;
};
struct ISysCalls
{
virtual ~ISysCalls() = default;
virtual int perf_event_open(const perf_event_attr* attr, pid_t pid, int cpu, int group_fd, unsigned long flags) = 0;
virtual int close(int file_descriptor) = 0;
virtual ssize_t read(int file_descriptor, void* buf, size_t count) = 0;
};
struct LinuxSysCalls final : ISysCalls
{
int perf_event_open(const perf_event_attr* attr, pid_t pid, int cpu, int group_fd, unsigned long flags) noexcept override;
int close(int file_descriptor) noexcept override;
ssize_t read(int file_descriptor, void* buf, size_t count) noexcept override;
};
~PerfEvent() noexcept;
PerfEvent(const PerfEvent&) = delete;
PerfEvent& operator=(const PerfEvent&) = delete;
PerfEvent(PerfEvent&& other) noexcept;
PerfEvent& operator=(PerfEvent&& other) noexcept;
static PerfEventOpenResult open(const Config& config, ISysCalls& sysCalls) noexcept;
std::optional<uint64_t> read_counter() noexcept;
private:
PerfEvent(const int file_descriptor, ISysCalls& sysCalls) noexcept;
int m_file_descriptor = -1;
ISysCalls& m_sysCalls;
};
struct PerfEventError
{
int code;
std::string message;
};
struct PerfEventOpenResult
{
std::optional<PerfEvent> event;
PerfEventError error;
bool has_error() const noexcept
{
return error.code != 0;
}
};
} // namespace cpuscope