mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ivan Shapovalov <intelfx@intelfx.name>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	 Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	 Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	 Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	 Adrian Hunter <adrian.hunter@intel.com>,
	 James Clark <james.clark@linaro.org>,
	Nick Terrell <terrelln@fb.com>,  David Sterba <dsterba@suse.com>
Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Ivan Shapovalov <intelfx@intelfx.name>
Subject: [PATCH 3/4] perf: zstd: add streaming decompression and format probe functions
Date: Wed, 14 Oct 2026 20:34:09 +0300	[thread overview]
Message-ID: <20261014-perf-zstd-v1-3-9652727886fa@intelfx.name> (raw)
In-Reply-To: <20261014-perf-zstd-v1-0-9652727886fa@intelfx.name>

Add an actual streaming zstd decompression (fd->fd) entry point,
plus a file->fd wrapper and a helper to detect zstd magic on a file
for compressions[] in dso.c.

Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name>
---
 tools/perf/util/compress.h |  18 ++++++++
 tools/perf/util/zstd.c     | 104 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+)

diff --git a/tools/perf/util/compress.h b/tools/perf/util/compress.h
index e7aa89d31fc6f..fd6b541358bb6 100644
--- a/tools/perf/util/compress.h
+++ b/tools/perf/util/compress.h
@@ -59,6 +59,9 @@ ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_
 
 size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size,
 			      void *dst, size_t dst_size);
+int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd);
+int zstd_decompress_to_fd(const char *input, int output_fd);
+bool zstd_is_compressed(const char *input);
 #else /* !HAVE_ZSTD_SUPPORT */
 
 static inline int zstd_init(struct zstd_data *data __maybe_unused, int level __maybe_unused)
@@ -88,6 +91,21 @@ static inline size_t zstd_decompress_buffer(struct zstd_data *data __maybe_unuse
 {
 	return 0;
 }
+
+static inline int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd)
+{
+	return -1;
+}
+
+static inline int zstd_decompress_to_fd(const char *input, int output_fd)
+{
+	return -1;
+}
+
+static inline bool zstd_is_compressed(const char *input)
+{
+	return false;
+}
 #endif
 
 #endif /* PERF_COMPRESS_H */
diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c
index 3af8910bb87cf..982655a1f1a51 100644
--- a/tools/perf/util/zstd.c
+++ b/tools/perf/util/zstd.c
@@ -1,7 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#include <errno.h>
+#include <fcntl.h>
 #include <string.h>
+#include <unistd.h>
 #include <linux/perf_event.h>
+#include <internal/lib.h>
 
 #include "util/compress.h"
 #include "util/debug.h"
@@ -172,3 +176,103 @@ size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size
 
 	return output.pos;
 }
+
+int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd)
+{
+	ssize_t ret;
+	bool unflushed = false;
+	char buf_in[ZSTD_DStreamInSize()];
+	char buf_out[ZSTD_DStreamOutSize()];
+	ZSTD_inBuffer in = {};
+	ZSTD_outBuffer out = {};
+
+	ret = zstd_init_dstream(data);
+	if (ret < 0)
+		return ret;
+
+	for (;;) {
+		if (in.pos == in.size) {
+			/* out of input, read more */
+			ret = read(input_fd, buf_in, sizeof(buf_in));
+			if (ret < 0) {
+				pr_err("zstd: failed to read() %zu bytes: %s\n",
+				       sizeof(buf_in), strerror(errno));
+				return ret;
+			}
+			in = (ZSTD_inBuffer) {
+				.src = &buf_in,
+				.pos = 0,
+				.size = ret,
+			};
+		}
+
+		if (in.pos == in.size && !unflushed) {
+			/* out of input, out of file, and nothing to flush, exit */
+			break;
+		}
+
+		out = (ZSTD_outBuffer) {
+			.dst = &buf_out,
+			.pos = 0,
+			.size = sizeof(buf_out),
+		};
+		ret = ZSTD_decompressStream(data->dstream, &out, &in);
+		if (ZSTD_isError(ret)) {
+			pr_err("zstd: failed to decompress %zu bytes into buffer of %zu bytes (wrote %zu): %s\n",
+				in.size, out.size, out.pos, ZSTD_getErrorName(ret));
+			return -1;
+		}
+		unflushed = ret > 0;
+
+		ret = writen(output_fd, out.dst, out.pos);
+		if (ret < 0) {
+			pr_err("zstd: failed to write() %zu bytes: %s\n",
+			       out.pos, strerror(errno));
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+int zstd_decompress_to_fd(const char *input, int output_fd)
+{
+	int input_fd = -1;
+	int ret;
+	struct zstd_data data;
+
+	input_fd = open(input, O_RDONLY);
+	if (input_fd < 0) {
+		pr_err("zstd: failed to open(%s): %s\n", input, strerror(errno));
+		return -1;
+	}
+
+	ret = zstd_init(&data, ZSTD_CLEVEL_DEFAULT);
+	if (ret < 0)
+		goto err;
+
+	ret = zstd_decompress_stream(&data, input_fd, output_fd);
+
+err:
+	zstd_fini(&data);
+	close(input_fd);
+	return ret;
+}
+
+bool zstd_is_compressed(const char *file)
+{
+	int fd = open(file, O_RDONLY);
+	const uint32_t magic_le = htole32(ZSTD_MAGICNUMBER);
+	char buf[4];
+	ssize_t rc;
+
+	if (fd < 0) {
+		pr_err("zstd: failed to open(%s): %s\n", file, strerror(errno));
+		return -1;
+	}
+
+	rc = read(fd, buf, sizeof(buf));
+	close(fd);
+	return rc == sizeof(buf) ?
+	       memcmp(buf, (const char *) &magic_le, sizeof(buf)) == 0 : false;
+}

-- 
2.55.0.8.g37e2a0019f


  parent reply	other threads:[~2026-10-14 17:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14 17:34 [PATCH 0/4] perf: support zstd-compressed kernel modules Ivan Shapovalov
2026-10-14 17:34 ` [PATCH 1/4] perf: zstd: zstd_decompress_stream -> zstd_decompress_buffer Ivan Shapovalov
2026-10-14 17:34 ` [PATCH 2/4] perf: zstd: factor out zstd_init_{c,d}stream() Ivan Shapovalov
2026-10-14 17:34 ` Ivan Shapovalov [this message]
2026-10-14 17:34 ` [PATCH 4/4] perf: support zstd-compressed kernel modules Ivan Shapovalov
2026-10-27  2:58   ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261014-perf-zstd-v1-3-9652727886fa@intelfx.name \
    --to=intelfx@intelfx.name \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dsterba@suse.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=terrelln@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®