PatchworkOS
Loading...
Searching...
No Matches
statistics.c
Go to the documentation of this file.
2
3#include <kernel/cpu/cpu.h>
4#include <kernel/cpu/smp.h>
5#include <kernel/fs/file.h>
6#include <kernel/fs/sysfs.h>
7#include <kernel/fs/vfs.h>
8#include <kernel/log/panic.h>
9#include <kernel/mem/pmm.h>
10#include <kernel/sched/sched.h>
11#include <kernel/sched/timer.h>
12#include <kernel/sync/lock.h>
13
14#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <sys/io.h>
18#include <sys/math.h>
19
23
25{
26 ctx->idleClocks = 0;
27 ctx->activeClocks = 0;
28 ctx->interruptClocks = 0;
29 ctx->interruptBegin = 0;
30 ctx->interruptEnd = 0;
31 lock_init(&ctx->lock);
32}
33
35{
36 (void)file; // Unused
37
38 char* string = malloc(MAX_PATH * (smp_cpu_amount() + 1));
39 if (string == NULL)
40 {
41 return ERR;
42 }
43
44 strcpy(string, "cpu idle_clocks active_clocks interrupt_clocks\n");
45 for (uint64_t i = 0; i < smp_cpu_amount(); i++)
46 {
47 cpu_t* cpu = smp_cpu(i);
49 LOCK_SCOPE(&stat->lock);
50
51 clock_t now = timer_uptime();
52 clock_t timeSinceLastEvent = now - stat->interruptEnd;
53 if (sched_is_idle(cpu))
54 {
55 stat->idleClocks += timeSinceLastEvent;
56 }
57 else
58 {
59 stat->activeClocks += timeSinceLastEvent;
60 }
61 stat->interruptEnd = now;
62
63 sprintf(&string[strlen(string)], "cpu%d %llu %llu %llu%c", cpu->id, stat->idleClocks, stat->activeClocks,
64 stat->interruptClocks, i + 1 != smp_cpu_amount() ? '\n' : '\0');
65 }
66
67 uint64_t length = strlen(string);
68 uint64_t readCount = BUFFER_READ(buffer, count, offset, string, length);
69 free(string);
70 return readCount;
71}
72
76
78{
79 (void)file; // Unused
80
81 char* string = malloc(MAX_PATH);
82 if (string == NULL)
83 {
84 return ERR;
85 }
86
87 sprintf(string, "value kib\ntotal %llu\nfree %llu\nreserved %llu", pmm_total_amount() * PAGE_SIZE / 1024,
89
90 uint64_t length = strlen(string);
91 uint64_t readCount = BUFFER_READ(buffer, count, offset, string, length);
92 free(string);
93 return readCount;
94}
95
99
101{
102 statDir = sysfs_dir_new(NULL, "stat", NULL, NULL);
103 if (statDir == NULL)
104 {
105 panic(NULL, "Failed to initialize statistics directory");
106 }
107
109 if (cpuFile == NULL)
110 {
111 panic(NULL, "Failed to create CPU statistics file");
112 }
114 if (memFile == NULL)
115 {
116 panic(NULL, "Failed to create memory statistics file");
117 }
118}
119
121{
122 (void)frame; // Unused
123
125 LOCK_SCOPE(&stat->lock);
126
127 stat->interruptBegin = timer_uptime();
128
129 clock_t timeBetweenTraps = stat->interruptBegin - stat->interruptEnd;
130 if (sched_is_idle(self))
131 {
132 stat->idleClocks += timeBetweenTraps;
133 }
134 else
135 {
136 stat->activeClocks += timeBetweenTraps;
137 }
138}
139
141{
142 (void)frame; // Unused
143
145 LOCK_SCOPE(&stat->lock);
146
147 stat->interruptEnd = timer_uptime();
148 stat->interruptClocks += stat->interruptEnd - stat->interruptBegin;
149}
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
static cpu_t * smp_cpu(cpuid_t id)
Returns a pointer to the cpu_t structure of the CPU with the given id.
Definition smp.h:77
static uint16_t smp_cpu_amount(void)
Returns the number of CPUs currently identified.
Definition smp.h:66
void statistics_init(void)
Initializes the statistics driver.
Definition statistics.c:100
void statistics_interrupt_begin(interrupt_frame_t *frame, cpu_t *self)
Called at the beginning of an interrupt.
Definition statistics.c:120
void statistics_cpu_ctx_init(statistics_cpu_ctx_t *ctx)
Initializes a per-CPU statistics context.
Definition statistics.c:24
void statistics_interrupt_end(interrupt_frame_t *frame, cpu_t *self)
Called at the end of an interrupt.
Definition statistics.c:140
#define BUFFER_READ(buffer, count, offset, src, size)
Helper macros for implementing file operations dealing with simple buffers.
Definition vfs.h:372
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:362
uint64_t pmm_free_amount(void)
Retrieves the amount of free physical memory.
Definition pmm.c:238
uint64_t pmm_reserved_amount(void)
Retrieves the amount of reserved physical memory.
Definition pmm.c:244
uint64_t pmm_total_amount(void)
Retrieves the total amount of physical memory managed by the PMM.
Definition pmm.c:232
bool sched_is_idle(cpu_t *cpu)
Checks if the CPU is idle.
Definition sched.c:149
static void lock_init(lock_t *lock)
Initializes a lock.
Definition lock.h:80
#define LOCK_SCOPE(lock)
Acquires a lock for the reminder of the current scope.
Definition lock.h:57
clock_t timer_uptime(void)
Time since boot.
Definition timer.c:73
uint64_t stat(const char *path, stat_t *stat)
System call for retrieving info about a file or directory.
Definition stat.c:9
#define PAGE_SIZE
Memory page size.
Definition proc.h:140
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static dentry_t * file
Definition log_file.c:17
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static atomic_long count
Definition main.c:9
static dentry_t * cpuFile
Definition statistics.c:21
static dentry_t * memFile
Definition statistics.c:22
static uint64_t statistics_mem_read(file_t *file, void *buffer, uint64_t count, uint64_t *offset)
Definition statistics.c:77
static uint64_t statistics_cpu_read(file_t *file, void *buffer, uint64_t count, uint64_t *offset)
Definition statistics.c:34
static dentry_t * statDir
Definition statistics.c:20
static file_ops_t cpuOps
Definition statistics.c:73
static file_ops_t memOps
Definition statistics.c:96
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int sprintf(char *_RESTRICT s, const char *_RESTRICT format,...)
Definition sprintf.c:5
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
_PUBLIC size_t strlen(const char *s)
Definition strlen.c:3
_PUBLIC char * strcpy(char *_RESTRICT s1, const char *_RESTRICT s2)
Definition strcpy.c:3
CPU structure.
Definition cpu.h:42
cpuid_t id
Definition cpu.h:43
statistics_cpu_ctx_t stat
Definition cpu.h:48
Directory entry structure.
Definition dentry.h:83
File operations structure.
Definition file.h:57
uint64_t(* read)(file_t *file, void *buffer, uint64_t count, uint64_t *offset)
Definition file.h:61
File structure.
Definition file.h:37
Trap Frame Structure.
Definition interrupt.h:42
Per-CPU statistics context.
Definition statistics.h:46
dentry_t * sysfs_dir_new(dentry_t *parent, const char *name, const inode_ops_t *inodeOps, void *private)
Create a new directory inside a mounted SysFS instance.
Definition sysfs.c:174
dentry_t * sysfs_file_new(dentry_t *parent, const char *name, const inode_ops_t *inodeOps, const file_ops_t *fileOps, void *private)
Create a new file inside a mounted SysFS instance.
Definition sysfs.c:216