Class: Hardware::CPU Private

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

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Helper module for querying CPU information.

Constant Summary collapse

INTEL_32BIT_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

[:i386].freeze
INTEL_64BIT_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

[:x86_64].freeze
INTEL_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

(INTEL_32BIT_ARCHS + INTEL_64BIT_ARCHS).freeze
PPC_32BIT_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

[:ppc, :ppc32, :ppc7400, :ppc7450, :ppc970].freeze
PPC_64BIT_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

[:ppc64, :ppc64le, :ppc970].freeze
PPC_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

(PPC_32BIT_ARCHS + PPC_64BIT_ARCHS).freeze
ARM_64BIT_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

[:arm64, :aarch64].freeze
ARM_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

ARM_64BIT_ARCHS
ALL_ARCHS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

[
  *INTEL_ARCHS,
  *PPC_ARCHS,
  *ARM_ARCHS,
].freeze
INTEL_64BIT_OLDEST_CPU =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

:core2

Class Method Summary collapse

Class Method Details

.aes?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


58
59
60
# File 'extend/os/mac/hardware/cpu.rb', line 58

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

.altivec?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


62
63
64
# File 'extend/os/mac/hardware/cpu.rb', line 62

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

.amd_family(family) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'extend/os/linux/hardware/cpu.rb', line 76

def amd_family(family)
  case family
  when 0x06
    :amd_k7
  when 0x0f
    :amd_k8
  when 0x10
    :amd_k10
  when 0x11
    :amd_k8_k10_hybrid
  when 0x12
    :amd_k12
  when 0x14
    :bobcat
  when 0x15
    :bulldozer
  when 0x16
    :jaguar
  when 0x17
    :zen
  when 0x19
    :zen3
  end
end

.archObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



73
74
75
76
77
78
79
80
81
82
# File 'hardware.rb', line 73

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

.arch_32_bitSymbol

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



46
47
48
49
50
51
52
53
54
55
56
# File 'hardware.rb', line 46

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

.arch_64_bitSymbol

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'hardware.rb', line 59

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

.arch_flag(arch) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



175
176
177
178
179
# File 'hardware.rb', line 175

def arch_flag(arch)
  return "-mcpu=#{arch}" if ppc?

  "-march=#{arch}"
end

.arm?Boolean

This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Check whether the CPU architecture is ARM.

Returns:

  • (Boolean)


151
152
153
# File 'hardware.rb', line 151

def arm?
  type == :arm
end

.avx2?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


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

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

.avx?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


66
67
68
# File 'extend/os/mac/hardware/cpu.rb', line 66

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

.big_endian?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


159
160
161
# File 'hardware.rb', line 159

def big_endian?
  [1].pack("I") == [1].pack("N")
end

.bitsObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



107
108
109
110
111
112
# File 'hardware.rb', line 107

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

.coresObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



99
100
101
102
103
104
105
# File 'hardware.rb', line 99

def cores
  return @cores if @cores

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

.extmodelObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



54
55
56
# File 'extend/os/mac/hardware/cpu.rb', line 54

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

.familySymbol

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



24
25
26
27
28
29
30
31
32
# File 'extend/os/mac/hardware/cpu.rb', line 24

def family
  if arm?
    arm_family
  elsif intel?
    intel_family
  else
    :dunno
  end
end

.feature?(name) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


171
172
173
# File 'hardware.rb', line 171

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

.featuresObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

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



109
110
111
112
113
114
115
# File 'extend/os/linux/hardware/cpu.rb', line 109

def features
  @features ||= sysctl_n(
    "machdep.cpu.features",
    "machdep.cpu.extfeatures",
    "machdep.cpu.leaf7_features",
  ).split.map { |s| s.downcase.to_sym }
end

.flagsObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Supported CPU instructions



102
103
104
105
# File 'extend/os/linux/hardware/cpu.rb', line 102

def flags
  @flags ||= cpuinfo[/^(?:flags|Features)\s*: (.*)/, 1]&.split
  @flags ||= []
end

.in_rosetta2?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

True when running under an Intel-based shell via Rosetta 2 on an Apple Silicon Mac. This can be detected via seeing if there's a conflict between what uname reports and the underlying sysctl flags, since the sysctl flags don't change behaviour under Rosetta 2.

Returns:

  • (Boolean)


38
39
40
# File 'extend/os/mac/hardware/cpu.rb', line 38

def in_rosetta2?
  sysctl_bool("sysctl.proc_translated")
end

.intel?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


127
128
129
# File 'hardware.rb', line 127

def intel?
  type == :intel
end

.intel_family(family, cpu_model) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'extend/os/mac/hardware/cpu.rb', line 129

def intel_family(_family = nil, _cpu_model = nil)
  case sysctl_int("hw.cpufamily")
  when 0x73d67300 # Yonah: Core Solo/Duo
    :core
  when 0x426f69ef # Merom: Core 2 Duo
    :core2
  when 0x78ea4fbc # Penryn
    :penryn
  when 0x6b5a4cd2 # Nehalem
    :nehalem
  when 0x573b5eec # Westmere
    :westmere
  when 0x5490b78c # Sandy Bridge
    :sandybridge
  when 0x1f65e835 # Ivy Bridge
    :ivybridge
  when 0x10b282dc # Haswell
    :haswell
  when 0x582ed09c # Broadwell
    :broadwell
  when 0x37fc219f # Skylake
    :skylake
  when 0x0f817246 # Kaby Lake
    :kabylake
  when 0x38435547 # Ice Lake
    :icelake
  when 0x1cf8a03e # Comet Lake
    :cometlake
  else
    :dunno
  end
end

.is_32_bit?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


119
120
121
# File 'hardware.rb', line 119

def is_32_bit?
  bits == 32
end

.is_64_bit?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


123
124
125
# File 'hardware.rb', line 123

def is_64_bit?
  bits == 64
end

.little_endian?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


155
156
157
# File 'hardware.rb', line 155

def little_endian?
  !big_endian?
end

.optimization_flagsObject Also known as: generic_optimization_flags

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



7
8
9
10
11
12
13
# File 'extend/os/linux/hardware/cpu.rb', line 7

def optimization_flags
  @optimization_flags ||= begin
    flags = generic_optimization_flags.dup
    flags[:native] = arch_flag(Homebrew::EnvConfig.arch)
    flags
  end
end

.physical_cpu_arm64?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Note:

This is more reliable than checking uname. sysctl returns the right answer even when running in Rosetta 2.

Returns:

  • (Boolean)


88
89
90
# File 'extend/os/mac/hardware/cpu.rb', line 88

def physical_cpu_arm64?
  sysctl_bool("hw.optional.arm64")
end

.ppc32?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


135
136
137
# File 'hardware.rb', line 135

def ppc32?
  ppc? && is_32_bit?
end

.ppc64?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


143
144
145
# File 'hardware.rb', line 143

def ppc64?
  ppc? && is_64_bit? && big_endian?
end

.ppc64le?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


139
140
141
# File 'hardware.rb', line 139

def ppc64le?
  ppc? && is_64_bit? && little_endian?
end

.ppc?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


131
132
133
# File 'hardware.rb', line 131

def ppc?
  type == :ppc
end

.sse3?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


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

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

.sse4?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


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

def sse4?
  sysctl_bool("hw.optional.sse4_1")
end

.sse4_2?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


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

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

.ssse3?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


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

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

.typeSymbol

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

These methods use info spewed out by sysctl. Look in for decoding info.

Returns:



13
14
15
16
17
18
19
20
21
22
# File 'extend/os/mac/hardware/cpu.rb', line 13

def type
  case sysctl_int("hw.cputype")
  when MachO::Headers::CPU_TYPE_I386
    :intel
  when MachO::Headers::CPU_TYPE_ARM64
    :arm
  else
    :dunno
  end
end

.virtualized?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


92
93
94
# File 'extend/os/mac/hardware/cpu.rb', line 92

def virtualized?
  sysctl_bool("kern.hv_vmm_present")
end