mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00:00
Kernel+ProfileViewer: Display additional filesystem events
This commit is contained in:
parent
c184a0786f
commit
54e79aa1d9
16 changed files with 655 additions and 135 deletions
|
@ -32,7 +32,7 @@ enum {
|
|||
PERF_EVENT_PAGE_FAULT = 8192,
|
||||
PERF_EVENT_SYSCALL = 16384,
|
||||
PERF_EVENT_SIGNPOST = 32768,
|
||||
PERF_EVENT_READ = 65536,
|
||||
PERF_EVENT_FILESYSTEM = 65536,
|
||||
};
|
||||
|
||||
#define PERF_EVENT_MASK_ALL (~0ull)
|
||||
|
|
|
@ -309,6 +309,7 @@ set(KERNEL_SOURCES
|
|||
Syscalls/prctl.cpp
|
||||
Syscalls/process.cpp
|
||||
Syscalls/profiling.cpp
|
||||
Syscalls/profiled_syscalls.cpp
|
||||
Syscalls/ptrace.cpp
|
||||
Syscalls/purge.cpp
|
||||
Syscalls/read.cpp
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
namespace Kernel {
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$open(Userspace<Syscall::SC_open_params const*> user_params)
|
||||
ErrorOr<FlatPtr> Process::open_impl(Userspace<Syscall::SC_open_params const*> user_params)
|
||||
{
|
||||
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
||||
auto params = TRY(copy_typed_from_user(user_params));
|
||||
|
@ -68,7 +68,7 @@ ErrorOr<FlatPtr> Process::sys$open(Userspace<Syscall::SC_open_params const*> use
|
|||
});
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$close(int fd)
|
||||
ErrorOr<FlatPtr> Process::close_impl(int fd)
|
||||
{
|
||||
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
||||
TRY(require_promise(Pledge::stdio));
|
||||
|
|
274
Kernel/Syscalls/profiled_syscalls.cpp
Normal file
274
Kernel/Syscalls/profiled_syscalls.cpp
Normal file
|
@ -0,0 +1,274 @@
|
|||
/*
|
||||
* Copyright (c) 2022-2023, Jakub Berkop <jakub.berkop@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <Kernel/FileSystem/OpenFileDescription.h>
|
||||
#include <Kernel/Tasks/PerformanceManager.h>
|
||||
#include <Kernel/Tasks/Process.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
ErrorOr<NonnullRefPtr<OpenFileDescription>> open_readable_file_description(auto& fds, int fd);
|
||||
|
||||
static ErrorOr<size_t> get_path_index(auto& fds, int fd, PerformanceEventBuffer* event_buffer)
|
||||
{
|
||||
auto description = TRY(open_readable_file_description(fds, fd));
|
||||
|
||||
if (auto path = description->original_absolute_path(); !path.is_error()) {
|
||||
return TRY(event_buffer->register_string(move(path.value())));
|
||||
} else if (auto pseudo_path = description->pseudo_path(); !pseudo_path.is_error()) {
|
||||
return TRY(event_buffer->register_string(move(pseudo_path.value())));
|
||||
} else {
|
||||
auto invalid_path_string = TRY(KString::try_create("<INVALID_FILE_PATH>"sv)); // TODO: Performance, unecessary allocations.
|
||||
return TRY(event_buffer->register_string(move(invalid_path_string)));
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Following functions are very similar, and could be refactored into a more generic solution.
|
||||
// However, it's not clear how to do it in a way that would be easy to read and understand.
|
||||
ErrorOr<FlatPtr> Process::sys$open(Userspace<Syscall::SC_open_params const*> user_params)
|
||||
{
|
||||
Optional<MonotonicTime> start_timestamp = {};
|
||||
|
||||
// We have to check whether profiling is enabled at before going into the syscall implementation
|
||||
// so that we can measure the time it took to execute the syscall.
|
||||
// This approach ensures that we don't have a race condition in case profiling was enabled during
|
||||
// the execution of the syscall.
|
||||
// If profiling is disabled at the beginning, we don't want to call TimeManagement::the().monotonic_time()
|
||||
// because of the overhead it would introduce for every syscall.
|
||||
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
|
||||
if (profiling_enabled_at_entry) {
|
||||
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
}
|
||||
|
||||
auto const params = TRY(copy_typed_from_user(user_params));
|
||||
auto result = open_impl(user_params);
|
||||
|
||||
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
|
||||
return result;
|
||||
|
||||
auto* event_buffer = current_perf_events_buffer();
|
||||
if (event_buffer == nullptr)
|
||||
return result;
|
||||
|
||||
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
auto const duration = end_timestamp - start_timestamp.value();
|
||||
|
||||
FilesystemEvent data;
|
||||
data.type = FilesystemEventType::Open;
|
||||
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
|
||||
|
||||
if (result.is_error()) {
|
||||
data.result.is_error = true;
|
||||
data.result.value = result.error().code();
|
||||
} else {
|
||||
data.result.is_error = false;
|
||||
data.result.value = 0;
|
||||
}
|
||||
|
||||
auto path = get_syscall_path_argument(params.path);
|
||||
if (!path.is_error()) {
|
||||
auto value = event_buffer->register_string(move(path.value()));
|
||||
data.data.open.filename_index = value.value();
|
||||
}
|
||||
|
||||
data.data.open.dirfd = params.dirfd;
|
||||
data.data.open.options = params.options;
|
||||
data.data.open.mode = params.mode;
|
||||
|
||||
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$close(int fd)
|
||||
{
|
||||
Optional<MonotonicTime> start_timestamp = {};
|
||||
|
||||
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
|
||||
if (profiling_enabled_at_entry) {
|
||||
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
}
|
||||
|
||||
auto result = close_impl(fd);
|
||||
if (Thread::current()->is_profiling_suppressed())
|
||||
return result;
|
||||
|
||||
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
|
||||
return result;
|
||||
|
||||
auto* event_buffer = current_perf_events_buffer();
|
||||
if (event_buffer == nullptr)
|
||||
return result;
|
||||
|
||||
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
auto const duration = end_timestamp - start_timestamp.value();
|
||||
|
||||
FilesystemEvent data;
|
||||
data.type = FilesystemEventType::Close;
|
||||
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
|
||||
data.data.close.fd = fd;
|
||||
|
||||
if (result.is_error()) {
|
||||
data.result.is_error = true;
|
||||
data.result.value = result.error().code();
|
||||
} else {
|
||||
data.result.is_error = false;
|
||||
data.result.value = 0;
|
||||
}
|
||||
|
||||
auto maybe_path_index = get_path_index(fds(), fd, event_buffer);
|
||||
if (maybe_path_index.is_error())
|
||||
return result;
|
||||
|
||||
data.data.close.filename_index = maybe_path_index.value();
|
||||
|
||||
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
||||
{
|
||||
Optional<MonotonicTime> start_timestamp = {};
|
||||
|
||||
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
|
||||
if (profiling_enabled_at_entry) {
|
||||
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
}
|
||||
|
||||
auto result = readv_impl(fd, iov, iov_count);
|
||||
|
||||
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
|
||||
return result;
|
||||
|
||||
if (Thread::current()->is_profiling_suppressed())
|
||||
return result;
|
||||
|
||||
auto* event_buffer = current_perf_events_buffer();
|
||||
if (event_buffer == nullptr)
|
||||
return result;
|
||||
|
||||
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
auto const duration = end_timestamp - start_timestamp.value();
|
||||
|
||||
FilesystemEvent data;
|
||||
data.type = FilesystemEventType::Readv;
|
||||
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
|
||||
data.data.readv.fd = fd;
|
||||
|
||||
if (result.is_error()) {
|
||||
data.result.is_error = true;
|
||||
data.result.value = result.error().code();
|
||||
} else {
|
||||
data.result.is_error = false;
|
||||
data.result.value = 0;
|
||||
}
|
||||
|
||||
auto maybe_path_index = get_path_index(fds(), fd, event_buffer);
|
||||
if (maybe_path_index.is_error())
|
||||
return result;
|
||||
|
||||
data.data.readv.filename_index = maybe_path_index.value();
|
||||
|
||||
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$read(int fd, Userspace<u8*> buffer, size_t size)
|
||||
{
|
||||
Optional<MonotonicTime> start_timestamp = {};
|
||||
|
||||
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
|
||||
if (profiling_enabled_at_entry) {
|
||||
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
}
|
||||
|
||||
auto result = read_impl(fd, buffer, size);
|
||||
|
||||
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
|
||||
return result;
|
||||
|
||||
auto* event_buffer = current_perf_events_buffer();
|
||||
if (event_buffer == nullptr)
|
||||
return result;
|
||||
|
||||
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
auto const duration = end_timestamp - start_timestamp.value();
|
||||
|
||||
FilesystemEvent data;
|
||||
data.type = FilesystemEventType::Read;
|
||||
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
|
||||
data.data.read.fd = fd;
|
||||
|
||||
if (result.is_error()) {
|
||||
data.result.is_error = true;
|
||||
data.result.value = result.error().code();
|
||||
} else {
|
||||
data.result.is_error = false;
|
||||
data.result.value = 0;
|
||||
}
|
||||
|
||||
auto maybe_path_index = get_path_index(fds(), fd, event_buffer);
|
||||
if (maybe_path_index.is_error())
|
||||
return result;
|
||||
|
||||
data.data.read.filename_index = maybe_path_index.value();
|
||||
|
||||
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$pread(int fd, Userspace<u8*> buffer, size_t size, off_t userspace_offset)
|
||||
{
|
||||
Optional<MonotonicTime> start_timestamp = {};
|
||||
|
||||
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
|
||||
if (profiling_enabled_at_entry) {
|
||||
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
}
|
||||
|
||||
auto result = pread_impl(fd, buffer, size, userspace_offset);
|
||||
|
||||
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
|
||||
return result;
|
||||
|
||||
auto* event_buffer = current_perf_events_buffer();
|
||||
if (event_buffer == nullptr)
|
||||
return result;
|
||||
|
||||
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
|
||||
auto const duration = end_timestamp - start_timestamp.value();
|
||||
|
||||
FilesystemEvent data;
|
||||
data.type = FilesystemEventType::Pread;
|
||||
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
|
||||
data.data.pread.fd = fd;
|
||||
data.data.pread.buffer_ptr = buffer.ptr();
|
||||
data.data.pread.size = size;
|
||||
data.data.pread.offset = userspace_offset;
|
||||
|
||||
if (result.is_error()) {
|
||||
data.result.is_error = true;
|
||||
data.result.value = result.error().code();
|
||||
} else {
|
||||
data.result.is_error = false;
|
||||
data.result.value = 0;
|
||||
}
|
||||
|
||||
auto maybe_path_index = get_path_index(fds(), fd, event_buffer);
|
||||
if (maybe_path_index.is_error())
|
||||
return result;
|
||||
|
||||
data.data.pread.filename_index = maybe_path_index.value();
|
||||
|
||||
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
|
@ -13,7 +13,7 @@ namespace Kernel {
|
|||
|
||||
using BlockFlags = Thread::FileBlocker::BlockFlags;
|
||||
|
||||
static ErrorOr<NonnullRefPtr<OpenFileDescription>> open_readable_file_description(auto& fds, int fd)
|
||||
ErrorOr<NonnullRefPtr<OpenFileDescription>> open_readable_file_description(auto& fds, int fd)
|
||||
{
|
||||
auto description = TRY(fds.with_shared([&](auto& fds) { return fds.open_file_description(fd); }));
|
||||
if (!description->is_readable())
|
||||
|
@ -38,7 +38,7 @@ static ErrorOr<void> check_blocked_read(OpenFileDescription* description)
|
|||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
||||
ErrorOr<FlatPtr> Process::readv_impl(int fd, Userspace<const struct iovec*> iov, int iov_count)
|
||||
{
|
||||
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
||||
TRY(require_promise(Pledge::stdio));
|
||||
|
@ -71,20 +71,6 @@ ErrorOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov,
|
|||
return nread;
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$read(int fd, Userspace<u8*> buffer, size_t size)
|
||||
{
|
||||
auto const start_timestamp = TimeManagement::the().uptime_ms();
|
||||
auto result = read_impl(fd, buffer, size);
|
||||
|
||||
if (Thread::current()->is_profiling_suppressed())
|
||||
return result;
|
||||
|
||||
auto description = TRY(open_readable_file_description(fds(), fd));
|
||||
PerformanceManager::add_read_event(*Thread::current(), fd, size, description, start_timestamp, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::read_impl(int fd, Userspace<u8*> buffer, size_t size)
|
||||
{
|
||||
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
||||
|
@ -101,7 +87,7 @@ ErrorOr<FlatPtr> Process::read_impl(int fd, Userspace<u8*> buffer, size_t size)
|
|||
return TRY(description->read(user_buffer, size));
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$pread(int fd, Userspace<u8*> buffer, size_t size, off_t offset)
|
||||
ErrorOr<FlatPtr> Process::pread_impl(int fd, Userspace<u8*> buffer, size_t size, off_t offset)
|
||||
{
|
||||
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
||||
TRY(require_promise(Pledge::stdio));
|
||||
|
|
|
@ -24,10 +24,10 @@ PerformanceEventBuffer::PerformanceEventBuffer(NonnullOwnPtr<KBuffer> buffer)
|
|||
{
|
||||
}
|
||||
|
||||
NEVER_INLINE ErrorOr<void> PerformanceEventBuffer::append(int type, FlatPtr arg1, FlatPtr arg2, StringView arg3, Thread* current_thread, FlatPtr arg4, u64 arg5, ErrorOr<FlatPtr> const& arg6)
|
||||
NEVER_INLINE ErrorOr<void> PerformanceEventBuffer::append(int type, FlatPtr arg1, FlatPtr arg2, StringView arg3, Thread* current_thread, FilesystemEvent filesystem_event)
|
||||
{
|
||||
FlatPtr base_pointer = (FlatPtr)__builtin_frame_address(0);
|
||||
return append_with_ip_and_bp(current_thread->pid(), current_thread->tid(), 0, base_pointer, type, 0, arg1, arg2, arg3, arg4, arg5, arg6);
|
||||
return append_with_ip_and_bp(current_thread->pid(), current_thread->tid(), 0, base_pointer, type, 0, arg1, arg2, arg3, filesystem_event);
|
||||
}
|
||||
|
||||
static Vector<FlatPtr, PerformanceEvent::max_stack_frame_count> raw_backtrace(FlatPtr bp, FlatPtr ip)
|
||||
|
@ -68,13 +68,13 @@ static Vector<FlatPtr, PerformanceEvent::max_stack_frame_count> raw_backtrace(Fl
|
|||
}
|
||||
|
||||
ErrorOr<void> PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID tid, RegisterState const& regs,
|
||||
int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FlatPtr arg4, u64 arg5, ErrorOr<FlatPtr> const& arg6)
|
||||
int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FilesystemEvent filesystem_event)
|
||||
{
|
||||
return append_with_ip_and_bp(pid, tid, regs.ip(), regs.bp(), type, lost_samples, arg1, arg2, arg3, arg4, arg5, arg6);
|
||||
return append_with_ip_and_bp(pid, tid, regs.ip(), regs.bp(), type, lost_samples, arg1, arg2, arg3, filesystem_event);
|
||||
}
|
||||
|
||||
ErrorOr<void> PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID tid,
|
||||
FlatPtr ip, FlatPtr bp, int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FlatPtr arg4, u64 arg5, ErrorOr<FlatPtr> const& arg6)
|
||||
FlatPtr ip, FlatPtr bp, int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FilesystemEvent filesystem_event)
|
||||
{
|
||||
if (count() >= capacity())
|
||||
return ENOBUFS;
|
||||
|
@ -160,12 +160,8 @@ ErrorOr<void> PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, Threa
|
|||
event.data.signpost.arg1 = arg1;
|
||||
event.data.signpost.arg2 = arg2;
|
||||
break;
|
||||
case PERF_EVENT_READ:
|
||||
event.data.read.fd = arg1;
|
||||
event.data.read.size = arg2;
|
||||
event.data.read.filename_index = arg4;
|
||||
event.data.read.start_timestamp = arg5;
|
||||
event.data.read.success = !arg6.is_error();
|
||||
case PERF_EVENT_FILESYSTEM:
|
||||
event.data.filesystem = filesystem_event;
|
||||
break;
|
||||
default:
|
||||
return EINVAL;
|
||||
|
@ -295,13 +291,51 @@ ErrorOr<void> PerformanceEventBuffer::to_json_impl(Serializer& object) const
|
|||
TRY(event_object.add("arg1"sv, event.data.signpost.arg1));
|
||||
TRY(event_object.add("arg2"sv, event.data.signpost.arg2));
|
||||
break;
|
||||
case PERF_EVENT_READ:
|
||||
TRY(event_object.add("type"sv, "read"));
|
||||
TRY(event_object.add("fd"sv, event.data.read.fd));
|
||||
TRY(event_object.add("size"sv, event.data.read.size));
|
||||
TRY(event_object.add("filename_index"sv, event.data.read.filename_index));
|
||||
TRY(event_object.add("start_timestamp"sv, event.data.read.start_timestamp));
|
||||
TRY(event_object.add("success"sv, event.data.read.success));
|
||||
case PERF_EVENT_FILESYSTEM:
|
||||
TRY(event_object.add("type"sv, "filesystem"sv));
|
||||
TRY(event_object.add("durationNs"sv, event.data.filesystem.durationNs));
|
||||
switch (event.data.filesystem.type) {
|
||||
case FilesystemEventType::Open: {
|
||||
auto const& open = event.data.filesystem.data.open;
|
||||
TRY(event_object.add("fs_event_type"sv, "open"sv));
|
||||
TRY(event_object.add("dirfd"sv, open.dirfd));
|
||||
TRY(event_object.add("filename_index"sv, open.filename_index));
|
||||
TRY(event_object.add("options"sv, open.options));
|
||||
TRY(event_object.add("mode"sv, open.mode));
|
||||
break;
|
||||
}
|
||||
case FilesystemEventType::Close: {
|
||||
auto const& close = event.data.filesystem.data.close;
|
||||
TRY(event_object.add("fs_event_type"sv, "close"sv));
|
||||
TRY(event_object.add("fd"sv, close.fd));
|
||||
TRY(event_object.add("filename_index"sv, close.filename_index));
|
||||
break;
|
||||
}
|
||||
case FilesystemEventType::Readv: {
|
||||
auto const& readv = event.data.filesystem.data.readv;
|
||||
TRY(event_object.add("fs_event_type"sv, "readv"sv));
|
||||
TRY(event_object.add("fd"sv, readv.fd));
|
||||
TRY(event_object.add("filename_index"sv, readv.filename_index));
|
||||
break;
|
||||
}
|
||||
case FilesystemEventType::Read: {
|
||||
auto const& read = event.data.filesystem.data.read;
|
||||
TRY(event_object.add("fs_event_type"sv, "read"sv));
|
||||
TRY(event_object.add("fd"sv, read.fd));
|
||||
TRY(event_object.add("filename_index"sv, read.filename_index));
|
||||
break;
|
||||
}
|
||||
case FilesystemEventType::Pread: {
|
||||
auto const& pread = event.data.filesystem.data.pread;
|
||||
TRY(event_object.add("fs_event_type"sv, "pread"sv));
|
||||
TRY(event_object.add("fd"sv, pread.fd));
|
||||
TRY(event_object.add("filename_index"sv, pread.filename_index));
|
||||
TRY(event_object.add("buffer_ptr"sv, pread.buffer_ptr));
|
||||
TRY(event_object.add("size"sv, pread.size));
|
||||
TRY(event_object.add("offset"sv, pread.offset));
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
TRY(event_object.add("pid"sv, event.pid));
|
||||
|
|
|
@ -77,6 +77,66 @@ struct [[gnu::packed]] ReadPerformanceEvent {
|
|||
bool success;
|
||||
};
|
||||
|
||||
enum class FilesystemEventType : u8 {
|
||||
Open,
|
||||
Close,
|
||||
Readv,
|
||||
Read,
|
||||
Pread
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] OpenEventData {
|
||||
int dirfd;
|
||||
size_t filename_index;
|
||||
int options;
|
||||
u64 mode;
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] CloseEventData {
|
||||
int fd;
|
||||
size_t filename_index;
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] ReadvEventData {
|
||||
int fd;
|
||||
size_t filename_index;
|
||||
// struct iovec* iov; // TODO: Implement
|
||||
// int iov_count; // TODO: Implement
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] ReadEventData {
|
||||
int fd;
|
||||
size_t filename_index;
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] PreadEventData {
|
||||
int fd;
|
||||
size_t filename_index;
|
||||
FlatPtr buffer_ptr;
|
||||
size_t size;
|
||||
off_t offset;
|
||||
};
|
||||
|
||||
// FIXME: This is a hack to make the compiler pack this struct correctly.
|
||||
struct [[gnu::packed]] PackedErrorOr {
|
||||
bool is_error;
|
||||
FlatPtr value;
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] FilesystemEvent {
|
||||
FilesystemEventType type;
|
||||
u64 durationNs;
|
||||
PackedErrorOr result;
|
||||
|
||||
union {
|
||||
OpenEventData open;
|
||||
CloseEventData close;
|
||||
ReadvEventData readv;
|
||||
ReadEventData read;
|
||||
PreadEventData pread;
|
||||
} data;
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] PerformanceEvent {
|
||||
u32 type { 0 };
|
||||
u8 stack_size { 0 };
|
||||
|
@ -96,7 +156,7 @@ struct [[gnu::packed]] PerformanceEvent {
|
|||
KMallocPerformanceEvent kmalloc;
|
||||
KFreePerformanceEvent kfree;
|
||||
SignpostPerformanceEvent signpost;
|
||||
ReadPerformanceEvent read;
|
||||
FilesystemEvent filesystem;
|
||||
} data;
|
||||
static constexpr size_t max_stack_frame_count = 64;
|
||||
FlatPtr stack[max_stack_frame_count];
|
||||
|
@ -111,11 +171,11 @@ class PerformanceEventBuffer {
|
|||
public:
|
||||
static OwnPtr<PerformanceEventBuffer> try_create_with_size(size_t buffer_size);
|
||||
|
||||
ErrorOr<void> append(int type, FlatPtr arg1, FlatPtr arg2, StringView arg3, Thread* current_thread = Thread::current(), FlatPtr arg4 = 0, u64 arg5 = 0, ErrorOr<FlatPtr> const& arg6 = 0);
|
||||
ErrorOr<void> append(int type, FlatPtr arg1, FlatPtr arg2, StringView arg3, Thread* current_thread = Thread::current(), FilesystemEvent filesystem_event = {});
|
||||
ErrorOr<void> append_with_ip_and_bp(ProcessID pid, ThreadID tid, FlatPtr eip, FlatPtr ebp,
|
||||
int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FlatPtr arg4 = 0, u64 arg5 = {}, ErrorOr<FlatPtr> const& arg6 = 0);
|
||||
int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FilesystemEvent filesystem_event = {});
|
||||
ErrorOr<void> append_with_ip_and_bp(ProcessID pid, ThreadID tid, RegisterState const& regs,
|
||||
int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FlatPtr arg4 = 0, u64 arg5 = {}, ErrorOr<FlatPtr> const& arg6 = 0);
|
||||
int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FilesystemEvent filesystem_event = {});
|
||||
|
||||
void clear()
|
||||
{
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
||||
* Copyright (c) 2023, Jakub Berkop <jakub.berkop@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -128,40 +129,6 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
static void add_read_event(Thread& thread, int fd, size_t size, OpenFileDescription const& file_description, u64 start_timestamp, ErrorOr<FlatPtr> const& result)
|
||||
{
|
||||
if (thread.is_profiling_suppressed())
|
||||
return;
|
||||
|
||||
auto* event_buffer = thread.process().current_perf_events_buffer();
|
||||
if (event_buffer == nullptr)
|
||||
return;
|
||||
|
||||
size_t filepath_string_index;
|
||||
|
||||
if (auto path = file_description.original_absolute_path(); !path.is_error()) {
|
||||
auto registered_result = event_buffer->register_string(move(path.value()));
|
||||
if (registered_result.is_error())
|
||||
return;
|
||||
filepath_string_index = registered_result.value();
|
||||
} else if (auto pseudo_path = file_description.pseudo_path(); !pseudo_path.is_error()) {
|
||||
auto registered_result = event_buffer->register_string(move(pseudo_path.value()));
|
||||
if (registered_result.is_error())
|
||||
return;
|
||||
filepath_string_index = registered_result.value();
|
||||
} else {
|
||||
auto invalid_path_string = KString::try_create("<INVALID_FILE_PATH>"sv); // TODO: Performance, unnecessary allocations.
|
||||
if (invalid_path_string.is_error())
|
||||
return;
|
||||
auto registered_result = event_buffer->register_string(move(invalid_path_string.value()));
|
||||
if (registered_result.is_error())
|
||||
return;
|
||||
filepath_string_index = registered_result.value();
|
||||
}
|
||||
|
||||
[[maybe_unused]] auto rc = event_buffer->append(PERF_EVENT_READ, fd, size, {}, &thread, filepath_string_index, start_timestamp, result); // wrong arguments
|
||||
}
|
||||
|
||||
static void timer_tick(RegisterState const& regs)
|
||||
{
|
||||
static UnixDateTime last_wakeup;
|
||||
|
|
|
@ -690,7 +690,11 @@ private:
|
|||
|
||||
ErrorOr<void> remap_range_as_stack(FlatPtr address, size_t size);
|
||||
|
||||
ErrorOr<FlatPtr> open_impl(Userspace<Syscall::SC_open_params const*>);
|
||||
ErrorOr<FlatPtr> close_impl(int fd);
|
||||
ErrorOr<FlatPtr> read_impl(int fd, Userspace<u8*> buffer, size_t size);
|
||||
ErrorOr<FlatPtr> pread_impl(int fd, Userspace<u8*>, size_t, off_t);
|
||||
ErrorOr<FlatPtr> readv_impl(int fd, Userspace<const struct iovec*> iov, int iov_count);
|
||||
|
||||
public:
|
||||
ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue