Class: Hardware::CPU

Inherits:
Object
  • Object
show all
Defined in:
brew/Library/Homebrew/hardware.rb,
brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb,
brew/Library/Homebrew/extend/os/linux/hardware/cpu.rb

Constant Summary collapse

INTEL_32BIT_ARCHS =
[:i386].freeze
INTEL_64BIT_ARCHS =
[:x86_64].freeze
PPC_32BIT_ARCHS =
[:ppc, :ppc32, :ppc7400, :ppc7450, :ppc970].freeze
PPC_64BIT_ARCHS =
[:ppc64].freeze
OPTIMIZATION_FLAGS =
{
  native:  "-march=native",
  nehalem: "-march=nehalem",
  core2:   "-march=core2",
  core:    "-march=prescott",
  armv6:   "-march=armv6",
  armv8:   "-march=armv8-a",
}.freeze
OPTIMIZATION_FLAGS_LINUX =
{
  native:  "-march=#{native_arch}",
  nehalem: "-march=nehalem",
  core2:   "-march=core2",
  core:    "-march=prescott",
  armv6:   "-march=armv6",
  armv8:   "-march=armv8-a",
}.freeze

Class Method Summary collapse

Class Method Details

.aes?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 74

def aes?
  sysctl_bool("hw.optional.aes")
end

.altivec?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 78

def altivec?
  sysctl_bool("hw.optional.altivec")
end

.archObject



48
49
50
51
52
53
54
55
56
57
# File 'brew/Library/Homebrew/hardware.rb', line 48

def arch
  case bits
  when 32
    arch_32_bit
  when 64
    arch_64_bit
  else
    :dunno
  end
end

.arch_32_bitObject



24
25
26
27
28
29
30
31
32
33
34
# File 'brew/Library/Homebrew/hardware.rb', line 24

def arch_32_bit
  if arm?
    :arm
  elsif intel?
    :i386
  elsif ppc?
    :ppc32
  else
    :dunno
  end
end

.arch_64_bitObject



36
37
38
39
40
41
42
43
44
45
46
# File 'brew/Library/Homebrew/hardware.rb', line 36

def arch_64_bit
  if arm?
    :arm64
  elsif intel?
    :x86_64
  elsif ppc?
    :ppc64
  else
    :dunno
  end
end

.arm?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'brew/Library/Homebrew/hardware.rb', line 111

def arm?
  type == :arm
end

.avx2?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 86

def avx2?
  sysctl_bool("hw.optional.avx2_0")
end

.avx?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 82

def avx?
  sysctl_bool("hw.optional.avx1_0")
end

.bitsObject



84
85
86
87
88
89
# File 'brew/Library/Homebrew/hardware.rb', line 84

def bits
  @bits ||= case RUBY_PLATFORM
  when /x86_64/, /ppc64/, /aarch64|arm64/ then 64
  when /i\d86/, /ppc/, /arm/ then 32
  end
end

.coresObject



76
77
78
79
80
81
82
# File 'brew/Library/Homebrew/hardware.rb', line 76

def cores
  return @cores if @cores

  @cores = Utils.popen_read("getconf", "_NPROCESSORS_ONLN").chomp.to_i
  @cores = 1 unless $CHILD_STATUS.success?
  @cores
end

.cpuinfoObject



20
21
22
# File 'brew/Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 20

def cpuinfo
  @cpuinfo ||= File.read("/proc/cpuinfo")
end

.extmodelObject



70
71
72
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 70

def extmodel
  sysctl_int("machdep.cpu.extmodel")
end

.familyObject



72
73
74
# File 'brew/Library/Homebrew/hardware.rb', line 72

def family
  :dunno
end

.feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'brew/Library/Homebrew/hardware.rb', line 119

def feature?(name)
  features.include?(name)
end

.featuresObject

Compatibility with Mac method, which returns lowercase symbols instead of strings



84
85
86
# File 'brew/Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 84

def features
  []
end

.flagsObject



77
78
79
80
# File 'brew/Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 77

def flags
  @flags ||= cpuinfo[/^(flags|Features).*/, 0]&.split
  @flags ||= []
end

.intel?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'brew/Library/Homebrew/hardware.rb', line 103

def intel?
  type == :intel
end

.is_32_bit?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'brew/Library/Homebrew/hardware.rb', line 95

def is_32_bit?
  bits == 32
end

.is_64_bit?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'brew/Library/Homebrew/hardware.rb', line 99

def is_64_bit?
  bits == 64
end

.optimization_flagsObject



20
21
22
# File 'brew/Library/Homebrew/hardware.rb', line 20

def optimization_flags
  OPTIMIZATION_FLAGS
end

.ppc?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'brew/Library/Homebrew/hardware.rb', line 107

def ppc?
  type == :ppc
end

.sse3?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 90

def sse3?
  sysctl_bool("hw.optional.sse3")
end

.sse4?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'brew/Library/Homebrew/hardware.rb', line 91

def sse4?
  RUBY_PLATFORM.to_s.include?("x86_64")
end

.sse4_2?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 98

def sse4_2?
  sysctl_bool("hw.optional.sse4_2")
end

.ssse3?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 94

def ssse3?
  sysctl_bool("hw.optional.supplementalsse3")
end

.typeObject

These methods use info spewed out by sysctl. Look in <mach/machine.h> for decoding info.



10
11
12
13
14
15
16
17
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 10

def type
  case RUBY_PLATFORM
  when /x86_64/, /i\d86/ then :intel
  when /arm/, /aarch64/ then :arm
  when /ppc\d+/ then :ppc
  else :dunno
  end
end

.universal_archsObject

Returns an array that’s been extended with ArchitectureListExtension, which provides helpers like #as_arch_flags and #as_cmake_arch_flags.



50
51
52
# File 'brew/Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 50

def universal_archs
  [arch].extend ArchitectureListExtension
end