PerformanceManager.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_eip_and_ebp(
  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 (auto* event_buffer = thread.process().current_perf_events_buffer()) {
  37. [[maybe_unused]] auto rc = event_buffer->append(PERF_EVENT_THREAD_CREATE, thread.tid().value(), 0, nullptr, &thread);
  38. }
  39. }
  40. inline static void add_thread_exit_event(Thread& thread)
  41. {
  42. if (auto* event_buffer = thread.process().current_perf_events_buffer()) {
  43. [[maybe_unused]] auto rc = event_buffer->append(PERF_EVENT_THREAD_EXIT, thread.tid().value(), 0, nullptr, &thread);
  44. }
  45. }
  46. inline static void add_cpu_sample_event(Thread& current_thread, const RegisterState& regs, u32 lost_time)
  47. {
  48. PerformanceEventBuffer* perf_events = nullptr;
  49. if (g_profiling_all_threads) {
  50. VERIFY(g_global_perf_events);
  51. perf_events = g_global_perf_events;
  52. } else if (current_thread.process().is_profiling()) {
  53. VERIFY(current_thread.process().perf_events());
  54. perf_events = current_thread.process().perf_events();
  55. }
  56. if (perf_events) {
  57. [[maybe_unused]] auto rc = perf_events->append_with_eip_and_ebp(
  58. current_thread.pid(), current_thread.tid(),
  59. regs.eip, regs.ebp, PERF_EVENT_SAMPLE, lost_time, 0, 0, nullptr);
  60. }
  61. }
  62. inline static void add_mmap_perf_event(Process& current_process, Region const& region)
  63. {
  64. if (auto* event_buffer = current_process.current_perf_events_buffer()) {
  65. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_MMAP, region.vaddr().get(), region.size(), region.name());
  66. }
  67. }
  68. inline static void add_unmap_perf_event(Process& current_process, Range const& region)
  69. {
  70. if (auto* event_buffer = current_process.current_perf_events_buffer()) {
  71. [[maybe_unused]] auto res = event_buffer->append(PERF_EVENT_MUNMAP, region.base().get(), region.size(), nullptr);
  72. }
  73. }
  74. inline static void timer_tick(RegisterState const& regs)
  75. {
  76. static Time last_wakeup;
  77. auto now = kgettimeofday();
  78. constexpr auto ideal_interval = Time::from_microseconds(1000'000 / OPTIMAL_PROFILE_TICKS_PER_SECOND_RATE);
  79. auto expected_wakeup = last_wakeup + ideal_interval;
  80. auto delay = (now > expected_wakeup) ? now - expected_wakeup : Time::from_microseconds(0);
  81. last_wakeup = now;
  82. auto current_thread = Thread::current();
  83. // FIXME: We currently don't collect samples while idle.
  84. // That will be an interesting mode to add in the future. :^)
  85. if (!current_thread || current_thread == Processor::current().idle_thread())
  86. return;
  87. auto lost_samples = delay.to_microseconds() / ideal_interval.to_microseconds();
  88. PerformanceManager::add_cpu_sample_event(*current_thread, regs, lost_samples);
  89. }
  90. };
  91. }