]> arthur.barton.de Git - netdata.git/blob - src/common.h
diskspace statistics are now a separate thread, to isolate the delays introduced...
[netdata.git] / src / common.h
1 #ifndef NETDATA_COMMON_H
2 #define NETDATA_COMMON_H 1
3
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 // ----------------------------------------------------------------------------
9 // system include files for all netdata C programs
10
11 /* select the memory allocator, based on autoconf findings */
12 #if defined(ENABLE_JEMALLOC)
13
14 #if defined(HAVE_JEMALLOC_JEMALLOC_H)
15 #include <jemalloc/jemalloc.h>
16 #else
17 #include <malloc.h>
18 #endif
19
20 #elif defined(ENABLE_TCMALLOC)
21
22 #include <google/tcmalloc.h>
23
24 #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
25
26 #if !(defined(__FreeBSD__) || defined(__APPLE__))
27 #include <malloc.h>
28 #endif /* __FreeBSD__ || __APPLE__ */
29
30 #endif
31
32 #include <pthread.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <stddef.h>
38 #include <ctype.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <arpa/inet.h>
42 #include <netinet/tcp.h>
43
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
46 #endif
47
48 #ifdef HAVE_RESOLV_H
49 #include <resolv.h>
50 #endif
51
52 #include <dirent.h>
53 #include <fcntl.h>
54 #include <getopt.h>
55 #include <grp.h>
56 #include <pwd.h>
57 #include <locale.h>
58
59 #ifdef HAVE_NETDB_H
60 #include <netdb.h>
61 #endif
62
63 #include <net/if.h>
64
65 #include <poll.h>
66 #include <signal.h>
67 #include <syslog.h>
68 #include <sys/mman.h>
69
70 #if !(defined(__FreeBSD__) || defined(__APPLE__))
71 #include <sys/prctl.h>
72 #endif /* __FreeBSD__ || __APPLE__*/
73
74 #include <sys/resource.h>
75 #include <sys/socket.h>
76 #include <sys/stat.h>
77 #include <sys/statvfs.h>
78 #include <sys/syscall.h>
79 #include <sys/time.h>
80 #include <sys/types.h>
81 #include <sys/wait.h>
82 #include <time.h>
83 #include <unistd.h>
84 #include <uuid/uuid.h>
85
86 // #1408
87 #ifdef MAJOR_IN_MKDEV
88 #include <sys/mkdev.h>
89 #endif
90 #ifdef MAJOR_IN_SYSMACROS
91 #include <sys/sysmacros.h>
92 #endif
93
94 /*
95 #include <mntent.h>
96 */
97
98 #ifdef STORAGE_WITH_MATH
99 #include <math.h>
100 #endif
101
102 #if defined(HAVE_INTTYPES_H)
103 #include <inttypes.h>
104 #elif defined(HAVE_STDINT_H)
105 #include <stdint.h>
106 #endif
107
108 #ifdef NETDATA_WITH_ZLIB
109 #include <zlib.h>
110 #endif
111
112 // ----------------------------------------------------------------------------
113 // netdata common definitions
114
115 #if (SIZEOF_VOID_P == 8)
116 #define ENVIRONMENT64
117 #elif (SIZEOF_VOID_P == 4)
118 #define ENVIRONMENT32
119 #else
120 #error "Cannot detect if this is a 32 or 64 bit CPU"
121 #endif
122
123 #ifdef __GNUC__
124 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
125 #endif // __GNUC__
126
127 #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
128 #define NEVERNULL __attribute__((returns_nonnull))
129 #else
130 #define NEVERNULL
131 #endif
132
133 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
134 #define MALLOCLIKE __attribute__((malloc))
135 #else
136 #define MALLOCLIKE
137 #endif
138
139 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
140 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
141 #else
142 #define PRINTFLIKE(f, a)
143 #endif
144
145 #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
146 #define NORETURN __attribute__ ((noreturn))
147 #else
148 #define NORETURN
149 #endif
150
151 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
152 #define WARNUNUSED __attribute__ ((warn_unused_result))
153 #else
154 #define WARNUNUSED
155 #endif
156
157 #ifdef abs
158 #undef abs
159 #endif
160 #define abs(x) ((x < 0)? -x : x)
161
162 #define GUID_LEN 36
163
164 // ----------------------------------------------------------------------------
165 // netdata include files
166
167 #include "avl.h"
168 #include "clocks.h"
169 #include "log.h"
170 #include "global_statistics.h"
171 #include "storage_number.h"
172 #include "web_buffer.h"
173 #include "web_buffer_svg.h"
174 #include "url.h"
175 #include "popen.h"
176
177 #include "procfile.h"
178 #include "appconfig.h"
179 #include "dictionary.h"
180 #include "proc_self_mountinfo.h"
181 #include "plugin_checks.h"
182 #include "plugin_idlejitter.h"
183 #include "plugin_nfacct.h"
184
185 #if defined(__FreeBSD__)
186 #include "plugin_freebsd.h"
187 #elif defined(__APPLE__)
188 #include "plugin_macos.h"
189 #else
190 #include "plugin_proc.h"
191 #include "plugin_proc_diskspace.h"
192 #endif /* __FreeBSD__, __APPLE__*/
193
194 #include "plugin_tc.h"
195 #include "plugins_d.h"
196 #include "socket.h"
197 #include "eval.h"
198 #include "health.h"
199 #include "rrd.h"
200 #include "rrd2json.h"
201 #include "web_client.h"
202 #include "web_server.h"
203 #include "registry.h"
204 #include "daemon.h"
205 #include "main.h"
206 #include "unit_test.h"
207 #include "ipc.h"
208 #include "backends.h"
209
210 extern void netdata_fix_chart_id(char *s);
211 extern void netdata_fix_chart_name(char *s);
212
213 extern uint32_t simple_hash(const char *name);
214 extern uint32_t simple_uhash(const char *name);
215
216 extern void strreverse(char* begin, char* end);
217 extern char *mystrsep(char **ptr, char *s);
218 extern char *trim(char *s);
219
220 extern char *strncpyz(char *dst, const char *src, size_t n);
221 extern int  vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
222 extern int  snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
223
224 // memory allocation functions that handle failures
225 #ifdef NETDATA_LOG_ALLOCATIONS
226 #define strdupz(s) strdupz_int(__FILE__, __FUNCTION__, __LINE__, s)
227 #define callocz(nmemb, size) callocz_int(__FILE__, __FUNCTION__, __LINE__, nmemb, size)
228 #define mallocz(size) mallocz_int(__FILE__, __FUNCTION__, __LINE__, size)
229 #define reallocz(ptr, size) reallocz_int(__FILE__, __FUNCTION__, __LINE__, ptr, size)
230 #define freez(ptr) freez_int(__FILE__, __FUNCTION__, __LINE__, ptr)
231
232 extern char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s);
233 extern void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size);
234 extern void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size);
235 extern void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size);
236 extern void freez_int(const char *file, const char *function, const unsigned long line, void *ptr);
237 #else
238 extern char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
239 extern void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
240 extern void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
241 extern void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
242 extern void freez(void *ptr);
243 #endif
244
245 extern void json_escape_string(char *dst, const char *src, size_t size);
246
247 extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
248 extern int savememory(const char *filename, void *mem, size_t size);
249
250 extern int fd_is_valid(int fd);
251
252 extern char *global_host_prefix;
253 extern int enable_ksm;
254
255 extern pid_t gettid(void);
256
257 extern int sleep_usec(usec_t usec);
258
259 extern char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
260
261 extern int processors;
262 extern long get_system_cpus(void);
263
264 extern pid_t pid_max;
265 extern pid_t get_system_pid_max(void);
266
267 /* Number of ticks per second */
268 extern unsigned int hz;
269 extern void get_system_HZ(void);
270
271
272 /* fix for alpine linux */
273 #ifndef RUSAGE_THREAD
274 #ifdef RUSAGE_CHILDREN
275 #define RUSAGE_THREAD RUSAGE_CHILDREN
276 #endif
277 #endif
278
279 extern int read_single_number_file(const char *filename, unsigned long long *result);
280
281 typedef enum {
282     NETDATA_SIMPLE_PATTERN_MODE_EXACT,
283     NETDATA_SIMPLE_PATTERN_MODE_PREFIX,
284     NETDATA_SIMPLE_PATTERN_MODE_SUFFIX,
285     NETDATA_SIMPLE_PATTERN_MODE_SUBSTRING
286 } NETDATA_SIMPLE_PREFIX_MODE;
287 typedef void NETDATA_SIMPLE_PATTERN;
288 extern NETDATA_SIMPLE_PATTERN *netdata_simple_pattern_list_create(const char *list, NETDATA_SIMPLE_PREFIX_MODE default_mode);
289 extern int netdata_simple_pattern_list_matches(NETDATA_SIMPLE_PATTERN *list, const char *str);
290
291 #endif /* NETDATA_COMMON_H */