1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:57:45 +00:00

Kernel+LibC: Add support for filtering profiling events

This adds the -t command-line argument for the profile tool. Using this
argument you can filter which event types you want in your profile.
This commit is contained in:
Gunnar Beutner 2021-05-14 08:10:43 +02:00 committed by Andreas Kling
parent 8b2ace0326
commit 572bbf28cc
10 changed files with 72 additions and 33 deletions

View file

@ -60,6 +60,9 @@ KResult PerformanceEventBuffer::append_with_eip_and_ebp(ProcessID pid, ThreadID
if (count() >= capacity())
return ENOBUFS;
if ((g_profiling_event_mask & type) == 0)
return EINVAL;
PerformanceEvent event;
event.type = type;
event.lost_samples = lost_samples;

View file

@ -53,7 +53,7 @@ struct [[gnu::packed]] ContextSwitchPerformanceEvent {
};
struct [[gnu::packed]] PerformanceEvent {
u8 type { 0 };
u16 type { 0 };
u8 stack_size { 0 };
u32 pid { 0 };
u32 tid { 0 };
@ -116,5 +116,6 @@ private:
extern bool g_profiling_all_threads;
extern PerformanceEventBuffer* g_global_perf_events;
extern u64 g_profiling_event_mask;
}

View file

@ -392,7 +392,7 @@ public:
KResultOr<int> sys$setkeymap(Userspace<const Syscall::SC_setkeymap_params*>);
KResultOr<int> sys$module_load(Userspace<const char*> path, size_t path_length);
KResultOr<int> sys$module_unload(Userspace<const char*> name, size_t name_length);
KResultOr<int> sys$profiling_enable(pid_t);
KResultOr<int> sys$profiling_enable(pid_t, u64);
KResultOr<int> sys$profiling_disable(pid_t);
KResultOr<int> sys$profiling_free_buffer(pid_t);
KResultOr<int> sys$futex(Userspace<const Syscall::SC_futex_params*>);

View file

@ -15,8 +15,9 @@ namespace Kernel {
bool g_profiling_all_threads;
PerformanceEventBuffer* g_global_perf_events;
u64 g_profiling_event_mask;
KResultOr<int> Process::sys$profiling_enable(pid_t pid)
KResultOr<int> Process::sys$profiling_enable(pid_t pid, u64 event_mask)
{
REQUIRE_NO_PROMISES;
@ -24,6 +25,7 @@ KResultOr<int> Process::sys$profiling_enable(pid_t pid)
if (!is_superuser())
return EPERM;
ScopedCritical critical;
g_profiling_event_mask = event_mask;
if (g_global_perf_events)
g_global_perf_events->clear();
else
@ -33,6 +35,7 @@ KResultOr<int> Process::sys$profiling_enable(pid_t pid)
if (!TimeManagement::the().enable_profile_timer())
return ENOTSUP;
g_profiling_all_threads = true;
PerformanceManager::add_process_created_event(*Scheduler::colonel());
Process::for_each([](auto& process) {
PerformanceManager::add_process_created_event(process);
return IterationDecision::Continue;
@ -52,6 +55,7 @@ KResultOr<int> Process::sys$profiling_enable(pid_t pid)
return ENOMEM;
if (!TimeManagement::the().enable_profile_timer())
return ENOTSUP;
g_profiling_event_mask = event_mask;
process->set_profiling(true);
return 0;
}

View file

@ -47,17 +47,17 @@ enum {
};
enum {
PERF_EVENT_SAMPLE,
PERF_EVENT_MALLOC,
PERF_EVENT_FREE,
PERF_EVENT_MMAP,
PERF_EVENT_MUNMAP,
PERF_EVENT_PROCESS_CREATE,
PERF_EVENT_PROCESS_EXEC,
PERF_EVENT_PROCESS_EXIT,
PERF_EVENT_THREAD_CREATE,
PERF_EVENT_THREAD_EXIT,
PERF_EVENT_CONTEXT_SWITCH,
PERF_EVENT_SAMPLE = 1,
PERF_EVENT_MALLOC = 2,
PERF_EVENT_FREE = 4,
PERF_EVENT_MMAP = 8,
PERF_EVENT_MUNMAP = 16,
PERF_EVENT_PROCESS_CREATE = 32,
PERF_EVENT_PROCESS_EXEC = 64,
PERF_EVENT_PROCESS_EXIT = 128,
PERF_EVENT_THREAD_CREATE = 256,
PERF_EVENT_THREAD_EXIT = 512,
PERF_EVENT_CONTEXT_SWITCH = 1024,
};
#define WNOHANG 1

View file

@ -287,7 +287,7 @@ void init_stage2(void*)
if (boot_profiling) {
dbgln("Starting full system boot profiling");
auto result = Process::current()->sys$profiling_enable(-1);
auto result = Process::current()->sys$profiling_enable(-1, ~0ull);
VERIFY(!result.is_error());
}