3 char *global_host_prefix = "";
6 volatile sig_atomic_t netdata_exit = 0;
8 // time(NULL) in milliseconds
9 unsigned long long timems(void) {
11 gettimeofday(&now, NULL);
12 return now.tv_sec * 1000000ULL + now.tv_usec;
15 int usecsleep(unsigned long long usec) {
17 #ifndef NETDATA_WITH_USLEEP
18 // we expect microseconds (1.000.000 per second)
19 // but timespec is nanoseconds (1.000.000.000 per second)
20 struct timespec req = { .tv_sec = usec / 1000000, .tv_nsec = (usec % 1000000) * 1000 }, rem;
22 while(nanosleep(&req, &rem) == -1) {
23 if(likely(errno == EINTR)) {
24 info("nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
25 req.tv_sec = rem.tv_sec;
26 req.tv_nsec = rem.tv_nsec;
29 error("Cannot nanosleep() for %llu microseconds.", usec);
36 int ret = usleep(usec);
37 if(unlikely(ret == -1 && errno == EINVAL)) {
38 // on certain systems, usec has to be up to 999999
40 int counter = usec / 999999;
44 usleep(usec % 999999);
47 error("Cannot usleep() for %llu microseconds.", usec);
53 error("usleep() failed for %llu microseconds.", usec);
59 unsigned char netdata_map_chart_names[256] = {
152 [92] = '/', // backslash
318 // make sure the supplied string
319 // is good for a netdata chart/dimension ID/NAME
320 void netdata_fix_chart_name(char *s) {
321 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
324 unsigned char netdata_map_chart_ids[256] = {
417 [92] = '/', // backslash
583 // make sure the supplied string
584 // is good for a netdata chart/dimension ID/NAME
585 void netdata_fix_chart_id(char *s) {
586 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
590 // http://stackoverflow.com/questions/7666509/hash-function-for-string
591 uint32_t simple_hash(const char *name)
593 const char *s = name;
594 uint32_t hash = 5381;
597 while((i = *s++)) hash = ((hash << 5) + hash) + i;
599 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
606 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
607 uint32_t simple_hash(const char *name) {
608 unsigned char *s = (unsigned char *)name;
609 uint32_t hval = 0x811c9dc5;
613 // multiply by the 32 bit FNV magic prime mod 2^32
614 // NOTE: No need to optimize with left shifts.
615 // GCC will use imul instruction anyway.
616 // Tested with 'gcc -O3 -S'
617 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
620 // xor the bottom with the current octet
621 hval ^= (uint32_t)*s++;
624 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
628 uint32_t simple_uhash(const char *name) {
629 unsigned char *s = (unsigned char *)name;
630 uint32_t hval = 0x811c9dc5, c;
634 if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
642 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
643 // one at a time hash
644 uint32_t simple_hash(const char *name) {
645 unsigned char *s = (unsigned char *)name;
658 // fprintf(stderr, "HASH: %u = %s\n", h, name);
664 void strreverse(char* begin, char* end)
677 char *mystrsep(char **ptr, char *s)
680 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
686 // skip leading spaces
687 // and 'comments' as well!?
688 while(*s && isspace(*s)) s++;
689 if(!*s || *s == '#') return NULL;
691 // skip tailing spaces
692 // this way is way faster. Writes only one NUL char.
693 ssize_t l = strlen(s);
697 while (p > s && isspace(*p)) p--;
706 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
707 static int log_madvise_1 = 1;
708 #ifdef MADV_MERGEABLE
709 static int log_madvise_2 = 1, log_madvise_3 = 1;
715 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
717 if(lseek(fd, size, SEEK_SET) == (off_t)size) {
718 if(write(fd, "", 1) == 1) {
719 if(ftruncate(fd, size))
720 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
722 #ifdef MADV_MERGEABLE
723 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
725 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
726 if(mem != MAP_FAILED) {
727 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
728 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
730 if(madvise(mem, size, advise) != 0 && log_madvise_1) {
731 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
735 #ifdef MADV_MERGEABLE
739 // test - load the file into memory
740 mem = calloc(1, size);
742 if(lseek(fd, 0, SEEK_SET) == 0) {
743 if(read(fd, mem, size) != (ssize_t)size)
744 error("Cannot read from file '%s'", filename);
747 error("Cannot seek to beginning of file '%s'.", filename);
750 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
751 if(mem != MAP_FAILED) {
752 if(lseek(fd, 0, SEEK_SET) == 0) {
753 if(read(fd, mem, size) != (ssize_t)size)
754 error("Cannot read from file '%s'", filename);
757 error("Cannot seek to beginning of file '%s'.", filename);
759 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
760 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0 && log_madvise_2) {
761 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
765 if(madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
766 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
771 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
775 else error("Cannot write to file '%s' at position %zu.", filename, size);
777 else error("Cannot seek file '%s' to size %zu.", filename, size);
781 else error("Cannot create/open file '%s'.", filename);
786 int savememory(const char *filename, void *mem, size_t size)
788 char tmpfilename[FILENAME_MAX + 1];
790 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
792 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
794 error("Cannot create/open file '%s'.", filename);
798 if(write(fd, mem, size) != (ssize_t)size) {
799 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
806 if(rename(tmpfilename, filename)) {
807 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
814 int fd_is_valid(int fd) {
815 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
819 ***************************************************************************
820 * Get number of clock ticks per second.
821 ***************************************************************************
829 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
833 hz = (unsigned int) ticks;
838 return syscall(SYS_gettid);
841 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
842 char *s = fgets(buf, buf_size, fp);
847 // find the string end
848 while (*++t != '\0');
850 // trim trailing spaces/newlines/tabs
851 while (--t > s && *t == '\n')
861 char *strncpyz(char *dst, const char *src, size_t n) {
872 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
875 size = vsnprintf(dst, n, fmt, args);
877 if(unlikely((size_t)size > n)) {
878 // there is bug in vsnprintf() and it returns
879 // a number higher to len, but it does not
880 // overflow the buffer.
888 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
892 int ret = vsnprintfz(dst, n, fmt, args);