| 1 | |
| 2 | #ifndef ARAL_H |
| 3 | #define ARAL_H 1 |
| 4 | |
| 5 | #include "../libnetdata.h" |
| 6 | |
| 7 | #define ARAL_MAX_NAME 23 |
| 8 | |
| 9 | typedef struct aral ARAL; |
| 10 | |
| 11 | struct aral_page_type_stats { |
| 12 | PAD64(size_t) allocations; |
| 13 | PAD64(size_t) allocated_bytes; |
| 14 | PAD64(size_t) used_bytes; |
| 15 | PAD64(size_t) padding_bytes; |
| 16 | }; |
| 17 | |
| 18 | struct aral_statistics { |
| 19 | struct { |
| 20 | PAD64(size_t) allocations; |
| 21 | PAD64(size_t) allocated_bytes; |
| 22 | } structures; |
| 23 | |
| 24 | struct aral_page_type_stats malloc; |
| 25 | struct aral_page_type_stats mmap; |
| 26 | }; |
| 27 | |
| 28 | // -------------------------------------------------------------------------------------------------------------------- |
| 29 | |
| 30 | const char *aral_name(ARAL *ar); |
| 31 | |
| 32 | ARAL *aral_create(const char *name, size_t element_size, size_t initial_page_elements, size_t max_page_size, |
| 33 | struct aral_statistics *stats, const char *filename, const char **cache_dir, |
| 34 | bool mmap, bool lockless, bool dont_dump); |
| 35 | |
| 36 | // -------------------------------------------------------------------------------------------------------------------- |
| 37 | |
| 38 | // return the size of the element, as requested |
| 39 | size_t aral_requested_element_size(ARAL *ar); |
| 40 | |
| 41 | // return the exact memory footprint of the elements |
| 42 | size_t aral_actual_element_size(ARAL *ar); |
| 43 | |
| 44 | // -------------------------------------------------------------------------------------------------------------------- |
| 45 | |
| 46 | size_t aral_optimal_malloc_page_size(void); |
| 47 | void aral_optimal_malloc_page_size_set(size_t size); |
| 48 | |
| 49 | // -------------------------------------------------------------------------------------------------------------------- |
| 50 | |
| 51 | /* |
| 52 | * |
| 53 | * The total memory used by ARAL is: |
| 54 | * |
| 55 | * total = structures + used + free + padding |
| 56 | * |
| 57 | * or |
| 58 | * |
| 59 | * total = structures + allocated + padding |
| 60 | * |
| 61 | * always: |
| 62 | * |
| 63 | * allocated = used + free |
| 64 | * |
| 65 | * Hints: |
| 66 | * - allocated, used and free are about the requested element size. |
| 67 | * - structures includes the extension of the elements for the metadata aral needs. |
| 68 | * - padding is lost due to alignment requirements |
| 69 | * |
| 70 | */ |
| 71 | |
| 72 | size_t aral_structures_bytes(ARAL *ar); |
| 73 | size_t aral_free_bytes(ARAL *ar); |
| 74 | size_t aral_used_bytes(ARAL *ar); |
| 75 | size_t aral_padding_bytes(ARAL *ar); |
| 76 | |
| 77 | struct aral_statistics *aral_get_statistics(ARAL *ar); |
| 78 | |
| 79 | size_t aral_structures_bytes_from_stats(struct aral_statistics *stats); |
| 80 | size_t aral_free_bytes_from_stats(struct aral_statistics *stats); |
| 81 | size_t aral_used_bytes_from_stats(struct aral_statistics *stats); |
| 82 | size_t aral_padding_bytes_from_stats(struct aral_statistics *stats); |
| 83 | |
| 84 | // -------------------------------------------------------------------------------------------------------------------- |
| 85 | |
| 86 | ARAL *aral_by_size_acquire(size_t size); |
| 87 | void aral_by_size_release(ARAL *ar); |
| 88 | |
| 89 | size_t aral_by_size_structures_bytes(void); |
| 90 | size_t aral_by_size_free_bytes(void); |
| 91 | size_t aral_by_size_used_bytes(void); |
| 92 | size_t aral_by_size_padding_bytes(void); |
| 93 | |
| 94 | struct aral_statistics *aral_by_size_statistics(void); |
| 95 | |
| 96 | // -------------------------------------------------------------------------------------------------------------------- |
| 97 | |
| 98 | #ifdef NETDATA_TRACE_ALLOCATIONS |
| 99 | |
| 100 | #define aral_callocz(ar) aral_callocz_internal(ar, false, __FILE__, __FUNCTION__, __LINE__) |
| 101 | #define aral_callocz_marked(ar) aral_callocz_internal(ar, true, __FILE__, __FUNCTION__, __LINE__) |
| 102 | #define aral_mallocz(ar) aral_mallocz_internal(ar, false, __FILE__, __FUNCTION__, __LINE__) |
| 103 | #define aral_mallocz_marked(ar) aral_mallocz_internal(ar, true, __FILE__, __FUNCTION__, __LINE__) |
| 104 | #define aral_freez(ar, ptr) aral_freez_internal(ar, ptr, __FILE__, __FUNCTION__, __LINE__) |
| 105 | #define aral_destroy(ar) aral_destroy_internal(ar, __FILE__, __FUNCTION__, __LINE__) |
| 106 | |
| 107 | void *aral_callocz_internal(ARAL *ar, bool marked, const char *file, const char *function, size_t line); |
| 108 | void *aral_mallocz_internal(ARAL *ar, bool marked, const char *file, const char *function, size_t line); |
| 109 | void aral_freez_internal(ARAL *ar, void *ptr, const char *file, const char *function, size_t line); |
| 110 | void aral_destroy_internal(ARAL *ar, const char *file, const char *function, size_t line); |
| 111 | |
| 112 | #else // NETDATA_TRACE_ALLOCATIONS |
| 113 | |
| 114 | #define aral_mallocz(ar) aral_mallocz_internal(ar, false) |
| 115 | #define aral_mallocz_marked(ar) aral_mallocz_internal(ar, true) |
| 116 | #define aral_callocz(ar) aral_callocz_internal(ar, false) |
| 117 | #define aral_callocz_marked(ar) aral_callocz_internal(ar, true) |
| 118 | #define aral_freez(ar, ptr) aral_freez_internal(ar, ptr) |
| 119 | #define aral_destroy(ar) aral_destroy_internal(ar) |
| 120 | |
| 121 | |
| 122 | void *aral_callocz_internal(ARAL *ar, bool marked); |
| 123 | void *aral_mallocz_internal(ARAL *ar, bool marked); |
| 124 | void aral_freez_internal(ARAL *ar, void *ptr); |
| 125 | void aral_destroy_internal(ARAL *ar); |
| 126 | |
| 127 | #endif // NETDATA_TRACE_ALLOCATIONS |
| 128 | |
| 129 | // -------------------------------------------------------------------------------------------------------------------- |
| 130 | // Declarations that do not depend on the NETDATA_TRACE_ALLOCATIONS macro |
| 131 | // shape, kept outside the conditional so callers in any compilation unit |
| 132 | // can see them in both build modes. |
| 133 | |
| 134 | void aral_unmark_allocation(ARAL *ar, void *ptr); |
| 135 | |
| 136 | int aral_unittest(size_t elements); |
| 137 | |
| 138 | #ifdef NETDATA_INTERNAL_CHECKS |
| 139 | int aral_unittest_concurrency(void); |
| 140 | #endif |
| 141 | |
| 142 | #endif // ARAL_H |