lib/sys/unix/sys/cpu.rb in sys-cpu-1.0.3 vs lib/sys/unix/sys/cpu.rb in sys-cpu-1.0.4
- old
+ new
@@ -1,9 +1,13 @@
+# frozen_string_literal: true
+
require 'ffi'
require 'rbconfig'
+# The Sys module is a name space only.
module Sys
+ # The CPU class encapsulates information about the physical CPU's on your system.
class CPU
extend FFI::Library
ffi_lib FFI::Library::LIBC
# Error raised if any of the CPU methods fail.
@@ -41,35 +45,35 @@
CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
begin
attach_function(
:sysctl,
- [:pointer, :uint, :pointer, :pointer, :pointer, :size_t],
+ %i[pointer uint pointer pointer pointer size_t],
:int
)
private_class_method :sysctl
rescue FFI::NotFoundError
# Do nothing, not supported on this platform.
end
begin
attach_function(
:sysctlbyname,
- [:string, :pointer, :pointer, :pointer, :size_t],
+ %i[string pointer pointer pointer size_t],
:int
)
private_class_method :sysctlbyname
rescue FFI::NotFoundError
# Do nothing, not supported on this platform.
end
# Solaris
begin
- attach_function :getloadavg, [:pointer, :int], :int
- attach_function :processor_info, [:int, :pointer], :int
+ attach_function :getloadavg, %i[pointer int], :int
+ attach_function :processor_info, %i[int pointer], :int
attach_function :sysconf, [:int], :long
- attach_function :sysinfo, [:int, :pointer, :long], :int
+ attach_function :sysinfo, %i[int pointer long], :int
private_class_method :getloadavg
private_class_method :processor_info
private_class_method :sysconf
private_class_method :sysinfo
@@ -160,11 +164,11 @@
if sysctl(mib, 2, buf, size, nil, 0) < 0
raise Error, 'sysctl function failed'
end
- buf.strip.unpack('C').first
+ buf.strip.unpack1('C')
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
@@ -180,21 +184,19 @@
size.write_int(buf.size)
if sysctl(mib, 2, buf, size, nil, 0) < 0
raise Error, 'sysctl function failed'
end
-
- buf.strip
else
buf = 0.chr * 257
if sysinfo(SI_MACHINE, buf, buf.size) < 0
raise Error, 'sysinfo function failed'
end
-
- buf.strip
end
+
+ buf.strip
end
# Returns a string indicating the cpu model.
#
def self.model
@@ -213,14 +215,12 @@
buf.strip
else
pinfo = ProcInfo.new
# Some systems start at 0, some at 1
- if processor_info(0, pinfo) < 0
- if processor_info(1, pinfo) < 0
- raise Error, 'process_info function failed'
- end
+ if processor_info(0, pinfo) < 0 && processor_info(1, pinfo) < 0
+ raise Error, 'processor_info function failed'
end
pinfo[:pi_processor_type].to_s
end
end
@@ -255,38 +255,31 @@
if sysctl(mib, 2, buf, size, nil, 0) < 0
raise Error, 'sysctl function failed'
end
- buf.unpack('I*').first / 1000000
+ buf.unpack1('I*') / 1_000_000
else
pinfo = ProcInfo.new
# Some systems start at 0, some at 1
- if processor_info(0, pinfo) < 0
- if processor_info(1, pinfo) < 0
- raise Error, 'process_info function failed'
- end
+ if processor_info(0, pinfo) < 0 && processor_info(1, pinfo) < 0
+ raise Error, 'processor_info function failed'
end
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
- raise Error, 'getloadavg function failed'
- end
-
- loadavg.get_array_of_double(0, 3)
- end
+ return unless respond_to?(:getloadavg, true)
+ loadavg = FFI::MemoryPointer.new(:double, 3)
+ raise Error, 'getloadavg function failed' if getloadavg(loadavg, loadavg.size) < 0
+ loadavg.get_array_of_double(0, 3)
end
# Returns the floating point processor type.
#
# Not supported on all platforms.
@@ -294,14 +287,13 @@
def self.fpu_type
raise NoMethodError unless respond_to?(:processor_info, true)
pinfo = ProcInfo.new
- if processor_info(0, pinfo) < 0
- if processor_info(1, pinfo) < 0
- raise Error, 'process_info function failed'
- end
+ # Some start at 0, some start at 1
+ if processor_info(0, pinfo) < 0 && processor_info(1, pinfo) < 0
+ raise Error, 'processor_info function failed'
end
pinfo[:pi_fputypes].to_s
end
@@ -314,10 +306,10 @@
raise NoMethodError unless respond_to?(:processor_info, true)
pinfo = ProcInfo.new
if processor_info(num, pinfo) < 0
- raise Error, 'process_info function failed'
+ raise Error, 'processor_info function failed'
end
case pinfo[:pi_state].to_i
when P_ONLINE
'online'