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