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, :ppc32, :ppc7400, :ppc7450, :ppc970].freeze
PPC_64BIT_ARCHS =
[:ppc64, :ppc64le, :ppc970].freeze

Class Method Summary collapse

Class Method Details

.aes?Boolean

Returns:

  • (Boolean)

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

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

.altivec?Boolean

Returns:

  • (Boolean)

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

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

.archObject


53
54
55
56
57
58
59
60
61
62
# File 'Library/Homebrew/hardware.rb', line 53

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

.arch_32_bitObject


27
28
29
30
31
32
33
34
35
36
37
# File 'Library/Homebrew/hardware.rb', line 27

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

.arch_64_bitObject


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'Library/Homebrew/hardware.rb', line 39

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


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

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

  "-march=#{arch}"
end

.arm?Boolean

Returns:

  • (Boolean)

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

def arm?
  type == :arm
end

.avx2?Boolean

Returns:

  • (Boolean)

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

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

.avx?Boolean

Returns:

  • (Boolean)

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

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

.big_endian?Boolean

Returns:

  • (Boolean)

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

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

.bitsObject


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

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


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

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


14
15
16
# File 'Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 14

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

.extmodelObject


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

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

.familyObject


77
78
79
# File 'Library/Homebrew/hardware.rb', line 77

def family
  :dunno
end

.feature?(name) ⇒ Boolean

Returns:

  • (Boolean)

144
145
146
# File 'Library/Homebrew/hardware.rb', line 144

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

.featuresObject

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


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

def features
  []
end

.flagsObject


75
76
77
78
# File 'Library/Homebrew/extend/os/linux/hardware/cpu.rb', line 75

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

.generic_optimization_flagsObject


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

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

.intel?Boolean

Returns:

  • (Boolean)

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

def intel?
  type == :intel
end

.is_32_bit?Boolean

Returns:

  • (Boolean)

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

def is_32_bit?
  bits == 32
end

.is_64_bit?Boolean

Returns:

  • (Boolean)

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

def is_64_bit?
  bits == 64
end

.little_endian?Boolean

Returns:

  • (Boolean)

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

def little_endian?
  !big_endian?
end

.optimization_flagsObject


13
14
15
16
17
18
19
20
21
22
23
24
# File 'Library/Homebrew/hardware.rb', line 13

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

.ppc32?Boolean

Returns:

  • (Boolean)

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

def ppc32?
  ppc? && is_32_bit?
end

.ppc64?Boolean

Returns:

  • (Boolean)

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

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

.ppc64le?Boolean

Returns:

  • (Boolean)

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

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

.ppc?Boolean

Returns:

  • (Boolean)

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

def ppc?
  type == :ppc
end

.sse3?Boolean

Returns:

  • (Boolean)

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

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

.sse4?Boolean

Returns:

  • (Boolean)

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

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

.sse4_2?Boolean

Returns:

  • (Boolean)

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

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

.ssse3?Boolean

Returns:

  • (Boolean)

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

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

.typeObject

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


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

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 and #as_cmake_arch_flags.


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

def universal_archs
  [arch].extend ArchitectureListExtension
end