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 #ifdef HAVE_SYS_PRCTL_H
71 #include <sys/prctl.h>
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 #define NETDATA_OS_TYPE "freebsd"
193 #elif defined(__APPLE__)
194 #include "plugin_macos.h"
195 #define NETDATA_OS_TYPE "macos"
197 #include "plugin_proc.h"
198 #include "plugin_proc_diskspace.h"
199 #define NETDATA_OS_TYPE "linux"
200 #endif /* __FreeBSD__, __APPLE__*/
202 #include "plugin_tc.h"
203 #include "plugins_d.h"
208 #include "rrd2json.h"
209 #include "web_client.h"
210 #include "web_server.h"
211 #include "registry.h"
214 #include "unit_test.h"
216 #include "backends.h"
218 #include "adaptive_resortable_list.h"
220 extern char *netdata_configured_config_dir;
221 extern char *netdata_configured_log_dir;
222 extern char *netdata_configured_plugins_dir;
223 extern char *netdata_configured_web_dir;
224 extern char *netdata_configured_cache_dir;
225 extern char *netdata_configured_varlib_dir;
226 extern char *netdata_configured_home_dir;
227 extern char *netdata_configured_host_prefix;
229 extern void netdata_fix_chart_id(char *s);
230 extern void netdata_fix_chart_name(char *s);
232 extern void strreverse(char* begin, char* end);
233 extern char *mystrsep(char **ptr, char *s);
234 extern char *trim(char *s);
236 extern int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
237 extern int snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
239 // memory allocation functions that handle failures
240 #ifdef NETDATA_LOG_ALLOCATIONS
241 #define strdupz(s) strdupz_int(__FILE__, __FUNCTION__, __LINE__, s)
242 #define callocz(nmemb, size) callocz_int(__FILE__, __FUNCTION__, __LINE__, nmemb, size)
243 #define mallocz(size) mallocz_int(__FILE__, __FUNCTION__, __LINE__, size)
244 #define reallocz(ptr, size) reallocz_int(__FILE__, __FUNCTION__, __LINE__, ptr, size)
245 #define freez(ptr) freez_int(__FILE__, __FUNCTION__, __LINE__, ptr)
247 extern char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s);
248 extern void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size);
249 extern void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size);
250 extern void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size);
251 extern void freez_int(const char *file, const char *function, const unsigned long line, void *ptr);
253 extern char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
254 extern void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
255 extern void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
256 extern void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
257 extern void freez(void *ptr);
260 extern void json_escape_string(char *dst, const char *src, size_t size);
262 extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
263 extern int savememory(const char *filename, void *mem, size_t size);
265 extern int fd_is_valid(int fd);
267 extern int enable_ksm;
269 extern pid_t gettid(void);
271 extern int sleep_usec(usec_t usec);
273 extern char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
275 extern int processors;
276 extern long get_system_cpus(void);
278 extern pid_t pid_max;
279 extern pid_t get_system_pid_max(void);
281 /* Number of ticks per second */
282 extern unsigned int hz;
283 extern void get_system_HZ(void);
285 extern volatile sig_atomic_t netdata_exit;
286 extern const char *os_type;
288 extern const char *program_version;
290 /* fix for alpine linux */
291 #ifndef RUSAGE_THREAD
292 #ifdef RUSAGE_CHILDREN
293 #define RUSAGE_THREAD RUSAGE_CHILDREN
297 #endif /* NETDATA_COMMON_H */