prometheus-client-mmap 0.9.10 → 0.10.0

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
  SHA256:
3
- metadata.gz: ea7b6fe48f2d5019181dcc236a5a8d187ea5aeb7e7d8e2acc8d31bfb6bc62207
4
- data.tar.gz: 6231f6c5e44fc07b7084d4f9e816c9eb97916bb00235829349b9903af4c9621b
3
+ metadata.gz: 7f1ee3b09ba34253c97cbc24ea1423e5a9974e679fd4a0d63c596aae583440bd
4
+ data.tar.gz: 2e2712829e334fb9d4a52658247f0e3a33b553ef40c7b9d67e008ff66d035661
5
5
  SHA512:
6
- metadata.gz: 48cc3fa10e0879d90340349ccc118dbd4b3eca40ee8e513f5600f58354f089c4ac7e8598ede6a696b24aafca51f75baeb8e2f4b1967a42b32dcacb2d408c83bb
7
- data.tar.gz: 2a01473c292535c2931efdbb1b5e958389d71521cb55f25edddef6f097eddec7d5cac08e0e4bab3b85ee5757626f67930312fefce99f24d7184541a1ab07b559
6
+ metadata.gz: aa63176edd44f14614d1546fc9d3c467177e061182dc36594c3c23bcb074a6470e7b441bd36a72538ad41866d00741c7afd580a2131500eb72954e3c3458f084
7
+ data.tar.gz: 8962a9d8d7df151b4645234291fb1a2f75f1caf9a99a9d5d755c7078cbb5705850864084cd0af5330dd2b346a5d2451000035a5e88798d5391bc06f37e26bab2
@@ -170,6 +170,35 @@ VALUE mm_s_alloc(VALUE obj) {
170
170
  return res;
171
171
  }
172
172
 
173
+ size_t next_page_boundary(size_t value) {
174
+ size_t page_size = sysconf(_SC_PAGESIZE);
175
+
176
+ while (page_size < value) {
177
+ page_size *= 2;
178
+ }
179
+
180
+ return page_size;
181
+ }
182
+
183
+ /* Reference implementations:
184
+ * mozilla: https://hg.mozilla.org/mozilla-central/file/3d846420a907/xpcom/glue/FileUtils.cpp#l71
185
+ * glibc: https://github.com/lattera/glibc/blob/master/sysdeps/posix/posix_fallocate.c
186
+ */
187
+ int reserve_mmap_file_bytes(int fd, size_t size) {
188
+ #if __linux__
189
+ /* From https://stackoverflow.com/a/22820221: The difference with
190
+ * ftruncate(2) is that (on file systems supporting it, e.g. Ext4)
191
+ * disk space is indeed reserved by posix_fallocate but ftruncate
192
+ * extends the file by adding holes (and without reserving disk
193
+ * space). */
194
+ return posix_fallocate(fd, 0, size);
195
+ #else
196
+ /* We simplify the reference implemnetations since we generally
197
+ * don't need to reserve more than a page size. */
198
+ return ftruncate(fd, size);
199
+ #endif
200
+ }
201
+
173
202
  VALUE mm_init(VALUE obj, VALUE fname) {
174
203
  struct stat st;
175
204
  int fd, smode = 0, pmode = 0, vscope, perm, init;
@@ -206,6 +235,7 @@ VALUE mm_init(VALUE obj, VALUE fname) {
206
235
  }
207
236
 
208
237
  if (fstat(fd, &st) == -1) {
238
+ close(fd);
209
239
  rb_raise(rb_eArgError, "Can't stat %s", path);
210
240
  }
211
241
  size = st.st_size;
@@ -215,17 +245,21 @@ VALUE mm_init(VALUE obj, VALUE fname) {
215
245
  offset = 0;
216
246
  init = 0;
217
247
 
218
- if (size == 0 && (smode & O_RDWR)) {
219
- if (lseek(fd, INITIAL_SIZE - 1, SEEK_END) == -1) {
220
- rb_raise(rb_eIOError, "Can't lseek %zu", INITIAL_SIZE - 1);
221
- }
222
- if (write(fd, "\000", 1) != 1) {
223
- rb_raise(rb_eIOError, "Can't extend %s", path);
224
- }
248
+ if (size == 0) {
225
249
  init = 1;
226
250
  size = INITIAL_SIZE;
227
251
  }
228
252
 
253
+ /* We need to ensure the underlying file descriptor is at least a page size.
254
+ * Otherwise, we could get a SIGBUS error if mmap() attempts to read or write
255
+ * past the file. */
256
+ size_t reserve_size = next_page_boundary(size);
257
+
258
+ if (reserve_mmap_file_bytes(fd, reserve_size) != 0) {
259
+ close(fd);
260
+ rb_raise(rb_eIOError, "Can't reserve %zu bytes for memory-mapped file", reserve_size);
261
+ }
262
+
229
263
  addr = mmap(0, size, pmode, vscope, fd, offset);
230
264
 
231
265
  if (addr == MAP_FAILED || !addr) {
Binary file
@@ -1,5 +1,5 @@
1
1
  module Prometheus
2
2
  module Client
3
- VERSION = '0.9.10'.freeze
3
+ VERSION = '0.10.0'.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.9.10
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Schmidt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-09-16 00:00:00.000000000 Z
12
+ date: 2020-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fuzzbert