]> arthur.barton.de Git - netdata.git/blob - src/common.h
407dd4859dda2815d3fb3d81c3d87f9a9a257624
[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 #ifdef has_jemalloc
9 #include <jemalloc/jemalloc.h>
10 #undef HAVE_C_MALLOPT
11 #undef HAVE_C_MALLINFO
12 #elif has_tcmalloc
13 #include <google/tcmalloc.h>
14 #undef HAVE_C_MALLOPT
15 #undef HAVE_C_MALLINFO
16 #else
17 #include <malloc.h>
18 #endif
19
20 #include <pthread.h>
21 #include <errno.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stddef.h>
27
28 #include <ctype.h>
29 #include <string.h>
30 #include <strings.h>
31
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35
36 #include <dirent.h>
37 #include <fcntl.h>
38 #include <getopt.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include <locale.h>
42
43 #include <netdb.h>
44 #include <poll.h>
45 #include <signal.h>
46 #include <syslog.h>
47 #include <sys/mman.h>
48 #include <sys/prctl.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <sys/stat.h>
52 #include <sys/statvfs.h>
53 #include <sys/syscall.h>
54 #include <sys/time.h>
55 #include <sys/types.h>
56 #include <sys/wait.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include <uuid/uuid.h>
60
61 #ifdef STORAGE_WITH_MATH
62 #include <math.h>
63 #endif
64
65 #if defined(HAVE_INTTYPES_H)
66 #include <inttypes.h>
67 #elif defined(HAVE_STDINT_H)
68 #include <stdint.h>
69 #endif
70
71 #ifdef NETDATA_WITH_ZLIB
72 #include <zlib.h>
73 #endif
74
75 #if (SIZEOF_VOID_P == 8)
76 #define ENVIRONMENT64
77 #elif (SIZEOF_VOID_P == 4)
78 #define ENVIRONMENT32
79 #else
80 #error "Cannot detect if this is a 32 or 64 bit CPU"
81 #endif
82
83 #ifdef __GNUC__
84 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
85 #endif // __GNUC__
86
87 #include "avl.h"
88 #include "log.h"
89 #include "global_statistics.h"
90 #include "storage_number.h"
91 #include "web_buffer.h"
92 #include "web_buffer_svg.h"
93 #include "url.h"
94 #include "popen.h"
95
96 #include "procfile.h"
97 #include "appconfig.h"
98 #include "dictionary.h"
99 #include "proc_self_mountinfo.h"
100 #include "plugin_checks.h"
101 #include "plugin_idlejitter.h"
102 #include "plugin_nfacct.h"
103 #include "plugin_proc.h"
104 #include "plugin_tc.h"
105 #include "plugins_d.h"
106
107 #include "eval.h"
108 #include "health.h"
109
110 #include "rrd.h"
111 #include "rrd2json.h"
112
113 #include "web_client.h"
114 #include "web_server.h"
115
116 #include "registry.h"
117 #include "daemon.h"
118 #include "main.h"
119 #include "unit_test.h"
120
121 #ifdef abs
122 #undef abs
123 #endif
124 #define abs(x) ((x < 0)? -x : x)
125
126 extern unsigned long long usec_dt(struct timeval *now, struct timeval *old);
127 extern unsigned long long timeval_usec(struct timeval *tv);
128
129 // #define usec_dt(now, last) (((((now)->tv_sec * 1000000ULL) + (now)->tv_usec) - (((last)->tv_sec * 1000000ULL) + (last)->tv_usec)))
130
131 extern void netdata_fix_chart_id(char *s);
132 extern void netdata_fix_chart_name(char *s);
133
134 extern uint32_t simple_hash(const char *name);
135 extern uint32_t simple_uhash(const char *name);
136
137 extern void strreverse(char* begin, char* end);
138 extern char *mystrsep(char **ptr, char *s);
139 extern char *trim(char *s);
140
141 extern char *strncpyz(char *dst, const char *src, size_t n);
142 extern int  vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
143 extern int  snprintfz(char *dst, size_t n, const char *fmt, ...) __attribute__ (( format (printf, 3, 4)));
144
145 // memory allocation functions that handle failures
146 #ifdef NETDATA_LOG_ALLOCATIONS
147 #define strdupz(s) strdupz_int(__FILE__, __FUNCTION__, __LINE__, s)
148 #define callocz(nmemb, size) callocz_int(__FILE__, __FUNCTION__, __LINE__, nmemb, size)
149 #define mallocz(size) mallocz_int(__FILE__, __FUNCTION__, __LINE__, size)
150 #define reallocz(ptr, size) reallocz_int(__FILE__, __FUNCTION__, __LINE__, ptr, size)
151 #define freez(ptr) freez_int(__FILE__, __FUNCTION__, __LINE__, ptr)
152
153 extern char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s);
154 extern void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size);
155 extern void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size);
156 extern void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size);
157 extern void freez_int(const char *file, const char *function, const unsigned long line, void *ptr);
158 #else
159 extern char *strdupz(const char *s);
160 extern void *callocz(size_t nmemb, size_t size);
161 extern void *mallocz(size_t size);
162 extern void *reallocz(void *ptr, size_t size);
163 extern void freez(void *ptr);
164 #endif
165
166 extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
167 extern int savememory(const char *filename, void *mem, size_t size);
168
169 extern int fd_is_valid(int fd);
170
171 extern char *global_host_prefix;
172 extern int enable_ksm;
173
174 extern pid_t gettid(void);
175
176 extern unsigned long long time_usec(void);
177 extern int sleep_usec(unsigned long long usec);
178
179 extern char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
180
181 extern int processors;
182 extern long get_system_cpus(void);
183
184 extern pid_t pid_max;
185 extern pid_t get_system_pid_max(void);
186
187 /* Number of ticks per second */
188 extern unsigned int hz;
189 extern void get_system_HZ(void);
190
191
192 /* fix for alpine linux */
193 #ifndef RUSAGE_THREAD
194 #ifdef RUSAGE_CHILDREN
195 #define RUSAGE_THREAD RUSAGE_CHILDREN
196 #endif
197 #endif
198
199 extern int read_single_number_file(const char *filename, unsigned long long *result);
200
201 #endif /* NETDATA_COMMON_H */