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 error("Cannot allocate SHARED memory for file '%s'.", filename);
778 int advise = MADV_SEQUENTIAL | MADV_DONTFORK;
779 if (flags & MAP_SHARED) advise |= MADV_WILLNEED;
781 if (madvise(mem, size, advise) != 0 && log_madvise_1) {
782 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
786 #ifdef MADV_MERGEABLE
790 // test - load the file into memory
791 mem = calloc(1, size);
793 if(lseek(fd, 0, SEEK_SET) == 0) {
794 if(read(fd, mem, size) != (ssize_t)size)
795 error("Cannot read from file '%s'", filename);
798 error("Cannot seek to beginning of file '%s'.", filename);
801 mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
802 if (mem == MAP_FAILED) {
803 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
807 if (lseek(fd, 0, SEEK_SET) == 0) {
808 if (read(fd, mem, size) != (ssize_t) size)
809 error("Cannot read from file '%s'", filename);
811 error("Cannot seek to beginning of file '%s'.", filename);
813 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
814 if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
815 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.",
820 if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
821 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.",
830 error("Cannot write to file '%s' at position %zu.", filename, size);
833 error("Cannot seek file '%s' to size %zu.", filename, size);
838 error("Cannot create/open file '%s'.", filename);
843 int savememory(const char *filename, void *mem, size_t size) {
844 char tmpfilename[FILENAME_MAX + 1];
846 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
848 int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
850 error("Cannot create/open file '%s'.", filename);
854 if (write(fd, mem, size) != (ssize_t) size) {
855 error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
862 if (rename(tmpfilename, filename)) {
863 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
870 int fd_is_valid(int fd) {
871 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
875 return (pid_t)syscall(SYS_gettid);
878 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
879 char *s = fgets(buf, (int)buf_size, fp);
884 // find the string end
885 while (*++t != '\0');
887 // trim trailing spaces/newlines/tabs
888 while (--t > s && *t == '\n')
898 char *strncpyz(char *dst, const char *src, size_t n) {
909 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
910 int size = vsnprintf(dst, n, fmt, args);
912 if (unlikely((size_t) size > n)) {
921 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
925 int ret = vsnprintfz(dst, n, fmt, args);
931 // ----------------------------------------------------------------------------
933 // to retrieve settings of the system
936 long get_system_cpus(void) {
941 char filename[FILENAME_MAX + 1];
942 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
944 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
946 error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
950 ff = procfile_readall(ff);
952 error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
958 for(i = 0; i < procfile_lines(ff); i++) {
959 if(!procfile_linewords(ff, i)) continue;
961 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
964 if(processors < 1) processors = 1;
968 info("System has %d processors.", processors);
972 pid_t pid_max = 32768;
973 pid_t get_system_pid_max(void) {
976 char filename[FILENAME_MAX + 1];
977 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
978 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
980 error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
984 ff = procfile_readall(ff);
986 error("Cannot read file '%s'. Assuming system supports %d pids.", filename, pid_max);
990 pid_max = (pid_t)atoi(procfile_lineword(ff, 0, 0));
994 error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
999 info("System supports %d pids.", pid_max);
1004 void get_system_HZ(void) {
1007 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
1011 hz = (unsigned int) ticks;