Skip to contentSkip to conversation

erofs-utils: lib: add bitops header

PATCH New Hongzhen Luo 1 participant 3 messages Jul 22, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 erofs-utils: lib: add bitops header

Add bitops header for subsequent bloom filter implementation. This is borrowed from a part of the previous patch. See:
https://lore.kernel.org/all/[email protected]/.

---
v2: Rename the bitops functions.
v1: https://lore.kernel.org/all/[email protected]/

---
 include/erofs/bitops.h | 37 +++++++++++++++++++++++++++++++++++++
 lib/Makefile.am        |  1 +
 2 files changed, 38 insertions(+)
 create mode 100644 include/erofs/bitops.h
include/erofs/bitops.h new file +37 -0
new file mode 100644
index 0000000..fa31934
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_BITOPS_H
+#define __EROFS_BITOPS_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "defs.h"
+
+static inline void __set_bit(int nr, volatile unsigned long *addr)
+{
+	unsigned long mask = BIT_MASK(nr);
+	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+	*p  |= mask;
+}
+
+static inline void __clear_bit(int nr, volatile unsigned long *addr)
+{
+	unsigned long mask = BIT_MASK(nr);
+	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+	*p &= ~mask;
+}
+
+static inline int __test_bit(int nr, const volatile unsigned long *addr)
+{
+	return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/Makefile.am +1 -0
index 2cb4cab..6b52470 100644
@@ -20,6 +20,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/io.h \
       $(top_srcdir)/include/erofs/list.h \
       $(top_srcdir)/include/erofs/print.h \
+      $(top_srcdir)/include/erofs/bitops.h \
       $(top_srcdir)/include/erofs/tar.h \
       $(top_srcdir)/include/erofs/trace.h \
       $(top_srcdir)/include/erofs/xattr.h \
Signature · 1 line
2.43.5
#1 lore raw

PATCH v2 2/3 erofs-utils: lib: add bloom filter

Introduce following bloom filter helpers:

erofs_bloom_init
erofs_bloom_add
erofs_bloom_test
erofs_bloom_exit

---
v2: Fold struct bitmap into struct erofs_bloom_filter to make it look cleaner.
v1: https://lore.kernel.org/all/[email protected]/

---
 include/erofs/bloom_filter.h | 31 ++++++++++++
 include/erofs/internal.h     |  2 +
 lib/Makefile.am              |  2 +-
 lib/bloom_filter.c           | 93 ++++++++++++++++++++++++++++++++++++
 4 files changed, 127 insertions(+), 1 deletion(-)
 create mode 100644 include/erofs/bloom_filter.h
 create mode 100644 lib/bloom_filter.c
include/erofs/bloom_filter.h new file +31 -0
new file mode 100644
index 0000000..6f30190
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_BLOOM_FILTER_H
+#define __EROFS_BLOOM_FILTER_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "internal.h"
+#include "bitops.h"
+
+struct erofs_bloom_filter {
+        unsigned long size;
+        unsigned long *map;
+        unsigned long bitmap_mask;
+        u32 hash_seed;
+        u32 nr_funcs;
+};
+
+int erofs_bloom_init(struct erofs_sb_info *sbi, u32 nr_funcs,
+                     unsigned long entries, u32 seed);
+long erofs_bloom_add(struct erofs_sb_info *sbi, void *data, size_t length);
+long erofs_bloom_test(struct erofs_sb_info *sbi, void *data, size_t length);
+void erofs_bloom_exit(struct erofs_sb_info *sbi);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
include/erofs/internal.h +2 -0
index 708e51e..d3dd676 100644
@@ -74,6 +74,7 @@ struct erofs_xattr_prefix_item {
 #define EROFS_PACKED_NID_UNALLOCATED	-1
 
 struct erofs_mkfs_dfops;
+struct erofs_bloom_filter;
 struct erofs_sb_info {
 	struct erofs_device_info *devs;
 	char *devname;
@@ -134,6 +135,7 @@ struct erofs_sb_info {
 #endif
 	struct erofs_bufmgr *bmgr;
 	bool useqpl;
+	struct erofs_bloom_filter *bf;
 };
 
 #define EROFS_SUPER_END (EROFS_SUPER_OFFSET + sizeof(struct erofs_super_block))
lib/Makefile.am +1 -1
index 6b52470..78140e7 100644
@@ -35,7 +35,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
 		      compress_hints.c hashmap.c sha256.c blobchunk.c dir.c \
 		      fragments.c dedupe.c uuid_unparse.c uuid.c tar.c \
-		      block_list.c xxhash.c rebuild.c diskbuf.c
+		      block_list.c xxhash.c rebuild.c diskbuf.c bloom_filter.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 if ENABLE_LZ4
lib/bloom_filter.c new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.43.5
#2 lore raw

PATCH v2 3/3 erofs-utils: lib: enhance erofs_rebuild_get_dentry with bloom filter

In reply to Hongzhen Luo

Add a bloom filter to exclude entries that are not in pwd->i_subdirs, thereby improving the performance of erofs_rebuild_get_dentry. Below are the results for different # of files in the same directory:

+---------+--------------------+
| # files | time reduction (%) |
+---------+--------------------+
|   1e4   |         55%        |
+---------+--------------------+
|   1e5   |         98%        |
+---------+--------------------+
|   2e5   |         98%        |
+---------+--------------------+
|   3e5   |         99%        |
+---------+--------------------+

---
v2: Changes since v1:
- move erofs_bloom_init() to erofs_super_init()
- make the # hash function and bit array size of the bloom filter configurable
v1: https://lore.kernel.org/all/[email protected]/

---
 include/erofs/config.h   |  2 ++
 include/erofs/internal.h |  1 +
 lib/rebuild.c            | 61 ++++++++++++++++++++++++++++++++++------
 lib/super.c              | 27 ++++++++++++++++++
 mkfs/main.c              | 20 +++++++++++++
 5 files changed, 103 insertions(+), 8 deletions(-)
include/erofs/config.h +2 -0
index f8726b5..c322c9a 100644
@@ -92,6 +92,8 @@ struct erofs_configure {
 	char *fs_config_file;
 	char *block_list_file;
 #endif
+	u32 c_bloom_nrfuc;
+	unsigned long c_bloom_bitsize;
 };
 
 extern struct erofs_configure cfg;
include/erofs/internal.h +1 -0
index d3dd676..ef7dd2d 100644
@@ -402,6 +402,7 @@ struct erofs_map_dev {
 };
 
 /* super.c */
+int erofs_super_init(struct erofs_sb_info *sbi);
 int erofs_read_superblock(struct erofs_sb_info *sbi);
 void erofs_put_super(struct erofs_sb_info *sbi);
 int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
lib/rebuild.c +53 -8

92 lines not on this page · raw message

lib/super.c +27 -0
index 45233c4..39c3bfd 100644
@@ -4,9 +4,14 @@
  */
 #include <string.h>
 #include <stdlib.h>
+#include <time.h>
 #include "erofs/print.h"
 #include "erofs/xattr.h"
 #include "erofs/cache.h"
+#include "erofs/bloom_filter.h"
+
+#define DEFAULT_BLOOM_NRFUNC	5
+#define DEFAULT_BLOOM_SIZE	1000000
 
 static bool check_layout_compatibility(struct erofs_sb_info *sbi,
 				       struct erofs_super_block *dsb)
@@ -72,6 +77,27 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 	return 0;
 }
 
+int erofs_super_init(struct erofs_sb_info *sbi)
+{
+	int err;
+
+	srand(time(NULL));
+	if (cfg.c_bloom_nrfuc && cfg.c_bloom_bitsize)
+		err = erofs_bloom_init(sbi, cfg.c_bloom_nrfuc,
+				       cfg.c_bloom_bitsize, rand());
+	else
+		err = erofs_bloom_init(sbi, DEFAULT_BLOOM_NRFUNC,
+				       DEFAULT_BLOOM_SIZE, rand());
+	if (err) {
+		erofs_err("failed to init bloom filter");
+		goto exit;
+	}
+
+	err = 0;
+exit:
+	return err;
+}
+
 int erofs_read_superblock(struct erofs_sb_info *sbi)
 {
 	u8 data[EROFS_MAX_BLOCK_SIZE];
@@ -153,6 +179,7 @@ void erofs_put_super(struct erofs_sb_info *sbi)
 		erofs_buffer_exit(sbi->bmgr);
 		sbi->bmgr = NULL;
 	}
+	erofs_bloom_exit(sbi);
 }
 
 int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
mkfs/main.c +20 -0
index 20f12fc..21003bc 100644
@@ -31,6 +31,7 @@
 #include "../lib/liberofs_private.h"
 #include "../lib/liberofs_uuid.h"
 #include "../lib/compressor.h"
+#include "erofs/bloom_filter.h"
 
 static struct option long_options[] = {
 	{"version", no_argument, 0, 'V'},
@@ -81,6 +82,7 @@ static struct option long_options[] = {
 	{"zfeature-bits", required_argument, NULL, 521},
 	{"clean", optional_argument, NULL, 522},
 	{"incremental", optional_argument, NULL, 523},
+	{"bloom-filter", optional_argument, NULL, 524},
 	{0, 0, 0, 0},
 };
 
@@ -179,6 +181,9 @@ static void usage(int argc, char **argv)
 		"                                            headerball=file data is omited in the source stream)\n"
 		" --ovlfs-strip=<0,1>   strip overlayfs metadata in the target image (e.g. whiteouts)\n"
 		" --quiet               quiet execution (do not write anything to standard output.)\n"
+		" --bloom-filter=X      boost up images generation with bloom filter\n"
+		"			(X=n,s; n=# of hash functions (default 5),\n"
+		"				s=bit array size (default 1e6))\n"
 #ifndef NDEBUG
 		" --random-pclusterblks randomize pclusterblks for big pcluster (debugging only)\n"
 		" --random-algorithms   randomize per-file algorithms (debugging only)\n"
@@ -820,6 +825,20 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 			}
 			incremental_mode = (opt == 523);
 			break;
+		case 524:
+			cfg.c_bloom_nrfuc = strtol(optarg, &endptr, 0);
+			if (*endptr != ',') {
+				erofs_err("invalid --bloom-filter=%s", optarg);
+				cfg.c_bloom_nrfuc = 0;
+				return -EINVAL;
+			}
+			cfg.c_bloom_bitsize = strtol(endptr + 1, &endptr, 0);
+			if (*endptr != '\0') {
+				erofs_err("invalid --bloom-filter=%s", optarg);
+				cfg.c_bloom_bitsize = 0;
+				return -EINVAL;
+			}
+			break;
 		case 'V':
 			version();
 			exit(0);
@@ -1344,6 +1363,7 @@ int main(int argc, char **argv)
 	}
 
 	erofs_inode_manager_init();
+	erofs_super_init(&g_sbi);
 
 	if (tar_mode) {
 		root = erofs_rebuild_make_root(&g_sbi);
Signature · 1 line
2.43.5