]> arthur.barton.de Git - netdata.git/blob - src/common.c
registry folder will be forced
[netdata.git] / src / common.c
1 #include "common.h"
2
3 char *global_host_prefix = "";
4 int enable_ksm = 1;
5
6 volatile sig_atomic_t netdata_exit = 0;
7
8 // ----------------------------------------------------------------------------
9 // memory allocation functions that handle failures
10
11 // although netdata does not use memory allocations too often (netdata tries to
12 // maintain its memory footprint stable during runtime, i.e. all buffers are
13 // allocated during initialization and are adapted to current use throughout
14 // its lifetime), these can be used to override the default system allocation
15 // routines.
16
17 #ifdef NETDATA_LOG_ALLOCATIONS
18 static struct memory_statistics {
19     volatile size_t malloc_calls_made;
20     volatile size_t calloc_calls_made;
21     volatile size_t realloc_calls_made;
22     volatile size_t strdup_calls_made;
23     volatile size_t free_calls_made;
24     volatile size_t memory_calls_made;
25     volatile size_t allocated_memory;
26     volatile size_t mmapped_memory;
27 } memory_statistics;
28
29 static inline void print_allocations(const char *file, const char *function, const unsigned long line) {
30     static struct memory_statistics old = { 0, 0, 0, 0, 0, 0, 0, 0 };
31
32     //if(unlikely(!(memory_statistics.memory_calls_made % 5))) {
33         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",
34                 line, file, function,
35                 (memory_statistics.allocated_memory + 512) / 1024, memory_statistics.allocated_memory - old.allocated_memory,
36                 (memory_statistics.mmapped_memory + 512) / 1024, memory_statistics.mmapped_memory - old.mmapped_memory,
37                 memory_statistics.malloc_calls_made, memory_statistics.malloc_calls_made - old.malloc_calls_made,
38                 memory_statistics.calloc_calls_made, memory_statistics.calloc_calls_made - old.calloc_calls_made,
39                 memory_statistics.realloc_calls_made, memory_statistics.realloc_calls_made - old.realloc_calls_made,
40                 memory_statistics.strdup_calls_made, memory_statistics.strdup_calls_made - old.strdup_calls_made,
41                 memory_statistics.free_calls_made, memory_statistics.free_calls_made - old.free_calls_made
42         );
43
44         memcpy(&old, &memory_statistics, sizeof(struct memory_statistics));
45     //}
46 }
47
48 static inline void malloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
49 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
50     __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
51     __atomic_fetch_add(&memory_statistics.malloc_calls_made, 1, __ATOMIC_SEQ_CST);
52     __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
53 #else
54     // this is for debugging - we don't care locking it
55     memory_statistics.memory_calls_made++;
56     memory_statistics.malloc_calls_made++;
57     memory_statistics.allocated_memory += size;
58 #endif
59     print_allocations(file, function, line);
60 }
61
62 static inline void mmap_accounting(size_t size) {
63 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
64     __atomic_fetch_add(&memory_statistics.malloc_calls_made, 1, __ATOMIC_SEQ_CST);
65     __atomic_fetch_add(&memory_statistics.mmapped_memory, size, __ATOMIC_SEQ_CST);
66 #else
67     // this is for debugging - we don't care locking it
68     memory_statistics.memory_calls_made++;
69     memory_statistics.mmapped_memory += size;
70 #endif
71 }
72
73 static inline void calloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
74 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
75     __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
76     __atomic_fetch_add(&memory_statistics.calloc_calls_made, 1, __ATOMIC_SEQ_CST);
77     __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
78 #else
79     // this is for debugging - we don't care locking it
80     memory_statistics.memory_calls_made++;
81     memory_statistics.calloc_calls_made++;
82     memory_statistics.allocated_memory += size;
83 #endif
84     print_allocations(file, function, line);
85 }
86
87 static inline void realloc_accounting(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
88     (void)ptr;
89
90 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
91     __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
92     __atomic_fetch_add(&memory_statistics.realloc_calls_made, 1, __ATOMIC_SEQ_CST);
93     __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
94 #else
95     // this is for debugging - we don't care locking it
96     memory_statistics.memory_calls_made++;
97     memory_statistics.realloc_calls_made++;
98     memory_statistics.allocated_memory += size;
99 #endif
100     print_allocations(file, function, line);
101 }
102
103 static inline void strdup_accounting(const char *file, const char *function, const unsigned long line, const char *s) {
104     size_t size = strlen(s) + 1;
105
106 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
107     __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
108     __atomic_fetch_add(&memory_statistics.strdup_calls_made, 1, __ATOMIC_SEQ_CST);
109     __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
110 #else
111     // this is for debugging - we don't care locking it
112     memory_statistics.memory_calls_made++;
113     memory_statistics.strdup_calls_made++;
114     memory_statistics.allocated_memory += size;
115 #endif
116     print_allocations(file, function, line);
117 }
118
119 static inline void free_accounting(const char *file, const char *function, const unsigned long line, void *ptr) {
120     (void)file;
121     (void)function;
122     (void)line;
123
124     if(likely(ptr)) {
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.free_calls_made, 1, __ATOMIC_SEQ_CST);
128 #else
129         // this is for debugging - we don't care locking it
130         memory_statistics.memory_calls_made++;
131         memory_statistics.free_calls_made++;
132 #endif
133     }
134 }
135 #endif
136
137 #ifdef NETDATA_LOG_ALLOCATIONS
138 char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s) {
139     strdup_accounting(file, function, line, s);
140 #else
141 char *strdupz(const char *s) {
142 #endif
143
144     char *t = strdup(s);
145     if (unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
146     return t;
147 }
148
149 #ifdef NETDATA_LOG_ALLOCATIONS
150 void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size) {
151     malloc_accounting(file, function, line, size);
152 #else
153 void *mallocz(size_t size) {
154 #endif
155
156     void *p = malloc(size);
157     if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
158     return p;
159 }
160
161 #ifdef NETDATA_LOG_ALLOCATIONS
162 void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size) {
163     calloc_accounting(file, function, line, nmemb * size);
164 #else
165 void *callocz(size_t nmemb, size_t size) {
166 #endif
167
168     void *p = calloc(nmemb, size);
169     if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
170     return p;
171 }
172
173 #ifdef NETDATA_LOG_ALLOCATIONS
174 void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
175     realloc_accounting(file, function, line, ptr, size);
176 #else
177 void *reallocz(void *ptr, size_t size) {
178 #endif
179
180     void *p = realloc(ptr, size);
181     if (unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
182     return p;
183 }
184
185 #ifdef NETDATA_LOG_ALLOCATIONS
186 void freez_int(const char *file, const char *function, const unsigned long line, void *ptr) {
187     free_accounting(file, function, line, ptr);
188 #else
189 void freez(void *ptr) {
190 #endif
191
192     free(ptr);
193 }
194
195 // ----------------------------------------------------------------------------
196 // time functions
197
198 inline unsigned long long timeval_usec(struct timeval *tv) {
199     return tv->tv_sec * 1000000ULL + tv->tv_usec;
200 }
201
202 // time(NULL) in nanoseconds
203 inline unsigned long long time_usec(void) {
204     struct timeval now;
205     gettimeofday(&now, NULL);
206     return timeval_usec(&now);
207 }
208
209 inline unsigned long long usec_dt(struct timeval *now, struct timeval *old) {
210     unsigned long long tv1 = timeval_usec(now);
211     unsigned long long tv2 = timeval_usec(old);
212     return (tv1 > tv2) ? (tv1 - tv2) : (tv2 - tv1);
213 }
214
215 int sleep_usec(unsigned long long 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             info("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     char aux;
868
869     while (end > begin) {
870         // clearer code.
871         aux = *end;
872         *end-- = *begin;
873         *begin++ = aux;
874     }
875 }
876
877 char *mystrsep(char **ptr, char *s) {
878     char *p = "";
879     while (p && !p[0] && *ptr) p = strsep(ptr, s);
880     return (p);
881 }
882
883 char *trim(char *s) {
884     // skip leading spaces
885     // and 'comments' as well!?
886     while (*s && isspace(*s)) s++;
887     if (!*s || *s == '#') return NULL;
888
889     // skip tailing spaces
890     // this way is way faster. Writes only one NUL char.
891     ssize_t l = strlen(s);
892     if (--l >= 0) {
893         char *p = s + l;
894         while (p > s && isspace(*p)) p--;
895         *++p = '\0';
896     }
897
898     if (!*s) return NULL;
899
900     return s;
901 }
902
903 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
904     static int log_madvise_1 = 1;
905 #ifdef MADV_MERGEABLE
906     static int log_madvise_2 = 1, log_madvise_3 = 1;
907 #endif
908     int fd;
909     void *mem = NULL;
910
911     errno = 0;
912     fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
913     if (fd != -1) {
914         if (lseek(fd, size, SEEK_SET) == (off_t) size) {
915             if (write(fd, "", 1) == 1) {
916                 if (ftruncate(fd, size))
917                     error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
918
919 #ifdef MADV_MERGEABLE
920                 if (flags & MAP_SHARED || !enable_ksm || !ksm) {
921 #endif
922                     mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
923                     if (mem == MAP_FAILED) {
924                         error("Cannot allocate SHARED memory for file '%s'.", filename);
925                         mem = NULL;
926                     }
927                     else {
928 #ifdef NETDATA_LOG_ALLOCATIONS
929                         mmap_accounting(size);
930 #endif
931                         int advise = MADV_SEQUENTIAL | MADV_DONTFORK;
932                         if (flags & MAP_SHARED) advise |= MADV_WILLNEED;
933
934                         if (madvise(mem, size, advise) != 0 && log_madvise_1) {
935                             error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
936                             log_madvise_1--;
937                         }
938                     }
939 #ifdef MADV_MERGEABLE
940                 }
941                 else {
942 /*
943                     // test - load the file into memory
944                     mem = calloc(1, size);
945                     if(mem) {
946                         if(lseek(fd, 0, SEEK_SET) == 0) {
947                             if(read(fd, mem, size) != (ssize_t)size)
948                                 error("Cannot read from file '%s'", filename);
949                         }
950                         else
951                             error("Cannot seek to beginning of file '%s'.", filename);
952                     }
953 */
954                     mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
955                     if (mem == MAP_FAILED) {
956                         error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
957                         mem = NULL;
958                     }
959                     else {
960 #ifdef NETDATA_LOG_ALLOCATIONS
961                         mmap_accounting(size);
962 #endif
963                         if (lseek(fd, 0, SEEK_SET) == 0) {
964                             if (read(fd, mem, size) != (ssize_t) size)
965                                 error("Cannot read from file '%s'", filename);
966                         } else
967                             error("Cannot seek to beginning of file '%s'.", filename);
968
969                         // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
970                         if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
971                             error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.",
972                                   filename);
973                             log_madvise_2--;
974                         }
975
976                         if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
977                             error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.",
978                                   filename);
979                             log_madvise_3--;
980                         }
981                     }
982                 }
983 #endif
984             }
985             else
986                 error("Cannot write to file '%s' at position %zu.", filename, size);
987         }
988         else
989             error("Cannot seek file '%s' to size %zu.", filename, size);
990
991         close(fd);
992     }
993     else
994         error("Cannot create/open file '%s'.", filename);
995
996     return mem;
997 }
998
999 int savememory(const char *filename, void *mem, size_t size) {
1000     char tmpfilename[FILENAME_MAX + 1];
1001
1002     snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
1003
1004     int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
1005     if (fd < 0) {
1006         error("Cannot create/open file '%s'.", filename);
1007         return -1;
1008     }
1009
1010     if (write(fd, mem, size) != (ssize_t) size) {
1011         error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
1012         close(fd);
1013         return -1;
1014     }
1015
1016     close(fd);
1017
1018     if (rename(tmpfilename, filename)) {
1019         error("Cannot rename '%s' to '%s'", tmpfilename, filename);
1020         return -1;
1021     }
1022
1023     return 0;
1024 }
1025
1026 int fd_is_valid(int fd) {
1027     return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
1028 }
1029
1030 pid_t gettid(void) {
1031     return (pid_t)syscall(SYS_gettid);
1032 }
1033
1034 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
1035     char *s = fgets(buf, (int)buf_size, fp);
1036     if (!s) return NULL;
1037
1038     char *t = s;
1039     if (*t != '\0') {
1040         // find the string end
1041         while (*++t != '\0');
1042
1043         // trim trailing spaces/newlines/tabs
1044         while (--t > s && *t == '\n')
1045             *t = '\0';
1046     }
1047
1048     if (len)
1049         *len = t - s + 1;
1050
1051     return s;
1052 }
1053
1054 char *strncpyz(char *dst, const char *src, size_t n) {
1055     char *p = dst;
1056
1057     while (*src && n--)
1058         *dst++ = *src++;
1059
1060     *dst = '\0';
1061
1062     return p;
1063 }
1064
1065 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
1066     int size = vsnprintf(dst, n, fmt, args);
1067
1068     if (unlikely((size_t) size > n)) {
1069         // truncated
1070         size = (int)n;
1071     }
1072
1073     dst[size] = '\0';
1074     return size;
1075 }
1076
1077 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
1078     va_list args;
1079
1080     va_start(args, fmt);
1081     int ret = vsnprintfz(dst, n, fmt, args);
1082     va_end(args);
1083
1084     return ret;
1085 }
1086
1087 // ----------------------------------------------------------------------------
1088 // system functions
1089 // to retrieve settings of the system
1090
1091 int processors = 1;
1092 long get_system_cpus(void) {
1093     procfile *ff = NULL;
1094
1095     processors = 1;
1096
1097     char filename[FILENAME_MAX + 1];
1098     snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
1099
1100     ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1101     if(!ff) {
1102         error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1103         return processors;
1104     }
1105
1106     ff = procfile_readall(ff);
1107     if(!ff) {
1108         error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1109         return processors;
1110     }
1111
1112     processors = 0;
1113     unsigned int i;
1114     for(i = 0; i < procfile_lines(ff); i++) {
1115         if(!procfile_linewords(ff, i)) continue;
1116
1117         if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
1118     }
1119     processors--;
1120     if(processors < 1) processors = 1;
1121
1122     procfile_close(ff);
1123
1124     info("System has %d processors.", processors);
1125     return processors;
1126 }
1127
1128 pid_t pid_max = 32768;
1129 pid_t get_system_pid_max(void) {
1130     procfile *ff = NULL;
1131
1132     char filename[FILENAME_MAX + 1];
1133     snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
1134     ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1135     if(!ff) {
1136         error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
1137         return pid_max;
1138     }
1139
1140     ff = procfile_readall(ff);
1141     if(!ff) {
1142         error("Cannot read file '%s'. Assuming system supports %d pids.", filename, pid_max);
1143         return pid_max;
1144     }
1145
1146     pid_max = (pid_t)atoi(procfile_lineword(ff, 0, 0));
1147     if(!pid_max) {
1148         procfile_close(ff);
1149         pid_max = 32768;
1150         error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
1151         return pid_max;
1152     }
1153
1154     procfile_close(ff);
1155     info("System supports %d pids.", pid_max);
1156     return pid_max;
1157 }
1158
1159 unsigned int hz;
1160 void get_system_HZ(void) {
1161     long ticks;
1162
1163     if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
1164         perror("sysconf");
1165     }
1166
1167     hz = (unsigned int) ticks;
1168 }
1169
1170 int read_single_number_file(const char *filename, unsigned long long *result) {
1171     char buffer[1024 + 1];
1172
1173     int fd = open(filename, O_RDONLY, 0666);
1174     if(unlikely(fd == -1)) return 1;
1175
1176     ssize_t r = read(fd, buffer, 1024);
1177     if(unlikely(r == -1)) {
1178         close(fd);
1179         return 2;
1180     }
1181
1182     close(fd);
1183     *result = strtoull(buffer, NULL, 0);
1184     return 0;
1185 }