6 #if defined(__FreeBSD__) || defined(__APPLE__)
8 # define MADV_DONTFORK INHERIT_NONE
9 #endif /* __FreeBSD__ || __APPLE__*/
11 char *global_host_prefix = "";
14 volatile sig_atomic_t netdata_exit = 0;
16 // ----------------------------------------------------------------------------
17 // memory allocation functions that handle failures
19 // although netdata does not use memory allocations too often (netdata tries to
20 // maintain its memory footprint stable during runtime, i.e. all buffers are
21 // allocated during initialization and are adapted to current use throughout
22 // its lifetime), these can be used to override the default system allocation
25 #ifdef NETDATA_LOG_ALLOCATIONS
26 static struct memory_statistics {
27 volatile size_t malloc_calls_made;
28 volatile size_t calloc_calls_made;
29 volatile size_t realloc_calls_made;
30 volatile size_t strdup_calls_made;
31 volatile size_t free_calls_made;
32 volatile size_t memory_calls_made;
33 volatile size_t allocated_memory;
34 volatile size_t mmapped_memory;
37 static inline void print_allocations(const char *file, const char *function, const unsigned long line) {
38 static struct memory_statistics old = { 0, 0, 0, 0, 0, 0, 0, 0 };
40 //if(unlikely(!(memory_statistics.memory_calls_made % 5))) {
41 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",
43 (memory_statistics.allocated_memory + 512) / 1024, memory_statistics.allocated_memory - old.allocated_memory,
44 (memory_statistics.mmapped_memory + 512) / 1024, memory_statistics.mmapped_memory - old.mmapped_memory,
45 memory_statistics.malloc_calls_made, memory_statistics.malloc_calls_made - old.malloc_calls_made,
46 memory_statistics.calloc_calls_made, memory_statistics.calloc_calls_made - old.calloc_calls_made,
47 memory_statistics.realloc_calls_made, memory_statistics.realloc_calls_made - old.realloc_calls_made,
48 memory_statistics.strdup_calls_made, memory_statistics.strdup_calls_made - old.strdup_calls_made,
49 memory_statistics.free_calls_made, memory_statistics.free_calls_made - old.free_calls_made
52 memcpy(&old, &memory_statistics, sizeof(struct memory_statistics));
56 static inline void malloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
57 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
58 __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
59 __atomic_fetch_add(&memory_statistics.malloc_calls_made, 1, __ATOMIC_SEQ_CST);
60 __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
62 // this is for debugging - we don't care locking it
63 memory_statistics.memory_calls_made++;
64 memory_statistics.malloc_calls_made++;
65 memory_statistics.allocated_memory += size;
67 print_allocations(file, function, line);
70 static inline void mmap_accounting(size_t size) {
71 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
72 __atomic_fetch_add(&memory_statistics.malloc_calls_made, 1, __ATOMIC_SEQ_CST);
73 __atomic_fetch_add(&memory_statistics.mmapped_memory, size, __ATOMIC_SEQ_CST);
75 // this is for debugging - we don't care locking it
76 memory_statistics.memory_calls_made++;
77 memory_statistics.mmapped_memory += size;
81 static inline void calloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
82 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
83 __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
84 __atomic_fetch_add(&memory_statistics.calloc_calls_made, 1, __ATOMIC_SEQ_CST);
85 __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
87 // this is for debugging - we don't care locking it
88 memory_statistics.memory_calls_made++;
89 memory_statistics.calloc_calls_made++;
90 memory_statistics.allocated_memory += size;
92 print_allocations(file, function, line);
95 static inline void realloc_accounting(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
98 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
99 __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
100 __atomic_fetch_add(&memory_statistics.realloc_calls_made, 1, __ATOMIC_SEQ_CST);
101 __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
103 // this is for debugging - we don't care locking it
104 memory_statistics.memory_calls_made++;
105 memory_statistics.realloc_calls_made++;
106 memory_statistics.allocated_memory += size;
108 print_allocations(file, function, line);
111 static inline void strdup_accounting(const char *file, const char *function, const unsigned long line, const char *s) {
112 size_t size = strlen(s) + 1;
114 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
115 __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
116 __atomic_fetch_add(&memory_statistics.strdup_calls_made, 1, __ATOMIC_SEQ_CST);
117 __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
119 // this is for debugging - we don't care locking it
120 memory_statistics.memory_calls_made++;
121 memory_statistics.strdup_calls_made++;
122 memory_statistics.allocated_memory += size;
124 print_allocations(file, function, line);
127 static inline void free_accounting(const char *file, const char *function, const unsigned long line, void *ptr) {
133 #if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
134 __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
135 __atomic_fetch_add(&memory_statistics.free_calls_made, 1, __ATOMIC_SEQ_CST);
137 // this is for debugging - we don't care locking it
138 memory_statistics.memory_calls_made++;
139 memory_statistics.free_calls_made++;
145 #ifdef NETDATA_LOG_ALLOCATIONS
146 char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s) {
147 strdup_accounting(file, function, line, s);
149 char *strdupz(const char *s) {
153 if (unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
157 #ifdef NETDATA_LOG_ALLOCATIONS
158 void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size) {
159 malloc_accounting(file, function, line, size);
161 void *mallocz(size_t size) {
164 void *p = malloc(size);
165 if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
169 #ifdef NETDATA_LOG_ALLOCATIONS
170 void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size) {
171 calloc_accounting(file, function, line, nmemb * size);
173 void *callocz(size_t nmemb, size_t size) {
176 void *p = calloc(nmemb, size);
177 if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
181 #ifdef NETDATA_LOG_ALLOCATIONS
182 void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
183 realloc_accounting(file, function, line, ptr, size);
185 void *reallocz(void *ptr, size_t size) {
188 void *p = realloc(ptr, size);
189 if (unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
193 #ifdef NETDATA_LOG_ALLOCATIONS
194 void freez_int(const char *file, const char *function, const unsigned long line, void *ptr) {
195 free_accounting(file, function, line, ptr);
197 void freez(void *ptr) {
203 void json_escape_string(char *dst, const char *src, size_t size) {
205 char *d = dst, *e = &dst[size - 1];
207 for(t = src; *t && d < e ;t++) {
208 if(unlikely(*t == '\\' || *t == '"')) {
209 if(unlikely(d + 1 >= e)) break;
218 int sleep_usec(usec_t usec) {
220 #ifndef NETDATA_WITH_USLEEP
221 // we expect microseconds (1.000.000 per second)
222 // but timespec is nanoseconds (1.000.000.000 per second)
223 struct timespec rem, req = {
224 .tv_sec = (time_t) (usec / 1000000),
225 .tv_nsec = (suseconds_t) ((usec % 1000000) * 1000)
228 while (nanosleep(&req, &rem) == -1) {
229 if (likely(errno == EINTR)) {
230 debug(D_SYSTEM, "nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
231 req.tv_sec = rem.tv_sec;
232 req.tv_nsec = rem.tv_nsec;
234 error("Cannot nanosleep() for %llu microseconds.", usec);
241 int ret = usleep(usec);
242 if(unlikely(ret == -1 && errno == EINVAL)) {
243 // on certain systems, usec has to be up to 999999
245 int counter = usec / 999999;
249 usleep(usec % 999999);
252 error("Cannot usleep() for %llu microseconds.", usec);
258 error("usleep() failed for %llu microseconds.", usec);
264 unsigned char netdata_map_chart_names[256] = {
357 [92] = '/', // backslash
523 // make sure the supplied string
524 // is good for a netdata chart/dimension ID/NAME
525 void netdata_fix_chart_name(char *s) {
526 while ((*s = netdata_map_chart_names[(unsigned char) *s])) s++;
529 unsigned char netdata_map_chart_ids[256] = {
622 [92] = '/', // backslash
788 // make sure the supplied string
789 // is good for a netdata chart/dimension ID/NAME
790 void netdata_fix_chart_id(char *s) {
791 while ((*s = netdata_map_chart_ids[(unsigned char) *s])) s++;
795 // http://stackoverflow.com/questions/7666509/hash-function-for-string
796 uint32_t simple_hash(const char *name)
798 const char *s = name;
799 uint32_t hash = 5381;
802 while((i = *s++)) hash = ((hash << 5) + hash) + i;
804 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
811 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
812 uint32_t simple_hash(const char *name) {
813 unsigned char *s = (unsigned char *) name;
814 uint32_t hval = 0x811c9dc5;
818 // multiply by the 32 bit FNV magic prime mod 2^32
819 // NOTE: No need to optimize with left shifts.
820 // GCC will use imul instruction anyway.
821 // Tested with 'gcc -O3 -S'
822 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
825 // xor the bottom with the current octet
826 hval ^= (uint32_t) *s++;
829 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
833 uint32_t simple_uhash(const char *name) {
834 unsigned char *s = (unsigned char *) name;
835 uint32_t hval = 0x811c9dc5, c;
839 if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
847 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
848 // one at a time hash
849 uint32_t simple_hash(const char *name) {
850 unsigned char *s = (unsigned char *)name;
863 // fprintf(stderr, "HASH: %u = %s\n", h, name);
869 void strreverse(char *begin, char *end) {
870 while (end > begin) {
878 char *mystrsep(char **ptr, char *s) {
880 while (p && !p[0] && *ptr) p = strsep(ptr, s);
884 char *trim(char *s) {
885 // skip leading spaces
886 // and 'comments' as well!?
887 while (*s && isspace(*s)) s++;
888 if (!*s || *s == '#') return NULL;
890 // skip tailing spaces
891 // this way is way faster. Writes only one NUL char.
892 ssize_t l = strlen(s);
895 while (p > s && isspace(*p)) p--;
899 if (!*s) return NULL;
904 void *mymmap(const char *filename, size_t size, int flags, int ksm) {
905 static int log_madvise_1 = 1;
906 #ifdef MADV_MERGEABLE
907 static int log_madvise_2 = 1, log_madvise_3 = 1;
912 int fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
914 if (lseek(fd, size, SEEK_SET) == (off_t) size) {
915 if (write(fd, "", 1) == 1) {
916 if (ftruncate(fd, size))
917 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
919 #ifdef MADV_MERGEABLE
920 if (flags & MAP_SHARED || !enable_ksm || !ksm) {
922 mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
923 if (mem == MAP_FAILED) {
924 error("Cannot allocate SHARED memory for file '%s'.", filename);
928 #ifdef NETDATA_LOG_ALLOCATIONS
929 mmap_accounting(size);
931 int advise = MADV_SEQUENTIAL | MADV_DONTFORK;
932 if (flags & MAP_SHARED) advise |= MADV_WILLNEED;
934 if (madvise(mem, size, advise) != 0 && log_madvise_1) {
935 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
939 #ifdef MADV_MERGEABLE
943 // test - load the file into memory
944 mem = calloc(1, size);
946 if(lseek(fd, 0, SEEK_SET) == 0) {
947 if(read(fd, mem, size) != (ssize_t)size)
948 error("Cannot read from file '%s'", filename);
951 error("Cannot seek to beginning of file '%s'.", filename);
954 mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
955 if (mem == MAP_FAILED) {
956 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
960 #ifdef NETDATA_LOG_ALLOCATIONS
961 mmap_accounting(size);
963 if (lseek(fd, 0, SEEK_SET) == 0) {
964 if (read(fd, mem, size) != (ssize_t) size)
965 error("Cannot read from file '%s'", filename);
967 error("Cannot seek to beginning of file '%s'.", filename);
969 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
970 if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
971 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.",
976 if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
977 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.",
986 error("Cannot write to file '%s' at position %zu.", filename, size);
989 error("Cannot seek file '%s' to size %zu.", filename, size);
994 error("Cannot create/open file '%s'.", filename);
999 int savememory(const char *filename, void *mem, size_t size) {
1000 char tmpfilename[FILENAME_MAX + 1];
1002 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
1004 int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
1006 error("Cannot create/open file '%s'.", filename);
1010 if (write(fd, mem, size) != (ssize_t) size) {
1011 error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
1018 if (rename(tmpfilename, filename)) {
1019 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
1026 int fd_is_valid(int fd) {
1027 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
1030 pid_t gettid(void) {
1032 return (pid_t)pthread_getthreadid_np();
1033 #elif defined(__APPLE__)
1034 uint64_t curthreadid;
1035 pthread_threadid_np(NULL, &curthreadid);
1036 return (pid_t)curthreadid;
1038 return (pid_t)syscall(SYS_gettid);
1039 #endif /* __FreeBSD__, __APPLE__*/
1042 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
1043 char *s = fgets(buf, (int)buf_size, fp);
1044 if (!s) return NULL;
1048 // find the string end
1049 while (*++t != '\0');
1051 // trim trailing spaces/newlines/tabs
1052 while (--t > s && *t == '\n')
1062 char *strncpyz(char *dst, const char *src, size_t n) {
1073 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
1074 int size = vsnprintf(dst, n, fmt, args);
1076 if (unlikely((size_t) size > n)) {
1085 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
1088 va_start(args, fmt);
1089 int ret = vsnprintfz(dst, n, fmt, args);
1095 // ----------------------------------------------------------------------------
1097 // to retrieve settings of the system
1100 long get_system_cpus(void) {
1103 char filename[FILENAME_MAX + 1];
1104 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
1106 procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1108 error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1112 ff = procfile_readall(ff);
1114 error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1120 for(i = 0; i < procfile_lines(ff); i++) {
1121 if(!procfile_linewords(ff, i)) continue;
1123 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
1126 if(processors < 1) processors = 1;
1130 debug(D_SYSTEM, "System has %d processors.", processors);
1134 pid_t pid_max = 32768;
1135 pid_t get_system_pid_max(void) {
1136 char filename[FILENAME_MAX + 1];
1137 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
1138 procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1140 error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
1144 ff = procfile_readall(ff);
1146 error("Cannot read file '%s'. Assuming system supports %d pids.", filename, pid_max);
1150 pid_max = (pid_t)atoi(procfile_lineword(ff, 0, 0));
1154 error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
1159 debug(D_SYSTEM, "System supports %d pids.", pid_max);
1164 void get_system_HZ(void) {
1167 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
1171 hz = (unsigned int) ticks;
1174 int read_single_number_file(const char *filename, unsigned long long *result) {
1175 char buffer[1024 + 1];
1177 int fd = open(filename, O_RDONLY, 0666);
1178 if(unlikely(fd == -1)) return 1;
1180 ssize_t r = read(fd, buffer, 1024);
1181 if(unlikely(r == -1)) {
1187 *result = strtoull(buffer, NULL, 0);
1191 // ----------------------------------------------------------------------------
1192 // simple_pattern_match
1194 struct simple_pattern {
1197 NETDATA_SIMPLE_PREFIX_MODE mode;
1198 struct simple_pattern *next;
1201 NETDATA_SIMPLE_PATTERN *netdata_simple_pattern_list_create(const char *list, NETDATA_SIMPLE_PREFIX_MODE default_mode) {
1202 struct simple_pattern *root = NULL;
1204 if(unlikely(!list || !*list)) return root;
1206 char *a = strdupz(list);
1212 while(isspace(*s)) s++;
1215 if(unlikely(!*s)) break;
1217 // find the next space
1219 while(*c && !isspace(*c)) c++;
1221 // find the next word
1223 if(likely(*c)) n = c + 1;
1226 // terminate our string
1230 strncpy(buf, s, 100);
1232 if(likely(n)) *c = ' ';
1235 NETDATA_SIMPLE_PREFIX_MODE mode;
1236 size_t len = strlen(s);
1237 if(len >= 2 && *s == '*' && s[len - 1] == '*') {
1241 mode = NETDATA_SIMPLE_PATTERN_MODE_SUBSTRING;
1243 else if(len >= 1 && *s == '*') {
1246 mode = NETDATA_SIMPLE_PATTERN_MODE_SUFFIX;
1248 else if(len >= 1 && s[len - 1] == '*') {
1251 mode = NETDATA_SIMPLE_PATTERN_MODE_PREFIX;
1254 mode = default_mode;
1258 error("simple pattern '%s' includes '%s' that is invalid", a, s);
1260 // allocate the structure
1261 struct simple_pattern *m = mallocz(sizeof(struct simple_pattern));
1262 m->match = strdup(s);
1263 m->len = strlen(m->match);
1269 error("simple pattern '%s' includes invalid matches", a);
1271 // prepare for next loop
1277 return (NETDATA_SIMPLE_PATTERN *)root;
1280 int netdata_simple_pattern_list_matches(NETDATA_SIMPLE_PATTERN *list, const char *str) {
1281 struct simple_pattern *m, *root = (struct simple_pattern *)list;
1283 if(unlikely(!root)) return 0;
1285 size_t len = strlen(str);
1286 for(m = root; m ; m = m->next) {
1289 case NETDATA_SIMPLE_PATTERN_MODE_SUBSTRING:
1290 if(unlikely(strstr(str, m->match)))
1294 case NETDATA_SIMPLE_PATTERN_MODE_PREFIX:
1295 if(unlikely(strncmp(str, m->match, m->len) == 0))
1299 case NETDATA_SIMPLE_PATTERN_MODE_SUFFIX:
1300 if(unlikely(strcmp(&str[len - m->len], m->match) == 0))
1304 case NETDATA_SIMPLE_PATTERN_MODE_EXACT:
1306 if(unlikely(strcmp(str, m->match) == 0))