aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/session.c
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@redhat.com>2013-10-15 10:27:33 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2013-10-21 16:33:24 -0400
commit6a4d98d787b38a130a67e78b64182b419899623a (patch)
treebd72dfaa6b4e93b0d304320dd373d0150724e29d /tools/perf/util/session.c
parentf5fc14124c5cefdd052a2b2a6a3f0ed531540113 (diff)
perf tools: Add perf_data_file__open interface to data object
Adding perf_data_file__open interface to data object to open the perf.data file for both read and write. 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-3-git-send-email-jolsa@redhat.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/session.c')
-rw-r--r--tools/perf/util/session.c95
1 files changed, 30 insertions, 65 deletions
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index e3f63df1d57c..d857c18d2eeb 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -16,73 +16,35 @@
16#include "perf_regs.h" 16#include "perf_regs.h"
17#include "vdso.h" 17#include "vdso.h"
18 18
19static int perf_session__open(struct perf_session *self, bool force) 19static int perf_session__open(struct perf_session *self)
20{ 20{
21 struct stat input_stat; 21 if (self->fd_pipe) {
22
23 if (!strcmp(self->filename, "-")) {
24 self->fd_pipe = true;
25 self->fd = STDIN_FILENO;
26
27 if (perf_session__read_header(self) < 0) 22 if (perf_session__read_header(self) < 0)
28 pr_err("incompatible file format (rerun with -v to learn more)"); 23 pr_err("incompatible file format (rerun with -v to learn more)");
29
30 return 0; 24 return 0;
31 } 25 }
32 26
33 self->fd = open(self->filename, O_RDONLY);
34 if (self->fd < 0) {
35 int err = errno;
36
37 pr_err("failed to open %s: %s", self->filename, strerror(err));
38 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
39 pr_err(" (try 'perf record' first)");
40 pr_err("\n");
41 return -errno;
42 }
43
44 if (fstat(self->fd, &input_stat) < 0)
45 goto out_close;
46
47 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
48 pr_err("file %s not owned by current user or root\n",
49 self->filename);
50 goto out_close;
51 }
52
53 if (!input_stat.st_size) {
54 pr_info("zero-sized file (%s), nothing to do!\n",
55 self->filename);
56 goto out_close;
57 }
58
59 if (perf_session__read_header(self) < 0) { 27 if (perf_session__read_header(self) < 0) {
60 pr_err("incompatible file format (rerun with -v to learn more)"); 28 pr_err("incompatible file format (rerun with -v to learn more)");
61 goto out_close; 29 return -1;
62 } 30 }
63 31
64 if (!perf_evlist__valid_sample_type(self->evlist)) { 32 if (!perf_evlist__valid_sample_type(self->evlist)) {
65 pr_err("non matching sample_type"); 33 pr_err("non matching sample_type");
66 goto out_close; 34 return -1;
67 } 35 }
68 36
69 if (!perf_evlist__valid_sample_id_all(self->evlist)) { 37 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
70 pr_err("non matching sample_id_all"); 38 pr_err("non matching sample_id_all");
71 goto out_close; 39 return -1;
72 } 40 }
73 41
74 if (!perf_evlist__valid_read_format(self->evlist)) { 42 if (!perf_evlist__valid_read_format(self->evlist)) {
75 pr_err("non matching read_format"); 43 pr_err("non matching read_format");
76 goto out_close; 44 return -1;
77 } 45 }
78 46
79 self->size = input_stat.st_size;
80 return 0; 47 return 0;
81
82out_close:
83 close(self->fd);
84 self->fd = -1;
85 return -1;
86} 48}
87 49
88void perf_session__set_id_hdr_size(struct perf_session *session) 50void perf_session__set_id_hdr_size(struct perf_session *session)
@@ -110,35 +72,35 @@ struct perf_session *perf_session__new(struct perf_data_file *file,
110 bool repipe, struct perf_tool *tool) 72 bool repipe, struct perf_tool *tool)
111{ 73{
112 struct perf_session *self; 74 struct perf_session *self;
113 const char *filename = file->path;
114 struct stat st;
115 size_t len;
116
117 if (!filename || !strlen(filename)) {
118 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
119 filename = "-";
120 else
121 filename = "perf.data";
122 }
123 75
124 len = strlen(filename); 76 self = zalloc(sizeof(*self));
125 self = zalloc(sizeof(*self) + len); 77 if (!self)
126
127 if (self == NULL)
128 goto out; 78 goto out;
129 79
130 memcpy(self->filename, filename, len);
131 self->repipe = repipe; 80 self->repipe = repipe;
132 INIT_LIST_HEAD(&self->ordered_samples.samples); 81 INIT_LIST_HEAD(&self->ordered_samples.samples);
133 INIT_LIST_HEAD(&self->ordered_samples.sample_cache); 82 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
134 INIT_LIST_HEAD(&self->ordered_samples.to_free); 83 INIT_LIST_HEAD(&self->ordered_samples.to_free);
135 machines__init(&self->machines); 84 machines__init(&self->machines);
136 85
137 if (perf_data_file__is_read(file)) { 86 if (file) {
138 if (perf_session__open(self, file->force) < 0) 87 if (perf_data_file__open(file))
139 goto out_delete; 88 goto out_delete;
140 perf_session__set_id_hdr_size(self); 89
141 } else if (perf_data_file__is_write(file)) { 90 self->fd = file->fd;
91 self->fd_pipe = file->is_pipe;
92 self->filename = file->path;
93 self->size = file->size;
94
95 if (perf_data_file__is_read(file)) {
96 if (perf_session__open(self) < 0)
97 goto out_close;
98
99 perf_session__set_id_hdr_size(self);
100 }
101 }
102
103 if (!file || perf_data_file__is_write(file)) {
142 /* 104 /*
143 * In O_RDONLY mode this will be performed when reading the 105 * In O_RDONLY mode this will be performed when reading the
144 * kernel MMAP event, in perf_event__process_mmap(). 106 * kernel MMAP event, in perf_event__process_mmap().
@@ -153,10 +115,13 @@ struct perf_session *perf_session__new(struct perf_data_file *file,
153 tool->ordered_samples = false; 115 tool->ordered_samples = false;
154 } 116 }
155 117
156out:
157 return self; 118 return self;
158out_delete: 119
120 out_close:
121 perf_data_file__close(file);
122 out_delete:
159 perf_session__delete(self); 123 perf_session__delete(self);
124 out:
160 return NULL; 125 return NULL;
161} 126}
162 127