3 char *global_host_prefix = "";
6 // time(NULL) in milliseconds
7 unsigned long long timems(void) {
9 gettimeofday(&now, NULL);
10 return now.tv_sec * 1000000ULL + now.tv_usec;
13 int usecsleep(unsigned long long usec) {
15 #ifndef NETDATA_WITH_USLEEP
16 // we expect microseconds (1.000.000 per second)
17 // but timespec is nanoseconds (1.000.000.000 per second)
18 struct timespec req = { .tv_sec = usec / 1000000, .tv_nsec = (usec % 1000000) * 1000 }, rem;
20 while(nanosleep(&req, &rem) == -1) {
21 if(likely(errno == EINTR)) {
22 info("nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
23 req.tv_sec = rem.tv_sec;
24 req.tv_nsec = rem.tv_nsec;
27 error("Cannot nanosleep() for %llu microseconds.", usec);
34 int ret = usleep(usec);
35 if(unlikely(ret == -1 && errno == EINVAL)) {
36 // on certain systems, usec has to be up to 999999
38 int counter = usec / 999999;
42 usleep(usec % 999999);
45 error("Cannot usleep() for %llu microseconds.", usec);
51 error("usleep() failed for %llu microseconds.", usec);
57 unsigned char netdata_map_chart_names[256] = {
150 [92] = '/', // backslash
316 // make sure the supplied string
317 // is good for a netdata chart/dimension ID/NAME
318 void netdata_fix_chart_name(char *s) {
319 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
322 unsigned char netdata_map_chart_ids[256] = {
415 [92] = '/', // backslash
581 // make sure the supplied string
582 // is good for a netdata chart/dimension ID/NAME
583 void netdata_fix_chart_id(char *s) {
584 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
588 // http://stackoverflow.com/questions/7666509/hash-function-for-string
589 uint32_t simple_hash(const char *name)
591 const char *s = name;
592 uint32_t hash = 5381;
595 while((i = *s++)) hash = ((hash << 5) + hash) + i;
597 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
604 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
605 uint32_t simple_hash(const char *name) {
606 unsigned char *s = (unsigned char *)name;
607 uint32_t hval = 0x811c9dc5;
611 // multiply by the 32 bit FNV magic prime mod 2^32
612 // NOTE: No need to optimize with left shifts.
613 // GCC will use imul instruction anyway.
614 // Tested with 'gcc -O3 -S'
615 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
618 // xor the bottom with the current octet
619 hval ^= (uint32_t)*s++;
622 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
626 uint32_t simple_uhash(const char *name) {
627 unsigned char *s = (unsigned char *)name;
628 uint32_t hval = 0x811c9dc5, c;
632 if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
640 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
641 // one at a time hash
642 uint32_t simple_hash(const char *name) {
643 unsigned char *s = (unsigned char *)name;
656 // fprintf(stderr, "HASH: %u = %s\n", h, name);
662 void strreverse(char* begin, char* end)
675 char *mystrsep(char **ptr, char *s)
678 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
684 // skip leading spaces
685 // and 'comments' as well!?
686 while(*s && isspace(*s)) s++;
687 if(!*s || *s == '#') return NULL;
689 // skip tailing spaces
690 // this way is way faster. Writes only one NUL char.
691 ssize_t l = strlen(s);
695 while (p > s && isspace(*p)) p--;
704 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
705 static int log_madvise_1 = 1;
706 #ifdef MADV_MERGEABLE
707 static int log_madvise_2 = 1, log_madvise_3 = 1;
713 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
715 if(lseek(fd, size, SEEK_SET) == (off_t)size) {
716 if(write(fd, "", 1) == 1) {
717 if(ftruncate(fd, size))
718 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
720 #ifdef MADV_MERGEABLE
721 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
723 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
724 if(mem != MAP_FAILED) {
725 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
726 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
728 if(madvise(mem, size, advise) != 0 && log_madvise_1) {
729 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
733 #ifdef MADV_MERGEABLE
737 // test - load the file into memory
738 mem = calloc(1, size);
740 if(lseek(fd, 0, SEEK_SET) == 0) {
741 if(read(fd, mem, size) != (ssize_t)size)
742 error("Cannot read from file '%s'", filename);
745 error("Cannot seek to beginning of file '%s'.", filename);
748 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
749 if(mem != MAP_FAILED) {
750 if(lseek(fd, 0, SEEK_SET) == 0) {
751 if(read(fd, mem, size) != (ssize_t)size)
752 error("Cannot read from file '%s'", filename);
755 error("Cannot seek to beginning of file '%s'.", filename);
757 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
758 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0 && log_madvise_2) {
759 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
763 if(madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
764 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
769 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
773 else error("Cannot write to file '%s' at position %zu.", filename, size);
775 else error("Cannot seek file '%s' to size %zu.", filename, size);
779 else error("Cannot create/open file '%s'.", filename);
784 int savememory(const char *filename, void *mem, size_t size)
786 char tmpfilename[FILENAME_MAX + 1];
788 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
790 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
792 error("Cannot create/open file '%s'.", filename);
796 if(write(fd, mem, size) != (ssize_t)size) {
797 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
804 if(rename(tmpfilename, filename)) {
805 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
812 int fd_is_valid(int fd) {
813 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
817 ***************************************************************************
818 * Get number of clock ticks per second.
819 ***************************************************************************
827 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
831 hz = (unsigned int) ticks;
836 return syscall(SYS_gettid);
839 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
840 char *s = fgets(buf, buf_size, fp);
845 // find the string end
846 while (*++t != '\0');
848 // trim trailing spaces/newlines/tabs
849 while (--t > s && *t == '\n')
859 char *strncpyz(char *dst, const char *src, size_t n) {
870 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
873 size = vsnprintf(dst, n, fmt, args);
875 if(unlikely((size_t)size > n)) {
876 // there is bug in vsnprintf() and it returns
877 // a number higher to len, but it does not
878 // overflow the buffer.
886 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
890 int ret = vsnprintfz(dst, n, fmt, args);