Hardware::CPU: Implement OS-agnostic methods
This commit is contained in:
parent
1b28922628
commit
0ce7a74c58
@ -1,26 +1,13 @@
|
|||||||
module Hardware
|
module Hardware
|
||||||
class CPU
|
class CPU
|
||||||
class << self
|
class << self
|
||||||
def universal_archs
|
|
||||||
[].extend ArchitectureListExtension
|
|
||||||
end
|
|
||||||
|
|
||||||
def cpuinfo
|
def cpuinfo
|
||||||
@cpuinfo ||= File.read("/proc/cpuinfo")
|
@cpuinfo ||= File.read("/proc/cpuinfo")
|
||||||
end
|
end
|
||||||
|
|
||||||
def type
|
|
||||||
@type ||= if cpuinfo =~ /Intel|AMD/
|
|
||||||
:intel
|
|
||||||
elsif cpuinfo =~ /ARM|Marvell/
|
|
||||||
:arm
|
|
||||||
else
|
|
||||||
:dunno
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def family
|
def family
|
||||||
return :arm if arm?
|
return :arm if arm?
|
||||||
|
return :ppc if ppc?
|
||||||
return :dunno unless intel?
|
return :dunno unless intel?
|
||||||
# See https://software.intel.com/en-us/articles/intel-architecture-and-processor-identification-with-cpuid-model-and-family-numbers
|
# See https://software.intel.com/en-us/articles/intel-architecture-and-processor-identification-with-cpuid-model-and-family-numbers
|
||||||
cpu_family = cpuinfo[/^cpu family\s*: ([0-9]+)/, 1].to_i
|
cpu_family = cpuinfo[/^cpu family\s*: ([0-9]+)/, 1].to_i
|
||||||
@ -70,12 +57,9 @@ module Hardware
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cores
|
|
||||||
cpuinfo.scan(/^processor/).size
|
|
||||||
end
|
|
||||||
|
|
||||||
def flags
|
def flags
|
||||||
@flags ||= cpuinfo[/^(flags|Features).*/, 0].split
|
@flags ||= cpuinfo[/^(flags|Features).*/, 0]&.split
|
||||||
|
@flags ||= []
|
||||||
end
|
end
|
||||||
|
|
||||||
# Compatibility with Mac method, which returns lowercase symbols
|
# Compatibility with Mac method, which returns lowercase symbols
|
||||||
@ -95,12 +79,6 @@ module Hardware
|
|||||||
def sse4?
|
def sse4?
|
||||||
flags.include? "sse4_1"
|
flags.include? "sse4_1"
|
||||||
end
|
end
|
||||||
|
|
||||||
alias is_64_bit? lm?
|
|
||||||
|
|
||||||
def bits
|
|
||||||
is_64_bit? ? 64 : 32
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -75,22 +75,6 @@ module Hardware
|
|||||||
sysctl_int("machdep.cpu.extmodel")
|
sysctl_int("machdep.cpu.extmodel")
|
||||||
end
|
end
|
||||||
|
|
||||||
def cores
|
|
||||||
sysctl_int("hw.ncpu")
|
|
||||||
end
|
|
||||||
|
|
||||||
def bits
|
|
||||||
sysctl_bool("hw.cpu64bit_capable") ? 64 : 32
|
|
||||||
end
|
|
||||||
|
|
||||||
def arch_32_bit
|
|
||||||
intel? ? :i386 : :ppc
|
|
||||||
end
|
|
||||||
|
|
||||||
def arch_64_bit
|
|
||||||
intel? ? :x86_64 : :ppc64
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns an array that's been extended with ArchitectureListExtension,
|
# Returns an array that's been extended with ArchitectureListExtension,
|
||||||
# which provides helpers like #as_arch_flags and #as_cmake_arch_flags.
|
# which provides helpers like #as_arch_flags and #as_cmake_arch_flags.
|
||||||
def universal_archs
|
def universal_archs
|
||||||
|
@ -19,16 +19,48 @@ module Hardware
|
|||||||
end
|
end
|
||||||
|
|
||||||
def arch_32_bit
|
def arch_32_bit
|
||||||
:i386
|
if arm?
|
||||||
|
:arm
|
||||||
|
elsif intel?
|
||||||
|
:i386
|
||||||
|
elsif ppc?
|
||||||
|
:ppc32
|
||||||
|
else
|
||||||
|
:dunno
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def arch_64_bit
|
def arch_64_bit
|
||||||
:x86_64
|
if arm?
|
||||||
|
:arm64
|
||||||
|
elsif intel?
|
||||||
|
:x86_64
|
||||||
|
elsif ppc?
|
||||||
|
:ppc64
|
||||||
|
else
|
||||||
|
:dunno
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def arch
|
||||||
|
case bits
|
||||||
|
when 32
|
||||||
|
arch_32_bit
|
||||||
|
when 64
|
||||||
|
arch_64_bit
|
||||||
|
else
|
||||||
|
:dunno
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def universal_archs
|
||||||
|
[arch].extend ArchitectureListExtension
|
||||||
end
|
end
|
||||||
|
|
||||||
def type
|
def type
|
||||||
case RUBY_PLATFORM
|
case RUBY_PLATFORM
|
||||||
when /x86_64/, /i\d86/ then :intel
|
when /x86_64/, /i\d86/ then :intel
|
||||||
|
when /arm/ then :arm
|
||||||
when /ppc\d+/ then :ppc
|
when /ppc\d+/ then :ppc
|
||||||
else :dunno
|
else :dunno
|
||||||
end
|
end
|
||||||
@ -39,13 +71,16 @@ module Hardware
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cores
|
def cores
|
||||||
1
|
return @cores if @cores
|
||||||
|
@cores = Utils.popen_read("getconf", "_NPROCESSORS_ONLN").chomp.to_i
|
||||||
|
@cores = 1 unless $CHILD_STATUS.success?
|
||||||
|
@cores
|
||||||
end
|
end
|
||||||
|
|
||||||
def bits
|
def bits
|
||||||
case RUBY_PLATFORM
|
@bits ||= case RUBY_PLATFORM
|
||||||
when /x86_64/, /ppc64/ then 64
|
when /x86_64/, /ppc64/, /aarch64|arm64/ then 64
|
||||||
when /i\d86/, /ppc/ then 32
|
when /i\d86/, /ppc/, /arm/ then 32
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user