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