crystal-sys-cpu
crystal-sys-cpu
A Crystal port of my sys-cpu Ruby library that exposes CPU details under System::CPU.
Status
This first Crystal port currently includes:
- Linux support via
/proc/cpuinfo,/proc/stat, and/proc/loadavg - macOS support via native libc and Mach calls, with a small
system_profilerfallback for model names - FreeBSD support via
sysctlandgetloadavg
Windows and the remaining BSD/Solaris surface from the Ruby gem are not ported yet.
Installation
Add this shard to your shard.yml:
dependencies:
system-cpu:
path: ../crystal-sys-cpu
Then require it:
require "system-cpu"
Usage
require "system-cpu"
pp System::CPU.load_avg
pp System::CPU.model
pp System::CPU.num_cpu
pp System::CPU.cpu_usage(sample_time: 0.1, samples: 2)
On Linux, System::CPU.processors returns parsed /proc/cpuinfo entries:
System::CPU.processors.each do |cpu|
cpu.members.each do |member|
puts "#{member}: #{cpu[member]}"
end
end
Processor records also support dynamic zero-argument attribute access for Linux fields:
first = System::CPU.processors.first
pp first.model_name
pp first.vendor_id
Notes
System::CPUis a module instead of a class, which fits Crystal better and avoids colliding with Crystal's existingSystemnamespace.- On macOS,
freqreturnsnilif the platform does not expose CPU frequency through the available system APIs. - On FreeBSD,
freqprefersdev.cpu.0.freqand falls back tohw.clockratewhen available.
Author
Daniel J. Berger
Repository
crystal-sys-cpu
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- about 11 hours ago
- May 10, 2026
License
MIT License
Links
Synced at
Fri, 22 May 2026 22:45:01 GMT
Languages