moonbit-num-cpus is a small MoonBit module for detecting the number of CPU
cores available on the current machine.
It supports:
- Logical CPU count, including SMT / hyper-threading
- Physical CPU count when the operating system can report it
- Native implementations for Windows, macOS, Linux, and other Unix-like systems
- Safe fallback behavior that never returns less than
1
moon add justjavac/num_cpus| Function | Description |
|---|---|
@num_cpus.get() |
Returns the number of logical CPU cores |
@num_cpus.get_physical() |
Returns the number of physical CPU cores, or a fallback value when exact detection is unavailable |
fn main {
let logical = @num_cpus.get()
let physical = @num_cpus.get_physical()
println("Logical CPU cores: \{logical}")
println("Physical CPU cores: \{physical}")
if logical > physical {
println("SMT or hyper-threading is likely enabled")
}
}fn worker_count() -> Int {
@num_cpus.get_physical()
}fn main {
let logical = @num_cpus.get()
let physical = @num_cpus.get_physical()
println("CPU topology: \{physical} physical / \{logical} logical")
}| Platform | Logical CPUs | Physical CPUs | Implementation |
|---|---|---|---|
| Windows | Yes | Yes | GetSystemInfo and GetLogicalProcessorInformation |
| macOS | Yes | Yes | sysconf and sysctl |
| Linux | Yes | Yes | sysconf and /proc/cpuinfo |
| Other Unix-like systems | Yes | Fallback | sysconf, with physical count falling back to logical count |
- Logical CPU detection uses native platform APIs rather than external commands.
- Physical CPU detection uses the best available system interface on each OS.
- When physical core detection is unavailable, the module falls back to the logical CPU count.
- The package is designed for the
nativetarget and declares it as the preferred target.
moon test --target native
moon check --deny-warn --target nativeMIT. See LICENSE.
Inspired by Rust's num_cpus crate.