[RISCV] Eliminate dead li after emitting VSETVLIs (#65934)
[llvm-project.git] / llvm / tools / llvm-exegesis / lib / PerfHelper.h
bloba50974f0a67be9f6bfab9f43ae8976b62df300fa
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 //===----------------------------------------------------------------------===//
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"
22 #include <cstdint>
23 #include <functional>
24 #include <memory>
26 #ifdef _MSC_VER
27 typedef int pid_t;
28 #else
29 #include <sys/types.h>
30 #endif // _MSC_VER
32 struct perf_event_attr;
34 namespace llvm {
35 namespace exegesis {
36 namespace pfm {
38 // Returns true on error.
39 bool pfmInitialize();
40 void pfmTerminate();
42 // Retrieves the encoding for the event described by pfm_event_string.
43 // NOTE: pfm_initialize() must be called before creating PerfEvent objects.
44 class PerfEvent {
45 public:
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);
55 ~PerfEvent();
57 // The pfm_event_string passed at construction time.
58 StringRef name() const;
60 // Whether the event was successfully created.
61 bool valid() const;
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;
70 protected:
71 PerfEvent() = default;
72 std::string EventString;
73 std::string FullQualifiedEventString;
74 perf_event_attr *Attr;
76 private:
77 void initRealEvent(StringRef PfmEventString);
80 // Uses a valid PerfEvent to configure the Kernel so we can measure the
81 // underlying event.
82 class Counter {
83 public:
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;
90 virtual ~Counter();
92 /// Starts the measurement of the event.
93 virtual void start();
95 /// Stops the measurement of the event.
96 void stop();
98 /// Returns the current value of the counter or -1 if it cannot be read.
99 int64_t read() const;
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; }
114 protected:
115 PerfEvent Event;
116 int FileDescriptor = -1;
117 bool IsDummyEvent;
119 private:
120 void initRealEvent(const PerfEvent &E, pid_t ProcessID);
123 } // namespace pfm
124 } // namespace exegesis
125 } // namespace llvm
127 #endif // LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H