class GitLab::Exporter::MemStats::Aggregator

Aggregates all metrics for a single PID in /proc/<pid>/smaps

Attributes

mappings[RW]
pid[RW]
totals[RW]

Public Class Methods

new(pid) click to toggle source
# File lib/gitlab_exporter/memstats.rb, line 35
def initialize(pid)
  @pid = pid
  @totals = Hash.new(0)
  @mappings = []
  @valid = true

  populate_info
end

Public Instance Methods

valid?() click to toggle source
# File lib/gitlab_exporter/memstats.rb, line 44
def valid?
  @valid
end

Private Instance Methods

consume_mapping(map_lines, totals) click to toggle source
# File lib/gitlab_exporter/memstats.rb, line 52
def consume_mapping(map_lines, totals)
  m = Mapping.new(map_lines)

  Mapping::FIELDS.each do |field|
    totals[field] += m.send(field)
  end

  m
end
create_memstats_not_available(totals) click to toggle source
# File lib/gitlab_exporter/memstats.rb, line 62
def create_memstats_not_available(totals)
  Mapping::FIELDS.each do |field|
    totals[field] += Float::NAN
  end
end
populate_info() click to toggle source
# File lib/gitlab_exporter/memstats.rb, line 68
def populate_info # rubocop:disable Metrics/MethodLength
  File.open("/proc/#{@pid}/smaps") do |smaps|
    map_lines = []

    loop do
      break if smaps.eof?

      line = smaps.readline.strip

      case line
      when /\w+:\s+/
        map_lines << line
      when /[0-9a-f]+:[0-9a-f]+\s+/
        mappings << consume_mapping(map_lines, totals) if map_lines.size.positive?

        map_lines.clear
        map_lines << line
      else
        break
      end
    end
  end
rescue StandardError => e
  puts "Error: #{e}"
  @valid = false
  create_memstats_not_available(totals)
end