Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Wang Nan and committed by
Arnaldo Carvalho de Melo
040f9915 b26dc730

+51 -1
+41
tools/perf/util/data.c
··· 136 136 { 137 137 return writen(file->fd, buf, size); 138 138 } 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 + }
+10 -1
tools/perf/util/data.h
··· 46 46 void perf_data_file__close(struct perf_data_file *file); 47 47 ssize_t perf_data_file__write(struct perf_data_file *file, 48 48 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); 50 59 #endif /* __PERF_DATA_H */