From d527cf455f2fa05e0a1a9f6ea02e38f23a3828f4 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Sun, 14 Sep 2025 18:45:38 +0700 Subject: [PATCH] Implement reading CPU count --- src/header/unistd/sysconf/redox.rs | 24 +++++++++++++++++++++--- tests/unistd/sysconf.c | 2 ++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/header/unistd/sysconf/redox.rs b/src/header/unistd/sysconf/redox.rs index 703c924626..13273edf73 100644 --- a/src/header/unistd/sysconf/redox.rs +++ b/src/header/unistd/sysconf/redox.rs @@ -1,7 +1,12 @@ use core::convert::TryInto; +use alloc::string::String; + use crate::{ - header::{errno, limits}, + error::Errno, + fs::File, + header::{errno, fcntl, limits}, + io::Read, platform::{self, types::*, Pal, Sys}, }; @@ -56,8 +61,8 @@ pub(super) fn sysconf_impl(name: c_int) -> c_long { _SC_TTY_NAME_MAX => 32, _SC_SYMLOOP_MAX => -1, _SC_HOST_NAME_MAX => 64, - _SC_NPROCESSORS_CONF => 1, - _SC_NPROCESSORS_ONLN => 1, + _SC_NPROCESSORS_CONF => get_cpu_count().unwrap_or(None).unwrap_or(1), + _SC_NPROCESSORS_ONLN => get_cpu_count().unwrap_or(None).unwrap_or(1), _SC_SIGQUEUE_MAX => 32, _SC_REALTIME_SIGNALS => 202405, _ => { @@ -66,3 +71,16 @@ pub(super) fn sysconf_impl(name: c_int) -> c_long { } } } + +pub fn get_cpu_count() -> Result, Errno> { + let mut string = String::new(); + let mut file = File::open(c"/scheme/sys/cpu".into(), fcntl::O_RDONLY)?; + file.read_to_string(&mut string) + .map_err(|_| Errno(errno::EIO).sync())?; + + Ok(string + .lines() + .find(|line| line.starts_with("CPUs:")) + .and_then(|line| line.split(':').nth(1)) + .and_then(|num_str| num_str.trim().parse::().ok())) +} diff --git a/tests/unistd/sysconf.c b/tests/unistd/sysconf.c index ecb20a53aa..db369eee97 100644 --- a/tests/unistd/sysconf.c +++ b/tests/unistd/sysconf.c @@ -25,4 +25,6 @@ int main(void) { SC(TTY_NAME_MAX); SC(SYMLOOP_MAX); SC(HOST_NAME_MAX); + SC(NPROCESSORS_CONF); + SC(NPROCESSORS_ONLN); }