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 // Uses a valid PerfEvent to configure the Kernel so we can measure the
84 // event: the PerfEvent to measure.
85 explicit Counter(PerfEvent
&&event
, pid_t ProcessID
= 0);
87 Counter(const Counter
&) = delete;
88 Counter(Counter
&&other
) = default;
92 /// Starts the measurement of the event.
95 /// Stops the measurement of the event.
98 /// Returns the current value of the counter or -1 if it cannot be read.
101 /// Returns the current value of the counter or error if it cannot be read.
102 /// FunctionBytes: The benchmark function being executed.
103 /// This is used to filter out the measurements to ensure they are only
104 /// within the benchmarked code.
105 /// If empty (or not specified), then no filtering will be done.
106 /// Not all counters choose to use this.
107 virtual llvm::Expected
<llvm::SmallVector
<int64_t, 4>>
108 readOrError(StringRef FunctionBytes
= StringRef()) const;
110 virtual int numValues() const;
112 int getFileDescriptor() const { return FileDescriptor
; }
116 int FileDescriptor
= -1;
120 void initRealEvent(const PerfEvent
&E
, pid_t ProcessID
);
124 } // namespace exegesis
127 #endif // LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H