perf data: Add global path holder

Add a 'path' member to 'struct perf_data'. It will keep the configured
path for the data (const char *). The path in struct perf_data_file is
now dynamically allocated (duped) from it.

This scheme is useful/used in following patches where struct
perf_data::path holds the 'configure' directory path and struct
perf_data_file::path holds the allocated path for specific files.

Also it actually makes the code little simpler.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/20190221094145.9151-3-jolsa@kernel.org
[ Fixup data-convert-bt.c missing conversion ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Jiri Olsa
2019-02-21 10:41:30 +01:00
committed by Arnaldo Carvalho de Melo
parent 45112e89a8
commit 2d4f27999b
21 changed files with 87 additions and 93 deletions

View File

@@ -1578,7 +1578,7 @@ int bt_convert__perf2ctf(const char *input, const char *path,
{
struct perf_session *session;
struct perf_data data = {
.file = { .path = input, .fd = -1 },
.path = input,
.mode = PERF_DATA_MODE_READ,
.force = opts->force,
};
@@ -1650,7 +1650,7 @@ int bt_convert__perf2ctf(const char *input, const char *path,
fprintf(stderr,
"[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
data.file.path, path);
data.path, path);
fprintf(stderr,
"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",

View File

@@ -19,11 +19,11 @@ static bool check_pipe(struct perf_data *data)
int fd = perf_data__is_read(data) ?
STDIN_FILENO : STDOUT_FILENO;
if (!data->file.path) {
if (!data->path) {
if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
is_pipe = true;
} else {
if (!strcmp(data->file.path, "-"))
if (!strcmp(data->path, "-"))
is_pipe = true;
}
@@ -37,13 +37,13 @@ static int check_backup(struct perf_data *data)
{
struct stat st;
if (!stat(data->file.path, &st) && st.st_size) {
if (!stat(data->path, &st) && st.st_size) {
/* TODO check errors properly */
char oldname[PATH_MAX];
snprintf(oldname, sizeof(oldname), "%s.old",
data->file.path);
data->path);
unlink(oldname);
rename(data->file.path, oldname);
rename(data->path, oldname);
}
return 0;
@@ -115,8 +115,22 @@ static int open_file(struct perf_data *data)
fd = perf_data__is_read(data) ?
open_file_read(data) : open_file_write(data);
if (fd < 0) {
free(data->file.path);
return -1;
}
data->file.fd = fd;
return fd < 0 ? -1 : 0;
return 0;
}
static int open_file_dup(struct perf_data *data)
{
data->file.path = strdup(data->path);
if (!data->file.path)
return -ENOMEM;
return open_file(data);
}
int perf_data__open(struct perf_data *data)
@@ -124,14 +138,15 @@ int perf_data__open(struct perf_data *data)
if (check_pipe(data))
return 0;
if (!data->file.path)
data->file.path = "perf.data";
if (!data->path)
data->path = "perf.data";
return open_file(data);
return open_file_dup(data);
}
void perf_data__close(struct perf_data *data)
{
free(data->file.path);
close(data->file.fd);
}
@@ -159,15 +174,15 @@ int perf_data__switch(struct perf_data *data,
if (perf_data__is_read(data))
return -EINVAL;
if (asprintf(&new_filepath, "%s.%s", data->file.path, postfix) < 0)
if (asprintf(&new_filepath, "%s.%s", data->path, postfix) < 0)
return -ENOMEM;
/*
* Only fire a warning, don't return error, continue fill
* original file.
*/
if (rename(data->file.path, new_filepath))
pr_warning("Failed to rename %s to %s\n", data->file.path, new_filepath);
if (rename(data->path, new_filepath))
pr_warning("Failed to rename %s to %s\n", data->path, new_filepath);
if (!at_exit) {
close(data->file.fd);

View File

@@ -10,12 +10,13 @@ enum perf_data_mode {
};
struct perf_data_file {
const char *path;
char *path;
int fd;
unsigned long size;
};
struct perf_data {
const char *path;
struct perf_data_file file;
bool is_pipe;
bool force;