4 #include <sys/syscall.h>
17 #include "appconfig.h"
18 #include "../config.h"
20 char *global_host_prefix = "";
23 // time(NULL) in milliseconds
24 unsigned long long timems(void) {
26 gettimeofday(&now, NULL);
27 return now.tv_sec * 1000000ULL + now.tv_usec;
30 unsigned char netdata_map_chart_names[256] = {
123 [92] = '/', // backslash
289 // make sure the supplied string
290 // is good for a netdata chart/dimension ID/NAME
291 void netdata_fix_chart_name(char *s) {
292 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
295 unsigned char netdata_map_chart_ids[256] = {
388 [92] = '/', // backslash
554 // make sure the supplied string
555 // is good for a netdata chart/dimension ID/NAME
556 void netdata_fix_chart_id(char *s) {
557 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
561 // http://stackoverflow.com/questions/7666509/hash-function-for-string
562 uint32_t simple_hash(const char *name)
564 const char *s = name;
565 uint32_t hash = 5381;
568 while((i = *s++)) hash = ((hash << 5) + hash) + i;
570 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
577 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
578 uint32_t simple_hash(const char *name) {
579 unsigned char *s = (unsigned char *)name;
580 uint32_t hval = 0x811c9dc5;
584 // multiply by the 32 bit FNV magic prime mod 2^32
586 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
588 // xor the bottom with the current octet
589 hval ^= (uint32_t)*s++;
592 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
597 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
598 // one at a time hash
599 uint32_t simple_hash(const char *name) {
600 unsigned char *s = (unsigned char *)name;
613 // fprintf(stderr, "HASH: %u = %s\n", h, name);
619 void strreverse(char* begin, char* end)
623 aux = *end, *end-- = *begin, *begin++ = aux;
626 char *mystrsep(char **ptr, char *s)
629 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
635 // skip leading spaces
636 while(*s && isspace(*s)) s++;
637 if(!*s || *s == '#') return NULL;
639 // skip tailing spaces
640 long c = (long) strlen(s) - 1;
641 while(c >= 0 && isspace(s[c])) {
645 if(c < 0) return NULL;
650 void *mymmap(const char *filename, size_t size, int flags, int ksm)
656 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
658 if(lseek(fd, size, SEEK_SET) == (long)size) {
659 if(write(fd, "", 1) == 1) {
660 if(ftruncate(fd, size))
661 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
663 #ifdef MADV_MERGEABLE
664 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
666 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
668 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
669 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
671 if(madvise(mem, size, advise) != 0)
672 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
674 #ifdef MADV_MERGEABLE
677 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
679 if(lseek(fd, 0, SEEK_SET) == 0) {
680 if(read(fd, mem, size) != (ssize_t)size)
681 error("Cannot read from file '%s'", filename);
684 error("Cannot seek to beginning of file '%s'.", filename);
686 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
687 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0)
688 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
690 if(madvise(mem, size, MADV_MERGEABLE) != 0)
691 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
694 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
698 else error("Cannot write to file '%s' at position %ld.", filename, size);
700 else error("Cannot seek file '%s' to size %ld.", filename, size);
704 else error("Cannot create/open file '%s'.", filename);
709 int savememory(const char *filename, void *mem, unsigned long size)
711 char tmpfilename[FILENAME_MAX + 1];
713 snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
715 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
717 error("Cannot create/open file '%s'.", filename);
721 if(write(fd, mem, size) != (long)size) {
722 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
730 if(rename(tmpfilename, filename)) {
731 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
738 int fd_is_valid(int fd) {
739 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
743 ***************************************************************************
744 * Get number of clock ticks per second.
745 ***************************************************************************
753 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
757 hz = (unsigned int) ticks;
762 return syscall(SYS_gettid);
765 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
766 char *s = fgets(buf, buf_size, fp);
771 // find the string end
772 while (*++t != '\0');
774 // trim trailing spaces/newlines/tabs
775 while (--t > s && *t == '\n')