libnetdata.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. #include <zlib.h>
  145. #ifdef HAVE_CAPABILITY
  146. #include <sys/capability.h>
  147. #endif
  148. // ----------------------------------------------------------------------------
  149. // netdata common definitions
  150. #ifdef __GNUC__
  151. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  152. #endif // __GNUC__
  153. #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
  154. #define NEVERNULL __attribute__((returns_nonnull))
  155. #else
  156. #define NEVERNULL
  157. #endif
  158. #ifdef HAVE_FUNC_ATTRIBUTE_NOINLINE
  159. #define NOINLINE __attribute__((noinline))
  160. #else
  161. #define NOINLINE
  162. #endif
  163. #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
  164. #define MALLOCLIKE __attribute__((malloc))
  165. #else
  166. #define MALLOCLIKE
  167. #endif
  168. #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
  169. #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
  170. #else
  171. #define PRINTFLIKE(f, a)
  172. #endif
  173. #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
  174. #define NORETURN __attribute__ ((noreturn))
  175. #else
  176. #define NORETURN
  177. #endif
  178. #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
  179. #define WARNUNUSED __attribute__ ((warn_unused_result))
  180. #else
  181. #define WARNUNUSED
  182. #endif
  183. void aral_judy_init(void);
  184. size_t judy_aral_overhead(void);
  185. size_t judy_aral_structures(void);
  186. #define ABS(x) (((x) < 0)? (-(x)) : (x))
  187. #define MIN(a,b) (((a)<(b))?(a):(b))
  188. #define MAX(a,b) (((a)>(b))?(a):(b))
  189. #define GUID_LEN 36
  190. // ---------------------------------------------------------------------------------------------
  191. // double linked list management
  192. // inspired by https://github.com/troydhanson/uthash/blob/master/src/utlist.h
  193. #define DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(head, item, prev, next) \
  194. do { \
  195. (item)->next = (head); \
  196. \
  197. if(likely(head)) { \
  198. (item)->prev = (head)->prev; \
  199. (head)->prev = (item); \
  200. } \
  201. else \
  202. (item)->prev = (item); \
  203. \
  204. (head) = (item); \
  205. } while (0)
  206. #define DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(head, item, prev, next) \
  207. do { \
  208. if(likely(head)) { \
  209. (item)->prev = (head)->prev; \
  210. (head)->prev->next = (item); \
  211. (head)->prev = (item); \
  212. (item)->next = NULL; \
  213. } \
  214. else { \
  215. (head) = (item); \
  216. (head)->prev = (head); \
  217. (head)->next = NULL; \
  218. } \
  219. \
  220. } while (0)
  221. #define DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(head, item, prev, next) \
  222. do { \
  223. fatal_assert((head) != NULL); \
  224. fatal_assert((item)->prev != NULL); \
  225. \
  226. if((item)->prev == (item)) \
  227. /* it is the only item in the list */ \
  228. (head) = NULL; \
  229. \
  230. else if((item) == (head)) { \
  231. /* it is the first item */ \
  232. fatal_assert((item)->next != NULL); \
  233. (item)->next->prev = (item)->prev; \
  234. (head) = (item)->next; \
  235. } \
  236. else { \
  237. /* it is any other item */ \
  238. (item)->prev->next = (item)->next; \
  239. \
  240. if ((item)->next) \
  241. (item)->next->prev = (item)->prev; \
  242. else \
  243. (head)->prev = (item)->prev; \
  244. } \
  245. \
  246. (item)->next = NULL; \
  247. (item)->prev = NULL; \
  248. } while (0)
  249. #define DOUBLE_LINKED_LIST_INSERT_ITEM_BEFORE_UNSAFE(head, existing, item, prev, next) \
  250. do { \
  251. if (existing) { \
  252. fatal_assert((head) != NULL); \
  253. fatal_assert((item) != NULL); \
  254. \
  255. (item)->next = (existing); \
  256. (item)->prev = (existing)->prev; \
  257. (existing)->prev = (item); \
  258. \
  259. if ((head) == (existing)) \
  260. (head) = (item); \
  261. else \
  262. (item)->prev->next = (item); \
  263. \
  264. } \
  265. else \
  266. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(head, item, prev, next); \
  267. \
  268. } while (0)
  269. #define DOUBLE_LINKED_LIST_INSERT_ITEM_AFTER_UNSAFE(head, existing, item, prev, next) \
  270. do { \
  271. if (existing) { \
  272. fatal_assert((head) != NULL); \
  273. fatal_assert((item) != NULL); \
  274. \
  275. (item)->next = (existing)->next; \
  276. (item)->prev = (existing); \
  277. (existing)->next = (item); \
  278. \
  279. if ((item)->next) \
  280. (item)->next->prev = (item); \
  281. else \
  282. (head)->prev = (item); \
  283. } \
  284. else \
  285. DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(head, item, prev, next); \
  286. \
  287. } while (0)
  288. #define DOUBLE_LINKED_LIST_APPEND_LIST_UNSAFE(head, head2, prev, next) \
  289. do { \
  290. if (head2) { \
  291. if (head) { \
  292. __typeof(head2) _head2_last_item = (head2)->prev; \
  293. \
  294. (head2)->prev = (head)->prev; \
  295. (head)->prev->next = (head2); \
  296. \
  297. (head)->prev = _head2_last_item; \
  298. } \
  299. else \
  300. (head) = (head2); \
  301. } \
  302. } while (0)
  303. #define DOUBLE_LINKED_LIST_FOREACH_FORWARD(head, var, prev, next) \
  304. for ((var) = (head); (var) ; (var) = (var)->next)
  305. #define DOUBLE_LINKED_LIST_FOREACH_BACKWARD(head, var, prev, next) \
  306. for ((var) = (head) ? (head)->prev : NULL ; (var) ; (var) = ((var) == (head)) ? NULL : (var)->prev)
  307. // ---------------------------------------------------------------------------------------------
  308. #include "storage_number/storage_number.h"
  309. typedef struct storage_point {
  310. NETDATA_DOUBLE min; // when count > 1, this is the minimum among them
  311. NETDATA_DOUBLE max; // when count > 1, this is the maximum among them
  312. NETDATA_DOUBLE sum; // the point sum - divided by count gives the average
  313. // end_time - start_time = point duration
  314. time_t start_time_s; // the time the point starts
  315. time_t end_time_s; // the time the point ends
  316. uint32_t count; // the number of original points aggregated
  317. uint32_t anomaly_count; // the number of original points found anomalous
  318. SN_FLAGS flags; // flags stored with the point
  319. } STORAGE_POINT;
  320. #define storage_point_unset(x) do { \
  321. (x).min = (x).max = (x).sum = NAN; \
  322. (x).count = 0; \
  323. (x).anomaly_count = 0; \
  324. (x).flags = SN_FLAG_NONE; \
  325. (x).start_time_s = 0; \
  326. (x).end_time_s = 0; \
  327. } while(0)
  328. #define storage_point_empty(x, start_s, end_s) do { \
  329. (x).min = (x).max = (x).sum = NAN; \
  330. (x).count = 1; \
  331. (x).anomaly_count = 0; \
  332. (x).flags = SN_FLAG_NONE; \
  333. (x).start_time_s = start_s; \
  334. (x).end_time_s = end_s; \
  335. } while(0)
  336. #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 }
  337. #define storage_point_is_unset(x) (!(x).count)
  338. #define storage_point_is_gap(x) (!netdata_double_isnumber((x).sum))
  339. #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))
  340. #define storage_point_merge_to(dst, src) do { \
  341. if(storage_point_is_unset(dst)) \
  342. (dst) = (src); \
  343. \
  344. else if(!storage_point_is_unset(src) && \
  345. !storage_point_is_gap(src)) { \
  346. \
  347. if((src).start_time_s < (dst).start_time_s) \
  348. (dst).start_time_s = (src).start_time_s;\
  349. \
  350. if((src).end_time_s > (dst).end_time_s) \
  351. (dst).end_time_s = (src).end_time_s; \
  352. \
  353. if((src).min < (dst).min) \
  354. (dst).min = (src).min; \
  355. \
  356. if((src).max > (dst).max) \
  357. (dst).max = (src).max; \
  358. \
  359. (dst).sum += (src).sum; \
  360. \
  361. (dst).count += (src).count; \
  362. (dst).anomaly_count += (src).anomaly_count; \
  363. \
  364. (dst).flags |= (src).flags & SN_FLAG_RESET; \
  365. } \
  366. } while(0)
  367. #define storage_point_add_to(dst, src) do { \
  368. if(storage_point_is_unset(dst)) \
  369. (dst) = (src); \
  370. \
  371. else if(!storage_point_is_unset(src) && \
  372. !storage_point_is_gap(src)) { \
  373. \
  374. if((src).start_time_s < (dst).start_time_s) \
  375. (dst).start_time_s = (src).start_time_s;\
  376. \
  377. if((src).end_time_s > (dst).end_time_s) \
  378. (dst).end_time_s = (src).end_time_s; \
  379. \
  380. (dst).min += (src).min; \
  381. (dst).max += (src).max; \
  382. (dst).sum += (src).sum; \
  383. \
  384. (dst).count += (src).count; \
  385. (dst).anomaly_count += (src).anomaly_count; \
  386. \
  387. (dst).flags |= (src).flags & SN_FLAG_RESET; \
  388. } \
  389. } while(0)
  390. #define storage_point_make_positive(sp) do { \
  391. if(!storage_point_is_unset(sp) && \
  392. !storage_point_is_gap(sp)) { \
  393. \
  394. if(unlikely(signbit((sp).sum))) \
  395. (sp).sum = -(sp).sum; \
  396. \
  397. if(unlikely(signbit((sp).min))) \
  398. (sp).min = -(sp).min; \
  399. \
  400. if(unlikely(signbit((sp).max))) \
  401. (sp).max = -(sp).max; \
  402. \
  403. if(unlikely((sp).min > (sp).max)) { \
  404. NETDATA_DOUBLE t = (sp).min; \
  405. (sp).min = (sp).max; \
  406. (sp).max = t; \
  407. } \
  408. } \
  409. } while(0)
  410. #define storage_point_anomaly_rate(sp) \
  411. (NETDATA_DOUBLE)(storage_point_is_unset(sp) ? 0.0 : (NETDATA_DOUBLE)((sp).anomaly_count) * 100.0 / (NETDATA_DOUBLE)((sp).count))
  412. #define storage_point_average_value(sp) \
  413. ((sp).count ? (sp).sum / (NETDATA_DOUBLE)((sp).count) : 0.0)
  414. // ---------------------------------------------------------------------------------------------
  415. void netdata_fix_chart_id(char *s);
  416. void netdata_fix_chart_name(char *s);
  417. int madvise_sequential(void *mem, size_t len);
  418. int madvise_random(void *mem, size_t len);
  419. int madvise_dontfork(void *mem, size_t len);
  420. int madvise_willneed(void *mem, size_t len);
  421. int madvise_dontneed(void *mem, size_t len);
  422. int madvise_dontdump(void *mem, size_t len);
  423. int madvise_mergeable(void *mem, size_t len);
  424. int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
  425. int snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
  426. // memory allocation functions that handle failures
  427. #ifdef NETDATA_TRACE_ALLOCATIONS
  428. int malloc_trace_walkthrough(int (*callback)(void *item, void *data), void *data);
  429. #define strdupz(s) strdupz_int(s, __FILE__, __FUNCTION__, __LINE__)
  430. #define callocz(nmemb, size) callocz_int(nmemb, size, __FILE__, __FUNCTION__, __LINE__)
  431. #define mallocz(size) mallocz_int(size, __FILE__, __FUNCTION__, __LINE__)
  432. #define reallocz(ptr, size) reallocz_int(ptr, size, __FILE__, __FUNCTION__, __LINE__)
  433. #define freez(ptr) freez_int(ptr, __FILE__, __FUNCTION__, __LINE__)
  434. #define mallocz_usable_size(ptr) mallocz_usable_size_int(ptr, __FILE__, __FUNCTION__, __LINE__)
  435. char *strdupz_int(const char *s, const char *file, const char *function, size_t line);
  436. void *callocz_int(size_t nmemb, size_t size, const char *file, const char *function, size_t line);
  437. void *mallocz_int(size_t size, const char *file, const char *function, size_t line);
  438. void *reallocz_int(void *ptr, size_t size, const char *file, const char *function, size_t line);
  439. void freez_int(void *ptr, const char *file, const char *function, size_t line);
  440. size_t mallocz_usable_size_int(void *ptr, const char *file, const char *function, size_t line);
  441. #else // NETDATA_TRACE_ALLOCATIONS
  442. char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
  443. void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
  444. void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
  445. void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
  446. void freez(void *ptr);
  447. #endif // NETDATA_TRACE_ALLOCATIONS
  448. void posix_memfree(void *ptr);
  449. void json_escape_string(char *dst, const char *src, size_t size);
  450. void json_fix_string(char *s);
  451. void *netdata_mmap(const char *filename, size_t size, int flags, int ksm, bool read_only, int *open_fd);
  452. int netdata_munmap(void *ptr, size_t size);
  453. int memory_file_save(const char *filename, void *mem, size_t size);
  454. int fd_is_valid(int fd);
  455. extern struct rlimit rlimit_nofile;
  456. extern int enable_ksm;
  457. char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
  458. int verify_netdata_host_prefix();
  459. int recursively_delete_dir(const char *path, const char *reason);
  460. extern volatile sig_atomic_t netdata_exit;
  461. extern const char *program_version;
  462. char *strdupz_path_subpath(const char *path, const char *subpath);
  463. int path_is_dir(const char *path, const char *subpath);
  464. int path_is_file(const char *path, const char *subpath);
  465. void recursive_config_double_dir_load(
  466. const char *user_path
  467. , const char *stock_path
  468. , const char *subpath
  469. , int (*callback)(const char *filename, void *data)
  470. , void *data
  471. , size_t depth
  472. );
  473. char *read_by_filename(char *filename, long *file_size);
  474. char *find_and_replace(const char *src, const char *find, const char *replace, const char *where);
  475. /* fix for alpine linux */
  476. #ifndef RUSAGE_THREAD
  477. #ifdef RUSAGE_CHILDREN
  478. #define RUSAGE_THREAD RUSAGE_CHILDREN
  479. #endif
  480. #endif
  481. #define BITS_IN_A_KILOBIT 1000
  482. #define KILOBITS_IN_A_MEGABIT 1000
  483. /* misc. */
  484. #define UNUSED(x) (void)(x)
  485. #ifdef __GNUC__
  486. #define UNUSED_FUNCTION(x) __attribute__((unused)) UNUSED_##x
  487. #else
  488. #define UNUSED_FUNCTION(x) UNUSED_##x
  489. #endif
  490. #define error_report(x, args...) do { errno = 0; error(x, ##args); } while(0)
  491. // Taken from linux kernel
  492. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  493. typedef struct bitmap256 {
  494. uint64_t data[4];
  495. } BITMAP256;
  496. bool bitmap256_get_bit(BITMAP256 *ptr, uint8_t idx);
  497. void bitmap256_set_bit(BITMAP256 *ptr, uint8_t idx, bool value);
  498. #define COMPRESSION_MAX_MSG_SIZE 0x4000
  499. #define PLUGINSD_LINE_MAX (COMPRESSION_MAX_MSG_SIZE - 1024)
  500. int config_isspace(char c);
  501. int pluginsd_space(char c);
  502. size_t quoted_strings_splitter(char *str, char **words, size_t max_words, int (*custom_isspace)(char));
  503. size_t pluginsd_split_words(char *str, char **words, size_t max_words);
  504. static inline char *get_word(char **words, size_t num_words, size_t index) {
  505. if (index >= num_words)
  506. return NULL;
  507. return words[index];
  508. }
  509. bool run_command_and_copy_output_to_stdout(const char *command, int max_line_length);
  510. typedef enum {
  511. OPEN_FD_ACTION_CLOSE,
  512. OPEN_FD_ACTION_FD_CLOEXEC
  513. } OPEN_FD_ACTION;
  514. typedef enum {
  515. OPEN_FD_EXCLUDE_STDIN = 0x01,
  516. OPEN_FD_EXCLUDE_STDOUT = 0x02,
  517. OPEN_FD_EXCLUDE_STDERR = 0x04
  518. } OPEN_FD_EXCLUDE;
  519. void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds);
  520. void netdata_cleanup_and_exit(int ret) NORETURN;
  521. void send_statistics(const char *action, const char *action_result, const char *action_data);
  522. extern char *netdata_configured_host_prefix;
  523. #include "libjudy/src/Judy.h"
  524. #include "july/july.h"
  525. #include "os.h"
  526. #include "threads/threads.h"
  527. #include "buffer/buffer.h"
  528. #include "locks/locks.h"
  529. #include "circular_buffer/circular_buffer.h"
  530. #include "avl/avl.h"
  531. #include "inlined.h"
  532. #include "clocks/clocks.h"
  533. #include "completion/completion.h"
  534. #include "popen/popen.h"
  535. #include "simple_pattern/simple_pattern.h"
  536. #ifdef ENABLE_HTTPS
  537. # include "socket/security.h"
  538. #endif
  539. #include "socket/socket.h"
  540. #include "config/appconfig.h"
  541. #include "log/log.h"
  542. #include "procfile/procfile.h"
  543. #include "string/string.h"
  544. #include "dictionary/dictionary.h"
  545. #if defined(HAVE_LIBBPF) && !defined(__cplusplus)
  546. #include "ebpf/ebpf.h"
  547. #endif
  548. #include "eval/eval.h"
  549. #include "statistical/statistical.h"
  550. #include "adaptive_resortable_list/adaptive_resortable_list.h"
  551. #include "url/url.h"
  552. #include "json/json.h"
  553. #include "health/health.h"
  554. #include "string/utf8.h"
  555. #include "libnetdata/aral/aral.h"
  556. #include "onewayalloc/onewayalloc.h"
  557. #include "worker_utilization/worker_utilization.h"
  558. #include "parser/parser.h"
  559. #include "yaml.h"
  560. #include "http/http_defs.h"
  561. // BEWARE: this exists in alarm-notify.sh
  562. #define DEFAULT_CLOUD_BASE_URL "https://app.netdata.cloud"
  563. #define RRD_STORAGE_TIERS 5
  564. static inline size_t struct_natural_alignment(size_t size) __attribute__((const));
  565. #define STRUCT_NATURAL_ALIGNMENT (sizeof(uintptr_t) * 2)
  566. static inline size_t struct_natural_alignment(size_t size) {
  567. if(unlikely(size % STRUCT_NATURAL_ALIGNMENT))
  568. size = size + STRUCT_NATURAL_ALIGNMENT - (size % STRUCT_NATURAL_ALIGNMENT);
  569. return size;
  570. }
  571. #ifdef NETDATA_TRACE_ALLOCATIONS
  572. struct malloc_trace {
  573. avl_t avl;
  574. const char *function;
  575. const char *file;
  576. size_t line;
  577. size_t malloc_calls;
  578. size_t calloc_calls;
  579. size_t realloc_calls;
  580. size_t strdup_calls;
  581. size_t free_calls;
  582. size_t mmap_calls;
  583. size_t munmap_calls;
  584. size_t allocations;
  585. size_t bytes;
  586. struct rrddim *rd_bytes;
  587. struct rrddim *rd_allocations;
  588. struct rrddim *rd_avg_alloc;
  589. struct rrddim *rd_ops;
  590. };
  591. #endif // NETDATA_TRACE_ALLOCATIONS
  592. static inline PPvoid_t JudyLFirstThenNext(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  593. if(unlikely(*first)) {
  594. *first = false;
  595. return JudyLFirst(PArray, PIndex, PJE0);
  596. }
  597. return JudyLNext(PArray, PIndex, PJE0);
  598. }
  599. static inline PPvoid_t JudyLLastThenPrev(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  600. if(unlikely(*first)) {
  601. *first = false;
  602. return JudyLLast(PArray, PIndex, PJE0);
  603. }
  604. return JudyLPrev(PArray, PIndex, PJE0);
  605. }
  606. typedef enum {
  607. TIMING_STEP_INTERNAL = 0,
  608. TIMING_STEP_BEGIN2_PREPARE,
  609. TIMING_STEP_BEGIN2_FIND_CHART,
  610. TIMING_STEP_BEGIN2_PARSE,
  611. TIMING_STEP_BEGIN2_ML,
  612. TIMING_STEP_BEGIN2_PROPAGATE,
  613. TIMING_STEP_BEGIN2_STORE,
  614. TIMING_STEP_SET2_PREPARE,
  615. TIMING_STEP_SET2_LOOKUP_DIMENSION,
  616. TIMING_STEP_SET2_PARSE,
  617. TIMING_STEP_SET2_ML,
  618. TIMING_STEP_SET2_PROPAGATE,
  619. TIMING_STEP_RRDSET_STORE_METRIC,
  620. TIMING_STEP_DBENGINE_FIRST_CHECK,
  621. TIMING_STEP_DBENGINE_CHECK_DATA,
  622. TIMING_STEP_DBENGINE_PACK,
  623. TIMING_STEP_DBENGINE_PAGE_FIN,
  624. TIMING_STEP_DBENGINE_MRG_UPDATE,
  625. TIMING_STEP_DBENGINE_PAGE_ALLOC,
  626. TIMING_STEP_DBENGINE_CREATE_NEW_PAGE,
  627. TIMING_STEP_DBENGINE_FLUSH_PAGE,
  628. TIMING_STEP_SET2_STORE,
  629. TIMING_STEP_END2_PREPARE,
  630. TIMING_STEP_END2_PUSH_V1,
  631. TIMING_STEP_END2_ML,
  632. TIMING_STEP_END2_RRDSET,
  633. TIMING_STEP_END2_PROPAGATE,
  634. TIMING_STEP_END2_STORE,
  635. // terminator
  636. TIMING_STEP_MAX,
  637. } TIMING_STEP;
  638. typedef enum {
  639. TIMING_ACTION_INIT,
  640. TIMING_ACTION_STEP,
  641. TIMING_ACTION_FINISH,
  642. } TIMING_ACTION;
  643. #ifdef NETDATA_TIMING_REPORT
  644. #define timing_init() timing_action(TIMING_ACTION_INIT, TIMING_STEP_INTERNAL)
  645. #define timing_step(step) timing_action(TIMING_ACTION_STEP, step)
  646. #define timing_report() timing_action(TIMING_ACTION_FINISH, TIMING_STEP_INTERNAL)
  647. #else
  648. #define timing_init() debug_dummy()
  649. #define timing_step(step) debug_dummy()
  650. #define timing_report() debug_dummy()
  651. #endif
  652. void timing_action(TIMING_ACTION action, TIMING_STEP step);
  653. int hash256_string(const unsigned char *string, size_t size, char *hash);
  654. # ifdef __cplusplus
  655. }
  656. # endif
  657. #endif // NETDATA_LIB_H