Skip to contentSkip to conversation

Opportunistically making files sparse

PATCH Superseded Sandeep Dhavale via Linux-erofs 2 participants 4 messages Apr 3, 2024 – Apr 4, 2024 Library code

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Opportunistically making files sparse

Hi,
We noticed that in android if you build erofs images with ELFs which have higher alignment say 16K or 64K, there was a considerable increase in the size of the uncompressed erofs image. The size increase could be mitigated with -Ededupe or --chunksize=4096 but that still results in lot of redundant disk IOs during file read as all the zero blocks are mapped to a single block on disk. Treating data blocks filled with zeros as hole will save the diskspace and also will save us lot of disk IOs during read.

Using EROFS tracepoints for the image built without the fix

md5sum-7535    [001] ..... 620668.748558: erofs_map_blocks_enter: dev = (7,0), nid = 60, la 364544 llen 45056 flags RAW
md5sum-7535    [001] ..... 620668.748559: erofs_map_blocks_exit: dev = (7,0), nid = 60, flags RAW la 364544 pa 40960 llen 4096 plen 4096 mflags M ret 0
md5sum-7535    [001] ..... 620668.748560: erofs_map_blocks_enter: dev = (7,0), nid = 60, la 368640 llen 40960 flags RAW
md5sum-7535    [001] ..... 620668.748560: erofs_map_blocks_exit: dev = (7,0), nid = 60, flags RAW la 368640 pa 40960 llen 4096 plen 4096 mflags M ret 0
md5sum-7535    [001] ..... 620668.748561: erofs_map_blocks_enter: dev = (7,0), nid = 60, la 372736 llen 36864 flags RAW
md5sum-7535    [001] ..... 620668.748561: erofs_map_blocks_exit: dev = (7,0), nid = 60, flags RAW la 372736 pa 40960 llen 4096 plen 4096 mflags M ret 0
md5sum-7535    [001] ..... 620668.748562: erofs_map_blocks_enter: dev = (7,0), nid = 60, la 376832 llen 32768 flags RAW

As you can see, all the reads are being redirected to read the same pa 40960. Also this causes fragmentation.

Using EROFS tracepoints for the image built with detection of zero blocks

md5sum-7496    [000] ..... 620150.387246: erofs_map_blocks_enter: dev = (7,0), nid = 60, la 0 llen 65536 flags RAW
md5sum-7496    [000] ..... 620150.387249: erofs_map_blocks_exit: dev = (7,0), nid = 60, flags RAW la 0 pa 0 llen 262144 plen 262144 mflags  ret 0
md5sum-7496    [000] ..... 620150.387358: erofs_map_blocks_enter: dev = (7,0), nid = 60, la 65536 llen 131072 flags RAW
md5sum-7496    [000] ..... 620150.387358: erofs_map_blocks_exit: dev = (7,0), nid = 60, flags RAW la 0 pa 0 llen 262144 plen 262144 mflags  ret 0
md5sum-7496    [000] ..... 620150.387460: erofs_map_blocks_enter: dev = (7,0), nid = 60, la 196608 llen 212992 flags RAW

I think this optimization has wins on diskspace and IO cost so its better to be default than enable conditionally with --sparse flag.

Thanks,
Sandeep.

PS: This patch is based on erofs-utils.git/experimental as it builds on the previous fix of minextblks at
https://lore.kernel.org/all/[email protected]/
which is not in erofs-utils.git/dev yet.

Sandeep Dhavale (1):
erofs-utils: lib: treat data blocks filled with 0s as a hole

 lib/blobchunk.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
Signature · 1 line
2.44.0.478.gd926399ef9-goog
#1 lore raw

PATCH 1/1 erofs-utils: lib: treat data blocks filled with 0s as a hole

Add optimization to treat data blocks filled with 0s as a hole. Even though diskspace savings are comparable to chunk based or dedupe, having no block assigned saves us redundant disk IOs during read.

This patch detects if the block is filled with zeros and marks chunk as erofs_holechunk so there is no physical block assigned.

---
 lib/blobchunk.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
lib/blobchunk.c +24 -1
index 641e3d4..8535058 100644
@@ -232,6 +232,21 @@ static void erofs_update_minextblks(struct erofs_sb_info *sbi,
 		*minextblks = lb;
 }
 
+static bool erofs_is_buf_zeros(void *buf, unsigned long len)
+{
+	int i, words;
+	const unsigned long *words_buf = buf;
+	words = len / sizeof(unsigned long);
+
+	DBG_BUGON(len % sizeof(unsigned long));
+
+	for (i = 0; i < words; i++) {
+		if (words_buf[i])
+			return false;
+	}
+	return true;
+}
+
 int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 				  erofs_off_t startoff)
 {
@@ -323,7 +338,15 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 			ret = -EIO;
 			goto err;
 		}
-
+		if (len == chunksize && erofs_is_buf_zeros(chunkdata, len)) {
+			/* if data is all zeros, treat this block as hole */
+			*(void **)idx++ = &erofs_holechunk;
+			erofs_update_minextblks(sbi, interval_start, pos,
+						&minextblks);
+			interval_start = pos + len;
+			lastch = NULL;
+			continue;
+		}
 		chunk = erofs_blob_getchunk(sbi, chunkdata, len);
 		if (IS_ERR(chunk)) {
 			ret = PTR_ERR(chunk);
Signature · 1 line
2.44.0.478.gd926399ef9-goog
#2 lore raw

Re: [PATCH 1/1] erofs-utils: lib: treat data blocks filled with 0s as a hole

Hi Sandeep,

Quoted 43 lines · Sandeep Dhavale

Add optimization to treat data blocks filled with 0s as a hole. Even though diskspace savings are comparable to chunk based or dedupe, having no block assigned saves us redundant disk IOs during read.

This patch detects if the block is filled with zeros and marks chunk as erofs_holechunk so there is no physical block assigned.

---
  lib/blobchunk.c | 25 ++++++++++++++++++++++++-
  1 file changed, 24 insertions(+), 1 deletion(-)
lib/blobchunk.c +15 -1
index 641e3d4..8535058 100644
@@ -232,6 +232,21 @@ static void erofs_update_minextblks(struct erofs_sb_info *sbi,
  		*minextblks = lb;
  }
 
+static bool erofs_is_buf_zeros(void *buf, unsigned long len)
+{
+	int i, words;
+	const unsigned long *words_buf = buf;
+	words = len / sizeof(unsigned long);
+
+	DBG_BUGON(len % sizeof(unsigned long));
+
+	for (i = 0; i < words; i++) {
+		if (words_buf[i])
+			return false;
+	}
+	return true;
+}
+
  int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
  				  erofs_off_t startoff)
  {
@@ -323,7 +338,15 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
  			ret = -EIO;
  			goto err;
  		}
-

lib/blobchunk.c int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd, · line 350

Quoted 10 lines
lib/blobchunk.c +9 -0
+		if (len == chunksize && erofs_is_buf_zeros(chunkdata, len)) {
+			/* if data is all zeros, treat this block as hole */
+			*(void **)idx++ = &erofs_holechunk;
+			erofs_update_minextblks(sbi, interval_start, pos,
+						&minextblks);
+			interval_start = pos + len;
+			lastch = NULL;
+			continue;
+		}
  		chunk = erofs_blob_getchunk(sbi, chunkdata, len);

Yes, it's a valid opt. Yet, I guess we could calculate the unique hash value of all zeroes (with chunksize) in advance (e.g. when initialization).

And compare the buf hash and the all-zeroed hash in erofs_blob_getchunk(), if they are the same, let's just compare all data then (or not compare since it's little chance to have such collision. In that case, erofs_blob_getchunk returns erofs_holechunk.

Does it sound a good idea to you?

Thanks,
Gao Xiang

Quoted 2 lines
lib/blobchunk.c +0 -0
  		if (IS_ERR(chunk)) {
  			ret = PTR_ERR(chunk);
#3 lore raw

Re: [PATCH 1/1] erofs-utils: lib: treat data blocks filled with 0s as a hole

Quoted 61 lines · Gao Xiang

Hi Sandeep,

Quoted 43 lines · Sandeep Dhavale

Add optimization to treat data blocks filled with 0s as a hole. Even though diskspace savings are comparable to chunk based or dedupe, having no block assigned saves us redundant disk IOs during read.

This patch detects if the block is filled with zeros and marks chunk as erofs_holechunk so there is no physical block assigned.

---
  lib/blobchunk.c | 25 ++++++++++++++++++++++++-
  1 file changed, 24 insertions(+), 1 deletion(-)
lib/blobchunk.c +15 -1
index 641e3d4..8535058 100644
@@ -232,6 +232,21 @@ static void erofs_update_minextblks(struct erofs_sb_info *sbi,
              *minextblks = lb;
  }
 
+static bool erofs_is_buf_zeros(void *buf, unsigned long len)
+{
+     int i, words;
+     const unsigned long *words_buf = buf;
+     words = len / sizeof(unsigned long);
+
+     DBG_BUGON(len % sizeof(unsigned long));
+
+     for (i = 0; i < words; i++) {
+             if (words_buf[i])
+                     return false;
+     }
+     return true;
+}
+
  int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
                                erofs_off_t startoff)
  {
@@ -323,7 +338,15 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
                      ret = -EIO;
                      goto err;
              }
-

lib/blobchunk.c int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd, · line 350

Quoted 10 lines
lib/blobchunk.c +9 -0
+             if (len == chunksize && erofs_is_buf_zeros(chunkdata, len)) {
+                     /* if data is all zeros, treat this block as hole */
+                     *(void **)idx++ = &erofs_holechunk;
+                     erofs_update_minextblks(sbi, interval_start, pos,
+                                             &minextblks);
+                     interval_start = pos + len;
+                     lastch = NULL;
+                     continue;
+             }
              chunk = erofs_blob_getchunk(sbi, chunkdata, len);

Yes, it's a valid opt. Yet, I guess we could calculate the unique hash value of all zeroes (with chunksize) in advance (e.g. when initialization).

Quoted 7 lines

And compare the buf hash and the all-zeroed hash in erofs_blob_getchunk(), if they are the same, let's just compare all data then (or not compare since it's little chance to have such collision. In that case, erofs_blob_getchunk returns erofs_holechunk.

Does it sound a good idea to you?

Hi Gao,
Sure, I will re-work this in v2.

Thanks,
Sandeep.

Quoted 5 lines

Thanks,
Gao Xiang

Quoted 2 lines
lib/blobchunk.c +0 -0
              if (IS_ERR(chunk)) {
                      ret = PTR_ERR(chunk);