Skip to content

Commit ecfd7a9

Browse files
WangNan0acmel
authored andcommitted
perf record: Add '--timestamp-filename' option to append timestamp to output file name
This option appends current timestamp to the output file name. For example: # perf record -a --timestamp-filename ^C[ perf record: Woken up 1 times to write data ] [ perf record: Dump perf.data.2015122622265847 ] [ perf record: Captured and wrote 0.742 MB perf.data.<timestamp> (90 samples) ] # ls perf.data.201512262226584 The timestamp will be useful for identifying each perf.data after the 'perf record' support for generating multiple output files gets introduced. Signed-off-by: Wang Nan <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Zefan Li <[email protected]> Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: He Kuang <[email protected]> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent c0bdc1c commit ecfd7a9

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

tools/perf/builtin-record.c

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct record {
5656
bool no_buildid_cache;
5757
bool no_buildid_cache_set;
5858
bool buildid_all;
59+
bool timestamp_filename;
5960
unsigned long long samples;
6061
};
6162

@@ -531,6 +532,37 @@ record__finish_output(struct record *rec)
531532
return;
532533
}
533534

535+
static int
536+
record__switch_output(struct record *rec, bool at_exit)
537+
{
538+
struct perf_data_file *file = &rec->file;
539+
int fd, err;
540+
541+
/* Same Size: "2015122520103046"*/
542+
char timestamp[] = "InvalidTimestamp";
543+
544+
rec->samples = 0;
545+
record__finish_output(rec);
546+
err = fetch_current_timestamp(timestamp, sizeof(timestamp));
547+
if (err) {
548+
pr_err("Failed to get current timestamp\n");
549+
return -EINVAL;
550+
}
551+
552+
fd = perf_data_file__switch(file, timestamp,
553+
rec->session->header.data_offset,
554+
at_exit);
555+
if (fd >= 0 && !at_exit) {
556+
rec->bytes_written = 0;
557+
rec->session->header.data_size = 0;
558+
}
559+
560+
if (!quiet)
561+
fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
562+
file->path, timestamp);
563+
return fd;
564+
}
565+
534566
static volatile int workload_exec_errno;
535567

536568
/*
@@ -865,21 +897,32 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
865897
/* this will be recalculated during process_buildids() */
866898
rec->samples = 0;
867899

868-
if (!err)
869-
record__finish_output(rec);
900+
if (!err) {
901+
if (!rec->timestamp_filename) {
902+
record__finish_output(rec);
903+
} else {
904+
fd = record__switch_output(rec, true);
905+
if (fd < 0) {
906+
status = fd;
907+
goto out_delete_session;
908+
}
909+
}
910+
}
870911

871912
if (!err && !quiet) {
872913
char samples[128];
914+
const char *postfix = rec->timestamp_filename ?
915+
".<timestamp>" : "";
873916

874917
if (rec->samples && !rec->opts.full_auxtrace)
875918
scnprintf(samples, sizeof(samples),
876919
" (%" PRIu64 " samples)", rec->samples);
877920
else
878921
samples[0] = '\0';
879922

880-
fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s ]\n",
923+
fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s ]\n",
881924
perf_data_file__size(file) / 1024.0 / 1024.0,
882-
file->path, samples);
925+
file->path, postfix, samples);
883926
}
884927

885928
out_delete_session:
@@ -1249,6 +1292,8 @@ struct option __record_options[] = {
12491292
"file", "vmlinux pathname"),
12501293
OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
12511294
"Record build-id of all DSOs regardless of hits"),
1295+
OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
1296+
"append timestamp to output filename"),
12521297
OPT_END()
12531298
};
12541299

0 commit comments

Comments
 (0)