2009-12-29 00:49:39

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 1/5] perf header: do_read shouldn't die

From: Arnaldo Carvalho de Melo <[email protected]>

Propagate the errors instead, its callers already propagate other
errors.

Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/header.c | 35 +++++++++++++++++++++--------------
1 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index df237c3..6b3cb94 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -432,19 +432,19 @@ int perf_header__write(struct perf_header *self, int fd, bool at_exit)
return 0;
}

-static void do_read(int fd, void *buf, size_t size)
+static int do_read(int fd, void *buf, size_t size)
{
while (size) {
int ret = read(fd, buf, size);

- if (ret < 0)
- die("failed to read");
- if (ret == 0)
- die("failed to read: missing data");
+ if (ret <= 0)
+ return -1;

size -= ret;
buf += ret;
}
+
+ return 0;
}

int perf_header__process_sections(struct perf_header *self, int fd,
@@ -455,7 +455,7 @@ int perf_header__process_sections(struct perf_header *self, int fd,
int nr_sections;
int sec_size;
int idx = 0;
- int err = 0, feat = 1;
+ int err = -1, feat = 1;

nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
if (!nr_sections)
@@ -469,8 +469,10 @@ int perf_header__process_sections(struct perf_header *self, int fd,

lseek(fd, self->data_offset + self->data_size, SEEK_SET);

- do_read(fd, feat_sec, sec_size);
+ if (do_read(fd, feat_sec, sec_size))
+ goto out_free;

+ err = 0;
while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
if (perf_header__has_feat(self, feat)) {
struct perf_file_section *sec = &feat_sec[idx++];
@@ -481,18 +483,18 @@ int perf_header__process_sections(struct perf_header *self, int fd,
}
++feat;
}
-
+out_free:
free(feat_sec);
return err;
-};
+}

int perf_file_header__read(struct perf_file_header *self,
struct perf_header *ph, int fd)
{
lseek(fd, 0, SEEK_SET);
- do_read(fd, self, sizeof(*self));

- if (self->magic != PERF_MAGIC ||
+ if (do_read(fd, self, sizeof(*self)) ||
+ self->magic != PERF_MAGIC ||
self->attr_size != sizeof(struct perf_file_attr))
return -1;

@@ -558,7 +560,8 @@ int perf_header__read(struct perf_header *self, int fd)
struct perf_header_attr *attr;
off_t tmp;

- do_read(fd, &f_attr, sizeof(f_attr));
+ if (do_read(fd, &f_attr, sizeof(f_attr)))
+ goto out_errno;
tmp = lseek(fd, 0, SEEK_CUR);

attr = perf_header_attr__new(&f_attr.attr);
@@ -569,7 +572,8 @@ int perf_header__read(struct perf_header *self, int fd)
lseek(fd, f_attr.ids.offset, SEEK_SET);

for (j = 0; j < nr_ids; j++) {
- do_read(fd, &f_id, sizeof(f_id));
+ if (do_read(fd, &f_id, sizeof(f_id)))
+ goto out_errno;

if (perf_header_attr__add_id(attr, f_id) < 0) {
perf_header_attr__delete(attr);
@@ -589,7 +593,8 @@ int perf_header__read(struct perf_header *self, int fd)
events = malloc(f_header.event_types.size);
if (events == NULL)
return -ENOMEM;
- do_read(fd, events, f_header.event_types.size);
+ if (do_read(fd, events, f_header.event_types.size))
+ goto out_errno;
event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
}

@@ -599,6 +604,8 @@ int perf_header__read(struct perf_header *self, int fd)

self->frozen = 1;
return 0;
+out_errno:
+ return -errno;
}

u64 perf_header__sample_type(struct perf_header *header)
--
1.5.5.1


2009-12-29 00:48:48

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 2/5] perf header: perf_header__push_event shouldn't die

From: Arnaldo Carvalho de Melo <[email protected]>

Just propagate eventual errors.

Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/header.c | 16 ++++++++++------
tools/perf/util/header.h | 2 +-
tools/perf/util/parse-events.c | 18 +++++++++++-------
3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 6b3cb94..709e325 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -105,24 +105,28 @@ struct perf_trace_event_type {
static int event_count;
static struct perf_trace_event_type *events;

-void perf_header__push_event(u64 id, const char *name)
+int perf_header__push_event(u64 id, const char *name)
{
if (strlen(name) > MAX_EVENT_NAME)
pr_warning("Event %s will be truncated\n", name);

if (!events) {
events = malloc(sizeof(struct perf_trace_event_type));
- if (!events)
- die("nomem");
+ if (events == NULL)
+ return -ENOMEM;
} else {
- events = realloc(events, (event_count + 1) * sizeof(struct perf_trace_event_type));
- if (!events)
- die("nomem");
+ struct perf_trace_event_type *nevents;
+
+ nevents = realloc(events, (event_count + 1) * sizeof(*events));
+ if (nevents == NULL)
+ return -ENOMEM;
+ events = nevents;
}
memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
events[event_count].event_id = id;
strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
event_count++;
+ return 0;
}

char *perf_header__find_event(u64 id)
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index d118d05..2b69aab 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -64,7 +64,7 @@ int perf_header__write(struct perf_header *self, int fd, bool at_exit);
int perf_header__add_attr(struct perf_header *self,
struct perf_header_attr *attr);

-void 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);

struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr);
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index e5bc0fb..625ef5a 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -753,11 +753,11 @@ modifier:
return ret;
}

-static void store_event_type(const char *orgname)
+static int store_event_type(const char *orgname)
{
char filename[PATH_MAX], *c;
FILE *file;
- int id;
+ int id, n;

sprintf(filename, "%s/", debugfs_path);
strncat(filename, orgname, strlen(orgname));
@@ -769,11 +769,14 @@ static void store_event_type(const char *orgname)

file = fopen(filename, "r");
if (!file)
- return;
- if (fscanf(file, "%i", &id) < 1)
- die("cannot store event ID");
+ return 0;
+ n = fscanf(file, "%i", &id);
fclose(file);
- perf_header__push_event(id, orgname);
+ if (n < 1) {
+ pr_err("cannot store event ID\n");
+ return -EINVAL;
+ }
+ return perf_header__push_event(id, orgname);
}

int parse_events(const struct option *opt __used, const char *str, int unset __used)
@@ -782,7 +785,8 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u
enum event_result ret;

if (strchr(str, ':'))
- store_event_type(str);
+ if (store_event_type(str) < 0)
+ return -1;

for (;;) {
if (nr_counters == MAX_COUNTERS)
--
1.5.5.1

2009-12-29 00:49:24

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 3/5] perf report: Add --hide-unresolved/-U command line option

From: Arnaldo Carvalho de Melo <[email protected]>

Useful to match the 'overhead' column in 'perf report' with the 'baseline' one
in 'perf diff'.

Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-report.c | 5 ++++-
1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 508934b..4292d7a 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -34,6 +34,7 @@
static char const *input_name = "perf.data";

static int force;
+static bool hide_unresolved;

static int show_threads;
static struct perf_read_values show_threads_values;
@@ -121,7 +122,7 @@ static int process_sample_event(event_t *event, struct perf_session *session)
return -1;
}

- if (al.filtered)
+ if (al.filtered || (hide_unresolved && al.sym == NULL))
return 0;

if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
@@ -342,6 +343,8 @@ static const struct option options[] = {
OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
"separator for columns, no spaces will be added between "
"columns '.' is reserved."),
+ OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
+ "Only display entries resolved to a symbol"),
OPT_END()
};

--
1.5.5.1

2009-12-29 00:48:55

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 4/5] perf diff: Don't add the period for unresolved symbols

From: Arnaldo Carvalho de Melo <[email protected]>

Since we don't add histograms buckets for them, this way the sum of baselines
should be 100%.

Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-diff.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index 1cbecaf..876a4b9 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -51,12 +51,12 @@ static int diff__process_sample_event(event_t *event, struct perf_session *sessi
return -1;
}

- if (al.filtered)
+ if (al.filtered || al.sym == NULL)
return 0;

event__parse_sample(event, session->sample_type, &data);

- if (al.sym && perf_session__add_hist_entry(session, &al, data.period)) {
+ if (perf_session__add_hist_entry(session, &al, data.period)) {
pr_warning("problem incrementing symbol count, skipping event\n");
return -1;
}
--
1.5.5.1

2009-12-29 00:49:05

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 5/5] perf diff: Fix support for all --sort combinations

From: Arnaldo Carvalho de Melo <[email protected]>

When we finish creating the hist_entries we _already_ have them sorted "by
name", in fact by what is in --sort, that is exactly how we can find the pairs
in perf_session__match_hists as 'comm', 'dso' & 'symbol' all are strings we
need to find the matches in the baseline session.

So only do the sort by hits followed by a resort by --sort if we need to find
the position for shwowing the --displacement of hist entries.

Now all these modes work correctly:

Example is a simple 'perf record -f find / > /dev/null' ran twice then followed
by the following commands:

$ perf diff -f --sort comm
# Baseline Delta Command
# ........ .......... .......
#
0.00% +100.00% find
$ perf diff -f --sort dso
# Baseline Delta Shared Object
# ........ .......... ..................
#
59.97% -0.44% [kernel]
21.17% +0.28% libc-2.5.so
18.49% +0.16% [ext3]
0.37% find
$ perf diff -f --sort symbol | head -8
# Baseline Delta Symbol
# ........ .......... ......
#
6.21% +0.36% [k] ext3fs_dirhash
3.43% +0.41% [.] __GI_strlen
3.53% +0.16% [k] __kmalloc
3.17% +0.49% [k] system_call
3.06% +0.37% [k] ext3_htree_store_dirent
$ perf diff -f --sort dso,symbol | head -8
# Baseline Delta Shared Object Symbol
# ........ .......... .................. ......
#
6.21% +0.36% [ext3] [k] ext3fs_dirhash
3.43% +0.41% libc-2.5.so [.] __GI_strlen
3.53% +0.16% [kernel] [k] __kmalloc
3.17% +0.49% [kernel] [k] system_call
3.06% +0.37% [ext3] [k] ext3_htree_store_dirent
$

And we don't have to do two expensive resorts in the common, non --displacement
case.

Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-diff.c | 52 ++++++++++++++++++--------------------------
1 files changed, 21 insertions(+), 31 deletions(-)

diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index 876a4b9..924bfb7 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -82,29 +82,19 @@ static void perf_session__insert_hist_entry_by_name(struct rb_root *root,
struct hist_entry *iter;

while (*p != NULL) {
- int cmp;
parent = *p;
iter = rb_entry(parent, struct hist_entry, rb_node);
-
- cmp = strcmp(he->map->dso->name, iter->map->dso->name);
- if (cmp > 0)
+ if (hist_entry__cmp(he, iter) < 0)
p = &(*p)->rb_left;
- else if (cmp < 0)
+ else
p = &(*p)->rb_right;
- else {
- cmp = strcmp(he->sym->name, iter->sym->name);
- if (cmp > 0)
- p = &(*p)->rb_left;
- else
- p = &(*p)->rb_right;
- }
}

rb_link_node(&he->rb_node, parent, p);
rb_insert_color(&he->rb_node, root);
}

-static void perf_session__resort_by_name(struct perf_session *self)
+static void perf_session__resort_hist_entries(struct perf_session *self)
{
unsigned long position = 1;
struct rb_root tmp = RB_ROOT;
@@ -122,29 +112,28 @@ static void perf_session__resort_by_name(struct perf_session *self)
self->hists = tmp;
}

+static void perf_session__set_hist_entries_positions(struct perf_session *self)
+{
+ perf_session__output_resort(self, self->events_stats.total);
+ perf_session__resort_hist_entries(self);
+}
+
static struct hist_entry *
-perf_session__find_hist_entry_by_name(struct perf_session *self,
- struct hist_entry *he)
+perf_session__find_hist_entry(struct perf_session *self,
+ struct hist_entry *he)
{
struct rb_node *n = self->hists.rb_node;

while (n) {
struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
- int cmp = strcmp(he->map->dso->name, iter->map->dso->name);
+ int64_t cmp = hist_entry__cmp(he, iter);

- if (cmp > 0)
+ if (cmp < 0)
n = n->rb_left;
- else if (cmp < 0)
+ else if (cmp > 0)
n = n->rb_right;
- else {
- cmp = strcmp(he->sym->name, iter->sym->name);
- if (cmp > 0)
- n = n->rb_left;
- else if (cmp < 0)
- n = n->rb_right;
- else
- return iter;
- }
+ else
+ return iter;
}

return NULL;
@@ -155,11 +144,9 @@ static void perf_session__match_hists(struct perf_session *old_session,
{
struct rb_node *nd;

- perf_session__resort_by_name(old_session);
-
for (nd = rb_first(&new_session->hists); nd; nd = rb_next(nd)) {
struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
- pos->pair = perf_session__find_hist_entry_by_name(old_session, pos);
+ pos->pair = perf_session__find_hist_entry(old_session, pos);
}
}

@@ -177,9 +164,12 @@ static int __cmd_diff(void)
ret = perf_session__process_events(session[i], &event_ops);
if (ret)
goto out_delete;
- perf_session__output_resort(session[i], session[i]->events_stats.total);
}

+ perf_session__output_resort(session[1], session[1]->events_stats.total);
+ if (show_displacement)
+ perf_session__set_hist_entries_positions(session[0]);
+
perf_session__match_hists(session[0], session[1]);
perf_session__fprintf_hists(session[1], session[0],
show_displacement, stdout);
--
1.5.5.1

2009-12-29 00:55:07

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH 5/5] perf diff: Fix support for all --sort combinations

Em Mon, Dec 28, 2009 at 10:48:36PM -0200, Arnaldo Carvalho de Melo escreveu:
> From: Arnaldo Carvalho de Melo <[email protected]>
>
> When we finish creating the hist_entries we _already_ have them sorted "by
> name", in fact by what is in --sort, that is exactly how we can find the pairs
> in perf_session__match_hists as 'comm', 'dso' & 'symbol' all are strings we
> need to find the matches in the baseline session.
>
> So only do the sort by hits followed by a resort by --sort if we need to find
> the position for shwowing the --displacement of hist entries.
>
> Now all these modes work correctly:
>
> Example is a simple 'perf record -f find / > /dev/null' ran twice then followed
> by the following commands:
>
> $ perf diff -f --sort comm
> # Baseline Delta Command
> # ........ .......... .......
> #
> 0.00% +100.00% find

Dammit, of course this one needs some more love... will work on it
tomorrow, the others, humm, _should_ be right.

Its late, I should be having some rest already, I guess, or whatever
:-)

- Arnaldo

2009-12-29 01:38:33

by Valdis Klētnieks

[permalink] [raw]
Subject: Re: [PATCH 3/5] perf report: Add --hide-unresolved/-U command line option

On Mon, 28 Dec 2009 22:48:34 -0200, Arnaldo Carvalho de Melo said:
> Useful to match the 'overhead' column in 'perf report' with the 'baseline' one
> in 'perf diff'.

Looks like a good one for tools/perf/Documentation/examples.txt ?


Attachments:
(No filename) (227.00 B)