3 char *global_host_prefix = "";
6 volatile sig_atomic_t netdata_exit = 0;
8 // ----------------------------------------------------------------------------
9 // memory allocation functions that handle failures
11 // although netdata does not use memory allocations too often (netdata tries to
12 // maintain its memory footprint stable during runtime, i.e. all buffers are
13 // allocated during initialization and are adapted to current use throughout
14 // its lifetime), these can be used to override the default system allocation
17 char *strdupz(const char *s) {
19 if(unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
23 void *mallocz(size_t size) {
24 void *p = malloc(size);
25 if(unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
29 void *callocz(size_t nmemb, size_t size) {
30 void *p = calloc(nmemb, size);
31 if(unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
35 void *reallocz(void *ptr, size_t size) {
36 void *p = realloc(ptr, size);
37 if(unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
41 void freez(void *ptr) {
45 // ----------------------------------------------------------------------------
48 inline unsigned long long timeval_usec(struct timeval *tv) {
49 return tv->tv_sec * 1000000ULL + tv->tv_usec;
52 // time(NULL) in nanoseconds
53 inline unsigned long long time_usec(void) {
55 gettimeofday(&now, NULL);
56 return timeval_usec(&now);
59 inline unsigned long long usec_dt(struct timeval *now, struct timeval *old) {
60 unsigned long long tv1 = timeval_usec(now);
61 unsigned long long tv2 = timeval_usec(old);
62 return (tv1 > tv2)?(tv1 - tv2):(tv2 - tv1);
65 int sleep_usec(unsigned long long usec) {
67 #ifndef NETDATA_WITH_USLEEP
68 // we expect microseconds (1.000.000 per second)
69 // but timespec is nanoseconds (1.000.000.000 per second)
70 struct timespec rem, req = {
71 .tv_sec = (time_t)(usec / 1000000),
72 .tv_nsec = (suseconds_t)((usec % 1000000) * 1000)
75 while(nanosleep(&req, &rem) == -1) {
76 if(likely(errno == EINTR)) {
77 info("nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
78 req.tv_sec = rem.tv_sec;
79 req.tv_nsec = rem.tv_nsec;
82 error("Cannot nanosleep() for %llu microseconds.", usec);
89 int ret = usleep(usec);
90 if(unlikely(ret == -1 && errno == EINVAL)) {
91 // on certain systems, usec has to be up to 999999
93 int counter = usec / 999999;
97 usleep(usec % 999999);
100 error("Cannot usleep() for %llu microseconds.", usec);
106 error("usleep() failed for %llu microseconds.", usec);
112 unsigned char netdata_map_chart_names[256] = {
205 [92] = '/', // backslash
371 // make sure the supplied string
372 // is good for a netdata chart/dimension ID/NAME
373 void netdata_fix_chart_name(char *s) {
374 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
377 unsigned char netdata_map_chart_ids[256] = {
470 [92] = '/', // backslash
636 // make sure the supplied string
637 // is good for a netdata chart/dimension ID/NAME
638 void netdata_fix_chart_id(char *s) {
639 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
643 // http://stackoverflow.com/questions/7666509/hash-function-for-string
644 uint32_t simple_hash(const char *name)
646 const char *s = name;
647 uint32_t hash = 5381;
650 while((i = *s++)) hash = ((hash << 5) + hash) + i;
652 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
659 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
660 uint32_t simple_hash(const char *name) {
661 unsigned char *s = (unsigned char *)name;
662 uint32_t hval = 0x811c9dc5;
666 // multiply by the 32 bit FNV magic prime mod 2^32
667 // NOTE: No need to optimize with left shifts.
668 // GCC will use imul instruction anyway.
669 // Tested with 'gcc -O3 -S'
670 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
673 // xor the bottom with the current octet
674 hval ^= (uint32_t)*s++;
677 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
681 uint32_t simple_uhash(const char *name) {
682 unsigned char *s = (unsigned char *)name;
683 uint32_t hval = 0x811c9dc5, c;
687 if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
695 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
696 // one at a time hash
697 uint32_t simple_hash(const char *name) {
698 unsigned char *s = (unsigned char *)name;
711 // fprintf(stderr, "HASH: %u = %s\n", h, name);
717 void strreverse(char* begin, char* end)
730 char *mystrsep(char **ptr, char *s)
733 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
739 // skip leading spaces
740 // and 'comments' as well!?
741 while(*s && isspace(*s)) s++;
742 if(!*s || *s == '#') return NULL;
744 // skip tailing spaces
745 // this way is way faster. Writes only one NUL char.
746 ssize_t l = strlen(s);
750 while (p > s && isspace(*p)) p--;
759 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
760 static int log_madvise_1 = 1;
761 #ifdef MADV_MERGEABLE
762 static int log_madvise_2 = 1, log_madvise_3 = 1;
768 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
770 if(lseek(fd, size, SEEK_SET) == (off_t)size) {
771 if(write(fd, "", 1) == 1) {
772 if(ftruncate(fd, size))
773 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
775 #ifdef MADV_MERGEABLE
776 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
778 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
779 if(mem != MAP_FAILED) {
780 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
781 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
783 if(madvise(mem, size, advise) != 0 && log_madvise_1) {
784 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
788 #ifdef MADV_MERGEABLE
792 // test - load the file into memory
793 mem = calloc(1, size);
795 if(lseek(fd, 0, SEEK_SET) == 0) {
796 if(read(fd, mem, size) != (ssize_t)size)
797 error("Cannot read from file '%s'", filename);
800 error("Cannot seek to beginning of file '%s'.", filename);
803 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
804 if(mem != MAP_FAILED) {
805 if(lseek(fd, 0, SEEK_SET) == 0) {
806 if(read(fd, mem, size) != (ssize_t)size)
807 error("Cannot read from file '%s'", filename);
810 error("Cannot seek to beginning of file '%s'.", filename);
812 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
813 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0 && log_madvise_2) {
814 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
818 if(madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
819 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
824 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
828 else error("Cannot write to file '%s' at position %zu.", filename, size);
830 else error("Cannot seek file '%s' to size %zu.", filename, size);
834 else error("Cannot create/open file '%s'.", filename);
839 int savememory(const char *filename, void *mem, size_t size)
841 char tmpfilename[FILENAME_MAX + 1];
843 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
845 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
847 error("Cannot create/open file '%s'.", filename);
851 if(write(fd, mem, size) != (ssize_t)size) {
852 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
859 if(rename(tmpfilename, filename)) {
860 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
867 int fd_is_valid(int fd) {
868 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
872 ***************************************************************************
873 * Get number of clock ticks per second.
874 ***************************************************************************
882 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
886 hz = (unsigned int) ticks;
891 return syscall(SYS_gettid);
894 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
895 char *s = fgets(buf, buf_size, fp);
900 // find the string end
901 while (*++t != '\0');
903 // trim trailing spaces/newlines/tabs
904 while (--t > s && *t == '\n')
914 char *strncpyz(char *dst, const char *src, size_t n) {
925 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
928 size = vsnprintf(dst, n, fmt, args);
930 if(unlikely((size_t)size > n)) {
931 // there is bug in vsnprintf() and it returns
932 // a number higher to len, but it does not
933 // overflow the buffer.
941 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
945 int ret = vsnprintfz(dst, n, fmt, args);