The Pedigree Project 0.1
InterruptTimeAccounting.cc
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#include "pedigree/kernel/process/DeferredTimeAccounting.h"
9#include "pedigree/kernel/process/InterruptTimeAccounting.h"
10#include "pedigree/kernel/process/Thread.h"
11#include "pedigree/kernel/processor/Processor.h"
12#include "pedigree/kernel/processor/ProcessorInformation.h"
13
14InterruptTimeAccounting::InterruptTimeAccounting(bool fromUserspace)
15 : m_pThread(Processor::information().getCurrentThread()), m_bFromUserspace(fromUserspace) {
16 if (!m_pThread || !m_pThread->getParent()) {
17 m_pThread = nullptr;
18 return;
19 }
20
21 m_pThread->transitionTime(KernelTimeTransition::interrupted(m_bFromUserspace),
22 KernelTimeTransition::handler());
23}
24
25InterruptTimeAccounting::~InterruptTimeAccounting() {
26 if (!m_pThread) {
27 return;
28 }
29
30 // A userspace-origin interrupt still has an ordinary kernel return tail.
31 // Its final architecture boundary owns the Kernel -> User transition so
32 // callbacks and subsystem work are charged to the kernel.
33 if (!m_bFromUserspace) {
34 m_pThread->transitionTime(KernelTimeTransition::handler(),
35 KernelTimeTransition::resumed(false));
36 }
37}
38
40 if (thread && thread->currentTimeAccountingMode() == CpuTimeMode::Kernel) {
41 thread->transitionTimeAtInterruptReturn(CpuTimeMode::Kernel, CpuTimeMode::User);
42 }
43}
static void finishUserReturn(Thread *thread)
CpuTimeMode currentTimeAccountingMode() const
Definition Thread.cc:447
ALWAYS_INLINE void transitionTimeAtInterruptReturn(CpuTimeMode from, CpuTimeMode to)
Definition Thread.h:358
void transitionTime(CpuTimeMode from, CpuTimeMode to, bool interruptsAlreadyDisabled=false)
Definition Thread.cc:386