1 #ifndef NETDATA_COMMON_H
2 #define NETDATA_COMMON_H 1
8 // ----------------------------------------------------------------------------
9 // system include files for all netdata C programs
11 /* select the memory allocator, based on autoconf findings */
12 #if defined(ENABLE_JEMALLOC)
14 #if defined(HAVE_JEMALLOC_JEMALLOC_H)
15 #include <jemalloc/jemalloc.h>
20 #elif defined(ENABLE_TCMALLOC)
22 #include <google/tcmalloc.h>
24 #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
26 #if !(defined(__FreeBSD__) || defined(__APPLE__))
28 #endif /* __FreeBSD__ || __APPLE__ */
41 #include <arpa/inet.h>
42 #include <netinet/tcp.h>
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
70 #if !(defined(__FreeBSD__) || defined(__APPLE__))
71 #include <sys/prctl.h>
72 #endif /* __FreeBSD__ || __APPLE__*/
74 #include <sys/resource.h>
75 #include <sys/socket.h>
77 #include <sys/statvfs.h>
78 #include <sys/syscall.h>
80 #include <sys/types.h>
84 #include <uuid/uuid.h>
88 #include <sys/mkdev.h>
90 #ifdef MAJOR_IN_SYSMACROS
91 #include <sys/sysmacros.h>
98 #ifdef STORAGE_WITH_MATH
102 #if defined(HAVE_INTTYPES_H)
103 #include <inttypes.h>
104 #elif defined(HAVE_STDINT_H)
108 #ifdef NETDATA_WITH_ZLIB
112 #ifdef HAVE_CAPABILITY
113 #include <sys/capability.h>
116 // ----------------------------------------------------------------------------
117 // netdata common definitions
119 #if (SIZEOF_VOID_P == 8)
120 #define ENVIRONMENT64
121 #elif (SIZEOF_VOID_P == 4)
122 #define ENVIRONMENT32
124 #error "Cannot detect if this is a 32 or 64 bit CPU"
128 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
131 #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
132 #define NEVERNULL __attribute__((returns_nonnull))
137 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
138 #define MALLOCLIKE __attribute__((malloc))
143 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
144 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
146 #define PRINTFLIKE(f, a)
149 #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
150 #define NORETURN __attribute__ ((noreturn))
155 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
156 #define WARNUNUSED __attribute__ ((warn_unused_result))
164 #define abs(x) ((x < 0)? -x : x)
168 // ----------------------------------------------------------------------------
169 // netdata include files
171 #include "simple_pattern.h"
175 #include "global_statistics.h"
176 #include "storage_number.h"
177 #include "web_buffer.h"
178 #include "web_buffer_svg.h"
182 #include "procfile.h"
183 #include "appconfig.h"
184 #include "dictionary.h"
185 #include "proc_self_mountinfo.h"
186 #include "plugin_checks.h"
187 #include "plugin_idlejitter.h"
188 #include "plugin_nfacct.h"
190 #if defined(__FreeBSD__)
191 #include "plugin_freebsd.h"
192 #elif defined(__APPLE__)
193 #include "plugin_macos.h"
195 #include "plugin_proc.h"
196 #include "plugin_proc_diskspace.h"
197 #endif /* __FreeBSD__, __APPLE__*/
199 #include "plugin_tc.h"
200 #include "plugins_d.h"
205 #include "rrd2json.h"
206 #include "web_client.h"
207 #include "web_server.h"
208 #include "registry.h"
211 #include "unit_test.h"
213 #include "backends.h"
215 #include "adaptive_resortable_list.h"
217 extern void netdata_fix_chart_id(char *s);
218 extern void netdata_fix_chart_name(char *s);
220 extern void strreverse(char* begin, char* end);
221 extern char *mystrsep(char **ptr, char *s);
222 extern char *trim(char *s);
224 extern int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
225 extern int snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
227 // memory allocation functions that handle failures
228 #ifdef NETDATA_LOG_ALLOCATIONS
229 #define strdupz(s) strdupz_int(__FILE__, __FUNCTION__, __LINE__, s)
230 #define callocz(nmemb, size) callocz_int(__FILE__, __FUNCTION__, __LINE__, nmemb, size)
231 #define mallocz(size) mallocz_int(__FILE__, __FUNCTION__, __LINE__, size)
232 #define reallocz(ptr, size) reallocz_int(__FILE__, __FUNCTION__, __LINE__, ptr, size)
233 #define freez(ptr) freez_int(__FILE__, __FUNCTION__, __LINE__, ptr)
235 extern char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s);
236 extern void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size);
237 extern void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size);
238 extern void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size);
239 extern void freez_int(const char *file, const char *function, const unsigned long line, void *ptr);
241 extern char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
242 extern void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
243 extern void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
244 extern void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
245 extern void freez(void *ptr);
248 extern void json_escape_string(char *dst, const char *src, size_t size);
250 extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
251 extern int savememory(const char *filename, void *mem, size_t size);
253 extern int fd_is_valid(int fd);
255 extern char *global_host_prefix;
256 extern int enable_ksm;
258 extern pid_t gettid(void);
260 extern int sleep_usec(usec_t usec);
262 extern char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
264 extern int processors;
265 extern long get_system_cpus(void);
267 extern pid_t pid_max;
268 extern pid_t get_system_pid_max(void);
270 /* Number of ticks per second */
271 extern unsigned int hz;
272 extern void get_system_HZ(void);
275 /* fix for alpine linux */
276 #ifndef RUSAGE_THREAD
277 #ifdef RUSAGE_CHILDREN
278 #define RUSAGE_THREAD RUSAGE_CHILDREN
282 #endif /* NETDATA_COMMON_H */