prometheus-client-mmap 0.7.0.beta27 → 0.7.0.beta28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fece44b3580dfbf1c00b4339578607b3fdadb28
4
- data.tar.gz: 80f8a7ddd666a5e2bf999e212e941da44d081c04
3
+ metadata.gz: 37b02cfd13e165809e2772ab5af3d8e53ad59fa0
4
+ data.tar.gz: 2b9778993828942aef818d0ba62e690c99a7c456
5
5
  SHA512:
6
- metadata.gz: b693e1a24ff8b3b38f4013eb7b0264d9e290ac7daca9129849f32ed862969660f70e1055ae133918fa4611ab4de41d4c76884e33cb692f5caa9faff74b75695a
7
- data.tar.gz: 2faf1e8cee2301a393649551914bb46be411f0ed0b9a03e59a861500dc8be08505b25937fc5f41d5b5c17fcb597243a37bd325537626ef0572605b427d3760ab
6
+ metadata.gz: 43fc00711b83898298d7d60e811636bca584289e39f81ee5a82c94041596bc214265b7fefb0b152adfa14ed5176db1fd0e009069eb2ca5c626e6fa84a5e3e95b
7
+ data.tar.gz: d2019871acee358cf7d6f23f6514dbae33578e55a7e58237f698e334a64c5d9c519b8a2a83ff037aec435b72854a92a435cac3e72fb8a61158ee96049a62d934
@@ -1,8 +1,12 @@
1
1
  #include <ruby.h>
2
+ #include <ruby/intern.h>
2
3
  #include "mmap.h"
3
4
 
4
5
  VALUE MMAPED_FILE = Qnil;
5
6
 
7
+ #define START_POSITION 8
8
+
9
+ VALUE rb_hash_has_key(VALUE hash, VALUE key);
6
10
 
7
11
  VALUE method_get_double(VALUE self, VALUE index) {
8
12
  mm_ipc *i_mm;
@@ -21,7 +25,71 @@ VALUE method_get_double(VALUE self, VALUE index) {
21
25
  return DBL2NUM(tmp);
22
26
  }
23
27
 
28
+ VALUE method_add_entry(VALUE self, VALUE positions, VALUE key, VALUE value) {
29
+ Check_Type(positions, T_HASH);
30
+ Check_Type(key, T_STRING);
31
+ VALUE position = rb_hash_lookup(positions, key);
32
+ if (position != Qnil){
33
+ return position;
34
+ }
35
+
36
+
37
+ // Check_Type(value, T_FLOAT); //TODO: will float catch DOUBLE ?
38
+
39
+ mm_ipc *i_mm;
40
+ GetMmap(self, i_mm, MM_MODIFY);
41
+
42
+ if ((i_mm->t->real) < sizeof(int32_t)) {
43
+ rb_raise(rb_eRuntimeError, "mmaped file too small %ld", i_mm->t->real);
44
+ }
45
+
46
+ uint32_t used = 0;
47
+
48
+ memcpy(&used, (char *)i_mm->t->addr, sizeof(uint32_t));
49
+
50
+ if (used == 0){
51
+ used = START_POSITION;
52
+ }
53
+
54
+ long slen = RSTRING_LEN(key);
55
+ uint32_t padding_length = 8 - (slen + 4) % 8; // padding | 8 byte aligned
56
+ //TODO: check slen if its less than uint32 throw exception otherwise
57
+ uint32_t entry_length = (uint32_t)slen;
58
+ uint32_t new_used = used + sizeof(uint32_t) + entry_length + padding_length + sizeof(double);
59
+
60
+ if (i_mm->t->real < new_used) {
61
+ rb_raise(rb_eRuntimeError, "mmaped file too small %ld", i_mm->t->real);
62
+ }
63
+
64
+ //TODO: add checks if mmap can be written to
65
+
66
+ char *pos = (char *)i_mm->t->addr + used;
67
+ // rb_raise(rb_eRuntimeError, "ugh %d %ld %u ", used, i_mm->t->real, entry_length);
68
+
69
+ memcpy(pos, &entry_length, sizeof(uint32_t));
70
+ pos += sizeof(uint32_t);
71
+
72
+ memmove(pos, StringValuePtr(key), slen);
73
+ pos += slen;
74
+
75
+ memset(pos, ' ', padding_length);
76
+ pos += padding_length;
77
+
78
+ double val = NUM2DBL(value);
79
+ memcpy(pos, &val, sizeof(double));
80
+
81
+
82
+ position = INT2NUM(new_used - 8);
83
+ rb_hash_aset(positions, key, position);
84
+
85
+ memcpy((char *)i_mm->t->addr, &new_used, sizeof(uint32_t));
86
+
87
+ return position;
88
+ }
89
+
90
+
24
91
  void Init_fast_mmaped_file() {
25
92
  MMAPED_FILE = rb_define_module("FastMmapedFile");
26
93
  rb_define_method(MMAPED_FILE, "get_double", method_get_double, 1);
94
+ rb_define_method(MMAPED_FILE, "add_entry", method_add_entry, 3);
27
95
  }
Binary file
@@ -2,6 +2,7 @@ require 'prometheus/client/helper/entry_parser'
2
2
  require 'prometheus/client/helper/file_locker'
3
3
  require 'mmap'
4
4
  require 'fast_mmaped_file'
5
+ # require 'fast_mmaped_file.so'
5
6
 
6
7
  module Prometheus
7
8
  module Client
@@ -30,8 +30,6 @@ module Prometheus
30
30
  @m = m
31
31
  # @m.mlock # TODO: Ensure memory is locked to RAM
32
32
 
33
- @used = @m.used
34
-
35
33
  @positions = {}
36
34
  read_all_positions.each do |key, pos|
37
35
  @positions[key] = pos
@@ -66,23 +64,21 @@ module Prometheus
66
64
 
67
65
  private
68
66
 
69
- def init_value(key)
70
- @mutex.synchronize do
71
- return if @positions.key?(key)
72
-
73
- until @m.try_add_entry(key, 0.0) do
74
- filepath = @m.filepath
75
- size = @m.size
76
- @m.close
77
-
78
- File.truncate(filepath, size * 2)
79
- @m = Helper::MmapedFile.open(filepath)
80
- end
67
+ def ok(key)
68
+ @m.add_entry(@positions, key, 0.0)
69
+ rescue RuntimeError => e
70
+ raise e if e.message.match /.*ugh.*/
71
+ nil
72
+ end
81
73
 
82
- # Update how much space we've used.
83
- @used = @m.used
74
+ def init_value(key)
75
+ until ok(key)
76
+ filepath = @m.filepath
77
+ size = @m.size
78
+ @m.close
84
79
 
85
- @positions[key] = @used - 8
80
+ File.truncate(filepath, size * 2)
81
+ @m = Helper::MmapedFile.open(filepath)
86
82
  end
87
83
  end
88
84
 
@@ -1,5 +1,5 @@
1
1
  module Prometheus
2
2
  module Client
3
- VERSION = '0.7.0.beta27'.freeze
3
+ VERSION = '0.7.0.beta28'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prometheus-client-mmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.beta27
4
+ version: 0.7.0.beta28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Schmidt