Commit 137ee20d authored by Ingo Molnar's avatar Ingo Molnar

Merge branch 'perf/core' of...

Merge branch 'perf/core' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux-2.6 into perf/core
parents 4a0b1665 1c0b04d1
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
#define SID(e, x, y) xyarray__entry(e->id, x, y)
enum write_mode_t { enum write_mode_t {
WRITE_FORCE, WRITE_FORCE,
...@@ -40,7 +39,6 @@ enum write_mode_t { ...@@ -40,7 +39,6 @@ enum write_mode_t {
static u64 user_interval = ULLONG_MAX; static u64 user_interval = ULLONG_MAX;
static u64 default_interval = 0; static u64 default_interval = 0;
static u64 sample_type;
static unsigned int page_size; static unsigned int page_size;
static unsigned int mmap_pages = 128; static unsigned int mmap_pages = 128;
...@@ -160,54 +158,6 @@ static void sig_atexit(void) ...@@ -160,54 +158,6 @@ static void sig_atexit(void)
kill(getpid(), signr); kill(getpid(), signr);
} }
static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
{
struct perf_header_attr *h_attr;
if (nr < session->header.attrs) {
h_attr = session->header.attr[nr];
} else {
h_attr = perf_header_attr__new(a);
if (h_attr != NULL)
if (perf_header__add_attr(&session->header, h_attr) < 0) {
perf_header_attr__delete(h_attr);
h_attr = NULL;
}
}
return h_attr;
}
static void create_counter(struct perf_evsel *evsel, int cpu)
{
struct perf_event_attr *attr = &evsel->attr;
struct perf_header_attr *h_attr;
struct perf_sample_id *sid;
int thread_index;
for (thread_index = 0; thread_index < evsel_list->threads->nr; thread_index++) {
h_attr = get_header_attr(attr, evsel->idx);
if (h_attr == NULL)
die("nomem\n");
if (!file_new) {
if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
fprintf(stderr, "incompatible append\n");
exit(-1);
}
}
sid = SID(evsel, cpu, thread_index);
if (perf_header_attr__add_id(h_attr, sid->id) < 0) {
pr_warning("Not enough memory to add id\n");
exit(-1);
}
}
if (!sample_type)
sample_type = attr->sample_type;
}
static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist) static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist)
{ {
struct perf_event_attr *attr = &evsel->attr; struct perf_event_attr *attr = &evsel->attr;
...@@ -278,10 +228,28 @@ static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist) ...@@ -278,10 +228,28 @@ static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist)
} }
} }
static bool perf_evlist__equal(struct perf_evlist *evlist,
struct perf_evlist *other)
{
struct perf_evsel *pos, *pair;
if (evlist->nr_entries != other->nr_entries)
return false;
pair = list_entry(other->entries.next, struct perf_evsel, node);
list_for_each_entry(pos, &evlist->entries, node) {
if (memcmp(&pos->attr, &pair->attr, sizeof(pos->attr) != 0))
return false;
pair = list_entry(pair->node.next, struct perf_evsel, node);
}
return true;
}
static void open_counters(struct perf_evlist *evlist) static void open_counters(struct perf_evlist *evlist)
{ {
struct perf_evsel *pos; struct perf_evsel *pos;
int cpu;
list_for_each_entry(pos, &evlist->entries, node) { list_for_each_entry(pos, &evlist->entries, node) {
struct perf_event_attr *attr = &pos->attr; struct perf_event_attr *attr = &pos->attr;
...@@ -364,10 +332,16 @@ static void open_counters(struct perf_evlist *evlist) ...@@ -364,10 +332,16 @@ static void open_counters(struct perf_evlist *evlist)
if (perf_evlist__mmap(evlist, mmap_pages, false) < 0) if (perf_evlist__mmap(evlist, mmap_pages, false) < 0)
die("failed to mmap with %d (%s)\n", errno, strerror(errno)); die("failed to mmap with %d (%s)\n", errno, strerror(errno));
for (cpu = 0; cpu < evsel_list->cpus->nr; ++cpu) { if (file_new)
list_for_each_entry(pos, &evlist->entries, node) session->evlist = evlist;
create_counter(pos, cpu); else {
} if (!perf_evlist__equal(session->evlist, evlist)) {
fprintf(stderr, "incompatible append\n");
exit(-1);
}
}
perf_session__update_sample_type(session);
} }
static int process_buildids(void) static int process_buildids(void)
...@@ -390,7 +364,7 @@ static void atexit_header(void) ...@@ -390,7 +364,7 @@ static void atexit_header(void)
if (!no_buildid) if (!no_buildid)
process_buildids(); process_buildids();
perf_header__write(&session->header, evsel_list, output, true); perf_session__write_header(session, evsel_list, output, true);
perf_session__delete(session); perf_session__delete(session);
perf_evlist__delete(evsel_list); perf_evlist__delete(evsel_list);
symbol__exit(); symbol__exit();
...@@ -524,7 +498,7 @@ static int __cmd_record(int argc, const char **argv) ...@@ -524,7 +498,7 @@ static int __cmd_record(int argc, const char **argv)
perf_header__set_feat(&session->header, HEADER_BUILD_ID); perf_header__set_feat(&session->header, HEADER_BUILD_ID);
if (!file_new) { if (!file_new) {
err = perf_header__read(session, output); err = perf_session__read_header(session, output);
if (err < 0) if (err < 0)
goto out_delete_session; goto out_delete_session;
} }
...@@ -588,8 +562,6 @@ static int __cmd_record(int argc, const char **argv) ...@@ -588,8 +562,6 @@ static int __cmd_record(int argc, const char **argv)
open_counters(evsel_list); open_counters(evsel_list);
perf_session__set_sample_type(session, sample_type);
/* /*
* perf_session__delete(session) will be called at atexit_header() * perf_session__delete(session) will be called at atexit_header()
*/ */
...@@ -600,20 +572,17 @@ static int __cmd_record(int argc, const char **argv) ...@@ -600,20 +572,17 @@ static int __cmd_record(int argc, const char **argv)
if (err < 0) if (err < 0)
return err; return err;
} else if (file_new) { } else if (file_new) {
err = perf_header__write(&session->header, evsel_list, err = perf_session__write_header(session, evsel_list,
output, false); output, false);
if (err < 0) if (err < 0)
return err; return err;
} }
post_processing_offset = lseek(output, 0, SEEK_CUR); post_processing_offset = lseek(output, 0, SEEK_CUR);
perf_session__set_sample_id_all(session, sample_id_all_avail);
if (pipe_output) { if (pipe_output) {
err = perf_event__synthesize_attrs(&session->header, err = perf_session__synthesize_attrs(session,
process_synthesized_event, process_synthesized_event);
session);
if (err < 0) { if (err < 0) {
pr_err("Couldn't synthesize attrs.\n"); pr_err("Couldn't synthesize attrs.\n");
return err; return err;
......
...@@ -70,8 +70,8 @@ static int perf_session__add_hist_entry(struct perf_session *session, ...@@ -70,8 +70,8 @@ static int perf_session__add_hist_entry(struct perf_session *session,
* FIXME: Propagate this back, but at least we're in a builtin, * FIXME: Propagate this back, but at least we're in a builtin,
* where exit() is allowed. ;-) * where exit() is allowed. ;-)
*/ */
ui__warning("Invalid %s file, contains samples with id not in " ui__warning("Invalid %s file, contains samples with id %" PRIu64 " not in "
"its header!\n", input_name); "its header!\n", input_name, sample->id);
exit_browser(0); exit_browser(0);
exit(1); exit(1);
} }
......
...@@ -883,7 +883,6 @@ static void start_counters(struct perf_evlist *evlist) ...@@ -883,7 +883,6 @@ static void start_counters(struct perf_evlist *evlist)
static int __cmd_top(void) static int __cmd_top(void)
{ {
pthread_t thread; pthread_t thread;
struct perf_evsel *first;
int ret __used; int ret __used;
/* /*
* FIXME: perf_session__new should allow passing a O_MMAP, so that all this * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
...@@ -900,8 +899,8 @@ static int __cmd_top(void) ...@@ -900,8 +899,8 @@ static int __cmd_top(void)
perf_event__synthesize_threads(perf_event__process, session); perf_event__synthesize_threads(perf_event__process, session);
start_counters(top.evlist); start_counters(top.evlist);
first = list_entry(top.evlist->entries.next, struct perf_evsel, node); session->evlist = top.evlist;
perf_session__set_sample_type(session, first->attr.sample_type); perf_session__update_sample_type(session);
/* Wait for a minimal set of events before starting the snapshot */ /* Wait for a minimal set of events before starting the snapshot */
poll(top.evlist->pollfd, top.evlist->nr_fds, 100); poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include <linux/hash.h> #include <linux/hash.h>
#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
#define SID(e, x, y) xyarray__entry(e->id, x, y) #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
struct thread_map *threads) struct thread_map *threads)
...@@ -106,8 +106,9 @@ void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) ...@@ -106,8 +106,9 @@ void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
evlist->nr_fds++; evlist->nr_fds++;
} }
void perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel, static void perf_evlist__id_hash(struct perf_evlist *evlist,
int cpu, int thread, u64 id) struct perf_evsel *evsel,
int cpu, int thread, u64 id)
{ {
int hash; int hash;
struct perf_sample_id *sid = SID(evsel, cpu, thread); 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,9 +119,16 @@ void perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel,
hlist_add_head(&sid->node, &evlist->heads[hash]); hlist_add_head(&sid->node, &evlist->heads[hash]);
} }
static int perf_evlist__id_hash_fd(struct perf_evlist *evlist, void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
struct perf_evsel *evsel, int cpu, int thread, u64 id)
int cpu, int thread, int fd) {
perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
evsel->id[evsel->ids++] = id;
}
static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
struct perf_evsel *evsel,
int cpu, int thread, int fd)
{ {
u64 read_data[4] = { 0, }; u64 read_data[4] = { 0, };
int id_idx = 1; /* The first entry is the counter value */ 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,7 +142,7 @@ static int perf_evlist__id_hash_fd(struct perf_evlist *evlist,
if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
++id_idx; ++id_idx;
perf_evlist__id_hash(evlist, evsel, cpu, thread, read_data[id_idx]); perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
return 0; return 0;
} }
...@@ -292,7 +300,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite) ...@@ -292,7 +300,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite)
list_for_each_entry(evsel, &evlist->entries, node) { list_for_each_entry(evsel, &evlist->entries, node) {
if ((evsel->attr.read_format & PERF_FORMAT_ID) && if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
evsel->id == NULL && evsel->sample_id == NULL &&
perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
return -ENOMEM; return -ENOMEM;
...@@ -308,7 +316,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite) ...@@ -308,7 +316,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite)
goto out_unmap; goto out_unmap;
if ((evsel->attr.read_format & PERF_FORMAT_ID) && if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
perf_evlist__id_hash_fd(evlist, evsel, cpu, thread, fd) < 0) perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
goto out_unmap; goto out_unmap;
} }
} }
......
...@@ -38,8 +38,8 @@ void perf_evlist__delete(struct perf_evlist *evlist); ...@@ -38,8 +38,8 @@ void perf_evlist__delete(struct perf_evlist *evlist);
void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry); void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry);
int perf_evlist__add_default(struct perf_evlist *evlist); int perf_evlist__add_default(struct perf_evlist *evlist);
void perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel, void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
int cpu, int thread, u64 id); int cpu, int thread, u64 id);
int perf_evlist__alloc_pollfd(struct perf_evlist *evlist); int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd); void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd);
......
...@@ -41,8 +41,18 @@ int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) ...@@ -41,8 +41,18 @@ int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
{ {
evsel->id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
return evsel->id != NULL ? 0 : -ENOMEM; if (evsel->sample_id == NULL)
return -ENOMEM;
evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
if (evsel->id == NULL) {
xyarray__delete(evsel->sample_id);
evsel->sample_id = NULL;
return -ENOMEM;
}
return 0;
} }
int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus) int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
...@@ -60,7 +70,9 @@ void perf_evsel__free_fd(struct perf_evsel *evsel) ...@@ -60,7 +70,9 @@ void perf_evsel__free_fd(struct perf_evsel *evsel)
void perf_evsel__free_id(struct perf_evsel *evsel) void perf_evsel__free_id(struct perf_evsel *evsel)
{ {
xyarray__delete(evsel->id); xyarray__delete(evsel->sample_id);
evsel->sample_id = NULL;
free(evsel->id);
evsel->id = NULL; evsel->id = NULL;
} }
...@@ -79,7 +91,8 @@ void perf_evsel__exit(struct perf_evsel *evsel) ...@@ -79,7 +91,8 @@ void perf_evsel__exit(struct perf_evsel *evsel)
{ {
assert(list_empty(&evsel->node)); assert(list_empty(&evsel->node));
xyarray__delete(evsel->fd); xyarray__delete(evsel->fd);
xyarray__delete(evsel->id); xyarray__delete(evsel->sample_id);
free(evsel->id);
} }
void perf_evsel__delete(struct perf_evsel *evsel) void perf_evsel__delete(struct perf_evsel *evsel)
......
...@@ -49,12 +49,17 @@ struct perf_evsel { ...@@ -49,12 +49,17 @@ struct perf_evsel {
struct perf_event_attr attr; struct perf_event_attr attr;
char *filter; char *filter;
struct xyarray *fd; struct xyarray *fd;
struct xyarray *id; struct xyarray *sample_id;
u64 *id;
struct perf_counts *counts; struct perf_counts *counts;
int idx; int idx;
int ids;
struct hists hists; struct hists hists;
char *name; char *name;
void *priv; union {
void *priv;
off_t id_offset;
};
struct cgroup_sel *cgrp; struct cgroup_sel *cgrp;
}; };
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include "evlist.h" #include "evlist.h"
#include "evsel.h"
#include "util.h" #include "util.h"
#include "header.h" #include "header.h"
#include "../perf.h" #include "../perf.h"
...@@ -19,89 +20,6 @@ ...@@ -19,89 +20,6 @@
static bool no_buildid_cache = false; static bool no_buildid_cache = false;
/*
* Create new perf.data header attribute:
*/
struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
{
struct perf_header_attr *self = malloc(sizeof(*self));
if (self != NULL) {
self->attr = *attr;
self->ids = 0;
self->size = 1;
self->id = malloc(sizeof(u64));
if (self->id == NULL) {
free(self);
self = NULL;
}
}
return self;
}
void perf_header_attr__delete(struct perf_header_attr *self)
{
free(self->id);
free(self);
}
int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
{
int pos = self->ids;
self->ids++;
if (self->ids > self->size) {
int nsize = self->size * 2;
u64 *nid = realloc(self->id, nsize * sizeof(u64));
if (nid == NULL)
return -1;
self->size = nsize;
self->id = nid;
}
self->id[pos] = id;
return 0;
}
int perf_header__init(struct perf_header *self)
{
self->size = 1;
self->attr = malloc(sizeof(void *));
return self->attr == NULL ? -ENOMEM : 0;
}
void perf_header__exit(struct perf_header *self)
{
int i;
for (i = 0; i < self->attrs; ++i)
perf_header_attr__delete(self->attr[i]);
free(self->attr);
}
int perf_header__add_attr(struct perf_header *self,
struct perf_header_attr *attr)
{
if (self->frozen)
return -1;
if (self->attrs == self->size) {
int nsize = self->size * 2;
struct perf_header_attr **nattr;
nattr = realloc(self->attr, nsize * sizeof(void *));
if (nattr == NULL)
return -1;
self->size = nsize;
self->attr = nattr;
}
self->attr[self->attrs++] = attr;
return 0;
}
static int event_count; static int event_count;
static struct perf_trace_event_type *events; static struct perf_trace_event_type *events;
...@@ -148,19 +66,19 @@ struct perf_file_attr { ...@@ -148,19 +66,19 @@ struct perf_file_attr {
struct perf_file_section ids; struct perf_file_section ids;
}; };
void perf_header__set_feat(struct perf_header *self, int feat) void perf_header__set_feat(struct perf_header *header, int feat)
{ {
set_bit(feat, self->adds_features); set_bit(feat, header->adds_features);
} }
void perf_header__clear_feat(struct perf_header *self, int feat) void perf_header__clear_feat(struct perf_header *header, int feat)
{ {
clear_bit(feat, self->adds_features); clear_bit(feat, header->adds_features);
} }
bool perf_header__has_feat(const struct perf_header *self, int feat) bool perf_header__has_feat(const struct perf_header *header, int feat)
{ {
return test_bit(feat, self->adds_features); return test_bit(feat, header->adds_features);
} }
static int do_write(int fd, const void *buf, size_t size) static int do_write(int fd, const void *buf, size_t size)
...@@ -229,22 +147,22 @@ static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, ...@@ -229,22 +147,22 @@ static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
return 0; return 0;
} }
static int machine__write_buildid_table(struct machine *self, int fd) static int machine__write_buildid_table(struct machine *machine, int fd)
{ {
int err; int err;
u16 kmisc = PERF_RECORD_MISC_KERNEL, u16 kmisc = PERF_RECORD_MISC_KERNEL,
umisc = PERF_RECORD_MISC_USER; umisc = PERF_RECORD_MISC_USER;
if (!machine__is_host(self)) { if (!machine__is_host(machine)) {
kmisc = PERF_RECORD_MISC_GUEST_KERNEL; kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
umisc = PERF_RECORD_MISC_GUEST_USER; umisc = PERF_RECORD_MISC_GUEST_USER;
} }
err = __dsos__write_buildid_table(&self->kernel_dsos, self->pid, err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
kmisc, fd); kmisc, fd);
if (err == 0) if (err == 0)
err = __dsos__write_buildid_table(&self->user_dsos, err = __dsos__write_buildid_table(&machine->user_dsos,
self->pid, umisc, fd); machine->pid, umisc, fd);
return err; return err;
} }
...@@ -362,12 +280,12 @@ int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) ...@@ -362,12 +280,12 @@ int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
return err; return err;
} }
static int dso__cache_build_id(struct dso *self, const char *debugdir) static int dso__cache_build_id(struct dso *dso, const char *debugdir)
{ {
bool is_kallsyms = self->kernel && self->long_name[0] != '/'; bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
return build_id_cache__add_b(self->build_id, sizeof(self->build_id), return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
self->long_name, debugdir, is_kallsyms); dso->long_name, debugdir, is_kallsyms);
} }
static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
...@@ -382,14 +300,14 @@ static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) ...@@ -382,14 +300,14 @@ static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
return err; return err;
} }
static int machine__cache_build_ids(struct machine *self, const char *debugdir) static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
{ {
int ret = __dsos__cache_build_ids(&self->kernel_dsos, debugdir); int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
ret |= __dsos__cache_build_ids(&self->user_dsos, debugdir); ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
return ret; return ret;
} }
static int perf_session__cache_build_ids(struct perf_session *self) static int perf_session__cache_build_ids(struct perf_session *session)
{ {
struct rb_node *nd; struct rb_node *nd;
int ret; int ret;
...@@ -400,28 +318,28 @@ static int perf_session__cache_build_ids(struct perf_session *self) ...@@ -400,28 +318,28 @@ static int perf_session__cache_build_ids(struct perf_session *self)
if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
return -1; return -1;
ret = machine__cache_build_ids(&self->host_machine, debugdir); ret = machine__cache_build_ids(&session->host_machine, debugdir);
for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) { for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
struct machine *pos = rb_entry(nd, struct machine, rb_node); struct machine *pos = rb_entry(nd, struct machine, rb_node);
ret |= machine__cache_build_ids(pos, debugdir); ret |= machine__cache_build_ids(pos, debugdir);
} }
return ret ? -1 : 0; return ret ? -1 : 0;
} }
static bool machine__read_build_ids(struct machine *self, bool with_hits) static bool machine__read_build_ids(struct machine *machine, bool with_hits)
{ {
bool ret = __dsos__read_build_ids(&self->kernel_dsos, with_hits); bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
ret |= __dsos__read_build_ids(&self->user_dsos, with_hits); ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
return ret; return ret;
} }
static bool perf_session__read_build_ids(struct perf_session *self, bool with_hits) static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
{ {
struct rb_node *nd; struct rb_node *nd;
bool ret = machine__read_build_ids(&self->host_machine, with_hits); bool ret = machine__read_build_ids(&session->host_machine, with_hits);
for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) { for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
struct machine *pos = rb_entry(nd, struct machine, rb_node); struct machine *pos = rb_entry(nd, struct machine, rb_node);
ret |= machine__read_build_ids(pos, with_hits); ret |= machine__read_build_ids(pos, with_hits);
} }
...@@ -429,7 +347,7 @@ static bool perf_session__read_build_ids(struct perf_session *self, bool with_hi ...@@ -429,7 +347,7 @@ static bool perf_session__read_build_ids(struct perf_session *self, bool with_hi
return ret; return ret;
} }
static int perf_header__adds_write(struct perf_header *self, static int perf_header__adds_write(struct perf_header *header,
struct perf_evlist *evlist, int fd) struct perf_evlist *evlist, int fd)
{ {
int nr_sections; int nr_sections;
...@@ -439,13 +357,13 @@ static int perf_header__adds_write(struct perf_header *self, ...@@ -439,13 +357,13 @@ static int perf_header__adds_write(struct perf_header *self,
u64 sec_start; u64 sec_start;
int idx = 0, err; int idx = 0, err;
session = container_of(self, struct perf_session, header); session = container_of(header, struct perf_session, header);
if (perf_header__has_feat(self, HEADER_BUILD_ID && if (perf_header__has_feat(header, HEADER_BUILD_ID &&
!perf_session__read_build_ids(session, true))) !perf_session__read_build_ids(session, true)))
perf_header__clear_feat(self, HEADER_BUILD_ID); perf_header__clear_feat(header, HEADER_BUILD_ID);
nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
if (!nr_sections) if (!nr_sections)
return 0; return 0;
...@@ -455,10 +373,10 @@ static int perf_header__adds_write(struct perf_header *self, ...@@ -455,10 +373,10 @@ static int perf_header__adds_write(struct perf_header *self,
sec_size = sizeof(*feat_sec) * nr_sections; sec_size = sizeof(*feat_sec) * nr_sections;
sec_start = self->data_offset + self->data_size; sec_start = header->data_offset + header->data_size;
lseek(fd, sec_start + sec_size, SEEK_SET); lseek(fd, sec_start + sec_size, SEEK_SET);
if (perf_header__has_feat(self, HEADER_TRACE_INFO)) { if (perf_header__has_feat(header, HEADER_TRACE_INFO)) {
struct perf_file_section *trace_sec; struct perf_file_section *trace_sec;
trace_sec = &feat_sec[idx++]; trace_sec = &feat_sec[idx++];
...@@ -469,14 +387,14 @@ static int perf_header__adds_write(struct perf_header *self, ...@@ -469,14 +387,14 @@ static int perf_header__adds_write(struct perf_header *self,
trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset; trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
} }
if (perf_header__has_feat(self, HEADER_BUILD_ID)) { if (perf_header__has_feat(header, HEADER_BUILD_ID)) {
struct perf_file_section *buildid_sec; struct perf_file_section *buildid_sec;
buildid_sec = &feat_sec[idx++]; buildid_sec = &feat_sec[idx++];
/* Write build-ids */ /* Write build-ids */
buildid_sec->offset = lseek(fd, 0, SEEK_CUR); buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
err = dsos__write_buildid_table(self, fd); err = dsos__write_buildid_table(header, fd);
if (err < 0) { if (err < 0) {
pr_debug("failed to write buildid table\n"); pr_debug("failed to write buildid table\n");
goto out_free; goto out_free;
...@@ -515,33 +433,41 @@ int perf_header__write_pipe(int fd) ...@@ -515,33 +433,41 @@ int perf_header__write_pipe(int fd)
return 0; return 0;
} }
int perf_header__write(struct perf_header *self, struct perf_evlist *evlist, int perf_session__write_header(struct perf_session *session,
int fd, bool at_exit) struct perf_evlist *evlist,
int fd, bool at_exit)
{ {
struct perf_file_header f_header; struct perf_file_header f_header;
struct perf_file_attr f_attr; struct perf_file_attr f_attr;
struct perf_header_attr *attr; struct perf_header *header = &session->header;
int i, err; struct perf_evsel *attr, *pair = NULL;
int err;
lseek(fd, sizeof(f_header), SEEK_SET); lseek(fd, sizeof(f_header), SEEK_SET);
for (i = 0; i < self->attrs; i++) { if (session->evlist != evlist)
attr = self->attr[i]; pair = list_entry(session->evlist->entries.next, struct perf_evsel, node);
list_for_each_entry(attr, &evlist->entries, node) {
attr->id_offset = lseek(fd, 0, SEEK_CUR); attr->id_offset = lseek(fd, 0, SEEK_CUR);
err = do_write(fd, attr->id, attr->ids * sizeof(u64)); err = do_write(fd, attr->id, attr->ids * sizeof(u64));
if (err < 0) { if (err < 0) {
out_err_write:
pr_debug("failed to write perf header\n"); pr_debug("failed to write perf header\n");
return err; return err;
} }
if (session->evlist != evlist) {
err = do_write(fd, pair->id, pair->ids * sizeof(u64));
if (err < 0)
goto out_err_write;
attr->ids += pair->ids;
pair = list_entry(pair->node.next, struct perf_evsel, node);
}
} }
header->attr_offset = lseek(fd, 0, SEEK_CUR);
self->attr_offset = lseek(fd, 0, SEEK_CUR); list_for_each_entry(attr, &evlist->entries, node) {
for (i = 0; i < self->attrs; i++) {
attr = self->attr[i];
f_attr = (struct perf_file_attr){ f_attr = (struct perf_file_attr){
.attr = attr->attr, .attr = attr->attr,
.ids = { .ids = {
...@@ -556,20 +482,20 @@ int perf_header__write(struct perf_header *self, struct perf_evlist *evlist, ...@@ -556,20 +482,20 @@ int perf_header__write(struct perf_header *self, struct perf_evlist *evlist,
} }
} }
self->event_offset = lseek(fd, 0, SEEK_CUR); header->event_offset = lseek(fd, 0, SEEK_CUR);
self->event_size = event_count * sizeof(struct perf_trace_event_type); header->event_size = event_count * sizeof(struct perf_trace_event_type);
if (events) { if (events) {
err = do_write(fd, events, self->event_size); err = do_write(fd, events, header->event_size);
if (err < 0) { if (err < 0) {
pr_debug("failed to write perf header events\n"); pr_debug("failed to write perf header events\n");
return err; return err;
} }
} }
self->data_offset = lseek(fd, 0, SEEK_CUR); header->data_offset = lseek(fd, 0, SEEK_CUR);
if (at_exit) { if (at_exit) {
err = perf_header__adds_write(self, evlist, fd); err = perf_header__adds_write(header, evlist, fd);
if (err < 0) if (err < 0)
return err; return err;
} }
...@@ -579,20 +505,20 @@ int perf_header__write(struct perf_header *self, struct perf_evlist *evlist, ...@@ -579,20 +505,20 @@ int perf_header__write(struct perf_header *self, struct perf_evlist *evlist,
.size = sizeof(f_header), .size = sizeof(f_header),
.attr_size = sizeof(f_attr), .attr_size = sizeof(f_attr),
.attrs = { .attrs = {
.offset = self->attr_offset, .offset = header->attr_offset,
.size = self->attrs * sizeof(f_attr), .size = evlist->nr_entries * sizeof(f_attr),
}, },
.data = { .data = {
.offset = self->data_offset, .offset = header->data_offset,
.size = self->data_size, .size = header->data_size,
}, },
.event_types = { .event_types = {
.offset = self->event_offset, .offset = header->event_offset,
.size = self->event_size, .size = header->event_size,
}, },
}; };
memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features)); memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
err = do_write(fd, &f_header, sizeof(f_header)); err = do_write(fd, &f_header, sizeof(f_header));
...@@ -600,26 +526,26 @@ int perf_header__write(struct perf_header *self, struct perf_evlist *evlist, ...@@ -600,26 +526,26 @@ int perf_header__write(struct perf_header *self, struct perf_evlist *evlist,
pr_debug("failed to write perf header\n"); pr_debug("failed to write perf header\n");
return err; return err;
} }
lseek(fd, self->data_offset + self->data_size, SEEK_SET); lseek(fd, header->data_offset + header->data_size, SEEK_SET);
self->frozen = 1; header->frozen = 1;
return 0; return 0;
} }
static int perf_header__getbuffer64(struct perf_header *self, static int perf_header__getbuffer64(struct perf_header *header,
int fd, void *buf, size_t size) int fd, void *buf, size_t size)
{ {
if (readn(fd, buf, size) <= 0) if (readn(fd, buf, size) <= 0)
return -1; return -1;
if (self->needs_swap) if (header->needs_swap)
mem_bswap_64(buf, size); mem_bswap_64(buf, size);
return 0; return 0;
} }
int perf_header__process_sections(struct perf_header *self, int fd, int perf_header__process_sections(struct perf_header *header, int fd,
int (*process)(struct perf_file_section *self, int (*process)(struct perf_file_section *section,
struct perf_header *ph, struct perf_header *ph,
int feat, int fd)) int feat, int fd))
{ {
...@@ -629,7 +555,7 @@ int perf_header__process_sections(struct perf_header *self, int fd, ...@@ -629,7 +555,7 @@ int perf_header__process_sections(struct perf_header *self, int fd,
int idx = 0; int idx = 0;
int err = -1, feat = 1; int err = -1, feat = 1;
nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
if (!nr_sections) if (!nr_sections)
return 0; return 0;
...@@ -639,17 +565,17 @@ int perf_header__process_sections(struct perf_header *self, int fd, ...@@ -639,17 +565,17 @@ int perf_header__process_sections(struct perf_header *self, int fd,
sec_size = sizeof(*feat_sec) * nr_sections; sec_size = sizeof(*feat_sec) * nr_sections;
lseek(fd, self->data_offset + self->data_size, SEEK_SET); lseek(fd, header->data_offset + header->data_size, SEEK_SET);
if (perf_header__getbuffer64(self, fd, feat_sec, sec_size)) if (perf_header__getbuffer64(header, fd, feat_sec, sec_size))
goto out_free; goto out_free;
err = 0; err = 0;
while (idx < nr_sections && feat < HEADER_LAST_FEATURE) { while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
if (perf_header__has_feat(self, feat)) { if (perf_header__has_feat(header, feat)) {
struct perf_file_section *sec = &feat_sec[idx++]; struct perf_file_section *sec = &feat_sec[idx++];
err = process(sec, self, feat, fd); err = process(sec, header, feat, fd);
if (err < 0) if (err < 0)
break; break;
} }
...@@ -660,35 +586,35 @@ int perf_header__process_sections(struct perf_header *self, int fd, ...@@ -660,35 +586,35 @@ int perf_header__process_sections(struct perf_header *self, int fd,
return err; return err;
} }
int perf_file_header__read(struct perf_file_header *self, int perf_file_header__read(struct perf_file_header *header,
struct perf_header *ph, int fd) struct perf_header *ph, int fd)
{ {
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
if (readn(fd, self, sizeof(*self)) <= 0 || if (readn(fd, header, sizeof(*header)) <= 0 ||
memcmp(&self->magic, __perf_magic, sizeof(self->magic))) memcmp(&header->magic, __perf_magic, sizeof(header->magic)))
return -1; return -1;
if (self->attr_size != sizeof(struct perf_file_attr)) { if (header->attr_size != sizeof(struct perf_file_attr)) {
u64 attr_size = bswap_64(self->attr_size); u64 attr_size = bswap_64(header->attr_size);
if (attr_size != sizeof(struct perf_file_attr)) if (attr_size != sizeof(struct perf_file_attr))
return -1; return -1;
mem_bswap_64(self, offsetof(struct perf_file_header, mem_bswap_64(header, offsetof(struct perf_file_header,
adds_features)); adds_features));
ph->needs_swap = true; ph->needs_swap = true;
} }
if (self->size != sizeof(*self)) { if (header->size != sizeof(*header)) {
/* Support the previous format */ /* Support the previous format */
if (self->size == offsetof(typeof(*self), adds_features)) if (header->size == offsetof(typeof(*header), adds_features))
bitmap_zero(self->adds_features, HEADER_FEAT_BITS); bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
else else
return -1; return -1;
} }
memcpy(&ph->adds_features, &self->adds_features, memcpy(&ph->adds_features, &header->adds_features,
sizeof(ph->adds_features)); sizeof(ph->adds_features));
/* /*
* FIXME: hack that assumes that if we need swap the perf.data file * FIXME: hack that assumes that if we need swap the perf.data file
...@@ -702,10 +628,10 @@ int perf_file_header__read(struct perf_file_header *self, ...@@ -702,10 +628,10 @@ int perf_file_header__read(struct perf_file_header *self,
perf_header__set_feat(ph, HEADER_BUILD_ID); perf_header__set_feat(ph, HEADER_BUILD_ID);
} }
ph->event_offset = self->event_types.offset; ph->event_offset = header->event_types.offset;
ph->event_size = self->event_types.size; ph->event_size = header->event_types.size;
ph->data_offset = self->data.offset; ph->data_offset = header->data.offset;
ph->data_size = self->data.size; ph->data_size = header->data.size;
return 0; return 0;
} }
...@@ -764,11 +690,10 @@ static int __event_process_build_id(struct build_id_event *bev, ...@@ -764,11 +690,10 @@ static int __event_process_build_id(struct build_id_event *bev,
return err; return err;
} }
static int perf_header__read_build_ids(struct perf_header *self, static int perf_header__read_build_ids(struct perf_header *header,
int input, u64 offset, u64 size) int input, u64 offset, u64 size)
{ {
struct perf_session *session = container_of(self, struct perf_session *session = container_of(header, struct perf_session, header);
struct perf_session, header);
struct build_id_event bev; struct build_id_event bev;
char filename[PATH_MAX]; char filename[PATH_MAX];
u64 limit = offset + size; u64 limit = offset + size;
...@@ -780,7 +705,7 @@ static int perf_header__read_build_ids(struct perf_header *self, ...@@ -780,7 +705,7 @@ static int perf_header__read_build_ids(struct perf_header *self,
if (read(input, &bev, sizeof(bev)) != sizeof(bev)) if (read(input, &bev, sizeof(bev)) != sizeof(bev))
goto out; goto out;
if (self->needs_swap) if (header->needs_swap)
perf_event_header__bswap(&bev.header); perf_event_header__bswap(&bev.header);
len = bev.header.size - sizeof(bev); len = bev.header.size - sizeof(bev);
...@@ -796,13 +721,13 @@ static int perf_header__read_build_ids(struct perf_header *self, ...@@ -796,13 +721,13 @@ static int perf_header__read_build_ids(struct perf_header *self,
return err; return err;
} }
static int perf_file_section__process(struct perf_file_section *self, static int perf_file_section__process(struct perf_file_section *section,
struct perf_header *ph, struct perf_header *ph,
int feat, int fd) int feat, int fd)
{ {
if (lseek(fd, self->offset, SEEK_SET) == (off_t)-1) { if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
pr_debug("Failed to lseek to %" PRIu64 " offset for feature " pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
"%d, continuing...\n", self->offset, feat); "%d, continuing...\n", section->offset, feat);
return 0; return 0;
} }
...@@ -812,7 +737,7 @@ static int perf_file_section__process(struct perf_file_section *self, ...@@ -812,7 +737,7 @@ static int perf_file_section__process(struct perf_file_section *self,
break; break;
case HEADER_BUILD_ID: case HEADER_BUILD_ID:
if (perf_header__read_build_ids(ph, fd, self->offset, self->size)) if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
pr_debug("Failed to read buildids, continuing...\n"); pr_debug("Failed to read buildids, continuing...\n");
break; break;
default: default:
...@@ -822,21 +747,21 @@ static int perf_file_section__process(struct perf_file_section *self, ...@@ -822,21 +747,21 @@ static int perf_file_section__process(struct perf_file_section *self,
return 0; return 0;
} }
static int perf_file_header__read_pipe(struct perf_pipe_file_header *self, static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
struct perf_header *ph, int fd, struct perf_header *ph, int fd,
bool repipe) bool repipe)
{ {
if (readn(fd, self, sizeof(*self)) <= 0 || if (readn(fd, header, sizeof(*header)) <= 0 ||
memcmp(&self->magic, __perf_magic, sizeof(self->magic))) memcmp(&header->magic, __perf_magic, sizeof(header->magic)))
return -1; return -1;
if (repipe && do_write(STDOUT_FILENO, self, sizeof(*self)) < 0) if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
return -1; return -1;
if (self->size != sizeof(*self)) { if (header->size != sizeof(*header)) {
u64 size = bswap_64(self->size); u64 size = bswap_64(header->size);
if (size != sizeof(*self)) if (size != sizeof(*header))
return -1; return -1;
ph->needs_swap = true; ph->needs_swap = true;
...@@ -847,10 +772,10 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *self, ...@@ -847,10 +772,10 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *self,
static int perf_header__read_pipe(struct perf_session *session, int fd) static int perf_header__read_pipe(struct perf_session *session, int fd)
{ {
struct perf_header *self = &session->header; struct perf_header *header = &session->header;
struct perf_pipe_file_header f_header; struct perf_pipe_file_header f_header;
if (perf_file_header__read_pipe(&f_header, self, fd, if (perf_file_header__read_pipe(&f_header, header, fd,
session->repipe) < 0) { session->repipe) < 0) {
pr_debug("incompatible file format\n"); pr_debug("incompatible file format\n");
return -EINVAL; return -EINVAL;
...@@ -861,18 +786,22 @@ static int perf_header__read_pipe(struct perf_session *session, int fd) ...@@ -861,18 +786,22 @@ static int perf_header__read_pipe(struct perf_session *session, int fd)
return 0; return 0;
} }
int perf_header__read(struct perf_session *session, int fd) int perf_session__read_header(struct perf_session *session, int fd)
{ {
struct perf_header *self = &session->header; struct perf_header *header = &session->header;
struct perf_file_header f_header; struct perf_file_header f_header;
struct perf_file_attr f_attr; struct perf_file_attr f_attr;
u64 f_id; u64 f_id;
int nr_attrs, nr_ids, i, j; int nr_attrs, nr_ids, i, j;
session->evlist = perf_evlist__new(NULL, NULL);
if (session->evlist == NULL)
return -ENOMEM;
if (session->fd_pipe) if (session->fd_pipe)
return perf_header__read_pipe(session, fd); return perf_header__read_pipe(session, fd);
if (perf_file_header__read(&f_header, self, fd) < 0) { if (perf_file_header__read(&f_header, header, fd) < 0) {
pr_debug("incompatible file format\n"); pr_debug("incompatible file format\n");
return -EINVAL; return -EINVAL;
} }
...@@ -881,33 +810,39 @@ int perf_header__read(struct perf_session *session, int fd) ...@@ -881,33 +810,39 @@ int perf_header__read(struct perf_session *session, int fd)
lseek(fd, f_header.attrs.offset, SEEK_SET); lseek(fd, f_header.attrs.offset, SEEK_SET);
for (i = 0; i < nr_attrs; i++) { for (i = 0; i < nr_attrs; i++) {
struct perf_header_attr *attr; struct perf_evsel *evsel;
off_t tmp; off_t tmp;
if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr))) if (perf_header__getbuffer64(header, fd, &f_attr, sizeof(f_attr)))
goto out_errno; goto out_errno;
tmp = lseek(fd, 0, SEEK_CUR); tmp = lseek(fd, 0, SEEK_CUR);
evsel = perf_evsel__new(&f_attr.attr, i);
attr = perf_header_attr__new(&f_attr.attr); if (evsel == NULL)
if (attr == NULL) goto out_delete_evlist;
return -ENOMEM; /*
* Do it before so that if perf_evsel__alloc_id fails, this
* entry gets purged too at perf_evlist__delete().
*/
perf_evlist__add(session->evlist, evsel);
nr_ids = f_attr.ids.size / sizeof(u64); nr_ids = f_attr.ids.size / sizeof(u64);
/*
* We don't have the cpu and thread maps on the header, so
* for allocating the perf_sample_id table we fake 1 cpu and
* hattr->ids threads.
*/
if (perf_evsel__alloc_id(evsel, 1, nr_ids))
goto out_delete_evlist;
lseek(fd, f_attr.ids.offset, SEEK_SET); lseek(fd, f_attr.ids.offset, SEEK_SET);
for (j = 0; j < nr_ids; j++) { for (j = 0; j < nr_ids; j++) {
if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id))) if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
goto out_errno; goto out_errno;
if (perf_header_attr__add_id(attr, f_id) < 0) { perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
perf_header_attr__delete(attr);
return -ENOMEM;
}
}
if (perf_header__add_attr(self, attr) < 0) {
perf_header_attr__delete(attr);
return -ENOMEM;
} }
lseek(fd, tmp, SEEK_SET); lseek(fd, tmp, SEEK_SET);
...@@ -918,51 +853,52 @@ int perf_header__read(struct perf_session *session, int fd) ...@@ -918,51 +853,52 @@ int perf_header__read(struct perf_session *session, int fd)
events = malloc(f_header.event_types.size); events = malloc(f_header.event_types.size);
if (events == NULL) if (events == NULL)
return -ENOMEM; return -ENOMEM;
if (perf_header__getbuffer64(self, fd, events, if (perf_header__getbuffer64(header, fd, events,
f_header.event_types.size)) f_header.event_types.size))
goto out_errno; goto out_errno;
event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
} }
perf_header__process_sections(self, fd, perf_file_section__process); perf_header__process_sections(header, fd, perf_file_section__process);
lseek(fd, self->data_offset, SEEK_SET); lseek(fd, header->data_offset, SEEK_SET);
self->frozen = 1; header->frozen = 1;
return 0; return 0;
out_errno: out_errno:
return -errno; return -errno;
out_delete_evlist:
perf_evlist__delete(session->evlist);
session->evlist = NULL;
return -ENOMEM;
} }
u64 perf_header__sample_type(struct perf_header *header) u64 perf_evlist__sample_type(struct perf_evlist *evlist)
{ {
struct perf_evsel *pos;
u64 type = 0; u64 type = 0;
int i;
for (i = 0; i < header->attrs; i++) {
struct perf_header_attr *attr = header->attr[i];
list_for_each_entry(pos, &evlist->entries, node) {
if (!type) if (!type)
type = attr->attr.sample_type; type = pos->attr.sample_type;
else if (type != attr->attr.sample_type) else if (type != pos->attr.sample_type)
die("non matching sample_type"); die("non matching sample_type");
} }
return type; return type;
} }
bool perf_header__sample_id_all(const struct perf_header *header) bool perf_evlist__sample_id_all(const struct perf_evlist *evlist)
{ {
bool value = false, first = true; bool value = false, first = true;
int i; struct perf_evsel *pos;
for (i = 0; i < header->attrs; i++) {
struct perf_header_attr *attr = header->attr[i];
list_for_each_entry(pos, &evlist->entries, node) {
if (first) { if (first) {
value = attr->attr.sample_id_all; value = pos->attr.sample_id_all;
first = false; first = false;
} else if (value != attr->attr.sample_id_all) } else if (value != pos->attr.sample_id_all)
die("non matching sample_id_all"); die("non matching sample_id_all");
} }
...@@ -1000,16 +936,13 @@ int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id, ...@@ -1000,16 +936,13 @@ int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
return err; return err;
} }
int perf_event__synthesize_attrs(struct perf_header *self, int perf_session__synthesize_attrs(struct perf_session *session,
perf_event__handler_t process, perf_event__handler_t process)
struct perf_session *session)
{ {
struct perf_header_attr *attr; struct perf_evsel *attr;
int i, err = 0; int err = 0;
for (i = 0; i < self->attrs; i++) {
attr = self->attr[i];
list_for_each_entry(attr, &session->evlist->entries, node) {
err = perf_event__synthesize_attr(&attr->attr, attr->ids, err = perf_event__synthesize_attr(&attr->attr, attr->ids,
attr->id, process, session); attr->id, process, session);
if (err) { if (err) {
...@@ -1024,27 +957,36 @@ int perf_event__synthesize_attrs(struct perf_header *self, ...@@ -1024,27 +957,36 @@ int perf_event__synthesize_attrs(struct perf_header *self,
int perf_event__process_attr(union perf_event *event, int perf_event__process_attr(union perf_event *event,
struct perf_session *session) struct perf_session *session)
{ {
struct perf_header_attr *attr;
unsigned int i, ids, n_ids; unsigned int i, ids, n_ids;
struct perf_evsel *evsel;
attr = perf_header_attr__new(&event->attr.attr); if (session->evlist == NULL) {
if (attr == NULL) session->evlist = perf_evlist__new(NULL, NULL);
if (session->evlist == NULL)
return -ENOMEM;
}
evsel = perf_evsel__new(&event->attr.attr,
session->evlist->nr_entries);
if (evsel == NULL)
return -ENOMEM; return -ENOMEM;
perf_evlist__add(session->evlist, evsel);
ids = event->header.size; ids = event->header.size;
ids -= (void *)&event->attr.id - (void *)event; ids -= (void *)&event->attr.id - (void *)event;
n_ids = ids / sizeof(u64); n_ids = ids / sizeof(u64);
/*
* We don't have the cpu and thread maps on the header, so
* for allocating the perf_sample_id table we fake 1 cpu and
* hattr->ids threads.
*/
if (perf_evsel__alloc_id(evsel, 1, n_ids))
return -ENOMEM;
for (i = 0; i < n_ids; i++) { for (i = 0; i < n_ids; i++) {
if (perf_header_attr__add_id(attr, event->attr.id[i]) < 0) { perf_evlist__id_add(session->evlist, evsel, 0, i,
perf_header_attr__delete(attr); event->attr.id[i]);
return -ENOMEM;
}
}
if (perf_header__add_attr(&session->header, attr) < 0) {
perf_header_attr__delete(attr);
return -ENOMEM;
} }
perf_session__update_sample_type(session); perf_session__update_sample_type(session);
......
...@@ -9,13 +9,6 @@ ...@@ -9,13 +9,6 @@
#include <linux/bitmap.h> #include <linux/bitmap.h>
struct perf_header_attr {
struct perf_event_attr attr;
int ids, size;
u64 *id;
off_t id_offset;
};
enum { enum {
HEADER_TRACE_INFO = 1, HEADER_TRACE_INFO = 1,
HEADER_BUILD_ID, HEADER_BUILD_ID,
...@@ -46,14 +39,12 @@ struct perf_pipe_file_header { ...@@ -46,14 +39,12 @@ struct perf_pipe_file_header {
struct perf_header; struct perf_header;
int perf_file_header__read(struct perf_file_header *self, int perf_file_header__read(struct perf_file_header *header,
struct perf_header *ph, int fd); struct perf_header *ph, int fd);
struct perf_header { struct perf_header {
int frozen; int frozen;
int attrs, size;
bool needs_swap; bool needs_swap;
struct perf_header_attr **attr;
s64 attr_offset; s64 attr_offset;
u64 data_offset; u64 data_offset;
u64 data_size; u64 data_size;
...@@ -62,35 +53,25 @@ struct perf_header { ...@@ -62,35 +53,25 @@ struct perf_header {
DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS); DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
}; };
int perf_header__init(struct perf_header *self);
void perf_header__exit(struct perf_header *self);
struct perf_evlist; struct perf_evlist;
int perf_header__read(struct perf_session *session, int fd); int perf_session__read_header(struct perf_session *session, int fd);
int perf_header__write(struct perf_header *self, struct perf_evlist *evlist, int perf_session__write_header(struct perf_session *session,
int fd, bool at_exit); struct perf_evlist *evlist,
int fd, bool at_exit);
int perf_header__write_pipe(int fd); int perf_header__write_pipe(int fd);
int perf_header__add_attr(struct perf_header *self,
struct perf_header_attr *attr);
int perf_header__push_event(u64 id, const char *name); int perf_header__push_event(u64 id, const char *name);
char *perf_header__find_event(u64 id); char *perf_header__find_event(u64 id);
struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr); u64 perf_evlist__sample_type(struct perf_evlist *evlist);
void perf_header_attr__delete(struct perf_header_attr *self); bool perf_evlist__sample_id_all(const struct perf_evlist *evlist);
void perf_header__set_feat(struct perf_header *header, int feat);
void perf_header__clear_feat(struct perf_header *header, int feat);
bool perf_header__has_feat(const struct perf_header *header, int feat);
int perf_header_attr__add_id(struct perf_header_attr *self, u64 id); int perf_header__process_sections(struct perf_header *header, int fd,
int (*process)(struct perf_file_section *section,
u64 perf_header__sample_type(struct perf_header *header);
bool perf_header__sample_id_all(const struct perf_header *header);
void perf_header__set_feat(struct perf_header *self, int feat);
void perf_header__clear_feat(struct perf_header *self, int feat);
bool perf_header__has_feat(const struct perf_header *self, int feat);
int perf_header__process_sections(struct perf_header *self, int fd,
int (*process)(struct perf_file_section *self,
struct perf_header *ph, struct perf_header *ph,
int feat, int fd)); int feat, int fd));
...@@ -101,9 +82,8 @@ int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir); ...@@ -101,9 +82,8 @@ int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir);
int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id, int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
perf_event__handler_t process, perf_event__handler_t process,
struct perf_session *session); struct perf_session *session);
int perf_event__synthesize_attrs(struct perf_header *self, int perf_session__synthesize_attrs(struct perf_session *session,
perf_event__handler_t process, perf_event__handler_t process);
struct perf_session *session);
int perf_event__process_attr(union perf_event *event, struct perf_session *session); int perf_event__process_attr(union perf_event *event, struct perf_session *session);
int perf_event__synthesize_event_type(u64 event_id, char *name, int perf_event__synthesize_event_type(u64 event_id, char *name,
......
...@@ -13,46 +13,6 @@ ...@@ -13,46 +13,6 @@
#include "sort.h" #include "sort.h"
#include "util.h" #include "util.h"
static int perf_session__read_evlist(struct perf_session *session)
{
int i, j;
session->evlist = perf_evlist__new(NULL, NULL);
if (session->evlist == NULL)
return -ENOMEM;
for (i = 0; i < session->header.attrs; ++i) {
struct perf_header_attr *hattr = session->header.attr[i];
struct perf_evsel *evsel = perf_evsel__new(&hattr->attr, i);
if (evsel == NULL)
goto out_delete_evlist;
/*
* Do it before so that if perf_evsel__alloc_id fails, this
* entry gets purged too at perf_evlist__delete().
*/
perf_evlist__add(session->evlist, evsel);
/*
* We don't have the cpu and thread maps on the header, so
* for allocating the perf_sample_id table we fake 1 cpu and
* hattr->ids threads.
*/
if (perf_evsel__alloc_id(evsel, 1, hattr->ids))
goto out_delete_evlist;
for (j = 0; j < hattr->ids; ++j)
perf_evlist__id_hash(session->evlist, evsel, 0, j,
hattr->id[j]);
}
return 0;
out_delete_evlist:
perf_evlist__delete(session->evlist);
session->evlist = NULL;
return -ENOMEM;
}
static int perf_session__open(struct perf_session *self, bool force) static int perf_session__open(struct perf_session *self, bool force)
{ {
struct stat input_stat; struct stat input_stat;
...@@ -61,7 +21,7 @@ static int perf_session__open(struct perf_session *self, bool force) ...@@ -61,7 +21,7 @@ static int perf_session__open(struct perf_session *self, bool force)
self->fd_pipe = true; self->fd_pipe = true;
self->fd = STDIN_FILENO; self->fd = STDIN_FILENO;
if (perf_header__read(self, self->fd) < 0) if (perf_session__read_header(self, self->fd) < 0)
pr_err("incompatible file format"); pr_err("incompatible file format");
return 0; return 0;
...@@ -93,16 +53,11 @@ static int perf_session__open(struct perf_session *self, bool force) ...@@ -93,16 +53,11 @@ static int perf_session__open(struct perf_session *self, bool force)
goto out_close; goto out_close;
} }
if (perf_header__read(self, self->fd) < 0) { if (perf_session__read_header(self, self->fd) < 0) {
pr_err("incompatible file format"); pr_err("incompatible file format");
goto out_close; goto out_close;
} }
if (perf_session__read_evlist(self) < 0) {
pr_err("Not enough memory to read the event selector list\n");
goto out_close;
}
self->size = input_stat.st_size; self->size = input_stat.st_size;
return 0; return 0;
...@@ -139,21 +94,10 @@ static void perf_session__id_header_size(struct perf_session *session) ...@@ -139,21 +94,10 @@ static void perf_session__id_header_size(struct perf_session *session)
session->id_hdr_size = size; session->id_hdr_size = size;
} }
void perf_session__set_sample_id_all(struct perf_session *session, bool value)
{
session->sample_id_all = value;
perf_session__id_header_size(session);
}
void perf_session__set_sample_type(struct perf_session *session, u64 type)
{
session->sample_type = type;
}
void perf_session__update_sample_type(struct perf_session *self) void perf_session__update_sample_type(struct perf_session *self)
{ {
self->sample_type = perf_header__sample_type(&self->header); self->sample_type = perf_evlist__sample_type(self->evlist);
self->sample_id_all = perf_header__sample_id_all(&self->header); self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
perf_session__id_header_size(self); perf_session__id_header_size(self);
} }
...@@ -182,9 +126,6 @@ struct perf_session *perf_session__new(const char *filename, int mode, ...@@ -182,9 +126,6 @@ struct perf_session *perf_session__new(const char *filename, int mode,
if (self == NULL) if (self == NULL)
goto out; goto out;
if (perf_header__init(&self->header) < 0)
goto out_free;
memcpy(self->filename, filename, len); memcpy(self->filename, filename, len);
self->threads = RB_ROOT; self->threads = RB_ROOT;
INIT_LIST_HEAD(&self->dead_threads); INIT_LIST_HEAD(&self->dead_threads);
...@@ -208,6 +149,7 @@ struct perf_session *perf_session__new(const char *filename, int mode, ...@@ -208,6 +149,7 @@ struct perf_session *perf_session__new(const char *filename, int mode,
if (mode == O_RDONLY) { if (mode == O_RDONLY) {
if (perf_session__open(self, force) < 0) if (perf_session__open(self, force) < 0)
goto out_delete; goto out_delete;
perf_session__update_sample_type(self);
} else if (mode == O_WRONLY) { } else if (mode == O_WRONLY) {
/* /*
* In O_RDONLY mode this will be performed when reading the * 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,8 +159,6 @@ struct perf_session *perf_session__new(const char *filename, int mode,
goto out_delete; goto out_delete;
} }
perf_session__update_sample_type(self);
if (ops && ops->ordering_requires_timestamps && if (ops && ops->ordering_requires_timestamps &&
ops->ordered_samples && !self->sample_id_all) { ops->ordered_samples && !self->sample_id_all) {
dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); 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,9 +167,6 @@ struct perf_session *perf_session__new(const char *filename, int mode,
out: out:
return self; return self;
out_free:
free(self);
return NULL;
out_delete: out_delete:
perf_session__delete(self); perf_session__delete(self);
return NULL; return NULL;
...@@ -260,7 +197,6 @@ static void perf_session__delete_threads(struct perf_session *self) ...@@ -260,7 +197,6 @@ static void perf_session__delete_threads(struct perf_session *self)
void perf_session__delete(struct perf_session *self) void perf_session__delete(struct perf_session *self)
{ {
perf_header__exit(&self->header);
perf_session__destroy_kernel_maps(self); perf_session__destroy_kernel_maps(self);
perf_session__delete_dead_threads(self); perf_session__delete_dead_threads(self);
perf_session__delete_threads(self); perf_session__delete_threads(self);
......
...@@ -112,8 +112,6 @@ void mem_bswap_64(void *src, int byte_size); ...@@ -112,8 +112,6 @@ void mem_bswap_64(void *src, int byte_size);
int perf_session__create_kernel_maps(struct perf_session *self); int perf_session__create_kernel_maps(struct perf_session *self);
void perf_session__update_sample_type(struct perf_session *self); void perf_session__update_sample_type(struct perf_session *self);
void perf_session__set_sample_id_all(struct perf_session *session, bool value);
void perf_session__set_sample_type(struct perf_session *session, u64 type);
void perf_session__remove_thread(struct perf_session *self, struct thread *th); void perf_session__remove_thread(struct perf_session *self, struct thread *th);
static inline static inline
......
...@@ -61,6 +61,12 @@ static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se) ...@@ -61,6 +61,12 @@ static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
rb_insert_color(&se->rb_node, tree); rb_insert_color(&se->rb_node, tree);
} }
#define SNPRINTF(buf, size, fmt, args...) \
({ \
size_t r = snprintf(buf, size, fmt, ## args); \
r > size ? size : r; \
})
size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
{ {
struct perf_evsel *counter; struct perf_evsel *counter;
...@@ -70,7 +76,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) ...@@ -70,7 +76,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
size_t ret = 0; size_t ret = 0;
if (!perf_guest) { if (!perf_guest) {
ret = snprintf(bf, size, ret = SNPRINTF(bf, size,
" PerfTop:%8.0f irqs/sec kernel:%4.1f%%" " PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
" exact: %4.1f%% [", samples_per_sec, " exact: %4.1f%% [", samples_per_sec,
100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) / 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
...@@ -81,7 +87,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) ...@@ -81,7 +87,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
float guest_kernel_samples_per_sec = top->guest_kernel_samples / top->delay_secs; float guest_kernel_samples_per_sec = top->guest_kernel_samples / top->delay_secs;
float guest_us_samples_per_sec = top->guest_us_samples / top->delay_secs; float guest_us_samples_per_sec = top->guest_us_samples / top->delay_secs;
ret = snprintf(bf, size, ret = SNPRINTF(bf, size,
" PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%" " PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%"
" guest kernel:%4.1f%% guest us:%4.1f%%" " guest kernel:%4.1f%% guest us:%4.1f%%"
" exact: %4.1f%% [", samples_per_sec, " exact: %4.1f%% [", samples_per_sec,
...@@ -101,38 +107,52 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) ...@@ -101,38 +107,52 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
if (top->evlist->nr_entries == 1 || !top->display_weighted) { if (top->evlist->nr_entries == 1 || !top->display_weighted) {
struct perf_evsel *first; struct perf_evsel *first;
first = list_entry(top->evlist->entries.next, struct perf_evsel, node); first = list_entry(top->evlist->entries.next, struct perf_evsel, node);
ret += snprintf(bf + ret, size - ret, "%" PRIu64 "%s ", ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ",
(uint64_t)first->attr.sample_period, (uint64_t)first->attr.sample_period,
top->freq ? "Hz" : ""); top->freq ? "Hz" : "");
} }
if (!top->display_weighted) { if (!top->display_weighted) {
ret += snprintf(bf + ret, size - ret, "%s", ret += SNPRINTF(bf + ret, size - ret, "%s",
event_name(top->sym_evsel)); event_name(top->sym_evsel));
} else list_for_each_entry(counter, &top->evlist->entries, node) { } else {
ret += snprintf(bf + ret, size - ret, "%s%s", /*
counter->idx ? "/" : "", event_name(counter)); * Don't let events eat all the space. Leaving 30 bytes
* for the rest should be enough.
*/
size_t last_pos = size - 30;
list_for_each_entry(counter, &top->evlist->entries, node) {
ret += SNPRINTF(bf + ret, size - ret, "%s%s",
counter->idx ? "/" : "",
event_name(counter));
if (ret > last_pos) {
sprintf(bf + last_pos - 3, "..");
ret = last_pos - 1;
break;
}
}
} }
ret += snprintf(bf + ret, size - ret, "], "); ret += SNPRINTF(bf + ret, size - ret, "], ");
if (top->target_pid != -1) if (top->target_pid != -1)
ret += snprintf(bf + ret, size - ret, " (target_pid: %d", ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %d",
top->target_pid); top->target_pid);
else if (top->target_tid != -1) else if (top->target_tid != -1)
ret += snprintf(bf + ret, size - ret, " (target_tid: %d", ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %d",
top->target_tid); top->target_tid);
else else
ret += snprintf(bf + ret, size - ret, " (all"); ret += SNPRINTF(bf + ret, size - ret, " (all");
if (top->cpu_list) if (top->cpu_list)
ret += snprintf(bf + ret, size - ret, ", CPU%s: %s)", ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
top->evlist->cpus->nr > 1 ? "s" : "", top->cpu_list); top->evlist->cpus->nr > 1 ? "s" : "", top->cpu_list);
else { else {
if (top->target_tid != -1) if (top->target_tid != -1)
ret += snprintf(bf + ret, size - ret, ")"); ret += SNPRINTF(bf + ret, size - ret, ")");
else else
ret += snprintf(bf + ret, size - ret, ", %d CPU%s)", ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
top->evlist->cpus->nr, top->evlist->cpus->nr,
top->evlist->cpus->nr > 1 ? "s" : ""); top->evlist->cpus->nr > 1 ? "s" : "");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment