PerfHelper.h 3.1 KB

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