Skip to content

Commit

Permalink
Kernel: Make PerformanceEventBuffer::add_process fallible with ErrorOr
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanHo committed Jan 12, 2022
1 parent d2ffcfb commit 8a4654a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
21 changes: 13 additions & 8 deletions Kernel/PerformanceEventBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,29 +313,34 @@ OwnPtr<PerformanceEventBuffer> PerformanceEventBuffer::try_create_with_size(size
return adopt_own_if_nonnull(new (nothrow) PerformanceEventBuffer(buffer_or_error.release_value()));
}

void PerformanceEventBuffer::add_process(const Process& process, ProcessEventType event_type)
ErrorOr<void> PerformanceEventBuffer::add_process(const Process& process, ProcessEventType event_type)
{
SpinlockLocker locker(process.address_space().get_lock());

OwnPtr<KString> executable;
if (process.executable())
executable = MUST(process.executable()->try_serialize_absolute_path());
executable = TRY(process.executable()->try_serialize_absolute_path());
else
executable = MUST(KString::formatted("<{}>", process.name()));
executable = TRY(KString::formatted("<{}>", process.name()));

[[maybe_unused]] auto rc = append_with_ip_and_bp(process.pid(), 0, 0, 0,
TRY(append_with_ip_and_bp(process.pid(), 0, 0, 0,
event_type == ProcessEventType::Create ? PERF_EVENT_PROCESS_CREATE : PERF_EVENT_PROCESS_EXEC,
0, process.pid().value(), 0, executable->view());
0, process.pid().value(), 0, executable->view()));

ErrorOr<void> result;
process.for_each_thread([&](auto& thread) {
[[maybe_unused]] auto rc = append_with_ip_and_bp(process.pid(), thread.tid().value(),
result = append_with_ip_and_bp(process.pid(), thread.tid().value(),
0, 0, PERF_EVENT_THREAD_CREATE, 0, 0, 0, nullptr);
return result.is_error() ? IterationDecision::Break : IterationDecision::Continue;
});
TRY(result);

for (auto const& region : process.address_space().regions()) {
[[maybe_unused]] auto rc = append_with_ip_and_bp(process.pid(), 0,
0, 0, PERF_EVENT_MMAP, 0, region->range().base().get(), region->range().size(), region->name());
TRY(append_with_ip_and_bp(process.pid(), 0,
0, 0, PERF_EVENT_MMAP, 0, region->range().base().get(), region->range().size(), region->name()));
}

return {};
}

ErrorOr<FlatPtr> PerformanceEventBuffer::register_string(NonnullOwnPtr<KString> string)
Expand Down
2 changes: 1 addition & 1 deletion Kernel/PerformanceEventBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class PerformanceEventBuffer {

ErrorOr<void> to_json(KBufferBuilder&) const;

void add_process(const Process&, ProcessEventType event_type);
ErrorOr<void> add_process(const Process&, ProcessEventType event_type);

ErrorOr<FlatPtr> register_string(NonnullOwnPtr<KString>);

Expand Down
4 changes: 2 additions & 2 deletions Kernel/PerformanceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class PerformanceManager {
{
if (g_profiling_all_threads) {
VERIFY(g_global_perf_events);
g_global_perf_events->add_process(process, ProcessEventType::Create);
(void)g_global_perf_events->add_process(process, ProcessEventType::Create);
}
}

inline static void add_process_exec_event(Process& process)
{
if (auto* event_buffer = process.current_perf_events_buffer()) {
event_buffer->add_process(process, ProcessEventType::Exec);
(void)event_buffer->add_process(process, ProcessEventType::Exec);
}
}

Expand Down
11 changes: 6 additions & 5 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,12 @@ void Process::tracer_trap(Thread& thread, const RegisterState& regs)

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, ProcessEventType::Create);
}
return !!m_perf_event_buffer;
if (m_perf_event_buffer)
return true;
m_perf_event_buffer = PerformanceEventBuffer::try_create_with_size(4 * MiB);
if (!m_perf_event_buffer)
return false;
return !m_perf_event_buffer->add_process(*this, ProcessEventType::Create).is_error();
}

void Process::delete_perf_events_buffer()
Expand Down

0 comments on commit 8a4654a

Please sign in to comment.