[PATCH RFC 1/2] perf tools: Support single perf.data file directory

From: Adrian Hunter
Date: Mon Sep 16 2019 - 04:58:06 EST


Support directory output that contains a regular perf.data file. This is
preparation for adding support for putting a copy of /proc/kcore in that
directory.

Distinguish the multiple file case from the regular (single) perf.data file
case by adding data->is_multi_file.

Signed-off-by: Adrian Hunter <adrian.hunter@xxxxxxxxx>
---
tools/perf/builtin-record.c | 2 +-
tools/perf/util/data.c | 37 +++++++++++++++++++++++++++----------
tools/perf/util/data.h | 6 ++++++
tools/perf/util/header.h | 1 +
tools/perf/util/session.c | 2 +-
tools/perf/util/util.c | 1 +
6 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 1447004eee8a..4c356a046dd3 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -534,7 +534,7 @@ static int record__process_auxtrace(struct perf_tool *tool,
size_t padding;
u8 pad[8] = {0};

- if (!perf_data__is_pipe(data) && !perf_data__is_dir(data)) {
+ if (!perf_data__is_pipe(data) && !perf_data__is_multi_file(data)) {
off_t file_offset;
int fd = perf_data__fd(data);
int err;
diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index 88fba2ba549f..2fd3114d1167 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -37,7 +37,7 @@ int perf_data__create_dir(struct perf_data *data, int nr)
struct perf_data_file *files = NULL;
int i, ret = -1;

- if (WARN_ON(!data->is_dir))
+ if (WARN_ON(!data->is_multi_file))
return -EINVAL;

files = zalloc(nr * sizeof(*files));
@@ -76,7 +76,7 @@ int perf_data__open_dir(struct perf_data *data)
DIR *dir;
int nr = 0;

- if (WARN_ON(!data->is_dir))
+ if (WARN_ON(!data->is_multi_file))
return -EINVAL;

/* The version is provided by DIR_FORMAT feature. */
@@ -217,6 +217,16 @@ static bool is_dir(struct perf_data *data)
return (st.st_mode & S_IFMT) == S_IFDIR;
}

+static bool is_multi_file(struct perf_data *data)
+{
+ char path[PATH_MAX];
+ struct stat st;
+
+ snprintf(path, sizeof(path), "%s/header", data->path);
+
+ return !stat(path, &st);
+}
+
static int open_file_read(struct perf_data *data)
{
struct stat st;
@@ -302,12 +312,17 @@ static int open_dir(struct perf_data *data)
{
int ret;

- /*
- * So far we open only the header, so we can read the data version and
- * layout.
- */
- if (asprintf(&data->file.path, "%s/header", data->path) < 0)
- return -1;
+ if (perf_data__is_multi_file(data)) {
+ /*
+ * So far we open only the header, so we can read the data version and
+ * layout.
+ */
+ if (asprintf(&data->file.path, "%s/header", data->path) < 0)
+ return -1;
+ } else {
+ if (asprintf(&data->file.path, "%s/perf.data", data->path) < 0)
+ return -1;
+ }

if (perf_data__is_write(data) &&
mkdir(data->path, S_IRWXU) < 0)
@@ -333,8 +348,10 @@ int perf_data__open(struct perf_data *data)
if (check_backup(data))
return -1;

- if (perf_data__is_read(data))
+ if (perf_data__is_read(data)) {
data->is_dir = is_dir(data);
+ data->is_multi_file = is_multi_file(data);
+ }

return perf_data__is_dir(data) ?
open_dir(data) : open_file_dup(data);
@@ -406,7 +423,7 @@ unsigned long perf_data__size(struct perf_data *data)
u64 size = data->file.size;
int i;

- if (!data->is_dir)
+ if (!data->is_multi_file)
return size;

for (i = 0; i < data->dir.nr; i++) {
diff --git a/tools/perf/util/data.h b/tools/perf/util/data.h
index 259868a39019..0c5994d969c7 100644
--- a/tools/perf/util/data.h
+++ b/tools/perf/util/data.h
@@ -20,6 +20,7 @@ struct perf_data {
struct perf_data_file file;
bool is_pipe;
bool is_dir;
+ bool is_multi_file;
bool force;
enum perf_data_mode mode;

@@ -50,6 +51,11 @@ static inline bool perf_data__is_dir(struct perf_data *data)
return data->is_dir;
}

+static inline bool perf_data__is_multi_file(struct perf_data *data)
+{
+ return data->is_multi_file;
+}
+
static inline int perf_data__fd(struct perf_data *data)
{
return data->file.fd;
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index 999dac41871e..73ad217958b8 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -53,6 +53,7 @@ enum perf_header_version {
};

enum perf_dir_version {
+ PERF_DIR_SINGLE_FILE = 0,
PERF_DIR_VERSION = 1,
};

diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 2b583e6adb49..6dd04bd092d1 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -222,7 +222,7 @@ struct perf_session *perf_session__new(struct perf_data *data,
perf_evlist__init_trace_event_sample_raw(session->evlist);

/* Open the directory data. */
- if (data->is_dir && perf_data__open_dir(data))
+ if (data->is_multi_file && perf_data__open_dir(data))
goto out_delete;
}
} else {
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 32322a20a68b..5ca7490afdd1 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -189,6 +189,7 @@ int rm_rf_perf_data(const char *path)
const char *pat[] = {
"header",
"data.*",
+ "perf.data",
NULL,
};

--
2.17.1