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