Method: NewRelic::Agent::SystemInfo.parse_cpuinfo

Defined in:
lib/new_relic/agent/system_info.rb

.parse_cpuinfo(cpuinfo) ⇒ Object

[View source]

123
124
125
126
127
128
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/new_relic/agent/system_info.rb', line 123

def self.parse_cpuinfo(cpuinfo)
  # Build a hash of the form
  #   { [phys_id, core_id] => num_logical_processors_on_this_core }
  cores = Hash.new(0)
  phys_id = core_id = nil

  total_processors = 0

  cpuinfo.split("\n").map(&:strip).each do |line|
    case line
    when /^processor\s*:/
      cores[[phys_id, core_id]] += 1 if phys_id && core_id
      phys_id = core_id = nil # reset these values
      total_processors += 1
    when /^physical id\s*:(.*)/
      phys_id = $1.strip.to_i
    when /^core id\s*:(.*)/
      core_id = $1.strip.to_i
    end
  end
  cores[[phys_id, core_id]] += 1 if phys_id && core_id

  num_physical_packages = cores.keys.map(&:first).uniq.size
  num_physical_cores = cores.size
  num_logical_processors = cores.values.sum

  if num_physical_cores == 0
    num_logical_processors = total_processors

    if total_processors == 0
      # Likely a malformed file.
      num_logical_processors = nil
    end

    if total_processors == 1
      # Some older, single-core processors might not list ids,
      # so we'll just mark them all 1.
      num_physical_packages = 1
      num_physical_cores = 1
    else
      # We have no way of knowing how many packages or cores
      # we have, even though we know how many processors there are.
      num_physical_packages = nil
      num_physical_cores = nil
    end
  end

  {
    :num_physical_packages => num_physical_packages,
    :num_physical_cores => num_physical_cores,
    :num_logical_processors => num_logical_processors
  }
end