From cf7982aa619d4127820ffec56db26a7d73703fb5 Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Wed, 15 Jun 2022 13:35:10 +0200 Subject: [PATCH] shared/util: Use multiple threads when compressing If supported, use as many threads as possible when compressing. Signed-off-by: Thomas Hipp --- shared/util.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/shared/util.go b/shared/util.go index 0679729ed..00e2be3b9 100644 --- a/shared/util.go +++ b/shared/util.go @@ -12,6 +12,7 @@ import ( "strings" "time" + "github.com/lxc/lxd/shared" "golang.org/x/sys/unix" "gopkg.in/flosch/pongo2.v3" yaml "gopkg.in/yaml.v2" @@ -115,13 +116,20 @@ func PackUpdate(ctx context.Context, filename, compression, path string, args .. func compressTarball(ctx context.Context, filename, compression string) (string, error) { fileExtension := "" + args := []string{"-f", filename} + + // If supported, use as many threads as possible. + if shared.StringInSlice(compression, []string{"zstd", "xz", "lzma"}) { + args = append(args, "--threads=0") + } + switch compression { case "lzop", "zstd": // Remove the uncompressed file as the compress fails to do so. defer os.Remove(filename) fallthrough case "bzip2", "xz", "lzip", "lzma", "gzip": - err := RunCommand(ctx, nil, nil, compression, "-f", filename) + err := RunCommand(ctx, nil, nil, compression, args...) if err != nil { return "", fmt.Errorf("Failed to compress tarball %q: %w", filename, err) }