num_cpus

Get the number of CPU cores available on the system.

cpu
cpus
core
processor
hardware
system
moon add justjavac/num_cpus@0.1.9
Download zip
Author
Version
0.1.9
License
MIT
Last updated
2 months ago
Downloads
34
README

#moonbit-num-cpus

CI Coverage Coverage Linux Coverage macOS Coverage Windows

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

#Installation

moon add justjavac/num_cpus

#API

FunctionDescription
@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

#Usage

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")
}
}

#Common Use Cases

#Size a thread pool

fn worker_count() -> Int {
@num_cpus.get_physical()
}

#Compare logical and physical cores

fn main {
let logical = @num_cpus.get()
let physical = @num_cpus.get_physical()
println("CPU topology: \{physical} physical / \{logical} logical")
}

#Platform Support

PlatformLogical CPUsPhysical CPUsImplementation
WindowsYesYesGetSystemInfo and GetLogicalProcessorInformation
macOSYesYessysconf and sysctl
LinuxYesYessysconf and /proc/cpuinfo
Other Unix-like systemsYesFallbacksysconf, with physical count falling back to logical count

#How It Works

  • 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 native target and declares it as the preferred target.

#Development

moon test --target native moon check --deny-warn --target native

#License

MIT. See LICENSE.

#Acknowledgments

Inspired by Rust's num_cpus crate.

#
get

fn get() -> Int

Get the number of CPU cores available on the system.

This function returns the number of logical CPU cores, which includes hyperthreading cores if available. For example, on a quad-core processor with hyperthreading, this would return 8.

The function uses FFI (Foreign Function Interface) to call native system APIs to retrieve the CPU count information.

Returns:
  • The number of logical CPU cores available on the system.

Example:
let logical_cores = @num_cpus.get()
println("Logical CPU cores: \{logical_cores}")

Note:
  • On multi-threaded systems, this includes all logical cores
  • The result is cached by the operating system and reflects the current hardware configuration
  • This function is thread-safe

#
get_physical

fn get_physical() -> Int

Get the number of physical CPU cores on the system.

This function returns the number of physical CPU cores, excluding hyperthreading. For example, on a quad-core processor with hyperthreading, this would return 4 (the actual physical cores), not 8.

The function uses FFI to call native system APIs that can distinguish between physical and logical cores.

Returns:
  • The number of physical CPU cores (not including hyperthreading).

Example:
let physical_cores = @num_cpus.get_physical()
let logical_cores = @num_cpus.get()
println("Physical cores: \{physical_cores}")
println("Logical cores: \{logical_cores}")
println("Hyperthreading ratio: \{logical_cores / physical_cores}")

Note:
  • This count excludes logical cores created by hyperthreading
  • On systems without hyperthreading, this will return the same value as get()
  • The implementation may fall back to logical core count on some platforms
  • This function is thread-safe

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io