diff options
author | vkalintiris <vasilis@netdata.cloud> | 2024-04-05 14:18:05 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-05 14:18:05 +0300 |
commit | cfae735749c22237f2fbeb6f5a55d33323ae39d7 (patch) | |
tree | f4ab93ae6691c2dc1f3d92d4adb8c6f8d4fe07ee | |
parent | 1e759e7ea87c2d9bda6af17ce579604d35e4038a (diff) |
Drop generic bitmap implementation. (#17322)
We only use it in one place to limit the
logs generated by dbengine per page type.
If we ever need a generic implementation we
can wrap std::bitset and provide a C API.
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/daemon/main.c | 1 | ||||
-rw-r--r-- | src/daemon/unit_test.c | 111 | ||||
-rw-r--r-- | src/database/engine/journalfile.c | 7 | ||||
-rw-r--r-- | src/libnetdata/bitmap.h | 81 | ||||
-rw-r--r-- | src/libnetdata/bitmap64.h | 35 | ||||
-rw-r--r-- | src/libnetdata/libnetdata.h | 2 |
7 files changed, 41 insertions, 198 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 748a9c341e..be1a8a097d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -665,7 +665,7 @@ set(LIBNETDATA_FILES src/libnetdata/dictionary/dictionary-callbacks.h src/libnetdata/linked-lists.h src/libnetdata/storage-point.h - src/libnetdata/bitmap.h + src/libnetdata/bitmap64.h ) if(ENABLE_PLUGIN_EBPF) diff --git a/src/daemon/main.c b/src/daemon/main.c index c9602432a2..30f7457537 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -1530,7 +1530,6 @@ int main(int argc, char **argv) { if (unit_test_buffer()) return 1; if (unit_test_str2ld()) return 1; if (buffer_unittest()) return 1; - if (unit_test_bitmaps()) return 1; // No call to load the config file on this code-path if (unittest_prepare_rrd(&user)) return 1; diff --git a/src/daemon/unit_test.c b/src/daemon/unit_test.c index 54586eab53..e7a743603e 100644 --- a/src/daemon/unit_test.c +++ b/src/daemon/unit_test.c @@ -1693,114 +1693,3 @@ error: fprintf(stderr,"SQLite tests failed\n"); return 1; } - -static int bitmapX_test(BITMAPX *ptr, char *expected, const char *msg) { - int errors = 0; - - for(uint32_t idx = 0; idx < ptr->bits ; idx++) { - bool found_set = bitmapX_get_bit(ptr, idx); - bool expected_set = expected[idx]; - - if(found_set != expected_set) { - fprintf(stderr, " >>> %s(): %s, bit %u is expected %s but found %s\n", - __FUNCTION__, msg, idx, expected_set?"SET":"UNSET", found_set?"SET":"UNSET"); - errors++; - } - } - - if(errors) - fprintf(stderr,"%s(): %s, found %d errors\n", - __FUNCTION__, msg, errors); - - return errors; -} - -#define bitmapX_set_bit_and_track(ptr, bit, value, expected) do { \ - bitmapX_set_bit(ptr, bit, value); \ - (expected)[bit] = value; \ -} while(0) - -int unit_test_bitmaps(void) { - fprintf(stderr, "%s() running...\n", __FUNCTION__ ); - - int errors = 0; - - char expected[8192]; - - BITMAP256 bmp256 = BITMAP256_INITIALIZER; - BITMAP1024 bmp1024 = BITMAP1024_INITIALIZER; - BITMAPX *bmp = NULL; - - for(int x = 0; x < 3 ; x++) { - char msg[100 + 1]; - - switch (x) { - default: - case 0: - bmp = (BITMAPX *) &bmp256; - break; - - case 1: - bmp = (BITMAPX *) &bmp1024; - break; - - case 2: - bmp = bitmapX_create(8192); - break; - } - - // reset - memset(expected, 0, bmp->bits); - memset(bmp->data, 0, bmp->bits / 8); - - snprintf(msg, 100, "TEST 1 BITMAP %u", bmp->bits); - bitmapX_set_bit_and_track(bmp, 0, true, expected); - errors += bitmapX_test(bmp, expected, msg); - - snprintf(msg, 100, "TEST 2 BITMAP %u", bmp->bits); - bitmapX_set_bit_and_track(bmp, 64, true, expected); - errors += bitmapX_test(bmp, expected, msg); - - snprintf(msg, 100, "TEST 3 BITMAP %u", bmp->bits); - bitmapX_set_bit_and_track(bmp, 128, true, expected); - errors += bitmapX_test(bmp, expected, msg); - - snprintf(msg, 100, "TEST 4 BITMAP %u", bmp->bits); - bitmapX_set_bit_and_track(bmp, 192, true, expected); - errors += bitmapX_test(bmp, expected, msg); - - for (uint32_t step = 1; step < 256; step++) { - snprintf(msg, 100, "TEST 5 (setting) BITMAP %u STEP %u", bmp->bits, step); - - // reset - memset(expected, 0, bmp->bits); - memset(bmp->data, 0, bmp->bits / 8); - - for (uint32_t i = 0; i < bmp->bits ; i += step) - bitmapX_set_bit_and_track(bmp, i, true, expected); - - errors += bitmapX_test(bmp, expected, msg); - } - - for (uint32_t step = 1; step < 256; step++) { - snprintf(msg, 100, "TEST 6 (clearing) BITMAP %u STEP %u", bmp->bits, step); - - // reset - memset(expected, 0, bmp->bits); - memset(bmp->data, 0, bmp->bits / 8); - - for (uint32_t i = 0; i < bmp->bits ; i++) - bitmapX_set_bit_and_track(bmp, i, true, expected); - - for (uint32_t i = 0; i < bmp->bits ; i += step) - bitmapX_set_bit_and_track(bmp, i, false, expected); - - errors += bitmapX_test(bmp, expected, msg); - } - } - - freez(bmp); - - fprintf(stderr, "%s() %d errors\n", __FUNCTION__, errors); - return errors; -} diff --git a/src/database/engine/journalfile.c b/src/database/engine/journalfile.c index 8099d017ff..1e25ad5fd7 100644 --- a/src/database/engine/journalfile.c +++ b/src/database/engine/journalfile.c @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later +#include "libnetdata/bitmap64.h" #include "rrdengine.h" static void after_extent_write_journalfile_v1_io(uv_fs_t* req) @@ -654,7 +655,7 @@ static int journalfile_check_superblock(uv_file file) static void journalfile_restore_extent_metadata(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile, void *buf, unsigned max_size) { - static BITMAP256 page_error_map = BITMAP256_INITIALIZER; + static bitmap64_t page_error_map = BITMAP64_INITIALIZER; unsigned i, count, payload_length, descr_size; struct rrdeng_jf_store_data *jf_metric_data; @@ -673,9 +674,9 @@ static void journalfile_restore_extent_metadata(struct rrdengine_instance *ctx, uint8_t page_type = jf_metric_data->descr[i].type; if (page_type > RRDENG_PAGE_TYPE_MAX) { - if (!bitmap256_get_bit(&page_error_map, page_type)) { + if (!bitmap64_get(&page_error_map, page_type)) { netdata_log_error("DBENGINE: unknown page type %d encountered.", page_type); - bitmap256_set_bit(&page_error_map, page_type, 1); + bitmap64_set(&page_error_map, page_type); } continue; } diff --git a/src/libnetdata/bitmap.h b/src/libnetdata/bitmap.h deleted file mode 100644 index 184f94cb6c..0000000000 --- a/src/libnetdata/bitmap.h +++ /dev/null @@ -1,81 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef NETDATA_BITMAP_H -#define NETDATA_BITMAP_H - -#ifdef ENV32BIT - -typedef struct bitmapX { - uint32_t bits; - uint32_t data[]; -} BITMAPX; - -typedef struct bitmap256 { - uint32_t bits; - uint32_t data[256 / 32]; -} BITMAP256; - -typedef struct bitmap1024 { - uint32_t bits; - uint32_t data[1024 / 32]; -} BITMAP1024; - -static inline BITMAPX *bitmapX_create(uint32_t bits) { - BITMAPX *bmp = (BITMAPX *)callocz(1, sizeof(BITMAPX) + sizeof(uint32_t) * ((bits + 31) / 32)); - uint32_t *p = (uint32_t *)&bmp->bits; - *p = bits; - return bmp; -} - -#define bitmapX_get_bit(ptr, idx) ((ptr)->data[(idx) >> 5] & (1U << ((idx) & 31))) -#define bitmapX_set_bit(ptr, idx, value) do { \ - register uint32_t _bitmask = 1U << ((idx) & 31); \ - if (value) \ - (ptr)->data[(idx) >> 5] |= _bitmask; \ - else \ - (ptr)->data[(idx) >> 5] &= ~_bitmask; \ -} while(0) - -#else // 64bit version of bitmaps - -typedef struct bitmapX { - uint32_t bits; - uint64_t data[]; -} BITMAPX; - -typedef struct bitmap256 { - uint32_t bits; - uint64_t data[256 / 64]; -} BITMAP256; - -typedef struct bitmap1024 { - uint32_t bits; - uint64_t data[1024 / 64]; -} BITMAP1024; - -static inline BITMAPX *bitmapX_create(uint32_t bits) { - BITMAPX *bmp = (BITMAPX *)callocz(1, sizeof(BITMAPX) + sizeof(uint64_t) * ((bits + 63) / 64)); - bmp->bits = bits; - return bmp; -} - -#define bitmapX_get_bit(ptr, idx) ((ptr)->data[(idx) >> 6] & (1ULL << ((idx) & 63))) -#define bitmapX_set_bit(ptr, idx, value) do { \ - register uint64_t _bitmask = 1ULL << ((idx) & 63); \ - if (value) \ - (ptr)->data[(idx) >> 6] |= _bitmask; \ - else \ - (ptr)->data[(idx) >> 6] &= ~_bitmask; \ -} while(0) - -#endif // 64bit version of bitmaps - -#define BITMAPX_INITIALIZER(wanted_bits) { .bits = (wanted_bits), .data = {0} } -#define BITMAP256_INITIALIZER (BITMAP256)BITMAPX_INITIALIZER(256) -#define BITMAP1024_INITIALIZER (BITMAP1024)BITMAPX_INITIALIZER(1024) -#define bitmap256_get_bit(ptr, idx) bitmapX_get_bit((BITMAPX *)ptr, idx) -#define bitmap256_set_bit(ptr, idx, value) bitmapX_set_bit((BITMAPX *)ptr, idx, value) -#define bitmap1024_get_bit(ptr, idx) bitmapX_get_bit((BITMAPX *)ptr, idx) -#define bitmap1024_set_bit(ptr, idx, value) bitmapX_set_bit((BITMAPX *)ptr, idx, value) - -#endif //NETDATA_BITMAP_H diff --git a/src/libnetdata/bitmap64.h b/src/libnetdata/bitmap64.h new file mode 100644 index 0000000000..425f3fd209 --- /dev/null +++ b/src/libnetdata/bitmap64.h @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef NETDATA_BITMAP64_H +#define NETDATA_BITMAP64_H + +#include <stdbool.h> +#include <stdint.h> +#include <assert.h> + +typedef uint64_t bitmap64_t; + +#define BITMAP64_INITIALIZER 0 + +static inline void bitmap64_set(bitmap64_t *bitmap, int position) +{ + assert(position >= 0 && position < 64); + + *bitmap |= (1ULL << position); +} + +static inline void bitmap64_clear(bitmap64_t *bitmap, int position) +{ + assert(position >= 0 && position < 64); + + *bitmap &= ~(1ULL << position); +} + +static inline bool bitmap64_get(const bitmap64_t *bitmap, int position) +{ + assert(position >= 0 && position < 64); + + return (*bitmap & (1ULL << position)); +} + +#endif // NETDATA_BITMAP64_H diff --git a/src/libnetdata/libnetdata.h b/src/libnetdata/libnetdata.h index effb4f8e28..21ae7ddc47 100644 --- a/src/libnetdata/libnetdata.h +++ b/src/libnetdata/libnetdata.h @@ -352,7 +352,7 @@ char *find_and_replace(const char *src, const char *find, const char *replace, c // Taken from linux kernel #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) -#include "bitmap.h" +#include "bitmap64.h" #define COMPRESSION_MAX_CHUNK 0x4000 #define COMPRESSION_MAX_OVERHEAD 128 |