diff options
author | Pierre Gondois <pierre.gondois@arm.com> | 2024-01-23 14:42:12 +0100 |
---|---|---|
committer | Steven Rostedt (Google) <rostedt@goodmis.org> | 2024-01-24 14:59:33 -0500 |
commit | 2ed5f2e845d8c0d3b52d5332bf834897cf089416 (patch) | |
tree | 50e27f73dc58c4af1ec8d9a7414ca582e6e22d27 | |
parent | caa9eda091d621f38b46077bc4fcd938354a8f42 (diff) | |
download | trace-cmd-2ed5f2e845d8c0d3b52d5332bf834897cf089416.tar.gz |
trace-cmd split: Add functions to generate temp files
To prepare handling of multiple instances and storing them
in temporary files, add utility functions generating file names,
removing files, creating files:
- get_temp_file()
- delete_temp_file()
- put_temp_file()
- touch_file()
Also make use these functions.
Link: https://lore.kernel.org/linux-trace-devel/20240123134215.385415-3-pierre.gondois@arm.com
Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
-rw-r--r-- | tracecmd/trace-split.c | 73 |
1 files changed, 58 insertions, 15 deletions
diff --git a/tracecmd/trace-split.c b/tracecmd/trace-split.c index 13fa8b7e..9e882983 100644 --- a/tracecmd/trace-split.c +++ b/tracecmd/trace-split.c @@ -421,6 +421,55 @@ static int parse_cpu(struct tracecmd_input *handle, return 0; } +static char *get_temp_file(const char *output_file, const char *name, int cpu) +{ + const char *dot; + char *file = NULL; + char *output; + char *base; + char *dir; + int ret; + + if (name) + dot = "."; + else + dot = name = ""; + + output = strdup(output_file); + if (!output) + die("Failed to duplicate %s", output_file); + + /* Extract basename() first, as dirname() truncates output */ + base = basename(output); + dir = dirname(output); + + ret = asprintf(&file, "%s/.tmp.%s.%s%s%d", dir, base, name, dot, cpu); + if (ret < 0) + die("Failed to allocate file for %s %s %s %d", dir, base, name, cpu); + free(output); + return file; +} + +static void delete_temp_file(const char *name) +{ + unlink(name); +} + +static void put_temp_file(char *file) +{ + free(file); +} + +static void touch_file(const char *file) +{ + int fd; + + fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + die("could not create file %s\n", file); + close(fd); +} + static unsigned long long parse_file(struct tracecmd_input *handle, const char *output_file, unsigned long long start, @@ -434,19 +483,11 @@ static unsigned long long parse_file(struct tracecmd_input *handle, struct cpu_data *cpu_data; struct tep_record *record; char **cpu_list; - char *output; - char *base; char *file; - char *dir; int cpus; int cpu; int fd; - output = strdup(output_file); - /* Extract basename() first, as dirname() truncates output */ - base = basename(output); - dir = dirname(output); - ohandle = tracecmd_copy(handle, output_file, TRACECMD_FILE_CMD_LINES, 0, NULL); cpus = tracecmd_cpus(handle); @@ -455,11 +496,9 @@ static unsigned long long parse_file(struct tracecmd_input *handle, die("Failed to allocate cpu_data for %d cpus", cpus); for (cpu = 0; cpu < cpus; cpu++) { - int ret; + file = get_temp_file(output_file, NULL, cpu); + touch_file(file); - ret = asprintf(&file, "%s/.tmp.%s.%d", dir, base, cpu); - if (ret < 0) - die("Failed to allocate file for %s %s %d", dir, base, cpu); fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); cpu_data[cpu].cpu = cpu; cpu_data[cpu].fd = fd; @@ -498,12 +537,16 @@ static unsigned long long parse_file(struct tracecmd_input *handle, current = record->ts + 1; tracecmd_free_record(record); } - unlink(cpu_data[cpu].file); - free(cpu_data[cpu].file); + } + + for (cpu = 0; cpu < cpus; cpu++) { + close(cpu_data[cpu].fd); + delete_temp_file(cpu_data[cpu].file); + put_temp_file(cpu_data[cpu].file); } free(cpu_data); free(cpu_list); - free(output); + tracecmd_output_close(ohandle); return current; |