πŸ“¦ justjavac / moonbit-core-affinity

A MoonBit library for managing CPU core affinities to control thread-to-core binding.

β˜… 2 stars β‘‚ 1 forks πŸ‘ 2 watching βš–οΈ MIT License
πŸ“₯ Clone https://github.com/justjavac/moonbit-core-affinity.git
HTTPS git clone https://github.com/justjavac/moonbit-core-affinity.git
SSH git clone git@github.com:justjavac/moonbit-core-affinity.git
CLI gh repo clone justjavac/moonbit-core-affinity
θΏ·ζΈ‘ θΏ·ζΈ‘ Bump version to 0.1.1 in moon.mod.json 0bcc456 8 days ago πŸ“ History
πŸ“‚ main View all commits β†’
πŸ“ .github
πŸ“ example
πŸ“ src
πŸ“„ .gitignore
πŸ“„ LICENSE
πŸ“„ moon.mod.json
πŸ“„ README.md
πŸ“„ README.md

moonbit-core-affinity

A MoonBit library for managing CPU core affinities to control thread-to-core binding. This library enables precise control over CPU core affinity, allowing you to bind threads to specific cores. It's particularly useful for high-performance applications, real-time systems, and scenarios where you need to optimize CPU cache locality, reduce context switching overhead, or isolate workloads on dedicated cores.

Installation

Add justjavac/core_affinity to your dependencies: ``bash moon update moon add justjavac/core_affinity ` ## Usage `moonbit fn main { // Get available core IDs let core_ids = @core_affinity.get_core_ids() println("Available core IDs: \{core_ids}") // Early return if no cores are available if core_ids.length() == 0 { println("No available cores found.") return } let first_core_id = core_ids[0] println("Setting affinity to core: \{first_core_id}") let success = @core_affinity.set_for_current([first_core_id]) if not(success) { println("Failed to set affinity to core: \{first_core_id}") return } println("Successfully set affinity to core: \{first_core_id}") } ` ## Platform Support | Platform | get_core_ids() | set_for_current() | Implementation | |----------|----------------|-------------------|----------------| | Windows | βœ… | βœ… | Win32 API (GetProcessAffinityMask, SetThreadAffinityMask) | | Linux | βœ… | βœ… | POSIX (schedgetaffinity, schedsetaffinity) | | macOS | βœ… | βœ… | BSD/Darwin thread affinity APIs | | Other Unix | ⚠️ | ⚠️ | Limited support, platform-dependent | - βœ… = Full support - ⚠️ = Limited/platform-dependent support ## Examples ### Basic Usage `moonbit fn main { let cores = @core_affinity.get_core_ids() println("Available cores: \{cores}") } ` ### Single Core Binding `moonbit fn bind_to_first_core() -> Bool { let cores = @core_affinity.get_core_ids() if cores.length() > 0 { @core_affinity.set_for_current([cores[0]]) } else { false } } ` ### Multi-Core Binding `moonbit fn bind_to_even_cores() -> Bool { let cores = @core_affinity.get_core_ids() let even_cores = [] for core in cores { if core % 2 == 0 { even_cores.push(core) } } if even_cores.length() > 0 { @core_affinity.set_for_current(even_cores) } else { false } } ` ## Examples You can find example usage in the example/ directory: `bash moon run --target native -C example . ``

License

MIT License - see LICENSE file for details.