PerfHelper.h 3.1 KB

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