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

Overview

Helper module for querying CPU information.

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, :ppc64le, :ppc970].freeze

Class Method Summary collapse

Class Method Details

.aes?Boolean

Returns:

  • (Boolean)

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

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

.altivec?Boolean

Returns:

  • (Boolean)

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

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

.archObject


58
59
60
61
62
63
64
65
66
67
# File 'Library/Homebrew/hardware.rb', line 58

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

.arch_32_bitObject


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

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

.arch_64_bitObject


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

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


153
154
155
156
157
# File 'Library/Homebrew/hardware.rb', line 153

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

  "-march=#{arch}"
end

.arm?Boolean

Returns:

  • (Boolean)

133
134
135
# File 'Library/Homebrew/hardware.rb', line 133

def arm?
  type == :arm
end

.avx2?Boolean

Returns:

  • (Boolean)

103
104
105
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 103

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

.avx?Boolean

Returns:

  • (Boolean)

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

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

.big_endian?Boolean

Returns:

  • (Boolean)

141
142
143
# File 'Library/Homebrew/hardware.rb', line 141

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

.bitsObject


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

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


86
87
88
89
90
91
92
# File 'Library/Homebrew/hardware.rb', line 86

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


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

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

.familyObject


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

def family
  :dunno
end

.feature?(name) ⇒ Boolean

Returns:

  • (Boolean)

149
150
151
# File 'Library/Homebrew/hardware.rb', line 149

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

.featuresObject

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


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

def features
  []
end

.flagsObject


72
73
74
75
# File 'Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 72

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

.generic_optimization_flagsObject


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

def optimization_flags
  @optimization_flags ||= {
    native:             arch_flag("native"),
    nehalem:            "-march=nehalem",
    sandybridge:        "-march=sandybridge",
    core2:              "-march=core2",
    core:               "-march=prescott",
    arm_vortex_tempest: "",
    armv6:              "-march=armv6",
    armv8:              "-march=armv8-a",
    ppc64:              "-mcpu=powerpc64",
    ppc64le:            "-mcpu=powerpc64le",
  }.freeze
end

.in_rosetta?Boolean

True when running under an Intel-based shell via Rosetta 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.

Returns:

  • (Boolean)

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

def in_rosetta?
  false
end

.intel?Boolean

Returns:

  • (Boolean)

113
114
115
# File 'Library/Homebrew/hardware.rb', line 113

def intel?
  type == :intel
end

.is_32_bit?Boolean

Returns:

  • (Boolean)

105
106
107
# File 'Library/Homebrew/hardware.rb', line 105

def is_32_bit?
  bits == 32
end

.is_64_bit?Boolean

Returns:

  • (Boolean)

109
110
111
# File 'Library/Homebrew/hardware.rb', line 109

def is_64_bit?
  bits == 64
end

.little_endian?Boolean

Returns:

  • (Boolean)

137
138
139
# File 'Library/Homebrew/hardware.rb', line 137

def little_endian?
  !big_endian?
end

.optimization_flagsObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'Library/Homebrew/hardware.rb', line 16

def optimization_flags
  @optimization_flags ||= {
    native:             arch_flag("native"),
    nehalem:            "-march=nehalem",
    sandybridge:        "-march=sandybridge",
    core2:              "-march=core2",
    core:               "-march=prescott",
    arm_vortex_tempest: "",
    armv6:              "-march=armv6",
    armv8:              "-march=armv8-a",
    ppc64:              "-mcpu=powerpc64",
    ppc64le:            "-mcpu=powerpc64le",
  }.freeze
end

.ppc32?Boolean

Returns:

  • (Boolean)

121
122
123
# File 'Library/Homebrew/hardware.rb', line 121

def ppc32?
  ppc? && is_32_bit?
end

.ppc64?Boolean

Returns:

  • (Boolean)

129
130
131
# File 'Library/Homebrew/hardware.rb', line 129

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

.ppc64le?Boolean

Returns:

  • (Boolean)

125
126
127
# File 'Library/Homebrew/hardware.rb', line 125

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

.ppc?Boolean

Returns:

  • (Boolean)

117
118
119
# File 'Library/Homebrew/hardware.rb', line 117

def ppc?
  type == :ppc
end

.sse3?Boolean

Returns:

  • (Boolean)

107
108
109
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 107

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

.sse4?Boolean

Returns:

  • (Boolean)

101
102
103
# File 'Library/Homebrew/hardware.rb', line 101

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

.sse4_2?Boolean

Returns:

  • (Boolean)

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

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

.ssse3?Boolean

Returns:

  • (Boolean)

111
112
113
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 111

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

.typeObject

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


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

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

.universal_archsObject

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


59
60
61
# File 'Library/Homebrew/extend/os/mac/hardware/cpu.rb', line 59

def universal_archs
  [arch].extend ArchitectureListExtension
end