4 #include <sys/syscall.h>
17 #include "appconfig.h"
18 #include "../config.h"
20 char *global_host_prefix = "";
23 unsigned char netdata_keys_map[256] = {
116 [92] = '/', // backslash
282 // make sure the supplied string
283 // is good for a netdata chart/dimension ID/NAME
284 void netdata_fix_id(char *s) {
285 while((*s = netdata_keys_map[(unsigned char)*s])) s++;
289 // http://stackoverflow.com/questions/7666509/hash-function-for-string
290 uint32_t simple_hash(const char *name)
292 const char *s = name;
293 uint32_t hash = 5381;
296 while((i = *s++)) hash = ((hash << 5) + hash) + i;
298 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
305 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
306 uint32_t simple_hash(const char *name) {
307 unsigned char *s = (unsigned char *)name;
308 uint32_t hval = 0x811c9dc5;
312 // multiply by the 32 bit FNV magic prime mod 2^32
314 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
316 // xor the bottom with the current octet
317 hval ^= (uint32_t)*s++;
320 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
325 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
326 // one at a time hash
327 uint32_t simple_hash(const char *name) {
328 unsigned char *s = (unsigned char *)name;
341 // fprintf(stderr, "HASH: %u = %s\n", h, name);
347 void strreverse(char* begin, char* end)
351 aux = *end, *end-- = *begin, *begin++ = aux;
354 char *mystrsep(char **ptr, char *s)
357 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
363 // skip leading spaces
364 while(*s && isspace(*s)) s++;
365 if(!*s || *s == '#') return NULL;
367 // skip tailing spaces
368 long c = (long) strlen(s) - 1;
369 while(c >= 0 && isspace(s[c])) {
373 if(c < 0) return NULL;
378 void *mymmap(const char *filename, size_t size, int flags, int ksm)
384 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
386 if(lseek(fd, size, SEEK_SET) == (long)size) {
387 if(write(fd, "", 1) == 1) {
388 if(ftruncate(fd, size))
389 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
391 #ifdef MADV_MERGEABLE
392 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
394 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
396 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
397 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
399 if(madvise(mem, size, advise) != 0)
400 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
402 #ifdef MADV_MERGEABLE
405 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
407 if(lseek(fd, 0, SEEK_SET) == 0) {
408 if(read(fd, mem, size) != (ssize_t)size)
409 error("Cannot read from file '%s'", filename);
412 error("Cannot seek to beginning of file '%s'.", filename);
414 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
415 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0)
416 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
418 if(madvise(mem, size, MADV_MERGEABLE) != 0)
419 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
422 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
426 else error("Cannot write to file '%s' at position %ld.", filename, size);
428 else error("Cannot seek file '%s' to size %ld.", filename, size);
432 else error("Cannot create/open file '%s'.", filename);
437 int savememory(const char *filename, void *mem, unsigned long size)
439 char tmpfilename[FILENAME_MAX + 1];
441 snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
443 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
445 error("Cannot create/open file '%s'.", filename);
449 if(write(fd, mem, size) != (long)size) {
450 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
458 if(rename(tmpfilename, filename)) {
459 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
466 int fd_is_valid(int fd) {
467 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
471 ***************************************************************************
472 * Get number of clock ticks per second.
473 ***************************************************************************
481 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
485 hz = (unsigned int) ticks;
490 return syscall(SYS_gettid);