]> arthur.barton.de Git - netdata.git/blob - src/common.c
Merge pull request #657 from ktsaou/master
[netdata.git] / src / common.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <sys/syscall.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <sys/mman.h>
13 #include <time.h>
14
15 #include "log.h"
16 #include "common.h"
17 #include "appconfig.h"
18 #include "../config.h"
19
20 char *global_host_prefix = "";
21 int enable_ksm = 1;
22
23 // time(NULL) in milliseconds
24 unsigned long long timems(void) {
25         struct timeval now;
26         gettimeofday(&now, NULL);
27         return now.tv_sec * 1000000ULL + now.tv_usec;
28 }
29
30 int usecsleep(unsigned long long usec) {
31
32 #ifdef NETDATA_WITH_NANOSLEEP
33         // we expect microseconds (1.000.000 per second)
34         // but timespec is nanoseconds (1.000.000.000 per second)
35         struct timespec req = { .tv_sec = usec / 1000000, .tv_nsec = (usec % 1000000) * 1000 }, rem;
36
37         while(nanosleep(&req, &rem) == -1) {
38                 error("nanosleep() failed for %llu microseconds.", usec);
39
40                 if(likely(errno == EINTR)) {
41                         req.tv_sec = rem.tv_sec;
42                         req.tv_nsec = rem.tv_nsec;
43                 }
44                 else {
45                         error("Cannot nanosleep() for %llu microseconds.", usec);
46                         break;
47                 }
48         }
49
50         return 0;
51 #else
52         int ret = usleep(usec);
53         if(unlikely(ret == -1 && errno == EINVAL)) {
54                 // on certain systems, usec has to be up to 999999
55                 if(usec > 999999) {
56                         int counter = usec / 999999;
57                         while(counter--)
58                                 usleep(999999);
59
60                         usleep(usec % 999999);
61                 }
62                 else {
63                         error("Cannot usleep() for %llu microseconds.", usec);
64                         return ret;
65                 }
66         }
67
68         if(ret != 0)
69                 error("usleep() failed for %llu microseconds.", usec);
70
71         return ret;
72 #endif
73 }
74
75 unsigned char netdata_map_chart_names[256] = {
76                 [0] = '\0', //
77                 [1] = '_', //
78                 [2] = '_', //
79                 [3] = '_', //
80                 [4] = '_', //
81                 [5] = '_', //
82                 [6] = '_', //
83                 [7] = '_', //
84                 [8] = '_', //
85                 [9] = '_', //
86                 [10] = '_', //
87                 [11] = '_', //
88                 [12] = '_', //
89                 [13] = '_', //
90                 [14] = '_', //
91                 [15] = '_', //
92                 [16] = '_', //
93                 [17] = '_', //
94                 [18] = '_', //
95                 [19] = '_', //
96                 [20] = '_', //
97                 [21] = '_', //
98                 [22] = '_', //
99                 [23] = '_', //
100                 [24] = '_', //
101                 [25] = '_', //
102                 [26] = '_', //
103                 [27] = '_', //
104                 [28] = '_', //
105                 [29] = '_', //
106                 [30] = '_', //
107                 [31] = '_', //
108                 [32] = '_', //
109                 [33] = '_', // !
110                 [34] = '_', // "
111                 [35] = '_', // #
112                 [36] = '_', // $
113                 [37] = '_', // %
114                 [38] = '_', // &
115                 [39] = '_', // '
116                 [40] = '_', // (
117                 [41] = '_', // )
118                 [42] = '_', // *
119                 [43] = '_', // +
120                 [44] = '.', // ,
121                 [45] = '-', // -
122                 [46] = '.', // .
123                 [47] = '/', // /
124                 [48] = '0', // 0
125                 [49] = '1', // 1
126                 [50] = '2', // 2
127                 [51] = '3', // 3
128                 [52] = '4', // 4
129                 [53] = '5', // 5
130                 [54] = '6', // 6
131                 [55] = '7', // 7
132                 [56] = '8', // 8
133                 [57] = '9', // 9
134                 [58] = '_', // :
135                 [59] = '_', // ;
136                 [60] = '_', // <
137                 [61] = '_', // =
138                 [62] = '_', // >
139                 [63] = '_', // ?
140                 [64] = '_', // @
141                 [65] = 'a', // A
142                 [66] = 'b', // B
143                 [67] = 'c', // C
144                 [68] = 'd', // D
145                 [69] = 'e', // E
146                 [70] = 'f', // F
147                 [71] = 'g', // G
148                 [72] = 'h', // H
149                 [73] = 'i', // I
150                 [74] = 'j', // J
151                 [75] = 'k', // K
152                 [76] = 'l', // L
153                 [77] = 'm', // M
154                 [78] = 'n', // N
155                 [79] = 'o', // O
156                 [80] = 'p', // P
157                 [81] = 'q', // Q
158                 [82] = 'r', // R
159                 [83] = 's', // S
160                 [84] = 't', // T
161                 [85] = 'u', // U
162                 [86] = 'v', // V
163                 [87] = 'w', // W
164                 [88] = 'x', // X
165                 [89] = 'y', // Y
166                 [90] = 'z', // Z
167                 [91] = '_', // [
168                 [92] = '/', // backslash
169                 [93] = '_', // ]
170                 [94] = '_', // ^
171                 [95] = '_', // _
172                 [96] = '_', // `
173                 [97] = 'a', // a
174                 [98] = 'b', // b
175                 [99] = 'c', // c
176                 [100] = 'd', // d
177                 [101] = 'e', // e
178                 [102] = 'f', // f
179                 [103] = 'g', // g
180                 [104] = 'h', // h
181                 [105] = 'i', // i
182                 [106] = 'j', // j
183                 [107] = 'k', // k
184                 [108] = 'l', // l
185                 [109] = 'm', // m
186                 [110] = 'n', // n
187                 [111] = 'o', // o
188                 [112] = 'p', // p
189                 [113] = 'q', // q
190                 [114] = 'r', // r
191                 [115] = 's', // s
192                 [116] = 't', // t
193                 [117] = 'u', // u
194                 [118] = 'v', // v
195                 [119] = 'w', // w
196                 [120] = 'x', // x
197                 [121] = 'y', // y
198                 [122] = 'z', // z
199                 [123] = '_', // {
200                 [124] = '_', // |
201                 [125] = '_', // }
202                 [126] = '_', // ~
203                 [127] = '_', //
204                 [128] = '_', //
205                 [129] = '_', //
206                 [130] = '_', //
207                 [131] = '_', //
208                 [132] = '_', //
209                 [133] = '_', //
210                 [134] = '_', //
211                 [135] = '_', //
212                 [136] = '_', //
213                 [137] = '_', //
214                 [138] = '_', //
215                 [139] = '_', //
216                 [140] = '_', //
217                 [141] = '_', //
218                 [142] = '_', //
219                 [143] = '_', //
220                 [144] = '_', //
221                 [145] = '_', //
222                 [146] = '_', //
223                 [147] = '_', //
224                 [148] = '_', //
225                 [149] = '_', //
226                 [150] = '_', //
227                 [151] = '_', //
228                 [152] = '_', //
229                 [153] = '_', //
230                 [154] = '_', //
231                 [155] = '_', //
232                 [156] = '_', //
233                 [157] = '_', //
234                 [158] = '_', //
235                 [159] = '_', //
236                 [160] = '_', //
237                 [161] = '_', //
238                 [162] = '_', //
239                 [163] = '_', //
240                 [164] = '_', //
241                 [165] = '_', //
242                 [166] = '_', //
243                 [167] = '_', //
244                 [168] = '_', //
245                 [169] = '_', //
246                 [170] = '_', //
247                 [171] = '_', //
248                 [172] = '_', //
249                 [173] = '_', //
250                 [174] = '_', //
251                 [175] = '_', //
252                 [176] = '_', //
253                 [177] = '_', //
254                 [178] = '_', //
255                 [179] = '_', //
256                 [180] = '_', //
257                 [181] = '_', //
258                 [182] = '_', //
259                 [183] = '_', //
260                 [184] = '_', //
261                 [185] = '_', //
262                 [186] = '_', //
263                 [187] = '_', //
264                 [188] = '_', //
265                 [189] = '_', //
266                 [190] = '_', //
267                 [191] = '_', //
268                 [192] = '_', //
269                 [193] = '_', //
270                 [194] = '_', //
271                 [195] = '_', //
272                 [196] = '_', //
273                 [197] = '_', //
274                 [198] = '_', //
275                 [199] = '_', //
276                 [200] = '_', //
277                 [201] = '_', //
278                 [202] = '_', //
279                 [203] = '_', //
280                 [204] = '_', //
281                 [205] = '_', //
282                 [206] = '_', //
283                 [207] = '_', //
284                 [208] = '_', //
285                 [209] = '_', //
286                 [210] = '_', //
287                 [211] = '_', //
288                 [212] = '_', //
289                 [213] = '_', //
290                 [214] = '_', //
291                 [215] = '_', //
292                 [216] = '_', //
293                 [217] = '_', //
294                 [218] = '_', //
295                 [219] = '_', //
296                 [220] = '_', //
297                 [221] = '_', //
298                 [222] = '_', //
299                 [223] = '_', //
300                 [224] = '_', //
301                 [225] = '_', //
302                 [226] = '_', //
303                 [227] = '_', //
304                 [228] = '_', //
305                 [229] = '_', //
306                 [230] = '_', //
307                 [231] = '_', //
308                 [232] = '_', //
309                 [233] = '_', //
310                 [234] = '_', //
311                 [235] = '_', //
312                 [236] = '_', //
313                 [237] = '_', //
314                 [238] = '_', //
315                 [239] = '_', //
316                 [240] = '_', //
317                 [241] = '_', //
318                 [242] = '_', //
319                 [243] = '_', //
320                 [244] = '_', //
321                 [245] = '_', //
322                 [246] = '_', //
323                 [247] = '_', //
324                 [248] = '_', //
325                 [249] = '_', //
326                 [250] = '_', //
327                 [251] = '_', //
328                 [252] = '_', //
329                 [253] = '_', //
330                 [254] = '_', //
331                 [255] = '_'  //
332 };
333
334 // make sure the supplied string
335 // is good for a netdata chart/dimension ID/NAME
336 void netdata_fix_chart_name(char *s) {
337         while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
338 }
339
340 unsigned char netdata_map_chart_ids[256] = {
341                 [0] = '\0', //
342                 [1] = '_', //
343                 [2] = '_', //
344                 [3] = '_', //
345                 [4] = '_', //
346                 [5] = '_', //
347                 [6] = '_', //
348                 [7] = '_', //
349                 [8] = '_', //
350                 [9] = '_', //
351                 [10] = '_', //
352                 [11] = '_', //
353                 [12] = '_', //
354                 [13] = '_', //
355                 [14] = '_', //
356                 [15] = '_', //
357                 [16] = '_', //
358                 [17] = '_', //
359                 [18] = '_', //
360                 [19] = '_', //
361                 [20] = '_', //
362                 [21] = '_', //
363                 [22] = '_', //
364                 [23] = '_', //
365                 [24] = '_', //
366                 [25] = '_', //
367                 [26] = '_', //
368                 [27] = '_', //
369                 [28] = '_', //
370                 [29] = '_', //
371                 [30] = '_', //
372                 [31] = '_', //
373                 [32] = '_', //
374                 [33] = '_', // !
375                 [34] = '_', // "
376                 [35] = '_', // #
377                 [36] = '_', // $
378                 [37] = '_', // %
379                 [38] = '_', // &
380                 [39] = '_', // '
381                 [40] = '_', // (
382                 [41] = '_', // )
383                 [42] = '_', // *
384                 [43] = '_', // +
385                 [44] = '.', // ,
386                 [45] = '-', // -
387                 [46] = '.', // .
388                 [47] = '_', // /
389                 [48] = '0', // 0
390                 [49] = '1', // 1
391                 [50] = '2', // 2
392                 [51] = '3', // 3
393                 [52] = '4', // 4
394                 [53] = '5', // 5
395                 [54] = '6', // 6
396                 [55] = '7', // 7
397                 [56] = '8', // 8
398                 [57] = '9', // 9
399                 [58] = '_', // :
400                 [59] = '_', // ;
401                 [60] = '_', // <
402                 [61] = '_', // =
403                 [62] = '_', // >
404                 [63] = '_', // ?
405                 [64] = '_', // @
406                 [65] = 'a', // A
407                 [66] = 'b', // B
408                 [67] = 'c', // C
409                 [68] = 'd', // D
410                 [69] = 'e', // E
411                 [70] = 'f', // F
412                 [71] = 'g', // G
413                 [72] = 'h', // H
414                 [73] = 'i', // I
415                 [74] = 'j', // J
416                 [75] = 'k', // K
417                 [76] = 'l', // L
418                 [77] = 'm', // M
419                 [78] = 'n', // N
420                 [79] = 'o', // O
421                 [80] = 'p', // P
422                 [81] = 'q', // Q
423                 [82] = 'r', // R
424                 [83] = 's', // S
425                 [84] = 't', // T
426                 [85] = 'u', // U
427                 [86] = 'v', // V
428                 [87] = 'w', // W
429                 [88] = 'x', // X
430                 [89] = 'y', // Y
431                 [90] = 'z', // Z
432                 [91] = '_', // [
433                 [92] = '/', // backslash
434                 [93] = '_', // ]
435                 [94] = '_', // ^
436                 [95] = '_', // _
437                 [96] = '_', // `
438                 [97] = 'a', // a
439                 [98] = 'b', // b
440                 [99] = 'c', // c
441                 [100] = 'd', // d
442                 [101] = 'e', // e
443                 [102] = 'f', // f
444                 [103] = 'g', // g
445                 [104] = 'h', // h
446                 [105] = 'i', // i
447                 [106] = 'j', // j
448                 [107] = 'k', // k
449                 [108] = 'l', // l
450                 [109] = 'm', // m
451                 [110] = 'n', // n
452                 [111] = 'o', // o
453                 [112] = 'p', // p
454                 [113] = 'q', // q
455                 [114] = 'r', // r
456                 [115] = 's', // s
457                 [116] = 't', // t
458                 [117] = 'u', // u
459                 [118] = 'v', // v
460                 [119] = 'w', // w
461                 [120] = 'x', // x
462                 [121] = 'y', // y
463                 [122] = 'z', // z
464                 [123] = '_', // {
465                 [124] = '_', // |
466                 [125] = '_', // }
467                 [126] = '_', // ~
468                 [127] = '_', //
469                 [128] = '_', //
470                 [129] = '_', //
471                 [130] = '_', //
472                 [131] = '_', //
473                 [132] = '_', //
474                 [133] = '_', //
475                 [134] = '_', //
476                 [135] = '_', //
477                 [136] = '_', //
478                 [137] = '_', //
479                 [138] = '_', //
480                 [139] = '_', //
481                 [140] = '_', //
482                 [141] = '_', //
483                 [142] = '_', //
484                 [143] = '_', //
485                 [144] = '_', //
486                 [145] = '_', //
487                 [146] = '_', //
488                 [147] = '_', //
489                 [148] = '_', //
490                 [149] = '_', //
491                 [150] = '_', //
492                 [151] = '_', //
493                 [152] = '_', //
494                 [153] = '_', //
495                 [154] = '_', //
496                 [155] = '_', //
497                 [156] = '_', //
498                 [157] = '_', //
499                 [158] = '_', //
500                 [159] = '_', //
501                 [160] = '_', //
502                 [161] = '_', //
503                 [162] = '_', //
504                 [163] = '_', //
505                 [164] = '_', //
506                 [165] = '_', //
507                 [166] = '_', //
508                 [167] = '_', //
509                 [168] = '_', //
510                 [169] = '_', //
511                 [170] = '_', //
512                 [171] = '_', //
513                 [172] = '_', //
514                 [173] = '_', //
515                 [174] = '_', //
516                 [175] = '_', //
517                 [176] = '_', //
518                 [177] = '_', //
519                 [178] = '_', //
520                 [179] = '_', //
521                 [180] = '_', //
522                 [181] = '_', //
523                 [182] = '_', //
524                 [183] = '_', //
525                 [184] = '_', //
526                 [185] = '_', //
527                 [186] = '_', //
528                 [187] = '_', //
529                 [188] = '_', //
530                 [189] = '_', //
531                 [190] = '_', //
532                 [191] = '_', //
533                 [192] = '_', //
534                 [193] = '_', //
535                 [194] = '_', //
536                 [195] = '_', //
537                 [196] = '_', //
538                 [197] = '_', //
539                 [198] = '_', //
540                 [199] = '_', //
541                 [200] = '_', //
542                 [201] = '_', //
543                 [202] = '_', //
544                 [203] = '_', //
545                 [204] = '_', //
546                 [205] = '_', //
547                 [206] = '_', //
548                 [207] = '_', //
549                 [208] = '_', //
550                 [209] = '_', //
551                 [210] = '_', //
552                 [211] = '_', //
553                 [212] = '_', //
554                 [213] = '_', //
555                 [214] = '_', //
556                 [215] = '_', //
557                 [216] = '_', //
558                 [217] = '_', //
559                 [218] = '_', //
560                 [219] = '_', //
561                 [220] = '_', //
562                 [221] = '_', //
563                 [222] = '_', //
564                 [223] = '_', //
565                 [224] = '_', //
566                 [225] = '_', //
567                 [226] = '_', //
568                 [227] = '_', //
569                 [228] = '_', //
570                 [229] = '_', //
571                 [230] = '_', //
572                 [231] = '_', //
573                 [232] = '_', //
574                 [233] = '_', //
575                 [234] = '_', //
576                 [235] = '_', //
577                 [236] = '_', //
578                 [237] = '_', //
579                 [238] = '_', //
580                 [239] = '_', //
581                 [240] = '_', //
582                 [241] = '_', //
583                 [242] = '_', //
584                 [243] = '_', //
585                 [244] = '_', //
586                 [245] = '_', //
587                 [246] = '_', //
588                 [247] = '_', //
589                 [248] = '_', //
590                 [249] = '_', //
591                 [250] = '_', //
592                 [251] = '_', //
593                 [252] = '_', //
594                 [253] = '_', //
595                 [254] = '_', //
596                 [255] = '_'  //
597 };
598
599 // make sure the supplied string
600 // is good for a netdata chart/dimension ID/NAME
601 void netdata_fix_chart_id(char *s) {
602         while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
603 }
604
605 /*
606 // http://stackoverflow.com/questions/7666509/hash-function-for-string
607 uint32_t simple_hash(const char *name)
608 {
609         const char *s = name;
610         uint32_t hash = 5381;
611         int i;
612
613         while((i = *s++)) hash = ((hash << 5) + hash) + i;
614
615         // fprintf(stderr, "HASH: %lu %s\n", hash, name);
616
617         return hash;
618 }
619 */
620
621
622 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
623 uint32_t simple_hash(const char *name) {
624         unsigned char *s = (unsigned char *)name;
625         uint32_t hval = 0x811c9dc5;
626
627         // FNV-1a algorithm
628         while (*s) {
629                 // multiply by the 32 bit FNV magic prime mod 2^32
630                 // NOTE: No need to optimize with left shifts.
631                 //       GCC will use imul instruction anyway.
632                 //       Tested with 'gcc -O3 -S'
633                 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
634                 hval *= 16777619;
635
636                 // xor the bottom with the current octet
637                 hval ^= (uint32_t)*s++;
638         }
639
640         // fprintf(stderr, "HASH: %u = %s\n", hval, name);
641         return hval;
642 }
643
644 uint32_t simple_uhash(const char *name) {
645         unsigned char *s = (unsigned char *)name;
646         uint32_t hval = 0x811c9dc5, c;
647
648         // FNV-1a algorithm
649         while((c = *s++)) {
650                 if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
651                 hval *= 16777619;
652                 hval ^= c;
653         }
654         return hval;
655 }
656
657 /*
658 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
659 // one at a time hash
660 uint32_t simple_hash(const char *name) {
661         unsigned char *s = (unsigned char *)name;
662         uint32_t h = 0;
663
664         while(*s) {
665                 h += *s++;
666                 h += (h << 10);
667                 h ^= (h >> 6);
668         }
669
670         h += (h << 3);
671         h ^= (h >> 11);
672         h += (h << 15);
673
674         // fprintf(stderr, "HASH: %u = %s\n", h, name);
675
676         return h;
677 }
678 */
679
680 void strreverse(char* begin, char* end)
681 {
682         char aux;
683
684         while (end > begin)
685         {
686                 // clearer code.
687                 aux = *end;
688                 *end-- = *begin;
689                 *begin++ = aux;
690         }
691 }
692
693 char *mystrsep(char **ptr, char *s)
694 {
695         char *p = "";
696         while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
697         return(p);
698 }
699
700 char *trim(char *s)
701 {
702         // skip leading spaces
703         // and 'comments' as well!?
704         while(*s && isspace(*s)) s++;
705         if(!*s || *s == '#') return NULL;
706
707         // skip tailing spaces
708         // this way is way faster. Writes only one NUL char.
709         ssize_t l = strlen(s);
710   if (--l >= 0)
711         {
712                 char *p = s + l;
713                 while (p > s && isspace(*p)) p--;
714                 *++p = '\0';
715         }
716
717         if(!*s) return NULL;
718
719         return s;
720 }
721
722 void *mymmap(const char *filename, size_t size, int flags, int ksm)
723 {
724         int fd;
725         void *mem = NULL;
726
727         errno = 0;
728         fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
729         if(fd != -1) {
730                 if(lseek(fd, size, SEEK_SET) == (off_t)size) {
731                         if(write(fd, "", 1) == 1) {
732                                 if(ftruncate(fd, size))
733                                         error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
734
735 #ifdef MADV_MERGEABLE
736                                 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
737 #endif
738                                         mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
739                                         if(mem != MAP_FAILED) {
740                                                 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
741                                                 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
742
743                                                 if(madvise(mem, size, advise) != 0)
744                                                         error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
745                                         }
746 #ifdef MADV_MERGEABLE
747                                 }
748                                 else {
749 /*
750                                         // test - load the file into memory
751                                         mem = calloc(1, size);
752                                         if(mem) {
753                                                 if(lseek(fd, 0, SEEK_SET) == 0) {
754                                                         if(read(fd, mem, size) != (ssize_t)size)
755                                                                 error("Cannot read from file '%s'", filename);
756                                                 }
757                                                 else
758                                                         error("Cannot seek to beginning of file '%s'.", filename);
759                                         }
760 */
761                                         mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
762                                         if(mem != MAP_FAILED) {
763                                                 if(lseek(fd, 0, SEEK_SET) == 0) {
764                                                         if(read(fd, mem, size) != (ssize_t)size)
765                                                                 error("Cannot read from file '%s'", filename);
766                                                 }
767                                                 else
768                                                         error("Cannot seek to beginning of file '%s'.", filename);
769
770                                                 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
771                                                 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0)
772                                                         error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
773
774                                                 if(madvise(mem, size, MADV_MERGEABLE) != 0)
775                                                         error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
776                                         }
777                                         else
778                                                 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
779                                 }
780 #endif
781                         }
782                         else error("Cannot write to file '%s' at position %zu.", filename, size);
783                 }
784                 else error("Cannot seek file '%s' to size %zu.", filename, size);
785
786                 close(fd);
787         }
788         else error("Cannot create/open file '%s'.", filename);
789
790         return mem;
791 }
792
793 int savememory(const char *filename, void *mem, size_t size)
794 {
795         char tmpfilename[FILENAME_MAX + 1];
796
797         snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
798
799         int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
800         if(fd < 0) {
801                 error("Cannot create/open file '%s'.", filename);
802                 return -1;
803         }
804
805         if(write(fd, mem, size) != (ssize_t)size) {
806                 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
807                 close(fd);
808                 return -1;
809         }
810
811         close(fd);
812
813         if(rename(tmpfilename, filename)) {
814                 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
815                 return -1;
816         }
817
818         return 0;
819 }
820
821 int fd_is_valid(int fd) {
822     return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
823 }
824
825 /*
826  ***************************************************************************
827  * Get number of clock ticks per second.
828  ***************************************************************************
829  */
830 unsigned int hz;
831
832 void get_HZ(void)
833 {
834         long ticks;
835
836         if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
837                 perror("sysconf");
838         }
839
840         hz = (unsigned int) ticks;
841 }
842
843 pid_t gettid(void)
844 {
845         return syscall(SYS_gettid);
846 }
847
848 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
849         char *s = fgets(buf, buf_size, fp);
850         if(!s) return NULL;
851
852         char *t = s;
853         if(*t != '\0') {
854                 // find the string end
855                 while (*++t != '\0');
856
857                 // trim trailing spaces/newlines/tabs
858                 while (--t > s && *t == '\n')
859                         *t = '\0';
860         }
861
862         if(len)
863                 *len = t - s + 1;
864
865         return s;
866 }
867
868 char *strncpyz(char *dst, const char *src, size_t n) {
869         char *p = dst;
870
871         while(*src && n--)
872                 *dst++ = *src++;
873
874         *dst = '\0';
875
876         return p;
877 }
878
879 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
880         int size;
881
882         size = vsnprintf(dst, n, fmt, args);
883
884         if(unlikely((size_t)size > n)) {
885                 // there is bug in vsnprintf() and it returns
886                 // a number higher to len, but it does not
887                 // overflow the buffer.
888                 size = n;
889         }
890
891         dst[size] = '\0';
892         return size;
893 }
894
895 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
896         va_list args;
897
898         va_start(args, fmt);
899         int ret = vsnprintfz(dst, n, fmt, args);
900         va_end(args);
901
902         return ret;
903 }