| 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 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#ifndef LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H |
| 15 |
#define LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H |
| 16 |
|
| 17 |
#include "llvm/ADT/SmallVector.h" |
| 18 |
#include "llvm/ADT/StringRef.h" |
| 19 |
#include "llvm/Config/config.h" |
| 20 |
#include "llvm/Support/Error.h" |
| 21 |
|
| 22 |
#include <cstdint> |
| 23 |
#include <functional> |
| 24 |
#include <memory> |
| 25 |
|
| 26 |
struct perf_event_attr; |
| 27 |
|
| 28 |
namespace llvm { |
| 29 |
namespace exegesis { |
| 30 |
namespace pfm { |
| 31 |
|
| 32 |
// Returns true on error. |
| 33 |
bool pfmInitialize(); |
| 34 |
void pfmTerminate(); |
| 35 |
|
| 36 |
// Retrieves the encoding for the event described by pfm_event_string. |
| 37 |
// NOTE: pfm_initialize() must be called before creating PerfEvent objects. |
| 38 |
class PerfEvent { |
| 39 |
public: |
| 40 |
// http://perfmon2.sourceforge.net/manv4/libpfm.html |
| 41 |
// Events are expressed as strings. e.g. "INSTRUCTION_RETIRED" |
| 42 |
explicit PerfEvent(StringRef PfmEventString); |
| 43 |
|
| 44 |
PerfEvent(const PerfEvent &) = delete; |
| 45 |
PerfEvent(PerfEvent &&other); |
| 46 |
~PerfEvent(); |
| 47 |
|
| 48 |
// The pfm_event_string passed at construction time. |
| 49 |
StringRef name() const; |
| 50 |
|
| 51 |
// Whether the event was successfully created. |
| 52 |
bool valid() const; |
| 53 |
|
| 54 |
// The encoded event to be passed to the Kernel. |
| 55 |
const perf_event_attr *attribute() const; |
| 56 |
|
| 57 |
// The fully qualified name for the event. |
| 58 |
// e.g. "snb_ep::INSTRUCTION_RETIRED:e=0:i=0:c=0:t=0:u=1:k=0:mg=0:mh=1" |
| 59 |
StringRef getPfmEventString() const; |
| 60 |
|
| 61 |
protected: |
| 62 |
PerfEvent() = default; |
| 63 |
std::string EventString; |
| 64 |
std::string FullQualifiedEventString; |
| 65 |
perf_event_attr *Attr; |
| 66 |
}; |
| 67 |
|
| 68 |
// Uses a valid PerfEvent to configure the Kernel so we can measure the |
| 69 |
// underlying event. |
| 70 |
class Counter { |
| 71 |
public: |
| 72 |
// event: the PerfEvent to measure. |
| 73 |
explicit Counter(PerfEvent &&event); |
| 74 |
|
| 75 |
Counter(const Counter &) = delete; |
| 76 |
Counter(Counter &&other) = default; |
| 77 |
|
| 78 |
virtual ~Counter(); |
| 79 |
|
| 80 |
/// Starts the measurement of the event. |
| 81 |
virtual void start(); |
| 82 |
|
| 83 |
/// Stops the measurement of the event. |
| 84 |
void stop(); |
| 85 |
|
| 86 |
/// Returns the current value of the counter or -1 if it cannot be read. |
| 87 |
int64_t read() const; |
| 88 |
|
| 89 |
/// Returns the current value of the counter or error if it cannot be read. |
| 90 |
/// FunctionBytes: The benchmark function being executed. |
| 91 |
/// This is used to filter out the measurements to ensure they are only |
| 92 |
/// within the benchmarked code. |
| 93 |
/// If empty (or not specified), then no filtering will be done. |
| 94 |
/// Not all counters choose to use this. |
| 95 |
virtual llvm::Expected<llvm::SmallVector<int64_t, 4>> |
| 96 |
readOrError(StringRef FunctionBytes = StringRef()) const; |
| 97 |
|
| 98 |
virtual int numValues() const; |
| 99 |
|
| 100 |
protected: |
| 101 |
PerfEvent Event; |
| 102 |
#ifdef HAVE_LIBPFM |
| 103 |
int FileDescriptor = -1; |
| 104 |
#endif |
| 105 |
}; |
| 106 |
|
| 107 |
} // namespace pfm |
| 108 |
} // namespace exegesis |
| 109 |
} // namespace llvm |
| 110 |
|
| 111 |
#endif // LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H |