4 #include <sys/syscall.h>
17 #include "appconfig.h"
18 #include "../config.h"
20 char *global_host_prefix = "";
23 unsigned char netdata_map_chart_names[256] = {
116 [92] = '/', // backslash
282 // make sure the supplied string
283 // is good for a netdata chart/dimension ID/NAME
284 void netdata_fix_chart_name(char *s) {
285 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
288 unsigned char netdata_map_chart_ids[256] = {
381 [92] = '/', // backslash
547 // make sure the supplied string
548 // is good for a netdata chart/dimension ID/NAME
549 void netdata_fix_chart_id(char *s) {
550 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
554 // http://stackoverflow.com/questions/7666509/hash-function-for-string
555 uint32_t simple_hash(const char *name)
557 const char *s = name;
558 uint32_t hash = 5381;
561 while((i = *s++)) hash = ((hash << 5) + hash) + i;
563 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
570 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
571 uint32_t simple_hash(const char *name) {
572 unsigned char *s = (unsigned char *)name;
573 uint32_t hval = 0x811c9dc5;
577 // multiply by the 32 bit FNV magic prime mod 2^32
579 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
581 // xor the bottom with the current octet
582 hval ^= (uint32_t)*s++;
585 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
590 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
591 // one at a time hash
592 uint32_t simple_hash(const char *name) {
593 unsigned char *s = (unsigned char *)name;
606 // fprintf(stderr, "HASH: %u = %s\n", h, name);
612 void strreverse(char* begin, char* end)
616 aux = *end, *end-- = *begin, *begin++ = aux;
619 char *mystrsep(char **ptr, char *s)
622 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
628 // skip leading spaces
629 while(*s && isspace(*s)) s++;
630 if(!*s || *s == '#') return NULL;
632 // skip tailing spaces
633 long c = (long) strlen(s) - 1;
634 while(c >= 0 && isspace(s[c])) {
638 if(c < 0) return NULL;
643 void *mymmap(const char *filename, size_t size, int flags, int ksm)
649 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
651 if(lseek(fd, size, SEEK_SET) == (long)size) {
652 if(write(fd, "", 1) == 1) {
653 if(ftruncate(fd, size))
654 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
656 #ifdef MADV_MERGEABLE
657 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
659 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
661 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
662 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
664 if(madvise(mem, size, advise) != 0)
665 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
667 #ifdef MADV_MERGEABLE
670 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
672 if(lseek(fd, 0, SEEK_SET) == 0) {
673 if(read(fd, mem, size) != (ssize_t)size)
674 error("Cannot read from file '%s'", filename);
677 error("Cannot seek to beginning of file '%s'.", filename);
679 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
680 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0)
681 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
683 if(madvise(mem, size, MADV_MERGEABLE) != 0)
684 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
687 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
691 else error("Cannot write to file '%s' at position %ld.", filename, size);
693 else error("Cannot seek file '%s' to size %ld.", filename, size);
697 else error("Cannot create/open file '%s'.", filename);
702 int savememory(const char *filename, void *mem, unsigned long size)
704 char tmpfilename[FILENAME_MAX + 1];
706 snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
708 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
710 error("Cannot create/open file '%s'.", filename);
714 if(write(fd, mem, size) != (long)size) {
715 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
723 if(rename(tmpfilename, filename)) {
724 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
731 int fd_is_valid(int fd) {
732 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
736 ***************************************************************************
737 * Get number of clock ticks per second.
738 ***************************************************************************
746 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
750 hz = (unsigned int) ticks;
755 return syscall(SYS_gettid);