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 // ----------------------------------------------------------------------------
47 // time(NULL) in milliseconds
48 unsigned long long timems(void) {
50 gettimeofday(&now, NULL);
51 return now.tv_sec * 1000000ULL + now.tv_usec;
54 int usecsleep(unsigned long long usec) {
56 #ifndef NETDATA_WITH_USLEEP
57 // we expect microseconds (1.000.000 per second)
58 // but timespec is nanoseconds (1.000.000.000 per second)
59 struct timespec rem, req = {
60 .tv_sec = (time_t)(usec / 1000000),
61 .tv_nsec = (suseconds_t)((usec % 1000000) * 1000)
64 while(nanosleep(&req, &rem) == -1) {
65 if(likely(errno == EINTR)) {
66 info("nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
67 req.tv_sec = rem.tv_sec;
68 req.tv_nsec = rem.tv_nsec;
71 error("Cannot nanosleep() for %llu microseconds.", usec);
78 int ret = usleep(usec);
79 if(unlikely(ret == -1 && errno == EINVAL)) {
80 // on certain systems, usec has to be up to 999999
82 int counter = usec / 999999;
86 usleep(usec % 999999);
89 error("Cannot usleep() for %llu microseconds.", usec);
95 error("usleep() failed for %llu microseconds.", usec);
101 unsigned char netdata_map_chart_names[256] = {
194 [92] = '/', // backslash
360 // make sure the supplied string
361 // is good for a netdata chart/dimension ID/NAME
362 void netdata_fix_chart_name(char *s) {
363 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
366 unsigned char netdata_map_chart_ids[256] = {
459 [92] = '/', // backslash
625 // make sure the supplied string
626 // is good for a netdata chart/dimension ID/NAME
627 void netdata_fix_chart_id(char *s) {
628 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
632 // http://stackoverflow.com/questions/7666509/hash-function-for-string
633 uint32_t simple_hash(const char *name)
635 const char *s = name;
636 uint32_t hash = 5381;
639 while((i = *s++)) hash = ((hash << 5) + hash) + i;
641 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
648 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
649 uint32_t simple_hash(const char *name) {
650 unsigned char *s = (unsigned char *)name;
651 uint32_t hval = 0x811c9dc5;
655 // multiply by the 32 bit FNV magic prime mod 2^32
656 // NOTE: No need to optimize with left shifts.
657 // GCC will use imul instruction anyway.
658 // Tested with 'gcc -O3 -S'
659 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
662 // xor the bottom with the current octet
663 hval ^= (uint32_t)*s++;
666 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
670 uint32_t simple_uhash(const char *name) {
671 unsigned char *s = (unsigned char *)name;
672 uint32_t hval = 0x811c9dc5, c;
676 if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
684 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
685 // one at a time hash
686 uint32_t simple_hash(const char *name) {
687 unsigned char *s = (unsigned char *)name;
700 // fprintf(stderr, "HASH: %u = %s\n", h, name);
706 void strreverse(char* begin, char* end)
719 char *mystrsep(char **ptr, char *s)
722 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
728 // skip leading spaces
729 // and 'comments' as well!?
730 while(*s && isspace(*s)) s++;
731 if(!*s || *s == '#') return NULL;
733 // skip tailing spaces
734 // this way is way faster. Writes only one NUL char.
735 ssize_t l = strlen(s);
739 while (p > s && isspace(*p)) p--;
748 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
749 static int log_madvise_1 = 1;
750 #ifdef MADV_MERGEABLE
751 static int log_madvise_2 = 1, log_madvise_3 = 1;
757 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
759 if(lseek(fd, size, SEEK_SET) == (off_t)size) {
760 if(write(fd, "", 1) == 1) {
761 if(ftruncate(fd, size))
762 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
764 #ifdef MADV_MERGEABLE
765 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
767 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
768 if(mem != MAP_FAILED) {
769 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
770 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
772 if(madvise(mem, size, advise) != 0 && log_madvise_1) {
773 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
777 #ifdef MADV_MERGEABLE
781 // test - load the file into memory
782 mem = calloc(1, size);
784 if(lseek(fd, 0, SEEK_SET) == 0) {
785 if(read(fd, mem, size) != (ssize_t)size)
786 error("Cannot read from file '%s'", filename);
789 error("Cannot seek to beginning of file '%s'.", filename);
792 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
793 if(mem != MAP_FAILED) {
794 if(lseek(fd, 0, SEEK_SET) == 0) {
795 if(read(fd, mem, size) != (ssize_t)size)
796 error("Cannot read from file '%s'", filename);
799 error("Cannot seek to beginning of file '%s'.", filename);
801 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
802 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0 && log_madvise_2) {
803 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
807 if(madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
808 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
813 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
817 else error("Cannot write to file '%s' at position %zu.", filename, size);
819 else error("Cannot seek file '%s' to size %zu.", filename, size);
823 else error("Cannot create/open file '%s'.", filename);
828 int savememory(const char *filename, void *mem, size_t size)
830 char tmpfilename[FILENAME_MAX + 1];
832 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
834 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
836 error("Cannot create/open file '%s'.", filename);
840 if(write(fd, mem, size) != (ssize_t)size) {
841 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
848 if(rename(tmpfilename, filename)) {
849 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
856 int fd_is_valid(int fd) {
857 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
861 ***************************************************************************
862 * Get number of clock ticks per second.
863 ***************************************************************************
871 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
875 hz = (unsigned int) ticks;
880 return syscall(SYS_gettid);
883 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
884 char *s = fgets(buf, buf_size, fp);
889 // find the string end
890 while (*++t != '\0');
892 // trim trailing spaces/newlines/tabs
893 while (--t > s && *t == '\n')
903 char *strncpyz(char *dst, const char *src, size_t n) {
914 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
917 size = vsnprintf(dst, n, fmt, args);
919 if(unlikely((size_t)size > n)) {
920 // there is bug in vsnprintf() and it returns
921 // a number higher to len, but it does not
922 // overflow the buffer.
930 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
934 int ret = vsnprintfz(dst, n, fmt, args);