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;
81 error("Cannot nanosleep() for %llu microseconds.", usec);
88 int ret = usleep(usec);
89 if(unlikely(ret == -1 && errno == EINVAL)) {
90 // on certain systems, usec has to be up to 999999
92 int counter = usec / 999999;
96 usleep(usec % 999999);
99 error("Cannot usleep() for %llu microseconds.", usec);
105 error("usleep() failed for %llu microseconds.", usec);
111 unsigned char netdata_map_chart_names[256] = {
204 [92] = '/', // backslash
370 // make sure the supplied string
371 // is good for a netdata chart/dimension ID/NAME
372 void netdata_fix_chart_name(char *s) {
373 while ((*s = netdata_map_chart_names[(unsigned char) *s])) s++;
376 unsigned char netdata_map_chart_ids[256] = {
469 [92] = '/', // backslash
635 // make sure the supplied string
636 // is good for a netdata chart/dimension ID/NAME
637 void netdata_fix_chart_id(char *s) {
638 while ((*s = netdata_map_chart_ids[(unsigned char) *s])) s++;
642 // http://stackoverflow.com/questions/7666509/hash-function-for-string
643 uint32_t simple_hash(const char *name)
645 const char *s = name;
646 uint32_t hash = 5381;
649 while((i = *s++)) hash = ((hash << 5) + hash) + i;
651 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
658 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
659 uint32_t simple_hash(const char *name) {
660 unsigned char *s = (unsigned char *) name;
661 uint32_t hval = 0x811c9dc5;
665 // multiply by the 32 bit FNV magic prime mod 2^32
666 // NOTE: No need to optimize with left shifts.
667 // GCC will use imul instruction anyway.
668 // Tested with 'gcc -O3 -S'
669 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
672 // xor the bottom with the current octet
673 hval ^= (uint32_t) *s++;
676 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
680 uint32_t simple_uhash(const char *name) {
681 unsigned char *s = (unsigned char *) name;
682 uint32_t hval = 0x811c9dc5, c;
686 if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
694 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
695 // one at a time hash
696 uint32_t simple_hash(const char *name) {
697 unsigned char *s = (unsigned char *)name;
710 // fprintf(stderr, "HASH: %u = %s\n", h, name);
716 void strreverse(char *begin, char *end) {
719 while (end > begin) {
727 char *mystrsep(char **ptr, char *s) {
729 while (p && !p[0] && *ptr) p = strsep(ptr, s);
733 char *trim(char *s) {
734 // skip leading spaces
735 // and 'comments' as well!?
736 while (*s && isspace(*s)) s++;
737 if (!*s || *s == '#') return NULL;
739 // skip tailing spaces
740 // this way is way faster. Writes only one NUL char.
741 ssize_t l = strlen(s);
744 while (p > s && isspace(*p)) p--;
748 if (!*s) return NULL;
753 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
754 static int log_madvise_1 = 1;
755 #ifdef MADV_MERGEABLE
756 static int log_madvise_2 = 1, log_madvise_3 = 1;
762 fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
764 if (lseek(fd, size, SEEK_SET) == (off_t) size) {
765 if (write(fd, "", 1) == 1) {
766 if (ftruncate(fd, size))
767 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
769 #ifdef MADV_MERGEABLE
770 if (flags & MAP_SHARED || !enable_ksm || !ksm) {
772 mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
773 if (mem != MAP_FAILED) {
774 int advise = MADV_SEQUENTIAL | MADV_DONTFORK;
775 if (flags & MAP_SHARED) advise |= MADV_WILLNEED;
777 if (madvise(mem, size, advise) != 0 && log_madvise_1) {
778 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
782 #ifdef MADV_MERGEABLE
785 // test - load the file into memory
786 mem = calloc(1, size);
788 if(lseek(fd, 0, SEEK_SET) == 0) {
789 if(read(fd, mem, size) != (ssize_t)size)
790 error("Cannot read from file '%s'", filename);
793 error("Cannot seek to beginning of file '%s'.", filename);
796 mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
797 if (mem != MAP_FAILED) {
798 if (lseek(fd, 0, SEEK_SET) == 0) {
799 if (read(fd, mem, size) != (ssize_t) size)
800 error("Cannot read from file '%s'", filename);
802 error("Cannot seek to beginning of file '%s'.", filename);
804 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
805 if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
806 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.",
811 if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
812 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.",
817 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
821 error("Cannot write to file '%s' at position %zu.", filename, size);
823 error("Cannot seek file '%s' to size %zu.", filename, size);
827 error("Cannot create/open file '%s'.", filename);
832 int savememory(const char *filename, void *mem, size_t size) {
833 char tmpfilename[FILENAME_MAX + 1];
835 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
837 int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
839 error("Cannot create/open file '%s'.", filename);
843 if (write(fd, mem, size) != (ssize_t) size) {
844 error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
851 if (rename(tmpfilename, filename)) {
852 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
859 int fd_is_valid(int fd) {
860 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
864 return (pid_t)syscall(SYS_gettid);
867 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
868 char *s = fgets(buf, (int)buf_size, fp);
873 // find the string end
874 while (*++t != '\0');
876 // trim trailing spaces/newlines/tabs
877 while (--t > s && *t == '\n')
887 char *strncpyz(char *dst, const char *src, size_t n) {
898 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
899 int size = vsnprintf(dst, n, fmt, args);
901 if (unlikely((size_t) size > n)) {
910 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
914 int ret = vsnprintfz(dst, n, fmt, args);
920 // ----------------------------------------------------------------------------
922 // to retrieve settings of the system
925 long get_system_cpus(void) {
930 char filename[FILENAME_MAX + 1];
931 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
933 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
935 error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
939 ff = procfile_readall(ff);
941 error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
947 for(i = 0; i < procfile_lines(ff); i++) {
948 if(!procfile_linewords(ff, i)) continue;
950 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
953 if(processors < 1) processors = 1;
957 info("System has %d processors.", processors);
961 pid_t pid_max = 32768;
962 pid_t get_system_pid_max(void) {
965 char filename[FILENAME_MAX + 1];
966 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
967 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
969 error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
973 ff = procfile_readall(ff);
975 error("Cannot read file '%s'. Assuming system supports %d pids.", filename, pid_max);
979 pid_max = (pid_t)atoi(procfile_lineword(ff, 0, 0));
983 error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
988 info("System supports %d pids.", pid_max);
993 void get_system_HZ(void) {
996 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
1000 hz = (unsigned int) ticks;