perf mmap: Lazily initialize zstd streams to save memory when not using it
Zstd streams create dictionaries that can require significant RAM, especially when there is one per-CPU. Tools like 'perf record' won't use the streams without the -z option, and so the creation of the streams is pure overhead. Switch to creating the streams on first use. Committer notes: ssize_t comes from sys/types.h, size_t from stddef.h. This worked on glibc as stdlib.h includes both, but not on musl libc. So do what 'man size_t' says and include sys/types.h and stddef.h instead of stdlib.h Signed-off-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com> Cc: Changbin Du <changbin.du@huawei.com> Cc: Colin Ian King <colin.i.king@gmail.com> Cc: Dmitrii Dolgov <9erthalion6@gmail.com> Cc: German Gomez <german.gomez@arm.com> Cc: Huacai Chen <chenhuacai@kernel.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@arm.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: K Prateek Nayak <kprateek.nayak@amd.com> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Leo Yan <leo.yan@linaro.org> Cc: Li Dong <lidong@vivo.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Ming Wang <wangming01@loongson.cn> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Nick Terrell <terrelln@fb.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Sandipan Das <sandipan.das@amd.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Steinar H. Gunderson <sesse@google.com> Cc: Vincent Whitchurch <vincent.whitchurch@axis.com> Cc: Wenyu Liu <liuwenyu7@huawei.com> Cc: Yang Jihong <yangjihong1@huawei.com> Link: https://lore.kernel.org/r/20231102175735.2272696-5-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
parent
d60469d7c0
commit
5940a20a18
@ -270,7 +270,7 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
|
|||||||
|
|
||||||
static int record__aio_enabled(struct record *rec);
|
static int record__aio_enabled(struct record *rec);
|
||||||
static int record__comp_enabled(struct record *rec);
|
static int record__comp_enabled(struct record *rec);
|
||||||
static size_t zstd_compress(struct perf_session *session, struct mmap *map,
|
static ssize_t zstd_compress(struct perf_session *session, struct mmap *map,
|
||||||
void *dst, size_t dst_size, void *src, size_t src_size);
|
void *dst, size_t dst_size, void *src, size_t src_size);
|
||||||
|
|
||||||
#ifdef HAVE_AIO_SUPPORT
|
#ifdef HAVE_AIO_SUPPORT
|
||||||
@ -405,9 +405,13 @@ static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
if (record__comp_enabled(aio->rec)) {
|
if (record__comp_enabled(aio->rec)) {
|
||||||
size = zstd_compress(aio->rec->session, NULL, aio->data + aio->size,
|
ssize_t compressed = zstd_compress(aio->rec->session, NULL, aio->data + aio->size,
|
||||||
mmap__mmap_len(map) - aio->size,
|
mmap__mmap_len(map) - aio->size,
|
||||||
buf, size);
|
buf, size);
|
||||||
|
if (compressed < 0)
|
||||||
|
return (int)compressed;
|
||||||
|
|
||||||
|
size = compressed;
|
||||||
} else {
|
} else {
|
||||||
memcpy(aio->data + aio->size, buf, size);
|
memcpy(aio->data + aio->size, buf, size);
|
||||||
}
|
}
|
||||||
@ -633,7 +637,13 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
|
|||||||
struct record *rec = to;
|
struct record *rec = to;
|
||||||
|
|
||||||
if (record__comp_enabled(rec)) {
|
if (record__comp_enabled(rec)) {
|
||||||
size = zstd_compress(rec->session, map, map->data, mmap__mmap_len(map), bf, size);
|
ssize_t compressed = zstd_compress(rec->session, map, map->data,
|
||||||
|
mmap__mmap_len(map), bf, size);
|
||||||
|
|
||||||
|
if (compressed < 0)
|
||||||
|
return (int)compressed;
|
||||||
|
|
||||||
|
size = compressed;
|
||||||
bf = map->data;
|
bf = map->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1527,10 +1537,10 @@ static size_t process_comp_header(void *record, size_t increment)
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t zstd_compress(struct perf_session *session, struct mmap *map,
|
static ssize_t zstd_compress(struct perf_session *session, struct mmap *map,
|
||||||
void *dst, size_t dst_size, void *src, size_t src_size)
|
void *dst, size_t dst_size, void *src, size_t src_size)
|
||||||
{
|
{
|
||||||
size_t compressed;
|
ssize_t compressed;
|
||||||
size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
|
size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
|
||||||
struct zstd_data *zstd_data = &session->zstd_data;
|
struct zstd_data *zstd_data = &session->zstd_data;
|
||||||
|
|
||||||
@ -1539,6 +1549,8 @@ static size_t zstd_compress(struct perf_session *session, struct mmap *map,
|
|||||||
|
|
||||||
compressed = zstd_compress_stream_to_records(zstd_data, dst, dst_size, src, src_size,
|
compressed = zstd_compress_stream_to_records(zstd_data, dst, dst_size, src, src_size,
|
||||||
max_record_size, process_comp_header);
|
max_record_size, process_comp_header);
|
||||||
|
if (compressed < 0)
|
||||||
|
return compressed;
|
||||||
|
|
||||||
if (map && map->file) {
|
if (map && map->file) {
|
||||||
thread->bytes_transferred += src_size;
|
thread->bytes_transferred += src_size;
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
#define PERF_COMPRESS_H
|
#define PERF_COMPRESS_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#ifdef HAVE_ZSTD_SUPPORT
|
#ifdef HAVE_ZSTD_SUPPORT
|
||||||
#include <zstd.h>
|
#include <zstd.h>
|
||||||
#endif
|
#endif
|
||||||
@ -21,6 +23,7 @@ struct zstd_data {
|
|||||||
#ifdef HAVE_ZSTD_SUPPORT
|
#ifdef HAVE_ZSTD_SUPPORT
|
||||||
ZSTD_CStream *cstream;
|
ZSTD_CStream *cstream;
|
||||||
ZSTD_DStream *dstream;
|
ZSTD_DStream *dstream;
|
||||||
|
int comp_level;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -29,7 +32,7 @@ struct zstd_data {
|
|||||||
int zstd_init(struct zstd_data *data, int level);
|
int zstd_init(struct zstd_data *data, int level);
|
||||||
int zstd_fini(struct zstd_data *data);
|
int zstd_fini(struct zstd_data *data);
|
||||||
|
|
||||||
size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size,
|
ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size,
|
||||||
void *src, size_t src_size, size_t max_record_size,
|
void *src, size_t src_size, size_t max_record_size,
|
||||||
size_t process_header(void *record, size_t increment));
|
size_t process_header(void *record, size_t increment));
|
||||||
|
|
||||||
@ -48,7 +51,7 @@ static inline int zstd_fini(struct zstd_data *data __maybe_unused)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
size_t zstd_compress_stream_to_records(struct zstd_data *data __maybe_unused,
|
ssize_t zstd_compress_stream_to_records(struct zstd_data *data __maybe_unused,
|
||||||
void *dst __maybe_unused, size_t dst_size __maybe_unused,
|
void *dst __maybe_unused, size_t dst_size __maybe_unused,
|
||||||
void *src __maybe_unused, size_t src_size __maybe_unused,
|
void *src __maybe_unused, size_t src_size __maybe_unused,
|
||||||
size_t max_record_size __maybe_unused,
|
size_t max_record_size __maybe_unused,
|
||||||
|
@ -295,15 +295,14 @@ int mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, struct perf_cpu
|
|||||||
|
|
||||||
map->core.flush = mp->flush;
|
map->core.flush = mp->flush;
|
||||||
|
|
||||||
map->comp_level = mp->comp_level;
|
|
||||||
#ifndef PYTHON_PERF
|
#ifndef PYTHON_PERF
|
||||||
if (zstd_init(&map->zstd_data, map->comp_level)) {
|
if (zstd_init(&map->zstd_data, mp->comp_level)) {
|
||||||
pr_debug2("failed to init mmap compressor, error %d\n", errno);
|
pr_debug2("failed to init mmap compressor, error %d\n", errno);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (map->comp_level && !perf_mmap__aio_enabled(map)) {
|
if (mp->comp_level && !perf_mmap__aio_enabled(map)) {
|
||||||
map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
|
map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
|
||||||
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
|
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
|
||||||
if (map->data == MAP_FAILED) {
|
if (map->data == MAP_FAILED) {
|
||||||
|
@ -39,7 +39,6 @@ struct mmap {
|
|||||||
#endif
|
#endif
|
||||||
struct mmap_cpu_mask affinity_mask;
|
struct mmap_cpu_mask affinity_mask;
|
||||||
void *data;
|
void *data;
|
||||||
int comp_level;
|
|
||||||
struct perf_data_file *file;
|
struct perf_data_file *file;
|
||||||
struct zstd_data zstd_data;
|
struct zstd_data zstd_data;
|
||||||
};
|
};
|
||||||
|
@ -7,35 +7,9 @@
|
|||||||
|
|
||||||
int zstd_init(struct zstd_data *data, int level)
|
int zstd_init(struct zstd_data *data, int level)
|
||||||
{
|
{
|
||||||
size_t ret;
|
data->comp_level = level;
|
||||||
|
data->dstream = NULL;
|
||||||
data->dstream = ZSTD_createDStream();
|
data->cstream = NULL;
|
||||||
if (data->dstream == NULL) {
|
|
||||||
pr_err("Couldn't create decompression stream.\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ZSTD_initDStream(data->dstream);
|
|
||||||
if (ZSTD_isError(ret)) {
|
|
||||||
pr_err("Failed to initialize decompression stream: %s\n", ZSTD_getErrorName(ret));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!level)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
data->cstream = ZSTD_createCStream();
|
|
||||||
if (data->cstream == NULL) {
|
|
||||||
pr_err("Couldn't create compression stream.\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ZSTD_initCStream(data->cstream, level);
|
|
||||||
if (ZSTD_isError(ret)) {
|
|
||||||
pr_err("Failed to initialize compression stream: %s\n", ZSTD_getErrorName(ret));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +28,7 @@ int zstd_fini(struct zstd_data *data)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size,
|
ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size,
|
||||||
void *src, size_t src_size, size_t max_record_size,
|
void *src, size_t src_size, size_t max_record_size,
|
||||||
size_t process_header(void *record, size_t increment))
|
size_t process_header(void *record, size_t increment))
|
||||||
{
|
{
|
||||||
@ -63,6 +37,21 @@ size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t
|
|||||||
ZSTD_outBuffer output;
|
ZSTD_outBuffer output;
|
||||||
void *record;
|
void *record;
|
||||||
|
|
||||||
|
if (!data->cstream) {
|
||||||
|
data->cstream = ZSTD_createCStream();
|
||||||
|
if (data->cstream == NULL) {
|
||||||
|
pr_err("Couldn't create compression stream.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ZSTD_initCStream(data->cstream, data->comp_level);
|
||||||
|
if (ZSTD_isError(ret)) {
|
||||||
|
pr_err("Failed to initialize compression stream: %s\n",
|
||||||
|
ZSTD_getErrorName(ret));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (input.pos < input.size) {
|
while (input.pos < input.size) {
|
||||||
record = dst;
|
record = dst;
|
||||||
size = process_header(record, 0);
|
size = process_header(record, 0);
|
||||||
@ -96,6 +85,20 @@ size_t zstd_decompress_stream(struct zstd_data *data, void *src, size_t src_size
|
|||||||
ZSTD_inBuffer input = { src, src_size, 0 };
|
ZSTD_inBuffer input = { src, src_size, 0 };
|
||||||
ZSTD_outBuffer output = { dst, dst_size, 0 };
|
ZSTD_outBuffer output = { dst, dst_size, 0 };
|
||||||
|
|
||||||
|
if (!data->dstream) {
|
||||||
|
data->dstream = ZSTD_createDStream();
|
||||||
|
if (data->dstream == NULL) {
|
||||||
|
pr_err("Couldn't create decompression stream.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ZSTD_initDStream(data->dstream);
|
||||||
|
if (ZSTD_isError(ret)) {
|
||||||
|
pr_err("Failed to initialize decompression stream: %s\n",
|
||||||
|
ZSTD_getErrorName(ret));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
while (input.pos < input.size) {
|
while (input.pos < input.size) {
|
||||||
ret = ZSTD_decompressStream(data->dstream, &output, &input);
|
ret = ZSTD_decompressStream(data->dstream, &output, &input);
|
||||||
if (ZSTD_isError(ret)) {
|
if (ZSTD_isError(ret)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user