class Prometheus::Client::MmapedDict

A dict of doubles, backed by an mmapped file.

The file starts with a 4 byte int, indicating how much of it is used. Then 4 bytes of padding. There's then a number of entries, consisting of a 4 byte int which is the size of the next field, a utf-8 encoded string key, padding to an 8 byte alignment, and then a 8 byte float which is the value.

Attributes

m[R]
positions[R]
used[R]

Public Class Methods

new(m) click to toggle source
# File lib/prometheus/client/mmaped_dict.rb, line 27
def initialize(m)
  @mutex = Mutex.new

  @m = m
  # @m.mlock # TODO: Ensure memory is locked to RAM

  @positions = {}
  read_all_positions.each do |key, pos|
    @positions[key] = pos
  end
rescue StandardError => e
  raise ParsingError, "exception #{e} while processing metrics file #{path}"
end
read_all_values(f) click to toggle source
# File lib/prometheus/client/mmaped_dict.rb, line 20
def self.read_all_values(f)
  Helper::PlainFile.new(f).entries.map do |data, encoded_len, value_offset, _|
    encoded, value = data.unpack(format('@4A%d@%dd', encoded_len, value_offset))
    [encoded, value]
  end
end

Public Instance Methods

close() click to toggle source
# File lib/prometheus/client/mmaped_dict.rb, line 53
def close
  @m.sync
  @m.close
rescue TypeError => e
  Prometheus::Client.logger.warn("munmap raised error #{e}")
end
inspect() click to toggle source
# File lib/prometheus/client/mmaped_dict.rb, line 60
def inspect
  "#<#{self.class}:0x#{(object_id << 1).to_s(16)}>"
end
path() click to toggle source
# File lib/prometheus/client/mmaped_dict.rb, line 49
def path
  @m.filepath if @m
end
read_value(key) click to toggle source
# File lib/prometheus/client/mmaped_dict.rb, line 41
def read_value(key)
  @m.fetch_entry(@positions, key, 0.0)
end
write_value(key, value) click to toggle source
# File lib/prometheus/client/mmaped_dict.rb, line 45
def write_value(key, value)
  @m.upsert_entry(@positions, key, value)
end

Private Instance Methods

init_value(key) click to toggle source
# File lib/prometheus/client/mmaped_dict.rb, line 66
def init_value(key)
  @m.add_entry(@positions, key, 0.0)
end
read_all_positions() click to toggle source

Yield (key, pos). No locking is performed.

# File lib/prometheus/client/mmaped_dict.rb, line 71
def read_all_positions
  @m.entries.map do |data, encoded_len, _, absolute_pos|
    encoded, = data.unpack(format('@4A%d', encoded_len))
    [encoded, absolute_pos]
  end
end