]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
import: always use the same buffer size 39008/head
authorLennart Poettering <lennart@poettering.net>
Fri, 22 Aug 2025 15:04:37 +0000 (17:04 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Sep 2025 13:43:13 +0000 (15:43 +0200)
Let's synchronize the buffer sizes used when passing around the disk
images, i.e. size both our internal buffers and the pipe buffers the
same (so that we can always write()/read() everything in one gone -
except for the noise compression inserts).

Let's also increase the buffer sizes from 16K to 128K, which made a
difference for me, because it reduces the number of syscalls quite a
bit.

src/import/export-raw.c
src/import/export-tar.c
src/import/import-common.c
src/import/import-common.h
src/import/import-compress.c
src/import/import-raw.c
src/import/import-tar.c

index 787d230d2ff95a7452fc08652efcc0def94e4fa4..767e10f3ce318ac905b8e49c5e33bfa8028a6c07 100644 (file)
@@ -11,6 +11,7 @@
 #include "fd-util.h"
 #include "format-util.h"
 #include "fs-util.h"
+#include "import-common.h"
 #include "log.h"
 #include "pretty-print.h"
 #include "ratelimit.h"
@@ -20,8 +21,6 @@
 #include "time-util.h"
 #include "tmpfile-util.h"
 
-#define COPY_BUFFER_SIZE (16*1024)
-
 typedef struct RawExport {
         sd_event *event;
 
@@ -161,7 +160,7 @@ static int raw_export_process(RawExport *e) {
 
         if (!e->tried_sendfile && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
 
-                l = sendfile(e->output_fd, e->input_fd, NULL, COPY_BUFFER_SIZE);
+                l = sendfile(e->output_fd, e->input_fd, NULL, IMPORT_BUFFER_SIZE);
                 if (l < 0) {
                         if (errno == EAGAIN)
                                 return 0;
@@ -181,7 +180,7 @@ static int raw_export_process(RawExport *e) {
         }
 
         while (e->buffer_size <= 0) {
-                uint8_t input[COPY_BUFFER_SIZE];
+                uint8_t input[IMPORT_BUFFER_SIZE];
 
                 if (e->eof) {
                         r = 0;
index 5b907769a070ce97c9adbb4ffbb7f1887ed2c39b..58b4273c6dc3fbfdff2b0b7f29d4bd97faa60311 100644 (file)
@@ -20,8 +20,6 @@
 #include "time-util.h"
 #include "tmpfile-util.h"
 
-#define COPY_BUFFER_SIZE (16*1024)
-
 typedef struct TarExport {
         sd_event *event;
 
@@ -181,7 +179,7 @@ static int tar_export_process(TarExport *e) {
 
         if (!e->tried_splice && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
 
-                l = splice(e->tar_fd, NULL, e->output_fd, NULL, COPY_BUFFER_SIZE, 0);
+                l = splice(e->tar_fd, NULL, e->output_fd, NULL, IMPORT_BUFFER_SIZE, 0);
                 if (l < 0) {
                         if (errno == EAGAIN)
                                 return 0;
@@ -201,7 +199,7 @@ static int tar_export_process(TarExport *e) {
         }
 
         while (e->buffer_size <= 0) {
-                uint8_t input[COPY_BUFFER_SIZE];
+                uint8_t input[IMPORT_BUFFER_SIZE];
 
                 if (e->eof) {
                         r = tar_export_finish(e);
index bab81d596f923158967092a37902c52d6c6da61c..2de01953a396f2975bd2702d95cef5c267898cf0 100644 (file)
@@ -32,6 +32,8 @@ int import_fork_tar_x(const char *path, pid_t *ret) {
         if (pipe2(pipefd, O_CLOEXEC) < 0)
                 return log_error_errno(errno, "Failed to create pipe for tar: %m");
 
+        (void) fcntl(pipefd[0], F_SETPIPE_SZ, IMPORT_BUFFER_SIZE);
+
         use_selinux = mac_selinux_use();
 
         r = safe_fork_full("(tar)",
@@ -101,6 +103,8 @@ int import_fork_tar_c(const char *path, pid_t *ret) {
         if (pipe2(pipefd, O_CLOEXEC) < 0)
                 return log_error_errno(errno, "Failed to create pipe for tar: %m");
 
+        (void) fcntl(pipefd[0], F_SETPIPE_SZ, IMPORT_BUFFER_SIZE);
+
         use_selinux = mac_selinux_use();
 
         r = safe_fork_full("(tar)",
index bb7d5614f01c49532a9b23044722c911cd568eb7..3b571a3f99320c9a757f2e0b597e61d7a0da031a 100644 (file)
@@ -41,3 +41,5 @@ int import_mangle_os_tree(const char *path);
 bool import_validate_local(const char *name, ImportFlags flags);
 
 int import_allocate_event_with_signals(sd_event **ret);
+
+#define IMPORT_BUFFER_SIZE (128U*1024U)
index 666a2c3aaba1466a768c5cec621bfb72be21dc7d..f893abc43e648efa6a515fc422a06cf352a98bfd 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "import-common.h"
 #include "import-compress.h"
 #include "log.h"
 #include "string-table.h"
@@ -148,7 +149,7 @@ int import_uncompress(ImportCompress *c, const void *data, size_t size, ImportCo
                 c->xz.avail_in = size;
 
                 while (c->xz.avail_in > 0) {
-                        uint8_t buffer[16 * 1024];
+                        uint8_t buffer[IMPORT_BUFFER_SIZE];
                         lzma_ret lzr;
 
                         c->xz.next_out = buffer;
@@ -172,7 +173,7 @@ int import_uncompress(ImportCompress *c, const void *data, size_t size, ImportCo
                 c->gzip.avail_in = size;
 
                 while (c->gzip.avail_in > 0) {
-                        uint8_t buffer[16 * 1024];
+                        uint8_t buffer[IMPORT_BUFFER_SIZE];
 
                         c->gzip.next_out = buffer;
                         c->gzip.avail_out = sizeof(buffer);
@@ -196,7 +197,7 @@ int import_uncompress(ImportCompress *c, const void *data, size_t size, ImportCo
                 c->bzip2.avail_in = size;
 
                 while (c->bzip2.avail_in > 0) {
-                        uint8_t buffer[16 * 1024];
+                        uint8_t buffer[IMPORT_BUFFER_SIZE];
 
                         c->bzip2.next_out = (char*) buffer;
                         c->bzip2.avail_out = sizeof(buffer);
@@ -222,7 +223,7 @@ int import_uncompress(ImportCompress *c, const void *data, size_t size, ImportCo
                 };
 
                 while (input.pos < input.size) {
-                        uint8_t buffer[16 * 1024];
+                        uint8_t buffer[IMPORT_BUFFER_SIZE];
                         ZSTD_outBuffer output = {
                                 .dst = buffer,
                                 .size = sizeof(buffer),
@@ -320,7 +321,7 @@ static int enlarge_buffer(void **buffer, size_t *buffer_size, size_t *buffer_all
         if (*buffer_allocated > *buffer_size)
                 return 0;
 
-        l = MAX(16*1024U, (*buffer_size * 2));
+        l = MAX(IMPORT_BUFFER_SIZE, (*buffer_size * 2));
         p = realloc(*buffer, l);
         if (!p)
                 return -ENOMEM;
index dc1a4b1c62372e71dab5e8169dbf8fb76c982d12..1d7302cd882437bdc191b14a53d98e813cb7b10f 100644 (file)
@@ -47,7 +47,7 @@ typedef struct RawImport {
 
         sd_event_source *input_event_source;
 
-        uint8_t buffer[16*1024];
+        uint8_t buffer[IMPORT_BUFFER_SIZE];
         size_t buffer_size;
 
         uint64_t written_compressed;
index b4b9b8dc99789f0e0c86a564e13568caecf1303b..14ee1b4ab80474406bcebb211618cb9b61c3d9b1 100644 (file)
@@ -49,7 +49,7 @@ typedef struct TarImport {
 
         sd_event_source *input_event_source;
 
-        uint8_t buffer[16*1024];
+        uint8_t buffer[IMPORT_BUFFER_SIZE];
         size_t buffer_size;
 
         uint64_t written_compressed;