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