cpu_profiler.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2019 ARM Limited.
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #pragma once
  25. #include "value.h"
  26. #include <string>
  27. #include <unordered_map>
  28. #include <unordered_set>
  29. namespace hwcpipe
  30. {
  31. // The available CPU counters. Profiler implementations will support a subset of them.
  32. enum class CpuCounter
  33. {
  34. Cycles,
  35. Instructions,
  36. CacheReferences,
  37. CacheMisses,
  38. BranchInstructions,
  39. BranchMisses,
  40. L1Accesses,
  41. InstrRetired,
  42. L2Accesses,
  43. L3Accesses,
  44. BusReads,
  45. BusWrites,
  46. MemReads,
  47. MemWrites,
  48. ASESpec,
  49. VFPSpec,
  50. CryptoSpec,
  51. MaxValue
  52. };
  53. // Mapping from CPU counter names to enum values. Used for JSON initialization.
  54. const std::unordered_map<std::string, CpuCounter> cpu_counter_names{
  55. {"Cycles", CpuCounter::Cycles},
  56. {"Instructions", CpuCounter::Instructions},
  57. {"CacheReferences", CpuCounter::CacheReferences},
  58. {"CacheMisses", CpuCounter::CacheMisses},
  59. {"BranchInstructions", CpuCounter::BranchInstructions},
  60. {"BranchMisses", CpuCounter::BranchMisses},
  61. {"L1Accesses", CpuCounter::L1Accesses},
  62. {"InstrRetired", CpuCounter::InstrRetired},
  63. {"L2Accesses", CpuCounter::L2Accesses},
  64. {"L3Accesses", CpuCounter::L3Accesses},
  65. {"BusReads", CpuCounter::BusReads},
  66. {"BusWrites", CpuCounter::BusWrites},
  67. {"MemReads", CpuCounter::MemReads},
  68. {"MemWrites", CpuCounter::MemWrites},
  69. {"ASESpec", CpuCounter::ASESpec},
  70. {"VFPSpec", CpuCounter::VFPSpec},
  71. {"CryptoSpec", CpuCounter::CryptoSpec},
  72. };
  73. // A hash function for CpuCounter values
  74. struct CpuCounterHash
  75. {
  76. template <typename T>
  77. std::size_t operator()(T t) const
  78. {
  79. return static_cast<std::size_t>(t);
  80. }
  81. };
  82. struct CpuCounterInfo
  83. {
  84. std::string desc;
  85. std::string unit;
  86. };
  87. // Mapping from each counter to its corresponding information (description and unit)
  88. const std::unordered_map<CpuCounter, CpuCounterInfo, CpuCounterHash> cpu_counter_info{
  89. {CpuCounter::Cycles, {"Number of CPU cycles", "cycles"}},
  90. {CpuCounter::Instructions, {"Number of CPU instructions", "instructions"}},
  91. {CpuCounter::CacheReferences, {"Number of cache references", "references"}},
  92. {CpuCounter::CacheMisses, {"Number of cache misses", "misses"}},
  93. {CpuCounter::BranchInstructions, {"Number of branch instructions", "instructions"}},
  94. {CpuCounter::BranchMisses, {"Number of branch misses", "misses"}},
  95. {CpuCounter::L1Accesses, {"L1 data cache accesses", "accesses"}},
  96. {CpuCounter::InstrRetired, {"All retired instructions", "instructions"}},
  97. {CpuCounter::L2Accesses, {"L2 data cache accesses", "accesses"}},
  98. {CpuCounter::L3Accesses, {"L3 data cache accesses", "accesses"}},
  99. {CpuCounter::BusReads, {"Bus access reads", "beats"}},
  100. {CpuCounter::BusWrites, {"Bus access writes", "beats"}},
  101. {CpuCounter::MemReads, {"Data memory access, load instructions", "instructions"}},
  102. {CpuCounter::MemWrites, {"Data memory access, store instructions", "instructions"}},
  103. {CpuCounter::ASESpec, {"Speculatively executed SIMD operations", "operations"}},
  104. {CpuCounter::VFPSpec, {"Speculatively executed floating point operations", "operations"}},
  105. {CpuCounter::CryptoSpec, {"Speculatively executed cryptographic operations", "operations"}},
  106. };
  107. typedef std::unordered_set<CpuCounter, CpuCounterHash> CpuCounterSet;
  108. typedef std::unordered_map<CpuCounter, Value, CpuCounterHash>
  109. CpuMeasurements;
  110. /** An interface for classes that collect CPU performance data. */
  111. class CpuProfiler
  112. {
  113. public:
  114. virtual ~CpuProfiler() = default;
  115. // Returns the enabled counters
  116. virtual const CpuCounterSet &enabled_counters() const = 0;
  117. // Returns the counters that the platform supports
  118. virtual const CpuCounterSet &supported_counters() const = 0;
  119. // Sets the enabled counters after initialization
  120. virtual void set_enabled_counters(CpuCounterSet counters) = 0;
  121. // Starts a profiling session
  122. virtual void run() = 0;
  123. // Sample the counters. Returns a map of measurements for the counters
  124. // that are both available and enabled.
  125. // A profiling session must be running when sampling the counters.
  126. virtual const CpuMeasurements &sample() = 0;
  127. // Stops the active profiling session
  128. virtual void stop() = 0;
  129. };
  130. } // namespace hwcpipe