Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kernel/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ constexpr int32_t ERR_XDEV = -15;
constexpr int32_t ERR_SPIPE = -16;
constexpr int32_t ERR_INTR = -17;
constexpr int32_t ERR_NODATA = -18;
constexpr int32_t ERR_PIPE = -19;

/**
* @brief Initialize the filesystem subsystem. Registers ramfs,
Expand Down
12 changes: 12 additions & 0 deletions kernel/mm/mm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "mm/heap.h"
#include "mm/paging.h"
#include "mm/shmem.h"
#include "sched/sched.h"
#include "sched/task.h"
#include "clock/clock.h"
#include "trace/ktrace_events.h"
#include "common/string.h"
#include "common/logging.h"
#include "hw/cache.h"
Expand Down Expand Up @@ -83,9 +87,17 @@ int32_t handle_user_pf(
return MM_CTX_ERR_INVALID_ARG;
}

uint64_t trace_start_ns = ktrace::is_recording(ktrace::EVENT_PAGE_FAULT) ? clock::now_ns() : 0;

sync::mutex_lock(mm_ctx->lock);
int32_t result = handle_user_pf_locked(mm_ctx, fault_address, pf_flags);
sync::mutex_unlock(mm_ctx->lock);

if (trace_start_ns) {
ktrace::record_page_fault(sched::current()->tid, fault_address, pf_flags,
clock::now_ns() - trace_start_ns, result);
}

return result;
}

Expand Down
4 changes: 4 additions & 0 deletions kernel/resource/providers/file_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ static int32_t map_fs_error_to_resource(int32_t fs_err) {
return ERR_BUSY;
case fs::ERR_NODATA:
return ERR_NODATA;
case fs::ERR_INTR:
return ERR_INTR;
case fs::ERR_PIPE:
return ERR_PIPE;
default:
return ERR_IO;
}
Expand Down
21 changes: 21 additions & 0 deletions kernel/sched/sched.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "hw/cpu_features.h"
#include "clock/clock.h"
#include "timer/timer.h"
#include "trace/ktrace_events.h"
#include "rc/reaper.h"
#include "exec/elf.h"
#include "random/random.h"
Expand Down Expand Up @@ -362,6 +363,18 @@ __PRIVILEGED_CODE static uint32_t load_balance_select_cpu() {
return 0;
}

static uint8_t switch_reason(bool prev_dead, bool preempted, uint32_t prev_state) {
if (prev_dead) {
return ktrace::SWITCH_REASON_EXITED;
}

if (preempted) {
return ktrace::SWITCH_REASON_PREEMPTED;
}

return prev_state == TASK_STATE_BLOCKED ? ktrace::SWITCH_REASON_BLOCKED : ktrace::SWITCH_REASON_YIELDED;
}

/**
* @note Privilege: **required**
*/
Expand Down Expand Up @@ -425,6 +438,11 @@ __PRIVILEGED_CODE task* pick_next_and_switch(task* prev, bool preempted) {
next->state.store_relaxed(TASK_STATE_RUNNING);
}

if (next != prev) {
ktrace::record_sched_switch(prev->tid, prev->name, next->tid, next->name,
switch_reason(prev_dead, preempted, prev_state));
}

// One timestamp ends prev's charge and starts next's, so no time goes uncharged
uint64_t now_ns = clock::now_ns();
charge_cpu_time(prev, now_ns);
Expand Down Expand Up @@ -563,6 +581,9 @@ __PRIVILEGED_CODE void wake(task* t) {
return;
}

task* waker = current();
ktrace::record_sched_wakeup(t->tid, t->name, task_cpu, waker->tid, waker->name);

// If queued or running, its own switch-out keeps a READY task on the runqueue
if (t->exec.on_runqueue) {
sync::spin_unlock_irqrestore(rq.lock, irq);
Expand Down
13 changes: 13 additions & 0 deletions kernel/syscall/syscall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "dynpriv/dynpriv.h"
#include "percpu/percpu.h"
#include "hw/cpu.h"
#include "clock/clock.h"
#include "trace/ktrace_events.h"
#include "common/logging.h"

constexpr uint32_t ELEVATION_CONTEXT_MASK = sched::TASK_FLAG_ELEVATED | sched::TASK_FLAG_IN_SYSCALL;
Expand Down Expand Up @@ -46,6 +48,11 @@ extern "C" __PRIVILEGED_CODE int64_t stlx_syscall_handler(
this_cpu(percpu_is_elevated) = true;
cpu::irq_enable();

uint64_t trace_start_ns = 0;
if (ktrace::is_recording(ktrace::EVENT_SYSCALL) && syscall_num != syscall::SYS_ELEVATE) {
trace_start_ns = clock::now_ns();
}

int64_t result;

if (syscall_num < syscall::MAX_SYSCALL_NUM && syscall::g_syscall_table[syscall_num]) {
Expand Down Expand Up @@ -74,6 +81,12 @@ extern "C" __PRIVILEGED_CODE int64_t stlx_syscall_handler(
}
}

if (trace_start_ns && self) {
uint32_t pid = self->group ? self->group->pid : 0;
ktrace::record_syscall(self->tid, pid, static_cast<uint32_t>(syscall_num),
clock::now_ns() - trace_start_ns, result);
}

// Return-boundary restore: dynamic runtime elevation follows the selected
// task mode once syscall handling and switch teardown are complete.
cpu::irq_disable();
Expand Down
Loading
Loading