libnetdata.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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_LZ4
  11. #define ENABLE_RRDPUSH_COMPRESSION 1
  12. #endif
  13. #ifdef ENABLE_OPENSSL
  14. #define ENABLE_HTTPS 1
  15. #endif
  16. #ifdef HAVE_LIBDATACHANNEL
  17. #define ENABLE_WEBRTC 1
  18. #endif
  19. #define STRINGIFY(x) #x
  20. #define TOSTRING(x) STRINGIFY(x)
  21. #define JUDYHS_INDEX_SIZE_ESTIMATE(key_bytes) (((key_bytes) + sizeof(Word_t) - 1) / sizeof(Word_t) * 4)
  22. #if defined(NETDATA_DEV_MODE) && !defined(NETDATA_INTERNAL_CHECKS)
  23. #define NETDATA_INTERNAL_CHECKS 1
  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 OS_LINUX 1
  35. #define OS_FREEBSD 2
  36. #define OS_MACOS 3
  37. #define MALLOC_ALIGNMENT (sizeof(uintptr_t) * 2)
  38. #define size_t_atomic_count(op, var, size) __atomic_## op ##_fetch(&(var), size, __ATOMIC_RELAXED)
  39. #define size_t_atomic_bytes(op, var, size) __atomic_## op ##_fetch(&(var), ((size) % MALLOC_ALIGNMENT)?((size) + MALLOC_ALIGNMENT - ((size) % MALLOC_ALIGNMENT)):(size), __ATOMIC_RELAXED)
  40. // ----------------------------------------------------------------------------
  41. // system include files for all netdata C programs
  42. /* select the memory allocator, based on autoconf findings */
  43. #if defined(ENABLE_JEMALLOC)
  44. #if defined(HAVE_JEMALLOC_JEMALLOC_H)
  45. #include <jemalloc/jemalloc.h>
  46. #else // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  47. #include <malloc.h>
  48. #endif // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  49. #elif defined(ENABLE_TCMALLOC)
  50. #include <google/tcmalloc.h>
  51. #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  52. #if !(defined(__FreeBSD__) || defined(__APPLE__))
  53. #include <malloc.h>
  54. #endif /* __FreeBSD__ || __APPLE__ */
  55. #endif /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  56. // ----------------------------------------------------------------------------
  57. #if defined(__FreeBSD__)
  58. #include <pthread_np.h>
  59. #define NETDATA_OS_TYPE "freebsd"
  60. #elif defined(__APPLE__)
  61. #define NETDATA_OS_TYPE "macos"
  62. #else
  63. #define NETDATA_OS_TYPE "linux"
  64. #endif /* __FreeBSD__, __APPLE__*/
  65. #include <pthread.h>
  66. #include <errno.h>
  67. #include <stdbool.h>
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <stdarg.h>
  71. #include <stddef.h>
  72. #include <ctype.h>
  73. #include <string.h>
  74. #include <strings.h>
  75. #include <arpa/inet.h>
  76. #include <netinet/tcp.h>
  77. #include <sys/ioctl.h>
  78. #include <libgen.h>
  79. #include <dirent.h>
  80. #include <fcntl.h>
  81. #include <getopt.h>
  82. #include <grp.h>
  83. #include <pwd.h>
  84. #include <limits.h>
  85. #include <locale.h>
  86. #include <net/if.h>
  87. #include <poll.h>
  88. #include <signal.h>
  89. #include <syslog.h>
  90. #include <sys/mman.h>
  91. #include <sys/resource.h>
  92. #include <sys/socket.h>
  93. #include <sys/syscall.h>
  94. #include <sys/time.h>
  95. #include <sys/types.h>
  96. #include <sys/wait.h>
  97. #include <sys/un.h>
  98. #include <time.h>
  99. #include <unistd.h>
  100. #include <uuid/uuid.h>
  101. #include <spawn.h>
  102. #include <uv.h>
  103. #include <assert.h>
  104. // CentOS 7 has older version that doesn't define this
  105. // same goes for MacOS
  106. #ifndef UUID_STR_LEN
  107. #define UUID_STR_LEN (37)
  108. #endif
  109. #ifdef HAVE_NETINET_IN_H
  110. #include <netinet/in.h>
  111. #endif
  112. #ifdef HAVE_RESOLV_H
  113. #include <resolv.h>
  114. #endif
  115. #ifdef HAVE_NETDB_H
  116. #include <netdb.h>
  117. #endif
  118. #ifdef HAVE_SYS_PRCTL_H
  119. #include <sys/prctl.h>
  120. #endif
  121. #ifdef HAVE_SYS_STAT_H
  122. #include <sys/stat.h>
  123. #endif
  124. #ifdef HAVE_SYS_VFS_H
  125. #include <sys/vfs.h>
  126. #endif
  127. #ifdef HAVE_SYS_STATFS_H
  128. #include <sys/statfs.h>
  129. #endif
  130. #ifdef HAVE_LINUX_MAGIC_H
  131. #include <linux/magic.h>
  132. #endif
  133. #ifdef HAVE_SYS_MOUNT_H
  134. #include <sys/mount.h>
  135. #endif
  136. #ifdef HAVE_SYS_STATVFS_H
  137. #include <sys/statvfs.h>
  138. #endif
  139. // #1408
  140. #ifdef MAJOR_IN_MKDEV
  141. #include <sys/mkdev.h>
  142. #endif
  143. #ifdef MAJOR_IN_SYSMACROS
  144. #include <sys/sysmacros.h>
  145. #endif
  146. #ifdef STORAGE_WITH_MATH
  147. #include <math.h>
  148. #include <float.h>
  149. #endif
  150. #if defined(HAVE_INTTYPES_H)
  151. #include <inttypes.h>
  152. #elif defined(HAVE_STDINT_H)
  153. #include <stdint.h>
  154. #endif
  155. #include <zlib.h>
  156. #ifdef HAVE_CAPABILITY
  157. #include <sys/capability.h>
  158. #endif
  159. // ----------------------------------------------------------------------------
  160. // netdata common definitions
  161. #ifdef __GNUC__
  162. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  163. #endif // __GNUC__
  164. #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
  165. #define NEVERNULL __attribute__((returns_nonnull))
  166. #else
  167. #define NEVERNULL
  168. #endif
  169. #ifdef HAVE_FUNC_ATTRIBUTE_NOINLINE
  170. #define NOINLINE __attribute__((noinline))
  171. #else
  172. #define NOINLINE
  173. #endif
  174. #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
  175. #define MALLOCLIKE __attribute__((malloc))
  176. #else
  177. #define MALLOCLIKE
  178. #endif
  179. #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
  180. #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
  181. #else
  182. #define PRINTFLIKE(f, a)
  183. #endif
  184. #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
  185. #define NORETURN __attribute__ ((noreturn))
  186. #else
  187. #define NORETURN
  188. #endif
  189. #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
  190. #define WARNUNUSED __attribute__ ((warn_unused_result))
  191. #else
  192. #define WARNUNUSED
  193. #endif
  194. void aral_judy_init(void);
  195. size_t judy_aral_overhead(void);
  196. size_t judy_aral_structures(void);
  197. #define ABS(x) (((x) < 0)? (-(x)) : (x))
  198. #define MIN(a,b) (((a)<(b))?(a):(b))
  199. #define MAX(a,b) (((a)>(b))?(a):(b))
  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 callocz(nmemb, size) callocz_int(nmemb, size, __FILE__, __FUNCTION__, __LINE__)
  443. #define mallocz(size) mallocz_int(size, __FILE__, __FUNCTION__, __LINE__)
  444. #define reallocz(ptr, size) reallocz_int(ptr, size, __FILE__, __FUNCTION__, __LINE__)
  445. #define freez(ptr) freez_int(ptr, __FILE__, __FUNCTION__, __LINE__)
  446. #define mallocz_usable_size(ptr) mallocz_usable_size_int(ptr, __FILE__, __FUNCTION__, __LINE__)
  447. char *strdupz_int(const char *s, const char *file, const char *function, size_t line);
  448. void *callocz_int(size_t nmemb, size_t size, const char *file, const char *function, size_t line);
  449. void *mallocz_int(size_t size, const char *file, const char *function, size_t line);
  450. void *reallocz_int(void *ptr, size_t size, const char *file, const char *function, size_t line);
  451. void freez_int(void *ptr, const char *file, const char *function, size_t line);
  452. size_t mallocz_usable_size_int(void *ptr, const char *file, const char *function, size_t line);
  453. #else // NETDATA_TRACE_ALLOCATIONS
  454. char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
  455. void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
  456. void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
  457. void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
  458. void freez(void *ptr);
  459. #endif // NETDATA_TRACE_ALLOCATIONS
  460. void posix_memfree(void *ptr);
  461. void json_escape_string(char *dst, const char *src, size_t size);
  462. void json_fix_string(char *s);
  463. void *netdata_mmap(const char *filename, size_t size, int flags, int ksm, bool read_only, int *open_fd);
  464. int netdata_munmap(void *ptr, size_t size);
  465. int memory_file_save(const char *filename, void *mem, size_t size);
  466. int fd_is_valid(int fd);
  467. extern struct rlimit rlimit_nofile;
  468. extern int enable_ksm;
  469. char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
  470. int verify_netdata_host_prefix();
  471. int recursively_delete_dir(const char *path, const char *reason);
  472. extern volatile sig_atomic_t netdata_exit;
  473. extern const char *program_version;
  474. char *strdupz_path_subpath(const char *path, const char *subpath);
  475. int path_is_dir(const char *path, const char *subpath);
  476. int path_is_file(const char *path, const char *subpath);
  477. void recursive_config_double_dir_load(
  478. const char *user_path
  479. , const char *stock_path
  480. , const char *subpath
  481. , int (*callback)(const char *filename, void *data)
  482. , void *data
  483. , size_t depth
  484. );
  485. char *read_by_filename(char *filename, long *file_size);
  486. char *find_and_replace(const char *src, const char *find, const char *replace, const char *where);
  487. /* fix for alpine linux */
  488. #ifndef RUSAGE_THREAD
  489. #ifdef RUSAGE_CHILDREN
  490. #define RUSAGE_THREAD RUSAGE_CHILDREN
  491. #endif
  492. #endif
  493. #define BITS_IN_A_KILOBIT 1000
  494. #define KILOBITS_IN_A_MEGABIT 1000
  495. /* misc. */
  496. #define UNUSED(x) (void)(x)
  497. #ifdef __GNUC__
  498. #define UNUSED_FUNCTION(x) __attribute__((unused)) UNUSED_##x
  499. #else
  500. #define UNUSED_FUNCTION(x) UNUSED_##x
  501. #endif
  502. #define error_report(x, args...) do { errno = 0; netdata_log_error(x, ##args); } while(0)
  503. // Taken from linux kernel
  504. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  505. #ifdef ENV32BIT
  506. typedef struct bitmapX {
  507. uint32_t bits;
  508. uint32_t data[];
  509. } BITMAPX;
  510. typedef struct bitmap256 {
  511. uint32_t bits;
  512. uint32_t data[256 / 32];
  513. } BITMAP256;
  514. typedef struct bitmap1024 {
  515. uint32_t bits;
  516. uint32_t data[1024 / 32];
  517. } BITMAP1024;
  518. static inline BITMAPX *bitmapX_create(uint32_t bits) {
  519. BITMAPX *bmp = (BITMAPX *)callocz(1, sizeof(BITMAPX) + sizeof(uint32_t) * ((bits + 31) / 32));
  520. uint32_t *p = (uint32_t *)&bmp->bits;
  521. *p = bits;
  522. return bmp;
  523. }
  524. #define bitmapX_get_bit(ptr, idx) ((ptr)->data[(idx) >> 5] & (1U << ((idx) & 31)))
  525. #define bitmapX_set_bit(ptr, idx, value) do { \
  526. register uint32_t _bitmask = 1U << ((idx) & 31); \
  527. if (value) \
  528. (ptr)->data[(idx) >> 5] |= _bitmask; \
  529. else \
  530. (ptr)->data[(idx) >> 5] &= ~_bitmask; \
  531. } while(0)
  532. #else // 64bit version of bitmaps
  533. typedef struct bitmapX {
  534. uint32_t bits;
  535. uint64_t data[];
  536. } BITMAPX;
  537. typedef struct bitmap256 {
  538. uint32_t bits;
  539. uint64_t data[256 / 64];
  540. } BITMAP256;
  541. typedef struct bitmap1024 {
  542. uint32_t bits;
  543. uint64_t data[1024 / 64];
  544. } BITMAP1024;
  545. static inline BITMAPX *bitmapX_create(uint32_t bits) {
  546. BITMAPX *bmp = (BITMAPX *)callocz(1, sizeof(BITMAPX) + sizeof(uint64_t) * ((bits + 63) / 64));
  547. bmp->bits = bits;
  548. return bmp;
  549. }
  550. #define bitmapX_get_bit(ptr, idx) ((ptr)->data[(idx) >> 6] & (1ULL << ((idx) & 63)))
  551. #define bitmapX_set_bit(ptr, idx, value) do { \
  552. register uint64_t _bitmask = 1ULL << ((idx) & 63); \
  553. if (value) \
  554. (ptr)->data[(idx) >> 6] |= _bitmask; \
  555. else \
  556. (ptr)->data[(idx) >> 6] &= ~_bitmask; \
  557. } while(0)
  558. #endif // 64bit version of bitmaps
  559. #define BITMAPX_INITIALIZER(wanted_bits) { .bits = (wanted_bits), .data = {0} }
  560. #define BITMAP256_INITIALIZER (BITMAP256)BITMAPX_INITIALIZER(256)
  561. #define BITMAP1024_INITIALIZER (BITMAP1024)BITMAPX_INITIALIZER(1024)
  562. #define bitmap256_get_bit(ptr, idx) bitmapX_get_bit((BITMAPX *)ptr, idx)
  563. #define bitmap256_set_bit(ptr, idx, value) bitmapX_set_bit((BITMAPX *)ptr, idx, value)
  564. #define bitmap1024_get_bit(ptr, idx) bitmapX_get_bit((BITMAPX *)ptr, idx)
  565. #define bitmap1024_set_bit(ptr, idx, value) bitmapX_set_bit((BITMAPX *)ptr, idx, value)
  566. #define COMPRESSION_MAX_MSG_SIZE 0x4000
  567. #define PLUGINSD_LINE_MAX (COMPRESSION_MAX_MSG_SIZE - 1024)
  568. int pluginsd_isspace(char c);
  569. int config_isspace(char c);
  570. int group_by_label_isspace(char c);
  571. extern bool isspace_map_pluginsd[256];
  572. extern bool isspace_map_config[256];
  573. extern bool isspace_map_group_by_label[256];
  574. static inline size_t quoted_strings_splitter(char *str, char **words, size_t max_words, bool *isspace_map) {
  575. char *s = str, quote = 0;
  576. size_t i = 0;
  577. // skip all white space
  578. while (unlikely(isspace_map[(uint8_t)*s]))
  579. s++;
  580. if(unlikely(!*s)) {
  581. words[i] = NULL;
  582. return 0;
  583. }
  584. // check for quote
  585. if (unlikely(*s == '\'' || *s == '"')) {
  586. quote = *s; // remember the quote
  587. s++; // skip the quote
  588. }
  589. // store the first word
  590. words[i++] = s;
  591. // while we have something
  592. while (likely(*s)) {
  593. // if it is an escape
  594. if (unlikely(*s == '\\' && s[1])) {
  595. s += 2;
  596. continue;
  597. }
  598. // if it is a quote
  599. else if (unlikely(*s == quote)) {
  600. quote = 0;
  601. *s = ' ';
  602. continue;
  603. }
  604. // if it is a space
  605. else if (unlikely(quote == 0 && isspace_map[(uint8_t)*s])) {
  606. // terminate the word
  607. *s++ = '\0';
  608. // skip all white space
  609. while (likely(isspace_map[(uint8_t)*s]))
  610. s++;
  611. // check for a quote
  612. if (unlikely(*s == '\'' || *s == '"')) {
  613. quote = *s; // remember the quote
  614. s++; // skip the quote
  615. }
  616. // if we reached the end, stop
  617. if (unlikely(!*s))
  618. break;
  619. // store the next word
  620. if (likely(i < max_words))
  621. words[i++] = s;
  622. else
  623. break;
  624. }
  625. // anything else
  626. else
  627. s++;
  628. }
  629. if (likely(i < max_words))
  630. words[i] = NULL;
  631. return i;
  632. }
  633. #define quoted_strings_splitter_query_group_by_label(str, words, max_words) \
  634. quoted_strings_splitter(str, words, max_words, isspace_map_group_by_label)
  635. #define quoted_strings_splitter_config(str, words, max_words) \
  636. quoted_strings_splitter(str, words, max_words, isspace_map_config)
  637. #define quoted_strings_splitter_pluginsd(str, words, max_words) \
  638. quoted_strings_splitter(str, words, max_words, isspace_map_pluginsd)
  639. static inline char *get_word(char **words, size_t num_words, size_t index) {
  640. if (unlikely(index >= num_words))
  641. return NULL;
  642. return words[index];
  643. }
  644. bool run_command_and_copy_output_to_stdout(const char *command, int max_line_length);
  645. typedef enum {
  646. OPEN_FD_ACTION_CLOSE,
  647. OPEN_FD_ACTION_FD_CLOEXEC
  648. } OPEN_FD_ACTION;
  649. typedef enum {
  650. OPEN_FD_EXCLUDE_STDIN = 0x01,
  651. OPEN_FD_EXCLUDE_STDOUT = 0x02,
  652. OPEN_FD_EXCLUDE_STDERR = 0x04
  653. } OPEN_FD_EXCLUDE;
  654. void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds);
  655. void netdata_cleanup_and_exit(int ret) NORETURN;
  656. void send_statistics(const char *action, const char *action_result, const char *action_data);
  657. extern char *netdata_configured_host_prefix;
  658. #include "libjudy/src/Judy.h"
  659. #include "july/july.h"
  660. #include "os.h"
  661. #include "threads/threads.h"
  662. #include "buffer/buffer.h"
  663. #include "locks/locks.h"
  664. #include "circular_buffer/circular_buffer.h"
  665. #include "avl/avl.h"
  666. #include "inlined.h"
  667. #include "clocks/clocks.h"
  668. #include "completion/completion.h"
  669. #include "popen/popen.h"
  670. #include "simple_pattern/simple_pattern.h"
  671. #ifdef ENABLE_HTTPS
  672. # include "socket/security.h"
  673. #endif
  674. #include "socket/socket.h"
  675. #include "config/appconfig.h"
  676. #include "log/log.h"
  677. #include "procfile/procfile.h"
  678. #include "string/string.h"
  679. #include "dictionary/dictionary.h"
  680. #if defined(HAVE_LIBBPF) && !defined(__cplusplus)
  681. #include "ebpf/ebpf.h"
  682. #endif
  683. #include "eval/eval.h"
  684. #include "statistical/statistical.h"
  685. #include "adaptive_resortable_list/adaptive_resortable_list.h"
  686. #include "url/url.h"
  687. #include "json/json.h"
  688. #include "health/health.h"
  689. #include "string/utf8.h"
  690. #include "libnetdata/aral/aral.h"
  691. #include "onewayalloc/onewayalloc.h"
  692. #include "worker_utilization/worker_utilization.h"
  693. #include "yaml.h"
  694. #include "http/http_defs.h"
  695. #include "gorilla/gorilla.h"
  696. // BEWARE: this exists in alarm-notify.sh
  697. #define DEFAULT_CLOUD_BASE_URL "https://app.netdata.cloud"
  698. #define RRD_STORAGE_TIERS 5
  699. static inline size_t struct_natural_alignment(size_t size) __attribute__((const));
  700. #define STRUCT_NATURAL_ALIGNMENT (sizeof(uintptr_t) * 2)
  701. static inline size_t struct_natural_alignment(size_t size) {
  702. if(unlikely(size % STRUCT_NATURAL_ALIGNMENT))
  703. size = size + STRUCT_NATURAL_ALIGNMENT - (size % STRUCT_NATURAL_ALIGNMENT);
  704. return size;
  705. }
  706. #ifdef NETDATA_TRACE_ALLOCATIONS
  707. struct malloc_trace {
  708. avl_t avl;
  709. const char *function;
  710. const char *file;
  711. size_t line;
  712. size_t malloc_calls;
  713. size_t calloc_calls;
  714. size_t realloc_calls;
  715. size_t strdup_calls;
  716. size_t free_calls;
  717. size_t mmap_calls;
  718. size_t munmap_calls;
  719. size_t allocations;
  720. size_t bytes;
  721. struct rrddim *rd_bytes;
  722. struct rrddim *rd_allocations;
  723. struct rrddim *rd_avg_alloc;
  724. struct rrddim *rd_ops;
  725. };
  726. #endif // NETDATA_TRACE_ALLOCATIONS
  727. static inline PPvoid_t JudyLFirstThenNext(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  728. if(unlikely(*first)) {
  729. *first = false;
  730. return JudyLFirst(PArray, PIndex, PJE0);
  731. }
  732. return JudyLNext(PArray, PIndex, PJE0);
  733. }
  734. static inline PPvoid_t JudyLLastThenPrev(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  735. if(unlikely(*first)) {
  736. *first = false;
  737. return JudyLLast(PArray, PIndex, PJE0);
  738. }
  739. return JudyLPrev(PArray, PIndex, PJE0);
  740. }
  741. typedef enum {
  742. TIMING_STEP_INTERNAL = 0,
  743. TIMING_STEP_BEGIN2_PREPARE,
  744. TIMING_STEP_BEGIN2_FIND_CHART,
  745. TIMING_STEP_BEGIN2_PARSE,
  746. TIMING_STEP_BEGIN2_ML,
  747. TIMING_STEP_BEGIN2_PROPAGATE,
  748. TIMING_STEP_BEGIN2_STORE,
  749. TIMING_STEP_SET2_PREPARE,
  750. TIMING_STEP_SET2_LOOKUP_DIMENSION,
  751. TIMING_STEP_SET2_PARSE,
  752. TIMING_STEP_SET2_ML,
  753. TIMING_STEP_SET2_PROPAGATE,
  754. TIMING_STEP_RRDSET_STORE_METRIC,
  755. TIMING_STEP_DBENGINE_FIRST_CHECK,
  756. TIMING_STEP_DBENGINE_CHECK_DATA,
  757. TIMING_STEP_DBENGINE_PACK,
  758. TIMING_STEP_DBENGINE_PAGE_FIN,
  759. TIMING_STEP_DBENGINE_MRG_UPDATE,
  760. TIMING_STEP_DBENGINE_PAGE_ALLOC,
  761. TIMING_STEP_DBENGINE_CREATE_NEW_PAGE,
  762. TIMING_STEP_DBENGINE_FLUSH_PAGE,
  763. TIMING_STEP_SET2_STORE,
  764. TIMING_STEP_END2_PREPARE,
  765. TIMING_STEP_END2_PUSH_V1,
  766. TIMING_STEP_END2_ML,
  767. TIMING_STEP_END2_RRDSET,
  768. TIMING_STEP_END2_PROPAGATE,
  769. TIMING_STEP_END2_STORE,
  770. TIMING_STEP_FREEIPMI_CTX_CREATE,
  771. TIMING_STEP_FREEIPMI_DSR_CACHE_DIR,
  772. TIMING_STEP_FREEIPMI_SENSOR_CONFIG_FILE,
  773. TIMING_STEP_FREEIPMI_SENSOR_READINGS_BY_X,
  774. TIMING_STEP_FREEIPMI_READ_record_id,
  775. TIMING_STEP_FREEIPMI_READ_sensor_number,
  776. TIMING_STEP_FREEIPMI_READ_sensor_type,
  777. TIMING_STEP_FREEIPMI_READ_sensor_name,
  778. TIMING_STEP_FREEIPMI_READ_sensor_state,
  779. TIMING_STEP_FREEIPMI_READ_sensor_units,
  780. TIMING_STEP_FREEIPMI_READ_sensor_bitmask_type,
  781. TIMING_STEP_FREEIPMI_READ_sensor_bitmask,
  782. TIMING_STEP_FREEIPMI_READ_sensor_bitmask_strings,
  783. TIMING_STEP_FREEIPMI_READ_sensor_reading_type,
  784. TIMING_STEP_FREEIPMI_READ_sensor_reading,
  785. TIMING_STEP_FREEIPMI_READ_event_reading_type_code,
  786. TIMING_STEP_FREEIPMI_READ_record_type,
  787. TIMING_STEP_FREEIPMI_READ_record_type_class,
  788. TIMING_STEP_FREEIPMI_READ_sel_state,
  789. TIMING_STEP_FREEIPMI_READ_event_direction,
  790. TIMING_STEP_FREEIPMI_READ_event_type_code,
  791. TIMING_STEP_FREEIPMI_READ_event_offset_type,
  792. TIMING_STEP_FREEIPMI_READ_event_offset,
  793. TIMING_STEP_FREEIPMI_READ_event_offset_string,
  794. TIMING_STEP_FREEIPMI_READ_manufacturer_id,
  795. // terminator
  796. TIMING_STEP_MAX,
  797. } TIMING_STEP;
  798. typedef enum {
  799. TIMING_ACTION_INIT,
  800. TIMING_ACTION_STEP,
  801. TIMING_ACTION_FINISH,
  802. } TIMING_ACTION;
  803. #ifdef NETDATA_TIMING_REPORT
  804. #define timing_init() timing_action(TIMING_ACTION_INIT, TIMING_STEP_INTERNAL)
  805. #define timing_step(step) timing_action(TIMING_ACTION_STEP, step)
  806. #define timing_report() timing_action(TIMING_ACTION_FINISH, TIMING_STEP_INTERNAL)
  807. #else
  808. #define timing_init() debug_dummy()
  809. #define timing_step(step) debug_dummy()
  810. #define timing_report() debug_dummy()
  811. #endif
  812. void timing_action(TIMING_ACTION action, TIMING_STEP step);
  813. int hash256_string(const unsigned char *string, size_t size, char *hash);
  814. # ifdef __cplusplus
  815. }
  816. # endif
  817. #endif // NETDATA_LIB_H