libnetdata.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_LIB_H
  3. #define NETDATA_LIB_H 1
  4. # ifdef __cplusplus
  5. extern "C" {
  6. # endif
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #define JUDYHS_INDEX_SIZE_ESTIMATE(key_bytes) (((key_bytes) + sizeof(Word_t) - 1) / sizeof(Word_t) * 4)
  11. #if defined(NETDATA_DEV_MODE) && !defined(NETDATA_INTERNAL_CHECKS)
  12. #define NETDATA_INTERNAL_CHECKS 1
  13. #endif
  14. #if SIZEOF_VOID_P == 4
  15. #define ENV32BIT 1
  16. #else
  17. #define ENV64BIT 1
  18. #endif
  19. // NETDATA_TRACE_ALLOCATIONS does not work under musl libc, so don't enable it
  20. //#if defined(NETDATA_INTERNAL_CHECKS) && !defined(NETDATA_TRACE_ALLOCATIONS)
  21. //#define NETDATA_TRACE_ALLOCATIONS 1
  22. //#endif
  23. #define OS_LINUX 1
  24. #define OS_FREEBSD 2
  25. #define OS_MACOS 3
  26. #define MALLOC_ALIGNMENT (sizeof(uintptr_t) * 2)
  27. #define size_t_atomic_count(op, var, size) __atomic_## op ##_fetch(&(var), size, __ATOMIC_RELAXED)
  28. #define size_t_atomic_bytes(op, var, size) __atomic_## op ##_fetch(&(var), ((size) % MALLOC_ALIGNMENT)?((size) + MALLOC_ALIGNMENT - ((size) % MALLOC_ALIGNMENT)):(size), __ATOMIC_RELAXED)
  29. // ----------------------------------------------------------------------------
  30. // system include files for all netdata C programs
  31. /* select the memory allocator, based on autoconf findings */
  32. #if defined(ENABLE_JEMALLOC)
  33. #if defined(HAVE_JEMALLOC_JEMALLOC_H)
  34. #include <jemalloc/jemalloc.h>
  35. #else // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  36. #include <malloc.h>
  37. #endif // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  38. #elif defined(ENABLE_TCMALLOC)
  39. #include <google/tcmalloc.h>
  40. #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  41. #if !(defined(__FreeBSD__) || defined(__APPLE__))
  42. #include <malloc.h>
  43. #endif /* __FreeBSD__ || __APPLE__ */
  44. #endif /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  45. // ----------------------------------------------------------------------------
  46. #if defined(__FreeBSD__)
  47. #include <pthread_np.h>
  48. #define NETDATA_OS_TYPE "freebsd"
  49. #elif defined(__APPLE__)
  50. #define NETDATA_OS_TYPE "macos"
  51. #else
  52. #define NETDATA_OS_TYPE "linux"
  53. #endif /* __FreeBSD__, __APPLE__*/
  54. #include <pthread.h>
  55. #include <errno.h>
  56. #include <stdbool.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <stdarg.h>
  60. #include <stddef.h>
  61. #include <ctype.h>
  62. #include <string.h>
  63. #include <strings.h>
  64. #include <arpa/inet.h>
  65. #include <netinet/tcp.h>
  66. #include <sys/ioctl.h>
  67. #include <libgen.h>
  68. #include <dirent.h>
  69. #include <fcntl.h>
  70. #include <getopt.h>
  71. #include <grp.h>
  72. #include <pwd.h>
  73. #include <limits.h>
  74. #include <locale.h>
  75. #include <net/if.h>
  76. #include <poll.h>
  77. #include <signal.h>
  78. #include <syslog.h>
  79. #include <sys/mman.h>
  80. #include <sys/resource.h>
  81. #include <sys/socket.h>
  82. #include <sys/syscall.h>
  83. #include <sys/time.h>
  84. #include <sys/types.h>
  85. #include <sys/wait.h>
  86. #include <sys/un.h>
  87. #include <time.h>
  88. #include <unistd.h>
  89. #include <uuid/uuid.h>
  90. #include <spawn.h>
  91. #include <uv.h>
  92. #include <assert.h>
  93. // CentOS 7 has older version that doesn't define this
  94. // same goes for MacOS
  95. #ifndef UUID_STR_LEN
  96. #define UUID_STR_LEN (37)
  97. #endif
  98. #ifdef HAVE_NETINET_IN_H
  99. #include <netinet/in.h>
  100. #endif
  101. #ifdef HAVE_RESOLV_H
  102. #include <resolv.h>
  103. #endif
  104. #ifdef HAVE_NETDB_H
  105. #include <netdb.h>
  106. #endif
  107. #ifdef HAVE_SYS_PRCTL_H
  108. #include <sys/prctl.h>
  109. #endif
  110. #ifdef HAVE_SYS_STAT_H
  111. #include <sys/stat.h>
  112. #endif
  113. #ifdef HAVE_SYS_VFS_H
  114. #include <sys/vfs.h>
  115. #endif
  116. #ifdef HAVE_SYS_STATFS_H
  117. #include <sys/statfs.h>
  118. #endif
  119. #ifdef HAVE_LINUX_MAGIC_H
  120. #include <linux/magic.h>
  121. #endif
  122. #ifdef HAVE_SYS_MOUNT_H
  123. #include <sys/mount.h>
  124. #endif
  125. #ifdef HAVE_SYS_STATVFS_H
  126. #include <sys/statvfs.h>
  127. #endif
  128. // #1408
  129. #ifdef MAJOR_IN_MKDEV
  130. #include <sys/mkdev.h>
  131. #endif
  132. #ifdef MAJOR_IN_SYSMACROS
  133. #include <sys/sysmacros.h>
  134. #endif
  135. #ifdef STORAGE_WITH_MATH
  136. #include <math.h>
  137. #include <float.h>
  138. #endif
  139. #if defined(HAVE_INTTYPES_H)
  140. #include <inttypes.h>
  141. #elif defined(HAVE_STDINT_H)
  142. #include <stdint.h>
  143. #endif
  144. #ifdef NETDATA_WITH_ZLIB
  145. #include <zlib.h>
  146. #endif
  147. #ifdef HAVE_CAPABILITY
  148. #include <sys/capability.h>
  149. #endif
  150. // ----------------------------------------------------------------------------
  151. // netdata common definitions
  152. #ifdef __GNUC__
  153. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  154. #endif // __GNUC__
  155. #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
  156. #define NEVERNULL __attribute__((returns_nonnull))
  157. #else
  158. #define NEVERNULL
  159. #endif
  160. #ifdef HAVE_FUNC_ATTRIBUTE_NOINLINE
  161. #define NOINLINE __attribute__((noinline))
  162. #else
  163. #define NOINLINE
  164. #endif
  165. #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
  166. #define MALLOCLIKE __attribute__((malloc))
  167. #else
  168. #define MALLOCLIKE
  169. #endif
  170. #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
  171. #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
  172. #else
  173. #define PRINTFLIKE(f, a)
  174. #endif
  175. #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
  176. #define NORETURN __attribute__ ((noreturn))
  177. #else
  178. #define NORETURN
  179. #endif
  180. #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
  181. #define WARNUNUSED __attribute__ ((warn_unused_result))
  182. #else
  183. #define WARNUNUSED
  184. #endif
  185. void aral_judy_init(void);
  186. size_t judy_aral_overhead(void);
  187. size_t judy_aral_structures(void);
  188. #define ABS(x) (((x) < 0)? (-(x)) : (x))
  189. #define MIN(a,b) (((a)<(b))?(a):(b))
  190. #define MAX(a,b) (((a)>(b))?(a):(b))
  191. #define GUID_LEN 36
  192. // ---------------------------------------------------------------------------------------------
  193. // double linked list management
  194. // inspired by https://github.com/troydhanson/uthash/blob/master/src/utlist.h
  195. #define DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(head, item, prev, next) \
  196. do { \
  197. (item)->next = (head); \
  198. \
  199. if(likely(head)) { \
  200. (item)->prev = (head)->prev; \
  201. (head)->prev = (item); \
  202. } \
  203. else \
  204. (item)->prev = (item); \
  205. \
  206. (head) = (item); \
  207. } while (0)
  208. #define DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(head, item, prev, next) \
  209. do { \
  210. if(likely(head)) { \
  211. (item)->prev = (head)->prev; \
  212. (head)->prev->next = (item); \
  213. (head)->prev = (item); \
  214. (item)->next = NULL; \
  215. } \
  216. else { \
  217. (head) = (item); \
  218. (head)->prev = (head); \
  219. (head)->next = NULL; \
  220. } \
  221. \
  222. } while (0)
  223. #define DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(head, item, prev, next) \
  224. do { \
  225. fatal_assert((head) != NULL); \
  226. fatal_assert((item)->prev != NULL); \
  227. \
  228. if((item)->prev == (item)) \
  229. /* it is the only item in the list */ \
  230. (head) = NULL; \
  231. \
  232. else if((item) == (head)) { \
  233. /* it is the first item */ \
  234. fatal_assert((item)->next != NULL); \
  235. (item)->next->prev = (item)->prev; \
  236. (head) = (item)->next; \
  237. } \
  238. else { \
  239. /* it is any other item */ \
  240. (item)->prev->next = (item)->next; \
  241. \
  242. if ((item)->next) \
  243. (item)->next->prev = (item)->prev; \
  244. else \
  245. (head)->prev = (item)->prev; \
  246. } \
  247. \
  248. (item)->next = NULL; \
  249. (item)->prev = NULL; \
  250. } while (0)
  251. #define DOUBLE_LINKED_LIST_INSERT_ITEM_BEFORE_UNSAFE(head, existing, item, prev, next) \
  252. do { \
  253. if (existing) { \
  254. fatal_assert((head) != NULL); \
  255. fatal_assert((item) != NULL); \
  256. \
  257. (item)->next = (existing); \
  258. (item)->prev = (existing)->prev; \
  259. (existing)->prev = (item); \
  260. \
  261. if ((head) == (existing)) \
  262. (head) = (item); \
  263. else \
  264. (item)->prev->next = (item); \
  265. \
  266. } \
  267. else \
  268. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(head, item, prev, next); \
  269. \
  270. } while (0)
  271. #define DOUBLE_LINKED_LIST_INSERT_ITEM_AFTER_UNSAFE(head, existing, item, prev, next) \
  272. do { \
  273. if (existing) { \
  274. fatal_assert((head) != NULL); \
  275. fatal_assert((item) != NULL); \
  276. \
  277. (item)->next = (existing)->next; \
  278. (item)->prev = (existing); \
  279. (existing)->next = (item); \
  280. \
  281. if ((item)->next) \
  282. (item)->next->prev = (item); \
  283. else \
  284. (head)->prev = (item); \
  285. } \
  286. else \
  287. DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(head, item, prev, next); \
  288. \
  289. } while (0)
  290. #define DOUBLE_LINKED_LIST_APPEND_LIST_UNSAFE(head, head2, prev, next) \
  291. do { \
  292. if (head2) { \
  293. if (head) { \
  294. __typeof(head2) _head2_last_item = (head2)->prev; \
  295. \
  296. (head2)->prev = (head)->prev; \
  297. (head)->prev->next = (head2); \
  298. \
  299. (head)->prev = _head2_last_item; \
  300. } \
  301. else \
  302. (head) = (head2); \
  303. } \
  304. } while (0)
  305. #define DOUBLE_LINKED_LIST_FOREACH_FORWARD(head, var, prev, next) \
  306. for ((var) = (head); (var) ; (var) = (var)->next)
  307. #define DOUBLE_LINKED_LIST_FOREACH_BACKWARD(head, var, prev, next) \
  308. for ((var) = (head) ? (head)->prev : NULL ; (var) ; (var) = ((var) == (head)) ? NULL : (var)->prev)
  309. // ---------------------------------------------------------------------------------------------
  310. #include "storage_number/storage_number.h"
  311. typedef struct storage_point {
  312. NETDATA_DOUBLE min; // when count > 1, this is the minimum among them
  313. NETDATA_DOUBLE max; // when count > 1, this is the maximum among them
  314. NETDATA_DOUBLE sum; // the point sum - divided by count gives the average
  315. // end_time - start_time = point duration
  316. time_t start_time_s; // the time the point starts
  317. time_t end_time_s; // the time the point ends
  318. uint32_t count; // the number of original points aggregated
  319. uint32_t anomaly_count; // the number of original points found anomalous
  320. SN_FLAGS flags; // flags stored with the point
  321. } STORAGE_POINT;
  322. #define storage_point_unset(x) do { \
  323. (x).min = (x).max = (x).sum = NAN; \
  324. (x).count = 0; \
  325. (x).anomaly_count = 0; \
  326. (x).flags = SN_FLAG_NONE; \
  327. (x).start_time_s = 0; \
  328. (x).end_time_s = 0; \
  329. } while(0)
  330. #define storage_point_empty(x, start_s, end_s) do { \
  331. (x).min = (x).max = (x).sum = NAN; \
  332. (x).count = 1; \
  333. (x).anomaly_count = 0; \
  334. (x).flags = SN_FLAG_NONE; \
  335. (x).start_time_s = start_s; \
  336. (x).end_time_s = end_s; \
  337. } while(0)
  338. #define STORAGE_POINT_UNSET (STORAGE_POINT){ .min = NAN, .max = NAN, .sum = NAN, .count = 0, .anomaly_count = 0, .flags = SN_FLAG_NONE, .start_time_s = 0, .end_time_s = 0 }
  339. #define storage_point_is_unset(x) (!(x).count)
  340. #define storage_point_is_gap(x) (!netdata_double_isnumber((x).sum))
  341. #define storage_point_is_zero(x) (!(x).count || (netdata_double_is_zero((x).min) && netdata_double_is_zero((x).max) && netdata_double_is_zero((x).sum) && (x).anomaly_count == 0))
  342. #define storage_point_merge_to(dst, src) do { \
  343. if(storage_point_is_unset(dst)) \
  344. (dst) = (src); \
  345. \
  346. else if(!storage_point_is_unset(src) && \
  347. !storage_point_is_gap(src)) { \
  348. \
  349. if((src).start_time_s < (dst).start_time_s) \
  350. (dst).start_time_s = (src).start_time_s;\
  351. \
  352. if((src).end_time_s > (dst).end_time_s) \
  353. (dst).end_time_s = (src).end_time_s; \
  354. \
  355. if((src).min < (dst).min) \
  356. (dst).min = (src).min; \
  357. \
  358. if((src).max > (dst).max) \
  359. (dst).max = (src).max; \
  360. \
  361. (dst).sum += (src).sum; \
  362. \
  363. (dst).count += (src).count; \
  364. (dst).anomaly_count += (src).anomaly_count; \
  365. \
  366. (dst).flags |= (src).flags & SN_FLAG_RESET; \
  367. } \
  368. } while(0)
  369. #define storage_point_add_to(dst, src) do { \
  370. if(storage_point_is_unset(dst)) \
  371. (dst) = (src); \
  372. \
  373. else if(!storage_point_is_unset(src) && \
  374. !storage_point_is_gap(src)) { \
  375. \
  376. if((src).start_time_s < (dst).start_time_s) \
  377. (dst).start_time_s = (src).start_time_s;\
  378. \
  379. if((src).end_time_s > (dst).end_time_s) \
  380. (dst).end_time_s = (src).end_time_s; \
  381. \
  382. (dst).min += (src).min; \
  383. (dst).max += (src).max; \
  384. (dst).sum += (src).sum; \
  385. \
  386. (dst).count += (src).count; \
  387. (dst).anomaly_count += (src).anomaly_count; \
  388. \
  389. (dst).flags |= (src).flags & SN_FLAG_RESET; \
  390. } \
  391. } while(0)
  392. #define storage_point_make_positive(sp) do { \
  393. if(!storage_point_is_unset(sp) && \
  394. !storage_point_is_gap(sp)) { \
  395. \
  396. if(unlikely(signbit((sp).sum))) \
  397. (sp).sum = -(sp).sum; \
  398. \
  399. if(unlikely(signbit((sp).min))) \
  400. (sp).min = -(sp).min; \
  401. \
  402. if(unlikely(signbit((sp).max))) \
  403. (sp).max = -(sp).max; \
  404. \
  405. if(unlikely((sp).min > (sp).max)) { \
  406. NETDATA_DOUBLE t = (sp).min; \
  407. (sp).min = (sp).max; \
  408. (sp).max = t; \
  409. } \
  410. } \
  411. } while(0)
  412. #define storage_point_anomaly_rate(sp) \
  413. (NETDATA_DOUBLE)(storage_point_is_unset(sp) ? 0.0 : (NETDATA_DOUBLE)((sp).anomaly_count) * 100.0 / (NETDATA_DOUBLE)((sp).count))
  414. #define storage_point_average_value(sp) \
  415. ((sp).count ? (sp).sum / (NETDATA_DOUBLE)((sp).count) : 0.0)
  416. // ---------------------------------------------------------------------------------------------
  417. void netdata_fix_chart_id(char *s);
  418. void netdata_fix_chart_name(char *s);
  419. int madvise_sequential(void *mem, size_t len);
  420. int madvise_random(void *mem, size_t len);
  421. int madvise_dontfork(void *mem, size_t len);
  422. int madvise_willneed(void *mem, size_t len);
  423. int madvise_dontneed(void *mem, size_t len);
  424. int madvise_dontdump(void *mem, size_t len);
  425. int madvise_mergeable(void *mem, size_t len);
  426. int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
  427. int snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
  428. // memory allocation functions that handle failures
  429. #ifdef NETDATA_TRACE_ALLOCATIONS
  430. int malloc_trace_walkthrough(int (*callback)(void *item, void *data), void *data);
  431. #define strdupz(s) strdupz_int(s, __FILE__, __FUNCTION__, __LINE__)
  432. #define callocz(nmemb, size) callocz_int(nmemb, size, __FILE__, __FUNCTION__, __LINE__)
  433. #define mallocz(size) mallocz_int(size, __FILE__, __FUNCTION__, __LINE__)
  434. #define reallocz(ptr, size) reallocz_int(ptr, size, __FILE__, __FUNCTION__, __LINE__)
  435. #define freez(ptr) freez_int(ptr, __FILE__, __FUNCTION__, __LINE__)
  436. #define mallocz_usable_size(ptr) mallocz_usable_size_int(ptr, __FILE__, __FUNCTION__, __LINE__)
  437. char *strdupz_int(const char *s, const char *file, const char *function, size_t line);
  438. void *callocz_int(size_t nmemb, size_t size, const char *file, const char *function, size_t line);
  439. void *mallocz_int(size_t size, const char *file, const char *function, size_t line);
  440. void *reallocz_int(void *ptr, size_t size, const char *file, const char *function, size_t line);
  441. void freez_int(void *ptr, const char *file, const char *function, size_t line);
  442. size_t mallocz_usable_size_int(void *ptr, const char *file, const char *function, size_t line);
  443. #else // NETDATA_TRACE_ALLOCATIONS
  444. char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
  445. void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
  446. void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
  447. void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
  448. void freez(void *ptr);
  449. #endif // NETDATA_TRACE_ALLOCATIONS
  450. void posix_memfree(void *ptr);
  451. void json_escape_string(char *dst, const char *src, size_t size);
  452. void json_fix_string(char *s);
  453. void *netdata_mmap(const char *filename, size_t size, int flags, int ksm, bool read_only, int *open_fd);
  454. int netdata_munmap(void *ptr, size_t size);
  455. int memory_file_save(const char *filename, void *mem, size_t size);
  456. int fd_is_valid(int fd);
  457. extern struct rlimit rlimit_nofile;
  458. extern int enable_ksm;
  459. char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
  460. int verify_netdata_host_prefix();
  461. int recursively_delete_dir(const char *path, const char *reason);
  462. extern volatile sig_atomic_t netdata_exit;
  463. extern const char *program_version;
  464. char *strdupz_path_subpath(const char *path, const char *subpath);
  465. int path_is_dir(const char *path, const char *subpath);
  466. int path_is_file(const char *path, const char *subpath);
  467. void recursive_config_double_dir_load(
  468. const char *user_path
  469. , const char *stock_path
  470. , const char *subpath
  471. , int (*callback)(const char *filename, void *data)
  472. , void *data
  473. , size_t depth
  474. );
  475. char *read_by_filename(char *filename, long *file_size);
  476. char *find_and_replace(const char *src, const char *find, const char *replace, const char *where);
  477. /* fix for alpine linux */
  478. #ifndef RUSAGE_THREAD
  479. #ifdef RUSAGE_CHILDREN
  480. #define RUSAGE_THREAD RUSAGE_CHILDREN
  481. #endif
  482. #endif
  483. #define BITS_IN_A_KILOBIT 1000
  484. #define KILOBITS_IN_A_MEGABIT 1000
  485. /* misc. */
  486. #define UNUSED(x) (void)(x)
  487. #ifdef __GNUC__
  488. #define UNUSED_FUNCTION(x) __attribute__((unused)) UNUSED_##x
  489. #else
  490. #define UNUSED_FUNCTION(x) UNUSED_##x
  491. #endif
  492. #define error_report(x, args...) do { errno = 0; error(x, ##args); } while(0)
  493. // Taken from linux kernel
  494. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  495. typedef struct bitmap256 {
  496. uint64_t data[4];
  497. } BITMAP256;
  498. bool bitmap256_get_bit(BITMAP256 *ptr, uint8_t idx);
  499. void bitmap256_set_bit(BITMAP256 *ptr, uint8_t idx, bool value);
  500. #define COMPRESSION_MAX_MSG_SIZE 0x4000
  501. #define PLUGINSD_LINE_MAX (COMPRESSION_MAX_MSG_SIZE - 1024)
  502. int config_isspace(char c);
  503. int pluginsd_space(char c);
  504. size_t quoted_strings_splitter(char *str, char **words, size_t max_words, int (*custom_isspace)(char));
  505. size_t pluginsd_split_words(char *str, char **words, size_t max_words);
  506. static inline char *get_word(char **words, size_t num_words, size_t index) {
  507. if (index >= num_words)
  508. return NULL;
  509. return words[index];
  510. }
  511. bool run_command_and_copy_output_to_stdout(const char *command, int max_line_length);
  512. typedef enum {
  513. OPEN_FD_ACTION_CLOSE,
  514. OPEN_FD_ACTION_FD_CLOEXEC
  515. } OPEN_FD_ACTION;
  516. typedef enum {
  517. OPEN_FD_EXCLUDE_STDIN = 0x01,
  518. OPEN_FD_EXCLUDE_STDOUT = 0x02,
  519. OPEN_FD_EXCLUDE_STDERR = 0x04
  520. } OPEN_FD_EXCLUDE;
  521. void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds);
  522. void netdata_cleanup_and_exit(int ret) NORETURN;
  523. void send_statistics(const char *action, const char *action_result, const char *action_data);
  524. extern char *netdata_configured_host_prefix;
  525. #include "libjudy/src/Judy.h"
  526. #include "july/july.h"
  527. #include "os.h"
  528. #include "threads/threads.h"
  529. #include "buffer/buffer.h"
  530. #include "locks/locks.h"
  531. #include "circular_buffer/circular_buffer.h"
  532. #include "avl/avl.h"
  533. #include "inlined.h"
  534. #include "clocks/clocks.h"
  535. #include "completion/completion.h"
  536. #include "popen/popen.h"
  537. #include "simple_pattern/simple_pattern.h"
  538. #ifdef ENABLE_HTTPS
  539. # include "socket/security.h"
  540. #endif
  541. #include "socket/socket.h"
  542. #include "config/appconfig.h"
  543. #include "log/log.h"
  544. #include "procfile/procfile.h"
  545. #include "string/string.h"
  546. #include "dictionary/dictionary.h"
  547. #if defined(HAVE_LIBBPF) && !defined(__cplusplus)
  548. #include "ebpf/ebpf.h"
  549. #endif
  550. #include "eval/eval.h"
  551. #include "statistical/statistical.h"
  552. #include "adaptive_resortable_list/adaptive_resortable_list.h"
  553. #include "url/url.h"
  554. #include "json/json.h"
  555. #include "health/health.h"
  556. #include "string/utf8.h"
  557. #include "libnetdata/aral/aral.h"
  558. #include "onewayalloc/onewayalloc.h"
  559. #include "worker_utilization/worker_utilization.h"
  560. #include "parser/parser.h"
  561. #include "yaml.h"
  562. // BEWARE: this exists in alarm-notify.sh
  563. #define DEFAULT_CLOUD_BASE_URL "https://app.netdata.cloud"
  564. #define RRD_STORAGE_TIERS 5
  565. static inline size_t struct_natural_alignment(size_t size) __attribute__((const));
  566. #define STRUCT_NATURAL_ALIGNMENT (sizeof(uintptr_t) * 2)
  567. static inline size_t struct_natural_alignment(size_t size) {
  568. if(unlikely(size % STRUCT_NATURAL_ALIGNMENT))
  569. size = size + STRUCT_NATURAL_ALIGNMENT - (size % STRUCT_NATURAL_ALIGNMENT);
  570. return size;
  571. }
  572. #ifdef NETDATA_TRACE_ALLOCATIONS
  573. struct malloc_trace {
  574. avl_t avl;
  575. const char *function;
  576. const char *file;
  577. size_t line;
  578. size_t malloc_calls;
  579. size_t calloc_calls;
  580. size_t realloc_calls;
  581. size_t strdup_calls;
  582. size_t free_calls;
  583. size_t mmap_calls;
  584. size_t munmap_calls;
  585. size_t allocations;
  586. size_t bytes;
  587. struct rrddim *rd_bytes;
  588. struct rrddim *rd_allocations;
  589. struct rrddim *rd_avg_alloc;
  590. struct rrddim *rd_ops;
  591. };
  592. #endif // NETDATA_TRACE_ALLOCATIONS
  593. static inline PPvoid_t JudyLFirstThenNext(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  594. if(unlikely(*first)) {
  595. *first = false;
  596. return JudyLFirst(PArray, PIndex, PJE0);
  597. }
  598. return JudyLNext(PArray, PIndex, PJE0);
  599. }
  600. static inline PPvoid_t JudyLLastThenPrev(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  601. if(unlikely(*first)) {
  602. *first = false;
  603. return JudyLLast(PArray, PIndex, PJE0);
  604. }
  605. return JudyLPrev(PArray, PIndex, PJE0);
  606. }
  607. typedef enum {
  608. TIMING_STEP_INTERNAL = 0,
  609. TIMING_STEP_BEGIN2_PREPARE,
  610. TIMING_STEP_BEGIN2_FIND_CHART,
  611. TIMING_STEP_BEGIN2_PARSE,
  612. TIMING_STEP_BEGIN2_ML,
  613. TIMING_STEP_BEGIN2_PROPAGATE,
  614. TIMING_STEP_BEGIN2_STORE,
  615. TIMING_STEP_SET2_PREPARE,
  616. TIMING_STEP_SET2_LOOKUP_DIMENSION,
  617. TIMING_STEP_SET2_PARSE,
  618. TIMING_STEP_SET2_ML,
  619. TIMING_STEP_SET2_PROPAGATE,
  620. TIMING_STEP_RRDSET_STORE_METRIC,
  621. TIMING_STEP_DBENGINE_FIRST_CHECK,
  622. TIMING_STEP_DBENGINE_CHECK_DATA,
  623. TIMING_STEP_DBENGINE_PACK,
  624. TIMING_STEP_DBENGINE_PAGE_FIN,
  625. TIMING_STEP_DBENGINE_MRG_UPDATE,
  626. TIMING_STEP_DBENGINE_PAGE_ALLOC,
  627. TIMING_STEP_DBENGINE_CREATE_NEW_PAGE,
  628. TIMING_STEP_DBENGINE_FLUSH_PAGE,
  629. TIMING_STEP_SET2_STORE,
  630. TIMING_STEP_END2_PREPARE,
  631. TIMING_STEP_END2_PUSH_V1,
  632. TIMING_STEP_END2_ML,
  633. TIMING_STEP_END2_RRDSET,
  634. TIMING_STEP_END2_PROPAGATE,
  635. TIMING_STEP_END2_STORE,
  636. // terminator
  637. TIMING_STEP_MAX,
  638. } TIMING_STEP;
  639. typedef enum {
  640. TIMING_ACTION_INIT,
  641. TIMING_ACTION_STEP,
  642. TIMING_ACTION_FINISH,
  643. } TIMING_ACTION;
  644. #ifdef NETDATA_TIMING_REPORT
  645. #define timing_init() timing_action(TIMING_ACTION_INIT, TIMING_STEP_INTERNAL)
  646. #define timing_step(step) timing_action(TIMING_ACTION_STEP, step)
  647. #define timing_report() timing_action(TIMING_ACTION_FINISH, TIMING_STEP_INTERNAL)
  648. #else
  649. #define timing_init() debug_dummy()
  650. #define timing_step(step) debug_dummy()
  651. #define timing_report() debug_dummy()
  652. #endif
  653. void timing_action(TIMING_ACTION action, TIMING_STEP step);
  654. int hash256_string(const unsigned char *string, size_t size, char *hash);
  655. # ifdef __cplusplus
  656. }
  657. # endif
  658. #endif // NETDATA_LIB_H