2020-02-02 19:26:27 +00:00
|
|
|
/*
|
2021-01-11 08:52:18 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-02-02 19:26:27 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-02 19:26:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/JsonArraySerializer.h>
|
2020-02-22 09:05:03 +00:00
|
|
|
#include <AK/JsonObject.h>
|
2020-03-22 00:12:45 +00:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2021-02-25 16:25:34 +00:00
|
|
|
#include <Kernel/Arch/x86/SmapDisabler.h>
|
2021-03-02 18:01:02 +00:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2020-02-02 19:26:27 +00:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
|
|
|
#include <Kernel/PerformanceEventBuffer.h>
|
2021-01-11 08:52:18 +00:00
|
|
|
#include <Kernel/Process.h>
|
2020-02-02 19:26:27 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-02 15:55:54 +00:00
|
|
|
PerformanceEventBuffer::PerformanceEventBuffer(NonnullOwnPtr<KBuffer> buffer)
|
|
|
|
: m_buffer(move(buffer))
|
2020-02-02 19:26:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:42:36 +00:00
|
|
|
NEVER_INLINE KResult PerformanceEventBuffer::append(int type, FlatPtr arg1, FlatPtr arg2, const StringView& arg3)
|
2021-01-17 09:53:18 +00:00
|
|
|
{
|
|
|
|
FlatPtr ebp;
|
|
|
|
asm volatile("movl %%ebp, %%eax"
|
|
|
|
: "=a"(ebp));
|
|
|
|
auto current_thread = Thread::current();
|
2021-04-25 21:42:36 +00:00
|
|
|
return append_with_eip_and_ebp(current_thread->pid(), current_thread->tid(), 0, ebp, type, arg1, arg2, arg3);
|
2021-01-17 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 10:51:13 +00:00
|
|
|
static Vector<FlatPtr, PerformanceEvent::max_stack_frame_count> raw_backtrace(FlatPtr ebp, FlatPtr eip)
|
|
|
|
{
|
|
|
|
Vector<FlatPtr, PerformanceEvent::max_stack_frame_count> backtrace;
|
2021-04-25 21:42:36 +00:00
|
|
|
if (eip != 0)
|
|
|
|
backtrace.append(eip);
|
2021-02-03 10:51:13 +00:00
|
|
|
FlatPtr stack_ptr_copy;
|
|
|
|
FlatPtr stack_ptr = (FlatPtr)ebp;
|
|
|
|
// FIXME: Figure out how to remove this SmapDisabler without breaking profile stacks.
|
|
|
|
SmapDisabler disabler;
|
|
|
|
while (stack_ptr) {
|
|
|
|
void* fault_at;
|
|
|
|
if (!safe_memcpy(&stack_ptr_copy, (void*)stack_ptr, sizeof(FlatPtr), fault_at))
|
|
|
|
break;
|
|
|
|
FlatPtr retaddr;
|
|
|
|
if (!safe_memcpy(&retaddr, (void*)(stack_ptr + sizeof(FlatPtr)), sizeof(FlatPtr), fault_at))
|
|
|
|
break;
|
2021-04-25 11:39:11 +00:00
|
|
|
if (retaddr == 0)
|
|
|
|
break;
|
2021-02-03 10:51:13 +00:00
|
|
|
backtrace.append(retaddr);
|
|
|
|
if (backtrace.size() == PerformanceEvent::max_stack_frame_count)
|
|
|
|
break;
|
|
|
|
stack_ptr = stack_ptr_copy;
|
|
|
|
}
|
|
|
|
return backtrace;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:42:36 +00:00
|
|
|
KResult PerformanceEventBuffer::append_with_eip_and_ebp(ProcessID pid, ThreadID tid,
|
|
|
|
u32 eip, u32 ebp, int type, FlatPtr arg1, FlatPtr arg2, const StringView& arg3)
|
2020-02-02 19:26:27 +00:00
|
|
|
{
|
|
|
|
if (count() >= capacity())
|
2021-01-20 22:11:17 +00:00
|
|
|
return ENOBUFS;
|
2020-02-02 19:26:27 +00:00
|
|
|
|
|
|
|
PerformanceEvent event;
|
|
|
|
event.type = type;
|
|
|
|
|
|
|
|
switch (type) {
|
2021-01-11 08:52:18 +00:00
|
|
|
case PERF_EVENT_SAMPLE:
|
|
|
|
break;
|
2020-02-02 19:26:27 +00:00
|
|
|
case PERF_EVENT_MALLOC:
|
|
|
|
event.data.malloc.size = arg1;
|
|
|
|
event.data.malloc.ptr = arg2;
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_FREE:
|
|
|
|
event.data.free.ptr = arg1;
|
|
|
|
break;
|
2021-04-25 21:42:36 +00:00
|
|
|
case PERF_EVENT_MMAP:
|
|
|
|
event.data.mmap.ptr = arg1;
|
|
|
|
event.data.mmap.size = arg2;
|
|
|
|
memset(event.data.mmap.name, 0, sizeof(event.data.mmap.name));
|
|
|
|
if (!arg3.is_empty())
|
|
|
|
memcpy(event.data.mmap.name, arg3.characters_without_null_termination(), min(arg3.length(), sizeof(event.data.mmap.name) - 1));
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_MUNMAP:
|
|
|
|
event.data.mmap.ptr = arg1;
|
|
|
|
event.data.mmap.size = arg2;
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_CREATE:
|
|
|
|
event.data.process_create.parent_pid = arg1;
|
|
|
|
memset(event.data.process_create.executable, 0, sizeof(event.data.process_create.executable));
|
|
|
|
if (!arg3.is_empty()) {
|
|
|
|
memcpy(event.data.process_create.executable, arg3.characters_without_null_termination(),
|
|
|
|
min(arg3.length(), sizeof(event.data.process_create.executable) - 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_EXEC:
|
|
|
|
memset(event.data.process_exec.executable, 0, sizeof(event.data.process_exec.executable));
|
|
|
|
if (!arg3.is_empty()) {
|
|
|
|
memcpy(event.data.process_exec.executable, arg3.characters_without_null_termination(),
|
|
|
|
min(arg3.length(), sizeof(event.data.process_exec.executable) - 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_EXIT:
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_THREAD_CREATE:
|
|
|
|
event.data.thread_create.parent_tid = arg1;
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_THREAD_EXIT:
|
|
|
|
break;
|
2020-02-02 19:26:27 +00:00
|
|
|
default:
|
2021-01-20 22:11:17 +00:00
|
|
|
return EINVAL;
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 10:51:13 +00:00
|
|
|
auto backtrace = raw_backtrace(ebp, eip);
|
2020-03-08 09:36:51 +00:00
|
|
|
event.stack_size = min(sizeof(event.stack) / sizeof(FlatPtr), static_cast<size_t>(backtrace.size()));
|
|
|
|
memcpy(event.stack, backtrace.data(), event.stack_size * sizeof(FlatPtr));
|
2020-02-02 19:26:27 +00:00
|
|
|
|
2021-04-25 21:42:36 +00:00
|
|
|
event.pid = pid.value();
|
|
|
|
event.tid = tid.value();
|
2020-11-15 18:58:19 +00:00
|
|
|
event.timestamp = TimeManagement::the().uptime_ms();
|
2020-02-02 19:26:27 +00:00
|
|
|
at(m_count++) = event;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
PerformanceEvent& PerformanceEventBuffer::at(size_t index)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(index < capacity());
|
2020-12-18 19:43:39 +00:00
|
|
|
auto* events = reinterpret_cast<PerformanceEvent*>(m_buffer->data());
|
2020-02-02 19:26:27 +00:00
|
|
|
return events[index];
|
|
|
|
}
|
|
|
|
|
2021-03-02 16:19:35 +00:00
|
|
|
template<typename Serializer>
|
|
|
|
bool PerformanceEventBuffer::to_json_impl(Serializer& object) const
|
2021-01-11 08:52:18 +00:00
|
|
|
{
|
2020-02-02 19:26:27 +00:00
|
|
|
auto array = object.add_array("events");
|
|
|
|
for (size_t i = 0; i < m_count; ++i) {
|
|
|
|
auto& event = at(i);
|
2020-03-01 20:08:04 +00:00
|
|
|
auto event_object = array.add_object();
|
2020-02-02 19:26:27 +00:00
|
|
|
switch (event.type) {
|
2021-01-11 08:52:18 +00:00
|
|
|
case PERF_EVENT_SAMPLE:
|
|
|
|
event_object.add("type", "sample");
|
|
|
|
break;
|
2020-02-02 19:26:27 +00:00
|
|
|
case PERF_EVENT_MALLOC:
|
2020-03-01 20:08:04 +00:00
|
|
|
event_object.add("type", "malloc");
|
|
|
|
event_object.add("ptr", static_cast<u64>(event.data.malloc.ptr));
|
|
|
|
event_object.add("size", static_cast<u64>(event.data.malloc.size));
|
2020-02-02 19:26:27 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_FREE:
|
2020-03-01 20:08:04 +00:00
|
|
|
event_object.add("type", "free");
|
|
|
|
event_object.add("ptr", static_cast<u64>(event.data.free.ptr));
|
2020-02-02 19:26:27 +00:00
|
|
|
break;
|
2021-04-25 21:42:36 +00:00
|
|
|
case PERF_EVENT_MMAP:
|
|
|
|
event_object.add("type", "mmap");
|
|
|
|
event_object.add("ptr", static_cast<u64>(event.data.mmap.ptr));
|
|
|
|
event_object.add("size", static_cast<u64>(event.data.mmap.size));
|
|
|
|
event_object.add("name", event.data.mmap.name);
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_MUNMAP:
|
|
|
|
event_object.add("type", "munmap");
|
|
|
|
event_object.add("ptr", static_cast<u64>(event.data.munmap.ptr));
|
|
|
|
event_object.add("size", static_cast<u64>(event.data.munmap.size));
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_CREATE:
|
|
|
|
event_object.add("type", "process_create");
|
|
|
|
event_object.add("parent_pid", static_cast<u64>(event.data.process_create.parent_pid));
|
|
|
|
event_object.add("executable", event.data.process_create.executable);
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_EXEC:
|
|
|
|
event_object.add("type", "process_exec");
|
|
|
|
event_object.add("executable", event.data.process_exec.executable);
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_EXIT:
|
|
|
|
event_object.add("type", "process_exit");
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_THREAD_CREATE:
|
|
|
|
event_object.add("type", "thread_create");
|
|
|
|
event_object.add("parent_tid", static_cast<u64>(event.data.thread_create.parent_tid));
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_THREAD_EXIT:
|
|
|
|
event_object.add("type", "thread_exit");
|
|
|
|
break;
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
2021-04-25 21:42:36 +00:00
|
|
|
event_object.add("pid", event.pid);
|
2021-01-11 08:52:18 +00:00
|
|
|
event_object.add("tid", event.tid);
|
2020-03-01 20:08:04 +00:00
|
|
|
event_object.add("timestamp", event.timestamp);
|
|
|
|
auto stack_array = event_object.add_array("stack");
|
2020-02-02 19:26:27 +00:00
|
|
|
for (size_t j = 0; j < event.stack_size; ++j) {
|
|
|
|
stack_array.add(event.stack[j]);
|
|
|
|
}
|
|
|
|
stack_array.finish();
|
2020-03-01 20:08:04 +00:00
|
|
|
event_object.finish();
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
|
|
|
array.finish();
|
|
|
|
object.finish();
|
2021-01-11 08:52:18 +00:00
|
|
|
return true;
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2021-03-02 18:01:02 +00:00
|
|
|
bool PerformanceEventBuffer::to_json(KBufferBuilder& builder) const
|
2021-03-02 16:19:35 +00:00
|
|
|
{
|
|
|
|
JsonObjectSerializer object(builder);
|
|
|
|
return to_json_impl(object);
|
|
|
|
}
|
|
|
|
|
2021-03-02 15:55:54 +00:00
|
|
|
OwnPtr<PerformanceEventBuffer> PerformanceEventBuffer::try_create_with_size(size_t buffer_size)
|
|
|
|
{
|
|
|
|
auto buffer = KBuffer::try_create_with_size(buffer_size, Region::Access::Read | Region::Access::Write, "Performance events", AllocationStrategy::AllocateNow);
|
|
|
|
if (!buffer)
|
|
|
|
return {};
|
|
|
|
return adopt_own(*new PerformanceEventBuffer(buffer.release_nonnull()));
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:42:36 +00:00
|
|
|
void PerformanceEventBuffer::add_process(const Process& process, ProcessEventType event_type)
|
2021-03-02 18:01:02 +00:00
|
|
|
{
|
|
|
|
ScopedSpinLock locker(process.space().get_lock());
|
|
|
|
|
|
|
|
String executable;
|
|
|
|
if (process.executable())
|
|
|
|
executable = process.executable()->absolute_path();
|
2021-04-25 21:42:36 +00:00
|
|
|
else
|
|
|
|
executable = String::formatted("<{}>", process.name());
|
|
|
|
|
|
|
|
[[maybe_unused]] auto rc = append_with_eip_and_ebp(process.pid(), 0, 0, 0,
|
|
|
|
event_type == ProcessEventType::Create ? PERF_EVENT_PROCESS_CREATE : PERF_EVENT_PROCESS_EXEC,
|
|
|
|
process.pid().value(), 0, executable.characters());
|
2021-03-02 18:01:02 +00:00
|
|
|
|
|
|
|
process.for_each_thread([&](auto& thread) {
|
2021-04-25 21:42:36 +00:00
|
|
|
[[maybe_unused]] auto rc = append_with_eip_and_ebp(process.pid(), thread.tid().value(),
|
|
|
|
0, 0, PERF_EVENT_THREAD_CREATE, 0, 0, nullptr);
|
2021-03-02 18:01:02 +00:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& region : process.space().regions()) {
|
2021-04-25 21:42:36 +00:00
|
|
|
[[maybe_unused]] auto rc = append_with_eip_and_ebp(process.pid(), 0,
|
|
|
|
0, 0, PERF_EVENT_MMAP, region->range().base().get(), region->range().size(), region->name().characters());
|
2021-03-02 18:01:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|