PerformanceEventBuffer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 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/JsonArraySerializer.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonObjectSerializer.h>
  29. #include <Kernel/KBufferBuilder.h>
  30. #include <Kernel/PerformanceEventBuffer.h>
  31. namespace Kernel {
  32. PerformanceEventBuffer::PerformanceEventBuffer()
  33. : m_buffer(KBuffer::create_with_size(4 * MiB))
  34. {
  35. }
  36. KResult PerformanceEventBuffer::append(int type, FlatPtr arg1, FlatPtr arg2)
  37. {
  38. if (count() >= capacity())
  39. return KResult(-ENOBUFS);
  40. PerformanceEvent event;
  41. event.type = type;
  42. switch (type) {
  43. case PERF_EVENT_MALLOC:
  44. event.data.malloc.size = arg1;
  45. event.data.malloc.ptr = arg2;
  46. #ifdef VERY_DEBUG
  47. dbg() << "PERF_EVENT_MALLOC: " << (void*)event.data.malloc.ptr << " (" << event.data.malloc.size << ")";
  48. #endif
  49. break;
  50. case PERF_EVENT_FREE:
  51. event.data.free.ptr = arg1;
  52. #ifdef VERY_DEBUG
  53. dbg() << "PERF_EVENT_FREE: " << (void*)event.data.free.ptr;
  54. #endif
  55. break;
  56. default:
  57. return KResult(-EINVAL);
  58. }
  59. FlatPtr ebp;
  60. asm volatile("movl %%ebp, %%eax"
  61. : "=a"(ebp));
  62. auto current_thread = Thread::current();
  63. auto eip = current_thread->get_register_dump_from_stack().eip;
  64. Vector<FlatPtr> backtrace;
  65. {
  66. SmapDisabler disabler;
  67. backtrace = current_thread->raw_backtrace(ebp, eip);
  68. }
  69. event.stack_size = min(sizeof(event.stack) / sizeof(FlatPtr), static_cast<size_t>(backtrace.size()));
  70. memcpy(event.stack, backtrace.data(), event.stack_size * sizeof(FlatPtr));
  71. #ifdef VERY_DEBUG
  72. for (size_t i = 0; i < event.stack_size; ++i)
  73. dbg() << " " << (void*)event.stack[i];
  74. #endif
  75. event.timestamp = TimeManagement::the().uptime_ms();
  76. at(m_count++) = event;
  77. return KSuccess;
  78. }
  79. PerformanceEvent& PerformanceEventBuffer::at(size_t index)
  80. {
  81. ASSERT(index < capacity());
  82. auto* events = reinterpret_cast<PerformanceEvent*>(m_buffer.data());
  83. return events[index];
  84. }
  85. OwnPtr<KBuffer> PerformanceEventBuffer::to_json(ProcessID pid, const String& executable_path) const
  86. {
  87. KBufferBuilder builder;
  88. JsonObjectSerializer object(builder);
  89. object.add("pid", pid.value());
  90. object.add("executable", executable_path);
  91. auto array = object.add_array("events");
  92. for (size_t i = 0; i < m_count; ++i) {
  93. auto& event = at(i);
  94. auto event_object = array.add_object();
  95. switch (event.type) {
  96. case PERF_EVENT_MALLOC:
  97. event_object.add("type", "malloc");
  98. event_object.add("ptr", static_cast<u64>(event.data.malloc.ptr));
  99. event_object.add("size", static_cast<u64>(event.data.malloc.size));
  100. break;
  101. case PERF_EVENT_FREE:
  102. event_object.add("type", "free");
  103. event_object.add("ptr", static_cast<u64>(event.data.free.ptr));
  104. break;
  105. }
  106. event_object.add("timestamp", event.timestamp);
  107. auto stack_array = event_object.add_array("stack");
  108. for (size_t j = 0; j < event.stack_size; ++j) {
  109. stack_array.add(event.stack[j]);
  110. }
  111. stack_array.finish();
  112. event_object.finish();
  113. }
  114. array.finish();
  115. object.finish();
  116. return builder.build();
  117. }
  118. }