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