aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorArnaldo Carvalho de Melo <acme@redhat.com>2011-03-10 09:15:54 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2011-03-10 09:15:54 -0500
commita91e5431d54f5359fccb5ec2512f252eb217707e (patch)
tree8f8ba4940d9f4e910b339baee13a710baa920378 /tools
parent6547250381eb315acff3d52b4872ad775359407c (diff)
perf session: Use evlist/evsel for managing perf.data attributes
So that we can reuse things like the id to attr lookup routine (perf_evlist__id2evsel) that uses a hash table instead of the linear lookup done in the older perf_header_attr routines, etc. Also to make evsels/evlist more pervasive an API, simplyfing using the emerging perf lib. cc: Arun Sharma <arun@sharma-home.net> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Mike Galbraith <efault@gmx.de> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Cc: Tom Zanussi <tzanussi@gmail.com> LKML-Reference: <new-submission> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/builtin-record.c101
-rw-r--r--tools/perf/builtin-report.c4
-rw-r--r--tools/perf/builtin-top.c5
-rw-r--r--tools/perf/util/evlist.c26
-rw-r--r--tools/perf/util/evlist.h4
-rw-r--r--tools/perf/util/evsel.c21
-rw-r--r--tools/perf/util/evsel.h9
-rw-r--r--tools/perf/util/header.c225
-rw-r--r--tools/perf/util/header.h36
-rw-r--r--tools/perf/util/session.c74
-rw-r--r--tools/perf/util/session.h2
11 files changed, 179 insertions, 328 deletions
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index d40a81e8cc56..6febcc168a8c 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -31,7 +31,6 @@
31#include <sys/mman.h> 31#include <sys/mman.h>
32 32
33#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 33#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
34#define SID(e, x, y) xyarray__entry(e->id, x, y)
35 34
36enum write_mode_t { 35enum write_mode_t {
37 WRITE_FORCE, 36 WRITE_FORCE,
@@ -40,7 +39,6 @@ enum write_mode_t {
40 39
41static u64 user_interval = ULLONG_MAX; 40static u64 user_interval = ULLONG_MAX;
42static u64 default_interval = 0; 41static u64 default_interval = 0;
43static u64 sample_type;
44 42
45static unsigned int page_size; 43static unsigned int page_size;
46static unsigned int mmap_pages = 128; 44static unsigned int mmap_pages = 128;
@@ -160,54 +158,6 @@ static void sig_atexit(void)
160 kill(getpid(), signr); 158 kill(getpid(), signr);
161} 159}
162 160
163static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
164{
165 struct perf_header_attr *h_attr;
166
167 if (nr < session->header.attrs) {
168 h_attr = session->header.attr[nr];
169 } else {
170 h_attr = perf_header_attr__new(a);
171 if (h_attr != NULL)
172 if (perf_header__add_attr(&session->header, h_attr) < 0) {
173 perf_header_attr__delete(h_attr);
174 h_attr = NULL;
175 }
176 }
177
178 return h_attr;
179}
180
181static void create_counter(struct perf_evsel *evsel, int cpu)
182{
183 struct perf_event_attr *attr = &evsel->attr;
184 struct perf_header_attr *h_attr;
185 struct perf_sample_id *sid;
186 int thread_index;
187
188 for (thread_index = 0; thread_index < evsel_list->threads->nr; thread_index++) {
189 h_attr = get_header_attr(attr, evsel->idx);
190 if (h_attr == NULL)
191 die("nomem\n");
192
193 if (!file_new) {
194 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
195 fprintf(stderr, "incompatible append\n");
196 exit(-1);
197 }
198 }
199
200 sid = SID(evsel, cpu, thread_index);
201 if (perf_header_attr__add_id(h_attr, sid->id) < 0) {
202 pr_warning("Not enough memory to add id\n");
203 exit(-1);
204 }
205 }
206
207 if (!sample_type)
208 sample_type = attr->sample_type;
209}
210
211static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist) 161static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist)
212{ 162{
213 struct perf_event_attr *attr = &evsel->attr; 163 struct perf_event_attr *attr = &evsel->attr;
@@ -278,10 +228,28 @@ static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist)
278 } 228 }
279} 229}
280 230
231static bool perf_evlist__equal(struct perf_evlist *evlist,
232 struct perf_evlist *other)
233{
234 struct perf_evsel *pos, *pair;
235
236 if (evlist->nr_entries != other->nr_entries)
237 return false;
238
239 pair = list_entry(other->entries.next, struct perf_evsel, node);
240
241 list_for_each_entry(pos, &evlist->entries, node) {
242 if (memcmp(&pos->attr, &pair->attr, sizeof(pos->attr) != 0))
243 return false;
244 pair = list_entry(pair->node.next, struct perf_evsel, node);
245 }
246
247 return true;
248}
249
281static void open_counters(struct perf_evlist *evlist) 250static void open_counters(struct perf_evlist *evlist)
282{ 251{
283 struct perf_evsel *pos; 252 struct perf_evsel *pos;
284 int cpu;
285 253
286 list_for_each_entry(pos, &evlist->entries, node) { 254 list_for_each_entry(pos, &evlist->entries, node) {
287 struct perf_event_attr *attr = &pos->attr; 255 struct perf_event_attr *attr = &pos->attr;
@@ -364,10 +332,16 @@ try_again:
364 if (perf_evlist__mmap(evlist, mmap_pages, false) < 0) 332 if (perf_evlist__mmap(evlist, mmap_pages, false) < 0)
365 die("failed to mmap with %d (%s)\n", errno, strerror(errno)); 333 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
366 334
367 for (cpu = 0; cpu < evsel_list->cpus->nr; ++cpu) { 335 if (file_new)
368 list_for_each_entry(pos, &evlist->entries, node) 336 session->evlist = evlist;
369 create_counter(pos, cpu); 337 else {
370 } 338 if (!perf_evlist__equal(session->evlist, evlist)) {
339 fprintf(stderr, "incompatible append\n");
340 exit(-1);
341 }
342 }
343
344 perf_session__update_sample_type(session);
371} 345}
372 346
373static int process_buildids(void) 347static int process_buildids(void)
@@ -390,7 +364,7 @@ static void atexit_header(void)
390 364
391 if (!no_buildid) 365 if (!no_buildid)
392 process_buildids(); 366 process_buildids();
393 perf_header__write(&session->header, evsel_list, output, true); 367 perf_session__write_header(session, evsel_list, output, true);
394 perf_session__delete(session); 368 perf_session__delete(session);
395 perf_evlist__delete(evsel_list); 369 perf_evlist__delete(evsel_list);
396 symbol__exit(); 370 symbol__exit();
@@ -524,7 +498,7 @@ static int __cmd_record(int argc, const char **argv)
524 perf_header__set_feat(&session->header, HEADER_BUILD_ID); 498 perf_header__set_feat(&session->header, HEADER_BUILD_ID);
525 499
526 if (!file_new) { 500 if (!file_new) {
527 err = perf_header__read(session, output); 501 err = perf_session__read_header(session, output);
528 if (err < 0) 502 if (err < 0)
529 goto out_delete_session; 503 goto out_delete_session;
530 } 504 }
@@ -588,8 +562,6 @@ static int __cmd_record(int argc, const char **argv)
588 562
589 open_counters(evsel_list); 563 open_counters(evsel_list);
590 564
591 perf_session__set_sample_type(session, sample_type);
592
593 /* 565 /*
594 * perf_session__delete(session) will be called at atexit_header() 566 * perf_session__delete(session) will be called at atexit_header()
595 */ 567 */
@@ -600,20 +572,17 @@ static int __cmd_record(int argc, const char **argv)
600 if (err < 0) 572 if (err < 0)
601 return err; 573 return err;
602 } else if (file_new) { 574 } else if (file_new) {
603 err = perf_header__write(&session->header, evsel_list, 575 err = perf_session__write_header(session, evsel_list,
604 output, false); 576 output, false);
605 if (err < 0) 577 if (err < 0)
606 return err; 578 return err;
607 } 579 }
608 580
609 post_processing_offset = lseek(output, 0, SEEK_CUR); 581 post_processing_offset = lseek(output, 0, SEEK_CUR);
610 582
611 perf_session__set_sample_id_all(session, sample_id_all_avail);
612
613 if (pipe_output) { 583 if (pipe_output) {
614 err = perf_event__synthesize_attrs(&session->header, 584 err = perf_session__synthesize_attrs(session,
615 process_synthesized_event, 585 process_synthesized_event);
616 session);
617 if (err < 0) { 586 if (err < 0) {
618 pr_err("Couldn't synthesize attrs.\n"); 587 pr_err("Couldn't synthesize attrs.\n");
619 return err; 588 return err;
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index e9b5d513333a..b1b82009ab9b 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -70,8 +70,8 @@ static int perf_session__add_hist_entry(struct perf_session *session,
70 * FIXME: Propagate this back, but at least we're in a builtin, 70 * FIXME: Propagate this back, but at least we're in a builtin,
71 * where exit() is allowed. ;-) 71 * where exit() is allowed. ;-)
72 */ 72 */
73 ui__warning("Invalid %s file, contains samples with id not in " 73 ui__warning("Invalid %s file, contains samples with id %" PRIu64 " not in "
74 "its header!\n", input_name); 74 "its header!\n", input_name, sample->id);
75 exit_browser(0); 75 exit_browser(0);
76 exit(1); 76 exit(1);
77 } 77 }
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 417f757e3cbe..80c9e062bd5b 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -883,7 +883,6 @@ try_again:
883static int __cmd_top(void) 883static int __cmd_top(void)
884{ 884{
885 pthread_t thread; 885 pthread_t thread;
886 struct perf_evsel *first;
887 int ret __used; 886 int ret __used;
888 /* 887 /*
889 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this 888 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
@@ -900,8 +899,8 @@ static int __cmd_top(void)
900 perf_event__synthesize_threads(perf_event__process, session); 899 perf_event__synthesize_threads(perf_event__process, session);
901 900
902 start_counters(top.evlist); 901 start_counters(top.evlist);
903 first = list_entry(top.evlist->entries.next, struct perf_evsel, node); 902 session->evlist = top.evlist;
904 perf_session__set_sample_type(session, first->attr.sample_type); 903 perf_session__update_sample_type(session);
905 904
906 /* Wait for a minimal set of events before starting the snapshot */ 905 /* Wait for a minimal set of events before starting the snapshot */
907 poll(top.evlist->pollfd, top.evlist->nr_fds, 100); 906 poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 190c64c6e266..d852cefa20de 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -19,7 +19,7 @@
19#include <linux/hash.h> 19#include <linux/hash.h>
20 20
21#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 21#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
22#define SID(e, x, y) xyarray__entry(e->id, x, y) 22#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
23 23
24void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 24void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
25 struct thread_map *threads) 25 struct thread_map *threads)
@@ -106,8 +106,9 @@ void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
106 evlist->nr_fds++; 106 evlist->nr_fds++;
107} 107}
108 108
109void perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel, 109static void perf_evlist__id_hash(struct perf_evlist *evlist,
110 int cpu, int thread, u64 id) 110 struct perf_evsel *evsel,
111 int cpu, int thread, u64 id)
111{ 112{
112 int hash; 113 int hash;
113 struct perf_sample_id *sid = SID(evsel, cpu, thread); 114 struct perf_sample_id *sid = SID(evsel, cpu, thread);
@@ -118,9 +119,16 @@ void perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel,
118 hlist_add_head(&sid->node, &evlist->heads[hash]); 119 hlist_add_head(&sid->node, &evlist->heads[hash]);
119} 120}
120 121
121static int perf_evlist__id_hash_fd(struct perf_evlist *evlist, 122void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
122 struct perf_evsel *evsel, 123 int cpu, int thread, u64 id)
123 int cpu, int thread, int fd) 124{
125 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
126 evsel->id[evsel->ids++] = id;
127}
128
129static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
130 struct perf_evsel *evsel,
131 int cpu, int thread, int fd)
124{ 132{
125 u64 read_data[4] = { 0, }; 133 u64 read_data[4] = { 0, };
126 int id_idx = 1; /* The first entry is the counter value */ 134 int id_idx = 1; /* The first entry is the counter value */
@@ -134,7 +142,7 @@ static int perf_evlist__id_hash_fd(struct perf_evlist *evlist,
134 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 142 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
135 ++id_idx; 143 ++id_idx;
136 144
137 perf_evlist__id_hash(evlist, evsel, cpu, thread, read_data[id_idx]); 145 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
138 return 0; 146 return 0;
139} 147}
140 148
@@ -292,7 +300,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite)
292 300
293 list_for_each_entry(evsel, &evlist->entries, node) { 301 list_for_each_entry(evsel, &evlist->entries, node) {
294 if ((evsel->attr.read_format & PERF_FORMAT_ID) && 302 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
295 evsel->id == NULL && 303 evsel->sample_id == NULL &&
296 perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) 304 perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
297 return -ENOMEM; 305 return -ENOMEM;
298 306
@@ -308,7 +316,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite)
308 goto out_unmap; 316 goto out_unmap;
309 317
310 if ((evsel->attr.read_format & PERF_FORMAT_ID) && 318 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
311 perf_evlist__id_hash_fd(evlist, evsel, cpu, thread, fd) < 0) 319 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
312 goto out_unmap; 320 goto out_unmap;
313 } 321 }
314 } 322 }
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 078d51256085..8b1cb7a4c5f1 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -38,8 +38,8 @@ void perf_evlist__delete(struct perf_evlist *evlist);
38void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry); 38void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry);
39int perf_evlist__add_default(struct perf_evlist *evlist); 39int perf_evlist__add_default(struct perf_evlist *evlist);
40 40
41void perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel, 41void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
42 int cpu, int thread, u64 id); 42 int cpu, int thread, u64 id);
43 43
44int perf_evlist__alloc_pollfd(struct perf_evlist *evlist); 44int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
45void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd); 45void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd);
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 8083d5126fca..662596afd7f1 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -41,8 +41,18 @@ int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
41 41
42int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) 42int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
43{ 43{
44 evsel->id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); 44 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
45 return evsel->id != NULL ? 0 : -ENOMEM; 45 if (evsel->sample_id == NULL)
46 return -ENOMEM;
47
48 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
49 if (evsel->id == NULL) {
50 xyarray__delete(evsel->sample_id);
51 evsel->sample_id = NULL;
52 return -ENOMEM;
53 }
54
55 return 0;
46} 56}
47 57
48int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus) 58int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
@@ -60,7 +70,9 @@ void perf_evsel__free_fd(struct perf_evsel *evsel)
60 70
61void perf_evsel__free_id(struct perf_evsel *evsel) 71void perf_evsel__free_id(struct perf_evsel *evsel)
62{ 72{
63 xyarray__delete(evsel->id); 73 xyarray__delete(evsel->sample_id);
74 evsel->sample_id = NULL;
75 free(evsel->id);
64 evsel->id = NULL; 76 evsel->id = NULL;
65} 77}
66 78
@@ -79,7 +91,8 @@ void perf_evsel__exit(struct perf_evsel *evsel)
79{ 91{
80 assert(list_empty(&evsel->node)); 92 assert(list_empty(&evsel->node));
81 xyarray__delete(evsel->fd); 93 xyarray__delete(evsel->fd);
82 xyarray__delete(evsel->id); 94 xyarray__delete(evsel->sample_id);
95 free(evsel->id);
83} 96}
84 97
85void perf_evsel__delete(struct perf_evsel *evsel) 98void perf_evsel__delete(struct perf_evsel *evsel)
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 281b60e5fc7b..6710ab538342 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -49,12 +49,17 @@ struct perf_evsel {
49 struct perf_event_attr attr; 49 struct perf_event_attr attr;
50 char *filter; 50 char *filter;
51 struct xyarray *fd; 51 struct xyarray *fd;
52 struct xyarray *id; 52 struct xyarray *sample_id;
53 u64 *id;
53 struct perf_counts *counts; 54 struct perf_counts *counts;
54 int idx; 55 int idx;
56 int ids;
55 struct hists hists; 57 struct hists hists;
56 char *name; 58 char *name;
57 void *priv; 59 union {
60 void *priv;
61 off_t id_offset;
62 };
58 struct cgroup_sel *cgrp; 63 struct cgroup_sel *cgrp;
59}; 64};
60 65
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 108b0db7bbef..40b10e4d3927 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -9,6 +9,7 @@
9#include <linux/kernel.h> 9#include <linux/kernel.h>
10 10
11#include "evlist.h" 11#include "evlist.h"
12#include "evsel.h"
12#include "util.h" 13#include "util.h"
13#include "header.h" 14#include "header.h"
14#include "../perf.h" 15#include "../perf.h"
@@ -19,89 +20,6 @@
19 20
20static bool no_buildid_cache = false; 21static bool no_buildid_cache = false;
21 22
22/*
23 * Create new perf.data header attribute:
24 */
25struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
26{
27 struct perf_header_attr *self = malloc(sizeof(*self));
28
29 if (self != NULL) {
30 self->attr = *attr;
31 self->ids = 0;
32 self->size = 1;
33 self->id = malloc(sizeof(u64));
34 if (self->id == NULL) {
35 free(self);
36 self = NULL;
37 }
38 }
39
40 return self;
41}
42
43void perf_header_attr__delete(struct perf_header_attr *self)
44{
45 free(self->id);
46 free(self);
47}
48
49int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
50{
51 int pos = self->ids;
52
53 self->ids++;
54 if (self->ids > self->size) {
55 int nsize = self->size * 2;
56 u64 *nid = realloc(self->id, nsize * sizeof(u64));
57
58 if (nid == NULL)
59 return -1;
60
61 self->size = nsize;
62 self->id = nid;
63 }
64 self->id[pos] = id;
65 return 0;
66}
67
68int perf_header__init(struct perf_header *self)
69{
70 self->size = 1;
71 self->attr = malloc(sizeof(void *));
72 return self->attr == NULL ? -ENOMEM : 0;
73}
74
75void perf_header__exit(struct perf_header *self)
76{
77 int i;
78 for (i = 0; i < self->attrs; ++i)
79 perf_header_attr__delete(self->attr[i]);
80 free(self->attr);
81}
82
83int perf_header__add_attr(struct perf_header *self,
84 struct perf_header_attr *attr)
85{
86 if (self->frozen)
87 return -1;
88
89 if (self->attrs == self->size) {
90 int nsize = self->size * 2;
91 struct perf_header_attr **nattr;
92
93 nattr = realloc(self->attr, nsize * sizeof(void *));
94 if (nattr == NULL)
95 return -1;
96
97 self->size = nsize;
98 self->attr = nattr;
99 }
100
101 self->attr[self->attrs++] = attr;
102 return 0;
103}
104
105static int event_count; 23static int event_count;
106static struct perf_trace_event_type *events; 24static struct perf_trace_event_type *events;
107 25
@@ -515,33 +433,41 @@ int perf_header__write_pipe(int fd)
515 return 0; 433 return 0;
516} 434}
517 435
518int perf_header__write(struct perf_header *self, struct perf_evlist *evlist, 436int perf_session__write_header(struct perf_session *session,
519 int fd, bool at_exit) 437 struct perf_evlist *evlist,
438 int fd, bool at_exit)
520{ 439{
521 struct perf_file_header f_header; 440 struct perf_file_header f_header;
522 struct perf_file_attr f_attr; 441 struct perf_file_attr f_attr;
523 struct perf_header_attr *attr; 442 struct perf_header *self = &session->header;
524 int i, err; 443 struct perf_evsel *attr, *pair = NULL;
444 int err;
525 445
526 lseek(fd, sizeof(f_header), SEEK_SET); 446 lseek(fd, sizeof(f_header), SEEK_SET);
527 447
528 for (i = 0; i < self->attrs; i++) { 448 if (session->evlist != evlist)
529 attr = self->attr[i]; 449 pair = list_entry(session->evlist->entries.next, struct perf_evsel, node);
530 450
451 list_for_each_entry(attr, &evlist->entries, node) {
531 attr->id_offset = lseek(fd, 0, SEEK_CUR); 452 attr->id_offset = lseek(fd, 0, SEEK_CUR);
532 err = do_write(fd, attr->id, attr->ids * sizeof(u64)); 453 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
533 if (err < 0) { 454 if (err < 0) {
455out_err_write:
534 pr_debug("failed to write perf header\n"); 456 pr_debug("failed to write perf header\n");
535 return err; 457 return err;
536 } 458 }
459 if (session->evlist != evlist) {
460 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
461 if (err < 0)
462 goto out_err_write;
463 attr->ids += pair->ids;
464 pair = list_entry(pair->node.next, struct perf_evsel, node);
465 }
537 } 466 }
538 467
539
540 self->attr_offset = lseek(fd, 0, SEEK_CUR); 468 self->attr_offset = lseek(fd, 0, SEEK_CUR);
541 469
542 for (i = 0; i < self->attrs; i++) { 470 list_for_each_entry(attr, &evlist->entries, node) {
543 attr = self->attr[i];
544
545 f_attr = (struct perf_file_attr){ 471 f_attr = (struct perf_file_attr){
546 .attr = attr->attr, 472 .attr = attr->attr,
547 .ids = { 473 .ids = {
@@ -580,7 +506,7 @@ int perf_header__write(struct perf_header *self, struct perf_evlist *evlist,
580 .attr_size = sizeof(f_attr), 506 .attr_size = sizeof(f_attr),
581 .attrs = { 507 .attrs = {
582 .offset = self->attr_offset, 508 .offset = self->attr_offset,
583 .size = self->attrs * sizeof(f_attr), 509 .size = evlist->nr_entries * sizeof(f_attr),
584 }, 510 },
585 .data = { 511 .data = {
586 .offset = self->data_offset, 512 .offset = self->data_offset,
@@ -861,7 +787,7 @@ static int perf_header__read_pipe(struct perf_session *session, int fd)
861 return 0; 787 return 0;
862} 788}
863 789
864int perf_header__read(struct perf_session *session, int fd) 790int perf_session__read_header(struct perf_session *session, int fd)
865{ 791{
866 struct perf_header *self = &session->header; 792 struct perf_header *self = &session->header;
867 struct perf_file_header f_header; 793 struct perf_file_header f_header;
@@ -869,6 +795,10 @@ int perf_header__read(struct perf_session *session, int fd)
869 u64 f_id; 795 u64 f_id;
870 int nr_attrs, nr_ids, i, j; 796 int nr_attrs, nr_ids, i, j;
871 797
798 session->evlist = perf_evlist__new(NULL, NULL);
799 if (session->evlist == NULL)
800 return -ENOMEM;
801
872 if (session->fd_pipe) 802 if (session->fd_pipe)
873 return perf_header__read_pipe(session, fd); 803 return perf_header__read_pipe(session, fd);
874 804
@@ -881,33 +811,39 @@ int perf_header__read(struct perf_session *session, int fd)
881 lseek(fd, f_header.attrs.offset, SEEK_SET); 811 lseek(fd, f_header.attrs.offset, SEEK_SET);
882 812
883 for (i = 0; i < nr_attrs; i++) { 813 for (i = 0; i < nr_attrs; i++) {
884 struct perf_header_attr *attr; 814 struct perf_evsel *evsel;
885 off_t tmp; 815 off_t tmp;
886 816
887 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr))) 817 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr)))
888 goto out_errno; 818 goto out_errno;
889 819
890 tmp = lseek(fd, 0, SEEK_CUR); 820 tmp = lseek(fd, 0, SEEK_CUR);
821 evsel = perf_evsel__new(&f_attr.attr, i);
891 822
892 attr = perf_header_attr__new(&f_attr.attr); 823 if (evsel == NULL)
893 if (attr == NULL) 824 goto out_delete_evlist;
894 return -ENOMEM; 825 /*
826 * Do it before so that if perf_evsel__alloc_id fails, this
827 * entry gets purged too at perf_evlist__delete().
828 */
829 perf_evlist__add(session->evlist, evsel);
895 830
896 nr_ids = f_attr.ids.size / sizeof(u64); 831 nr_ids = f_attr.ids.size / sizeof(u64);
832 /*
833 * We don't have the cpu and thread maps on the header, so
834 * for allocating the perf_sample_id table we fake 1 cpu and
835 * hattr->ids threads.
836 */
837 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
838 goto out_delete_evlist;
839
897 lseek(fd, f_attr.ids.offset, SEEK_SET); 840 lseek(fd, f_attr.ids.offset, SEEK_SET);
898 841
899 for (j = 0; j < nr_ids; j++) { 842 for (j = 0; j < nr_ids; j++) {
900 if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id))) 843 if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id)))
901 goto out_errno; 844 goto out_errno;
902 845
903 if (perf_header_attr__add_id(attr, f_id) < 0) { 846 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
904 perf_header_attr__delete(attr);
905 return -ENOMEM;
906 }
907 }
908 if (perf_header__add_attr(self, attr) < 0) {
909 perf_header_attr__delete(attr);
910 return -ENOMEM;
911 } 847 }
912 848
913 lseek(fd, tmp, SEEK_SET); 849 lseek(fd, tmp, SEEK_SET);
@@ -932,37 +868,38 @@ int perf_header__read(struct perf_session *session, int fd)
932 return 0; 868 return 0;
933out_errno: 869out_errno:
934 return -errno; 870 return -errno;
871
872out_delete_evlist:
873 perf_evlist__delete(session->evlist);
874 session->evlist = NULL;
875 return -ENOMEM;
935} 876}
936 877
937u64 perf_header__sample_type(struct perf_header *header) 878u64 perf_evlist__sample_type(struct perf_evlist *evlist)
938{ 879{
880 struct perf_evsel *pos;
939 u64 type = 0; 881 u64 type = 0;
940 int i;
941
942 for (i = 0; i < header->attrs; i++) {
943 struct perf_header_attr *attr = header->attr[i];
944 882
883 list_for_each_entry(pos, &evlist->entries, node) {
945 if (!type) 884 if (!type)
946 type = attr->attr.sample_type; 885 type = pos->attr.sample_type;
947 else if (type != attr->attr.sample_type) 886 else if (type != pos->attr.sample_type)
948 die("non matching sample_type"); 887 die("non matching sample_type");
949 } 888 }
950 889
951 return type; 890 return type;
952} 891}
953 892
954bool perf_header__sample_id_all(const struct perf_header *header) 893bool perf_evlist__sample_id_all(const struct perf_evlist *evlist)
955{ 894{
956 bool value = false, first = true; 895 bool value = false, first = true;
957 int i; 896 struct perf_evsel *pos;
958
959 for (i = 0; i < header->attrs; i++) {
960 struct perf_header_attr *attr = header->attr[i];
961 897
898 list_for_each_entry(pos, &evlist->entries, node) {
962 if (first) { 899 if (first) {
963 value = attr->attr.sample_id_all; 900 value = pos->attr.sample_id_all;
964 first = false; 901 first = false;
965 } else if (value != attr->attr.sample_id_all) 902 } else if (value != pos->attr.sample_id_all)
966 die("non matching sample_id_all"); 903 die("non matching sample_id_all");
967 } 904 }
968 905
@@ -1000,16 +937,13 @@ int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
1000 return err; 937 return err;
1001} 938}
1002 939
1003int perf_event__synthesize_attrs(struct perf_header *self, 940int perf_session__synthesize_attrs(struct perf_session *session,
1004 perf_event__handler_t process, 941 perf_event__handler_t process)
1005 struct perf_session *session)
1006{ 942{
1007 struct perf_header_attr *attr; 943 struct perf_evsel *attr;
1008 int i, err = 0; 944 int err = 0;
1009
1010 for (i = 0; i < self->attrs; i++) {
1011 attr = self->attr[i];
1012 945
946 list_for_each_entry(attr, &session->evlist->entries, node) {
1013 err = perf_event__synthesize_attr(&attr->attr, attr->ids, 947 err = perf_event__synthesize_attr(&attr->attr, attr->ids,
1014 attr->id, process, session); 948 attr->id, process, session);
1015 if (err) { 949 if (err) {
@@ -1024,27 +958,36 @@ int perf_event__synthesize_attrs(struct perf_header *self,
1024int perf_event__process_attr(union perf_event *event, 958int perf_event__process_attr(union perf_event *event,
1025 struct perf_session *session) 959 struct perf_session *session)
1026{ 960{
1027 struct perf_header_attr *attr;
1028 unsigned int i, ids, n_ids; 961 unsigned int i, ids, n_ids;
962 struct perf_evsel *evsel;
1029 963
1030 attr = perf_header_attr__new(&event->attr.attr); 964 if (session->evlist == NULL) {
1031 if (attr == NULL) 965 session->evlist = perf_evlist__new(NULL, NULL);
966 if (session->evlist == NULL)
967 return -ENOMEM;
968 }
969
970 evsel = perf_evsel__new(&event->attr.attr,
971 session->evlist->nr_entries);
972 if (evsel == NULL)
1032 return -ENOMEM; 973 return -ENOMEM;
1033 974
975 perf_evlist__add(session->evlist, evsel);
976
1034 ids = event->header.size; 977 ids = event->header.size;
1035 ids -= (void *)&event->attr.id - (void *)event; 978 ids -= (void *)&event->attr.id - (void *)event;
1036 n_ids = ids / sizeof(u64); 979 n_ids = ids / sizeof(u64);
980 /*
981 * We don't have the cpu and thread maps on the header, so
982 * for allocating the perf_sample_id table we fake 1 cpu and
983 * hattr->ids threads.
984 */
985 if (perf_evsel__alloc_id(evsel, 1, n_ids))
986 return -ENOMEM;
1037 987
1038 for (i = 0; i < n_ids; i++) { 988 for (i = 0; i < n_ids; i++) {
1039 if (perf_header_attr__add_id(attr, event->attr.id[i]) < 0) { 989 perf_evlist__id_add(session->evlist, evsel, 0, i,
1040 perf_header_attr__delete(attr); 990 event->attr.id[i]);
1041 return -ENOMEM;
1042 }
1043 }
1044
1045 if (perf_header__add_attr(&session->header, attr) < 0) {
1046 perf_header_attr__delete(attr);
1047 return -ENOMEM;
1048 } 991 }
1049 992
1050 perf_session__update_sample_type(session); 993 perf_session__update_sample_type(session);
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index 2fab13348aab..4cc267559bb6 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -9,13 +9,6 @@
9 9
10#include <linux/bitmap.h> 10#include <linux/bitmap.h>
11 11
12struct perf_header_attr {
13 struct perf_event_attr attr;
14 int ids, size;
15 u64 *id;
16 off_t id_offset;
17};
18
19enum { 12enum {
20 HEADER_TRACE_INFO = 1, 13 HEADER_TRACE_INFO = 1,
21 HEADER_BUILD_ID, 14 HEADER_BUILD_ID,
@@ -51,9 +44,7 @@ int perf_file_header__read(struct perf_file_header *self,
51 44
52struct perf_header { 45struct perf_header {
53 int frozen; 46 int frozen;
54 int attrs, size;
55 bool needs_swap; 47 bool needs_swap;
56 struct perf_header_attr **attr;
57 s64 attr_offset; 48 s64 attr_offset;
58 u64 data_offset; 49 u64 data_offset;
59 u64 data_size; 50 u64 data_size;
@@ -62,29 +53,19 @@ struct perf_header {
62 DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS); 53 DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
63}; 54};
64 55
65int perf_header__init(struct perf_header *self);
66void perf_header__exit(struct perf_header *self);
67
68struct perf_evlist; 56struct perf_evlist;
69 57
70int perf_header__read(struct perf_session *session, int fd); 58int perf_session__read_header(struct perf_session *session, int fd);
71int perf_header__write(struct perf_header *self, struct perf_evlist *evlist, 59int perf_session__write_header(struct perf_session *session,
72 int fd, bool at_exit); 60 struct perf_evlist *evlist,
61 int fd, bool at_exit);
73int perf_header__write_pipe(int fd); 62int perf_header__write_pipe(int fd);
74 63
75int perf_header__add_attr(struct perf_header *self,
76 struct perf_header_attr *attr);
77
78int perf_header__push_event(u64 id, const char *name); 64int perf_header__push_event(u64 id, const char *name);
79char *perf_header__find_event(u64 id); 65char *perf_header__find_event(u64 id);
80 66
81struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr); 67u64 perf_evlist__sample_type(struct perf_evlist *evlist);
82void perf_header_attr__delete(struct perf_header_attr *self); 68bool perf_evlist__sample_id_all(const struct perf_evlist *evlist);
83
84int perf_header_attr__add_id(struct perf_header_attr *self, u64 id);
85
86u64 perf_header__sample_type(struct perf_header *header);
87bool perf_header__sample_id_all(const struct perf_header *header);
88void perf_header__set_feat(struct perf_header *self, int feat); 69void perf_header__set_feat(struct perf_header *self, int feat);
89void perf_header__clear_feat(struct perf_header *self, int feat); 70void perf_header__clear_feat(struct perf_header *self, int feat);
90bool perf_header__has_feat(const struct perf_header *self, int feat); 71bool perf_header__has_feat(const struct perf_header *self, int feat);
@@ -101,9 +82,8 @@ int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir);
101int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id, 82int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
102 perf_event__handler_t process, 83 perf_event__handler_t process,
103 struct perf_session *session); 84 struct perf_session *session);
104int perf_event__synthesize_attrs(struct perf_header *self, 85int perf_session__synthesize_attrs(struct perf_session *session,
105 perf_event__handler_t process, 86 perf_event__handler_t process);
106 struct perf_session *session);
107int perf_event__process_attr(union perf_event *event, struct perf_session *session); 87int perf_event__process_attr(union perf_event *event, struct perf_session *session);
108 88
109int perf_event__synthesize_event_type(u64 event_id, char *name, 89int perf_event__synthesize_event_type(u64 event_id, char *name,
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 0d414199889d..f26639fa0fb3 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -13,46 +13,6 @@
13#include "sort.h" 13#include "sort.h"
14#include "util.h" 14#include "util.h"
15 15
16static int perf_session__read_evlist(struct perf_session *session)
17{
18 int i, j;
19
20 session->evlist = perf_evlist__new(NULL, NULL);
21 if (session->evlist == NULL)
22 return -ENOMEM;
23
24 for (i = 0; i < session->header.attrs; ++i) {
25 struct perf_header_attr *hattr = session->header.attr[i];
26 struct perf_evsel *evsel = perf_evsel__new(&hattr->attr, i);
27
28 if (evsel == NULL)
29 goto out_delete_evlist;
30 /*
31 * Do it before so that if perf_evsel__alloc_id fails, this
32 * entry gets purged too at perf_evlist__delete().
33 */
34 perf_evlist__add(session->evlist, evsel);
35 /*
36 * We don't have the cpu and thread maps on the header, so
37 * for allocating the perf_sample_id table we fake 1 cpu and
38 * hattr->ids threads.
39 */
40 if (perf_evsel__alloc_id(evsel, 1, hattr->ids))
41 goto out_delete_evlist;
42
43 for (j = 0; j < hattr->ids; ++j)
44 perf_evlist__id_hash(session->evlist, evsel, 0, j,
45 hattr->id[j]);
46 }
47
48 return 0;
49
50out_delete_evlist:
51 perf_evlist__delete(session->evlist);
52 session->evlist = NULL;
53 return -ENOMEM;
54}
55
56static int perf_session__open(struct perf_session *self, bool force) 16static int perf_session__open(struct perf_session *self, bool force)
57{ 17{
58 struct stat input_stat; 18 struct stat input_stat;
@@ -61,7 +21,7 @@ static int perf_session__open(struct perf_session *self, bool force)
61 self->fd_pipe = true; 21 self->fd_pipe = true;
62 self->fd = STDIN_FILENO; 22 self->fd = STDIN_FILENO;
63 23
64 if (perf_header__read(self, self->fd) < 0) 24 if (perf_session__read_header(self, self->fd) < 0)
65 pr_err("incompatible file format"); 25 pr_err("incompatible file format");
66 26
67 return 0; 27 return 0;
@@ -93,16 +53,11 @@ static int perf_session__open(struct perf_session *self, bool force)
93 goto out_close; 53 goto out_close;
94 } 54 }
95 55
96 if (perf_header__read(self, self->fd) < 0) { 56 if (perf_session__read_header(self, self->fd) < 0) {
97 pr_err("incompatible file format"); 57 pr_err("incompatible file format");
98 goto out_close; 58 goto out_close;
99 } 59 }
100 60
101 if (perf_session__read_evlist(self) < 0) {
102 pr_err("Not enough memory to read the event selector list\n");
103 goto out_close;
104 }
105
106 self->size = input_stat.st_size; 61 self->size = input_stat.st_size;
107 return 0; 62 return 0;
108 63
@@ -139,21 +94,10 @@ out:
139 session->id_hdr_size = size; 94 session->id_hdr_size = size;
140} 95}
141 96
142void perf_session__set_sample_id_all(struct perf_session *session, bool value)
143{
144 session->sample_id_all = value;
145 perf_session__id_header_size(session);
146}
147
148void perf_session__set_sample_type(struct perf_session *session, u64 type)
149{
150 session->sample_type = type;
151}
152
153void perf_session__update_sample_type(struct perf_session *self) 97void perf_session__update_sample_type(struct perf_session *self)
154{ 98{
155 self->sample_type = perf_header__sample_type(&self->header); 99 self->sample_type = perf_evlist__sample_type(self->evlist);
156 self->sample_id_all = perf_header__sample_id_all(&self->header); 100 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
157 perf_session__id_header_size(self); 101 perf_session__id_header_size(self);
158} 102}
159 103
@@ -182,9 +126,6 @@ struct perf_session *perf_session__new(const char *filename, int mode,
182 if (self == NULL) 126 if (self == NULL)
183 goto out; 127 goto out;
184 128
185 if (perf_header__init(&self->header) < 0)
186 goto out_free;
187
188 memcpy(self->filename, filename, len); 129 memcpy(self->filename, filename, len);
189 self->threads = RB_ROOT; 130 self->threads = RB_ROOT;
190 INIT_LIST_HEAD(&self->dead_threads); 131 INIT_LIST_HEAD(&self->dead_threads);
@@ -208,6 +149,7 @@ struct perf_session *perf_session__new(const char *filename, int mode,
208 if (mode == O_RDONLY) { 149 if (mode == O_RDONLY) {
209 if (perf_session__open(self, force) < 0) 150 if (perf_session__open(self, force) < 0)
210 goto out_delete; 151 goto out_delete;
152 perf_session__update_sample_type(self);
211 } else if (mode == O_WRONLY) { 153 } else if (mode == O_WRONLY) {
212 /* 154 /*
213 * In O_RDONLY mode this will be performed when reading the 155 * In O_RDONLY mode this will be performed when reading the
@@ -217,8 +159,6 @@ struct perf_session *perf_session__new(const char *filename, int mode,
217 goto out_delete; 159 goto out_delete;
218 } 160 }
219 161
220 perf_session__update_sample_type(self);
221
222 if (ops && ops->ordering_requires_timestamps && 162 if (ops && ops->ordering_requires_timestamps &&
223 ops->ordered_samples && !self->sample_id_all) { 163 ops->ordered_samples && !self->sample_id_all) {
224 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); 164 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
@@ -227,9 +167,6 @@ struct perf_session *perf_session__new(const char *filename, int mode,
227 167
228out: 168out:
229 return self; 169 return self;
230out_free:
231 free(self);
232 return NULL;
233out_delete: 170out_delete:
234 perf_session__delete(self); 171 perf_session__delete(self);
235 return NULL; 172 return NULL;
@@ -260,7 +197,6 @@ static void perf_session__delete_threads(struct perf_session *self)
260 197
261void perf_session__delete(struct perf_session *self) 198void perf_session__delete(struct perf_session *self)
262{ 199{
263 perf_header__exit(&self->header);
264 perf_session__destroy_kernel_maps(self); 200 perf_session__destroy_kernel_maps(self);
265 perf_session__delete_dead_threads(self); 201 perf_session__delete_dead_threads(self);
266 perf_session__delete_threads(self); 202 perf_session__delete_threads(self);
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index 05dd7bcb9453..b5b148b0aaca 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -112,8 +112,6 @@ void mem_bswap_64(void *src, int byte_size);
112int perf_session__create_kernel_maps(struct perf_session *self); 112int perf_session__create_kernel_maps(struct perf_session *self);
113 113
114void perf_session__update_sample_type(struct perf_session *self); 114void perf_session__update_sample_type(struct perf_session *self);
115void perf_session__set_sample_id_all(struct perf_session *session, bool value);
116void perf_session__set_sample_type(struct perf_session *session, u64 type);
117void perf_session__remove_thread(struct perf_session *self, struct thread *th); 115void perf_session__remove_thread(struct perf_session *self, struct thread *th);
118 116
119static inline 117static inline