Profiling.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Demangle.h>
  27. #include <AK/Singleton.h>
  28. #include <AK/StringBuilder.h>
  29. #include <Kernel/FileSystem/Custody.h>
  30. #include <Kernel/KBuffer.h>
  31. #include <Kernel/KSyms.h>
  32. #include <Kernel/Process.h>
  33. #include <Kernel/Profiling.h>
  34. namespace Kernel {
  35. namespace Profiling {
  36. static size_t s_slot_count;
  37. static AK::Singleton<KBuffer, []() -> KBuffer* {
  38. auto buffer = KBuffer::try_create_with_size(8 * MiB, Region::Access::Read | Region::Access::Write, "Profiling Buffer", AllocationStrategy::AllocateNow);
  39. s_slot_count = buffer->size() / sizeof(Sample);
  40. return buffer.leak_ptr();
  41. }>
  42. s_profiling_buffer;
  43. static size_t s_next_slot_index;
  44. static ProcessID s_pid { -1 };
  45. String& executable_path()
  46. {
  47. static String* path;
  48. if (!path)
  49. path = new String;
  50. return *path;
  51. }
  52. ProcessID pid()
  53. {
  54. return s_pid;
  55. }
  56. void start(Process& process)
  57. {
  58. if (process.executable())
  59. executable_path() = process.executable()->absolute_path().impl();
  60. else
  61. executable_path() = {};
  62. s_pid = process.pid();
  63. s_profiling_buffer.ensure_instance();
  64. s_next_slot_index = 0;
  65. }
  66. static Sample& sample_slot(size_t index)
  67. {
  68. return ((Sample*)s_profiling_buffer->data())[index];
  69. }
  70. Sample& next_sample_slot()
  71. {
  72. auto& slot = sample_slot(s_next_slot_index++);
  73. if (s_next_slot_index >= s_slot_count)
  74. s_next_slot_index = 0;
  75. return slot;
  76. }
  77. void stop()
  78. {
  79. // FIXME: This probably shouldn't be empty.
  80. }
  81. void did_exec(const String& new_executable_path)
  82. {
  83. executable_path() = new_executable_path;
  84. s_next_slot_index = 0;
  85. }
  86. void for_each_sample(Function<void(Sample&)> callback)
  87. {
  88. for (size_t i = 0; i < s_next_slot_index; ++i) {
  89. auto& sample = sample_slot(i);
  90. callback(sample);
  91. }
  92. }
  93. }
  94. }