libnetdata.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_LIB_H
  3. #define NETDATA_LIB_H 1
  4. # ifdef __cplusplus
  5. extern "C" {
  6. # endif
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #define JUDYHS_INDEX_SIZE_ESTIMATE(key_bytes) (((key_bytes) + sizeof(Word_t) - 1) / sizeof(Word_t) * 4)
  11. #if defined(NETDATA_DEV_MODE) && !defined(NETDATA_INTERNAL_CHECKS)
  12. #define NETDATA_INTERNAL_CHECKS 1
  13. #endif
  14. #if SIZEOF_VOID_P == 4
  15. #define ENV32BIT 1
  16. #else
  17. #define ENV64BIT 1
  18. #endif
  19. // NETDATA_TRACE_ALLOCATIONS does not work under musl libc, so don't enable it
  20. //#if defined(NETDATA_INTERNAL_CHECKS) && !defined(NETDATA_TRACE_ALLOCATIONS)
  21. //#define NETDATA_TRACE_ALLOCATIONS 1
  22. //#endif
  23. #define OS_LINUX 1
  24. #define OS_FREEBSD 2
  25. #define OS_MACOS 3
  26. // ----------------------------------------------------------------------------
  27. // system include files for all netdata C programs
  28. /* select the memory allocator, based on autoconf findings */
  29. #if defined(ENABLE_JEMALLOC)
  30. #if defined(HAVE_JEMALLOC_JEMALLOC_H)
  31. #include <jemalloc/jemalloc.h>
  32. #else // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  33. #include <malloc.h>
  34. #endif // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  35. #elif defined(ENABLE_TCMALLOC)
  36. #include <google/tcmalloc.h>
  37. #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  38. #if !(defined(__FreeBSD__) || defined(__APPLE__))
  39. #include <malloc.h>
  40. #endif /* __FreeBSD__ || __APPLE__ */
  41. #endif /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  42. // ----------------------------------------------------------------------------
  43. #if defined(__FreeBSD__)
  44. #include <pthread_np.h>
  45. #define NETDATA_OS_TYPE "freebsd"
  46. #elif defined(__APPLE__)
  47. #define NETDATA_OS_TYPE "macos"
  48. #else
  49. #define NETDATA_OS_TYPE "linux"
  50. #endif /* __FreeBSD__, __APPLE__*/
  51. #include <pthread.h>
  52. #include <errno.h>
  53. #include <stdbool.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <stdarg.h>
  57. #include <stddef.h>
  58. #include <ctype.h>
  59. #include <string.h>
  60. #include <strings.h>
  61. #include <arpa/inet.h>
  62. #include <netinet/tcp.h>
  63. #include <sys/ioctl.h>
  64. #include <libgen.h>
  65. #include <dirent.h>
  66. #include <fcntl.h>
  67. #include <getopt.h>
  68. #include <grp.h>
  69. #include <pwd.h>
  70. #include <limits.h>
  71. #include <locale.h>
  72. #include <net/if.h>
  73. #include <poll.h>
  74. #include <signal.h>
  75. #include <syslog.h>
  76. #include <sys/mman.h>
  77. #include <sys/resource.h>
  78. #include <sys/socket.h>
  79. #include <sys/syscall.h>
  80. #include <sys/time.h>
  81. #include <sys/types.h>
  82. #include <sys/wait.h>
  83. #include <sys/un.h>
  84. #include <time.h>
  85. #include <unistd.h>
  86. #include <uuid/uuid.h>
  87. #include <spawn.h>
  88. #include <uv.h>
  89. #include <assert.h>
  90. // CentOS 7 has older version that doesn't define this
  91. // same goes for MacOS
  92. #ifndef UUID_STR_LEN
  93. #define UUID_STR_LEN (37)
  94. #endif
  95. #ifdef HAVE_NETINET_IN_H
  96. #include <netinet/in.h>
  97. #endif
  98. #ifdef HAVE_RESOLV_H
  99. #include <resolv.h>
  100. #endif
  101. #ifdef HAVE_NETDB_H
  102. #include <netdb.h>
  103. #endif
  104. #ifdef HAVE_SYS_PRCTL_H
  105. #include <sys/prctl.h>
  106. #endif
  107. #ifdef HAVE_SYS_STAT_H
  108. #include <sys/stat.h>
  109. #endif
  110. #ifdef HAVE_SYS_VFS_H
  111. #include <sys/vfs.h>
  112. #endif
  113. #ifdef HAVE_SYS_STATFS_H
  114. #include <sys/statfs.h>
  115. #endif
  116. #ifdef HAVE_LINUX_MAGIC_H
  117. #include <linux/magic.h>
  118. #endif
  119. #ifdef HAVE_SYS_MOUNT_H
  120. #include <sys/mount.h>
  121. #endif
  122. #ifdef HAVE_SYS_STATVFS_H
  123. #include <sys/statvfs.h>
  124. #endif
  125. // #1408
  126. #ifdef MAJOR_IN_MKDEV
  127. #include <sys/mkdev.h>
  128. #endif
  129. #ifdef MAJOR_IN_SYSMACROS
  130. #include <sys/sysmacros.h>
  131. #endif
  132. #ifdef STORAGE_WITH_MATH
  133. #include <math.h>
  134. #include <float.h>
  135. #endif
  136. #if defined(HAVE_INTTYPES_H)
  137. #include <inttypes.h>
  138. #elif defined(HAVE_STDINT_H)
  139. #include <stdint.h>
  140. #endif
  141. #ifdef NETDATA_WITH_ZLIB
  142. #include <zlib.h>
  143. #endif
  144. #ifdef HAVE_CAPABILITY
  145. #include <sys/capability.h>
  146. #endif
  147. // ----------------------------------------------------------------------------
  148. // netdata common definitions
  149. #ifdef __GNUC__
  150. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  151. #endif // __GNUC__
  152. #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
  153. #define NEVERNULL __attribute__((returns_nonnull))
  154. #else
  155. #define NEVERNULL
  156. #endif
  157. #ifdef HAVE_FUNC_ATTRIBUTE_NOINLINE
  158. #define NOINLINE __attribute__((noinline))
  159. #else
  160. #define NOINLINE
  161. #endif
  162. #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
  163. #define MALLOCLIKE __attribute__((malloc))
  164. #else
  165. #define MALLOCLIKE
  166. #endif
  167. #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
  168. #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
  169. #else
  170. #define PRINTFLIKE(f, a)
  171. #endif
  172. #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
  173. #define NORETURN __attribute__ ((noreturn))
  174. #else
  175. #define NORETURN
  176. #endif
  177. #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
  178. #define WARNUNUSED __attribute__ ((warn_unused_result))
  179. #else
  180. #define WARNUNUSED
  181. #endif
  182. void aral_judy_init(void);
  183. size_t judy_aral_overhead(void);
  184. size_t judy_aral_structures(void);
  185. #define ABS(x) (((x) < 0)? (-(x)) : (x))
  186. #define MIN(a,b) (((a)<(b))?(a):(b))
  187. #define MAX(a,b) (((a)>(b))?(a):(b))
  188. #define GUID_LEN 36
  189. // ---------------------------------------------------------------------------------------------
  190. // double linked list management
  191. // inspired by https://github.com/troydhanson/uthash/blob/master/src/utlist.h
  192. #define DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(head, item, prev, next) \
  193. do { \
  194. (item)->next = (head); \
  195. \
  196. if(likely(head)) { \
  197. (item)->prev = (head)->prev; \
  198. (head)->prev = (item); \
  199. } \
  200. else \
  201. (item)->prev = (item); \
  202. \
  203. (head) = (item); \
  204. } while (0)
  205. #define DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(head, item, prev, next) \
  206. do { \
  207. if(likely(head)) { \
  208. (item)->prev = (head)->prev; \
  209. (head)->prev->next = (item); \
  210. (head)->prev = (item); \
  211. (item)->next = NULL; \
  212. } \
  213. else { \
  214. (head) = (item); \
  215. (head)->prev = (head); \
  216. (head)->next = NULL; \
  217. } \
  218. \
  219. } while (0)
  220. #define DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(head, item, prev, next) \
  221. do { \
  222. fatal_assert((head) != NULL); \
  223. fatal_assert((item)->prev != NULL); \
  224. \
  225. if((item)->prev == (item)) \
  226. /* it is the only item in the list */ \
  227. (head) = NULL; \
  228. \
  229. else if((item) == (head)) { \
  230. /* it is the first item */ \
  231. fatal_assert((item)->next != NULL); \
  232. (item)->next->prev = (item)->prev; \
  233. (head) = (item)->next; \
  234. } \
  235. else { \
  236. /* it is any other item */ \
  237. (item)->prev->next = (item)->next; \
  238. \
  239. if ((item)->next) \
  240. (item)->next->prev = (item)->prev; \
  241. else \
  242. (head)->prev = (item)->prev; \
  243. } \
  244. \
  245. (item)->next = NULL; \
  246. (item)->prev = NULL; \
  247. } while (0)
  248. #define DOUBLE_LINKED_LIST_INSERT_ITEM_BEFORE_UNSAFE(head, existing, item, prev, next) \
  249. do { \
  250. if (existing) { \
  251. fatal_assert((head) != NULL); \
  252. fatal_assert((item) != NULL); \
  253. \
  254. (item)->next = (existing); \
  255. (item)->prev = (existing)->prev; \
  256. (existing)->prev = (item); \
  257. \
  258. if ((head) == (existing)) \
  259. (head) = (item); \
  260. else \
  261. (item)->prev->next = (item); \
  262. \
  263. } \
  264. else \
  265. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(head, item, prev, next); \
  266. \
  267. } while (0)
  268. #define DOUBLE_LINKED_LIST_INSERT_ITEM_AFTER_UNSAFE(head, existing, item, prev, next) \
  269. do { \
  270. if (existing) { \
  271. fatal_assert((head) != NULL); \
  272. fatal_assert((item) != NULL); \
  273. \
  274. (item)->next = (existing)->next; \
  275. (item)->prev = (existing); \
  276. (existing)->next = (item); \
  277. \
  278. if ((item)->next) \
  279. (item)->next->prev = (item); \
  280. else \
  281. (head)->prev = (item); \
  282. } \
  283. else \
  284. DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(head, item, prev, next); \
  285. \
  286. } while (0)
  287. #define DOUBLE_LINKED_LIST_APPEND_LIST_UNSAFE(head, head2, prev, next) \
  288. do { \
  289. if (head2) { \
  290. if (head) { \
  291. __typeof(head2) _head2_last_item = (head2)->prev; \
  292. \
  293. (head2)->prev = (head)->prev; \
  294. (head)->prev->next = (head2); \
  295. \
  296. (head)->prev = _head2_last_item; \
  297. } \
  298. else \
  299. (head) = (head2); \
  300. } \
  301. } while (0)
  302. #define DOUBLE_LINKED_LIST_FOREACH_FORWARD(head, var, prev, next) \
  303. for ((var) = (head); (var) ; (var) = (var)->next)
  304. #define DOUBLE_LINKED_LIST_FOREACH_BACKWARD(head, var, prev, next) \
  305. for ((var) = (head) ? (head)->prev : NULL ; (var) ; (var) = ((var) == (head)) ? NULL : (var)->prev)
  306. // ---------------------------------------------------------------------------------------------
  307. void netdata_fix_chart_id(char *s);
  308. void netdata_fix_chart_name(char *s);
  309. void strreverse(char* begin, char* end);
  310. char *mystrsep(char **ptr, char *s);
  311. char *trim(char *s); // remove leading and trailing spaces; may return NULL
  312. char *trim_all(char *buffer); // like trim(), but also remove duplicate spaces inside the string; may return NULL
  313. int madvise_sequential(void *mem, size_t len);
  314. int madvise_random(void *mem, size_t len);
  315. int madvise_dontfork(void *mem, size_t len);
  316. int madvise_willneed(void *mem, size_t len);
  317. int madvise_dontneed(void *mem, size_t len);
  318. int madvise_dontdump(void *mem, size_t len);
  319. int madvise_mergeable(void *mem, size_t len);
  320. int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
  321. int snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
  322. // memory allocation functions that handle failures
  323. #ifdef NETDATA_TRACE_ALLOCATIONS
  324. int malloc_trace_walkthrough(int (*callback)(void *item, void *data), void *data);
  325. #define strdupz(s) strdupz_int(s, __FILE__, __FUNCTION__, __LINE__)
  326. #define callocz(nmemb, size) callocz_int(nmemb, size, __FILE__, __FUNCTION__, __LINE__)
  327. #define mallocz(size) mallocz_int(size, __FILE__, __FUNCTION__, __LINE__)
  328. #define reallocz(ptr, size) reallocz_int(ptr, size, __FILE__, __FUNCTION__, __LINE__)
  329. #define freez(ptr) freez_int(ptr, __FILE__, __FUNCTION__, __LINE__)
  330. #define mallocz_usable_size(ptr) mallocz_usable_size_int(ptr, __FILE__, __FUNCTION__, __LINE__)
  331. char *strdupz_int(const char *s, const char *file, const char *function, size_t line);
  332. void *callocz_int(size_t nmemb, size_t size, const char *file, const char *function, size_t line);
  333. void *mallocz_int(size_t size, const char *file, const char *function, size_t line);
  334. void *reallocz_int(void *ptr, size_t size, const char *file, const char *function, size_t line);
  335. void freez_int(void *ptr, const char *file, const char *function, size_t line);
  336. size_t mallocz_usable_size_int(void *ptr, const char *file, const char *function, size_t line);
  337. #else // NETDATA_TRACE_ALLOCATIONS
  338. char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
  339. void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
  340. void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
  341. void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
  342. void freez(void *ptr);
  343. #endif // NETDATA_TRACE_ALLOCATIONS
  344. void posix_memfree(void *ptr);
  345. void json_escape_string(char *dst, const char *src, size_t size);
  346. void json_fix_string(char *s);
  347. void *netdata_mmap(const char *filename, size_t size, int flags, int ksm, bool read_only, int *open_fd);
  348. int netdata_munmap(void *ptr, size_t size);
  349. int memory_file_save(const char *filename, void *mem, size_t size);
  350. int fd_is_valid(int fd);
  351. extern struct rlimit rlimit_nofile;
  352. extern int enable_ksm;
  353. char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
  354. int verify_netdata_host_prefix();
  355. int recursively_delete_dir(const char *path, const char *reason);
  356. extern volatile sig_atomic_t netdata_exit;
  357. extern const char *program_version;
  358. char *strdupz_path_subpath(const char *path, const char *subpath);
  359. int path_is_dir(const char *path, const char *subpath);
  360. int path_is_file(const char *path, const char *subpath);
  361. void recursive_config_double_dir_load(
  362. const char *user_path
  363. , const char *stock_path
  364. , const char *subpath
  365. , int (*callback)(const char *filename, void *data)
  366. , void *data
  367. , size_t depth
  368. );
  369. char *read_by_filename(char *filename, long *file_size);
  370. char *find_and_replace(const char *src, const char *find, const char *replace, const char *where);
  371. /* fix for alpine linux */
  372. #ifndef RUSAGE_THREAD
  373. #ifdef RUSAGE_CHILDREN
  374. #define RUSAGE_THREAD RUSAGE_CHILDREN
  375. #endif
  376. #endif
  377. #define BITS_IN_A_KILOBIT 1000
  378. #define KILOBITS_IN_A_MEGABIT 1000
  379. /* misc. */
  380. #define UNUSED(x) (void)(x)
  381. #ifdef __GNUC__
  382. #define UNUSED_FUNCTION(x) __attribute__((unused)) UNUSED_##x
  383. #else
  384. #define UNUSED_FUNCTION(x) UNUSED_##x
  385. #endif
  386. #define error_report(x, args...) do { errno = 0; error(x, ##args); } while(0)
  387. // Taken from linux kernel
  388. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  389. typedef struct bitmap256 {
  390. uint64_t data[4];
  391. } BITMAP256;
  392. bool bitmap256_get_bit(BITMAP256 *ptr, uint8_t idx);
  393. void bitmap256_set_bit(BITMAP256 *ptr, uint8_t idx, bool value);
  394. #define COMPRESSION_MAX_MSG_SIZE 0x4000
  395. #define PLUGINSD_LINE_MAX (COMPRESSION_MAX_MSG_SIZE - 1024)
  396. int config_isspace(char c);
  397. int pluginsd_space(char c);
  398. size_t quoted_strings_splitter(char *str, char **words, size_t max_words, int (*custom_isspace)(char), char *recover_input, char **recover_location, int max_recover);
  399. size_t pluginsd_split_words(char *str, char **words, size_t max_words, char *recover_string, char **recover_location, int max_recover);
  400. static inline char *get_word(char **words, size_t num_words, size_t index) {
  401. if (index >= num_words)
  402. return NULL;
  403. return words[index];
  404. }
  405. bool run_command_and_copy_output_to_stdout(const char *command, int max_line_length);
  406. typedef enum {
  407. OPEN_FD_ACTION_CLOSE,
  408. OPEN_FD_ACTION_FD_CLOEXEC
  409. } OPEN_FD_ACTION;
  410. typedef enum {
  411. OPEN_FD_EXCLUDE_STDIN = 0x01,
  412. OPEN_FD_EXCLUDE_STDOUT = 0x02,
  413. OPEN_FD_EXCLUDE_STDERR = 0x04
  414. } OPEN_FD_EXCLUDE;
  415. void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds);
  416. void netdata_cleanup_and_exit(int ret) NORETURN;
  417. void send_statistics(const char *action, const char *action_result, const char *action_data);
  418. extern char *netdata_configured_host_prefix;
  419. #include "libjudy/src/Judy.h"
  420. #include "july/july.h"
  421. #include "os.h"
  422. #include "storage_number/storage_number.h"
  423. #include "threads/threads.h"
  424. #include "buffer/buffer.h"
  425. #include "locks/locks.h"
  426. #include "circular_buffer/circular_buffer.h"
  427. #include "avl/avl.h"
  428. #include "inlined.h"
  429. #include "clocks/clocks.h"
  430. #include "completion/completion.h"
  431. #include "popen/popen.h"
  432. #include "simple_pattern/simple_pattern.h"
  433. #ifdef ENABLE_HTTPS
  434. # include "socket/security.h"
  435. #endif
  436. #include "socket/socket.h"
  437. #include "config/appconfig.h"
  438. #include "log/log.h"
  439. #include "procfile/procfile.h"
  440. #include "string/string.h"
  441. #include "dictionary/dictionary.h"
  442. #if defined(HAVE_LIBBPF) && !defined(__cplusplus)
  443. #include "ebpf/ebpf.h"
  444. #endif
  445. #include "eval/eval.h"
  446. #include "statistical/statistical.h"
  447. #include "adaptive_resortable_list/adaptive_resortable_list.h"
  448. #include "url/url.h"
  449. #include "json/json.h"
  450. #include "health/health.h"
  451. #include "string/utf8.h"
  452. #include "libnetdata/aral/aral.h"
  453. #include "onewayalloc/onewayalloc.h"
  454. #include "worker_utilization/worker_utilization.h"
  455. // BEWARE: Outside of the C code this also exists in alarm-notify.sh
  456. #define DEFAULT_CLOUD_BASE_URL "https://api.netdata.cloud"
  457. #define DEFAULT_CLOUD_UI_URL "https://app.netdata.cloud"
  458. #define RRD_STORAGE_TIERS 5
  459. static inline size_t struct_natural_alignment(size_t size) __attribute__((const));
  460. #define STRUCT_NATURAL_ALIGNMENT (sizeof(uintptr_t) * 2)
  461. static inline size_t struct_natural_alignment(size_t size) {
  462. if(unlikely(size % STRUCT_NATURAL_ALIGNMENT))
  463. size = size + STRUCT_NATURAL_ALIGNMENT - (size % STRUCT_NATURAL_ALIGNMENT);
  464. return size;
  465. }
  466. #ifdef NETDATA_TRACE_ALLOCATIONS
  467. struct malloc_trace {
  468. avl_t avl;
  469. const char *function;
  470. const char *file;
  471. size_t line;
  472. size_t malloc_calls;
  473. size_t calloc_calls;
  474. size_t realloc_calls;
  475. size_t strdup_calls;
  476. size_t free_calls;
  477. size_t mmap_calls;
  478. size_t munmap_calls;
  479. size_t allocations;
  480. size_t bytes;
  481. struct rrddim *rd_bytes;
  482. struct rrddim *rd_allocations;
  483. struct rrddim *rd_avg_alloc;
  484. struct rrddim *rd_ops;
  485. };
  486. #endif // NETDATA_TRACE_ALLOCATIONS
  487. static inline PPvoid_t JudyLFirstThenNext(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  488. if(unlikely(*first)) {
  489. *first = false;
  490. return JudyLFirst(PArray, PIndex, PJE0);
  491. }
  492. return JudyLNext(PArray, PIndex, PJE0);
  493. }
  494. static inline PPvoid_t JudyLLastThenPrev(Pcvoid_t PArray, Word_t * PIndex, bool *first) {
  495. if(unlikely(*first)) {
  496. *first = false;
  497. return JudyLLast(PArray, PIndex, PJE0);
  498. }
  499. return JudyLPrev(PArray, PIndex, PJE0);
  500. }
  501. static inline size_t indexing_partition_old(Word_t ptr, Word_t modulo) {
  502. size_t total = 0;
  503. total += (ptr & 0xff) >> 0;
  504. total += (ptr & 0xff00) >> 8;
  505. total += (ptr & 0xff0000) >> 16;
  506. total += (ptr & 0xff000000) >> 24;
  507. if(sizeof(Word_t) > 4) {
  508. total += (ptr & 0xff00000000) >> 32;
  509. total += (ptr & 0xff0000000000) >> 40;
  510. total += (ptr & 0xff000000000000) >> 48;
  511. total += (ptr & 0xff00000000000000) >> 56;
  512. }
  513. return (total % modulo);
  514. }
  515. static uint32_t murmur32(uint32_t h) __attribute__((const));
  516. static inline uint32_t murmur32(uint32_t h) {
  517. h ^= h >> 16;
  518. h *= 0x85ebca6b;
  519. h ^= h >> 13;
  520. h *= 0xc2b2ae35;
  521. h ^= h >> 16;
  522. return h;
  523. }
  524. static uint64_t murmur64(uint64_t h) __attribute__((const));
  525. static inline uint64_t murmur64(uint64_t k) {
  526. k ^= k >> 33;
  527. k *= 0xff51afd7ed558ccdUL;
  528. k ^= k >> 33;
  529. k *= 0xc4ceb9fe1a85ec53UL;
  530. k ^= k >> 33;
  531. return k;
  532. }
  533. static inline size_t indexing_partition(Word_t ptr, Word_t modulo) __attribute__((const));
  534. static inline size_t indexing_partition(Word_t ptr, Word_t modulo) {
  535. if(sizeof(Word_t) == 8) {
  536. uint64_t hash = murmur64(ptr);
  537. return hash % modulo;
  538. }
  539. else {
  540. uint32_t hash = murmur32(ptr);
  541. return hash % modulo;
  542. }
  543. }
  544. # ifdef __cplusplus
  545. }
  546. # endif
  547. #endif // NETDATA_LIB_H