prometheus-client-mmap 0.20.3-x86_64-darwin
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +253 -0
- data/ext/fast_mmaped_file/extconf.rb +30 -0
- data/ext/fast_mmaped_file/fast_mmaped_file.c +122 -0
- data/ext/fast_mmaped_file/file_format.c +5 -0
- data/ext/fast_mmaped_file/file_format.h +11 -0
- data/ext/fast_mmaped_file/file_parsing.c +195 -0
- data/ext/fast_mmaped_file/file_parsing.h +27 -0
- data/ext/fast_mmaped_file/file_reading.c +102 -0
- data/ext/fast_mmaped_file/file_reading.h +30 -0
- data/ext/fast_mmaped_file/globals.h +14 -0
- data/ext/fast_mmaped_file/mmap.c +427 -0
- data/ext/fast_mmaped_file/mmap.h +61 -0
- data/ext/fast_mmaped_file/rendering.c +199 -0
- data/ext/fast_mmaped_file/rendering.h +8 -0
- data/ext/fast_mmaped_file/utils.c +56 -0
- data/ext/fast_mmaped_file/utils.h +22 -0
- data/ext/fast_mmaped_file/value_access.c +242 -0
- data/ext/fast_mmaped_file/value_access.h +15 -0
- data/ext/fast_mmaped_file_rs/.cargo/config.toml +23 -0
- data/ext/fast_mmaped_file_rs/Cargo.lock +790 -0
- data/ext/fast_mmaped_file_rs/Cargo.toml +30 -0
- data/ext/fast_mmaped_file_rs/README.md +52 -0
- data/ext/fast_mmaped_file_rs/extconf.rb +30 -0
- data/ext/fast_mmaped_file_rs/src/error.rs +174 -0
- data/ext/fast_mmaped_file_rs/src/file_entry.rs +579 -0
- data/ext/fast_mmaped_file_rs/src/file_info.rs +190 -0
- data/ext/fast_mmaped_file_rs/src/lib.rs +79 -0
- data/ext/fast_mmaped_file_rs/src/macros.rs +14 -0
- data/ext/fast_mmaped_file_rs/src/map.rs +492 -0
- data/ext/fast_mmaped_file_rs/src/mmap.rs +151 -0
- data/ext/fast_mmaped_file_rs/src/parser.rs +346 -0
- data/ext/fast_mmaped_file_rs/src/raw_entry.rs +473 -0
- data/ext/fast_mmaped_file_rs/src/testhelper.rs +222 -0
- data/ext/fast_mmaped_file_rs/src/util.rs +121 -0
- data/lib/2.7/fast_mmaped_file.bundle +0 -0
- data/lib/2.7/fast_mmaped_file_rs.bundle +0 -0
- data/lib/3.0/fast_mmaped_file.bundle +0 -0
- data/lib/3.0/fast_mmaped_file_rs.bundle +0 -0
- data/lib/3.1/fast_mmaped_file.bundle +0 -0
- data/lib/3.1/fast_mmaped_file_rs.bundle +0 -0
- data/lib/3.2/fast_mmaped_file.bundle +0 -0
- data/lib/3.2/fast_mmaped_file_rs.bundle +0 -0
- data/lib/prometheus/client/configuration.rb +23 -0
- data/lib/prometheus/client/counter.rb +27 -0
- data/lib/prometheus/client/formats/text.rb +118 -0
- data/lib/prometheus/client/gauge.rb +40 -0
- data/lib/prometheus/client/helper/entry_parser.rb +132 -0
- data/lib/prometheus/client/helper/file_locker.rb +50 -0
- data/lib/prometheus/client/helper/json_parser.rb +23 -0
- data/lib/prometheus/client/helper/metrics_processing.rb +45 -0
- data/lib/prometheus/client/helper/metrics_representation.rb +51 -0
- data/lib/prometheus/client/helper/mmaped_file.rb +64 -0
- data/lib/prometheus/client/helper/plain_file.rb +29 -0
- data/lib/prometheus/client/histogram.rb +80 -0
- data/lib/prometheus/client/label_set_validator.rb +86 -0
- data/lib/prometheus/client/metric.rb +80 -0
- data/lib/prometheus/client/mmaped_dict.rb +79 -0
- data/lib/prometheus/client/mmaped_value.rb +154 -0
- data/lib/prometheus/client/page_size.rb +17 -0
- data/lib/prometheus/client/push.rb +203 -0
- data/lib/prometheus/client/rack/collector.rb +88 -0
- data/lib/prometheus/client/rack/exporter.rb +96 -0
- data/lib/prometheus/client/registry.rb +65 -0
- data/lib/prometheus/client/simple_value.rb +31 -0
- data/lib/prometheus/client/summary.rb +69 -0
- data/lib/prometheus/client/support/unicorn.rb +35 -0
- data/lib/prometheus/client/uses_value_type.rb +20 -0
- data/lib/prometheus/client/version.rb +5 -0
- data/lib/prometheus/client.rb +58 -0
- data/lib/prometheus.rb +3 -0
- data/vendor/c/hashmap/.gitignore +52 -0
- data/vendor/c/hashmap/LICENSE +21 -0
- data/vendor/c/hashmap/README.md +90 -0
- data/vendor/c/hashmap/_config.yml +1 -0
- data/vendor/c/hashmap/src/hashmap.c +692 -0
- data/vendor/c/hashmap/src/hashmap.h +267 -0
- data/vendor/c/hashmap/test/Makefile +22 -0
- data/vendor/c/hashmap/test/hashmap_test.c +608 -0
- data/vendor/c/jsmn/.travis.yml +4 -0
- data/vendor/c/jsmn/LICENSE +20 -0
- data/vendor/c/jsmn/Makefile +41 -0
- data/vendor/c/jsmn/README.md +168 -0
- data/vendor/c/jsmn/example/jsondump.c +126 -0
- data/vendor/c/jsmn/example/simple.c +76 -0
- data/vendor/c/jsmn/jsmn.c +314 -0
- data/vendor/c/jsmn/jsmn.h +76 -0
- data/vendor/c/jsmn/library.json +16 -0
- data/vendor/c/jsmn/test/test.h +27 -0
- data/vendor/c/jsmn/test/tests.c +407 -0
- data/vendor/c/jsmn/test/testutil.h +94 -0
- metadata +243 -0
@@ -0,0 +1,199 @@
|
|
1
|
+
#include "rendering.h"
|
2
|
+
|
3
|
+
#include <float.h>
|
4
|
+
#include <jsmn.h>
|
5
|
+
|
6
|
+
#include "file_parsing.h"
|
7
|
+
#include "globals.h"
|
8
|
+
#include "utils.h"
|
9
|
+
|
10
|
+
#ifndef DBL_DECIMAL_DIG
|
11
|
+
#define DBL_DECIMAL_DIG 17
|
12
|
+
#endif
|
13
|
+
|
14
|
+
#define LABELS_START_OFFSET 4
|
15
|
+
|
16
|
+
static inline int is_valid(const jsmntok_t *token) { return token->start < token->end && token->start >= 0; }
|
17
|
+
static inline int valid_not_null(const entry_t *entry, const jsmntok_t *token) {
|
18
|
+
static const char null_s[] = "null";
|
19
|
+
|
20
|
+
if (!is_valid(token)) {
|
21
|
+
return 0;
|
22
|
+
}
|
23
|
+
|
24
|
+
if (token->type != JSMN_PRIMITIVE) {
|
25
|
+
return 1;
|
26
|
+
}
|
27
|
+
|
28
|
+
size_t token_len = token->end - token->start;
|
29
|
+
|
30
|
+
if (token_len < sizeof(null_s) - 1) {
|
31
|
+
return 1;
|
32
|
+
}
|
33
|
+
|
34
|
+
return strncmp(null_s, entry->json + token->start, sizeof(null_s) - 1) != 0;
|
35
|
+
}
|
36
|
+
|
37
|
+
static inline int append_token(VALUE string, const entry_t *entry, const jsmntok_t *token) {
|
38
|
+
if (!is_valid(token)) {
|
39
|
+
save_exception(prom_eParsingError, "parsing failed: %s", entry->json);
|
40
|
+
return 0;
|
41
|
+
}
|
42
|
+
|
43
|
+
rb_str_cat(string, entry->json + token->start, token->end - token->start);
|
44
|
+
return 1;
|
45
|
+
}
|
46
|
+
|
47
|
+
static int append_labels(VALUE string, const entry_t *entry, const int label_count, const jsmntok_t *tokens) {
|
48
|
+
if (label_count <= 0) {
|
49
|
+
if (is_pid_significant(entry)) {
|
50
|
+
rb_str_cat(string, "{pid=\"", 6);
|
51
|
+
rb_str_append(string, entry->pid);
|
52
|
+
rb_str_cat(string, "\"}", 2);
|
53
|
+
}
|
54
|
+
return 1;
|
55
|
+
}
|
56
|
+
|
57
|
+
rb_str_cat(string, "{", 1);
|
58
|
+
|
59
|
+
for (int i = 0; i < label_count; i++) {
|
60
|
+
int key = LABELS_START_OFFSET + i;
|
61
|
+
int val = LABELS_START_OFFSET + label_count + 1 + i;
|
62
|
+
|
63
|
+
if (!append_token(string, entry, &tokens[key])) {
|
64
|
+
return 0;
|
65
|
+
}
|
66
|
+
rb_str_cat(string, "=", 1);
|
67
|
+
|
68
|
+
rb_str_cat(string, "\"", 1);
|
69
|
+
if (valid_not_null(entry, &tokens[val])) {
|
70
|
+
append_token(string, entry, &tokens[val]);
|
71
|
+
}
|
72
|
+
rb_str_cat(string, "\"", 1);
|
73
|
+
|
74
|
+
if (i < label_count - 1) {
|
75
|
+
rb_str_cat(string, ",", 1);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
if (is_pid_significant(entry)) {
|
80
|
+
rb_str_cat(string, ",pid=\"", 6);
|
81
|
+
rb_str_append(string, entry->pid);
|
82
|
+
rb_str_cat(string, "\"", 1);
|
83
|
+
}
|
84
|
+
|
85
|
+
rb_str_cat(string, "}", 1);
|
86
|
+
return 1;
|
87
|
+
}
|
88
|
+
|
89
|
+
static int validate_token_count(const int token_count, const entry_t *entry) {
|
90
|
+
if (token_count < 0) {
|
91
|
+
save_exception(prom_eParsingError, "too many labels or malformed json: %s", entry->json);
|
92
|
+
return 0;
|
93
|
+
}
|
94
|
+
|
95
|
+
if (token_count < LABELS_START_OFFSET) {
|
96
|
+
save_exception(prom_eParsingError, "malformed json: %s", entry->json);
|
97
|
+
return 0;
|
98
|
+
}
|
99
|
+
|
100
|
+
if ((token_count - (LABELS_START_OFFSET + 1)) % 2 != 0) {
|
101
|
+
save_exception(prom_eParsingError, "mismatched number of labels: %s", entry->json);
|
102
|
+
return 0;
|
103
|
+
}
|
104
|
+
|
105
|
+
return 1;
|
106
|
+
}
|
107
|
+
|
108
|
+
static int append_entry(VALUE string, const entry_t *entry) {
|
109
|
+
jsmn_parser parser;
|
110
|
+
jsmn_init(&parser);
|
111
|
+
|
112
|
+
jsmntok_t tokens[200];
|
113
|
+
jsmntok_t *name_token = &tokens[2];
|
114
|
+
|
115
|
+
int token_count = jsmn_parse(&parser, entry->json, entry->json_size, tokens, sizeof(tokens) / sizeof(tokens[0]));
|
116
|
+
int label_count = (token_count - (LABELS_START_OFFSET + 1)) / 2;
|
117
|
+
|
118
|
+
//
|
119
|
+
// Example JSON "['metric', 'name',['label_a','label_b'],['value_a', 'value_b']]"
|
120
|
+
// will be parsed into following token list:
|
121
|
+
//
|
122
|
+
// [ "'metric', 'name',['label_a','label_b'],['value_a', 'value_b']",
|
123
|
+
// "metric", "name",
|
124
|
+
// "['label_a','label_b']", "label_a", "label_b",
|
125
|
+
// "['value_a', 'value_b']", "value_a", "value_b" ]
|
126
|
+
//
|
127
|
+
// where 'metric' is the name of the metric, while 'name'
|
128
|
+
// is in summaries and histograms to store names of "submetrics" like:
|
129
|
+
// histogram_name_bucket or histogram_name_sum
|
130
|
+
|
131
|
+
if (!validate_token_count(token_count, entry)) {
|
132
|
+
return 0;
|
133
|
+
}
|
134
|
+
|
135
|
+
if (!append_token(string, entry, name_token)) {
|
136
|
+
return 0;
|
137
|
+
}
|
138
|
+
|
139
|
+
if (!append_labels(string, entry, label_count, tokens)) {
|
140
|
+
return 0;
|
141
|
+
}
|
142
|
+
|
143
|
+
char value[255];
|
144
|
+
|
145
|
+
// print value with highest possible precision so that we do not lose any data
|
146
|
+
int written = snprintf(value, sizeof(value), " %.*g\n", DBL_DECIMAL_DIG, entry->value);
|
147
|
+
rb_str_cat(string, value, written);
|
148
|
+
|
149
|
+
return 1;
|
150
|
+
}
|
151
|
+
|
152
|
+
static void append_entry_head(VALUE string, const entry_t *entry) {
|
153
|
+
static const char help_beg[] = "# HELP ";
|
154
|
+
static const char help_fin[] = " Multiprocess metric\n";
|
155
|
+
|
156
|
+
rb_str_cat(string, help_beg, sizeof(help_beg) - 1);
|
157
|
+
rb_str_cat(string, entry->name, entry->name_len);
|
158
|
+
rb_str_cat(string, help_fin, sizeof(help_fin) - 1);
|
159
|
+
|
160
|
+
static const char type_beg[] = "# TYPE ";
|
161
|
+
|
162
|
+
rb_str_cat(string, type_beg, sizeof(type_beg) - 1);
|
163
|
+
rb_str_cat(string, entry->name, entry->name_len);
|
164
|
+
rb_str_cat(string, " ", 1);
|
165
|
+
rb_str_cat2(string, rb_id2name(entry->type));
|
166
|
+
rb_str_cat(string, "\n", 1);
|
167
|
+
}
|
168
|
+
|
169
|
+
static inline int entry_name_equal(const entry_t *a, const entry_t *b) {
|
170
|
+
if (a == NULL || b == NULL) {
|
171
|
+
return a == b;
|
172
|
+
}
|
173
|
+
|
174
|
+
if (a->name_len != b->name_len) {
|
175
|
+
return 0;
|
176
|
+
}
|
177
|
+
|
178
|
+
return strncmp(a->name, b->name, a->name_len) == 0;
|
179
|
+
}
|
180
|
+
|
181
|
+
int entries_to_string(VALUE string, entry_t **sorted_entries, size_t entries_count) {
|
182
|
+
entry_t *previous = NULL;
|
183
|
+
|
184
|
+
for (size_t i = 0; i < entries_count; i++) {
|
185
|
+
entry_t *entry = sorted_entries[i];
|
186
|
+
|
187
|
+
// when entry->name changes write metric header
|
188
|
+
if (!entry_name_equal(previous, entry)) {
|
189
|
+
previous = entry;
|
190
|
+
append_entry_head(string, entry);
|
191
|
+
}
|
192
|
+
|
193
|
+
if (!append_entry(string, entry)) {
|
194
|
+
return 0;
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
return 1;
|
199
|
+
}
|
@@ -0,0 +1,56 @@
|
|
1
|
+
#include "utils.h"
|
2
|
+
|
3
|
+
#include <errno.h>
|
4
|
+
|
5
|
+
static void rb_save_exception(VALUE exception, VALUE message) {
|
6
|
+
VALUE current_thread = rb_thread_current();
|
7
|
+
|
8
|
+
rb_thread_local_aset(current_thread, rb_intern("prometheus_last_exception"), exception);
|
9
|
+
rb_thread_local_aset(current_thread, rb_intern("prometheus_last_exception_message"), message);
|
10
|
+
}
|
11
|
+
/* @deprecated - use with_exception ignoring return value */
|
12
|
+
void save_exception(VALUE exception, const char *fmt, ...) {
|
13
|
+
va_list args;
|
14
|
+
|
15
|
+
va_start(args, fmt);
|
16
|
+
VALUE message = rb_vsprintf(fmt, args);
|
17
|
+
|
18
|
+
rb_save_exception(exception, message);
|
19
|
+
va_end(args);
|
20
|
+
}
|
21
|
+
|
22
|
+
int with_exception(VALUE exception, const char *fmt, ...) {
|
23
|
+
va_list args;
|
24
|
+
|
25
|
+
va_start(args, fmt);
|
26
|
+
VALUE message = rb_vsprintf(fmt, args);
|
27
|
+
|
28
|
+
rb_save_exception(exception, message);
|
29
|
+
va_end(args);
|
30
|
+
|
31
|
+
return FAILURE;
|
32
|
+
}
|
33
|
+
|
34
|
+
int with_exception_errno(VALUE exception, const char *fmt, ...) {
|
35
|
+
va_list args;
|
36
|
+
|
37
|
+
va_start(args, fmt);
|
38
|
+
VALUE message = rb_vsprintf(fmt, args);
|
39
|
+
rb_str_catf(message, " (%s)", strerror(errno));
|
40
|
+
|
41
|
+
rb_save_exception(exception, message);
|
42
|
+
va_end(args);
|
43
|
+
|
44
|
+
return FAILURE;
|
45
|
+
}
|
46
|
+
|
47
|
+
NORETURN(void raise_last_exception()) {
|
48
|
+
VALUE current_thread = rb_thread_current();
|
49
|
+
VALUE exception = rb_thread_local_aref(current_thread, rb_intern("prometheus_last_exception"));
|
50
|
+
VALUE message = rb_thread_local_aref(current_thread, rb_intern("prometheus_last_exception_message"));
|
51
|
+
if (exception != Qnil) {
|
52
|
+
rb_raise(exception, "%s", StringValueCStr(message));
|
53
|
+
} else {
|
54
|
+
rb_raise(rb_eRuntimeError, "no exception found in thread local");
|
55
|
+
}
|
56
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#ifndef UNUSED_H
|
2
|
+
#define UNUSED_H
|
3
|
+
#include <ruby.h>
|
4
|
+
|
5
|
+
#ifdef UNUSED
|
6
|
+
#elif defined(__GNUC__)
|
7
|
+
#define UNUSED(x) UNUSED_##x __attribute__((unused))
|
8
|
+
#elif defined(__LCLINT__)
|
9
|
+
#define UNUSED(x) /*@unused@*/ x
|
10
|
+
#else
|
11
|
+
#define UNUSED(x) x
|
12
|
+
#endif
|
13
|
+
|
14
|
+
#define SUCCESS 1
|
15
|
+
#define FAILURE 0
|
16
|
+
|
17
|
+
NORETURN(void raise_last_exception());
|
18
|
+
void save_exception(VALUE exception, const char *fmt, ...);
|
19
|
+
int with_exception(VALUE exception, const char *fmt, ...);
|
20
|
+
int with_exception_errno(VALUE exception, const char *fmt, ...);
|
21
|
+
|
22
|
+
#endif
|
@@ -0,0 +1,242 @@
|
|
1
|
+
#include "value_access.h"
|
2
|
+
|
3
|
+
#include <errno.h>
|
4
|
+
#include <fcntl.h>
|
5
|
+
#include <ruby/intern.h>
|
6
|
+
#include <sys/mman.h>
|
7
|
+
#include <unistd.h>
|
8
|
+
|
9
|
+
#include "file_format.h"
|
10
|
+
#include "mmap.h"
|
11
|
+
#include "utils.h"
|
12
|
+
|
13
|
+
static void close_file(mm_ipc *i_mm) {
|
14
|
+
close(i_mm->t->fd);
|
15
|
+
i_mm->t->fd = -1;
|
16
|
+
}
|
17
|
+
|
18
|
+
static int open_and_extend_file(mm_ipc *i_mm, size_t len) {
|
19
|
+
if (i_mm->t->fd < 0) {
|
20
|
+
int fd;
|
21
|
+
|
22
|
+
if ((fd = open(i_mm->t->path, i_mm->t->smode)) == -1) {
|
23
|
+
return with_exception_errno(rb_eArgError, "%s: Can't open %s", __FILE__, i_mm->t->path);
|
24
|
+
}
|
25
|
+
i_mm->t->fd = fd;
|
26
|
+
}
|
27
|
+
|
28
|
+
if (lseek(i_mm->t->fd, len - 1, SEEK_SET) == -1) {
|
29
|
+
close_file(i_mm);
|
30
|
+
return with_exception_errno(rb_eIOError, "Can't lseek %zu", len - 1);
|
31
|
+
}
|
32
|
+
|
33
|
+
if (write(i_mm->t->fd, "\000", 1) != 1) {
|
34
|
+
close_file(i_mm);
|
35
|
+
return with_exception_errno(rb_eIOError, "Can't extend %s", i_mm->t->path);
|
36
|
+
}
|
37
|
+
i_mm->t->len = len;
|
38
|
+
|
39
|
+
return SUCCESS;
|
40
|
+
}
|
41
|
+
|
42
|
+
static int perform_munmap(mm_ipc *i_mm) {
|
43
|
+
if (i_mm->t->addr != NULL && munmap(i_mm->t->addr, i_mm->t->len)) {
|
44
|
+
i_mm->t->addr = NULL;
|
45
|
+
return with_exception_errno(rb_eArgError, "munmap failed");
|
46
|
+
}
|
47
|
+
|
48
|
+
i_mm->t->addr = NULL;
|
49
|
+
i_mm->t->len = 0;
|
50
|
+
i_mm->t->real = 0;
|
51
|
+
|
52
|
+
return SUCCESS;
|
53
|
+
}
|
54
|
+
|
55
|
+
static int perform_mmap(mm_ipc *i_mm, size_t len) {
|
56
|
+
MMAP_RETTYPE addr = mmap(0, len, i_mm->t->pmode, i_mm->t->vscope, i_mm->t->fd, i_mm->t->offset);
|
57
|
+
|
58
|
+
if (addr == MAP_FAILED) {
|
59
|
+
return with_exception_errno(rb_eArgError, "mmap failed");
|
60
|
+
}
|
61
|
+
|
62
|
+
i_mm->t->addr = addr;
|
63
|
+
i_mm->t->len = len;
|
64
|
+
i_mm->t->real = len;
|
65
|
+
|
66
|
+
return SUCCESS;
|
67
|
+
}
|
68
|
+
|
69
|
+
static int expand(VALUE self, mm_ipc *i_mm, size_t len) {
|
70
|
+
if (len < i_mm->t->len) {
|
71
|
+
return with_exception(rb_eArgError, "Can't reduce the size of mmap");
|
72
|
+
}
|
73
|
+
|
74
|
+
if (!perform_munmap(i_mm)) {
|
75
|
+
return FAILURE;
|
76
|
+
}
|
77
|
+
|
78
|
+
if (!open_and_extend_file(i_mm, len)) {
|
79
|
+
return FAILURE;
|
80
|
+
}
|
81
|
+
|
82
|
+
if (!perform_mmap(i_mm, len)) {
|
83
|
+
close_file(i_mm);
|
84
|
+
return FAILURE;
|
85
|
+
}
|
86
|
+
|
87
|
+
if ((i_mm->t->flag & MM_LOCK) && mlock(i_mm->t->addr, len) == -1) {
|
88
|
+
return with_exception_errno(rb_eArgError, "mlock(%d)", errno);
|
89
|
+
}
|
90
|
+
|
91
|
+
mm_update(self);
|
92
|
+
|
93
|
+
return SUCCESS;
|
94
|
+
}
|
95
|
+
|
96
|
+
static void save_entry(mm_ipc *i_mm, size_t offset, VALUE key, VALUE value) {
|
97
|
+
uint32_t key_length = (uint32_t)RSTRING_LEN(key);
|
98
|
+
|
99
|
+
char *pos = (char *)i_mm->t->addr + offset;
|
100
|
+
|
101
|
+
memcpy(pos, &key_length, sizeof(uint32_t));
|
102
|
+
pos += sizeof(uint32_t);
|
103
|
+
|
104
|
+
memmove(pos, StringValuePtr(key), key_length);
|
105
|
+
pos += key_length;
|
106
|
+
|
107
|
+
memset(pos, ' ', padding_length(key_length)); // TODO: considder padding with /0
|
108
|
+
pos += padding_length(key_length);
|
109
|
+
|
110
|
+
double val = NUM2DBL(value);
|
111
|
+
memcpy(pos, &val, sizeof(double));
|
112
|
+
}
|
113
|
+
|
114
|
+
static void save_value(mm_ipc *i_mm, VALUE _offset, VALUE value) {
|
115
|
+
Check_Type(_offset, T_FIXNUM);
|
116
|
+
size_t offset = NUM2UINT(_offset);
|
117
|
+
if ((i_mm->t->real + sizeof(double)) <= offset) {
|
118
|
+
rb_raise(rb_eIndexError, "offset %zu out of string", offset);
|
119
|
+
}
|
120
|
+
|
121
|
+
if (i_mm->t->flag & MM_FROZEN) {
|
122
|
+
rb_error_frozen("mmap");
|
123
|
+
}
|
124
|
+
|
125
|
+
char *pos = (char *)i_mm->t->addr + offset;
|
126
|
+
|
127
|
+
double val = NUM2DBL(value);
|
128
|
+
memcpy(pos, &val, sizeof(double));
|
129
|
+
}
|
130
|
+
|
131
|
+
static VALUE load_value(mm_ipc *i_mm, VALUE _offset) {
|
132
|
+
Check_Type(_offset, T_FIXNUM);
|
133
|
+
size_t offset = NUM2UINT(_offset);
|
134
|
+
if ((i_mm->t->real + sizeof(double)) <= offset) {
|
135
|
+
rb_raise(rb_eIndexError, "offset %zu out of string", offset);
|
136
|
+
}
|
137
|
+
|
138
|
+
char *pos = (char *)i_mm->t->addr + offset;
|
139
|
+
|
140
|
+
double value;
|
141
|
+
memcpy(&value, pos, sizeof(double));
|
142
|
+
return DBL2NUM(value);
|
143
|
+
}
|
144
|
+
|
145
|
+
uint32_t load_used(mm_ipc *i_mm) {
|
146
|
+
uint32_t used = *((uint32_t *)i_mm->t->addr);
|
147
|
+
|
148
|
+
if (used == 0) {
|
149
|
+
used = START_POSITION;
|
150
|
+
}
|
151
|
+
return used;
|
152
|
+
}
|
153
|
+
|
154
|
+
void save_used(mm_ipc *i_mm, uint32_t used) { *((uint32_t *)i_mm->t->addr) = used; }
|
155
|
+
|
156
|
+
static VALUE initialize_entry(VALUE self, mm_ipc *i_mm, VALUE positions, VALUE key, VALUE value) {
|
157
|
+
if (i_mm->t->flag & MM_FROZEN) {
|
158
|
+
rb_error_frozen("mmap");
|
159
|
+
}
|
160
|
+
|
161
|
+
if (RSTRING_LEN(key) > INT32_MAX) {
|
162
|
+
rb_raise(rb_eArgError, "string length gt %d", INT32_MAX);
|
163
|
+
}
|
164
|
+
|
165
|
+
uint32_t key_length = (uint32_t)RSTRING_LEN(key);
|
166
|
+
uint32_t value_offset = sizeof(uint32_t) + key_length + padding_length(key_length);
|
167
|
+
uint32_t entry_length = value_offset + sizeof(double);
|
168
|
+
|
169
|
+
uint32_t used = load_used(i_mm);
|
170
|
+
while (i_mm->t->len < (used + entry_length)) {
|
171
|
+
if (!expand(self, i_mm, i_mm->t->len * 2)) {
|
172
|
+
raise_last_exception();
|
173
|
+
}
|
174
|
+
}
|
175
|
+
|
176
|
+
save_entry(i_mm, used, key, value);
|
177
|
+
save_used(i_mm, used + entry_length);
|
178
|
+
|
179
|
+
return rb_hash_aset(positions, key, INT2NUM(used + value_offset));
|
180
|
+
}
|
181
|
+
|
182
|
+
VALUE method_fetch_entry(VALUE self, VALUE positions, VALUE key, VALUE default_value) {
|
183
|
+
Check_Type(positions, T_HASH);
|
184
|
+
Check_Type(key, T_STRING);
|
185
|
+
|
186
|
+
mm_ipc *i_mm;
|
187
|
+
GET_MMAP(self, i_mm, MM_MODIFY);
|
188
|
+
|
189
|
+
VALUE position = rb_hash_lookup(positions, key);
|
190
|
+
|
191
|
+
if (position != Qnil) {
|
192
|
+
return load_value(i_mm, position);
|
193
|
+
}
|
194
|
+
|
195
|
+
position = initialize_entry(self, i_mm, positions, key, default_value);
|
196
|
+
return load_value(i_mm, position);
|
197
|
+
}
|
198
|
+
|
199
|
+
VALUE method_upsert_entry(VALUE self, VALUE positions, VALUE key, VALUE value) {
|
200
|
+
Check_Type(positions, T_HASH);
|
201
|
+
Check_Type(key, T_STRING);
|
202
|
+
|
203
|
+
mm_ipc *i_mm;
|
204
|
+
GET_MMAP(self, i_mm, MM_MODIFY);
|
205
|
+
|
206
|
+
VALUE position = rb_hash_lookup(positions, key);
|
207
|
+
|
208
|
+
if (position != Qnil) {
|
209
|
+
save_value(i_mm, position, value);
|
210
|
+
return load_value(i_mm, position);
|
211
|
+
}
|
212
|
+
|
213
|
+
position = initialize_entry(self, i_mm, positions, key, value);
|
214
|
+
return load_value(i_mm, position);
|
215
|
+
}
|
216
|
+
|
217
|
+
VALUE method_load_used(VALUE self) {
|
218
|
+
mm_ipc *i_mm;
|
219
|
+
|
220
|
+
GET_MMAP(self, i_mm, MM_MODIFY);
|
221
|
+
return UINT2NUM(load_used(i_mm));
|
222
|
+
}
|
223
|
+
|
224
|
+
VALUE method_save_used(VALUE self, VALUE value) {
|
225
|
+
Check_Type(value, T_FIXNUM);
|
226
|
+
mm_ipc *i_mm;
|
227
|
+
|
228
|
+
GET_MMAP(self, i_mm, MM_MODIFY);
|
229
|
+
|
230
|
+
if (i_mm->t->flag & MM_FROZEN) {
|
231
|
+
rb_error_frozen("mmap");
|
232
|
+
}
|
233
|
+
|
234
|
+
if (i_mm->t->len < INITIAL_SIZE) {
|
235
|
+
if (!expand(self, i_mm, INITIAL_SIZE)) {
|
236
|
+
raise_last_exception();
|
237
|
+
}
|
238
|
+
}
|
239
|
+
|
240
|
+
save_used(i_mm, NUM2UINT(value));
|
241
|
+
return value;
|
242
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef VALUE_ACCESS_H
|
2
|
+
#define VALUE_ACCESS_H
|
3
|
+
#include <ruby.h>
|
4
|
+
|
5
|
+
VALUE method_load_used(VALUE self);
|
6
|
+
|
7
|
+
VALUE method_save_used(VALUE self, VALUE value);
|
8
|
+
|
9
|
+
VALUE method_get_double(VALUE self, VALUE index);
|
10
|
+
|
11
|
+
VALUE method_fetch_entry(VALUE self, VALUE positions, VALUE key, VALUE default_value);
|
12
|
+
|
13
|
+
VALUE method_upsert_entry(VALUE self, VALUE positions, VALUE key, VALUE value);
|
14
|
+
|
15
|
+
#endif
|
@@ -0,0 +1,23 @@
|
|
1
|
+
[target.aarch64-apple-darwin]
|
2
|
+
# Without this flag, when linking static libruby, the linker removes symbols
|
3
|
+
# (such as `_rb_ext_ractor_safe`) which it thinks are dead code... but they are
|
4
|
+
# not, and they need to be included for the `embed` feature to work with static
|
5
|
+
# Ruby.
|
6
|
+
rustflags = [
|
7
|
+
"-C", "link-dead-code=on",
|
8
|
+
"-C", "link-arg=-undefined",
|
9
|
+
"-C", "link-arg=dynamic_lookup",
|
10
|
+
]
|
11
|
+
|
12
|
+
[target.x86_64-apple-darwin]
|
13
|
+
rustflags = [
|
14
|
+
"-C", "link-dead-code=on",
|
15
|
+
"-C", "link-arg=-undefined",
|
16
|
+
"-C", "link-arg=dynamic_lookup",
|
17
|
+
]
|
18
|
+
|
19
|
+
[target.aarch64-unknown-linux-gnu]
|
20
|
+
rustflags = [ "-C", "link-dead-code=on" ]
|
21
|
+
|
22
|
+
[target.x86_64-unknown-linux-gnu]
|
23
|
+
rustflags = [ "-C", "link-dead-code=on" ]
|