@cryptotaxi247 / netdata-1 / commits / cfae73574

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.

vkalintiris committed Apr 5, 2024 at 14:18 UTC cfae735749c22237f2fbeb6f5a55d33323ae39d7
7 files changed +41 -198
CMakeLists.txt
+1 -1
@@ -665,7 +665,7 @@ set(LIBNETDATA_FILES
665 src/libnetdata/dictionary/dictionary-callbacks.h
666 src/libnetdata/linked-lists.h
667 src/libnetdata/storage-point.h
668 - src/libnetdata/bitmap.h
668 + src/libnetdata/bitmap64.h
669 )
670
671 if(ENABLE_PLUGIN_EBPF)
src/daemon/main.c
-1
@@ -1530,7 +1530,6 @@ int main(int argc, char **argv) {
1530 if (unit_test_buffer()) return 1;
1531 if (unit_test_str2ld()) return 1;
1532 if (buffer_unittest()) return 1;
1533 - if (unit_test_bitmaps()) return 1;
1533
1534 // No call to load the config file on this code-path
1535 if (unittest_prepare_rrd(&user)) return 1;
src/daemon/unit_test.c
-111
@@ -1693,114 +1693,3 @@ error:
1693 fprintf(stderr,"SQLite tests failed\n");
1694 return 1;
1695 }
1696 -
1697 -static int bitmapX_test(BITMAPX *ptr, char *expected, const char *msg) {
1698 - int errors = 0;
1699 -
1700 - for(uint32_t idx = 0; idx < ptr->bits ; idx++) {
1701 - bool found_set = bitmapX_get_bit(ptr, idx);
1702 - bool expected_set = expected[idx];
1703 -
1704 - if(found_set != expected_set) {
1705 - fprintf(stderr, " >>> %s(): %s, bit %u is expected %s but found %s\n",
1706 - __FUNCTION__, msg, idx, expected_set?"SET":"UNSET", found_set?"SET":"UNSET");
1707 - errors++;
1708 - }
1709 - }
1710 -
1711 - if(errors)
1712 - fprintf(stderr,"%s(): %s, found %d errors\n",
1713 - __FUNCTION__, msg, errors);
1714 -
1715 - return errors;
1716 -}
1717 -
1718 -#define bitmapX_set_bit_and_track(ptr, bit, value, expected) do { \
1719 - bitmapX_set_bit(ptr, bit, value); \
1720 - (expected)[bit] = value; \
1721 -} while(0)
1722 -
1723 -int unit_test_bitmaps(void) {
1724 - fprintf(stderr, "%s() running...\n", __FUNCTION__ );
1725 -
1726 - int errors = 0;
1727 -
1728 - char expected[8192];
1729 -
1730 - BITMAP256 bmp256 = BITMAP256_INITIALIZER;
1731 - BITMAP1024 bmp1024 = BITMAP1024_INITIALIZER;
1732 - BITMAPX *bmp = NULL;
1733 -
1734 - for(int x = 0; x < 3 ; x++) {
1735 - char msg[100 + 1];
1736 -
1737 - switch (x) {
1738 - default:
1739 - case 0:
1740 - bmp = (BITMAPX *) &bmp256;
1741 - break;
1742 -
1743 - case 1:
1744 - bmp = (BITMAPX *) &bmp1024;
1745 - break;
1746 -
1747 - case 2:
1748 - bmp = bitmapX_create(8192);
1749 - break;
1750 - }
1751 -
1752 - // reset
1753 - memset(expected, 0, bmp->bits);
1754 - memset(bmp->data, 0, bmp->bits / 8);
1755 -
1756 - snprintf(msg, 100, "TEST 1 BITMAP %u", bmp->bits);
1757 - bitmapX_set_bit_and_track(bmp, 0, true, expected);
1758 - errors += bitmapX_test(bmp, expected, msg);
1759 -
1760 - snprintf(msg, 100, "TEST 2 BITMAP %u", bmp->bits);
1761 - bitmapX_set_bit_and_track(bmp, 64, true, expected);
1762 - errors += bitmapX_test(bmp, expected, msg);
1763 -
1764 - snprintf(msg, 100, "TEST 3 BITMAP %u", bmp->bits);
1765 - bitmapX_set_bit_and_track(bmp, 128, true, expected);
1766 - errors += bitmapX_test(bmp, expected, msg);
1767 -
1768 - snprintf(msg, 100, "TEST 4 BITMAP %u", bmp->bits);
1769 - bitmapX_set_bit_and_track(bmp, 192, true, expected);
1770 - errors += bitmapX_test(bmp, expected, msg);
1771 -
1772 - for (uint32_t step = 1; step < 256; step++) {
1773 - snprintf(msg, 100, "TEST 5 (setting) BITMAP %u STEP %u", bmp->bits, step);
1774 -
1775 - // reset
1776 - memset(expected, 0, bmp->bits);
1777 - memset(bmp->data, 0, bmp->bits / 8);
1778 -
1779 - for (uint32_t i = 0; i < bmp->bits ; i += step)
1780 - bitmapX_set_bit_and_track(bmp, i, true, expected);
1781 -
1782 - errors += bitmapX_test(bmp, expected, msg);
1783 - }
1784 -
1785 - for (uint32_t step = 1; step < 256; step++) {
1786 - snprintf(msg, 100, "TEST 6 (clearing) BITMAP %u STEP %u", bmp->bits, step);
1787 -
1788 - // reset
1789 - memset(expected, 0, bmp->bits);
1790 - memset(bmp->data, 0, bmp->bits / 8);
1791 -
1792 - for (uint32_t i = 0; i < bmp->bits ; i++)
1793 - bitmapX_set_bit_and_track(bmp, i, true, expected);
1794 -
1795 - for (uint32_t i = 0; i < bmp->bits ; i += step)
1796 - bitmapX_set_bit_and_track(bmp, i, false, expected);
1797 -
1798 - errors += bitmapX_test(bmp, expected, msg);
1799 - }
1800 - }
1801 -
1802 - freez(bmp);
1803 -
1804 - fprintf(stderr, "%s() %d errors\n", __FUNCTION__, errors);
1805 - return errors;
1806 -}
src/database/engine/journalfile.c
+4 -3
@@ -1,4 +1,5 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 +#include "libnetdata/bitmap64.h"
3 #include "rrdengine.h"
4
5 static void after_extent_write_journalfile_v1_io(uv_fs_t* req)
@@ -654,7 +655,7 @@ static int journalfile_check_superblock(uv_file file)
655
656 static void journalfile_restore_extent_metadata(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile, void *buf, unsigned max_size)
657 {
657 - static BITMAP256 page_error_map = BITMAP256_INITIALIZER;
658 + static bitmap64_t page_error_map = BITMAP64_INITIALIZER;
659 unsigned i, count, payload_length, descr_size;
660 struct rrdeng_jf_store_data *jf_metric_data;
661
@@ -673,9 +674,9 @@ static void journalfile_restore_extent_metadata(struct rrdengine_instance *ctx,
674 uint8_t page_type = jf_metric_data->descr[i].type;
675
676 if (page_type > RRDENG_PAGE_TYPE_MAX) {
676 - if (!bitmap256_get_bit(&page_error_map, page_type)) {
677 + if (!bitmap64_get(&page_error_map, page_type)) {
678 netdata_log_error("DBENGINE: unknown page type %d encountered.", page_type);
678 - bitmap256_set_bit(&page_error_map, page_type, 1);
679 + bitmap64_set(&page_error_map, page_type);
680 }
681 continue;
682 }
src/libnetdata/bitmap.h deleted
-81
@@ -1,81 +0,0 @@
1 -// SPDX-License-Identifier: GPL-3.0-or-later
2 -
3 -#ifndef NETDATA_BITMAP_H
4 -#define NETDATA_BITMAP_H
5 -
6 -#ifdef ENV32BIT
7 -
8 -typedef struct bitmapX {
9 - uint32_t bits;
10 - uint32_t data[];
11 -} BITMAPX;
12 -
13 -typedef struct bitmap256 {
14 - uint32_t bits;
15 - uint32_t data[256 / 32];
16 -} BITMAP256;
17 -
18 -typedef struct bitmap1024 {
19 - uint32_t bits;
20 - uint32_t data[1024 / 32];
21 -} BITMAP1024;
22 -
23 -static inline BITMAPX *bitmapX_create(uint32_t bits) {
24 - BITMAPX *bmp = (BITMAPX *)callocz(1, sizeof(BITMAPX) + sizeof(uint32_t) * ((bits + 31) / 32));
25 - uint32_t *p = (uint32_t *)&bmp->bits;
26 - *p = bits;
27 - return bmp;
28 -}
29 -
30 -#define bitmapX_get_bit(ptr, idx) ((ptr)->data[(idx) >> 5] & (1U << ((idx) & 31)))
31 -#define bitmapX_set_bit(ptr, idx, value) do { \
32 - register uint32_t _bitmask = 1U << ((idx) & 31); \
33 - if (value) \
34 - (ptr)->data[(idx) >> 5] |= _bitmask; \
35 - else \
36 - (ptr)->data[(idx) >> 5] &= ~_bitmask; \
37 -} while(0)
38 -
39 -#else // 64bit version of bitmaps
40 -
41 -typedef struct bitmapX {
42 - uint32_t bits;
43 - uint64_t data[];
44 -} BITMAPX;
45 -
46 -typedef struct bitmap256 {
47 - uint32_t bits;
48 - uint64_t data[256 / 64];
49 -} BITMAP256;
50 -
51 -typedef struct bitmap1024 {
52 - uint32_t bits;
53 - uint64_t data[1024 / 64];
54 -} BITMAP1024;
55 -
56 -static inline BITMAPX *bitmapX_create(uint32_t bits) {
57 - BITMAPX *bmp = (BITMAPX *)callocz(1, sizeof(BITMAPX) + sizeof(uint64_t) * ((bits + 63) / 64));
58 - bmp->bits = bits;
59 - return bmp;
60 -}
61 -
62 -#define bitmapX_get_bit(ptr, idx) ((ptr)->data[(idx) >> 6] & (1ULL << ((idx) & 63)))
63 -#define bitmapX_set_bit(ptr, idx, value) do { \
64 - register uint64_t _bitmask = 1ULL << ((idx) & 63); \
65 - if (value) \
66 - (ptr)->data[(idx) >> 6] |= _bitmask; \
67 - else \
68 - (ptr)->data[(idx) >> 6] &= ~_bitmask; \
69 -} while(0)
70 -
71 -#endif // 64bit version of bitmaps
72 -
73 -#define BITMAPX_INITIALIZER(wanted_bits) { .bits = (wanted_bits), .data = {0} }
74 -#define BITMAP256_INITIALIZER (BITMAP256)BITMAPX_INITIALIZER(256)
75 -#define BITMAP1024_INITIALIZER (BITMAP1024)BITMAPX_INITIALIZER(1024)
76 -#define bitmap256_get_bit(ptr, idx) bitmapX_get_bit((BITMAPX *)ptr, idx)
77 -#define bitmap256_set_bit(ptr, idx, value) bitmapX_set_bit((BITMAPX *)ptr, idx, value)
78 -#define bitmap1024_get_bit(ptr, idx) bitmapX_get_bit((BITMAPX *)ptr, idx)
79 -#define bitmap1024_set_bit(ptr, idx, value) bitmapX_set_bit((BITMAPX *)ptr, idx, value)
80 -
81 -#endif //NETDATA_BITMAP_H
src/libnetdata/bitmap64.h new
+35
@@ -0,0 +1,35 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +#ifndef NETDATA_BITMAP64_H
4 +#define NETDATA_BITMAP64_H
5 +
6 +#include <stdbool.h>
7 +#include <stdint.h>
8 +#include <assert.h>
9 +
10 +typedef uint64_t bitmap64_t;
11 +
12 +#define BITMAP64_INITIALIZER 0
13 +
14 +static inline void bitmap64_set(bitmap64_t *bitmap, int position)
15 +{
16 + assert(position >= 0 && position < 64);
17 +
18 + *bitmap |= (1ULL << position);
19 +}
20 +
21 +static inline void bitmap64_clear(bitmap64_t *bitmap, int position)
22 +{
23 + assert(position >= 0 && position < 64);
24 +
25 + *bitmap &= ~(1ULL << position);
26 +}
27 +
28 +static inline bool bitmap64_get(const bitmap64_t *bitmap, int position)
29 +{
30 + assert(position >= 0 && position < 64);
31 +
32 + return (*bitmap & (1ULL << position));
33 +}
34 +
35 +#endif // NETDATA_BITMAP64_H
src/libnetdata/libnetdata.h
+1 -1
@@ -352,7 +352,7 @@ char *find_and_replace(const char *src, const char *find, const char *replace, c
352 // Taken from linux kernel
353 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
354
355 -#include "bitmap.h"
355 +#include "bitmap64.h"
356
357 #define COMPRESSION_MAX_CHUNK 0x4000
358 #define COMPRESSION_MAX_OVERHEAD 128