Skip to contentSkip to conversation

erofs-utils: introduce multi-threading framework

PATCH In review Gao Xiang 3 participants 10 messages Mar 15, 2024 – Mar 21, 2024

Subsystems (2)
lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH v7 1/5 erofs-utils: introduce multi-threading framework

From: Yifan Zhao <[email protected]>

Add a workqueue implementation for multi-threading support inspired by xfsprogs.

---
 configure.ac              |  16 +++++
 include/erofs/internal.h  |   3 +
 include/erofs/workqueue.h |  34 +++++++++++
 lib/Makefile.am           |   4 ++
 lib/workqueue.c           | 123 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 180 insertions(+)
 create mode 100644 include/erofs/workqueue.h
 create mode 100644 lib/workqueue.c
configure.ac +16 -0
index 4b59230..3ccd6bb 100644
@@ -96,6 +96,14 @@ AC_DEFUN([EROFS_UTILS_PARSE_DIRECTORY],
 
 AC_ARG_VAR([MAX_BLOCK_SIZE], [The maximum block size which erofs-utils supports])
 
+AC_MSG_CHECKING([whether to enable multi-threading support])
+AC_ARG_ENABLE([multithreading],
+    AS_HELP_STRING([--enable-multithreading],
+                   [enable multi-threading support @<:@default=no@:>@]),
+    [enable_multithreading="$enableval"],
+    [enable_multithreading="no"])
+AC_MSG_RESULT([$enable_multithreading])
+
 AC_ARG_ENABLE([debug],
     [AS_HELP_STRING([--enable-debug],
                     [enable debugging mode @<:@default=no@:>@])],
@@ -280,6 +288,13 @@ AS_IF([test "x$MAX_BLOCK_SIZE" = "x"], [
                              [erofs_cv_max_block_size=4096]))
 ], [erofs_cv_max_block_size=$MAX_BLOCK_SIZE])
 
+# Configure multi-threading support
+AS_IF([test "x$enable_multithreading" != "xno"], [
+    AC_CHECK_HEADERS([pthread.h])
+    AC_CHECK_LIB([pthread], [pthread_mutex_lock], [], AC_MSG_ERROR([libpthread is required for multi-threaded build]))
+    AC_DEFINE(EROFS_MT_ENABLED, 1, [Enable multi-threading support])
+], [])
+
 # Configure debug mode
 AS_IF([test "x$enable_debug" != "xno"], [], [
   dnl Turn off all assert checking.
@@ -471,6 +486,7 @@ AS_IF([test "x$enable_fuzzing" != "xyes"], [], [
 AM_CONDITIONAL([ENABLE_FUZZING], [test "x${enable_fuzzing}" = "xyes"])
 
 # Set up needed symbols, conditionals and compiler/linker flags
+AM_CONDITIONAL([ENABLE_EROFS_MT], [test "x${enable_multithreading}" != "xno"])
 AM_CONDITIONAL([ENABLE_LZ4], [test "x${have_lz4}" = "xyes"])
 AM_CONDITIONAL([ENABLE_LZ4HC], [test "x${have_lz4hc}" = "xyes"])
 AM_CONDITIONAL([ENABLE_FUSE], [test "x${have_fuse}" = "xyes"])
include/erofs/internal.h +3 -0
index 5e968d6..4cd2059 100644
@@ -22,6 +22,9 @@ typedef unsigned short umode_t;
 #include <sys/types.h> /* for off_t definition */
 #include <sys/stat.h> /* for S_ISCHR definition */
 #include <stdio.h>
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
 
 #ifndef PATH_MAX
 #define PATH_MAX        4096    /* # chars in a path name including nul */
include/erofs/workqueue.h new file +34 -0
new file mode 100644
index 0000000..36037c3
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_WORKQUEUE_H
+#define __EROFS_WORKQUEUE_H
+
+#include "internal.h"
+
+struct erofs_workqueue;
+
+typedef void *(*erofs_wq_func_t)(struct erofs_workqueue *, void *);
+
+struct erofs_work {
+	struct erofs_work *next;
+	void (*fn)(struct erofs_work *work, void *tlsp);
+};
+
+struct erofs_workqueue {
+	struct erofs_work *head, *tail;
+	pthread_mutex_t lock;
+	pthread_cond_t cond_empty;
+	pthread_cond_t cond_full;
+	pthread_t *workers;
+	unsigned int nworker;
+	unsigned int max_jobs;
+	unsigned int job_count;
+	bool shutdown;
+	erofs_wq_func_t on_start, on_exit;
+};
+
+int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
+			  unsigned int max_jobs, erofs_wq_func_t on_start,
+			  erofs_wq_func_t on_exit);
+int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work);
+int erofs_destroy_workqueue(struct erofs_workqueue *wq);
+#endif
lib/Makefile.am +4 -0
index 54b9c9c..b3bea74 100644
@@ -53,3 +53,7 @@ liberofs_la_SOURCES += kite_deflate.c compressor_deflate.c
 if ENABLE_LIBDEFLATE
 liberofs_la_SOURCES += compressor_libdeflate.c
 endif
+if ENABLE_EROFS_MT
+liberofs_la_LDFLAGS = -lpthread
+liberofs_la_SOURCES += workqueue.c
+endif
lib/workqueue.c new file +123 -0

123 lines not on this page · raw message

Signature · 1 line
2.39.3
author
#1 lore raw

PATCH v7 2/5 erofs-utils: add a helper to get available processors

In order to prepare for multi-threaded decompression.

---
 configure.ac           |  1 +
 include/erofs/config.h |  1 +
 lib/config.c           | 12 ++++++++++++
 3 files changed, 14 insertions(+)
configure.ac +1 -0
index 3ccd6bb..2e69260 100644
@@ -256,6 +256,7 @@ AC_CHECK_FUNCS(m4_flatten([
 	strerror
 	strrchr
 	strtoull
+	sysconf
 	tmpfile64
 	utimensat]))
 
include/erofs/config.h +1 -0
index eecf575..73e3ac2 100644
@@ -109,6 +109,7 @@ static inline int erofs_selabel_open(const char *file_contexts)
 
 void erofs_update_progressinfo(const char *fmt, ...);
 char *erofs_trim_for_progressinfo(const char *str, int placeholder);
+unsigned int erofs_get_available_processors(void);
 
 #ifdef __cplusplus
 }
lib/config.c +12 -0
index 1096cd1..947a183 100644
@@ -14,6 +14,9 @@
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
 struct erofs_configure cfg;
 struct erofs_sb_info sbi;
@@ -177,3 +180,12 @@ void erofs_update_progressinfo(const char *fmt, ...)
 	fputs(msg, stdout);
 	fputc('\n', stdout);
 }
+
+unsigned int erofs_get_available_processors(void)
+{
+#if defined(HAVE_UNISTD_H) && defined(HAVE_SYSCONF)
+	return sysconf(_SC_NPROCESSORS_ONLN);
+#else
+	return 0;
+#endif
+}
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH v7 2/5] erofs-utils: add a helper to get available processors

Quoted 1 line · Gao Xiang

In order to prepare for multi-threaded decompression.

multi-threaded compression.

Thanks,
Jianan

Quoted 59 lines
---
  configure.ac           |  1 +
  include/erofs/config.h |  1 +
  lib/config.c           | 12 ++++++++++++
  3 files changed, 14 insertions(+)
configure.ac +1 -0
index 3ccd6bb..2e69260 100644
@@ -256,6 +256,7 @@ AC_CHECK_FUNCS(m4_flatten([
  	strerror
  	strrchr
  	strtoull
+	sysconf
  	tmpfile64
  	utimensat]))
 
include/erofs/config.h +1 -0
index eecf575..73e3ac2 100644
@@ -109,6 +109,7 @@ static inline int erofs_selabel_open(const char *file_contexts)
 
  void erofs_update_progressinfo(const char *fmt, ...);
  char *erofs_trim_for_progressinfo(const char *str, int placeholder);
+unsigned int erofs_get_available_processors(void);
 
  #ifdef __cplusplus
  }
lib/config.c +12 -0
index 1096cd1..947a183 100644
@@ -14,6 +14,9 @@
  #ifdef HAVE_SYS_IOCTL_H
  #include <sys/ioctl.h>
  #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
  struct erofs_configure cfg;
  struct erofs_sb_info sbi;
@@ -177,3 +180,12 @@ void erofs_update_progressinfo(const char *fmt, ...)
  	fputs(msg, stdout);
  	fputc('\n', stdout);
  }
+
+unsigned int erofs_get_available_processors(void)
+{
+#if defined(HAVE_UNISTD_H) && defined(HAVE_SYSCONF)
+	return sysconf(_SC_NPROCESSORS_ONLN);
+#else
+	return 0;
+#endif
+}
author
#3 lore raw

Re: [PATCH v7 2/5] erofs-utils: add a helper to get available processors

Quoted 5 lines · Huang Jianan
Quoted 1 line · Gao Xiang

In order to prepare for multi-threaded decompression.

multi-threaded compression.

It already merged into -dev. I would not update this since it's a minor stuff. Let's work on inter-file stuffs.

Thanks,
Gao Xiang

author
#4 lore raw

PATCH v7 3/5 erofs-utils: mkfs: add --workers=# parameter

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

This patch introduces --workers=# parameter for the incoming multi-threaded compression support.

It also introduces a concept called segment size to split large inodes for multi-threaded compression, which has the fixed value 16MiB and cannot be modified for now.

---
 include/erofs/config.h |  4 ++++
 lib/config.c           |  4 ++++
 mkfs/main.c            | 23 +++++++++++++++++++++++
 3 files changed, 31 insertions(+)
include/erofs/config.h +4 -0
index 73e3ac2..d2f91ff 100644
@@ -75,6 +75,10 @@ struct erofs_configure {
 	char c_force_chunkformat;
 	/* < 0, xattr disabled and INT_MAX, always use inline xattrs */
 	int c_inline_xattr_tolerance;
+#ifdef EROFS_MT_ENABLED
+	u64 c_segment_size;
+	u32 c_mt_workers;
+#endif
 
 	u32 c_pclusterblks_max, c_pclusterblks_def, c_pclusterblks_packed;
 	u32 c_max_decompressed_extent_bytes;
lib/config.c +4 -0
index 947a183..2530274 100644
@@ -38,6 +38,10 @@ void erofs_init_configure(void)
 	cfg.c_pclusterblks_max = 1;
 	cfg.c_pclusterblks_def = 1;
 	cfg.c_max_decompressed_extent_bytes = -1;
+#ifdef EROFS_MT_ENABLED
+	cfg.c_segment_size = 16ULL * 1024 * 1024;
+	cfg.c_mt_workers = 1;
+#endif
 
 	erofs_stdout_tty = isatty(STDOUT_FILENO);
 }
mkfs/main.c +23 -0
index 8a68a72..126a049 100644
@@ -77,6 +77,9 @@ static struct option long_options[] = {
 #ifdef HAVE_LIBLZMA
 	{"unlzma", optional_argument, NULL, 519},
 	{"unxz", optional_argument, NULL, 519},
+#endif
+#ifdef EROFS_MT_ENABLED
+	{"workers", required_argument, NULL, 520},
 #endif
 	{0, 0, 0, 0},
 };
@@ -178,6 +181,9 @@ static void usage(int argc, char **argv)
 #ifdef HAVE_LIBLZMA
 		" --unxz[=X]            try to filter the tarball stream through xz/lzma/lzip\n"
 		"                       (and optionally dump the raw stream to X together)\n"
+#endif
+#ifdef EROFS_MT_ENABLED
+		" --workers=#           set the number of worker threads to # (default=1)\n"
 #endif
 		" --xattr-prefix=X      X=extra xattr name prefix\n"
 		" --mount-point=X       X=prefix of target fs path (default: /)\n"
@@ -660,6 +666,23 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 				erofstar.dumpfile = strdup(optarg);
 			tarerofs_decoder = EROFS_IOS_DECODER_GZIP + (opt - 518);
 			break;
+#ifdef EROFS_MT_ENABLED
+		case 520: {
+			unsigned int processors;
+
+			cfg.c_mt_workers = strtoul(optarg, &endptr, 0);
+			if (errno || *endptr != '\0') {
+				erofs_err("invalid worker number %s", optarg);
+				return -EINVAL;
+			}
+
+			processors = erofs_get_available_processors();
+			if (cfg.c_mt_workers > processors)
+				erofs_warn("the number of workers %d is more than the number of processors %d, performance may be impacted.",
+					   cfg.c_mt_workers, processors);
+			break;
+		}
+#endif
 		case 'V':
 			version();
 			exit(0);
Signature · 1 line
2.39.3
author
#5 lore raw

PATCH v7 4/5 erofs-utils: lib: introduce atomic operations

In reply to Gao Xiang

Add some helpers (relaxed semantics) in order to prepare for the upcoming multi-threaded support.

For example, compressor may be initialized more than once in different worker threads, resulting in noisy warnings.

This patch makes sure that each message will be printed only once by adding __warnonce atomic booleans to each erofs_compressor_init().

---
 include/erofs/atomic.h      | 28 ++++++++++++++++++++++++++++
 lib/compressor_deflate.c    | 11 ++++++++---
 lib/compressor_libdeflate.c |  6 +++++-
 lib/compressor_liblzma.c    |  5 ++++-
 4 files changed, 45 insertions(+), 5 deletions(-)
 create mode 100644 include/erofs/atomic.h
include/erofs/atomic.h new file +28 -0
new file mode 100644
index 0000000..214cdb1
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+/*
+ * Copyright (C) 2024 Alibaba Cloud
+ */
+#ifndef __EROFS_ATOMIC_H
+#define __EROFS_ATOMIC_H
+
+/*
+ * Just use GCC/clang built-in functions for now
+ * See: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
+ */
+typedef unsigned long erofs_atomic_t;
+typedef char erofs_atomic_bool_t;
+
+#define erofs_atomic_read(ptr) ({ \
+	typeof(*ptr) __n;    \
+	__atomic_load(ptr, &__n, __ATOMIC_RELAXED); \
+__n;})
+
+#define erofs_atomic_set(ptr, n) do { \
+	typeof(*ptr) __n = (n);    \
+	__atomic_store(ptr, &__n, __ATOMIC_RELAXED); \
+} while(0)
+
+#define erofs_atomic_test_and_set(ptr) \
+	__atomic_test_and_set(ptr, __ATOMIC_RELAXED)
+
+#endif
lib/compressor_deflate.c +8 -3
index 8629415..e482224 100644
@@ -7,6 +7,7 @@
 #include "erofs/print.h"
 #include "erofs/config.h"
 #include "compressor.h"
+#include "erofs/atomic.h"
 
 void *kite_deflate_init(int level, unsigned int dict_size);
 void kite_deflate_end(void *s);
@@ -36,6 +37,8 @@ static int compressor_deflate_exit(struct erofs_compress *c)
 
 static int compressor_deflate_init(struct erofs_compress *c)
 {
+	static erofs_atomic_bool_t __warnonce;
+
 	if (c->private_data) {
 		kite_deflate_end(c->private_data);
 		c->private_data = NULL;
@@ -44,9 +47,11 @@ static int compressor_deflate_init(struct erofs_compress *c)
 	if (IS_ERR_VALUE(c->private_data))
 		return PTR_ERR(c->private_data);
 
-	erofs_warn("EXPERIMENTAL DEFLATE algorithm in use. Use at your own risk!");
-	erofs_warn("*Carefully* check filesystem data correctness to avoid corruption!");
-	erofs_warn("Please send a report to <[email protected]> if something is wrong.");
+	if (!erofs_atomic_test_and_set(&__warnonce)) {
+		erofs_warn("EXPERIMENTAL DEFLATE algorithm in use. Use at your own risk!");
+		erofs_warn("*Carefully* check filesystem data correctness to avoid corruption!");
+		erofs_warn("Please send a report to <[email protected]> if something is wrong.");
+	}
 	return 0;
 }
 
lib/compressor_libdeflate.c +5 -1
index 62d93f7..14cbce4 100644
@@ -4,6 +4,7 @@
 #include "erofs/config.h"
 #include <libdeflate.h>
 #include "compressor.h"
+#include "erofs/atomic.h"
 
 static int libdeflate_compress_destsize(const struct erofs_compress *c,
 				        const void *src, unsigned int *srcsize,
@@ -82,12 +83,15 @@ static int compressor_libdeflate_exit(struct erofs_compress *c)
 
 static int compressor_libdeflate_init(struct erofs_compress *c)
 {
+	static erofs_atomic_bool_t __warnonce;
+
 	libdeflate_free_compressor(c->private_data);
 	c->private_data = libdeflate_alloc_compressor(c->compression_level);
 	if (!c->private_data)
 		return -ENOMEM;
 
-	erofs_warn("EXPERIMENTAL libdeflate compressor in use. Use at your own risk!");
+	if (!erofs_atomic_test_and_set(&__warnonce))
+		erofs_warn("EXPERIMENTAL libdeflate compressor in use. Use at your own risk!");
 	return 0;
 }
 
lib/compressor_liblzma.c +4 -1
index 712f44f..2f19a93 100644
@@ -9,6 +9,7 @@
 #include "erofs/config.h"
 #include "erofs/print.h"
 #include "erofs/internal.h"
+#include "erofs/atomic.h"
 #include "compressor.h"
 
 struct erofs_liblzma_context {
@@ -85,6 +86,7 @@ static int erofs_compressor_liblzma_init(struct erofs_compress *c)
 {
 	struct erofs_liblzma_context *ctx;
 	u32 preset;
+	static erofs_atomic_bool_t __warnonce;
 
 	ctx = malloc(sizeof(*ctx));
 	if (!ctx)
@@ -103,7 +105,8 @@ static int erofs_compressor_liblzma_init(struct erofs_compress *c)
 	ctx->opt.dict_size = c->dict_size;
 
 	c->private_data = ctx;
-	erofs_warn("It may take a longer time since MicroLZMA is still single-threaded for now.");
+	if (!erofs_atomic_test_and_set(&__warnonce))
+		erofs_warn("It may take a longer time since MicroLZMA is still single-threaded for now.");
 	return 0;
 }
 
Signature · 1 line
2.39.3
author
#6 lore raw

PATCH v7 5/5 erofs-utils: mkfs: introduce inner-file multi-threaded compression

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

Currently, the creation of EROFS compressed image creation is single-threaded, which suffers from performance issues. This patch attempts to address it by compressing the large file in parallel.

Specifically, each input file larger than 16MB is splited into segments, and each worker thread compresses a segment as if it were a separate file. Finally, the main thread merges all the compressed segments.

Multi-threaded compression is not compatible with -Ededupe, -E(all-)fragments and -Eztailpacking for now.

---
v7:
- support -Eztailpacking;
- wq_private -> wq_tls;
- minor updates.

 include/erofs/compress.h |   3 +-
 lib/compress.c           | 548 ++++++++++++++++++++++++++++++++-------
 lib/compressor.c         |   2 +
 mkfs/main.c              |   8 +-
 4 files changed, 464 insertions(+), 97 deletions(-)
include/erofs/compress.h +2 -1
index b3272f7..3253611 100644
@@ -14,7 +14,8 @@ extern "C"
 
 #include "internal.h"
 
-#define EROFS_CONFIG_COMPR_MAX_SZ           (4000 * 1024)
+#define EROFS_CONFIG_COMPR_MAX_SZ	(4000 * 1024)
+#define Z_EROFS_COMPR_QUEUE_SZ		(EROFS_CONFIG_COMPR_MAX_SZ * 2)
 
 void z_erofs_drop_inline_pcluster(struct erofs_inode *inode);
 int erofs_write_compressed_file(struct erofs_inode *inode, int fd);
lib/compress.c +453 -95

794 lines not on this page · raw message

lib/compressor.c +2 -0
index 58eae2a..175259e 100644
@@ -86,6 +86,8 @@ int erofs_compressor_init(struct erofs_sb_info *sbi, struct erofs_compress *c,
 
 	/* should be written in "minimum compression ratio * 100" */
 	c->compress_threshold = 100;
+	c->compression_level = -1;
+	c->dict_size = 0;
 
 	if (!alg_name) {
 		c->alg = NULL;
mkfs/main.c +7 -1
index 126a049..5dbaf9f 100644
@@ -678,7 +678,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 
 			processors = erofs_get_available_processors();
 			if (cfg.c_mt_workers > processors)
-				erofs_warn("the number of workers %d is more than the number of processors %d, performance may be impacted.",
+				erofs_warn("%d workers exceed %d processors, potentially impacting performance.",
 					   cfg.c_mt_workers, processors);
 			break;
 		}
@@ -838,6 +838,12 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		}
 		cfg.c_pclusterblks_packed = pclustersize_packed >> sbi.blkszbits;
 	}
+#ifdef EROFS_MT_ENABLED
+	if (cfg.c_mt_workers > 1 && (cfg.c_dedupe || cfg.c_fragments)) {
+		erofs_warn("Note that dedupe/fragments are NOT supported in multi-threaded mode for now, reseting --workers=1.");
+		cfg.c_mt_workers = 1;
+	}
+#endif
 	return 0;
 }
 
Signature · 1 line
2.39.3
author
#7 lore raw

Re: [PATCH v7 5/5] erofs-utils: mkfs: introduce inner-file multi-threaded compression

Quoted 13 lines · Gao Xiang

From: Yifan Zhao <[email protected]>

Currently, the creation of EROFS compressed image creation is single-threaded, which suffers from performance issues. This patch attempts to address it by compressing the large file in parallel.

Specifically, each input file larger than 16MB is splited into segments, and each worker thread compresses a segment as if it were a separate file. Finally, the main thread merges all the compressed segments.

Multi-threaded compression is not compatible with -Ededupe, -E(all-)fragments and -Eztailpacking for now.

Quoted 6 lines

---
v7:
- support -Eztailpacking;

Apply the following diff to fix this:

lib/compress.c +4 -4
index 0d796c8..7ad48b0 100644
@@ -509,10 +509,10 @@ static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,
 	struct erofs_compress *const h = ctx->chandle;
 	unsigned int len = ctx->tail - ctx->head;
 	bool is_packed_inode = erofs_is_packed_inode(inode);
-	bool final = !ctx->remaining;
-	bool may_packing = (cfg.c_fragments && final && !is_packed_inode &&
-			    !z_erofs_mt_enabled);
-	bool may_inline = (cfg.c_ztailpacking && final && !may_packing);
+	bool tsg = (ctx->seg_idx + 1 >= ctx->seg_num), final = !ctx->remaining;
+	bool may_packing = (cfg.c_fragments && tsg && final &&
+			    !is_packed_inode && !z_erofs_mt_enabled);
+	bool may_inline = (cfg.c_ztailpacking && tsg && final && !may_packing);
 	unsigned int compressedsize;
 	int ret;
 

Thanks,
Gao Xiang

#8 lore raw

Re: [PATCH v7 5/5] erofs-utils: mkfs: introduce inner-file multi-threaded compression

In reply to Gao Xiang

I think it is easier to understand the source code if the names of variables and pointers in the same structure are unified.

struct z_erofs_compress_ictx inode_ctx; // i stands for inode? struct z_erofs_compress_ictx *ictx;

struct z_erofs_compress_sctx seg_ctx;
struct z_erofs_compress_sctx *sctx;

2024年3月15日(金) 10:11 Gao Xiang <[email protected]>:

Quoted 907 lines

From: Yifan Zhao <[email protected]>

Currently, the creation of EROFS compressed image creation is single-threaded, which suffers from performance issues. This patch attempts to address it by compressing the large file in parallel.

Specifically, each input file larger than 16MB is splited into segments, and each worker thread compresses a segment as if it were a separate file. Finally, the main thread merges all the compressed segments.

Multi-threaded compression is not compatible with -Ededupe, -E(all-)fragments and -Eztailpacking for now.

---
v7:
- support -Eztailpacking;
- wq_private -> wq_tls;
- minor updates.

 include/erofs/compress.h |   3 +-
 lib/compress.c           | 548 ++++++++++++++++++++++++++++++++-------
 lib/compressor.c         |   2 +
 mkfs/main.c              |   8 +-
 4 files changed, 464 insertions(+), 97 deletions(-)
include/erofs/compress.h +2 -1
index b3272f7..3253611 100644
@@ -14,7 +14,8 @@ extern "C"
 
 #include "internal.h"
 
-#define EROFS_CONFIG_COMPR_MAX_SZ           (4000 * 1024)
+#define EROFS_CONFIG_COMPR_MAX_SZ      (4000 * 1024)
+#define Z_EROFS_COMPR_QUEUE_SZ         (EROFS_CONFIG_COMPR_MAX_SZ * 2)
 
 void z_erofs_drop_inline_pcluster(struct erofs_inode *inode);
 int erofs_write_compressed_file(struct erofs_inode *inode, int fd);
lib/compress.c +453 -95
index 4101009..0d796c8 100644
@@ -20,6 +20,9 @@
 #include "erofs/block_list.h"
 #include "erofs/compress_hints.h"
 #include "erofs/fragments.h"
+#ifdef EROFS_MT_ENABLED
+#include "erofs/workqueue.h"
+#endif
 
 /* compressing configuration specified by users */
 struct erofs_compress_cfg {
@@ -33,29 +36,77 @@ struct z_erofs_extent_item {
        struct z_erofs_inmem_extent e;
 };
 
-struct z_erofs_vle_compress_ctx {
-       u8 queue[EROFS_CONFIG_COMPR_MAX_SZ * 2];
+struct z_erofs_compress_ictx {
+       struct erofs_inode *inode;
+       int fd;
+       unsigned int pclustersize;
+
+       u32 tof_chksum;
+       bool fix_dedupedfrag;
+       bool fragemitted;
+
+       /* fields for write indexes */
+       u8 *metacur;
+       struct list_head extents;
+       u16 clusterofs;
+};
+
+struct z_erofs_compress_sctx {         /* segment context */
+       struct z_erofs_compress_ictx *ictx;
+
+       u8 *queue;
        struct list_head extents;
        struct z_erofs_extent_item *pivot;
 
-       struct erofs_inode *inode;
-       struct erofs_compress_cfg *ccfg;
+       struct erofs_compress *chandle;
+       char *destbuf;
 
-       u8 *metacur;
        unsigned int head, tail;
        erofs_off_t remaining;
-       unsigned int pclustersize;
        erofs_blk_t blkaddr;            /* pointing to the next blkaddr */
        u16 clusterofs;
 
-       u32 tof_chksum;
-       bool fix_dedupedfrag;
-       bool fragemitted;
+       int seg_num, seg_idx;
+
+       void *membuf;
+       erofs_off_t memoff;
+};
+
+#ifdef EROFS_MT_ENABLED
+struct erofs_compress_wq_tls {
+       u8 *queue;
+       char *destbuf;
+       struct erofs_compress_cfg *ccfg;
 };
 
+struct erofs_compress_work {
+       /* Note: struct erofs_work must be the first member */
+       struct erofs_work work;
+       struct z_erofs_compress_sctx ctx;
+       struct erofs_compress_work *next;
+
+       unsigned int alg_id;
+       char *alg_name;
+       unsigned int comp_level;
+       unsigned int dict_size;
+
+       int errcode;
+};
+
+static struct {
+       struct erofs_workqueue wq;
+       struct erofs_compress_work *idle;
+       pthread_mutex_t mutex;
+       pthread_cond_t cond;
+       int nfini;
+} z_erofs_mt_ctrl;
+#endif
+
+static bool z_erofs_mt_enabled;
+
 #define Z_EROFS_LEGACY_MAP_HEADER_SIZE Z_EROFS_FULL_INDEX_ALIGN(0)
 
-static void z_erofs_write_indexes_final(struct z_erofs_vle_compress_ctx *ctx)
+static void z_erofs_write_indexes_final(struct z_erofs_compress_ictx *ctx)
 {
        const unsigned int type = Z_EROFS_LCLUSTER_TYPE_PLAIN;
        struct z_erofs_lcluster_index di;
@@ -71,7 +122,7 @@ static void z_erofs_write_indexes_final(struct z_erofs_vle_compress_ctx *ctx)
        ctx->metacur += sizeof(di);
 }
 
-static void z_erofs_write_extent(struct z_erofs_vle_compress_ctx *ctx,
+static void z_erofs_write_extent(struct z_erofs_compress_ictx *ctx,
                                 struct z_erofs_inmem_extent *e)
 {
        struct erofs_inode *inode = ctx->inode;
@@ -170,7 +221,7 @@ static void z_erofs_write_extent(struct z_erofs_vle_compress_ctx *ctx,
        ctx->clusterofs = clusterofs + count;
 }
 
-static void z_erofs_write_indexes(struct z_erofs_vle_compress_ctx *ctx)
+static void z_erofs_write_indexes(struct z_erofs_compress_ictx *ctx)
 {
        struct z_erofs_extent_item *ei, *n;
 
@@ -184,15 +235,16 @@ static void z_erofs_write_indexes(struct z_erofs_vle_compress_ctx *ctx)
        z_erofs_write_indexes_final(ctx);
 }
 
-static bool z_erofs_need_refill(struct z_erofs_vle_compress_ctx *ctx)
+static bool z_erofs_need_refill(struct z_erofs_compress_sctx *ctx)
 {
        const bool final = !ctx->remaining;
        unsigned int qh_aligned, qh_after;
+       struct erofs_inode *inode = ctx->ictx->inode;
 
        if (final || ctx->head < EROFS_CONFIG_COMPR_MAX_SZ)
                return false;
 
-       qh_aligned = round_down(ctx->head, erofs_blksiz(ctx->inode->sbi));
+       qh_aligned = round_down(ctx->head, erofs_blksiz(inode->sbi));
        qh_after = ctx->head - qh_aligned;
        memmove(ctx->queue, ctx->queue + qh_aligned, ctx->tail - qh_aligned);
        ctx->tail -= qh_aligned;
@@ -204,7 +256,7 @@ static struct z_erofs_extent_item dummy_pivot = {
        .e.length = 0
 };
 
-static void z_erofs_commit_extent(struct z_erofs_vle_compress_ctx *ctx,
+static void z_erofs_commit_extent(struct z_erofs_compress_sctx *ctx,
                                  struct z_erofs_extent_item *ei)
 {
        if (ei == &dummy_pivot)
@@ -212,14 +264,13 @@ static void z_erofs_commit_extent(struct z_erofs_vle_compress_ctx *ctx,
 
        list_add_tail(&ei->list, &ctx->extents);
        ctx->clusterofs = (ctx->clusterofs + ei->e.length) &
-                       (erofs_blksiz(ctx->inode->sbi) - 1);
-
+                         (erofs_blksiz(ctx->ictx->inode->sbi) - 1);
 }
 
-static int z_erofs_compress_dedupe(struct z_erofs_vle_compress_ctx *ctx,
+static int z_erofs_compress_dedupe(struct z_erofs_compress_sctx *ctx,
                                   unsigned int *len)
 {
-       struct erofs_inode *inode = ctx->inode;
+       struct erofs_inode *inode = ctx->ictx->inode;
        const unsigned int lclustermask = (1 << inode->z_logical_clusterbits) - 1;
        struct erofs_sb_info *sbi = inode->sbi;
        struct z_erofs_extent_item *ei = ctx->pivot;
@@ -315,16 +366,17 @@ out:
        return 0;
 }
 
-static int write_uncompressed_extent(struct z_erofs_vle_compress_ctx *ctx,
+static int write_uncompressed_extent(struct z_erofs_compress_sctx *ctx,
                                     unsigned int len, char *dst)
 {
-       struct erofs_sb_info *sbi = ctx->inode->sbi;
+       struct erofs_inode *inode = ctx->ictx->inode;
+       struct erofs_sb_info *sbi = inode->sbi;
        unsigned int count = min(erofs_blksiz(sbi), len);
        unsigned int interlaced_offset, rightpart;
        int ret;
 
        /* write interlaced uncompressed data if needed */
-       if (ctx->inode->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
+       if (inode->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
                interlaced_offset = ctx->clusterofs;
        else
                interlaced_offset = 0;
@@ -335,11 +387,17 @@ static int write_uncompressed_extent(struct z_erofs_vle_compress_ctx *ctx,
        memcpy(dst + interlaced_offset, ctx->queue + ctx->head, rightpart);
        memcpy(dst, ctx->queue + ctx->head + rightpart, count - rightpart);
 
-       erofs_dbg("Writing %u uncompressed data to block %u",
-                 count, ctx->blkaddr);
-       ret = blk_write(sbi, dst, ctx->blkaddr, 1);
-       if (ret)
-               return ret;
+       if (ctx->membuf) {
+               erofs_dbg("Writing %u uncompressed data to membuf", count);
+               memcpy(ctx->membuf + ctx->memoff, dst, erofs_blksiz(sbi));
+               ctx->memoff += erofs_blksiz(sbi);
+       } else {
+               erofs_dbg("Writing %u uncompressed data to block %u", count,
+                         ctx->blkaddr);
+               ret = blk_write(sbi, dst, ctx->blkaddr, 1);
+               if (ret)
+                       return ret;
+       }
        return count;
 }
 
@@ -379,12 +437,12 @@ static int z_erofs_fill_inline_data(struct erofs_inode *inode, void *data,
        return len;
 }
 
-static void tryrecompress_trailing(struct z_erofs_vle_compress_ctx *ctx,
+static void tryrecompress_trailing(struct z_erofs_compress_sctx *ctx,
                                   struct erofs_compress *ec,
                                   void *in, unsigned int *insize,
                                   void *out, unsigned int *compressedsize)
 {
-       struct erofs_sb_info *sbi = ctx->inode->sbi;
+       struct erofs_sb_info *sbi = ctx->ictx->inode->sbi;
        static char tmp[Z_EROFS_PCLUSTER_MAX_SIZE];
        unsigned int count;
        int ret = *compressedsize;
@@ -406,10 +464,11 @@ static void tryrecompress_trailing(struct z_erofs_vle_compress_ctx *ctx,
        *compressedsize = ret;
 }
 
-static bool z_erofs_fixup_deduped_fragment(struct z_erofs_vle_compress_ctx *ctx,
+static bool z_erofs_fixup_deduped_fragment(struct z_erofs_compress_sctx *ctx,
                                           unsigned int len)
 {
-       struct erofs_inode *inode = ctx->inode;
+       struct z_erofs_compress_ictx *ictx = ctx->ictx;
+       struct erofs_inode *inode = ictx->inode;
        struct erofs_sb_info *sbi = inode->sbi;
        const unsigned int newsize = ctx->remaining + len;
 
@@ -417,9 +476,10 @@ static bool z_erofs_fixup_deduped_fragment(struct z_erofs_vle_compress_ctx *ctx,
 
        /* try to fix again if it gets larger (should be rare) */
        if (inode->fragment_size < newsize) {
-               ctx->pclustersize = min_t(erofs_off_t, z_erofs_get_max_pclustersize(inode),
-                                         roundup(newsize - inode->fragment_size,
-                                                 erofs_blksiz(sbi)));
+               ictx->pclustersize = min_t(erofs_off_t,
+                               z_erofs_get_max_pclustersize(inode),
+                               roundup(newsize - inode->fragment_size,
+                                       erofs_blksiz(sbi)));
                return false;
        }
 
@@ -436,29 +496,32 @@ static bool z_erofs_fixup_deduped_fragment(struct z_erofs_vle_compress_ctx *ctx,
        return true;
 }
 
-static int __z_erofs_compress_one(struct z_erofs_vle_compress_ctx *ctx,
+static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,
                                  struct z_erofs_inmem_extent *e)
 {
-       static char dstbuf[EROFS_CONFIG_COMPR_MAX_SZ + EROFS_MAX_BLOCK_SIZE];
-       struct erofs_inode *inode = ctx->inode;
+       static char g_dstbuf[EROFS_CONFIG_COMPR_MAX_SZ + EROFS_MAX_BLOCK_SIZE];
+       char *dstbuf = ctx->destbuf ?: g_dstbuf;
+       struct z_erofs_compress_ictx *ictx = ctx->ictx;
+       struct erofs_inode *inode = ictx->inode;
        struct erofs_sb_info *sbi = inode->sbi;
        unsigned int blksz = erofs_blksiz(sbi);
        char *const dst = dstbuf + blksz;
-       struct erofs_compress *const h = &ctx->ccfg->handle;
+       struct erofs_compress *const h = ctx->chandle;
        unsigned int len = ctx->tail - ctx->head;
        bool is_packed_inode = erofs_is_packed_inode(inode);
        bool final = !ctx->remaining;
-       bool may_packing = (cfg.c_fragments && final && !is_packed_inode);
+       bool may_packing = (cfg.c_fragments && final && !is_packed_inode &&
+                           !z_erofs_mt_enabled);
        bool may_inline = (cfg.c_ztailpacking && final && !may_packing);
        unsigned int compressedsize;
        int ret;
 
-       if (len <= ctx->pclustersize) {
+       if (len <= ictx->pclustersize) {
                if (!final || !len)
                        return 1;
                if (may_packing) {
-                       if (inode->fragment_size && !ctx->fix_dedupedfrag) {
-                               ctx->pclustersize = roundup(len, blksz);
+                       if (inode->fragment_size && !ictx->fix_dedupedfrag) {
+                               ictx->pclustersize = roundup(len, blksz);
                                goto fix_dedupedfrag;
                        }
                        e->length = len;
@@ -470,7 +533,7 @@ static int __z_erofs_compress_one(struct z_erofs_vle_compress_ctx *ctx,
 
        e->length = min(len, cfg.c_max_decompressed_extent_bytes);
        ret = erofs_compress_destsize(h, ctx->queue + ctx->head,
-                                     &e->length, dst, ctx->pclustersize);
+                                     &e->length, dst, ictx->pclustersize);
        if (ret <= 0) {
                erofs_err("failed to compress %s: %s", inode->i_srcpath,
                          erofs_strerror(ret));
@@ -507,16 +570,16 @@ nocompression:
                e->compressedblks = 1;
                e->raw = true;
        } else if (may_packing && len == e->length &&
-                  compressedsize < ctx->pclustersize &&
-                  (!inode->fragment_size || ctx->fix_dedupedfrag)) {
+                  compressedsize < ictx->pclustersize &&
+                  (!inode->fragment_size || ictx->fix_dedupedfrag)) {
 frag_packing:
                ret = z_erofs_pack_fragments(inode, ctx->queue + ctx->head,
-                                            len, ctx->tof_chksum);
+                                            len, ictx->tof_chksum);
                if (ret < 0)
                        return ret;
                e->compressedblks = 0; /* indicate a fragment */
                e->raw = false;
-               ctx->fragemitted = true;
+               ictx->fragemitted = true;
        /* tailpcluster should be less than 1 block */
        } else if (may_inline && len == e->length && compressedsize < blksz) {
                if (ctx->clusterofs + len <= blksz) {
@@ -545,8 +608,8 @@ frag_packing:
                 */
                if (may_packing && len == e->length &&
                    (compressedsize & (blksz - 1)) &&
-                   ctx->tail < sizeof(ctx->queue)) {
-                       ctx->pclustersize = roundup(compressedsize, blksz);
+                   ctx->tail < Z_EROFS_COMPR_QUEUE_SZ) {
+                       ictx->pclustersize = roundup(compressedsize, blksz);
                        goto fix_dedupedfrag;
                }
 
@@ -569,34 +632,45 @@ frag_packing:
                }
 
                /* write compressed data */
-               erofs_dbg("Writing %u compressed data to %u of %u blocks",
-                         e->length, ctx->blkaddr, e->compressedblks);
+               if (ctx->membuf) {
+                       erofs_off_t sz = e->compressedblks * blksz;
+                       erofs_dbg("Writing %u compressed data to membuf of %u blocks",
+                                 e->length, e->compressedblks);
 
-               ret = blk_write(sbi, dst - padding, ctx->blkaddr,
-                               e->compressedblks);
-               if (ret)
-                       return ret;
+                       memcpy(ctx->membuf + ctx->memoff, dst - padding, sz);
+                       ctx->memoff += sz;
+               } else {
+                       erofs_dbg("Writing %u compressed data to %u of %u blocks",
+                                 e->length, ctx->blkaddr, e->compressedblks);
+
+                       ret = blk_write(sbi, dst - padding, ctx->blkaddr,
+                                       e->compressedblks);
+                       if (ret)
+                               return ret;
+               }
                e->raw = false;
                may_inline = false;
                may_packing = false;
        }
        e->partial = false;
        e->blkaddr = ctx->blkaddr;
+       if (ctx->blkaddr != EROFS_NULL_ADDR)
+               ctx->blkaddr += e->compressedblks;
        if (!may_inline && !may_packing && !is_packed_inode)
                (void)z_erofs_dedupe_insert(e, ctx->queue + ctx->head);
-       ctx->blkaddr += e->compressedblks;
        ctx->head += e->length;
        return 0;
 
 fix_dedupedfrag:
        DBG_BUGON(!inode->fragment_size);
        ctx->remaining += inode->fragment_size;
-       ctx->fix_dedupedfrag = true;
+       ictx->fix_dedupedfrag = true;
        return 1;
 }
 
-static int z_erofs_compress_one(struct z_erofs_vle_compress_ctx *ctx)
+static int z_erofs_compress_one(struct z_erofs_compress_sctx *ctx)
 {
+       struct z_erofs_compress_ictx *ictx = ctx->ictx;
        unsigned int len = ctx->tail - ctx->head;
        struct z_erofs_extent_item *ei;
 
@@ -624,7 +698,7 @@ static int z_erofs_compress_one(struct z_erofs_vle_compress_ctx *ctx)
 
                len -= ei->e.length;
                ctx->pivot = ei;
-               if (ctx->fix_dedupedfrag && !ctx->fragemitted &&
+               if (ictx->fix_dedupedfrag && !ictx->fragemitted &&
                    z_erofs_fixup_deduped_fragment(ctx, len))
                        break;
 
@@ -912,13 +986,268 @@ void z_erofs_drop_inline_pcluster(struct erofs_inode *inode)
        inode->eof_tailraw = NULL;
 }
 
+int z_erofs_compress_segment(struct z_erofs_compress_sctx *ctx,
+                            u64 offset, erofs_blk_t blkaddr)
+{
+       int fd = ctx->ictx->fd;
+
+       ctx->blkaddr = blkaddr;
+       while (ctx->remaining) {
+               const u64 rx = min_t(u64, ctx->remaining,
+                                    Z_EROFS_COMPR_QUEUE_SZ - ctx->tail);
+               int ret;
+
+               ret = (offset == -1 ?
+                       read(fd, ctx->queue + ctx->tail, rx) :
+                       pread(fd, ctx->queue + ctx->tail, rx, offset));
+               if (ret != rx)
+                       return -errno;
+
+               ctx->remaining -= rx;
+               ctx->tail += rx;
+               if (offset != -1)
+                       offset += rx;
+
+               ret = z_erofs_compress_one(ctx);
+               if (ret)
+                       return ret;
+       }
+       DBG_BUGON(ctx->head != ctx->tail);
+
+       if (ctx->pivot) {
+               z_erofs_commit_extent(ctx, ctx->pivot);
+               ctx->pivot = NULL;
+       }
+       return 0;
+}
+
+#ifdef EROFS_MT_ENABLED
+void *z_erofs_mt_wq_tls_alloc(struct erofs_workqueue *wq, void *ptr)
+{
+       struct erofs_compress_wq_tls *tls;
+
+       tls = calloc(1, sizeof(*tls));
+       if (!tls)
+               return NULL;
+
+       tls->queue = malloc(Z_EROFS_COMPR_QUEUE_SZ);
+       if (!tls->queue)
+               goto err_free_priv;
+
+       tls->destbuf = calloc(1, EROFS_CONFIG_COMPR_MAX_SZ +
+                             EROFS_MAX_BLOCK_SIZE);
+       if (!tls->destbuf)
+               goto err_free_queue;
+
+       tls->ccfg = calloc(EROFS_MAX_COMPR_CFGS, sizeof(*tls->ccfg));
+       if (!tls->ccfg)
+               goto err_free_destbuf;
+       return tls;
+
+err_free_destbuf:
+       free(tls->destbuf);
+err_free_queue:
+       free(tls->queue);
+err_free_priv:
+       free(tls);
+       return NULL;
+}
+
+int z_erofs_mt_wq_tls_init_compr(struct erofs_sb_info *sbi,
+                                struct erofs_compress_wq_tls *tls,
+                                unsigned int alg_id, char *alg_name,
+                                unsigned int comp_level,
+                                unsigned int dict_size)
+{
+       struct erofs_compress_cfg *lc = &tls->ccfg[alg_id];
+       int ret;
+
+       if (likely(lc->enable))
+               return 0;
+
+       ret = erofs_compressor_init(sbi, &lc->handle, alg_name,
+                                   comp_level, dict_size);
+       if (ret)
+               return ret;
+       lc->algorithmtype = alg_id;
+       lc->enable = true;
+       return 0;
+}
+
+void *z_erofs_mt_wq_tls_free(struct erofs_workqueue *wq, void *priv)
+{
+       struct erofs_compress_wq_tls *tls = priv;
+       int i;
+
+       for (i = 0; i < EROFS_MAX_COMPR_CFGS; i++)
+               if (tls->ccfg[i].enable)
+                       erofs_compressor_exit(&tls->ccfg[i].handle);
+
+       free(tls->ccfg);
+       free(tls->destbuf);
+       free(tls->queue);
+       free(tls);
+       return NULL;
+}
+
+void z_erofs_mt_workfn(struct erofs_work *work, void *tlsp)
+{
+       struct erofs_compress_work *cwork = (struct erofs_compress_work *)work;
+       struct erofs_compress_wq_tls *tls = tlsp;
+       struct z_erofs_compress_sctx *ctx = &cwork->ctx;
+       u64 offset = ctx->seg_idx * cfg.c_segment_size;
+       int ret = 0;
+
+       ret = z_erofs_mt_wq_tls_init_compr(ctx->ictx->inode->sbi, tls,
+                                          cwork->alg_id, cwork->alg_name,
+                                          cwork->comp_level,
+                                          cwork->dict_size);
+       if (ret)
+               goto out;
+
+       ctx->queue = tls->queue;
+       ctx->destbuf = tls->destbuf;
+       ctx->chandle = &tls->ccfg[cwork->alg_id].handle;
+
+       ctx->membuf = malloc(ctx->remaining);
+       if (!ctx->membuf) {
+               ret = -ENOMEM;
+               goto out;
+       }
+       ctx->memoff = 0;
+
+       ret = z_erofs_compress_segment(ctx, offset, EROFS_NULL_ADDR);
+
+out:
+       cwork->errcode = ret;
+       pthread_mutex_lock(&z_erofs_mt_ctrl.mutex);
+       ++z_erofs_mt_ctrl.nfini;
+       pthread_cond_signal(&z_erofs_mt_ctrl.cond);
+       pthread_mutex_unlock(&z_erofs_mt_ctrl.mutex);
+}
+
+int z_erofs_merge_segment(struct z_erofs_compress_ictx *ictx,
+                         struct z_erofs_compress_sctx *ctx)
+{
+       struct z_erofs_extent_item *ei, *n;
+       struct erofs_sb_info *sbi = ictx->inode->sbi;
+       erofs_blk_t blkoff = 0;
+       int ret = 0, ret2;
+
+       list_for_each_entry_safe(ei, n, &ctx->extents, list) {
+               list_del(&ei->list);
+               list_add_tail(&ei->list, &ictx->extents);
+
+               if (ei->e.blkaddr != EROFS_NULL_ADDR)   /* deduped extents */
+                       continue;
+
+               ei->e.blkaddr = ctx->blkaddr;
+               ctx->blkaddr += ei->e.compressedblks;
+
+               ret2 = blk_write(sbi, ctx->membuf + blkoff * erofs_blksiz(sbi),
+                                ei->e.blkaddr, ei->e.compressedblks);
+               blkoff += ei->e.compressedblks;
+               if (ret2) {
+                       ret = ret2;
+                       continue;
+               }
+       }
+       free(ctx->membuf);
+       return ret;
+}
+
+int z_erofs_mt_compress(struct z_erofs_compress_ictx *ctx,
+                       struct erofs_compress_cfg *ccfg,
+                       erofs_blk_t blkaddr,
+                       erofs_blk_t *compressed_blocks)
+{
+       struct erofs_compress_work *cur, *head = NULL, **last = &head;
+       struct erofs_inode *inode = ctx->inode;
+       int nsegs = DIV_ROUND_UP(inode->i_size, cfg.c_segment_size);
+       int ret, i;
+
+       z_erofs_mt_ctrl.nfini = 0;
+
+       for (i = 0; i < nsegs; i++) {
+               if (z_erofs_mt_ctrl.idle) {
+                       cur = z_erofs_mt_ctrl.idle;
+                       z_erofs_mt_ctrl.idle = cur->next;
+                       cur->next = NULL;
+               } else {
+                       cur = calloc(1, sizeof(*cur));
+                       if (!cur)
+                               return -ENOMEM;
+               }
+               *last = cur;
+               last = &cur->next;
+
+               cur->ctx = (struct z_erofs_compress_sctx) {
+                       .ictx = ctx,
+                       .seg_num = nsegs,
+                       .seg_idx = i,
+                       .pivot = &dummy_pivot,
+               };
+               init_list_head(&cur->ctx.extents);
+
+               if (i == nsegs - 1)
+                       cur->ctx.remaining = inode->i_size -
+                                             inode->fragment_size -
+                                             i * cfg.c_segment_size;
+               else
+                       cur->ctx.remaining = cfg.c_segment_size;
+
+               cur->alg_id = ccfg->handle.alg->id;
+               cur->alg_name = ccfg->handle.alg->name;
+               cur->comp_level = ccfg->handle.compression_level;
+               cur->dict_size = ccfg->handle.dict_size;
+
+               cur->work.fn = z_erofs_mt_workfn;
+               erofs_queue_work(&z_erofs_mt_ctrl.wq, &cur->work);
+       }
+
+       pthread_mutex_lock(&z_erofs_mt_ctrl.mutex);
+       while (z_erofs_mt_ctrl.nfini != nsegs)
+               pthread_cond_wait(&z_erofs_mt_ctrl.cond,
+                                 &z_erofs_mt_ctrl.mutex);
+       pthread_mutex_unlock(&z_erofs_mt_ctrl.mutex);
+
+       ret = 0;
+       while (head) {
+               cur = head;
+               head = cur->next;
+
+               if (cur->errcode) {
+                       ret = cur->errcode;
+               } else {
+                       int ret2;
+
+                       cur->ctx.blkaddr = blkaddr;
+                       ret2 = z_erofs_merge_segment(ctx, &cur->ctx);
+                       if (ret2)
+                               ret = ret2;
+
+                       *compressed_blocks += cur->ctx.blkaddr - blkaddr;
+                       blkaddr = cur->ctx.blkaddr;
+               }
+
+               cur->next = z_erofs_mt_ctrl.idle;
+               z_erofs_mt_ctrl.idle = cur;
+       }
+       return ret;
+}
+#endif
+
 int erofs_write_compressed_file(struct erofs_inode *inode, int fd)
 {
+       static u8 g_queue[Z_EROFS_COMPR_QUEUE_SZ];
        struct erofs_buffer_head *bh;
-       static struct z_erofs_vle_compress_ctx ctx;
-       erofs_blk_t blkaddr, compressed_blocks;
+       static struct z_erofs_compress_ictx ctx;
+       static struct z_erofs_compress_sctx sctx;
+       struct erofs_compress_cfg *ccfg;
+       erofs_blk_t blkaddr, compressed_blocks = 0;
        unsigned int legacymetasize;
        int ret;
+       bool ismt = false;
        struct erofs_sb_info *sbi = inode->sbi;
        u8 *compressmeta = malloc(BLK_ROUND_UP(sbi, inode->i_size) *
                                  sizeof(struct z_erofs_lcluster_index) +
@@ -963,8 +1292,8 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd)
                }
        }
 #endif
-       ctx.ccfg = &erofs_ccfg[inode->z_algorithmtype[0]];
-       inode->z_algorithmtype[0] = ctx.ccfg[0].algorithmtype;
+       ccfg = &erofs_ccfg[inode->z_algorithmtype[0]];
+       inode->z_algorithmtype[0] = ccfg[0].algorithmtype;
        inode->z_algorithmtype[1] = 0;
 
        inode->idata_size = 0;
@@ -983,50 +1312,45 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd)
        blkaddr = erofs_mapbh(bh->block);       /* start_blkaddr */
        ctx.inode = inode;
        ctx.pclustersize = z_erofs_get_max_pclustersize(inode);
-       ctx.blkaddr = blkaddr;
        ctx.metacur = compressmeta + Z_EROFS_LEGACY_MAP_HEADER_SIZE;
-       ctx.head = ctx.tail = 0;
-       ctx.clusterofs = 0;
-       ctx.pivot = &dummy_pivot;
        init_list_head(&ctx.extents);
-       ctx.remaining = inode->i_size - inode->fragment_size;
+       ctx.fd = fd;
        ctx.fix_dedupedfrag = false;
        ctx.fragemitted = false;
+       sctx = (struct z_erofs_compress_sctx) { .ictx = &ctx, };
+       init_list_head(&sctx.extents);
+
        if (cfg.c_all_fragments && !erofs_is_packed_inode(inode) &&
            !inode->fragment_size) {
                ret = z_erofs_pack_file_from_fd(inode, fd, ctx.tof_chksum);
                if (ret)
                        goto err_free_idata;
+#ifdef EROFS_MT_ENABLED
+       } else if (z_erofs_mt_enabled && inode->i_size > cfg.c_segment_size) {
+               ismt = true;
+               ret = z_erofs_mt_compress(&ctx, ccfg, blkaddr, &compressed_blocks);
+               if (ret)
+                       goto err_free_idata;
+#endif
        } else {
-               while (ctx.remaining) {
-                       const u64 rx = min_t(u64, ctx.remaining,
-                                            sizeof(ctx.queue) - ctx.tail);
-
-                       ret = read(fd, ctx.queue + ctx.tail, rx);
-                       if (ret != rx) {
-                               ret = -errno;
-                               goto err_bdrop;
-                       }
-                       ctx.remaining -= rx;
-                       ctx.tail += rx;
-
-                       ret = z_erofs_compress_one(&ctx);
-                       if (ret)
-                               goto err_free_idata;
-               }
+               sctx.queue = g_queue;
+               sctx.destbuf = NULL;
+               sctx.chandle = &ccfg->handle;
+               sctx.remaining = inode->i_size - inode->fragment_size;
+               sctx.seg_num = 1;
+               sctx.seg_idx = 0;
+               sctx.pivot = &dummy_pivot;
+
+               ret = z_erofs_compress_segment(&sctx, -1, blkaddr);
+               if (ret)
+                       goto err_free_idata;
+               compressed_blocks = sctx.blkaddr - blkaddr;
        }
-       DBG_BUGON(ctx.head != ctx.tail);
 
        /* fall back to no compression mode */
-       compressed_blocks = ctx.blkaddr - blkaddr;
        DBG_BUGON(compressed_blocks < !!inode->idata_size);
        compressed_blocks -= !!inode->idata_size;
 
-       if (ctx.pivot) {
-               z_erofs_commit_extent(&ctx, ctx.pivot);
-               ctx.pivot = NULL;
-       }
-
        /* generate an extent for the deduplicated fragment */
        if (inode->fragment_size && !ctx.fragemitted) {
                struct z_erofs_extent_item *ei;
@@ -1042,13 +1366,16 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd)
                        .compressedblks = 0,
                        .raw = false,
                        .partial = false,
-                       .blkaddr = ctx.blkaddr,
+                       .blkaddr = sctx.blkaddr,
                };
                init_list_head(&ei->list);
-               z_erofs_commit_extent(&ctx, ei);
+               z_erofs_commit_extent(&sctx, ei);
        }
        z_erofs_fragments_commit(inode);
 
+       if (!ismt)
+               list_splice_tail(&sctx.extents, &ctx.extents);
+
        z_erofs_write_indexes(&ctx);
        legacymetasize = ctx.metacur - compressmeta;
        /* estimate if data compression saves space or not */
@@ -1257,8 +1584,25 @@ int z_erofs_compress_init(struct erofs_sb_info *sbi, struct erofs_buffer_head *s
                return -EINVAL;
        }
 
-       if (erofs_sb_has_compr_cfgs(sbi))
-               return z_erofs_build_compr_cfgs(sbi, sb_bh, max_dict_size);
+       if (erofs_sb_has_compr_cfgs(sbi)) {
+               ret = z_erofs_build_compr_cfgs(sbi, sb_bh, max_dict_size);
+               if (ret)
+                       return ret;
+       }
+
+       z_erofs_mt_enabled = false;
+#ifdef EROFS_MT_ENABLED
+       if (cfg.c_mt_workers > 1) {
+               pthread_mutex_init(&z_erofs_mt_ctrl.mutex, NULL);
+               pthread_cond_init(&z_erofs_mt_ctrl.cond, NULL);
+               ret = erofs_alloc_workqueue(&z_erofs_mt_ctrl.wq,
+                                           cfg.c_mt_workers,
+                                           cfg.c_mt_workers << 2,
+                                           z_erofs_mt_wq_tls_alloc,
+                                           z_erofs_mt_wq_tls_free);
+               z_erofs_mt_enabled = !ret;
+       }
+#endif
        return 0;
 }
 
@@ -1271,5 +1615,19 @@ int z_erofs_compress_exit(void)
                if (ret)
                        return ret;
        }
+
+       if (z_erofs_mt_enabled) {
+#ifdef EROFS_MT_ENABLED
+               ret = erofs_destroy_workqueue(&z_erofs_mt_ctrl.wq);
+               if (ret)
+                       return ret;
+               while (z_erofs_mt_ctrl.idle) {
+                       struct erofs_compress_work *tmp =
+                               z_erofs_mt_ctrl.idle->next;
+                       free(z_erofs_mt_ctrl.idle);
+                       z_erofs_mt_ctrl.idle = tmp;
+               }
+#endif
+       }
        return 0;
 }
lib/compressor.c +2 -0
index 58eae2a..175259e 100644
@@ -86,6 +86,8 @@ int erofs_compressor_init(struct erofs_sb_info *sbi, struct erofs_compress *c,
 
        /* should be written in "minimum compression ratio * 100" */
        c->compress_threshold = 100;
+       c->compression_level = -1;
+       c->dict_size = 0;
 
        if (!alg_name) {
                c->alg = NULL;
mkfs/main.c +7 -2
index 126a049..5dbaf9f 100644
@@ -678,7 +678,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 
                        processors = erofs_get_available_processors();
                        if (cfg.c_mt_workers > processors)
-                               erofs_warn("the number of workers %d is more than the number of processors %d, performance may be impacted.",
+                               erofs_warn("%d workers exceed %d processors, potentially impacting performance.",
                                           cfg.c_mt_workers, processors);
                        break;
                }
@@ -838,6 +838,12 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
                }
                cfg.c_pclusterblks_packed = pclustersize_packed >> sbi.blkszbits;
        }
+#ifdef EROFS_MT_ENABLED
+       if (cfg.c_mt_workers > 1 && (cfg.c_dedupe || cfg.c_fragments)) {
+               erofs_warn("Note that dedupe/fragments are NOT supported in multi-threaded mode for now, reseting --workers=1.");
+               cfg.c_mt_workers = 1;
+       }
+#endif
        return 0;
 }
 
--

2.39.3

author
#9 lore raw

Re: [PATCH v7 5/5] erofs-utils: mkfs: introduce inner-file multi-threaded compression

Quoted 8 lines · Noboru Asai

I think it is easier to understand the source code if the names of variables and pointers in the same structure are unified.

struct z_erofs_compress_ictx inode_ctx; // i stands for inode? struct z_erofs_compress_ictx *ictx;

struct z_erofs_compress_sctx seg_ctx;
struct z_erofs_compress_sctx *sctx;

"ictx" means "inode context", "sctx" means "segment context". If there is no confusion, "ctx" naming can be used in a function.

Also I tend to avoid making a huge diff to make ctx->"sctx or ictx", otherwise "git blame" will be in a mess.

Thanks,
Gao Xiang