Class: Hardware::CPU

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/hardware.rb,
Library/Homebrew/extend/os/mac/hardware/cpu.rb,
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, :ppc7400, :ppc7450, :ppc970].freeze
PPC_64BIT_ARCHS =
[:ppc64].freeze
OPTIMIZATION_FLAGS =
{
  core2: "-march=core2",
  core: "-march=prescott",
  dunno: "-march=native",
}.freeze
PPC_OPTIMIZATION_FLAGS =
{
  g3: "-mcpu=750",
  g4: "-mcpu=7400",
  g4e: "-mcpu=7450",
  g5: "-mcpu=970",
  g5_64: "-mcpu=970 -arch ppc64",
}.freeze

Class Method Summary collapse

Class Method Details

.aes?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 116

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

.altivec?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 120

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

.archObject



45
46
47
48
49
50
51
52
53
54
# File 'Library/Homebrew/hardware.rb', line 45

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

.arch_32_bitObject



21
22
23
24
25
26
27
28
29
30
31
# File 'Library/Homebrew/hardware.rb', line 21

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

.arch_64_bitObject



33
34
35
36
37
38
39
40
41
42
43
# File 'Library/Homebrew/hardware.rb', line 33

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

.arm?Boolean

Returns:

  • (Boolean)


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

def arm?
  type == :arm
end

.avx2?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 128

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

.avx?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 124

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

.bitsObject



80
81
82
83
84
85
# File 'Library/Homebrew/hardware.rb', line 80

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

.can_run?(arch) ⇒ Boolean

Determines whether the current CPU and macOS combination can run an executable of the specified architecture. `arch` is a symbol in the same format returned by Hardware::CPU.family

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 98

def can_run?(arch)
  if is_32_bit?
    arch_32_bit == arch
  elsif intel?
    [:i386, :x86_64].include? arch
  elsif ppc?
    [:ppc, :ppc64].include? arch
  else
    false
  end
end

.coresObject



73
74
75
76
77
78
# File 'Library/Homebrew/hardware.rb', line 73

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



4
5
6
# File 'Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 4

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

.extmodelObject



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

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

.familyObject



69
70
71
# File 'Library/Homebrew/hardware.rb', line 69

def family
  :dunno
end

.feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'Library/Homebrew/hardware.rb', line 115

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

.featuresObject

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



67
68
69
# File 'Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 67

def features
  []
end

.flagsObject



60
61
62
63
# File 'Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 60

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

.intel?Boolean

Returns:

  • (Boolean)


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

def intel?
  type == :intel
end

.is_32_bit?Boolean

Returns:

  • (Boolean)


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

def is_32_bit?
  bits == 32
end

.is_64_bit?Boolean

Returns:

  • (Boolean)


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

def is_64_bit?
  bits == 64
end

.optimization_flagsObject



17
18
19
# File 'Library/Homebrew/hardware.rb', line 17

def optimization_flags
  OPTIMIZATION_FLAGS
end

.ppc?Boolean

Returns:

  • (Boolean)


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

def ppc?
  type == :ppc
end

.sse3?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 132

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

.sse4?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'Library/Homebrew/hardware.rb', line 87

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

.sse4_2?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 144

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

.ssse3?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 136

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.



17
18
19
20
21
22
23
24
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 17

def type
  case RUBY_PLATFORM
  when /x86_64/, /i\d86/ then :intel
  when /arm/ 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.



80
81
82
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 80

def universal_archs
  [arch].extend ArchitectureListExtension
end