mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:47:44 +00:00
Kernel+Profiler: Capture metadata about all profiled processes
The perfcore file format was previously limited to a single process since the pid/executable/regions data was top-level in the JSON. This patch moves the process-specific data into a top-level array named "processes" and we now add entries for each process that has been sampled during the profile run. This makes it possible to see samples from multiple threads when viewing a perfcore file with Profiler. This is extremely cool! :^)
This commit is contained in:
parent
ea500dd3e3
commit
5e7abea31e
11 changed files with 223 additions and 102 deletions
|
@ -488,16 +488,10 @@ static bool procfs$pid_perf_events(InodeIdentifier identifier, KBufferBuilder& b
|
|||
auto process = Process::from_pid(to_pid(identifier));
|
||||
if (!process)
|
||||
return false;
|
||||
|
||||
InterruptDisabler disabler;
|
||||
|
||||
if (!process->executable())
|
||||
return false;
|
||||
|
||||
if (!process->perf_events())
|
||||
return false;
|
||||
|
||||
return process->perf_events()->to_json(builder, process->pid(), process->executable()->absolute_path());
|
||||
return process->perf_events()->to_json(builder);
|
||||
}
|
||||
|
||||
static bool procfs$net_adapters(InodeIdentifier, KBufferBuilder& builder)
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonObjectSerializer.h>
|
||||
#include <Kernel/Arch/x86/SmapDisabler.h>
|
||||
#include <Kernel/FileSystem/Custody.h>
|
||||
#include <Kernel/KBufferBuilder.h>
|
||||
#include <Kernel/PerformanceEventBuffer.h>
|
||||
#include <Kernel/Process.h>
|
||||
|
@ -111,14 +112,6 @@ PerformanceEvent& PerformanceEventBuffer::at(size_t index)
|
|||
return events[index];
|
||||
}
|
||||
|
||||
OwnPtr<KBuffer> PerformanceEventBuffer::to_json(ProcessID pid, const String& executable_path) const
|
||||
{
|
||||
KBufferBuilder builder;
|
||||
if (!to_json(builder, pid, executable_path))
|
||||
return {};
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
template<typename Serializer>
|
||||
bool PerformanceEventBuffer::to_json_impl(Serializer& object) const
|
||||
{
|
||||
|
@ -154,33 +147,28 @@ bool PerformanceEventBuffer::to_json_impl(Serializer& object) const
|
|||
return true;
|
||||
}
|
||||
|
||||
bool PerformanceEventBuffer::to_json(KBufferBuilder& builder)
|
||||
bool PerformanceEventBuffer::to_json(KBufferBuilder& builder) const
|
||||
{
|
||||
JsonObjectSerializer object(builder);
|
||||
return to_json_impl(object);
|
||||
}
|
||||
|
||||
bool PerformanceEventBuffer::to_json(KBufferBuilder& builder, ProcessID pid, const String& executable_path) const
|
||||
{
|
||||
auto process = Process::from_pid(pid);
|
||||
VERIFY(process);
|
||||
ScopedSpinLock locker(process->space().get_lock());
|
||||
auto processes_array = object.add_array("processes");
|
||||
for (auto& it : m_processes) {
|
||||
auto& process = *it.value;
|
||||
auto process_object = processes_array.add_object();
|
||||
process_object.add("pid", process.pid.value());
|
||||
process_object.add("executable", process.executable);
|
||||
|
||||
JsonObjectSerializer object(builder);
|
||||
object.add("pid", pid.value());
|
||||
object.add("executable", executable_path);
|
||||
|
||||
{
|
||||
auto region_array = object.add_array("regions");
|
||||
for (const auto& region : process->space().regions()) {
|
||||
auto region_object = region_array.add_object();
|
||||
region_object.add("base", region.vaddr().get());
|
||||
region_object.add("size", region.size());
|
||||
region_object.add("name", region.name());
|
||||
auto regions_array = process_object.add_array("regions");
|
||||
for (auto& region : process.regions) {
|
||||
auto region_object = regions_array.add_object();
|
||||
region_object.add("name", region.name);
|
||||
region_object.add("base", region.range.base().get());
|
||||
region_object.add("size", region.range.size());
|
||||
}
|
||||
region_array.finish();
|
||||
}
|
||||
|
||||
processes_array.finish();
|
||||
|
||||
return to_json_impl(object);
|
||||
}
|
||||
|
||||
|
@ -192,4 +180,35 @@ OwnPtr<PerformanceEventBuffer> PerformanceEventBuffer::try_create_with_size(size
|
|||
return adopt_own(*new PerformanceEventBuffer(buffer.release_nonnull()));
|
||||
}
|
||||
|
||||
void PerformanceEventBuffer::add_process(const Process& process)
|
||||
{
|
||||
// FIXME: What about threads that have died?
|
||||
|
||||
ScopedSpinLock locker(process.space().get_lock());
|
||||
|
||||
String executable;
|
||||
if (process.executable())
|
||||
executable = process.executable()->absolute_path();
|
||||
|
||||
auto sampled_process = adopt_own(*new SampledProcess {
|
||||
.pid = process.pid().value(),
|
||||
.executable = executable,
|
||||
.threads = {},
|
||||
.regions = {},
|
||||
});
|
||||
process.for_each_thread([&](auto& thread) {
|
||||
sampled_process->threads.set(thread.tid());
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
for (auto& region : process.space().regions()) {
|
||||
sampled_process->regions.append(SampledProcess::Region {
|
||||
.name = region.name(),
|
||||
.range = region.range(),
|
||||
});
|
||||
}
|
||||
|
||||
m_processes.set(process.pid(), move(sampled_process));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -75,15 +75,25 @@ public:
|
|||
return const_cast<PerformanceEventBuffer&>(*this).at(index);
|
||||
}
|
||||
|
||||
OwnPtr<KBuffer> to_json(ProcessID, const String& executable_path) const;
|
||||
bool to_json(KBufferBuilder&, ProcessID, const String& executable_path) const;
|
||||
bool to_json(KBufferBuilder&) const;
|
||||
|
||||
// Used by full-system profile (/proc/profile)
|
||||
bool to_json(KBufferBuilder&);
|
||||
void add_process(const Process&);
|
||||
|
||||
private:
|
||||
explicit PerformanceEventBuffer(NonnullOwnPtr<KBuffer>);
|
||||
|
||||
struct SampledProcess {
|
||||
ProcessID pid;
|
||||
String executable;
|
||||
HashTable<ThreadID> threads;
|
||||
|
||||
struct Region {
|
||||
String name;
|
||||
Range range;
|
||||
};
|
||||
Vector<Region> regions;
|
||||
};
|
||||
|
||||
template<typename Serializer>
|
||||
bool to_json_impl(Serializer&) const;
|
||||
|
||||
|
@ -91,6 +101,8 @@ private:
|
|||
|
||||
size_t m_count { 0 };
|
||||
NonnullOwnPtr<KBuffer> m_buffer;
|
||||
|
||||
HashMap<ProcessID, NonnullOwnPtr<SampledProcess>> m_processes;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -448,10 +448,13 @@ bool Process::dump_perfcore()
|
|||
if (description_or_error.is_error())
|
||||
return false;
|
||||
auto& description = description_or_error.value();
|
||||
auto json = m_perf_event_buffer->to_json(m_pid, m_executable ? m_executable->absolute_path() : "");
|
||||
if (!json)
|
||||
KBufferBuilder builder;
|
||||
if (!m_perf_event_buffer->to_json(builder))
|
||||
return false;
|
||||
|
||||
auto json = builder.build();
|
||||
if (!json)
|
||||
return false;
|
||||
auto json_buffer = UserOrKernelBuffer::for_kernel_buffer(json->data());
|
||||
return !description->write(json_buffer, json->size()).is_error();
|
||||
}
|
||||
|
@ -671,6 +674,7 @@ bool Process::create_perf_events_buffer_if_needed()
|
|||
{
|
||||
if (!m_perf_event_buffer) {
|
||||
m_perf_event_buffer = PerformanceEventBuffer::try_create_with_size(4 * MiB);
|
||||
m_perf_event_buffer->add_process(*this);
|
||||
}
|
||||
return !!m_perf_event_buffer;
|
||||
}
|
||||
|
|
|
@ -552,8 +552,15 @@ void Scheduler::timer_tick(const RegisterState& regs)
|
|||
VERIFY(g_global_perf_events);
|
||||
// FIXME: We currently don't collect samples while idle.
|
||||
// That will be an interesting mode to add in the future. :^)
|
||||
if (current_thread != Processor::current().idle_thread())
|
||||
if (current_thread != Processor::current().idle_thread()) {
|
||||
perf_events = g_global_perf_events;
|
||||
if (current_thread->process().space().enforces_syscall_regions()) {
|
||||
// FIXME: This is very nasty! We dump the current process's address
|
||||
// space layout *every time* it's sampled. We should figure out
|
||||
// a way to do this less often.
|
||||
perf_events->add_process(current_thread->process());
|
||||
}
|
||||
}
|
||||
} else if (current_thread->process().is_profiling()) {
|
||||
VERIFY(current_thread->process().perf_events());
|
||||
perf_events = current_thread->process().perf_events();
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <Kernel/FileSystem/FileDescription.h>
|
||||
#include <Kernel/PerformanceEventBuffer.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/VM/MemoryManager.h>
|
||||
#include <Kernel/VM/PageDirectory.h>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue