PerformanceManager.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. (void)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. (void)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, {});
  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, {}, &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, {}, &thread);
  48. }
  49. }
  50. inline static void add_cpu_sample_event(Thread& current_thread, RegisterState const& 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(), regs, PERF_EVENT_SAMPLE, lost_time, 0, 0, {});
  57. }
  58. }
  59. inline static void add_mmap_perf_event(Process& current_process, Memory::Region const& region)
  60. {
  61. if (auto* event_buffer = current_process.current_perf_events_buffer()) {
  62. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_MMAP, region.vaddr().get(), region.size(), region.name());
  63. }
  64. }
  65. inline static void add_unmap_perf_event(Process& current_process, Memory::VirtualRange const& region)
  66. {
  67. if (auto* event_buffer = current_process.current_perf_events_buffer()) {
  68. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_MUNMAP, region.base().get(), region.size(), {});
  69. }
  70. }
  71. inline static void add_context_switch_perf_event(Thread& current_thread, Thread& next_thread)
  72. {
  73. if (current_thread.is_profiling_suppressed())
  74. return;
  75. if (auto* event_buffer = current_thread.process().current_perf_events_buffer()) {
  76. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_CONTEXT_SWITCH, next_thread.pid().value(), next_thread.tid().value(), {});
  77. }
  78. }
  79. inline static void add_kmalloc_perf_event(Thread& current_thread, size_t size, FlatPtr ptr)
  80. {
  81. if (current_thread.is_profiling_suppressed())
  82. return;
  83. if (auto* event_buffer = current_thread.process().current_perf_events_buffer()) {
  84. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_KMALLOC, size, ptr, {});
  85. }
  86. }
  87. inline static void add_kfree_perf_event(Thread& current_thread, size_t size, FlatPtr ptr)
  88. {
  89. if (current_thread.is_profiling_suppressed())
  90. return;
  91. if (auto* event_buffer = current_thread.process().current_perf_events_buffer()) {
  92. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_KFREE, size, ptr, {});
  93. }
  94. }
  95. inline static void add_page_fault_event(Thread& thread, RegisterState const& regs)
  96. {
  97. if (thread.is_profiling_suppressed())
  98. return;
  99. if (auto* event_buffer = thread.process().current_perf_events_buffer()) {
  100. [[maybe_unused]] auto rc = event_buffer->append_with_ip_and_bp(
  101. thread.pid(), thread.tid(), regs, PERF_EVENT_PAGE_FAULT, 0, 0, 0, {});
  102. }
  103. }
  104. inline static void add_syscall_event(Thread& thread, RegisterState const& regs)
  105. {
  106. if (thread.is_profiling_suppressed())
  107. return;
  108. if (auto* event_buffer = thread.process().current_perf_events_buffer()) {
  109. [[maybe_unused]] auto rc = event_buffer->append_with_ip_and_bp(
  110. thread.pid(), thread.tid(), regs, PERF_EVENT_SYSCALL, 0, 0, 0, {});
  111. }
  112. }
  113. inline static void add_read_event(Thread& thread, int fd, size_t size, OpenFileDescription const& file_description, u64 start_timestamp, ErrorOr<FlatPtr> result)
  114. {
  115. if (thread.is_profiling_suppressed())
  116. return;
  117. auto* event_buffer = thread.process().current_perf_events_buffer();
  118. if (event_buffer == nullptr)
  119. return;
  120. size_t filepath_string_index;
  121. if (auto path = file_description.original_absolute_path(); !path.is_error()) {
  122. auto registered_result = event_buffer->register_string(move(path.value()));
  123. if (registered_result.is_error())
  124. return;
  125. filepath_string_index = registered_result.value();
  126. } else if (auto pseudo_path = file_description.pseudo_path(); !pseudo_path.is_error()) {
  127. auto registered_result = event_buffer->register_string(move(pseudo_path.value()));
  128. if (registered_result.is_error())
  129. return;
  130. filepath_string_index = registered_result.value();
  131. } else {
  132. auto invalid_path_string = KString::try_create("<INVALID_FILE_PATH>"sv); // TODO: Performance, unnecessary allocations.
  133. if (invalid_path_string.is_error())
  134. return;
  135. auto registered_result = event_buffer->register_string(move(invalid_path_string.value()));
  136. if (registered_result.is_error())
  137. return;
  138. filepath_string_index = registered_result.value();
  139. }
  140. [[maybe_unused]] auto rc = event_buffer->append(PERF_EVENT_READ, fd, size, {}, &thread, filepath_string_index, start_timestamp, result); // wrong arguments
  141. }
  142. inline static void timer_tick(RegisterState const& regs)
  143. {
  144. static Time last_wakeup;
  145. auto now = kgettimeofday();
  146. constexpr auto ideal_interval = Time::from_microseconds(1000'000 / OPTIMAL_PROFILE_TICKS_PER_SECOND_RATE);
  147. auto expected_wakeup = last_wakeup + ideal_interval;
  148. auto delay = (now > expected_wakeup) ? now - expected_wakeup : Time::from_microseconds(0);
  149. last_wakeup = now;
  150. auto* current_thread = Thread::current();
  151. // FIXME: We currently don't collect samples while idle.
  152. // That will be an interesting mode to add in the future. :^)
  153. if (!current_thread || current_thread == Processor::idle_thread())
  154. return;
  155. auto lost_samples = delay.to_microseconds() / ideal_interval.to_microseconds();
  156. PerformanceManager::add_cpu_sample_event(*current_thread, regs, lost_samples);
  157. }
  158. };
  159. }