From 95a33c484eace06bb06cf5c89a6a2cf038ebdc02 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 17 Aug 2023 20:15:32 +0200 Subject: [PATCH] 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? --- kernel/CMakeLists.txt | 1 + kernel/src/sys/memstat.cpp | 21 +++++++++++++++++++++ libc/CMakeLists.txt | 1 + libc/include/bits/memstat.h | 14 ++++++++++++++ libc/include/sys/memstat.h | 20 ++++++++++++++++++++ libc/src/sys/memstat.cpp | 13 +++++++++++++ libluna/include/luna/Syscall.h | 2 +- 7 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 kernel/src/sys/memstat.cpp create mode 100644 libc/include/bits/memstat.h create mode 100644 libc/include/sys/memstat.h create mode 100644 libc/src/sys/memstat.cpp diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index fbf11b11..e602a1c5 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -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 diff --git a/kernel/src/sys/memstat.cpp b/kernel/src/sys/memstat.cpp new file mode 100644 index 00000000..8d42a781 --- /dev/null +++ b/kernel/src/sys/memstat.cpp @@ -0,0 +1,21 @@ +#include "Pledge.h" +#include "memory/MemoryManager.h" +#include "sys/Syscall.h" +#include "thread/Scheduler.h" +#include + +Result 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; +} diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 19f4e654..aef47b36 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -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 diff --git a/libc/include/bits/memstat.h b/libc/include/bits/memstat.h new file mode 100644 index 00000000..365f011f --- /dev/null +++ b/libc/include/bits/memstat.h @@ -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 diff --git a/libc/include/sys/memstat.h b/libc/include/sys/memstat.h new file mode 100644 index 00000000..6980d259 --- /dev/null +++ b/libc/include/sys/memstat.h @@ -0,0 +1,20 @@ +/* sys/memstat.h: Memory usage querying. */ + +#ifndef _SYS_MEMSTAT_H +#define _SYS_MEMSTAT_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* Query system memory usage. */ + int memstat(struct membuf* mem); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libc/src/sys/memstat.cpp b/libc/src/sys/memstat.cpp new file mode 100644 index 00000000..2d600ac1 --- /dev/null +++ b/libc/src/sys/memstat.cpp @@ -0,0 +1,13 @@ +#include +#include +#include +#include + +extern "C" +{ + int memstat(struct membuf* buf) + { + long rc = syscall(SYS_memstat, buf); + __errno_return(rc, int); + } +} diff --git a/libluna/include/luna/Syscall.h b/libluna/include/luna/Syscall.h index d75b1ec4..55003ab4 100644 --- a/libluna/include/luna/Syscall.h +++ b/libluna/include/luna/Syscall.h @@ -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 {