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