perf data: Add open flag

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.

Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alice Rogers <alice.mei.rogers@gmail.com>
Cc: Dapeng Mi <dapeng1.mi@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers
2026-06-15 18:15:26 -07:00
committed by Namhyung Kim
parent 7c11450939
commit ea43eb1151
2 changed files with 25 additions and 6 deletions

View File

@@ -345,9 +345,10 @@ static int open_dir(struct perf_data *data)
if (asprintf(&data->file.path, "%s/data", data->path) < 0)
return -1;
if (perf_data__is_write(data) &&
mkdir(data->path, S_IRWXU) < 0)
if (perf_data__is_write(data) && mkdir(data->path, 0700) < 0) {
zfree(&data->file.path);
return -1;
}
ret = open_file(data);
@@ -360,9 +361,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 +383,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)
@@ -457,6 +473,7 @@ int perf_data__switch(struct perf_data *data,
if (!at_exit) {
perf_data_file__close(&data->file);
data->open = false;
ret = perf_data__open(data);
if (ret < 0)
goto out;

View File

@@ -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. */