lib/sys/unix/sys/cpu.rb in sys-cpu-0.7.1 vs lib/sys/unix/sys/cpu.rb in sys-cpu-0.7.2
- old
+ new
@@ -7,13 +7,10 @@
ffi_lib FFI::Library::LIBC
# Error raised if any of the CPU methods fail.
class Error < StandardError; end
- # The version of the sys-cpu library
- VERSION = '0.7.1'
-
CTL_HW = 6 # Generic hardware/cpu
HW_MACHINE = 1 # Machine class
HW_MODEL = 2 # Specific machine model
HW_NCPU = 3 # Number of CPU's
@@ -87,10 +84,14 @@
:pi_fputypes, [:char, 32],
:pi_clock, :int
)
end
+ # Returns the cpu's architecture. On most systems this will be identical
+ # to the CPU.machine method. On OpenBSD it will be identical to the CPU.model
+ # method.
+ #
def self.architecture
if respond_to?(:sysinfo, true)
buf = 0.chr * 257
if sysinfo(SI_ARCHITECTURE, buf, buf.size) < 0
@@ -129,10 +130,14 @@
buf.strip
end
end
+ # Returns the number of cpu's on your system. Note that each core on
+ # multi-core systems are counted as a cpu, e.g. one dual core cpu would
+ # return 2, not 1.
+ #
def self.num_cpu
if respond_to?(:sysctlbyname, true)
optr = FFI::MemoryPointer.new(:long)
size = FFI::MemoryPointer.new(:size_t)
@@ -165,10 +170,14 @@
buf.strip.unpack("C").first
end
end
+ # Returns the cpu's class type. On most systems this will be identical
+ # to the CPU.architecture method. On OpenBSD it will be identical to the
+ # CPU.model method.
+ #
def self.machine
if respond_to?(:sysctl, true)
buf = 0.chr * 32
mib = FFI::MemoryPointer.new(:int, 2)
size = FFI::MemoryPointer.new(:long, 1)
@@ -190,10 +199,12 @@
buf.strip
end
end
+ # Returns a string indicating the cpu model.
+ #
def self.model
if RbConfig::CONFIG['host_os'] =~ /darwin/i
ptr = FFI::MemoryPointer.new(:long)
size = FFI::MemoryPointer.new(:size_t)
@@ -240,10 +251,12 @@
pinfo[:pi_processor_type].to_s
end
end
end
+ # Returns an integer indicating the speed of the CPU.
+ #
def self.freq
if respond_to?(:sysctlbyname, true)
optr = FFI::MemoryPointer.new(:long)
size = FFI::MemoryPointer.new(:size_t)
@@ -289,10 +302,13 @@
pinfo[:pi_clock].to_i
end
end
+ # Returns an array of three floats indicating the 1, 5 and 15 minute load
+ # average.
+ #
def self.load_avg
if respond_to?(:getloadavg, true)
loadavg = FFI::MemoryPointer.new(:double, 3)
if getloadavg(loadavg, loadavg.size) < 0
@@ -301,10 +317,14 @@
loadavg.get_array_of_double(0, 3)
end
end
+ # Returns the floating point processor type.
+ #
+ # Not supported on all platforms.
+ #
def self.fpu_type
raise NoMethodError unless respond_to?(:processor_info, true)
pinfo = ProcInfo.new
@@ -315,9 +335,14 @@
end
pinfo[:pi_fputypes].to_s
end
+ # Returns the current state of processor +num+, or 0 if no number is
+ # specified.
+ #
+ # Not supported on all platforms.
+ #
def self.state(num = 0)
raise NoMethodError unless respond_to?(:processor_info, true)
pinfo = ProcInfo.new