Skip to content
Daniel Berger edited this page Aug 20, 2024 · 15 revisions

Description

The sys-cpu library provides an interface for gathering information about your system's CPU.

Synopsis

require 'sys/cpu'
include Sys

puts CPU.architecture # CPU architecture, e.g. "i386".
puts CPU.freq         # CPU frequency.
puts CPU.load_avg     # Array containing load average for 1, 5 and 15 minute periods.
puts CPU.model        # CPU model.
puts CPU.machine      # CPU class. May be identical to architecture.
puts CPU.num_cpu      # Number of CPU's (cores).

OS X

The CPU.model method doesn't seem to necessarily refer to the model of the CPU so much as the overall hardware model.

Windows

The Windows version of this library supports the standard CPU methods and adds the CPU.type and CPU.processors methods. The CPU.type methods merely returns the CPU type (brand), while CPU.processors yields a struct containing more detailed information, derived from the Win32_Processor WMI class, than the standard methods do.

puts CPU.type                 # CPU brand/type.
CPU.processors{ |cpu| p cpu } # Display full information for each processor.

Linux

The methods available on Linux are mostly derived from the available information in /proc/cpuinfo. Below is some sample code to see all available information. The Linux version also adds the CPU.cpu_stats method.

p CPU.cpu_stats

CPU.processors{ |cs|
  cs.members.each{ |m|
    puts "#{m}: " + cs[m].to_s
  }
}

HP-UX

puts CPU.num_active_cpu # The number of active processors on the system.

The CPU.model and CPU.machine methods are not supported on HP-UX.

Solaris

puts CPU.state(0)     # The state of CPU 0 (the default if no arg is provided).
puts CPU.cpu_type     # The CPU architecture.
puts CPU.fpu_type     # The type of floating point unit, if any.

The CPU.architecture and CPU.machine methods are not supported on Solaris.

Note that Solaris support was dropped in version 1.1.0. It's pretty much dead at this point. If you still need it then simply install 1.0.6 instead.

Containerized Environments

This library only reports CPU information, such as the number of cores, for your host. Getting information on a per-pod basis will require the software that you used for containerization, e.g. kubectl top pod <pod-name> --namespace=NAMESPACE.

Clone this wiki locally