1//===-- PerfHelper.h ------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Helpers for measuring perf events.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H
15#define LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H
16
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Config/config.h"
20#include "llvm/Support/Error.h"
21
22#include <cstdint>
23#include <functional>
24#include <memory>
25
26#ifdef _MSC_VER
27typedef int pid_t;
28#else
29#include <sys/types.h>
30#endif // _MSC_VER
31
32struct perf_event_attr;
33
34namespace llvm {
35namespace exegesis {
36namespace pfm {
37
38// Returns true on error.
39bool pfmInitialize();
40void pfmTerminate();
41
42// Retrieves the encoding for the event described by pfm_event_string.
43// NOTE: pfm_initialize() must be called before creating PerfEvent objects.
44class PerfEvent {
45public:
46 // Dummy event that does not require access to counters (for tests).
47 static const char *const DummyEventString;
48
49 // http://perfmon2.sourceforge.net/manv4/libpfm.html
50 // Events are expressed as strings. e.g. "INSTRUCTION_RETIRED"
51 explicit PerfEvent(StringRef PfmEventString);
52
53 PerfEvent(const PerfEvent &) = delete;
54 PerfEvent(PerfEvent &&other);
55 ~PerfEvent();
56
57 // The pfm_event_string passed at construction time.
58 StringRef name() const;
59
60 // Whether the event was successfully created.
61 bool valid() const;
62
63 // The encoded event to be passed to the Kernel.
64 const perf_event_attr *attribute() const;
65
66 // The fully qualified name for the event.
67 // e.g. "snb_ep::INSTRUCTION_RETIRED:e=0:i=0:c=0:t=0:u=1:k=0:mg=0:mh=1"
68 StringRef getPfmEventString() const;
69
70protected:
71 PerfEvent() = default;
72 std::string EventString;
73 std::string FullQualifiedEventString;
74 perf_event_attr *Attr;
75
76private:
77 void initRealEvent(StringRef PfmEventString);
78};
79
80// Represents a single event that has been configured in the Linux perf
81// subsystem.
82class ConfiguredEvent {
83public:
84 ConfiguredEvent(PerfEvent &&EventToConfigure);
85
86 void initRealEvent(const pid_t ProcessID, const int GroupFD = -1);
87 Expected<SmallVector<int64_t>> readOrError(StringRef FunctionBytes) const;
88 int getFileDescriptor() const { return FileDescriptor; }
89 bool isDummyEvent() const {
90 return Event.name() == PerfEvent::DummyEventString;
91 }
92
93 ConfiguredEvent(const ConfiguredEvent &) = delete;
94 ConfiguredEvent(ConfiguredEvent &&other) = default;
95
96 ~ConfiguredEvent();
97
98private:
99 PerfEvent Event;
100 int FileDescriptor = -1;
101};
102
103// Consists of a counter measuring a specific event and associated validation
104// counters measuring execution conditions. All counters in a group are part
105// of a single event group and are thus scheduled on and off the CPU as a single
106// unit.
107class CounterGroup {
108public:
109 // event: the PerfEvent to measure.
110 explicit CounterGroup(PerfEvent &&event, std::vector<PerfEvent> &&ValEvents,
111 pid_t ProcessID = 0);
112
113 CounterGroup(const CounterGroup &) = delete;
114 CounterGroup(CounterGroup &&other) = default;
115
116 virtual ~CounterGroup() = default;
117
118 /// Starts the measurement of the event.
119 virtual void start();
120
121 /// Stops the measurement of the event.
122 void stop();
123
124 /// Returns the current value of the counter or error if it cannot be read.
125 /// FunctionBytes: The benchmark function being executed.
126 /// This is used to filter out the measurements to ensure they are only
127 /// within the benchmarked code.
128 /// If empty (or not specified), then no filtering will be done.
129 /// Not all counters choose to use this.
130 virtual Expected<SmallVector<int64_t, 4>>
131 readOrError(StringRef FunctionBytes = StringRef()) const;
132
133 virtual Expected<SmallVector<int64_t>> readValidationCountersOrError() const;
134
135 virtual int numValues() const;
136
137 int getFileDescriptor() const { return EventCounter.getFileDescriptor(); }
138
139protected:
140 ConfiguredEvent EventCounter;
141 bool IsDummyEvent;
142 std::vector<ConfiguredEvent> ValidationEventCounters;
143
144private:
145 void initRealEvent(pid_t ProcessID);
146};
147
148} // namespace pfm
149} // namespace exegesis
150} // namespace llvm
151
152#endif // LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H
153