rrdenginelib.h 2.6 KB

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