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/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/Support/Error.h"
27 struct perf_event_attr
;
33 // Returns true on error.
37 // Retrieves the encoding for the event described by pfm_event_string.
38 // NOTE: pfm_initialize() must be called before creating PerfEvent objects.
41 // http://perfmon2.sourceforge.net/manv4/libpfm.html
42 // Events are expressed as strings. e.g. "INSTRUCTION_RETIRED"
43 explicit PerfEvent(StringRef PfmEventString
);
45 PerfEvent(const PerfEvent
&) = delete;
46 PerfEvent(PerfEvent
&&other
);
49 // The pfm_event_string passed at construction time.
50 StringRef
name() const;
52 // Whether the event was successfully created.
55 // The encoded event to be passed to the Kernel.
56 const perf_event_attr
*attribute() const;
58 // The fully qualified name for the event.
59 // e.g. "snb_ep::INSTRUCTION_RETIRED:e=0:i=0:c=0:t=0:u=1:k=0:mg=0:mh=1"
60 StringRef
getPfmEventString() const;
63 PerfEvent() = default;
64 std::string EventString
;
65 std::string FullQualifiedEventString
;
66 perf_event_attr
*Attr
;
69 // Uses a valid PerfEvent to configure the Kernel so we can measure the
73 // event: the PerfEvent to measure.
74 explicit Counter(PerfEvent
&&event
);
76 Counter(const Counter
&) = delete;
77 Counter(Counter
&&other
) = default;
81 /// Starts the measurement of the event.
84 /// Stops the measurement of the event.
87 /// Returns the current value of the counter or -1 if it cannot be read.
90 /// Returns the current value of the counter or error if it cannot be read.
91 /// FunctionBytes: The benchmark function being executed.
92 /// This is used to filter out the measurements to ensure they are only
93 /// within the benchmarked code.
94 /// If empty (or not specified), then no filtering will be done.
95 /// Not all counters choose to use this.
96 virtual llvm::Expected
<llvm::SmallVector
<int64_t, 4>>
97 readOrError(StringRef FunctionBytes
= StringRef()) const;
99 virtual int numValues() const;
104 int FileDescriptor
= -1;
109 } // namespace exegesis
112 #endif // LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H