]> arthur.barton.de Git - netdata.git/blob - src/common.c
Merge pull request #1849 from yassinei/yassinei-fixed-commas
[netdata.git] / src / common.c
1 #include "common.h"
2
3 #ifdef __APPLE__
4 #define INHERIT_NONE 0
5 #endif /* __APPLE__ */
6 #if defined(__FreeBSD__) || defined(__APPLE__)
7 #    define O_NOATIME     0
8 #    define MADV_DONTFORK INHERIT_NONE
9 #endif /* __FreeBSD__ || __APPLE__*/
10
11 char *netdata_configured_config_dir  = NULL;
12 char *netdata_configured_log_dir     = NULL;
13 char *netdata_configured_plugins_dir = NULL;
14 char *netdata_configured_web_dir     = NULL;
15 char *netdata_configured_cache_dir   = NULL;
16 char *netdata_configured_varlib_dir  = NULL;
17 char *netdata_configured_home_dir    = NULL;
18 char *netdata_configured_host_prefix = NULL;
19
20 int enable_ksm = 1;
21
22 volatile sig_atomic_t netdata_exit = 0;
23 const char *os_type = NETDATA_OS_TYPE;
24 const char *program_version = VERSION;
25
26 // ----------------------------------------------------------------------------
27 // memory allocation functions that handle failures
28
29 // although netdata does not use memory allocations too often (netdata tries to
30 // maintain its memory footprint stable during runtime, i.e. all buffers are
31 // allocated during initialization and are adapted to current use throughout
32 // its lifetime), these can be used to override the default system allocation
33 // routines.
34
35 #ifdef NETDATA_LOG_ALLOCATIONS
36 static struct memory_statistics {
37     volatile size_t malloc_calls_made;
38     volatile size_t calloc_calls_made;
39     volatile size_t realloc_calls_made;
40     volatile size_t strdup_calls_made;
41     volatile size_t free_calls_made;
42     volatile size_t memory_calls_made;
43     volatile size_t allocated_memory;
44     volatile size_t mmapped_memory;
45 } memory_statistics;
46
47 static inline void print_allocations(const char *file, const char *function, const unsigned long line) {
48     static struct memory_statistics old = { 0, 0, 0, 0, 0, 0, 0, 0 };
49
50     //if(unlikely(!(memory_statistics.memory_calls_made % 5))) {
51         fprintf(stderr, "(%04lu@%-10.10s:%-15.15s): Allocated %zu KB (+%zu B), mmapped %zu KB (+%zu B): malloc %zu (+%zu), calloc %zu (+%zu), realloc %zu (+%zu), strdup %zu (+%zu), free %zu (+%zu)\n",
52                 line, file, function,
53                 (memory_statistics.allocated_memory + 512) / 1024, memory_statistics.allocated_memory - old.allocated_memory,
54                 (memory_statistics.mmapped_memory + 512) / 1024, memory_statistics.mmapped_memory - old.mmapped_memory,
55                 memory_statistics.malloc_calls_made, memory_statistics.malloc_calls_made - old.malloc_calls_made,
56                 memory_statistics.calloc_calls_made, memory_statistics.calloc_calls_made - old.calloc_calls_made,
57                 memory_statistics.realloc_calls_made, memory_statistics.realloc_calls_made - old.realloc_calls_made,
58                 memory_statistics.strdup_calls_made, memory_statistics.strdup_calls_made - old.strdup_calls_made,
59                 memory_statistics.free_calls_made, memory_statistics.free_calls_made - old.free_calls_made
60         );
61
62         memcpy(&old, &memory_statistics, sizeof(struct memory_statistics));
63     //}
64 }
65
66 static inline void malloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
67 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
68     __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
69     __atomic_fetch_add(&memory_statistics.malloc_calls_made, 1, __ATOMIC_SEQ_CST);
70     __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
71 #else
72     // this is for debugging - we don't care locking it
73     memory_statistics.memory_calls_made++;
74     memory_statistics.malloc_calls_made++;
75     memory_statistics.allocated_memory += size;
76 #endif
77     print_allocations(file, function, line);
78 }
79
80 static inline void mmap_accounting(size_t size) {
81 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
82     __atomic_fetch_add(&memory_statistics.malloc_calls_made, 1, __ATOMIC_SEQ_CST);
83     __atomic_fetch_add(&memory_statistics.mmapped_memory, size, __ATOMIC_SEQ_CST);
84 #else
85     // this is for debugging - we don't care locking it
86     memory_statistics.memory_calls_made++;
87     memory_statistics.mmapped_memory += size;
88 #endif
89 }
90
91 static inline void calloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
92 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
93     __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
94     __atomic_fetch_add(&memory_statistics.calloc_calls_made, 1, __ATOMIC_SEQ_CST);
95     __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
96 #else
97     // this is for debugging - we don't care locking it
98     memory_statistics.memory_calls_made++;
99     memory_statistics.calloc_calls_made++;
100     memory_statistics.allocated_memory += size;
101 #endif
102     print_allocations(file, function, line);
103 }
104
105 static inline void realloc_accounting(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
106     (void)ptr;
107
108 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
109     __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
110     __atomic_fetch_add(&memory_statistics.realloc_calls_made, 1, __ATOMIC_SEQ_CST);
111     __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
112 #else
113     // this is for debugging - we don't care locking it
114     memory_statistics.memory_calls_made++;
115     memory_statistics.realloc_calls_made++;
116     memory_statistics.allocated_memory += size;
117 #endif
118     print_allocations(file, function, line);
119 }
120
121 static inline void strdup_accounting(const char *file, const char *function, const unsigned long line, const char *s) {
122     size_t size = strlen(s) + 1;
123
124 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
125     __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
126     __atomic_fetch_add(&memory_statistics.strdup_calls_made, 1, __ATOMIC_SEQ_CST);
127     __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
128 #else
129     // this is for debugging - we don't care locking it
130     memory_statistics.memory_calls_made++;
131     memory_statistics.strdup_calls_made++;
132     memory_statistics.allocated_memory += size;
133 #endif
134     print_allocations(file, function, line);
135 }
136
137 static inline void free_accounting(const char *file, const char *function, const unsigned long line, void *ptr) {
138     (void)file;
139     (void)function;
140     (void)line;
141
142     if(likely(ptr)) {
143 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
144         __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
145         __atomic_fetch_add(&memory_statistics.free_calls_made, 1, __ATOMIC_SEQ_CST);
146 #else
147         // this is for debugging - we don't care locking it
148         memory_statistics.memory_calls_made++;
149         memory_statistics.free_calls_made++;
150 #endif
151     }
152 }
153 #endif
154
155 #ifdef NETDATA_LOG_ALLOCATIONS
156 char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s) {
157     strdup_accounting(file, function, line, s);
158 #else
159 char *strdupz(const char *s) {
160 #endif
161
162     char *t = strdup(s);
163     if (unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
164     return t;
165 }
166
167 #ifdef NETDATA_LOG_ALLOCATIONS
168 void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size) {
169     malloc_accounting(file, function, line, size);
170 #else
171 void *mallocz(size_t size) {
172 #endif
173
174     void *p = malloc(size);
175     if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
176     return p;
177 }
178
179 #ifdef NETDATA_LOG_ALLOCATIONS
180 void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size) {
181     calloc_accounting(file, function, line, nmemb * size);
182 #else
183 void *callocz(size_t nmemb, size_t size) {
184 #endif
185
186     void *p = calloc(nmemb, size);
187     if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
188     return p;
189 }
190
191 #ifdef NETDATA_LOG_ALLOCATIONS
192 void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
193     realloc_accounting(file, function, line, ptr, size);
194 #else
195 void *reallocz(void *ptr, size_t size) {
196 #endif
197
198     void *p = realloc(ptr, size);
199     if (unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
200     return p;
201 }
202
203 #ifdef NETDATA_LOG_ALLOCATIONS
204 void freez_int(const char *file, const char *function, const unsigned long line, void *ptr) {
205     free_accounting(file, function, line, ptr);
206 #else
207 void freez(void *ptr) {
208 #endif
209
210     free(ptr);
211 }
212
213 void json_escape_string(char *dst, const char *src, size_t size) {
214     const char *t;
215     char *d = dst, *e = &dst[size - 1];
216
217     for(t = src; *t && d < e ;t++) {
218         if(unlikely(*t == '\\' || *t == '"')) {
219             if(unlikely(d + 1 >= e)) break;
220             *d++ = '\\';
221         }
222         *d++ = *t;
223     }
224
225     *d = '\0';
226 }
227
228 int sleep_usec(usec_t usec) {
229
230 #ifndef NETDATA_WITH_USLEEP
231     // we expect microseconds (1.000.000 per second)
232     // but timespec is nanoseconds (1.000.000.000 per second)
233     struct timespec rem, req = {
234             .tv_sec = (time_t) (usec / 1000000),
235             .tv_nsec = (suseconds_t) ((usec % 1000000) * 1000)
236     };
237
238     while (nanosleep(&req, &rem) == -1) {
239         if (likely(errno == EINTR)) {
240             debug(D_SYSTEM, "nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
241             req.tv_sec = rem.tv_sec;
242             req.tv_nsec = rem.tv_nsec;
243         } else {
244             error("Cannot nanosleep() for %llu microseconds.", usec);
245             break;
246         }
247     }
248
249     return 0;
250 #else
251     int ret = usleep(usec);
252     if(unlikely(ret == -1 && errno == EINVAL)) {
253         // on certain systems, usec has to be up to 999999
254         if(usec > 999999) {
255             int counter = usec / 999999;
256             while(counter--)
257                 usleep(999999);
258
259             usleep(usec % 999999);
260         }
261         else {
262             error("Cannot usleep() for %llu microseconds.", usec);
263             return ret;
264         }
265     }
266
267     if(ret != 0)
268         error("usleep() failed for %llu microseconds.", usec);
269
270     return ret;
271 #endif
272 }
273
274 unsigned char netdata_map_chart_names[256] = {
275         [0] = '\0', //
276         [1] = '_', //
277         [2] = '_', //
278         [3] = '_', //
279         [4] = '_', //
280         [5] = '_', //
281         [6] = '_', //
282         [7] = '_', //
283         [8] = '_', //
284         [9] = '_', //
285         [10] = '_', //
286         [11] = '_', //
287         [12] = '_', //
288         [13] = '_', //
289         [14] = '_', //
290         [15] = '_', //
291         [16] = '_', //
292         [17] = '_', //
293         [18] = '_', //
294         [19] = '_', //
295         [20] = '_', //
296         [21] = '_', //
297         [22] = '_', //
298         [23] = '_', //
299         [24] = '_', //
300         [25] = '_', //
301         [26] = '_', //
302         [27] = '_', //
303         [28] = '_', //
304         [29] = '_', //
305         [30] = '_', //
306         [31] = '_', //
307         [32] = '_', //
308         [33] = '_', // !
309         [34] = '_', // "
310         [35] = '_', // #
311         [36] = '_', // $
312         [37] = '_', // %
313         [38] = '_', // &
314         [39] = '_', // '
315         [40] = '_', // (
316         [41] = '_', // )
317         [42] = '_', // *
318         [43] = '_', // +
319         [44] = '.', // ,
320         [45] = '-', // -
321         [46] = '.', // .
322         [47] = '/', // /
323         [48] = '0', // 0
324         [49] = '1', // 1
325         [50] = '2', // 2
326         [51] = '3', // 3
327         [52] = '4', // 4
328         [53] = '5', // 5
329         [54] = '6', // 6
330         [55] = '7', // 7
331         [56] = '8', // 8
332         [57] = '9', // 9
333         [58] = '_', // :
334         [59] = '_', // ;
335         [60] = '_', // <
336         [61] = '_', // =
337         [62] = '_', // >
338         [63] = '_', // ?
339         [64] = '_', // @
340         [65] = 'a', // A
341         [66] = 'b', // B
342         [67] = 'c', // C
343         [68] = 'd', // D
344         [69] = 'e', // E
345         [70] = 'f', // F
346         [71] = 'g', // G
347         [72] = 'h', // H
348         [73] = 'i', // I
349         [74] = 'j', // J
350         [75] = 'k', // K
351         [76] = 'l', // L
352         [77] = 'm', // M
353         [78] = 'n', // N
354         [79] = 'o', // O
355         [80] = 'p', // P
356         [81] = 'q', // Q
357         [82] = 'r', // R
358         [83] = 's', // S
359         [84] = 't', // T
360         [85] = 'u', // U
361         [86] = 'v', // V
362         [87] = 'w', // W
363         [88] = 'x', // X
364         [89] = 'y', // Y
365         [90] = 'z', // Z
366         [91] = '_', // [
367         [92] = '/', // backslash
368         [93] = '_', // ]
369         [94] = '_', // ^
370         [95] = '_', // _
371         [96] = '_', // `
372         [97] = 'a', // a
373         [98] = 'b', // b
374         [99] = 'c', // c
375         [100] = 'd', // d
376         [101] = 'e', // e
377         [102] = 'f', // f
378         [103] = 'g', // g
379         [104] = 'h', // h
380         [105] = 'i', // i
381         [106] = 'j', // j
382         [107] = 'k', // k
383         [108] = 'l', // l
384         [109] = 'm', // m
385         [110] = 'n', // n
386         [111] = 'o', // o
387         [112] = 'p', // p
388         [113] = 'q', // q
389         [114] = 'r', // r
390         [115] = 's', // s
391         [116] = 't', // t
392         [117] = 'u', // u
393         [118] = 'v', // v
394         [119] = 'w', // w
395         [120] = 'x', // x
396         [121] = 'y', // y
397         [122] = 'z', // z
398         [123] = '_', // {
399         [124] = '_', // |
400         [125] = '_', // }
401         [126] = '_', // ~
402         [127] = '_', //
403         [128] = '_', //
404         [129] = '_', //
405         [130] = '_', //
406         [131] = '_', //
407         [132] = '_', //
408         [133] = '_', //
409         [134] = '_', //
410         [135] = '_', //
411         [136] = '_', //
412         [137] = '_', //
413         [138] = '_', //
414         [139] = '_', //
415         [140] = '_', //
416         [141] = '_', //
417         [142] = '_', //
418         [143] = '_', //
419         [144] = '_', //
420         [145] = '_', //
421         [146] = '_', //
422         [147] = '_', //
423         [148] = '_', //
424         [149] = '_', //
425         [150] = '_', //
426         [151] = '_', //
427         [152] = '_', //
428         [153] = '_', //
429         [154] = '_', //
430         [155] = '_', //
431         [156] = '_', //
432         [157] = '_', //
433         [158] = '_', //
434         [159] = '_', //
435         [160] = '_', //
436         [161] = '_', //
437         [162] = '_', //
438         [163] = '_', //
439         [164] = '_', //
440         [165] = '_', //
441         [166] = '_', //
442         [167] = '_', //
443         [168] = '_', //
444         [169] = '_', //
445         [170] = '_', //
446         [171] = '_', //
447         [172] = '_', //
448         [173] = '_', //
449         [174] = '_', //
450         [175] = '_', //
451         [176] = '_', //
452         [177] = '_', //
453         [178] = '_', //
454         [179] = '_', //
455         [180] = '_', //
456         [181] = '_', //
457         [182] = '_', //
458         [183] = '_', //
459         [184] = '_', //
460         [185] = '_', //
461         [186] = '_', //
462         [187] = '_', //
463         [188] = '_', //
464         [189] = '_', //
465         [190] = '_', //
466         [191] = '_', //
467         [192] = '_', //
468         [193] = '_', //
469         [194] = '_', //
470         [195] = '_', //
471         [196] = '_', //
472         [197] = '_', //
473         [198] = '_', //
474         [199] = '_', //
475         [200] = '_', //
476         [201] = '_', //
477         [202] = '_', //
478         [203] = '_', //
479         [204] = '_', //
480         [205] = '_', //
481         [206] = '_', //
482         [207] = '_', //
483         [208] = '_', //
484         [209] = '_', //
485         [210] = '_', //
486         [211] = '_', //
487         [212] = '_', //
488         [213] = '_', //
489         [214] = '_', //
490         [215] = '_', //
491         [216] = '_', //
492         [217] = '_', //
493         [218] = '_', //
494         [219] = '_', //
495         [220] = '_', //
496         [221] = '_', //
497         [222] = '_', //
498         [223] = '_', //
499         [224] = '_', //
500         [225] = '_', //
501         [226] = '_', //
502         [227] = '_', //
503         [228] = '_', //
504         [229] = '_', //
505         [230] = '_', //
506         [231] = '_', //
507         [232] = '_', //
508         [233] = '_', //
509         [234] = '_', //
510         [235] = '_', //
511         [236] = '_', //
512         [237] = '_', //
513         [238] = '_', //
514         [239] = '_', //
515         [240] = '_', //
516         [241] = '_', //
517         [242] = '_', //
518         [243] = '_', //
519         [244] = '_', //
520         [245] = '_', //
521         [246] = '_', //
522         [247] = '_', //
523         [248] = '_', //
524         [249] = '_', //
525         [250] = '_', //
526         [251] = '_', //
527         [252] = '_', //
528         [253] = '_', //
529         [254] = '_', //
530         [255] = '_'  //
531 };
532
533 // make sure the supplied string
534 // is good for a netdata chart/dimension ID/NAME
535 void netdata_fix_chart_name(char *s) {
536     while ((*s = netdata_map_chart_names[(unsigned char) *s])) s++;
537 }
538
539 unsigned char netdata_map_chart_ids[256] = {
540         [0] = '\0', //
541         [1] = '_', //
542         [2] = '_', //
543         [3] = '_', //
544         [4] = '_', //
545         [5] = '_', //
546         [6] = '_', //
547         [7] = '_', //
548         [8] = '_', //
549         [9] = '_', //
550         [10] = '_', //
551         [11] = '_', //
552         [12] = '_', //
553         [13] = '_', //
554         [14] = '_', //
555         [15] = '_', //
556         [16] = '_', //
557         [17] = '_', //
558         [18] = '_', //
559         [19] = '_', //
560         [20] = '_', //
561         [21] = '_', //
562         [22] = '_', //
563         [23] = '_', //
564         [24] = '_', //
565         [25] = '_', //
566         [26] = '_', //
567         [27] = '_', //
568         [28] = '_', //
569         [29] = '_', //
570         [30] = '_', //
571         [31] = '_', //
572         [32] = '_', //
573         [33] = '_', // !
574         [34] = '_', // "
575         [35] = '_', // #
576         [36] = '_', // $
577         [37] = '_', // %
578         [38] = '_', // &
579         [39] = '_', // '
580         [40] = '_', // (
581         [41] = '_', // )
582         [42] = '_', // *
583         [43] = '_', // +
584         [44] = '.', // ,
585         [45] = '-', // -
586         [46] = '.', // .
587         [47] = '_', // /
588         [48] = '0', // 0
589         [49] = '1', // 1
590         [50] = '2', // 2
591         [51] = '3', // 3
592         [52] = '4', // 4
593         [53] = '5', // 5
594         [54] = '6', // 6
595         [55] = '7', // 7
596         [56] = '8', // 8
597         [57] = '9', // 9
598         [58] = '_', // :
599         [59] = '_', // ;
600         [60] = '_', // <
601         [61] = '_', // =
602         [62] = '_', // >
603         [63] = '_', // ?
604         [64] = '_', // @
605         [65] = 'a', // A
606         [66] = 'b', // B
607         [67] = 'c', // C
608         [68] = 'd', // D
609         [69] = 'e', // E
610         [70] = 'f', // F
611         [71] = 'g', // G
612         [72] = 'h', // H
613         [73] = 'i', // I
614         [74] = 'j', // J
615         [75] = 'k', // K
616         [76] = 'l', // L
617         [77] = 'm', // M
618         [78] = 'n', // N
619         [79] = 'o', // O
620         [80] = 'p', // P
621         [81] = 'q', // Q
622         [82] = 'r', // R
623         [83] = 's', // S
624         [84] = 't', // T
625         [85] = 'u', // U
626         [86] = 'v', // V
627         [87] = 'w', // W
628         [88] = 'x', // X
629         [89] = 'y', // Y
630         [90] = 'z', // Z
631         [91] = '_', // [
632         [92] = '/', // backslash
633         [93] = '_', // ]
634         [94] = '_', // ^
635         [95] = '_', // _
636         [96] = '_', // `
637         [97] = 'a', // a
638         [98] = 'b', // b
639         [99] = 'c', // c
640         [100] = 'd', // d
641         [101] = 'e', // e
642         [102] = 'f', // f
643         [103] = 'g', // g
644         [104] = 'h', // h
645         [105] = 'i', // i
646         [106] = 'j', // j
647         [107] = 'k', // k
648         [108] = 'l', // l
649         [109] = 'm', // m
650         [110] = 'n', // n
651         [111] = 'o', // o
652         [112] = 'p', // p
653         [113] = 'q', // q
654         [114] = 'r', // r
655         [115] = 's', // s
656         [116] = 't', // t
657         [117] = 'u', // u
658         [118] = 'v', // v
659         [119] = 'w', // w
660         [120] = 'x', // x
661         [121] = 'y', // y
662         [122] = 'z', // z
663         [123] = '_', // {
664         [124] = '_', // |
665         [125] = '_', // }
666         [126] = '_', // ~
667         [127] = '_', //
668         [128] = '_', //
669         [129] = '_', //
670         [130] = '_', //
671         [131] = '_', //
672         [132] = '_', //
673         [133] = '_', //
674         [134] = '_', //
675         [135] = '_', //
676         [136] = '_', //
677         [137] = '_', //
678         [138] = '_', //
679         [139] = '_', //
680         [140] = '_', //
681         [141] = '_', //
682         [142] = '_', //
683         [143] = '_', //
684         [144] = '_', //
685         [145] = '_', //
686         [146] = '_', //
687         [147] = '_', //
688         [148] = '_', //
689         [149] = '_', //
690         [150] = '_', //
691         [151] = '_', //
692         [152] = '_', //
693         [153] = '_', //
694         [154] = '_', //
695         [155] = '_', //
696         [156] = '_', //
697         [157] = '_', //
698         [158] = '_', //
699         [159] = '_', //
700         [160] = '_', //
701         [161] = '_', //
702         [162] = '_', //
703         [163] = '_', //
704         [164] = '_', //
705         [165] = '_', //
706         [166] = '_', //
707         [167] = '_', //
708         [168] = '_', //
709         [169] = '_', //
710         [170] = '_', //
711         [171] = '_', //
712         [172] = '_', //
713         [173] = '_', //
714         [174] = '_', //
715         [175] = '_', //
716         [176] = '_', //
717         [177] = '_', //
718         [178] = '_', //
719         [179] = '_', //
720         [180] = '_', //
721         [181] = '_', //
722         [182] = '_', //
723         [183] = '_', //
724         [184] = '_', //
725         [185] = '_', //
726         [186] = '_', //
727         [187] = '_', //
728         [188] = '_', //
729         [189] = '_', //
730         [190] = '_', //
731         [191] = '_', //
732         [192] = '_', //
733         [193] = '_', //
734         [194] = '_', //
735         [195] = '_', //
736         [196] = '_', //
737         [197] = '_', //
738         [198] = '_', //
739         [199] = '_', //
740         [200] = '_', //
741         [201] = '_', //
742         [202] = '_', //
743         [203] = '_', //
744         [204] = '_', //
745         [205] = '_', //
746         [206] = '_', //
747         [207] = '_', //
748         [208] = '_', //
749         [209] = '_', //
750         [210] = '_', //
751         [211] = '_', //
752         [212] = '_', //
753         [213] = '_', //
754         [214] = '_', //
755         [215] = '_', //
756         [216] = '_', //
757         [217] = '_', //
758         [218] = '_', //
759         [219] = '_', //
760         [220] = '_', //
761         [221] = '_', //
762         [222] = '_', //
763         [223] = '_', //
764         [224] = '_', //
765         [225] = '_', //
766         [226] = '_', //
767         [227] = '_', //
768         [228] = '_', //
769         [229] = '_', //
770         [230] = '_', //
771         [231] = '_', //
772         [232] = '_', //
773         [233] = '_', //
774         [234] = '_', //
775         [235] = '_', //
776         [236] = '_', //
777         [237] = '_', //
778         [238] = '_', //
779         [239] = '_', //
780         [240] = '_', //
781         [241] = '_', //
782         [242] = '_', //
783         [243] = '_', //
784         [244] = '_', //
785         [245] = '_', //
786         [246] = '_', //
787         [247] = '_', //
788         [248] = '_', //
789         [249] = '_', //
790         [250] = '_', //
791         [251] = '_', //
792         [252] = '_', //
793         [253] = '_', //
794         [254] = '_', //
795         [255] = '_'  //
796 };
797
798 // make sure the supplied string
799 // is good for a netdata chart/dimension ID/NAME
800 void netdata_fix_chart_id(char *s) {
801     while ((*s = netdata_map_chart_ids[(unsigned char) *s])) s++;
802 }
803
804 /*
805 // http://stackoverflow.com/questions/7666509/hash-function-for-string
806 uint32_t simple_hash(const char *name)
807 {
808     const char *s = name;
809     uint32_t hash = 5381;
810     int i;
811
812     while((i = *s++)) hash = ((hash << 5) + hash) + i;
813
814     // fprintf(stderr, "HASH: %lu %s\n", hash, name);
815
816     return hash;
817 }
818 */
819
820 /*
821 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
822 uint32_t simple_hash(const char *name) {
823     unsigned char *s = (unsigned char *) name;
824     uint32_t hval = 0x811c9dc5;
825
826     // FNV-1a algorithm
827     while (*s) {
828         // multiply by the 32 bit FNV magic prime mod 2^32
829         // NOTE: No need to optimize with left shifts.
830         //       GCC will use imul instruction anyway.
831         //       Tested with 'gcc -O3 -S'
832         //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
833         hval *= 16777619;
834
835         // xor the bottom with the current octet
836         hval ^= (uint32_t) *s++;
837     }
838
839     // fprintf(stderr, "HASH: %u = %s\n", hval, name);
840     return hval;
841 }
842
843 uint32_t simple_uhash(const char *name) {
844     unsigned char *s = (unsigned char *) name;
845     uint32_t hval = 0x811c9dc5, c;
846
847     // FNV-1a algorithm
848     while ((c = *s++)) {
849         if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
850         hval *= 16777619;
851         hval ^= c;
852     }
853     return hval;
854 }
855 */
856
857 /*
858 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
859 // one at a time hash
860 uint32_t simple_hash(const char *name) {
861     unsigned char *s = (unsigned char *)name;
862     uint32_t h = 0;
863
864     while(*s) {
865         h += *s++;
866         h += (h << 10);
867         h ^= (h >> 6);
868     }
869
870     h += (h << 3);
871     h ^= (h >> 11);
872     h += (h << 15);
873
874     // fprintf(stderr, "HASH: %u = %s\n", h, name);
875
876     return h;
877 }
878 */
879
880 void strreverse(char *begin, char *end) {
881     while (end > begin) {
882         // clearer code.
883         char aux = *end;
884         *end-- = *begin;
885         *begin++ = aux;
886     }
887 }
888
889 char *mystrsep(char **ptr, char *s) {
890     char *p = "";
891     while (p && !p[0] && *ptr) p = strsep(ptr, s);
892     return (p);
893 }
894
895 char *trim(char *s) {
896     // skip leading spaces
897     // and 'comments' as well!?
898     while (*s && isspace(*s)) s++;
899     if (!*s || *s == '#') return NULL;
900
901     // skip tailing spaces
902     // this way is way faster. Writes only one NUL char.
903     ssize_t l = strlen(s);
904     if (--l >= 0) {
905         char *p = s + l;
906         while (p > s && isspace(*p)) p--;
907         *++p = '\0';
908     }
909
910     if (!*s) return NULL;
911
912     return s;
913 }
914
915 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
916     static int log_madvise_1 = 1;
917 #ifdef MADV_MERGEABLE
918     static int log_madvise_2 = 1, log_madvise_3 = 1;
919 #endif
920     void *mem = NULL;
921
922     errno = 0;
923     int fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
924     if (fd != -1) {
925         if (lseek(fd, size, SEEK_SET) == (off_t) size) {
926             if (write(fd, "", 1) == 1) {
927                 if (ftruncate(fd, size))
928                     error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
929
930 #ifdef MADV_MERGEABLE
931                 if (flags & MAP_SHARED || !enable_ksm || !ksm) {
932 #endif
933                     mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
934                     if (mem == MAP_FAILED) {
935                         error("Cannot allocate SHARED memory for file '%s'.", filename);
936                         mem = NULL;
937                     }
938                     else {
939 #ifdef NETDATA_LOG_ALLOCATIONS
940                         mmap_accounting(size);
941 #endif
942                         int advise = MADV_SEQUENTIAL | MADV_DONTFORK;
943                         if (flags & MAP_SHARED) advise |= MADV_WILLNEED;
944
945                         if (madvise(mem, size, advise) != 0 && log_madvise_1) {
946                             error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
947                             log_madvise_1--;
948                         }
949                     }
950 #ifdef MADV_MERGEABLE
951                 }
952                 else {
953 /*
954                     // test - load the file into memory
955                     mem = calloc(1, size);
956                     if(mem) {
957                         if(lseek(fd, 0, SEEK_SET) == 0) {
958                             if(read(fd, mem, size) != (ssize_t)size)
959                                 error("Cannot read from file '%s'", filename);
960                         }
961                         else
962                             error("Cannot seek to beginning of file '%s'.", filename);
963                     }
964 */
965                     mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
966                     if (mem == MAP_FAILED) {
967                         error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
968                         mem = NULL;
969                     }
970                     else {
971 #ifdef NETDATA_LOG_ALLOCATIONS
972                         mmap_accounting(size);
973 #endif
974                         if (lseek(fd, 0, SEEK_SET) == 0) {
975                             if (read(fd, mem, size) != (ssize_t) size)
976                                 error("Cannot read from file '%s'", filename);
977                         } else
978                             error("Cannot seek to beginning of file '%s'.", filename);
979
980                         // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
981                         if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
982                             error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.",
983                                   filename);
984                             log_madvise_2--;
985                         }
986
987                         if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
988                             error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.",
989                                   filename);
990                             log_madvise_3--;
991                         }
992                     }
993                 }
994 #endif
995             }
996             else
997                 error("Cannot write to file '%s' at position %zu.", filename, size);
998         }
999         else
1000             error("Cannot seek file '%s' to size %zu.", filename, size);
1001
1002         close(fd);
1003     }
1004     else
1005         error("Cannot create/open file '%s'.", filename);
1006
1007     return mem;
1008 }
1009
1010 int savememory(const char *filename, void *mem, size_t size) {
1011     char tmpfilename[FILENAME_MAX + 1];
1012
1013     snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
1014
1015     int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
1016     if (fd < 0) {
1017         error("Cannot create/open file '%s'.", filename);
1018         return -1;
1019     }
1020
1021     if (write(fd, mem, size) != (ssize_t) size) {
1022         error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
1023         close(fd);
1024         return -1;
1025     }
1026
1027     close(fd);
1028
1029     if (rename(tmpfilename, filename)) {
1030         error("Cannot rename '%s' to '%s'", tmpfilename, filename);
1031         return -1;
1032     }
1033
1034     return 0;
1035 }
1036
1037 int fd_is_valid(int fd) {
1038     return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
1039 }
1040
1041 pid_t gettid(void) {
1042 #ifdef __FreeBSD__
1043     return (pid_t)pthread_getthreadid_np();
1044 #elif defined(__APPLE__)
1045     uint64_t curthreadid;
1046     pthread_threadid_np(NULL, &curthreadid);
1047     return (pid_t)curthreadid;
1048 #else
1049     return (pid_t)syscall(SYS_gettid);
1050 #endif /* __FreeBSD__, __APPLE__*/
1051 }
1052
1053 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
1054     char *s = fgets(buf, (int)buf_size, fp);
1055     if (!s) return NULL;
1056
1057     char *t = s;
1058     if (*t != '\0') {
1059         // find the string end
1060         while (*++t != '\0');
1061
1062         // trim trailing spaces/newlines/tabs
1063         while (--t > s && *t == '\n')
1064             *t = '\0';
1065     }
1066
1067     if (len)
1068         *len = t - s + 1;
1069
1070     return s;
1071 }
1072
1073 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
1074     int size = vsnprintf(dst, n, fmt, args);
1075
1076     if (unlikely((size_t) size > n)) {
1077         // truncated
1078         size = (int)n;
1079     }
1080
1081     dst[size] = '\0';
1082     return size;
1083 }
1084
1085 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
1086     va_list args;
1087
1088     va_start(args, fmt);
1089     int ret = vsnprintfz(dst, n, fmt, args);
1090     va_end(args);
1091
1092     return ret;
1093 }
1094
1095 // ----------------------------------------------------------------------------
1096 // system functions
1097 // to retrieve settings of the system
1098
1099 int processors = 1;
1100 long get_system_cpus(void) {
1101     processors = 1;
1102
1103     #ifdef __APPLE__
1104         int32_t tmp_processors;
1105
1106         if (unlikely(GETSYSCTL("hw.logicalcpu", tmp_processors))) {
1107             error("Assuming system has %d processors.", processors);
1108         } else {
1109             processors = tmp_processors;
1110         }
1111
1112         return processors;
1113     #elif __FreeBSD__
1114         int32_t tmp_processors;
1115
1116         if (unlikely(GETSYSCTL("hw.ncpu", tmp_processors))) {
1117             error("Assuming system has %d processors.", processors);
1118         } else {
1119             processors = tmp_processors;
1120         }
1121
1122         return processors;
1123     #else
1124
1125     char filename[FILENAME_MAX + 1];
1126     snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
1127
1128     procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1129     if(!ff) {
1130         error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1131         return processors;
1132     }
1133
1134     ff = procfile_readall(ff);
1135     if(!ff) {
1136         error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1137         return processors;
1138     }
1139
1140     processors = 0;
1141     unsigned int i;
1142     for(i = 0; i < procfile_lines(ff); i++) {
1143         if(!procfile_linewords(ff, i)) continue;
1144
1145         if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
1146     }
1147     processors--;
1148     if(processors < 1) processors = 1;
1149
1150     procfile_close(ff);
1151
1152     debug(D_SYSTEM, "System has %d processors.", processors);
1153     return processors;
1154
1155     #endif /* __APPLE__, __FreeBSD__ */
1156 }
1157
1158 pid_t pid_max = 32768;
1159 pid_t get_system_pid_max(void) {
1160     #ifdef __APPLE__
1161         // As we currently do not know a solution to query pid_max from the os
1162         // we use the number defined in bsd/sys/proc_internal.h in XNU sources
1163         pid_max = 99999;
1164         return pid_max;
1165     #elif __FreeBSD__
1166         int32_t tmp_pid_max;
1167
1168         if (unlikely(GETSYSCTL("kern.pid_max", tmp_pid_max))) {
1169             pid_max = 99999;
1170             error("Assuming system's maximum pid is %d.", pid_max);
1171         } else {
1172             pid_max = tmp_pid_max;
1173         }
1174
1175         return pid_max;
1176     #else
1177
1178     static char read = 0;
1179     if(unlikely(read)) return pid_max;
1180     read = 1;
1181
1182     char filename[FILENAME_MAX + 1];
1183     snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix);
1184
1185     unsigned long long max = 0;
1186     if(read_single_number_file(filename, &max) != 0) {
1187         error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
1188         return pid_max;
1189     }
1190
1191     if(!max) {
1192         error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
1193         return pid_max;
1194     }
1195
1196     pid_max = (pid_t) max;
1197     return pid_max;
1198
1199     #endif /* __APPLE__, __FreeBSD__ */
1200 }
1201
1202 unsigned int hz;
1203 void get_system_HZ(void) {
1204     long ticks;
1205
1206     if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
1207         error("Cannot get system clock ticks");
1208     }
1209
1210     hz = (unsigned int) ticks;
1211 }
1212
1213 /*
1214 // poor man cycle counting
1215 static unsigned long tsc;
1216 void begin_tsc(void) {
1217     unsigned long a, d;
1218     asm volatile ("cpuid\nrdtsc" : "=a" (a), "=d" (d) : "0" (0) : "ebx", "ecx");
1219     tsc = ((unsigned long)d << 32) | (unsigned long)a;
1220 }
1221 unsigned long end_tsc(void) {
1222     unsigned long a, d;
1223     asm volatile ("rdtscp" : "=a" (a), "=d" (d) : : "ecx");
1224     return (((unsigned long)d << 32) | (unsigned long)a) - tsc;
1225 }
1226 */