kernel+libc: Add a memstat() syscall

We can finally show memory usage in userspace.

This could have been done using sysfs, but I'm lazy and don't want to implement that. Maybe in the next release?
This commit is contained in:
apio 2023-08-17 20:15:32 +02:00
parent 4a654bf093
commit 95a33c484e
Signed by: apio
GPG Key ID: B8A7D06E42258954
7 changed files with 71 additions and 1 deletions

View File

@ -47,6 +47,7 @@ set(SOURCES
src/sys/poll.cpp
src/sys/alarm.cpp
src/sys/pledge.cpp
src/sys/memstat.cpp
src/fs/VFS.cpp
src/fs/Pipe.cpp
src/fs/Mount.cpp

View File

@ -0,0 +1,21 @@
#include "Pledge.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/memstat.h>
Result<u64> sys_memstat(Registers*, SyscallArgs args)
{
auto* current = Scheduler::current();
TRY(check_pledge(current, Promise::p_stdio));
struct membuf buf;
buf.mem_total = MemoryManager::total();
buf.mem_used = MemoryManager::used();
buf.mem_free = MemoryManager::free();
buf.mem_reserved = MemoryManager::reserved();
if (!MemoryManager::copy_to_user_typed((struct membuf*)args[0], &buf)) return err(EFAULT);
return 0;
}

View File

@ -33,6 +33,7 @@ set(SOURCES
src/sys/utsname.cpp
src/sys/mount.cpp
src/sys/pstat.cpp
src/sys/memstat.cpp
src/sys/resource.cpp
src/sys/socket.cpp
src/sys/poll.cpp

View File

@ -0,0 +1,14 @@
/* bits/memstat.h: The membuf structure for memstat(). */
#ifndef _BITS_MEMSTAT_H
#define _BITS_MEMSTAT_H
struct membuf
{
unsigned long mem_total;
unsigned long mem_used;
unsigned long mem_free;
unsigned long mem_reserved;
};
#endif

View File

@ -0,0 +1,20 @@
/* sys/memstat.h: Memory usage querying. */
#ifndef _SYS_MEMSTAT_H
#define _SYS_MEMSTAT_H
#include <bits/memstat.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* Query system memory usage. */
int memstat(struct membuf* mem);
#ifdef __cplusplus
}
#endif
#endif

13
libc/src/sys/memstat.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <bits/errno-return.h>
#include <sys/memstat.h>
#include <sys/syscall.h>
#include <unistd.h>
extern "C"
{
int memstat(struct membuf* buf)
{
long rc = syscall(SYS_memstat, buf);
__errno_return(rc, int);
}
}

View File

@ -8,7 +8,7 @@
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat) \
_e(pivot_root) _e(sigreturn) _e(sigaction) _e(kill) _e(sigprocmask) _e(setpgid) _e(isatty) \
_e(getpgid) _e(socket) _e(bind) _e(connect) _e(listen) _e(accept) _e(poll) _e(msync) \
_e(truncate) _e(ftruncate) _e(utimensat) _e(alarm) _e(pledge)
_e(truncate) _e(ftruncate) _e(utimensat) _e(alarm) _e(pledge) _e(memstat)
enum Syscalls
{