crystal-sys-cpu

A Crystal interface for gathering CPU information

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_profiler fallback for model names
  • FreeBSD support via sysctl and getloadavg

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::CPU is a module instead of a class, which fits Crystal better and avoids colliding with Crystal's existing System namespace.
  • On macOS, freq returns nil if the platform does not expose CPU frequency through the available system APIs.
  • On FreeBSD, freq prefers dev.cpu.0.freq and falls back to hw.clockrate when 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