PerformanceManager.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <Kernel/PerformanceEventBuffer.h>
  8. #include <Kernel/Process.h>
  9. #include <Kernel/Thread.h>
  10. namespace Kernel {
  11. class PerformanceManager {
  12. public:
  13. inline static void add_process_created_event(Process& process)
  14. {
  15. if (g_profiling_all_threads) {
  16. VERIFY(g_global_perf_events);
  17. g_global_perf_events->add_process(process, ProcessEventType::Create);
  18. }
  19. }
  20. inline static void add_process_exec_event(Process& process)
  21. {
  22. if (auto* event_buffer = process.current_perf_events_buffer()) {
  23. event_buffer->add_process(process, ProcessEventType::Exec);
  24. }
  25. }
  26. inline static void add_process_exit_event(Process& process)
  27. {
  28. if (g_profiling_all_threads) {
  29. VERIFY(g_global_perf_events);
  30. [[maybe_unused]] auto rc = g_global_perf_events->append_with_ip_and_bp(
  31. process.pid(), 0, 0, 0, PERF_EVENT_PROCESS_EXIT, 0, 0, 0, nullptr);
  32. }
  33. }
  34. inline static void add_thread_created_event(Thread& thread)
  35. {
  36. if (thread.is_profiling_suppressed())
  37. return;
  38. if (auto* event_buffer = thread.process().current_perf_events_buffer()) {
  39. [[maybe_unused]] auto rc = event_buffer->append(PERF_EVENT_THREAD_CREATE, thread.tid().value(), 0, nullptr, &thread);
  40. }
  41. }
  42. inline static void add_thread_exit_event(Thread& thread)
  43. {
  44. // As an exception this doesn't check whether profiling is suppressed for
  45. // the thread so we can record the thread_exit event anyway.
  46. if (auto* event_buffer = thread.process().current_perf_events_buffer()) {
  47. [[maybe_unused]] auto rc = event_buffer->append(PERF_EVENT_THREAD_EXIT, thread.tid().value(), 0, nullptr, &thread);
  48. }
  49. }
  50. inline static void add_cpu_sample_event(Thread& current_thread, const RegisterState& regs, u32 lost_time)
  51. {
  52. if (current_thread.is_profiling_suppressed())
  53. return;
  54. if (auto* event_buffer = current_thread.process().current_perf_events_buffer()) {
  55. [[maybe_unused]] auto rc = event_buffer->append_with_ip_and_bp(
  56. current_thread.pid(), current_thread.tid(),
  57. regs.ip(), regs.bp(), PERF_EVENT_SAMPLE, lost_time, 0, 0, nullptr);
  58. }
  59. }
  60. inline static void add_mmap_perf_event(Process& current_process, Memory::Region const& region)
  61. {
  62. if (auto* event_buffer = current_process.current_perf_events_buffer()) {
  63. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_MMAP, region.vaddr().get(), region.size(), region.name());
  64. }
  65. }
  66. inline static void add_unmap_perf_event(Process& current_process, Memory::VirtualRange const& region)
  67. {
  68. if (auto* event_buffer = current_process.current_perf_events_buffer()) {
  69. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_MUNMAP, region.base().get(), region.size(), nullptr);
  70. }
  71. }
  72. inline static void add_context_switch_perf_event(Thread& current_thread, Thread& next_thread)
  73. {
  74. if (current_thread.is_profiling_suppressed())
  75. return;
  76. if (auto* event_buffer = current_thread.process().current_perf_events_buffer()) {
  77. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_CONTEXT_SWITCH, next_thread.pid().value(), next_thread.tid().value(), nullptr);
  78. }
  79. }
  80. inline static void add_kmalloc_perf_event(Thread& current_thread, size_t size, FlatPtr ptr)
  81. {
  82. if (current_thread.is_profiling_suppressed())
  83. return;
  84. if (auto* event_buffer = current_thread.process().current_perf_events_buffer()) {
  85. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_KMALLOC, size, ptr, nullptr);
  86. }
  87. }
  88. inline static void add_kfree_perf_event(Thread& current_thread, size_t size, FlatPtr ptr)
  89. {
  90. if (current_thread.is_profiling_suppressed())
  91. return;
  92. if (auto* event_buffer = current_thread.process().current_perf_events_buffer()) {
  93. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_KFREE, size, ptr, nullptr);
  94. }
  95. }
  96. inline static void add_page_fault_event(Thread& thread, const RegisterState& regs)
  97. {
  98. if (thread.is_profiling_suppressed())
  99. return;
  100. if (auto* event_buffer = thread.process().current_perf_events_buffer()) {
  101. [[maybe_unused]] auto rc = event_buffer->append_with_ip_and_bp(
  102. thread.pid(), thread.tid(),
  103. regs.ip(), regs.bp(), PERF_EVENT_PAGE_FAULT, 0, 0, 0, nullptr);
  104. }
  105. }
  106. inline static void add_syscall_event(Thread& thread, const RegisterState& regs)
  107. {
  108. if (thread.is_profiling_suppressed())
  109. return;
  110. if (auto* event_buffer = thread.process().current_perf_events_buffer()) {
  111. [[maybe_unused]] auto rc = event_buffer->append_with_ip_and_bp(
  112. thread.pid(), thread.tid(),
  113. regs.ip(), regs.bp(), PERF_EVENT_SYSCALL, 0, 0, 0, nullptr);
  114. }
  115. }
  116. inline static void timer_tick(RegisterState const& regs)
  117. {
  118. static Time last_wakeup;
  119. auto now = kgettimeofday();
  120. constexpr auto ideal_interval = Time::from_microseconds(1000'000 / OPTIMAL_PROFILE_TICKS_PER_SECOND_RATE);
  121. auto expected_wakeup = last_wakeup + ideal_interval;
  122. auto delay = (now > expected_wakeup) ? now - expected_wakeup : Time::from_microseconds(0);
  123. last_wakeup = now;
  124. auto current_thread = Thread::current();
  125. // FIXME: We currently don't collect samples while idle.
  126. // That will be an interesting mode to add in the future. :^)
  127. if (!current_thread || current_thread == Processor::current().idle_thread())
  128. return;
  129. auto lost_samples = delay.to_microseconds() / ideal_interval.to_microseconds();
  130. PerformanceManager::add_cpu_sample_event(*current_thread, regs, lost_samples);
  131. }
  132. };
  133. }