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