Skip to contentSkip to conversation

erofs-utils: optimize buffer allocation logic

PATCH In review Gao Xiang via Linux-erofs 1 participant 7 messages Jan 22, 2021 – Feb 6, 2021 1 message not in the archive Library code

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

v7 cover erofs-utils: optimize buffer allocation logic

Hi all,

This introduces bucket lists for mapped buffer block to boost up buffer allocation and buffer mapping. Thanks to Weiwen for contribution!

changes since v6:
- introduce erofs_bfind_for_attach to clean up erofs_balloc();
- use a new formula mentioned by Jianan to calculate used_before;
- only DBG_BUGON on debug version for __erofs_battach < 0.

Thanks,
Gao Xiang

Gao Xiang (1):
erofs-utils: introduce erofs_bfind_for_attach()

Hu Weiwen (2):
erofs-utils: get rid of `end' argument from erofs_mapbh()
erofs-utils: optimize buffer allocation logic

 include/erofs/cache.h |   3 +-
 lib/cache.c           | 186 ++++++++++++++++++++++++++++++++----------
 lib/compress.c        |   2 +-
 lib/inode.c           |  10 +--
 lib/xattr.c           |   2 +-
 mkfs/main.c           |   2 +-
 6 files changed, 151 insertions(+), 54 deletions(-)
Signature · 1 line
2.24.0
#2 lore raw

PATCH v7 1/3 erofs-utils: get rid of `end' argument from erofs_mapbh()

From: Hu Weiwen <[email protected]>

end arguement is completely broken now. Also, it could be reintroduced later if needed.

---
 include/erofs/cache.h |  2 +-
 lib/cache.c           |  6 ++----
 lib/compress.c        |  2 +-
 lib/inode.c           | 10 +++++-----
 lib/xattr.c           |  2 +-
 mkfs/main.c           |  2 +-
 6 files changed, 11 insertions(+), 13 deletions(-)
include/erofs/cache.h +1 -1
index 8c171f5a130e..f8dff67b9736 100644
@@ -95,7 +95,7 @@ struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
 struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
 					int type, unsigned int size);
 
-erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb, bool end);
+erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb);
 bool erofs_bflush(struct erofs_buffer_block *bb);
 
 void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
lib/cache.c +2 -4
index 0d5c4a5d48de..32a58311f563 100644
@@ -257,16 +257,14 @@ static erofs_blk_t __erofs_mapbh(struct erofs_buffer_block *bb)
 	return blkaddr;
 }
 
-erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb, bool end)
+erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb)
 {
 	struct erofs_buffer_block *t, *nt;
 
 	if (!bb || bb->blkaddr == NULL_ADDR) {
 		list_for_each_entry_safe(t, nt, &blkh.list, list) {
-			if (!end && (t == bb || nt == &blkh))
-				break;
 			(void)__erofs_mapbh(t);
-			if (end && t == bb)
+			if (t == bb)
 				break;
 		}
 	}
lib/compress.c +1 -1
index 86db940b6edd..2b1f93c389ff 100644
@@ -416,7 +416,7 @@ int erofs_write_compressed_file(struct erofs_inode *inode)
 
 	memset(compressmeta, 0, Z_EROFS_LEGACY_MAP_HEADER_SIZE);
 
-	blkaddr = erofs_mapbh(bh->block, true);	/* start_blkaddr */
+	blkaddr = erofs_mapbh(bh->block);	/* start_blkaddr */
 	ctx.blkaddr = blkaddr;
 	ctx.metacur = compressmeta + Z_EROFS_LEGACY_MAP_HEADER_SIZE;
 	ctx.head = ctx.tail = 0;
lib/inode.c +5 -5
index 640068f4147c..ee0afacd4b40 100644
@@ -148,7 +148,7 @@ static int __allocate_inode_bh_data(struct erofs_inode *inode,
 	inode->bh_data = bh;
 
 	/* get blkaddr of the bh */
-	ret = erofs_mapbh(bh->block, true);
+	ret = erofs_mapbh(bh->block);
 	DBG_BUGON(ret < 0);
 
 	/* write blocks except for the tail-end block */
@@ -522,7 +522,7 @@ int erofs_prepare_tail_block(struct erofs_inode *inode)
 		bh->op = &erofs_skip_write_bhops;
 
 		/* get blkaddr of bh */
-		ret = erofs_mapbh(bh->block, true);
+		ret = erofs_mapbh(bh->block);
 		DBG_BUGON(ret < 0);
 		inode->u.i_blkaddr = bh->block->blkaddr;
 
@@ -632,7 +632,7 @@ int erofs_write_tail_end(struct erofs_inode *inode)
 		int ret;
 		erofs_off_t pos;
 
-		erofs_mapbh(bh->block, true);
+		erofs_mapbh(bh->block);
 		pos = erofs_btell(bh, true) - EROFS_BLKSIZ;
 		ret = dev_write(inode->idata, pos, inode->idata_size);
 		if (ret)
@@ -881,7 +881,7 @@ void erofs_fixup_meta_blkaddr(struct erofs_inode *rootdir)
 	struct erofs_buffer_head *const bh = rootdir->bh;
 	erofs_off_t off, meta_offset;
 
-	erofs_mapbh(bh->block, true);
+	erofs_mapbh(bh->block);
 	off = erofs_btell(bh, false);
 
 	if (off > rootnid_maxoffset)
@@ -900,7 +900,7 @@ erofs_nid_t erofs_lookupnid(struct erofs_inode *inode)
 	if (!bh)
 		return inode->nid;
 
-	erofs_mapbh(bh->block, true);
+	erofs_mapbh(bh->block);
 	off = erofs_btell(bh, false);
 
 	meta_offset = blknr_to_addr(sbi.meta_blkaddr);
lib/xattr.c +1 -1
index 49ebb9c2f539..8b7bcb126fe9 100644
@@ -575,7 +575,7 @@ int erofs_build_shared_xattrs_from_path(const char *path)
 	}
 	bh->op = &erofs_skip_write_bhops;
 
-	erofs_mapbh(bh->block, true);
+	erofs_mapbh(bh->block);
 	off = erofs_btell(bh, false);
 
 	sbi.xattr_blkaddr = off / EROFS_BLKSIZ;
mkfs/main.c +1 -1
index abd48be0fa4f..d9c4c7fff5c1 100644
@@ -304,7 +304,7 @@ int erofs_mkfs_update_super_block(struct erofs_buffer_head *bh,
 		round_up(EROFS_SUPER_END, EROFS_BLKSIZ);
 	char *buf;
 
-	*blocks         = erofs_mapbh(NULL, true);
+	*blocks         = erofs_mapbh(NULL);
 	sb.blocks       = cpu_to_le32(*blocks);
 	sb.root_nid     = cpu_to_le16(root_nid);
 	memcpy(sb.uuid, sbi.uuid, sizeof(sb.uuid));
Signature · 1 line
2.24.0
author Reviewed-by Li Guifu on v7 1/3
#3 lore raw

Re: [PATCH v7 1/3] erofs-utils: get rid of `end' argument from erofs_mapbh()

Quoted 7 lines · Gao Xiang via Linux-erofs

From: Hu Weiwen <[email protected]>

end arguement is completely broken now. Also, it could be reintroduced later if needed.

Quoted 9 lines
---
 include/erofs/cache.h |  2 +-
 lib/cache.c           |  6 ++----
 lib/compress.c        |  2 +-
 lib/inode.c           | 10 +++++-----
 lib/xattr.c           |  2 +-
 mkfs/main.c           |  2 +-
 6 files changed, 11 insertions(+), 13 deletions(-)

It looks good

Thanks,

#4 lore raw

PATCH v7 2/3 erofs-utils: introduce erofs_bfind_for_attach()

In reply to Gao Xiang via Linux-erofs

From: Gao Xiang <[email protected]>

Seperate erofs_balloc() to make the logic more clearer.

---
 lib/cache.c | 81 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 31 deletions(-)
lib/cache.c +50 -31
index 32a58311f563..f02413d0f887 100644
@@ -125,25 +125,25 @@ int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr)
 	return __erofs_battach(bb, NULL, incr, 1, 0, false);
 }
 
-struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
-				       unsigned int required_ext,
-				       unsigned int inline_ext)
+static int erofs_bfind_for_attach(int type, erofs_off_t size,
+				  unsigned int required_ext,
+				  unsigned int inline_ext,
+				  unsigned int alignsize,
+				  struct erofs_buffer_block **bbp)
 {
 	struct erofs_buffer_block *cur, *bb;
-	struct erofs_buffer_head *bh;
-	unsigned int alignsize, used0, usedmax;
-
-	int ret = get_alignsize(type, &type);
-
-	if (ret < 0)
-		return ERR_PTR(ret);
-	alignsize = ret;
+	unsigned int used0, usedmax;
 
 	used0 = (size + required_ext) % EROFS_BLKSIZ + inline_ext;
+	/* inline data should be in the same fs block */
+	if (used0 > EROFS_BLKSIZ)
+		return -ENOSPC;
+
 	usedmax = 0;
 	bb = NULL;
 
 	list_for_each_entry(cur, &blkh.list, list) {
+		int ret;
 		unsigned int used_before, used;
 
 		used_before = cur->buffers.off % EROFS_BLKSIZ;
@@ -179,34 +179,53 @@ struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
 			usedmax = used;
 		}
 	}
+	*bbp = bb;
+	return 0;
+}
+
+struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
+				       unsigned int required_ext,
+				       unsigned int inline_ext)
+{
+	struct erofs_buffer_block *bb;
+	struct erofs_buffer_head *bh;
+	unsigned int alignsize;
+
+	int ret = get_alignsize(type, &type);
+
+	if (ret < 0)
+		return ERR_PTR(ret);
+	alignsize = ret;
+
+	/* try to find if we could reuse an allocated buffer block */
+	ret = erofs_bfind_for_attach(type, size, required_ext, inline_ext,
+				     alignsize, &bb);
+	if (ret)
+		return ERR_PTR(ret);
 
 	if (bb) {
 		bh = malloc(sizeof(struct erofs_buffer_head));
 		if (!bh)
 			return ERR_PTR(-ENOMEM);
-		goto found;
-	}
-
-	/* allocate a new buffer block */
-	if (used0 > EROFS_BLKSIZ)
-		return ERR_PTR(-ENOSPC);
-
-	bb = malloc(sizeof(struct erofs_buffer_block));
-	if (!bb)
-		return ERR_PTR(-ENOMEM);
+	} else {
+		/* get a new buffer block instead */
+		bb = malloc(sizeof(struct erofs_buffer_block));
+		if (!bb)
+			return ERR_PTR(-ENOMEM);
 
-	bb->type = type;
-	bb->blkaddr = NULL_ADDR;
-	bb->buffers.off = 0;
-	init_list_head(&bb->buffers.list);
-	list_add_tail(&bb->list, &blkh.list);
+		bb->type = type;
+		bb->blkaddr = NULL_ADDR;
+		bb->buffers.off = 0;
+		init_list_head(&bb->buffers.list);
+		list_add_tail(&bb->list, &blkh.list);
 
-	bh = malloc(sizeof(struct erofs_buffer_head));
-	if (!bh) {
-		free(bb);
-		return ERR_PTR(-ENOMEM);
+		bh = malloc(sizeof(struct erofs_buffer_head));
+		if (!bh) {
+			free(bb);
+			return ERR_PTR(-ENOMEM);
+		}
 	}
-found:
+
 	ret = __erofs_battach(bb, bh, size, alignsize,
 			      required_ext + inline_ext, false);
 	if (ret < 0)
Signature · 1 line
2.24.0
author Reviewed-by Li Guifu on v7 2/3
#5 lore raw

Re: [PATCH v7 2/3] erofs-utils: introduce erofs_bfind_for_attach()

Quoted 10 lines · Gao Xiang via Linux-erofs

From: Gao Xiang <[email protected]>

Seperate erofs_balloc() to make the logic more clearer.

---
 lib/cache.c | 81 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 31 deletions(-)

It looks good

Thanks,

#6 lore raw

PATCH v7 3/3 erofs-utils: optimize buffer allocation logic

In reply to Gao Xiang via Linux-erofs

From: Hu Weiwen <[email protected]>

When using EROFS to pack our dataset which consists of millions of files, mkfs.erofs is very slow compared with mksquashfs.

The bottleneck is erofs_balloc' and erofs_mapbh' function, which iterate over all previously allocated buffer blocks, making the complexity of the algrithm O(N^2) where N is the number of files.

With this patch:

* global last_mapped_block' is mantained to avoid full scan in erofs_mapbh` function.

* global `mapped_buckets' maintains a list of already mapped buffer blocks for each type and for each possible used bytes in the last EROFS_BLKSIZ. Then it is used to identify the most suitable blocks in future `erofs_balloc', avoiding full scan. Note that not-mapped (and the last mapped) blocks can be expended, so we deal with them separately.

When I test it with ImageNet dataset (1.33M files, 147GiB), it takes about 4 hours. Most time is spent on IO.

---
 include/erofs/cache.h |   1 +
 lib/cache.c           | 105 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 93 insertions(+), 13 deletions(-)
include/erofs/cache.h +1 -0
index f8dff67b9736..611ca5b8432b 100644
@@ -39,6 +39,7 @@ struct erofs_buffer_head {
 
 struct erofs_buffer_block {
 	struct list_head list;
+	struct list_head mapped_list;
 
 	erofs_blk_t blkaddr;
 	int type;
lib/cache.c +92 -13

186 lines not on this page · raw message

Signature · 1 line
2.24.0
author Reviewed-by Li Guifu on v7 3/3
#7 lore raw
Quoted 23 lines · Gao Xiang via Linux-erofs

From: Hu Weiwen <[email protected]>

When using EROFS to pack our dataset which consists of millions of files, mkfs.erofs is very slow compared with mksquashfs.

The bottleneck is erofs_balloc' and erofs_mapbh' function, which iterate over all previously allocated buffer blocks, making the complexity of the algrithm O(N^2) where N is the number of files.

With this patch:

* global last_mapped_block' is mantained to avoid full scan in erofs_mapbh` function.

* global `mapped_buckets' maintains a list of already mapped buffer blocks for each type and for each possible used bytes in the last EROFS_BLKSIZ. Then it is used to identify the most suitable blocks in future `erofs_balloc', avoiding full scan. Note that not-mapped (and the last mapped) blocks can be expended, so we deal with them separately.

When I test it with ImageNet dataset (1.33M files, 147GiB), it takes about 4 hours. Most time is spent on IO.

Quoted 8 lines
---
 include/erofs/cache.h |   1 +
 lib/cache.c           | 105 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 93 insertions(+), 13 deletions(-)

It looks good

Thanks,