Class: Prometheus::Client::MmapedDict

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus/client/mmaped_dict.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m) ⇒ MmapedDict

Returns a new instance of MmapedDict.



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#mObject (readonly)

Returns the value of attribute m.



18
19
20
# File 'lib/prometheus/client/mmaped_dict.rb', line 18

def m
  @m
end

#positionsObject (readonly)

Returns the value of attribute positions.



18
19
20
# File 'lib/prometheus/client/mmaped_dict.rb', line 18

def positions
  @positions
end

#usedObject (readonly)

Returns the value of attribute used.



18
19
20
# File 'lib/prometheus/client/mmaped_dict.rb', line 18

def used
  @used
end

Class Method Details

.read_all_values(f) ⇒ Object



20
21
22
23
24
25
# 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

Instance Method Details

#closeObject



53
54
55
56
57
58
# 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

#inspectObject



60
61
62
# File 'lib/prometheus/client/mmaped_dict.rb', line 60

def inspect
  "#<#{self.class}:0x#{(object_id << 1).to_s(16)}>"
end

#pathObject



49
50
51
# File 'lib/prometheus/client/mmaped_dict.rb', line 49

def path
  @m.filepath if @m
end

#read_value(key) ⇒ Object



41
42
43
# 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) ⇒ Object



45
46
47
# File 'lib/prometheus/client/mmaped_dict.rb', line 45

def write_value(key, value)
  @m.upsert_entry(@positions, key, value)
end