Skip to content

Commit 040f991

Browse files
WangNan0acmel
authored andcommitted
perf data: Add perf_data_file__switch() helper
perf_data_file__switch() closes current output file, renames it, then open a new one to continue recording. It will be used by 'perf record' to split output into multiple perf.data files. Signed-off-by: Wang Nan <wangnan0@huawei.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1460535673-159866-3-git-send-email-wangnan0@huawei.com Signed-off-by: He Kuang <hekuang@huawei.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent b26dc73 commit 040f991

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

tools/perf/util/data.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,44 @@ ssize_t perf_data_file__write(struct perf_data_file *file,
136136
{
137137
return writen(file->fd, buf, size);
138138
}
139+
140+
int perf_data_file__switch(struct perf_data_file *file,
141+
const char *postfix,
142+
size_t pos, bool at_exit)
143+
{
144+
char *new_filepath;
145+
int ret;
146+
147+
if (check_pipe(file))
148+
return -EINVAL;
149+
if (perf_data_file__is_read(file))
150+
return -EINVAL;
151+
152+
if (asprintf(&new_filepath, "%s.%s", file->path, postfix) < 0)
153+
return -ENOMEM;
154+
155+
/*
156+
* Only fire a warning, don't return error, continue fill
157+
* original file.
158+
*/
159+
if (rename(file->path, new_filepath))
160+
pr_warning("Failed to rename %s to %s\n", file->path, new_filepath);
161+
162+
if (!at_exit) {
163+
close(file->fd);
164+
ret = perf_data_file__open(file);
165+
if (ret < 0)
166+
goto out;
167+
168+
if (lseek(file->fd, pos, SEEK_SET) == (off_t)-1) {
169+
ret = -errno;
170+
pr_debug("Failed to lseek to %zu: %s",
171+
pos, strerror(errno));
172+
goto out;
173+
}
174+
}
175+
ret = file->fd;
176+
out:
177+
free(new_filepath);
178+
return ret;
179+
}

tools/perf/util/data.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,14 @@ int perf_data_file__open(struct perf_data_file *file);
4646
void perf_data_file__close(struct perf_data_file *file);
4747
ssize_t perf_data_file__write(struct perf_data_file *file,
4848
void *buf, size_t size);
49-
49+
/*
50+
* If at_exit is set, only rename current perf.data to
51+
* perf.data.<postfix>, continue write on original file.
52+
* Set at_exit when flushing the last output.
53+
*
54+
* Return value is fd of new output.
55+
*/
56+
int perf_data_file__switch(struct perf_data_file *file,
57+
const char *postfix,
58+
size_t pos, bool at_exit);
5059
#endif /* __PERF_DATA_H */

0 commit comments

Comments
 (0)