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

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: 37b02cfd13e165809e2772ab5af3d8e53ad59fa0
4
- data.tar.gz: 2b9778993828942aef818d0ba62e690c99a7c456
3
+ metadata.gz: 553a05d7d0b14439046cfbda874603af9b6ba6f6
4
+ data.tar.gz: a1a202b9bffe768e978aaf9eef85c526b78afd2b
5
5
  SHA512:
6
- metadata.gz: 43fc00711b83898298d7d60e811636bca584289e39f81ee5a82c94041596bc214265b7fefb0b152adfa14ed5176db1fd0e009069eb2ca5c626e6fa84a5e3e95b
7
- data.tar.gz: d2019871acee358cf7d6f23f6514dbae33578e55a7e58237f698e334a64c5d9c519b8a2a83ff037aec435b72854a92a435cac3e72fb8a61158ee96049a62d934
6
+ metadata.gz: 32f7f2f9fda00f570817ce0a0dbb47e713ef9f849a01f8c41b551e297e72a0872b94a816b346ee0635077cc33efe36504ad6be084be4f4d2ed6d3a4647f7f7eb
7
+ data.tar.gz: ab1a83e5e0be8a7d860d7dcac6b073ca21f37360e8577e0b4f9fc60789c51d22a2728e068fb2aa186884592f516c1a2932c5d8164a6032f5d3ff93e13fb1c303
@@ -1,5 +1,10 @@
1
1
  #include <ruby.h>
2
2
  #include <ruby/intern.h>
3
+ #include <sys/mman.h>
4
+ #include <unistd.h>
5
+ #include <fcntl.h>
6
+ #include <errno.h>
7
+
3
8
  #include "mmap.h"
4
9
 
5
10
  VALUE MMAPED_FILE = Qnil;
@@ -25,6 +30,40 @@ VALUE method_get_double(VALUE self, VALUE index) {
25
30
  return DBL2NUM(tmp);
26
31
  }
27
32
 
33
+ void expand(mm_ipc *i_mm, size_t len){
34
+ int fd;
35
+ if (munmap(i_mm->t->addr, i_mm->t->len)) {
36
+ rb_raise(rb_eArgError, "munmap failed");
37
+ }
38
+ if ((fd = open(i_mm->t->path, i_mm->t->smode)) == -1) {
39
+ rb_raise(rb_eArgError, "Can't open %s", i_mm->t->path);
40
+ }
41
+
42
+ if (len < i_mm->t->len) {
43
+ rb_raise(rb_eArgError, "Can't reduce the size of mmap");
44
+ }
45
+
46
+ if (lseek(fd, len - i_mm->t->len - 1, SEEK_END) == -1) {
47
+ rb_raise(rb_eIOError, "Can't lseek %lu", len - i_mm->t->len - 1);
48
+ }
49
+ if (write(fd, "\000", 1) != 1) {
50
+ rb_raise(rb_eIOError, "Can't extend %s", i_mm->t->path);
51
+ }
52
+
53
+ i_mm->t->addr = mmap(0, len, i_mm->t->pmode, i_mm->t->vscope, fd, i_mm->t->offset);
54
+ close(fd);
55
+
56
+ if (i_mm->t->addr == MAP_FAILED) {
57
+ rb_raise(rb_eArgError, "mmap failed");
58
+ }
59
+
60
+ if ((i_mm->t->flag & MM_LOCK) && mlock(i_mm->t->addr, len) == -1) {
61
+ rb_raise(rb_eArgError, "mlock(%d)", errno);
62
+ }
63
+ i_mm->t->len = len;
64
+ i_mm->t->real = len;
65
+ }
66
+
28
67
  VALUE method_add_entry(VALUE self, VALUE positions, VALUE key, VALUE value) {
29
68
  Check_Type(positions, T_HASH);
30
69
  Check_Type(key, T_STRING);
@@ -33,14 +72,11 @@ VALUE method_add_entry(VALUE self, VALUE positions, VALUE key, VALUE value) {
33
72
  return position;
34
73
  }
35
74
 
36
-
37
- // Check_Type(value, T_FLOAT); //TODO: will float catch DOUBLE ?
38
-
39
75
  mm_ipc *i_mm;
40
76
  GetMmap(self, i_mm, MM_MODIFY);
41
77
 
42
- if ((i_mm->t->real) < sizeof(int32_t)) {
43
- rb_raise(rb_eRuntimeError, "mmaped file too small %ld", i_mm->t->real);
78
+ if ((i_mm->t->len) < sizeof(int32_t)) {
79
+ expand(i_mm, sizeof(int32_t)); // TODO: better size
44
80
  }
45
81
 
46
82
  uint32_t used = 0;
@@ -57,14 +93,13 @@ VALUE method_add_entry(VALUE self, VALUE positions, VALUE key, VALUE value) {
57
93
  uint32_t entry_length = (uint32_t)slen;
58
94
  uint32_t new_used = used + sizeof(uint32_t) + entry_length + padding_length + sizeof(double);
59
95
 
60
- if (i_mm->t->real < new_used) {
61
- rb_raise(rb_eRuntimeError, "mmaped file too small %ld", i_mm->t->real);
96
+ if (i_mm->t->len < new_used) {
97
+ expand(i_mm, i_mm->t->len * 2);
62
98
  }
63
99
 
64
100
  //TODO: add checks if mmap can be written to
65
101
 
66
102
  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
103
 
69
104
  memcpy(pos, &entry_length, sizeof(uint32_t));
70
105
  pos += sizeof(uint32_t);
Binary file
@@ -2,7 +2,6 @@ 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'
6
5
 
7
6
  module Prometheus
8
7
  module Client
@@ -39,15 +39,15 @@ module Prometheus
39
39
  end
40
40
 
41
41
  def read_value(key)
42
- init_value(key) unless @positions.key?(key)
42
+ pos = @m.add_entry(@positions, key, 0.0)
43
43
 
44
- @m.get_double(@positions[key])
44
+ @m.get_double(pos)
45
45
  end
46
46
 
47
47
  def write_value(key, value)
48
- init_value(key) unless @positions.key?(key)
48
+ pos = @m.add_entry(@positions, key, 0.0)
49
49
 
50
- pos = @positions[key]
50
+ # pos = @positions[key]
51
51
  @m[pos..pos + 7] = [value].pack('d')
52
52
  end
53
53
 
@@ -64,22 +64,8 @@ module Prometheus
64
64
 
65
65
  private
66
66
 
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
73
-
74
67
  def init_value(key)
75
- until ok(key)
76
- filepath = @m.filepath
77
- size = @m.size
78
- @m.close
79
-
80
- File.truncate(filepath, size * 2)
81
- @m = Helper::MmapedFile.open(filepath)
82
- end
68
+ @m.add_entry(@positions, key, 0.0)
83
69
  end
84
70
 
85
71
  # Yield (key, pos). No locking is performed.
@@ -1,5 +1,5 @@
1
1
  module Prometheus
2
2
  module Client
3
- VERSION = '0.7.0.beta28'.freeze
3
+ VERSION = '0.7.0.beta29'.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.beta28
4
+ version: 0.7.0.beta29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Schmidt