libnetdata.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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(const 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. #define XXH_INLINE_ALL
  659. #include "xxhash.h"
  660. #include "libjudy/src/Judy.h"
  661. #include "july/july.h"
  662. #include "os.h"
  663. #include "threads/threads.h"
  664. #include "buffer/buffer.h"
  665. #include "locks/locks.h"
  666. #include "circular_buffer/circular_buffer.h"
  667. #include "avl/avl.h"
  668. #include "inlined.h"
  669. #include "clocks/clocks.h"
  670. #include "completion/completion.h"
  671. #include "popen/popen.h"
  672. #include "simple_pattern/simple_pattern.h"
  673. #ifdef ENABLE_HTTPS
  674. # include "socket/security.h"
  675. #endif
  676. #include "socket/socket.h"
  677. #include "config/appconfig.h"
  678. #include "log/log.h"
  679. #include "procfile/procfile.h"
  680. #include "string/string.h"
  681. #include "dictionary/dictionary.h"
  682. #if defined(HAVE_LIBBPF) && !defined(__cplusplus)
  683. #include "ebpf/ebpf.h"
  684. #endif
  685. #include "eval/eval.h"
  686. #include "statistical/statistical.h"
  687. #include "adaptive_resortable_list/adaptive_resortable_list.h"
  688. #include "url/url.h"
  689. #include "json/json.h"
  690. #include "health/health.h"
  691. #include "string/utf8.h"
  692. #include "libnetdata/aral/aral.h"
  693. #include "onewayalloc/onewayalloc.h"
  694. #include "worker_utilization/worker_utilization.h"
  695. #include "yaml.h"
  696. #include "http/http_defs.h"
  697. #include "gorilla/gorilla.h"
  698. #include "facets/facets.h"
  699. #include "dyn_conf/dyn_conf.h"
  700. #include "functions_evloop/functions_evloop.h"
  701. // BEWARE: this exists in alarm-notify.sh
  702. #define DEFAULT_CLOUD_BASE_URL "https://app.netdata.cloud"
  703. #define RRD_STORAGE_TIERS 5
  704. static inline size_t struct_natural_alignment(size_t size) __attribute__((const));
  705. #define STRUCT_NATURAL_ALIGNMENT (sizeof(uintptr_t) * 2)
  706. static inline size_t struct_natural_alignment(size_t size) {
  707. if(unlikely(size % STRUCT_NATURAL_ALIGNMENT))
  708. size = size + STRUCT_NATURAL_ALIGNMENT - (size % STRUCT_NATURAL_ALIGNMENT);
  709. return size;
  710. }
  711. #ifdef NETDATA_TRACE_ALLOCATIONS
  712. struct malloc_trace {
  713. avl_t avl;
  714. const char *function;
  715. const char *file;
  716. size_t line;
  717. size_t malloc_calls;
  718. size_t calloc_calls;
  719. size_t realloc_calls;
  720. size_t strdup_calls;
  721. size_t free_calls;
  722. size_t mmap_calls;
  723. size_t munmap_calls;
  724. size_t allocations;
  725. size_t bytes;
  726. struct rrddim *rd_bytes;
  727. struct rrddim *rd_allocations;
  728. struct rrddim *rd_avg_alloc;
  729. struct rrddim *rd_ops;
  730. };
  731. #endif // NETDATA_TRACE_ALLOCATIONS
  732. static inline PPvoid_t JudyLFirstThenNext(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  733. if(unlikely(*first)) {
  734. *first = false;
  735. return JudyLFirst(PArray, PIndex, PJE0);
  736. }
  737. return JudyLNext(PArray, PIndex, PJE0);
  738. }
  739. static inline PPvoid_t JudyLLastThenPrev(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  740. if(unlikely(*first)) {
  741. *first = false;
  742. return JudyLLast(PArray, PIndex, PJE0);
  743. }
  744. return JudyLPrev(PArray, PIndex, PJE0);
  745. }
  746. typedef enum {
  747. TIMING_STEP_INTERNAL = 0,
  748. TIMING_STEP_BEGIN2_PREPARE,
  749. TIMING_STEP_BEGIN2_FIND_CHART,
  750. TIMING_STEP_BEGIN2_PARSE,
  751. TIMING_STEP_BEGIN2_ML,
  752. TIMING_STEP_BEGIN2_PROPAGATE,
  753. TIMING_STEP_BEGIN2_STORE,
  754. TIMING_STEP_SET2_PREPARE,
  755. TIMING_STEP_SET2_LOOKUP_DIMENSION,
  756. TIMING_STEP_SET2_PARSE,
  757. TIMING_STEP_SET2_ML,
  758. TIMING_STEP_SET2_PROPAGATE,
  759. TIMING_STEP_RRDSET_STORE_METRIC,
  760. TIMING_STEP_DBENGINE_FIRST_CHECK,
  761. TIMING_STEP_DBENGINE_CHECK_DATA,
  762. TIMING_STEP_DBENGINE_PACK,
  763. TIMING_STEP_DBENGINE_PAGE_FIN,
  764. TIMING_STEP_DBENGINE_MRG_UPDATE,
  765. TIMING_STEP_DBENGINE_PAGE_ALLOC,
  766. TIMING_STEP_DBENGINE_CREATE_NEW_PAGE,
  767. TIMING_STEP_DBENGINE_FLUSH_PAGE,
  768. TIMING_STEP_SET2_STORE,
  769. TIMING_STEP_END2_PREPARE,
  770. TIMING_STEP_END2_PUSH_V1,
  771. TIMING_STEP_END2_ML,
  772. TIMING_STEP_END2_RRDSET,
  773. TIMING_STEP_END2_PROPAGATE,
  774. TIMING_STEP_END2_STORE,
  775. TIMING_STEP_FREEIPMI_CTX_CREATE,
  776. TIMING_STEP_FREEIPMI_DSR_CACHE_DIR,
  777. TIMING_STEP_FREEIPMI_SENSOR_CONFIG_FILE,
  778. TIMING_STEP_FREEIPMI_SENSOR_READINGS_BY_X,
  779. TIMING_STEP_FREEIPMI_READ_record_id,
  780. TIMING_STEP_FREEIPMI_READ_sensor_number,
  781. TIMING_STEP_FREEIPMI_READ_sensor_type,
  782. TIMING_STEP_FREEIPMI_READ_sensor_name,
  783. TIMING_STEP_FREEIPMI_READ_sensor_state,
  784. TIMING_STEP_FREEIPMI_READ_sensor_units,
  785. TIMING_STEP_FREEIPMI_READ_sensor_bitmask_type,
  786. TIMING_STEP_FREEIPMI_READ_sensor_bitmask,
  787. TIMING_STEP_FREEIPMI_READ_sensor_bitmask_strings,
  788. TIMING_STEP_FREEIPMI_READ_sensor_reading_type,
  789. TIMING_STEP_FREEIPMI_READ_sensor_reading,
  790. TIMING_STEP_FREEIPMI_READ_event_reading_type_code,
  791. TIMING_STEP_FREEIPMI_READ_record_type,
  792. TIMING_STEP_FREEIPMI_READ_record_type_class,
  793. TIMING_STEP_FREEIPMI_READ_sel_state,
  794. TIMING_STEP_FREEIPMI_READ_event_direction,
  795. TIMING_STEP_FREEIPMI_READ_event_type_code,
  796. TIMING_STEP_FREEIPMI_READ_event_offset_type,
  797. TIMING_STEP_FREEIPMI_READ_event_offset,
  798. TIMING_STEP_FREEIPMI_READ_event_offset_string,
  799. TIMING_STEP_FREEIPMI_READ_manufacturer_id,
  800. // terminator
  801. TIMING_STEP_MAX,
  802. } TIMING_STEP;
  803. typedef enum {
  804. TIMING_ACTION_INIT,
  805. TIMING_ACTION_STEP,
  806. TIMING_ACTION_FINISH,
  807. } TIMING_ACTION;
  808. #ifdef NETDATA_TIMING_REPORT
  809. #define timing_init() timing_action(TIMING_ACTION_INIT, TIMING_STEP_INTERNAL)
  810. #define timing_step(step) timing_action(TIMING_ACTION_STEP, step)
  811. #define timing_report() timing_action(TIMING_ACTION_FINISH, TIMING_STEP_INTERNAL)
  812. #else
  813. #define timing_init() debug_dummy()
  814. #define timing_step(step) debug_dummy()
  815. #define timing_report() debug_dummy()
  816. #endif
  817. void timing_action(TIMING_ACTION action, TIMING_STEP step);
  818. int hash256_string(const unsigned char *string, size_t size, char *hash);
  819. extern bool unittest_running;
  820. #define API_RELATIVE_TIME_MAX (3 * 365 * 86400)
  821. bool rrdr_relative_window_to_absolute(time_t *after, time_t *before, time_t now);
  822. bool rrdr_relative_window_to_absolute_query(time_t *after, time_t *before, time_t *now_ptr, bool unittest_running);
  823. int netdata_base64_decode(const char *encoded, char *decoded, size_t decoded_size);
  824. # ifdef __cplusplus
  825. }
  826. # endif
  827. #endif // NETDATA_LIB_H