PatchworkOS  10941b4
A non-POSIX operating system.
Loading...
Searching...
No Matches
perf.h
Go to the documentation of this file.
1#pragma once
2
4#include <kernel/sync/lock.h>
5
6#include <time.h>
7
8typedef struct cpu cpu_t;
9typedef struct thread thread_t;
10
11/**
12 * @brief Performance driver.
13 * @defgroup kernel_drivers_performance Performance Driver
14 * @ingroup kernel_drivers
15 *
16 * The Performance driver is exposed in the `/dev/perf` directory. Below is an overview of the files in this
17 * directory.
18 *
19 * ## Cpu performance
20 *
21 * The `/dev/perf/cpu` file contains per-CPU performance data in the following format:
22 * ```
23 * cpu idle_clocks active_clocks interrupt_clocks
24 * %lu %lu %lu %lu
25 * %lu %lu %lu %lu
26 * ...
27 * %lu %lu %lu %lu
28 * ```
29 *
30 * ## Memory performance
31 *
32 * The `/dev/perf/mem` file contains memory performance data in the following format:
33 * ```
34 * total_pages %lu
35 * free_pages %lu
36 * used_pages %lu
37 * ```
38 *
39 * @see @ref kernel_proc "Process" for per-process performance data.
40 *
41 * @{
42 */
43
44/**
45 * @brief Per-CPU performance context.
46 * @struct perf_cpu_ctx_t
47 */
57
58/**
59 * @brief Per-Process performance context.
60 * @struct stat_process_ctx_t
61 */
62typedef struct
63{
64 _Atomic(clock_t) userClocks; ///< Total user mode CPU time used by this process.
66 kernelClocks; ///< Total kernel mode CPU time used by this process, does not include interrupt time.
67 clock_t startTime; ///< The time when the process was started.
69
70/**
71 * @brief Per-Thread performance context.
72 * @struct perf_thread_ctx_t
73 *
74 * The thread context tracks tracks the time it spends in and outside of system calls, this is then accumulated into the
75 * process performance context.
76 */
77typedef struct
78{
79 clock_t syscallBegin; ///< The time the current syscall began. Also used to "skip" time spent in interrupts.
82
83/**
84 * @brief Initializes a per-CPU performance context, must be called on the CPU that owns the context.
85 *
86 * @param ctx The context to initialize.
87 */
89
90/**
91 * @brief Initializes a per-process performance context.
92 *
93 * @param ctx The context to initialize.
94 */
96
97/**
98 * @brief Initializes a per-thread performance context.
99 *
100 * @param ctx The context to initialize.
101 */
103
104/**
105 * @brief Initializes the performance driver.
106 */
107void perf_init(void);
108
109/**
110 * @brief Called at the beginning of an interrupt to update cpu performance data.
111 *
112 * Must be called with interrupts disabled.
113 *
114 * @param self The current CPU.
115 */
116void perf_interrupt_begin(cpu_t* self);
117
118/**
119 * @brief Called at the end of an interrupt to update cpu performance data.
120 *
121 * Must be called with interrupts disabled.
122 *
123 * @param self The current CPU.
124 */
125void perf_interrupt_end(cpu_t* self);
126
127/**
128 * @brief Called at the beginning of a syscall to update process performance data.
129 */
130void perf_syscall_begin(void);
131
132/**
133 * @brief Called at the end of a syscall to update process performance data.
134 */
135void perf_syscall_end(void);
136
137/** @} */
void perf_cpu_ctx_init(perf_cpu_ctx_t *ctx)
Initializes a per-CPU performance context, must be called on the CPU that owns the context.
Definition perf.c:113
void perf_syscall_end(void)
Called at the end of a syscall to update process performance data.
Definition perf.c:234
void perf_interrupt_begin(cpu_t *self)
Called at the beginning of an interrupt to update cpu performance data.
Definition perf.c:156
void perf_interrupt_end(cpu_t *self)
Called at the end of an interrupt to update cpu performance data.
Definition perf.c:192
void perf_syscall_begin(void)
Called at the beginning of a syscall to update process performance data.
Definition perf.c:210
void perf_process_ctx_init(perf_process_ctx_t *ctx)
Initializes a per-process performance context.
Definition perf.c:123
void perf_thread_ctx_init(perf_thread_ctx_t *ctx)
Initializes a per-thread performance context.
Definition perf.c:130
void perf_init(void)
Initializes the performance driver.
Definition perf.c:136
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
CPU structure.
Definition cpu.h:122
A simple ticket lock implementation.
Definition lock.h:44
Per-CPU performance context.
Definition perf.h:49
lock_t lock
Definition perf.h:55
clock_t activeClocks
Definition perf.h:50
clock_t interruptEnd
Definition perf.h:54
clock_t idleClocks
Definition perf.h:52
clock_t interruptBegin
Definition perf.h:53
clock_t interruptClocks
Definition perf.h:51
_Atomic(clock_t) kernelClocks
Total kernel mode CPU time used by this process, does not include interrupt time.
clock_t startTime
The time when the process was started.
Definition perf.h:67
_Atomic(clock_t) userClocks
Total user mode CPU time used by this process.
Per-Thread performance context.
Definition perf.h:78
clock_t syscallBegin
The time the current syscall began. Also used to "skip" time spent in interrupts.
Definition perf.h:79
clock_t syscallEnd
Definition perf.h:80
Thread of execution structure.
Definition thread.h:56