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 #ifdef NETDATA_LOG_ALLOCATIONS
18 static struct memory_statistics {
19 size_t malloc_calls_made;
20 size_t calloc_calls_made;
21 size_t realloc_calls_made;
22 size_t strdup_calls_made;
23 size_t free_calls_made;
24 size_t memory_calls_made;
25 size_t allocated_memory;
26 size_t mmapped_memory;
27 } memory_statistics = {
28 0, 0, 0, 0, 0, 0, 0, 0
31 static inline void print_allocations(const char *file, const char *function, const unsigned long line) {
32 static struct memory_statistics old = { 0, 0, 0, 0, 0, 0, 0, 0 };
34 //if(unlikely(!(memory_statistics.memory_calls_made % 5))) {
35 fprintf(stderr, "(%04lu@%-10.10s:%-15.15s): Allocated %zu KB (+%zu B), mmapped %zu KB (+%zu B): malloc %zu (+%zu), calloc %zu (+%zu), realloc %zu (+%zu), strdup %zu (+%zu), free %zu (+%zu)\n",
37 (memory_statistics.allocated_memory + 512) / 1024, memory_statistics.allocated_memory - old.allocated_memory,
38 (memory_statistics.mmapped_memory + 512) / 1024, memory_statistics.mmapped_memory - old.mmapped_memory,
39 memory_statistics.malloc_calls_made, memory_statistics.malloc_calls_made - old.malloc_calls_made,
40 memory_statistics.calloc_calls_made, memory_statistics.calloc_calls_made - old.calloc_calls_made,
41 memory_statistics.realloc_calls_made, memory_statistics.realloc_calls_made - old.realloc_calls_made,
42 memory_statistics.strdup_calls_made, memory_statistics.strdup_calls_made - old.strdup_calls_made,
43 memory_statistics.free_calls_made, memory_statistics.free_calls_made - old.free_calls_made
46 memcpy(&old, &memory_statistics, sizeof(struct memory_statistics));
50 static inline void malloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
51 memory_statistics.memory_calls_made++;
52 memory_statistics.malloc_calls_made++;
53 memory_statistics.allocated_memory += size;
54 print_allocations(file, function, line);
57 static inline void mmap_accounting(size_t size) {
58 memory_statistics.memory_calls_made++;
59 memory_statistics.mmapped_memory += size;
62 static inline void calloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
63 memory_statistics.memory_calls_made++;
64 memory_statistics.calloc_calls_made++;
65 memory_statistics.allocated_memory += size;
66 print_allocations(file, function, line);
69 static inline void realloc_accounting(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
72 memory_statistics.memory_calls_made++;
73 memory_statistics.realloc_calls_made++;
74 memory_statistics.allocated_memory += size;
75 print_allocations(file, function, line);
78 static inline void strdup_accounting(const char *file, const char *function, const unsigned long line, const char *s) {
79 memory_statistics.memory_calls_made++;
80 memory_statistics.strdup_calls_made++;
81 memory_statistics.allocated_memory += strlen(s) + 1;
82 print_allocations(file, function, line);
85 static inline void free_accounting(const char *file, const char *function, const unsigned long line, void *ptr) {
91 memory_statistics.memory_calls_made++;
92 memory_statistics.free_calls_made++;
97 #ifdef NETDATA_LOG_ALLOCATIONS
98 char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s) {
99 strdup_accounting(file, function, line, s);
101 char *strdupz(const char *s) {
105 if (unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
109 #ifdef NETDATA_LOG_ALLOCATIONS
110 void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size) {
111 malloc_accounting(file, function, line, size);
113 void *mallocz(size_t size) {
116 void *p = malloc(size);
117 if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
121 #ifdef NETDATA_LOG_ALLOCATIONS
122 void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size) {
123 calloc_accounting(file, function, line, nmemb * size);
125 void *callocz(size_t nmemb, size_t size) {
128 void *p = calloc(nmemb, size);
129 if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
133 #ifdef NETDATA_LOG_ALLOCATIONS
134 void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
135 realloc_accounting(file, function, line, ptr, size);
137 void *reallocz(void *ptr, size_t size) {
140 void *p = realloc(ptr, size);
141 if (unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
145 #ifdef NETDATA_LOG_ALLOCATIONS
146 void freez_int(const char *file, const char *function, const unsigned long line, void *ptr) {
147 free_accounting(file, function, line, ptr);
149 void freez(void *ptr) {
155 // ----------------------------------------------------------------------------
158 inline unsigned long long timeval_usec(struct timeval *tv) {
159 return tv->tv_sec * 1000000ULL + tv->tv_usec;
162 // time(NULL) in nanoseconds
163 inline unsigned long long time_usec(void) {
165 gettimeofday(&now, NULL);
166 return timeval_usec(&now);
169 inline unsigned long long usec_dt(struct timeval *now, struct timeval *old) {
170 unsigned long long tv1 = timeval_usec(now);
171 unsigned long long tv2 = timeval_usec(old);
172 return (tv1 > tv2) ? (tv1 - tv2) : (tv2 - tv1);
175 int sleep_usec(unsigned long long usec) {
177 #ifndef NETDATA_WITH_USLEEP
178 // we expect microseconds (1.000.000 per second)
179 // but timespec is nanoseconds (1.000.000.000 per second)
180 struct timespec rem, req = {
181 .tv_sec = (time_t) (usec / 1000000),
182 .tv_nsec = (suseconds_t) ((usec % 1000000) * 1000)
185 while (nanosleep(&req, &rem) == -1) {
186 if (likely(errno == EINTR)) {
187 info("nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
188 req.tv_sec = rem.tv_sec;
189 req.tv_nsec = rem.tv_nsec;
191 error("Cannot nanosleep() for %llu microseconds.", usec);
198 int ret = usleep(usec);
199 if(unlikely(ret == -1 && errno == EINVAL)) {
200 // on certain systems, usec has to be up to 999999
202 int counter = usec / 999999;
206 usleep(usec % 999999);
209 error("Cannot usleep() for %llu microseconds.", usec);
215 error("usleep() failed for %llu microseconds.", usec);
221 unsigned char netdata_map_chart_names[256] = {
314 [92] = '/', // backslash
480 // make sure the supplied string
481 // is good for a netdata chart/dimension ID/NAME
482 void netdata_fix_chart_name(char *s) {
483 while ((*s = netdata_map_chart_names[(unsigned char) *s])) s++;
486 unsigned char netdata_map_chart_ids[256] = {
579 [92] = '/', // backslash
745 // make sure the supplied string
746 // is good for a netdata chart/dimension ID/NAME
747 void netdata_fix_chart_id(char *s) {
748 while ((*s = netdata_map_chart_ids[(unsigned char) *s])) s++;
752 // http://stackoverflow.com/questions/7666509/hash-function-for-string
753 uint32_t simple_hash(const char *name)
755 const char *s = name;
756 uint32_t hash = 5381;
759 while((i = *s++)) hash = ((hash << 5) + hash) + i;
761 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
768 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
769 uint32_t simple_hash(const char *name) {
770 unsigned char *s = (unsigned char *) name;
771 uint32_t hval = 0x811c9dc5;
775 // multiply by the 32 bit FNV magic prime mod 2^32
776 // NOTE: No need to optimize with left shifts.
777 // GCC will use imul instruction anyway.
778 // Tested with 'gcc -O3 -S'
779 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
782 // xor the bottom with the current octet
783 hval ^= (uint32_t) *s++;
786 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
790 uint32_t simple_uhash(const char *name) {
791 unsigned char *s = (unsigned char *) name;
792 uint32_t hval = 0x811c9dc5, c;
796 if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
804 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
805 // one at a time hash
806 uint32_t simple_hash(const char *name) {
807 unsigned char *s = (unsigned char *)name;
820 // fprintf(stderr, "HASH: %u = %s\n", h, name);
826 void strreverse(char *begin, char *end) {
829 while (end > begin) {
837 char *mystrsep(char **ptr, char *s) {
839 while (p && !p[0] && *ptr) p = strsep(ptr, s);
843 char *trim(char *s) {
844 // skip leading spaces
845 // and 'comments' as well!?
846 while (*s && isspace(*s)) s++;
847 if (!*s || *s == '#') return NULL;
849 // skip tailing spaces
850 // this way is way faster. Writes only one NUL char.
851 ssize_t l = strlen(s);
854 while (p > s && isspace(*p)) p--;
858 if (!*s) return NULL;
863 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
864 static int log_madvise_1 = 1;
865 #ifdef MADV_MERGEABLE
866 static int log_madvise_2 = 1, log_madvise_3 = 1;
872 fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
874 if (lseek(fd, size, SEEK_SET) == (off_t) size) {
875 if (write(fd, "", 1) == 1) {
876 if (ftruncate(fd, size))
877 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
879 #ifdef MADV_MERGEABLE
880 if (flags & MAP_SHARED || !enable_ksm || !ksm) {
882 mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
883 if (mem == MAP_FAILED) {
884 error("Cannot allocate SHARED memory for file '%s'.", filename);
888 #ifdef NETDATA_LOG_ALLOCATIONS
889 mmap_accounting(size);
891 int advise = MADV_SEQUENTIAL | MADV_DONTFORK;
892 if (flags & MAP_SHARED) advise |= MADV_WILLNEED;
894 if (madvise(mem, size, advise) != 0 && log_madvise_1) {
895 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
899 #ifdef MADV_MERGEABLE
903 // test - load the file into memory
904 mem = calloc(1, size);
906 if(lseek(fd, 0, SEEK_SET) == 0) {
907 if(read(fd, mem, size) != (ssize_t)size)
908 error("Cannot read from file '%s'", filename);
911 error("Cannot seek to beginning of file '%s'.", filename);
914 mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
915 if (mem == MAP_FAILED) {
916 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
920 #ifdef NETDATA_LOG_ALLOCATIONS
921 mmap_accounting(size);
923 if (lseek(fd, 0, SEEK_SET) == 0) {
924 if (read(fd, mem, size) != (ssize_t) size)
925 error("Cannot read from file '%s'", filename);
927 error("Cannot seek to beginning of file '%s'.", filename);
929 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
930 if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
931 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.",
936 if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
937 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.",
946 error("Cannot write to file '%s' at position %zu.", filename, size);
949 error("Cannot seek file '%s' to size %zu.", filename, size);
954 error("Cannot create/open file '%s'.", filename);
959 int savememory(const char *filename, void *mem, size_t size) {
960 char tmpfilename[FILENAME_MAX + 1];
962 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
964 int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
966 error("Cannot create/open file '%s'.", filename);
970 if (write(fd, mem, size) != (ssize_t) size) {
971 error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
978 if (rename(tmpfilename, filename)) {
979 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
986 int fd_is_valid(int fd) {
987 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
991 return (pid_t)syscall(SYS_gettid);
994 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
995 char *s = fgets(buf, (int)buf_size, fp);
1000 // find the string end
1001 while (*++t != '\0');
1003 // trim trailing spaces/newlines/tabs
1004 while (--t > s && *t == '\n')
1014 char *strncpyz(char *dst, const char *src, size_t n) {
1025 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
1026 int size = vsnprintf(dst, n, fmt, args);
1028 if (unlikely((size_t) size > n)) {
1037 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
1040 va_start(args, fmt);
1041 int ret = vsnprintfz(dst, n, fmt, args);
1047 // ----------------------------------------------------------------------------
1049 // to retrieve settings of the system
1052 long get_system_cpus(void) {
1053 procfile *ff = NULL;
1057 char filename[FILENAME_MAX + 1];
1058 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
1060 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1062 error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1066 ff = procfile_readall(ff);
1068 error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1074 for(i = 0; i < procfile_lines(ff); i++) {
1075 if(!procfile_linewords(ff, i)) continue;
1077 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
1080 if(processors < 1) processors = 1;
1084 info("System has %d processors.", processors);
1088 pid_t pid_max = 32768;
1089 pid_t get_system_pid_max(void) {
1090 procfile *ff = NULL;
1092 char filename[FILENAME_MAX + 1];
1093 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
1094 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1096 error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
1100 ff = procfile_readall(ff);
1102 error("Cannot read file '%s'. Assuming system supports %d pids.", filename, pid_max);
1106 pid_max = (pid_t)atoi(procfile_lineword(ff, 0, 0));
1110 error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
1115 info("System supports %d pids.", pid_max);
1120 void get_system_HZ(void) {
1123 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
1127 hz = (unsigned int) ticks;