1 //===-- PerfHelper.h ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Helpers for measuring perf events.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H
15 #define LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/Support/Error.h"
29 #include <sys/types.h>
32 struct perf_event_attr
;
38 // Returns true on error.
42 // Retrieves the encoding for the event described by pfm_event_string.
43 // NOTE: pfm_initialize() must be called before creating PerfEvent objects.
46 // Dummy event that does not require access to counters (for tests).
47 static const char *const DummyEventString
;
49 // http://perfmon2.sourceforge.net/manv4/libpfm.html
50 // Events are expressed as strings. e.g. "INSTRUCTION_RETIRED"
51 explicit PerfEvent(StringRef PfmEventString
);
53 PerfEvent(const PerfEvent
&) = delete;
54 PerfEvent(PerfEvent
&&other
);
57 // The pfm_event_string passed at construction time.
58 StringRef
name() const;
60 // Whether the event was successfully created.
63 // The encoded event to be passed to the Kernel.
64 const perf_event_attr
*attribute() const;
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;
71 PerfEvent() = default;
72 std::string EventString
;
73 std::string FullQualifiedEventString
;
74 perf_event_attr
*Attr
;
77 void initRealEvent(StringRef PfmEventString
);
80 // Represents a single event that has been configured in the Linux perf
82 class ConfiguredEvent
{
84 ConfiguredEvent(PerfEvent
&&EventToConfigure
);
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
;
93 ConfiguredEvent(const ConfiguredEvent
&) = delete;
94 ConfiguredEvent(ConfiguredEvent
&&other
) = default;
100 int FileDescriptor
= -1;
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
109 // event: the PerfEvent to measure.
110 explicit CounterGroup(PerfEvent
&&event
, std::vector
<PerfEvent
> &&ValEvents
,
111 pid_t ProcessID
= 0);
113 CounterGroup(const CounterGroup
&) = delete;
114 CounterGroup(CounterGroup
&&other
) = default;
116 virtual ~CounterGroup() = default;
118 /// Starts the measurement of the event.
119 virtual void start();
121 /// Stops the measurement of the event.
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;
133 virtual Expected
<SmallVector
<int64_t>> readValidationCountersOrError() const;
135 virtual int numValues() const;
137 int getFileDescriptor() const { return EventCounter
.getFileDescriptor(); }
140 ConfiguredEvent EventCounter
;
142 std::vector
<ConfiguredEvent
> ValidationEventCounters
;
145 void initRealEvent(pid_t ProcessID
);
149 } // namespace exegesis
152 #endif // LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H