Home | History | Annotate | Line # | Download | only in lib
      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/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"
     22 
     23 #include <cstdint>
     24 #include <functional>
     25 #include <memory>
     26 
     27 struct perf_event_attr;
     28 
     29 namespace llvm {
     30 namespace exegesis {
     31 namespace pfm {
     32 
     33 // Returns true on error.
     34 bool pfmInitialize();
     35 void pfmTerminate();
     36 
     37 // Retrieves the encoding for the event described by pfm_event_string.
     38 // NOTE: pfm_initialize() must be called before creating PerfEvent objects.
     39 class PerfEvent {
     40 public:
     41   // http://perfmon2.sourceforge.net/manv4/libpfm.html
     42   // Events are expressed as strings. e.g. "INSTRUCTION_RETIRED"
     43   explicit PerfEvent(StringRef PfmEventString);
     44 
     45   PerfEvent(const PerfEvent &) = delete;
     46   PerfEvent(PerfEvent &&other);
     47   ~PerfEvent();
     48 
     49   // The pfm_event_string passed at construction time.
     50   StringRef name() const;
     51 
     52   // Whether the event was successfully created.
     53   bool valid() const;
     54 
     55   // The encoded event to be passed to the Kernel.
     56   const perf_event_attr *attribute() const;
     57 
     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;
     61 
     62 protected:
     63   PerfEvent() = default;
     64   std::string EventString;
     65   std::string FullQualifiedEventString;
     66   perf_event_attr *Attr;
     67 };
     68 
     69 // Uses a valid PerfEvent to configure the Kernel so we can measure the
     70 // underlying event.
     71 class Counter {
     72 public:
     73   // event: the PerfEvent to measure.
     74   explicit Counter(PerfEvent &&event);
     75 
     76   Counter(const Counter &) = delete;
     77   Counter(Counter &&other) = default;
     78 
     79   virtual ~Counter();
     80 
     81   /// Starts the measurement of the event.
     82   virtual void start();
     83 
     84   /// Stops the measurement of the event.
     85   void stop();
     86 
     87   /// Returns the current value of the counter or -1 if it cannot be read.
     88   int64_t read() const;
     89 
     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;
     98 
     99   virtual int numValues() const;
    100 
    101 protected:
    102   PerfEvent Event;
    103 #ifdef HAVE_LIBPFM
    104   int FileDescriptor = -1;
    105 #endif
    106 };
    107 
    108 } // namespace pfm
    109 } // namespace exegesis
    110 } // namespace llvm
    111 
    112 #endif // LLVM_TOOLS_LLVM_EXEGESIS_PERFHELPER_H
    113