aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@redhat.com>2013-10-15 10:27:32 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2013-10-21 16:33:24 -0400
commitf5fc14124c5cefdd052a2b2a6a3f0ed531540113 (patch)
tree9cef9b2a75334478486869ff002749679a9e488d /tools/perf
parent09600e0f9ebb06235b852a646a3644b7d4a71aca (diff)
perf tools: Add data object to handle perf data file
This patch is adding 'struct perf_data_file' object as a placeholder for all attributes regarding perf.data file handling. Changing perf_session__new to take it as an argument. The rest of the functionality will be added later to keep this change simple enough, because all the places using perf_session are changed now. Signed-off-by: Jiri Olsa <jolsa@redhat.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Andi Kleen <andi@firstfloor.org> Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/1381847254-28809-2-git-send-email-jolsa@redhat.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf')
-rw-r--r--tools/perf/builtin-annotate.c9
-rw-r--r--tools/perf/builtin-buildid-cache.c8
-rw-r--r--tools/perf/builtin-buildid-list.c9
-rw-r--r--tools/perf/builtin-diff.c19
-rw-r--r--tools/perf/builtin-evlist.c7
-rw-r--r--tools/perf/builtin-inject.c7
-rw-r--r--tools/perf/builtin-kmem.c7
-rw-r--r--tools/perf/builtin-kvm.c13
-rw-r--r--tools/perf/builtin-lock.c7
-rw-r--r--tools/perf/builtin-mem.c9
-rw-r--r--tools/perf/builtin-record.c61
-rw-r--r--tools/perf/builtin-report.c10
-rw-r--r--tools/perf/builtin-sched.c6
-rw-r--r--tools/perf/builtin-script.c15
-rw-r--r--tools/perf/builtin-timechart.c10
-rw-r--r--tools/perf/builtin-top.c6
-rw-r--r--tools/perf/builtin-trace.c8
-rw-r--r--tools/perf/perf.h1
-rw-r--r--tools/perf/util/data.h29
-rw-r--r--tools/perf/util/session.c12
-rw-r--r--tools/perf/util/session.h6
21 files changed, 186 insertions, 73 deletions
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 94f9a8e78117..95df683d6074 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -28,6 +28,7 @@
28#include "util/hist.h" 28#include "util/hist.h"
29#include "util/session.h" 29#include "util/session.h"
30#include "util/tool.h" 30#include "util/tool.h"
31#include "util/data.h"
31#include "arch/common.h" 32#include "arch/common.h"
32 33
33#include <dlfcn.h> 34#include <dlfcn.h>
@@ -199,9 +200,13 @@ static int __cmd_annotate(struct perf_annotate *ann)
199 struct perf_session *session; 200 struct perf_session *session;
200 struct perf_evsel *pos; 201 struct perf_evsel *pos;
201 u64 total_nr_samples; 202 u64 total_nr_samples;
203 struct perf_data_file file = {
204 .path = input_name,
205 .mode = PERF_DATA_MODE_READ,
206 .force = ann->force,
207 };
202 208
203 session = perf_session__new(input_name, O_RDONLY, 209 session = perf_session__new(&file, false, &ann->tool);
204 ann->force, false, &ann->tool);
205 if (session == NULL) 210 if (session == NULL)
206 return -ENOMEM; 211 return -ENOMEM;
207 212
diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c
index 8140b7b249fa..cfede86161d8 100644
--- a/tools/perf/builtin-buildid-cache.c
+++ b/tools/perf/builtin-buildid-cache.c
@@ -221,8 +221,12 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
221 221
222static int build_id_cache__fprintf_missing(const char *filename, bool force, FILE *fp) 222static int build_id_cache__fprintf_missing(const char *filename, bool force, FILE *fp)
223{ 223{
224 struct perf_session *session = perf_session__new(filename, O_RDONLY, 224 struct perf_data_file file = {
225 force, false, NULL); 225 .path = filename,
226 .mode = PERF_DATA_MODE_READ,
227 .force = force,
228 };
229 struct perf_session *session = perf_session__new(&file, false, NULL);
226 if (session == NULL) 230 if (session == NULL)
227 return -1; 231 return -1;
228 232
diff --git a/tools/perf/builtin-buildid-list.c b/tools/perf/builtin-buildid-list.c
index e74366a13218..0164c1c3e834 100644
--- a/tools/perf/builtin-buildid-list.c
+++ b/tools/perf/builtin-buildid-list.c
@@ -15,6 +15,7 @@
15#include "util/parse-options.h" 15#include "util/parse-options.h"
16#include "util/session.h" 16#include "util/session.h"
17#include "util/symbol.h" 17#include "util/symbol.h"
18#include "util/data.h"
18 19
19static int sysfs__fprintf_build_id(FILE *fp) 20static int sysfs__fprintf_build_id(FILE *fp)
20{ 21{
@@ -52,6 +53,11 @@ static bool dso__skip_buildid(struct dso *dso, int with_hits)
52static int perf_session__list_build_ids(bool force, bool with_hits) 53static int perf_session__list_build_ids(bool force, bool with_hits)
53{ 54{
54 struct perf_session *session; 55 struct perf_session *session;
56 struct perf_data_file file = {
57 .path = input_name,
58 .mode = PERF_DATA_MODE_READ,
59 .force = force,
60 };
55 61
56 symbol__elf_init(); 62 symbol__elf_init();
57 /* 63 /*
@@ -60,8 +66,7 @@ static int perf_session__list_build_ids(bool force, bool with_hits)
60 if (filename__fprintf_build_id(input_name, stdout)) 66 if (filename__fprintf_build_id(input_name, stdout))
61 goto out; 67 goto out;
62 68
63 session = perf_session__new(input_name, O_RDONLY, force, false, 69 session = perf_session__new(&file, false, &build_id__mark_dso_hit_ops);
64 &build_id__mark_dso_hit_ops);
65 if (session == NULL) 70 if (session == NULL)
66 return -1; 71 return -1;
67 /* 72 /*
diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index 2a78dc806c39..419d27dd708b 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -16,6 +16,7 @@
16#include "util/sort.h" 16#include "util/sort.h"
17#include "util/symbol.h" 17#include "util/symbol.h"
18#include "util/util.h" 18#include "util/util.h"
19#include "util/data.h"
19 20
20#include <stdlib.h> 21#include <stdlib.h>
21#include <math.h> 22#include <math.h>
@@ -42,7 +43,7 @@ struct diff_hpp_fmt {
42 43
43struct data__file { 44struct data__file {
44 struct perf_session *session; 45 struct perf_session *session;
45 const char *file; 46 struct perf_data_file file;
46 int idx; 47 int idx;
47 struct hists *hists; 48 struct hists *hists;
48 struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX]; 49 struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX];
@@ -601,7 +602,7 @@ static void data__fprintf(void)
601 602
602 data__for_each_file(i, d) 603 data__for_each_file(i, d)
603 fprintf(stdout, "# [%d] %s %s\n", 604 fprintf(stdout, "# [%d] %s %s\n",
604 d->idx, d->file, 605 d->idx, d->file.path,
605 !d->idx ? "(Baseline)" : ""); 606 !d->idx ? "(Baseline)" : "");
606 607
607 fprintf(stdout, "#\n"); 608 fprintf(stdout, "#\n");
@@ -663,17 +664,16 @@ static int __cmd_diff(void)
663 int ret = -EINVAL, i; 664 int ret = -EINVAL, i;
664 665
665 data__for_each_file(i, d) { 666 data__for_each_file(i, d) {
666 d->session = perf_session__new(d->file, O_RDONLY, force, 667 d->session = perf_session__new(&d->file, false, &tool);
667 false, &tool);
668 if (!d->session) { 668 if (!d->session) {
669 pr_err("Failed to open %s\n", d->file); 669 pr_err("Failed to open %s\n", d->file.path);
670 ret = -ENOMEM; 670 ret = -ENOMEM;
671 goto out_delete; 671 goto out_delete;
672 } 672 }
673 673
674 ret = perf_session__process_events(d->session, &tool); 674 ret = perf_session__process_events(d->session, &tool);
675 if (ret) { 675 if (ret) {
676 pr_err("Failed to process %s\n", d->file); 676 pr_err("Failed to process %s\n", d->file.path);
677 goto out_delete; 677 goto out_delete;
678 } 678 }
679 679
@@ -1016,7 +1016,12 @@ static int data_init(int argc, const char **argv)
1016 return -ENOMEM; 1016 return -ENOMEM;
1017 1017
1018 data__for_each_file(i, d) { 1018 data__for_each_file(i, d) {
1019 d->file = use_default ? defaults[i] : argv[i]; 1019 struct perf_data_file *file = &d->file;
1020
1021 file->path = use_default ? defaults[i] : argv[i];
1022 file->mode = PERF_DATA_MODE_READ,
1023 file->force = force,
1024
1020 d->idx = i; 1025 d->idx = i;
1021 } 1026 }
1022 1027
diff --git a/tools/perf/builtin-evlist.c b/tools/perf/builtin-evlist.c
index 05bd9dfe875c..20b0f12763b0 100644
--- a/tools/perf/builtin-evlist.c
+++ b/tools/perf/builtin-evlist.c
@@ -14,13 +14,18 @@
14#include "util/parse-events.h" 14#include "util/parse-events.h"
15#include "util/parse-options.h" 15#include "util/parse-options.h"
16#include "util/session.h" 16#include "util/session.h"
17#include "util/data.h"
17 18
18static int __cmd_evlist(const char *file_name, struct perf_attr_details *details) 19static int __cmd_evlist(const char *file_name, struct perf_attr_details *details)
19{ 20{
20 struct perf_session *session; 21 struct perf_session *session;
21 struct perf_evsel *pos; 22 struct perf_evsel *pos;
23 struct perf_data_file file = {
24 .path = file_name,
25 .mode = PERF_DATA_MODE_READ,
26 };
22 27
23 session = perf_session__new(file_name, O_RDONLY, 0, false, NULL); 28 session = perf_session__new(&file, 0, NULL);
24 if (session == NULL) 29 if (session == NULL)
25 return -ENOMEM; 30 return -ENOMEM;
26 31
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index f51a9637f69b..4aa6d7850bcc 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -15,6 +15,7 @@
15#include "util/tool.h" 15#include "util/tool.h"
16#include "util/debug.h" 16#include "util/debug.h"
17#include "util/build-id.h" 17#include "util/build-id.h"
18#include "util/data.h"
18 19
19#include "util/parse-options.h" 20#include "util/parse-options.h"
20 21
@@ -345,6 +346,10 @@ static int __cmd_inject(struct perf_inject *inject)
345{ 346{
346 struct perf_session *session; 347 struct perf_session *session;
347 int ret = -EINVAL; 348 int ret = -EINVAL;
349 struct perf_data_file file = {
350 .path = inject->input_name,
351 .mode = PERF_DATA_MODE_READ,
352 };
348 353
349 signal(SIGINT, sig_handler); 354 signal(SIGINT, sig_handler);
350 355
@@ -355,7 +360,7 @@ static int __cmd_inject(struct perf_inject *inject)
355 inject->tool.tracing_data = perf_event__repipe_tracing_data; 360 inject->tool.tracing_data = perf_event__repipe_tracing_data;
356 } 361 }
357 362
358 session = perf_session__new(inject->input_name, O_RDONLY, false, true, &inject->tool); 363 session = perf_session__new(&file, true, &inject->tool);
359 if (session == NULL) 364 if (session == NULL)
360 return -ENOMEM; 365 return -ENOMEM;
361 366
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 9b5f077fee5b..1126382659a9 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -13,6 +13,7 @@
13 13
14#include "util/parse-options.h" 14#include "util/parse-options.h"
15#include "util/trace-event.h" 15#include "util/trace-event.h"
16#include "util/data.h"
16 17
17#include "util/debug.h" 18#include "util/debug.h"
18 19
@@ -486,8 +487,12 @@ static int __cmd_kmem(void)
486 { "kmem:kfree", perf_evsel__process_free_event, }, 487 { "kmem:kfree", perf_evsel__process_free_event, },
487 { "kmem:kmem_cache_free", perf_evsel__process_free_event, }, 488 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
488 }; 489 };
490 struct perf_data_file file = {
491 .path = input_name,
492 .mode = PERF_DATA_MODE_READ,
493 };
489 494
490 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_kmem); 495 session = perf_session__new(&file, false, &perf_kmem);
491 if (session == NULL) 496 if (session == NULL)
492 return -ENOMEM; 497 return -ENOMEM;
493 498
diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index cfa0b7979914..188bb29b373f 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -17,6 +17,7 @@
17#include "util/tool.h" 17#include "util/tool.h"
18#include "util/stat.h" 18#include "util/stat.h"
19#include "util/top.h" 19#include "util/top.h"
20#include "util/data.h"
20 21
21#include <sys/prctl.h> 22#include <sys/prctl.h>
22#include <sys/timerfd.h> 23#include <sys/timerfd.h>
@@ -1215,10 +1216,13 @@ static int read_events(struct perf_kvm_stat *kvm)
1215 .comm = perf_event__process_comm, 1216 .comm = perf_event__process_comm,
1216 .ordered_samples = true, 1217 .ordered_samples = true,
1217 }; 1218 };
1219 struct perf_data_file file = {
1220 .path = input_name,
1221 .mode = PERF_DATA_MODE_READ,
1222 };
1218 1223
1219 kvm->tool = eops; 1224 kvm->tool = eops;
1220 kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false, 1225 kvm->session = perf_session__new(&file, false, &kvm->tool);
1221 &kvm->tool);
1222 if (!kvm->session) { 1226 if (!kvm->session) {
1223 pr_err("Initializing perf session failed\n"); 1227 pr_err("Initializing perf session failed\n");
1224 return -EINVAL; 1228 return -EINVAL;
@@ -1450,6 +1454,9 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,
1450 "perf kvm stat live [<options>]", 1454 "perf kvm stat live [<options>]",
1451 NULL 1455 NULL
1452 }; 1456 };
1457 struct perf_data_file file = {
1458 .mode = PERF_DATA_MODE_WRITE,
1459 };
1453 1460
1454 1461
1455 /* event handling */ 1462 /* event handling */
@@ -1514,7 +1521,7 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,
1514 /* 1521 /*
1515 * perf session 1522 * perf session
1516 */ 1523 */
1517 kvm->session = perf_session__new(NULL, O_WRONLY, false, false, &kvm->tool); 1524 kvm->session = perf_session__new(&file, false, &kvm->tool);
1518 if (kvm->session == NULL) { 1525 if (kvm->session == NULL) {
1519 err = -ENOMEM; 1526 err = -ENOMEM;
1520 goto out; 1527 goto out;
diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c
index 6a9076f165f4..33c7253295b9 100644
--- a/tools/perf/builtin-lock.c
+++ b/tools/perf/builtin-lock.c
@@ -15,6 +15,7 @@
15#include "util/debug.h" 15#include "util/debug.h"
16#include "util/session.h" 16#include "util/session.h"
17#include "util/tool.h" 17#include "util/tool.h"
18#include "util/data.h"
18 19
19#include <sys/types.h> 20#include <sys/types.h>
20#include <sys/prctl.h> 21#include <sys/prctl.h>
@@ -853,8 +854,12 @@ static int __cmd_report(bool display_info)
853 .comm = perf_event__process_comm, 854 .comm = perf_event__process_comm,
854 .ordered_samples = true, 855 .ordered_samples = true,
855 }; 856 };
857 struct perf_data_file file = {
858 .path = input_name,
859 .mode = PERF_DATA_MODE_READ,
860 };
856 861
857 session = perf_session__new(input_name, O_RDONLY, 0, false, &eops); 862 session = perf_session__new(&file, false, &eops);
858 if (!session) { 863 if (!session) {
859 pr_err("Initializing perf session failed\n"); 864 pr_err("Initializing perf session failed\n");
860 return -ENOMEM; 865 return -ENOMEM;
diff --git a/tools/perf/builtin-mem.c b/tools/perf/builtin-mem.c
index 253133a6251d..31c00f186da1 100644
--- a/tools/perf/builtin-mem.c
+++ b/tools/perf/builtin-mem.c
@@ -5,6 +5,7 @@
5#include "util/trace-event.h" 5#include "util/trace-event.h"
6#include "util/tool.h" 6#include "util/tool.h"
7#include "util/session.h" 7#include "util/session.h"
8#include "util/data.h"
8 9
9#define MEM_OPERATION_LOAD "load" 10#define MEM_OPERATION_LOAD "load"
10#define MEM_OPERATION_STORE "store" 11#define MEM_OPERATION_STORE "store"
@@ -119,10 +120,14 @@ static int process_sample_event(struct perf_tool *tool,
119 120
120static int report_raw_events(struct perf_mem *mem) 121static int report_raw_events(struct perf_mem *mem)
121{ 122{
123 struct perf_data_file file = {
124 .path = input_name,
125 .mode = PERF_DATA_MODE_READ,
126 };
122 int err = -EINVAL; 127 int err = -EINVAL;
123 int ret; 128 int ret;
124 struct perf_session *session = perf_session__new(input_name, O_RDONLY, 129 struct perf_session *session = perf_session__new(&file, false,
125 0, false, &mem->tool); 130 &mem->tool);
126 131
127 if (session == NULL) 132 if (session == NULL)
128 return -ENOMEM; 133 return -ENOMEM;
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index d269dfa3ab08..4ea46ffbfc1c 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -24,6 +24,7 @@
24#include "util/symbol.h" 24#include "util/symbol.h"
25#include "util/cpumap.h" 25#include "util/cpumap.h"
26#include "util/thread_map.h" 26#include "util/thread_map.h"
27#include "util/data.h"
27 28
28#include <unistd.h> 29#include <unistd.h>
29#include <sched.h> 30#include <sched.h>
@@ -65,11 +66,10 @@ struct perf_record {
65 struct perf_tool tool; 66 struct perf_tool tool;
66 struct perf_record_opts opts; 67 struct perf_record_opts opts;
67 u64 bytes_written; 68 u64 bytes_written;
68 const char *output_name; 69 struct perf_data_file file;
69 struct perf_evlist *evlist; 70 struct perf_evlist *evlist;
70 struct perf_session *session; 71 struct perf_session *session;
71 const char *progname; 72 const char *progname;
72 int output;
73 int realtime_prio; 73 int realtime_prio;
74 bool no_buildid; 74 bool no_buildid;
75 bool no_buildid_cache; 75 bool no_buildid_cache;
@@ -84,8 +84,10 @@ static void advance_output(struct perf_record *rec, size_t size)
84 84
85static int write_output(struct perf_record *rec, void *buf, size_t size) 85static int write_output(struct perf_record *rec, void *buf, size_t size)
86{ 86{
87 struct perf_data_file *file = &rec->file;
88
87 while (size) { 89 while (size) {
88 int ret = write(rec->output, buf, size); 90 int ret = write(file->fd, buf, size);
89 91
90 if (ret < 0) { 92 if (ret < 0) {
91 pr_err("failed to write perf data, error: %m\n"); 93 pr_err("failed to write perf data, error: %m\n");
@@ -248,13 +250,15 @@ out:
248 250
249static int process_buildids(struct perf_record *rec) 251static int process_buildids(struct perf_record *rec)
250{ 252{
251 u64 size = lseek(rec->output, 0, SEEK_CUR); 253 struct perf_data_file *file = &rec->file;
254 struct perf_session *session = rec->session;
252 255
256 u64 size = lseek(file->fd, 0, SEEK_CUR);
253 if (size == 0) 257 if (size == 0)
254 return 0; 258 return 0;
255 259
256 rec->session->fd = rec->output; 260 session->fd = file->fd;
257 return __perf_session__process_events(rec->session, rec->post_processing_offset, 261 return __perf_session__process_events(session, rec->post_processing_offset,
258 size - rec->post_processing_offset, 262 size - rec->post_processing_offset,
259 size, &build_id__mark_dso_hit_ops); 263 size, &build_id__mark_dso_hit_ops);
260} 264}
@@ -262,17 +266,18 @@ static int process_buildids(struct perf_record *rec)
262static void perf_record__exit(int status, void *arg) 266static void perf_record__exit(int status, void *arg)
263{ 267{
264 struct perf_record *rec = arg; 268 struct perf_record *rec = arg;
269 struct perf_data_file *file = &rec->file;
265 270
266 if (status != 0) 271 if (status != 0)
267 return; 272 return;
268 273
269 if (!rec->opts.pipe_output) { 274 if (!file->is_pipe) {
270 rec->session->header.data_size += rec->bytes_written; 275 rec->session->header.data_size += rec->bytes_written;
271 276
272 if (!rec->no_buildid) 277 if (!rec->no_buildid)
273 process_buildids(rec); 278 process_buildids(rec);
274 perf_session__write_header(rec->session, rec->evlist, 279 perf_session__write_header(rec->session, rec->evlist,
275 rec->output, true); 280 file->fd, true);
276 perf_session__delete(rec->session); 281 perf_session__delete(rec->session);
277 perf_evlist__delete(rec->evlist); 282 perf_evlist__delete(rec->evlist);
278 symbol__exit(); 283 symbol__exit();
@@ -342,14 +347,15 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
342{ 347{
343 struct stat st; 348 struct stat st;
344 int flags; 349 int flags;
345 int err, output, feat; 350 int err, feat;
346 unsigned long waking = 0; 351 unsigned long waking = 0;
347 const bool forks = argc > 0; 352 const bool forks = argc > 0;
348 struct machine *machine; 353 struct machine *machine;
349 struct perf_tool *tool = &rec->tool; 354 struct perf_tool *tool = &rec->tool;
350 struct perf_record_opts *opts = &rec->opts; 355 struct perf_record_opts *opts = &rec->opts;
351 struct perf_evlist *evsel_list = rec->evlist; 356 struct perf_evlist *evsel_list = rec->evlist;
352 const char *output_name = rec->output_name; 357 struct perf_data_file *file = &rec->file;
358 const char *output_name = file->path;
353 struct perf_session *session; 359 struct perf_session *session;
354 bool disabled = false; 360 bool disabled = false;
355 361
@@ -363,13 +369,13 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
363 369
364 if (!output_name) { 370 if (!output_name) {
365 if (!fstat(STDOUT_FILENO, &st) && S_ISFIFO(st.st_mode)) 371 if (!fstat(STDOUT_FILENO, &st) && S_ISFIFO(st.st_mode))
366 opts->pipe_output = true; 372 file->is_pipe = true;
367 else 373 else
368 rec->output_name = output_name = "perf.data"; 374 file->path = output_name = "perf.data";
369 } 375 }
370 if (output_name) { 376 if (output_name) {
371 if (!strcmp(output_name, "-")) 377 if (!strcmp(output_name, "-"))
372 opts->pipe_output = true; 378 file->is_pipe = true;
373 else if (!stat(output_name, &st) && st.st_size) { 379 else if (!stat(output_name, &st) && st.st_size) {
374 char oldname[PATH_MAX]; 380 char oldname[PATH_MAX];
375 snprintf(oldname, sizeof(oldname), "%s.old", 381 snprintf(oldname, sizeof(oldname), "%s.old",
@@ -381,19 +387,16 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
381 387
382 flags = O_CREAT|O_RDWR|O_TRUNC; 388 flags = O_CREAT|O_RDWR|O_TRUNC;
383 389
384 if (opts->pipe_output) 390 if (file->is_pipe)
385 output = STDOUT_FILENO; 391 file->fd = STDOUT_FILENO;
386 else 392 else
387 output = open(output_name, flags, S_IRUSR | S_IWUSR); 393 file->fd = open(output_name, flags, S_IRUSR | S_IWUSR);
388 if (output < 0) { 394 if (file->fd < 0) {
389 perror("failed to create output file"); 395 perror("failed to create output file");
390 return -1; 396 return -1;
391 } 397 }
392 398
393 rec->output = output; 399 session = perf_session__new(file, false, NULL);
394
395 session = perf_session__new(output_name, O_WRONLY,
396 true, false, NULL);
397 if (session == NULL) { 400 if (session == NULL) {
398 pr_err("Not enough memory for reading perf file header\n"); 401 pr_err("Not enough memory for reading perf file header\n");
399 return -1; 402 return -1;
@@ -415,7 +418,7 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
415 418
416 if (forks) { 419 if (forks) {
417 err = perf_evlist__prepare_workload(evsel_list, &opts->target, 420 err = perf_evlist__prepare_workload(evsel_list, &opts->target,
418 argv, opts->pipe_output, 421 argv, file->is_pipe,
419 true); 422 true);
420 if (err < 0) { 423 if (err < 0) {
421 pr_err("Couldn't run the workload!\n"); 424 pr_err("Couldn't run the workload!\n");
@@ -436,13 +439,13 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
436 */ 439 */
437 on_exit(perf_record__exit, rec); 440 on_exit(perf_record__exit, rec);
438 441
439 if (opts->pipe_output) { 442 if (file->is_pipe) {
440 err = perf_header__write_pipe(output); 443 err = perf_header__write_pipe(file->fd);
441 if (err < 0) 444 if (err < 0)
442 goto out_delete_session; 445 goto out_delete_session;
443 } else { 446 } else {
444 err = perf_session__write_header(session, evsel_list, 447 err = perf_session__write_header(session, evsel_list,
445 output, false); 448 file->fd, false);
446 if (err < 0) 449 if (err < 0)
447 goto out_delete_session; 450 goto out_delete_session;
448 } 451 }
@@ -455,11 +458,11 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
455 goto out_delete_session; 458 goto out_delete_session;
456 } 459 }
457 460
458 rec->post_processing_offset = lseek(output, 0, SEEK_CUR); 461 rec->post_processing_offset = lseek(file->fd, 0, SEEK_CUR);
459 462
460 machine = &session->machines.host; 463 machine = &session->machines.host;
461 464
462 if (opts->pipe_output) { 465 if (file->is_pipe) {
463 err = perf_event__synthesize_attrs(tool, session, 466 err = perf_event__synthesize_attrs(tool, session,
464 process_synthesized_event); 467 process_synthesized_event);
465 if (err < 0) { 468 if (err < 0) {
@@ -476,7 +479,7 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
476 * return this more properly and also 479 * return this more properly and also
477 * propagate errors that now are calling die() 480 * propagate errors that now are calling die()
478 */ 481 */
479 err = perf_event__synthesize_tracing_data(tool, output, evsel_list, 482 err = perf_event__synthesize_tracing_data(tool, file->fd, evsel_list,
480 process_synthesized_event); 483 process_synthesized_event);
481 if (err <= 0) { 484 if (err <= 0) {
482 pr_err("Couldn't record tracing data.\n"); 485 pr_err("Couldn't record tracing data.\n");
@@ -845,7 +848,7 @@ const struct option record_options[] = {
845 OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 848 OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
846 "list of cpus to monitor"), 849 "list of cpus to monitor"),
847 OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 850 OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
848 OPT_STRING('o', "output", &record.output_name, "file", 851 OPT_STRING('o', "output", &record.file.path, "file",
849 "output file name"), 852 "output file name"),
850 OPT_BOOLEAN('i', "no-inherit", &record.opts.no_inherit, 853 OPT_BOOLEAN('i', "no-inherit", &record.opts.no_inherit,
851 "child tasks do not inherit counters"), 854 "child tasks do not inherit counters"),
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 21b5c2f54c2a..60d7f8ec8505 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -33,6 +33,7 @@
33#include "util/thread.h" 33#include "util/thread.h"
34#include "util/sort.h" 34#include "util/sort.h"
35#include "util/hist.h" 35#include "util/hist.h"
36#include "util/data.h"
36#include "arch/common.h" 37#include "arch/common.h"
37 38
38#include <dlfcn.h> 39#include <dlfcn.h>
@@ -857,6 +858,9 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
857 "Don't show entries under that percent", parse_percent_limit), 858 "Don't show entries under that percent", parse_percent_limit),
858 OPT_END() 859 OPT_END()
859 }; 860 };
861 struct perf_data_file file = {
862 .mode = PERF_DATA_MODE_READ,
863 };
860 864
861 perf_config(perf_report_config, &report); 865 perf_config(perf_report_config, &report);
862 866
@@ -886,9 +890,11 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
886 perf_hpp__init(); 890 perf_hpp__init();
887 } 891 }
888 892
893 file.path = input_name;
894 file.force = report.force;
895
889repeat: 896repeat:
890 session = perf_session__new(input_name, O_RDONLY, 897 session = perf_session__new(&file, false, &report.tool);
891 report.force, false, &report.tool);
892 if (session == NULL) 898 if (session == NULL)
893 return -ENOMEM; 899 return -ENOMEM;
894 900
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index d8c51b2f263f..5a46b102eb08 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1446,8 +1446,12 @@ static int perf_sched__read_events(struct perf_sched *sched,
1446 { "sched:sched_migrate_task", process_sched_migrate_task_event, }, 1446 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1447 }; 1447 };
1448 struct perf_session *session; 1448 struct perf_session *session;
1449 struct perf_data_file file = {
1450 .path = input_name,
1451 .mode = PERF_DATA_MODE_READ,
1452 };
1449 1453
1450 session = perf_session__new(input_name, O_RDONLY, 0, false, &sched->tool); 1454 session = perf_session__new(&file, false, &sched->tool);
1451 if (session == NULL) { 1455 if (session == NULL) {
1452 pr_debug("No Memory for session\n"); 1456 pr_debug("No Memory for session\n");
1453 return -1; 1457 return -1;
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index ebb2b5fc6945..f0c77a199616 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -15,6 +15,7 @@
15#include "util/evlist.h" 15#include "util/evlist.h"
16#include "util/evsel.h" 16#include "util/evsel.h"
17#include "util/sort.h" 17#include "util/sort.h"
18#include "util/data.h"
18#include <linux/bitmap.h> 19#include <linux/bitmap.h>
19 20
20static char const *script_name; 21static char const *script_name;
@@ -1115,10 +1116,14 @@ int find_scripts(char **scripts_array, char **scripts_path_array)
1115 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; 1116 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1116 DIR *scripts_dir, *lang_dir; 1117 DIR *scripts_dir, *lang_dir;
1117 struct perf_session *session; 1118 struct perf_session *session;
1119 struct perf_data_file file = {
1120 .path = input_name,
1121 .mode = PERF_DATA_MODE_READ,
1122 };
1118 char *temp; 1123 char *temp;
1119 int i = 0; 1124 int i = 0;
1120 1125
1121 session = perf_session__new(input_name, O_RDONLY, 0, false, NULL); 1126 session = perf_session__new(&file, false, NULL);
1122 if (!session) 1127 if (!session)
1123 return -1; 1128 return -1;
1124 1129
@@ -1319,12 +1324,17 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1319 "perf script [<options>] <top-script> [script-args]", 1324 "perf script [<options>] <top-script> [script-args]",
1320 NULL 1325 NULL
1321 }; 1326 };
1327 struct perf_data_file file = {
1328 .mode = PERF_DATA_MODE_READ,
1329 };
1322 1330
1323 setup_scripting(); 1331 setup_scripting();
1324 1332
1325 argc = parse_options(argc, argv, options, script_usage, 1333 argc = parse_options(argc, argv, options, script_usage,
1326 PARSE_OPT_STOP_AT_NON_OPTION); 1334 PARSE_OPT_STOP_AT_NON_OPTION);
1327 1335
1336 file.path = input_name;
1337
1328 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { 1338 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1329 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); 1339 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1330 if (!rec_script_path) 1340 if (!rec_script_path)
@@ -1488,8 +1498,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1488 if (!script_name) 1498 if (!script_name)
1489 setup_pager(); 1499 setup_pager();
1490 1500
1491 session = perf_session__new(input_name, O_RDONLY, 0, false, 1501 session = perf_session__new(&file, false, &perf_script);
1492 &perf_script);
1493 if (session == NULL) 1502 if (session == NULL)
1494 return -ENOMEM; 1503 return -ENOMEM;
1495 1504
diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
index c2e02319347a..e11c61d9bda4 100644
--- a/tools/perf/builtin-timechart.c
+++ b/tools/perf/builtin-timechart.c
@@ -36,6 +36,7 @@
36#include "util/session.h" 36#include "util/session.h"
37#include "util/svghelper.h" 37#include "util/svghelper.h"
38#include "util/tool.h" 38#include "util/tool.h"
39#include "util/data.h"
39 40
40#define SUPPORT_OLD_POWER_EVENTS 1 41#define SUPPORT_OLD_POWER_EVENTS 1
41#define PWR_EVENT_EXIT -1 42#define PWR_EVENT_EXIT -1
@@ -990,8 +991,13 @@ static int __cmd_timechart(const char *output_name)
990 { "power:power_frequency", process_sample_power_frequency }, 991 { "power:power_frequency", process_sample_power_frequency },
991#endif 992#endif
992 }; 993 };
993 struct perf_session *session = perf_session__new(input_name, O_RDONLY, 994 struct perf_data_file file = {
994 0, false, &perf_timechart); 995 .path = input_name,
996 .mode = PERF_DATA_MODE_READ,
997 };
998
999 struct perf_session *session = perf_session__new(&file, false,
1000 &perf_timechart);
995 int ret = -EINVAL; 1001 int ret = -EINVAL;
996 1002
997 if (session == NULL) 1003 if (session == NULL)
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 65c49b2f51c1..752bebeac3aa 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -929,11 +929,15 @@ static int __cmd_top(struct perf_top *top)
929 struct perf_record_opts *opts = &top->record_opts; 929 struct perf_record_opts *opts = &top->record_opts;
930 pthread_t thread; 930 pthread_t thread;
931 int ret; 931 int ret;
932 struct perf_data_file file = {
933 .mode = PERF_DATA_MODE_WRITE,
934 };
935
932 /* 936 /*
933 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this 937 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
934 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now. 938 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
935 */ 939 */
936 top->session = perf_session__new(NULL, O_WRONLY, false, false, NULL); 940 top->session = perf_session__new(&file, false, NULL);
937 if (top->session == NULL) 941 if (top->session == NULL)
938 return -ENOMEM; 942 return -ENOMEM;
939 943
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index db959ac3d46e..fa620bc1db69 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -1834,7 +1834,10 @@ static int trace__replay(struct trace *trace)
1834 { "raw_syscalls:sys_exit", trace__sys_exit, }, 1834 { "raw_syscalls:sys_exit", trace__sys_exit, },
1835 { "probe:vfs_getname", trace__vfs_getname, }, 1835 { "probe:vfs_getname", trace__vfs_getname, },
1836 }; 1836 };
1837 1837 struct perf_data_file file = {
1838 .path = input_name,
1839 .mode = PERF_DATA_MODE_READ,
1840 };
1838 struct perf_session *session; 1841 struct perf_session *session;
1839 int err = -1; 1842 int err = -1;
1840 1843
@@ -1857,8 +1860,7 @@ static int trace__replay(struct trace *trace)
1857 if (symbol__init() < 0) 1860 if (symbol__init() < 0)
1858 return -1; 1861 return -1;
1859 1862
1860 session = perf_session__new(input_name, O_RDONLY, 0, false, 1863 session = perf_session__new(&file, false, &trace->tool);
1861 &trace->tool);
1862 if (session == NULL) 1864 if (session == NULL)
1863 return -ENOMEM; 1865 return -ENOMEM;
1864 1866
diff --git a/tools/perf/perf.h b/tools/perf/perf.h
index 84502e88488b..f61c230beec4 100644
--- a/tools/perf/perf.h
+++ b/tools/perf/perf.h
@@ -220,7 +220,6 @@ struct perf_record_opts {
220 bool no_delay; 220 bool no_delay;
221 bool no_inherit; 221 bool no_inherit;
222 bool no_samples; 222 bool no_samples;
223 bool pipe_output;
224 bool raw_samples; 223 bool raw_samples;
225 bool sample_address; 224 bool sample_address;
226 bool sample_weight; 225 bool sample_weight;
diff --git a/tools/perf/util/data.h b/tools/perf/util/data.h
new file mode 100644
index 000000000000..ffa0186e6000
--- /dev/null
+++ b/tools/perf/util/data.h
@@ -0,0 +1,29 @@
1#ifndef __PERF_DATA_H
2#define __PERF_DATA_H
3
4#include <stdbool.h>
5
6enum perf_data_mode {
7 PERF_DATA_MODE_WRITE,
8 PERF_DATA_MODE_READ,
9};
10
11struct perf_data_file {
12 const char *path;
13 int fd;
14 bool is_pipe;
15 bool force;
16 enum perf_data_mode mode;
17};
18
19static inline bool perf_data_file__is_read(struct perf_data_file *file)
20{
21 return file->mode == PERF_DATA_MODE_READ;
22}
23
24static inline bool perf_data_file__is_write(struct perf_data_file *file)
25{
26 return file->mode == PERF_DATA_MODE_WRITE;
27}
28
29#endif /* __PERF_DATA_H */
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index d4559ca08ca6..e3f63df1d57c 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -106,11 +106,11 @@ static void perf_session__destroy_kernel_maps(struct perf_session *self)
106 machines__destroy_kernel_maps(&self->machines); 106 machines__destroy_kernel_maps(&self->machines);
107} 107}
108 108
109struct perf_session *perf_session__new(const char *filename, int mode, 109struct perf_session *perf_session__new(struct perf_data_file *file,
110 bool force, bool repipe, 110 bool repipe, struct perf_tool *tool)
111 struct perf_tool *tool)
112{ 111{
113 struct perf_session *self; 112 struct perf_session *self;
113 const char *filename = file->path;
114 struct stat st; 114 struct stat st;
115 size_t len; 115 size_t len;
116 116
@@ -134,11 +134,11 @@ struct perf_session *perf_session__new(const char *filename, int mode,
134 INIT_LIST_HEAD(&self->ordered_samples.to_free); 134 INIT_LIST_HEAD(&self->ordered_samples.to_free);
135 machines__init(&self->machines); 135 machines__init(&self->machines);
136 136
137 if (mode == O_RDONLY) { 137 if (perf_data_file__is_read(file)) {
138 if (perf_session__open(self, force) < 0) 138 if (perf_session__open(self, file->force) < 0)
139 goto out_delete; 139 goto out_delete;
140 perf_session__set_id_hdr_size(self); 140 perf_session__set_id_hdr_size(self);
141 } else if (mode == O_WRONLY) { 141 } else if (perf_data_file__is_write(file)) {
142 /* 142 /*
143 * In O_RDONLY mode this will be performed when reading the 143 * In O_RDONLY mode this will be performed when reading the
144 * kernel MMAP event, in perf_event__process_mmap(). 144 * kernel MMAP event, in perf_event__process_mmap().
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index 04bf7373a7e5..f2f6251fd62c 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -7,6 +7,7 @@
7#include "machine.h" 7#include "machine.h"
8#include "symbol.h" 8#include "symbol.h"
9#include "thread.h" 9#include "thread.h"
10#include "data.h"
10#include <linux/rbtree.h> 11#include <linux/rbtree.h>
11#include <linux/perf_event.h> 12#include <linux/perf_event.h>
12 13
@@ -49,9 +50,8 @@ struct perf_session {
49 50
50struct perf_tool; 51struct perf_tool;
51 52
52struct perf_session *perf_session__new(const char *filename, int mode, 53struct perf_session *perf_session__new(struct perf_data_file *file,
53 bool force, bool repipe, 54 bool repipe, struct perf_tool *tool);
54 struct perf_tool *tool);
55void perf_session__delete(struct perf_session *session); 55void perf_session__delete(struct perf_session *session);
56 56
57void perf_event_header__bswap(struct perf_event_header *self); 57void perf_event_header__bswap(struct perf_event_header *self);