thread_instruction_count.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/android/thread_instruction_count.h"
  5. #include "base/check_op.h"
  6. #include "base/logging.h"
  7. #include "base/no_destructor.h"
  8. #include "base/numerics/safe_conversions.h"
  9. #include "base/threading/thread_local_storage.h"
  10. #include <linux/perf_event.h>
  11. #include <sys/syscall.h>
  12. #include <unistd.h>
  13. namespace base {
  14. namespace android {
  15. namespace {
  16. constexpr int kPerfFdOpenFailed = -1;
  17. ThreadLocalStorage::Slot& InstructionCounterFdSlot() {
  18. static NoDestructor<ThreadLocalStorage::Slot> fd_slot([](void* fd_ptr) {
  19. int fd = checked_cast<int>(reinterpret_cast<intptr_t>(fd_ptr));
  20. if (fd > 0)
  21. close(fd);
  22. });
  23. return *fd_slot;
  24. }
  25. // Opens a new file descriptor that emits the value of
  26. // PERF_COUNT_HW_INSTRUCTIONS in userspace (excluding kernel and hypervisor
  27. // instructions) for the given |thread_id|, or 0 for the calling thread.
  28. //
  29. // Returns kPerfFdOpenFailed if opening the file descriptor failed.
  30. int OpenInstructionCounterFdForThread(int thread_id) {
  31. struct perf_event_attr pe = {0};
  32. pe.type = PERF_TYPE_HARDWARE;
  33. pe.size = sizeof(struct perf_event_attr);
  34. pe.config = PERF_COUNT_HW_INSTRUCTIONS;
  35. pe.exclude_kernel = 1;
  36. pe.exclude_hv = 1;
  37. long fd = syscall(__NR_perf_event_open, &pe, thread_id, /* cpu */ -1,
  38. /* group_fd */ -1, /* flags */ 0);
  39. if (fd < 0) {
  40. PLOG(ERROR) << "perf_event_open: omitting instruction counters";
  41. return kPerfFdOpenFailed;
  42. }
  43. return checked_cast<int>(fd);
  44. }
  45. // Retrieves the active perf counter FD for the current thread, performing
  46. // lazy-initialization if necessary.
  47. int InstructionCounterFdForCurrentThread() {
  48. auto& slot = InstructionCounterFdSlot();
  49. int fd = checked_cast<int>(reinterpret_cast<intptr_t>(slot.Get()));
  50. if (fd == 0) {
  51. fd = OpenInstructionCounterFdForThread(0);
  52. slot.Set(reinterpret_cast<void*>(fd));
  53. }
  54. return fd;
  55. }
  56. } // namespace
  57. // static
  58. bool ThreadInstructionCount::IsSupported() {
  59. return InstructionCounterFdForCurrentThread() > 0;
  60. }
  61. // static
  62. ThreadInstructionCount ThreadInstructionCount::Now() {
  63. DCHECK(IsSupported());
  64. int fd = InstructionCounterFdForCurrentThread();
  65. if (fd <= 0)
  66. return ThreadInstructionCount();
  67. uint64_t instructions = 0;
  68. ssize_t bytes_read = read(fd, &instructions, sizeof(instructions));
  69. CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(instructions)))
  70. << "Short reads of small size from kernel memory is not expected. If "
  71. "this fails, use HANDLE_EINTR.";
  72. return ThreadInstructionCount(instructions);
  73. }
  74. } // namespace android
  75. } // namespace base