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