πŸ“¦ justjavac / moonbit-num-cpus

A Moonbit module to get the number of CPU cores available on the system.

β˜… 2 stars β‘‚ 1 forks πŸ‘ 2 watching βš–οΈ MIT License
cpumoonbitmoonbit-lang
πŸ“₯ Clone https://github.com/justjavac/moonbit-num-cpus.git
HTTPS git clone https://github.com/justjavac/moonbit-num-cpus.git
SSH git clone git@github.com:justjavac/moonbit-num-cpus.git
CLI gh repo clone justjavac/moonbit-num-cpus
θΏ·ζΈ‘ θΏ·ζΈ‘ Make functions public in pkg.generated.mbti b87a5ce 8 days ago πŸ“ History
πŸ“‚ main View all commits β†’
πŸ“ .github
πŸ“ example
πŸ“ src
πŸ“„ .gitignore
πŸ“„ LICENSE
πŸ“„ moon.mod.json
πŸ“„ README.md
πŸ“„ README.md

moonbit-num-cpus

A MoonBit module to get the number of CPU cores available on the system, supporting both logical and physical core detection.

Installation

Add justjavac/num_cpus to your dependencies:

moon update
moon add justjavac/num_cpus

Usage

fn main {
  let logical_cpus = @num_cpus.get()
  let physical_cpus = @num_cpus.get_physical()
  
  println("Logical CPU cores: \{logical_cpus}")
  println("Physical CPU cores: \{physical_cpus}")
  
  if logical_cpus > physical_cpus {
    println("Hyperthreading/SMT is enabled")
  }
}

Platform Support

PlatformLogical CPUsPhysical CPUsImplementation
Windowsβœ…βœ…Win32 API (GetSystemInfo, GetLogicalProcessorInformation)
macOSβœ…βœ…POSIX (sysconf) + BSD (sysctlbyname)
Linuxβœ…βœ…POSIX (sysconf) + /proc/cpuinfo parsing
Other Unixβœ…βš οΈPOSIX (sysconf), falls back to logical count
  • βœ… = Full support
  • ⚠️ = Limited/fallback support
  • βž– = Not applicable

Implementation Details

C FFI Architecture

The native implementation uses C FFI to call platform-specific system APIs:

// Windows
int moonbit_get_cpu_count();           // GetSystemInfo
int moonbit_get_physical_cpu_count();  // GetLogicalProcessorInformation

// Unix-like systems  
int moonbit_get_cpu_count();           // sysconf(_SC_NPROCESSORS_ONLN)
int moonbit_get_physical_cpu_count();  // Platform-specific implementations

Cross-Platform Strategy

  • Logical CPUs: Uses standardized POSIX sysconf() on Unix systems and
Win32 GetSystemInfo() on Windows
  • Physical CPUs: Uses platform-specific APIs to distinguish physical from
logical cores
  • Graceful Fallbacks: If physical core detection fails, falls back to
logical core count
  • Safety: Never returns 0; minimum return value is 1

Examples

Basic Usage

fn main {
  println("CPU cores: \{@num_cpus.get()}")
}

Comparing Logical vs Physical

fn main {
  let logical = @num_cpus.get()
  let physical = @num_cpus.get_physical()
  
  let ratio = logical.to_double() / physical.to_double()
  
  if ratio == 1.0 {
    println("No hyperthreading detected")
  } else if ratio == 2.0 {
    println("2x hyperthreading (common)")
  } else {
    println("SMT ratio: \{ratio}")
  }
}

Thread Pool Sizing

fn get_optimal_thread_count() -> Int {
  let logical = @num_cpus.get()
  let physical = @num_cpus.get_physical()
  
  // Use physical cores for CPU-bound tasks
  // Use logical cores for I/O-bound tasks
  // This is just an example - you'd implement is_cpu_bound_workload()
  physical  // Simplified example
}

License

MIT License - see LICENSE file for details.

Acknowledgments

crate
  • Uses cross-platform system APIs for accurate CPU detection
  • Built with MoonBit's C FFI capabilities