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