rrdenginelib.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRDENGINELIB_H
  3. #define NETDATA_RRDENGINELIB_H
  4. #include "libnetdata/libnetdata.h"
  5. /* Forward declarations */
  6. struct rrdeng_page_descr;
  7. struct rrdengine_instance;
  8. #define STR_HELPER(x) #x
  9. #define STR(x) STR_HELPER(x)
  10. #define BITS_PER_ULONG (sizeof(unsigned long) * 8)
  11. #define ALIGN_BYTES_FLOOR(x) (((x) / RRDENG_BLOCK_SIZE) * RRDENG_BLOCK_SIZE)
  12. #define ALIGN_BYTES_CEILING(x) ((((x) + RRDENG_BLOCK_SIZE - 1) / RRDENG_BLOCK_SIZE) * RRDENG_BLOCK_SIZE)
  13. #define ROUND_USEC_TO_SEC(x) (((x) + USEC_PER_SEC / 2 - 1) / USEC_PER_SEC)
  14. typedef uintptr_t rrdeng_stats_t;
  15. #ifdef __ATOMIC_RELAXED
  16. #define rrd_atomic_fetch_add(p, n) __atomic_fetch_add(p, n, __ATOMIC_RELAXED)
  17. #define rrd_atomic_add_fetch(p, n) __atomic_add_fetch(p, n, __ATOMIC_RELAXED)
  18. #else
  19. #define rrd_atomic_fetch_add(p, n) __sync_fetch_and_add(p, n)
  20. #define rrd_atomic_add_fetch(p, n) __sync_add_and_fetch(p, n)
  21. #endif
  22. #define rrd_stat_atomic_add(p, n) rrd_atomic_fetch_add(p, n)
  23. /* returns -1 if it didn't find the first cleared bit, the position otherwise. Starts from LSB. */
  24. static inline int find_first_zero(unsigned x)
  25. {
  26. return ffs((int)(~x)) - 1;
  27. }
  28. /* Starts from LSB. */
  29. static inline uint8_t check_bit(unsigned x, size_t pos)
  30. {
  31. return !!(x & (1 << pos));
  32. }
  33. /* Starts from LSB. val is 0 or 1 */
  34. static inline void modify_bit(unsigned *x, unsigned pos, uint8_t val)
  35. {
  36. switch(val) {
  37. case 0:
  38. *x &= ~(1U << pos);
  39. break;
  40. case 1:
  41. *x |= 1U << pos;
  42. break;
  43. default:
  44. error("modify_bit() called with invalid argument.");
  45. break;
  46. }
  47. }
  48. #define RRDENG_PATH_MAX (4096)
  49. /* returns old *ptr value */
  50. static inline unsigned long ulong_compare_and_swap(volatile unsigned long *ptr,
  51. unsigned long oldval, unsigned long newval)
  52. {
  53. return __sync_val_compare_and_swap(ptr, oldval, newval);
  54. }
  55. #ifndef O_DIRECT
  56. /* Workaround for OS X */
  57. #define O_DIRECT (0)
  58. #endif
  59. static inline int crc32cmp(void *crcp, uLong crc)
  60. {
  61. return (*(uint32_t *)crcp != crc);
  62. }
  63. static inline void crc32set(void *crcp, uLong crc)
  64. {
  65. *(uint32_t *)crcp = crc;
  66. }
  67. extern void print_page_cache_descr(struct rrdeng_page_descr *page_cache_descr);
  68. extern void print_page_descr(struct rrdeng_page_descr *descr);
  69. extern int check_file_properties(uv_file file, uint64_t *file_size, size_t min_size);
  70. extern int open_file_for_io(char *path, int flags, uv_file *file, int direct);
  71. static inline int open_file_direct_io(char *path, int flags, uv_file *file)
  72. {
  73. return open_file_for_io(path, flags, file, 1);
  74. }
  75. static inline int open_file_buffered_io(char *path, int flags, uv_file *file)
  76. {
  77. return open_file_for_io(path, flags, file, 0);
  78. }
  79. extern char *get_rrdeng_statistics(struct rrdengine_instance *ctx, char *str, size_t size);
  80. extern int compute_multidb_diskspace();
  81. extern int is_legacy_child(const char *machine_guid);
  82. #endif /* NETDATA_RRDENGINELIB_H */