prometheus-client-mmap 0.28.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.tool-versions +1 -1
  3. data/README.md +2 -30
  4. data/ext/fast_mmaped_file_rs/extconf.rb +1 -3
  5. data/ext/fast_mmaped_file_rs/src/error.rs +8 -0
  6. data/ext/fast_mmaped_file_rs/src/mmap/inner.rs +38 -13
  7. data/ext/fast_mmaped_file_rs/src/mmap.rs +71 -5
  8. data/lib/prometheus/client/configuration.rb +1 -2
  9. data/lib/prometheus/client/formats/text.rb +1 -12
  10. data/lib/prometheus/client/helper/mmaped_file.rb +3 -14
  11. data/lib/prometheus/client/rack/exporter.rb +1 -3
  12. data/lib/prometheus/client/version.rb +1 -1
  13. metadata +4 -43
  14. data/ext/fast_mmaped_file/extconf.rb +0 -30
  15. data/ext/fast_mmaped_file/fast_mmaped_file.c +0 -122
  16. data/ext/fast_mmaped_file/file_format.c +0 -5
  17. data/ext/fast_mmaped_file/file_format.h +0 -11
  18. data/ext/fast_mmaped_file/file_parsing.c +0 -195
  19. data/ext/fast_mmaped_file/file_parsing.h +0 -27
  20. data/ext/fast_mmaped_file/file_reading.c +0 -102
  21. data/ext/fast_mmaped_file/file_reading.h +0 -30
  22. data/ext/fast_mmaped_file/globals.h +0 -14
  23. data/ext/fast_mmaped_file/mmap.c +0 -428
  24. data/ext/fast_mmaped_file/mmap.h +0 -61
  25. data/ext/fast_mmaped_file/rendering.c +0 -199
  26. data/ext/fast_mmaped_file/rendering.h +0 -8
  27. data/ext/fast_mmaped_file/utils.c +0 -56
  28. data/ext/fast_mmaped_file/utils.h +0 -22
  29. data/ext/fast_mmaped_file/value_access.c +0 -242
  30. data/ext/fast_mmaped_file/value_access.h +0 -15
  31. data/lib/prometheus/client/helper/loader.rb +0 -44
  32. data/vendor/c/hashmap/.gitignore +0 -52
  33. data/vendor/c/hashmap/LICENSE +0 -21
  34. data/vendor/c/hashmap/README.md +0 -90
  35. data/vendor/c/hashmap/_config.yml +0 -1
  36. data/vendor/c/hashmap/src/hashmap.c +0 -692
  37. data/vendor/c/hashmap/src/hashmap.h +0 -267
  38. data/vendor/c/hashmap/test/Makefile +0 -22
  39. data/vendor/c/hashmap/test/hashmap_test.c +0 -608
  40. data/vendor/c/jsmn/.travis.yml +0 -4
  41. data/vendor/c/jsmn/LICENSE +0 -20
  42. data/vendor/c/jsmn/Makefile +0 -41
  43. data/vendor/c/jsmn/README.md +0 -168
  44. data/vendor/c/jsmn/example/jsondump.c +0 -126
  45. data/vendor/c/jsmn/example/simple.c +0 -76
  46. data/vendor/c/jsmn/jsmn.c +0 -314
  47. data/vendor/c/jsmn/jsmn.h +0 -76
  48. data/vendor/c/jsmn/library.json +0 -16
  49. data/vendor/c/jsmn/test/test.h +0 -27
  50. data/vendor/c/jsmn/test/tests.c +0 -407
  51. data/vendor/c/jsmn/test/testutil.h +0 -94
@@ -1,11 +0,0 @@
1
- #ifndef FILE_FORMAT_H
2
- #define FILE_FORMAT_H
3
-
4
- #include <stdint.h>
5
-
6
- #define START_POSITION 8
7
- #define INITIAL_SIZE (2 * sizeof(int32_t))
8
-
9
- uint32_t padding_length(uint32_t key_length);
10
-
11
- #endif
@@ -1,195 +0,0 @@
1
- #include "file_parsing.h"
2
-
3
- #include <hashmap.h>
4
- #include <jsmn.h>
5
-
6
- #include "file_format.h"
7
- #include "globals.h"
8
- #include "utils.h"
9
-
10
- HASHMAP_FUNCS_CREATE(entry, const entry_t, entry_t)
11
-
12
- typedef int (*compare_fn)(const void *a, const void *b);
13
-
14
- static size_t hashmap_hash_entry(const entry_t *entry) { return hashmap_hash_string(entry->json); }
15
-
16
- static int hashmap_compare_entry(const entry_t *a, const entry_t *b) {
17
- if (a->json_size != b->json_size) {
18
- return -1;
19
- }
20
-
21
- if (is_pid_significant(a) && (rb_str_equal(a->pid, b->pid) == Qfalse)) {
22
- return -1;
23
- }
24
-
25
- return strncmp(a->json, b->json, a->json_size);
26
- }
27
-
28
- static void entry_free(entry_t *entry) {
29
- free(entry->json);
30
- free(entry);
31
- }
32
-
33
- static inline double min(double a, double b) { return a < b ? a : b; }
34
-
35
- static inline double max(double a, double b) { return a > b ? a : b; }
36
-
37
- static void merge_entry(entry_t *found, const entry_t *entry) {
38
- if (entry->type == sym_gauge) {
39
- if (entry->multiprocess_mode == sym_min) {
40
- found->value = min(found->value, entry->value);
41
- } else if (entry->multiprocess_mode == sym_max) {
42
- found->value = max(found->value, entry->value);
43
- } else if (entry->multiprocess_mode == sym_livesum) {
44
- found->value += entry->value;
45
- } else {
46
- found->value = entry->value;
47
- }
48
- } else {
49
- found->value += entry->value;
50
- }
51
- }
52
-
53
- void merge_or_store(struct hashmap *map, entry_t *entry) {
54
- entry_t *found = entry_hashmap_get(map, entry);
55
- if (found) {
56
- merge_entry(found, entry);
57
- entry_free(entry);
58
- } else {
59
- entry_hashmap_put(map, entry, entry); // use the hashmap like hashset actually
60
- }
61
- }
62
-
63
- entry_t *entry_new(buffer_t *source, uint32_t pos, uint32_t encoded_len, file_t *file_info) {
64
- entry_t *entry = calloc(1, sizeof(entry_t));
65
- if (entry == NULL) {
66
- return NULL;
67
- }
68
-
69
- entry->json = malloc(encoded_len + 1);
70
- if (entry->json == NULL) {
71
- free(entry);
72
- return NULL;
73
- }
74
-
75
- memcpy(entry->json, source->buffer + pos, encoded_len);
76
- entry->json[encoded_len] = '\0';
77
- entry->json_size = encoded_len;
78
-
79
- entry->pid = file_info->pid;
80
- entry->multiprocess_mode = file_info->multiprocess_mode;
81
- entry->type = file_info->type;
82
-
83
- char *value_ptr = source->buffer + pos + encoded_len + padding_length(encoded_len);
84
- memcpy(&(entry->value), value_ptr, sizeof(double));
85
-
86
- return entry;
87
- }
88
-
89
- static int add_parsed_name(entry_t *entry) {
90
- jsmn_parser parser;
91
- jsmn_init(&parser);
92
-
93
- jsmntok_t tokens[2];
94
- memset(&tokens, 0, sizeof(tokens));
95
-
96
- jsmn_parse(&parser, entry->json, entry->json_size, tokens, 2);
97
- jsmntok_t *name_token = &tokens[1];
98
-
99
- if (name_token->start < name_token->end && name_token->start > 0) {
100
- entry->name = entry->json + name_token->start;
101
- entry->name_len = name_token->end - name_token->start;
102
- return 1;
103
- }
104
- return 0;
105
- }
106
-
107
- static int entry_lexical_comparator(const entry_t **a, const entry_t **b) {
108
- size_t size_a = (*a)->json_size;
109
- size_t size_b = (*b)->json_size;
110
- size_t min_length = size_a < size_b ? size_a : size_b;
111
-
112
- return strncmp((*a)->json, (*b)->json, min_length);
113
- }
114
-
115
- void hashmap_setup(struct hashmap *map) {
116
- hashmap_init(map, (size_t(*)(const void *))hashmap_hash_entry,
117
- (int (*)(const void *, const void *))hashmap_compare_entry, 1000);
118
- hashmap_set_key_alloc_funcs(map, NULL, (void (*)(void *))entry_free);
119
- }
120
-
121
- int process_buffer(file_t *file_info, buffer_t *source, struct hashmap *map) {
122
- if (source->size < START_POSITION) {
123
- // nothing to read
124
- return 1;
125
- }
126
- uint32_t used;
127
- memcpy(&used, source->buffer, sizeof(uint32_t));
128
-
129
- if (used > source->size) {
130
- save_exception(prom_eParsingError, "source file %s corrupted, used %u > file size %u", file_info->path, used,
131
- source->size);
132
- return 0;
133
- }
134
-
135
- uint32_t pos = START_POSITION;
136
- while (pos + sizeof(uint32_t) < used) {
137
- uint32_t encoded_len;
138
- memcpy(&encoded_len, source->buffer + pos, sizeof(uint32_t));
139
- pos += sizeof(uint32_t);
140
-
141
- uint32_t value_offset = encoded_len + padding_length(encoded_len);
142
-
143
- if (pos + value_offset + sizeof(double) > used) {
144
- save_exception(prom_eParsingError, "source file %s corrupted, used %u < stored data length %u",
145
- file_info->path, used, pos + value_offset + sizeof(double));
146
- return 0;
147
- }
148
-
149
- entry_t *entry = entry_new(source, pos, encoded_len, file_info);
150
- if (entry == NULL) {
151
- save_exception(rb_eNoMemError, "Failed creating metrics entry");
152
- return 0;
153
- }
154
-
155
- merge_or_store(map, entry);
156
-
157
- pos += value_offset + sizeof(double);
158
- }
159
- return 1;
160
- }
161
-
162
- int sort_map_entries(const struct hashmap *map, entry_t ***sorted_entries) {
163
- size_t num = hashmap_size(map);
164
-
165
- entry_t **list = calloc(num, sizeof(entry_t *));
166
-
167
- if (list == NULL) {
168
- save_exception(rb_eNoMemError, "Couldn't allocate for %zu memory", num * sizeof(entry_t *));
169
- return 0;
170
- }
171
-
172
- size_t cnt = 0;
173
- struct hashmap_iter *iter;
174
- for (iter = hashmap_iter(map); iter; iter = hashmap_iter_next(map, iter)) {
175
- entry_t *entry = (entry_t *)entry_hashmap_iter_get_key(iter);
176
- if (add_parsed_name(entry)) {
177
- list[cnt] = entry;
178
- cnt++;
179
- }
180
- }
181
- if (cnt != num) {
182
- save_exception(rb_eRuntimeError, "Processed entries %zu != map entries %zu", cnt, num);
183
- free(list);
184
- return 0;
185
- }
186
-
187
- qsort(list, cnt, sizeof(entry_t *), (compare_fn)&entry_lexical_comparator);
188
- *sorted_entries = list;
189
- return 1;
190
- }
191
-
192
- int is_pid_significant(const entry_t *e) {
193
- ID mp = e->multiprocess_mode;
194
- return e->type == sym_gauge && !(mp == sym_min || mp == sym_max || mp == sym_livesum);
195
- }
@@ -1,27 +0,0 @@
1
- #ifndef FILE_PARSING_H
2
- #define FILE_PARSING_H
3
- #include <file_reading.h>
4
- #include <hashmap.h>
5
- #include <ruby.h>
6
-
7
- typedef struct {
8
- char *json;
9
- size_t json_size;
10
- char *name;
11
- size_t name_len;
12
-
13
- ID multiprocess_mode;
14
- ID type;
15
- VALUE pid;
16
-
17
- double value;
18
- } entry_t;
19
-
20
- void hashmap_setup(struct hashmap *map);
21
-
22
- int process_buffer(file_t *file_info, buffer_t *source, struct hashmap *map);
23
- int sort_map_entries(const struct hashmap *map, entry_t ***sorted_entries);
24
-
25
- int is_pid_significant(const entry_t *e);
26
-
27
- #endif
@@ -1,102 +0,0 @@
1
- #include "file_reading.h"
2
-
3
- #include <errno.h>
4
- #include <fcntl.h>
5
-
6
- #include "utils.h"
7
-
8
- static int file_open(file_t *source, const char *filepath) {
9
- source->file = fopen(filepath, "r");
10
- size_t filepath_len = strlen(filepath) + sizeof(char);
11
- source->path = malloc(filepath_len);
12
- memcpy(source->path, filepath, filepath_len);
13
-
14
- if (source->file == NULL) {
15
- save_exception(rb_eArgError, "Can't open %s, errno: %d", filepath, errno);
16
-
17
- return 0;
18
- }
19
-
20
- struct stat sb;
21
- if (fstat(fileno(source->file), &sb) != 0) {
22
- fclose(source->file);
23
- save_exception(rb_eIOError, "Can't stat file, errno: %d", errno);
24
-
25
- return 0;
26
- }
27
- source->length = sb.st_size;
28
-
29
- // go to start
30
- if (fseek(source->file, 0L, SEEK_SET) != 0) {
31
- fclose(source->file);
32
- save_exception(rb_eIOError, "Can't fseek %zu, errno: %d", 0, errno);
33
-
34
- return 0;
35
- }
36
-
37
- return 1;
38
- }
39
-
40
- int file_close(file_t *source) {
41
- free(source->path);
42
- if (fclose(source->file) != 0) {
43
- save_exception(rb_eIOError, "Can't fclose file, errno: %d", 0, errno);
44
- return 0;
45
- }
46
- source->file = 0;
47
-
48
- return 1;
49
- }
50
-
51
- int file_open_from_params(file_t *source, VALUE params) {
52
- if (RARRAY_LEN(params) != 4) {
53
- save_exception(rb_eArgError, "wrong number of arguments %lu instead of 4", RARRAY_LEN(params));
54
- return 0;
55
- }
56
-
57
- VALUE filepath = rb_ary_entry(params, 0);
58
-
59
- source->multiprocess_mode = rb_sym2id(rb_ary_entry(params, 1));
60
- source->type = rb_sym2id(rb_ary_entry(params, 2));
61
- source->pid = rb_ary_entry(params, 3);
62
-
63
- return file_open(source, StringValueCStr(filepath));
64
- }
65
-
66
- int read_from_file(const file_t *source, buffer_t *data) {
67
- data->size = 0;
68
- if (data->buffer == NULL) {
69
- data->buffer = malloc(source->length);
70
- if (data->buffer == NULL) {
71
- save_exception(rb_eIOError, "Can't malloc %zu, errno: %d", source->length, errno);
72
- return 0;
73
- }
74
-
75
- data->capacity = source->length;
76
- } else if (data->capacity < source->length) {
77
- data->buffer = realloc(data->buffer, source->length);
78
- if (data->buffer == NULL) {
79
- save_exception(rb_eIOError, "Can't realloc %zu, errno: %d", source->length, errno);
80
- return 0;
81
- }
82
-
83
- data->capacity = source->length;
84
- }
85
-
86
- data->size = fread(data->buffer, sizeof(char), source->length, source->file);
87
- if (data->size != source->length) {
88
- save_exception(rb_eIOError, "Couldn't read whole file, read %zu, instead of %zu", data->size, source->length);
89
- return 0;
90
- }
91
-
92
- return 1;
93
- }
94
-
95
- void buffer_dispose(buffer_t *buffer) {
96
- if (buffer->buffer) {
97
- free(buffer->buffer);
98
- }
99
- buffer->buffer = NULL;
100
- buffer->size = 0;
101
- buffer->capacity = 0;
102
- }
@@ -1,30 +0,0 @@
1
- #ifndef FILE_READING_H
2
- #define FILE_READING_H
3
- #include <ruby.h>
4
-
5
- typedef struct {
6
- FILE *file;
7
- size_t length;
8
- char *path;
9
-
10
- // Information processed from file path
11
- ID multiprocess_mode;
12
- ID type;
13
- VALUE pid;
14
- } file_t;
15
-
16
- typedef struct {
17
- char *buffer;
18
- size_t size;
19
- size_t capacity;
20
- } buffer_t;
21
-
22
- int file_close(file_t *file);
23
-
24
- int file_open_from_params(file_t *file, VALUE params);
25
-
26
- int read_from_file(const file_t *source, buffer_t *data);
27
-
28
- void buffer_dispose(buffer_t *buffer);
29
-
30
- #endif
@@ -1,14 +0,0 @@
1
- #ifndef GLOBALS_H
2
- #define GLOBALS_H
3
- #include <ruby.h>
4
-
5
- extern ID sym_min;
6
- extern ID sym_max;
7
- extern ID sym_livesum;
8
- extern ID sym_gauge;
9
- extern ID sym_pid;
10
- extern ID sym_samples;
11
-
12
- extern VALUE prom_eParsingError;
13
-
14
- #endif