rrdenginelib.h 2.6 KB

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