libnetdata.h 36 KB

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