pmu_counter.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2017-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. #include "pmu_counter.h"
  25. #include <asm/unistd.h>
  26. #include <cstring>
  27. #include <stdexcept>
  28. #include <sys/ioctl.h>
  29. #include <linux/version.h>
  30. PmuCounter::PmuCounter() :
  31. _perf_config()
  32. {
  33. _perf_config.size = sizeof(perf_event_attr);
  34. // Start disabled
  35. _perf_config.disabled = 1;
  36. // The inherit bit specifies that this counter should count events of child
  37. // tasks as well as the task specified
  38. _perf_config.inherit = 1;
  39. // Enables saving of event counts on context switch for inherited tasks
  40. _perf_config.inherit_stat = 1;
  41. }
  42. PmuCounter::PmuCounter(PmuEventInfo config) :
  43. PmuCounter()
  44. {
  45. open(config);
  46. }
  47. PmuCounter::~PmuCounter()
  48. {
  49. close();
  50. }
  51. void PmuCounter::open(PmuEventInfo config)
  52. {
  53. _perf_config.config = config.event;
  54. _perf_config.type = config.type;
  55. open(_perf_config);
  56. }
  57. void PmuCounter::open(const perf_event_attr &perf_config)
  58. {
  59. // Measure this process/thread (+ children) on any CPU
  60. _fd = syscall(__NR_perf_event_open, &perf_config, 0, -1, -1, 0);
  61. if (_fd < 0)
  62. {
  63. throw std::runtime_error("perf_event_open failed. Counter ID: " + config_to_str(_perf_config));
  64. }
  65. const int result = ioctl(_fd, PERF_EVENT_IOC_ENABLE, 0);
  66. if (result == -1)
  67. {
  68. throw std::runtime_error("Failed to enable PMU counter: " + std::string(strerror(errno)));
  69. }
  70. }
  71. void PmuCounter::close()
  72. {
  73. if (_fd != -1)
  74. {
  75. ::close(_fd);
  76. _fd = -1;
  77. }
  78. }
  79. bool PmuCounter::reset()
  80. {
  81. const int result = ioctl(_fd, PERF_EVENT_IOC_RESET, 0);
  82. if (result == -1)
  83. {
  84. throw std::runtime_error("Failed to reset PMU counter: " + std::string(std::strerror(errno)));
  85. }
  86. return result != -1;
  87. }
  88. std::string PmuCounter::config_to_str(const perf_event_attr &perf_config)
  89. {
  90. switch (perf_config.type)
  91. {
  92. case PERF_TYPE_HARDWARE:
  93. switch (perf_config.config)
  94. {
  95. case PERF_COUNT_HW_CPU_CYCLES:
  96. return "PERF_COUNT_HW_CPU_CYCLES";
  97. case PERF_COUNT_HW_INSTRUCTIONS:
  98. return "PERF_COUNT_HW_INSTRUCTIONS";
  99. case PERF_COUNT_HW_CACHE_REFERENCES:
  100. return "PERF_COUNT_HW_CACHE_REFERENCES";
  101. case PERF_COUNT_HW_CACHE_MISSES:
  102. return "PERF_COUNT_HW_CACHE_MISSES";
  103. case PERF_COUNT_HW_BRANCH_INSTRUCTIONS:
  104. return "PERF_COUNT_HW_BRANCH_INSTRUCTIONS";
  105. case PERF_COUNT_HW_BRANCH_MISSES:
  106. return "PERF_COUNT_HW_BRANCH_MISSES";
  107. case PERF_COUNT_HW_BUS_CYCLES:
  108. return "PERF_COUNT_HW_BUS_CYCLES";
  109. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
  110. case PERF_COUNT_HW_STALLED_CYCLES_FRONTEND:
  111. return "PERF_COUNT_HW_STALLED_CYCLES_FRONTEND";
  112. case PERF_COUNT_HW_STALLED_CYCLES_BACKEND:
  113. return "PERF_COUNT_HW_STALLED_CYCLES_BACKEND";
  114. #endif
  115. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0)
  116. case PERF_COUNT_HW_REF_CPU_CYCLES:
  117. return "PERF_COUNT_HW_REF_CPU_CYCLES";
  118. #endif
  119. default:
  120. return "UNKNOWN HARDWARE COUNTER";
  121. }
  122. case PERF_TYPE_SOFTWARE:
  123. switch (perf_config.config)
  124. {
  125. case PERF_COUNT_SW_CPU_CLOCK:
  126. return "PERF_COUNT_SW_CPU_CLOCK";
  127. case PERF_COUNT_SW_TASK_CLOCK:
  128. return "PERF_COUNT_SW_TASK_CLOCK";
  129. case PERF_COUNT_SW_PAGE_FAULTS:
  130. return "PERF_COUNT_SW_PAGE_FAULTS";
  131. case PERF_COUNT_SW_CONTEXT_SWITCHES:
  132. return "PERF_COUNT_SW_CONTEXT_SWITCHES";
  133. case PERF_COUNT_SW_CPU_MIGRATIONS:
  134. return "PERF_COUNT_SW_CPU_MIGRATIONS";
  135. case PERF_COUNT_SW_PAGE_FAULTS_MIN:
  136. return "PERF_COUNT_SW_PAGE_FAULTS_MIN";
  137. case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
  138. return "PERF_COUNT_SW_PAGE_FAULTS_MAJ";
  139. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
  140. case PERF_COUNT_SW_ALIGNMENT_FAULTS:
  141. return "PERF_COUNT_SW_ALIGNMENT_FAULTS";
  142. case PERF_COUNT_SW_EMULATION_FAULTS:
  143. return "PERF_COUNT_SW_EMULATION_FAULTS";
  144. #endif
  145. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,12,0)
  146. case PERF_COUNT_SW_DUMMY:
  147. return "PERF_COUNT_SW_DUMMY";
  148. #endif
  149. default:
  150. return "UNKNOWN SOFTWARE COUNTER";
  151. }
  152. case PERF_TYPE_RAW:
  153. switch (static_cast<PmuImplDefined>(perf_config.config))
  154. {
  155. case PmuImplDefined::L1_ACCESSES:
  156. return "L1_ACCESSES";
  157. case PmuImplDefined::INSTR_RETIRED:
  158. return "INSTR_RETIRED";
  159. case PmuImplDefined::L2_ACCESSES:
  160. return "L2_ACCESSES";
  161. case PmuImplDefined::L3_ACCESSES:
  162. return "L3_ACCESSES";
  163. case PmuImplDefined::BUS_READS:
  164. return "BUS_READS";
  165. case PmuImplDefined::BUS_WRITES:
  166. return "BUS_WRITES";
  167. case PmuImplDefined::MEM_READS:
  168. return "MEM_READS";
  169. case PmuImplDefined::MEM_WRITES:
  170. return "MEM_WRITES";
  171. case PmuImplDefined::ASE_SPEC:
  172. return "ASE_SPEC";
  173. case PmuImplDefined::VFP_SPEC:
  174. return "VFP_SPEC";
  175. case PmuImplDefined::CRYPTO_SPEC:
  176. return "CRYPTO_SPEC";
  177. default:
  178. return "UNKNOWN RAW COUNTER";
  179. }
  180. default:
  181. return std::to_string(perf_config.config);
  182. }
  183. }