[PATCH v1 09/58] perf data: Add open flag
From: Ian Rogers
Date: Sun Apr 19 2026 - 20:02:19 EST
Avoid double opens and ensure only open files are closed. This
addresses some issues with python integration where the data file
wants to be opened before being given to a session.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/data.c | 21 ++++++++++++++++++---
tools/perf/util/data.h | 4 +++-
2 files changed, 21 insertions(+), 4 deletions(-)
diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index 94dc534a7386..9a7195c68a16 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -360,9 +360,16 @@ static int open_dir(struct perf_data *data)
int perf_data__open(struct perf_data *data)
{
- if (check_pipe(data))
+ int ret;
+
+ if (data->open)
return 0;
+ if (check_pipe(data)) {
+ data->open = true;
+ return 0;
+ }
+
/* currently it allows stdio for pipe only */
data->file.use_stdio = false;
@@ -375,16 +382,24 @@ int perf_data__open(struct perf_data *data)
if (perf_data__is_read(data))
data->is_dir = is_dir(data);
- return perf_data__is_dir(data) ?
- open_dir(data) : open_file_dup(data);
+ ret = perf_data__is_dir(data) ? open_dir(data) : open_file_dup(data);
+
+ if (!ret)
+ data->open = true;
+
+ return ret;
}
void perf_data__close(struct perf_data *data)
{
+ if (!data->open)
+ return;
+
if (perf_data__is_dir(data))
perf_data__close_dir(data);
perf_data_file__close(&data->file);
+ data->open = false;
}
static ssize_t perf_data_file__read(struct perf_data_file *file, void *buf, size_t size)
diff --git a/tools/perf/util/data.h b/tools/perf/util/data.h
index 8299fb5fa7da..76f57f60361f 100644
--- a/tools/perf/util/data.h
+++ b/tools/perf/util/data.h
@@ -50,6 +50,8 @@ struct perf_data {
const char *path;
/** @file: Underlying file to be used. */
struct perf_data_file file;
+ /** @open: Has the file or directory been opened. */
+ bool open;
/** @is_pipe: Underlying file is a pipe. */
bool is_pipe;
/** @is_dir: Underlying file is a directory. */
@@ -59,7 +61,7 @@ struct perf_data {
/** @in_place_update: A file opened for reading but will be written to. */
bool in_place_update;
/** @mode: Read or write mode. */
- enum perf_data_mode mode;
+ enum perf_data_mode mode:8;
struct {
/** @version: perf_dir_version. */
--
2.54.0.rc1.513.gad8abe7a5a-goog