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/StringRef.h"
19 #include "llvm/Config/config.h"
23 struct perf_event_attr
;
29 // Returns true on error.
33 // Retrieves the encoding for the event described by pfm_event_string.
34 // NOTE: pfm_initialize() must be called before creating PerfEvent objects.
37 // http://perfmon2.sourceforge.net/manv4/libpfm.html
38 // Events are expressed as strings. e.g. "INSTRUCTION_RETIRED"
39 explicit PerfEvent(llvm::StringRef pfm_event_string
);
41 PerfEvent(const PerfEvent
&) = delete;
42 PerfEvent(PerfEvent
&&other
);
45 // The pfm_event_string passed at construction time.
46 llvm::StringRef
name() const;
48 // Whether the event was successfully created.
51 // The encoded event to be passed to the Kernel.
52 const perf_event_attr
*attribute() const;
54 // The fully qualified name for the event.
55 // e.g. "snb_ep::INSTRUCTION_RETIRED:e=0:i=0:c=0:t=0:u=1:k=0:mg=0:mh=1"
56 llvm::StringRef
getPfmEventString() const;
59 const std::string EventString
;
60 std::string FullQualifiedEventString
;
61 perf_event_attr
*Attr
;
64 // Uses a valid PerfEvent to configure the Kernel so we can measure the
67 // event: the PerfEvent to measure.
68 explicit Counter(const PerfEvent
&event
);
70 Counter(const Counter
&) = delete;
71 Counter(Counter
&&other
) = default;
75 void start(); // Starts the measurement of the event.
76 void stop(); // Stops the measurement of the event.
77 int64_t read() const; // Return the current value of the counter.
81 int FileDescriptor
= -1;
85 // Helper to measure a list of PerfEvent for a particular function.
86 // callback is called for each successful measure (PerfEvent needs to be valid).
87 template <typename Function
>
89 llvm::ArrayRef
<PerfEvent
> Events
,
90 const std::function
<void(const PerfEvent
&Event
, int64_t Value
)> &Callback
,
92 for (const auto &Event
: Events
) {
99 Callback(Event
, Cnt
.read());
104 } // namespace exegesis
107 #endif // LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H