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