Skip to contentSkip to conversation

perf tools fixes

GIT PULL In review Frederic Weisbecker 2 participants 9 messages May 23, 2011 perf events

lore
Share & tools
lore
#0 lore raw

Ingo,

Please pull the perf/urgent branch that can be found at:

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
perf/urgent

Thanks,
Frederic
---

Frederic Weisbecker (2):
perf tools: Fix sample size bit operations perf tools: Fix ommitted mmap data update on remap

 tools/perf/util/event.c   |    2 +-
 tools/perf/util/session.c |   39 ++++++++++++++++++++++++++-------------
 2 files changed, 27 insertions(+), 14 deletions(-)
#1 lore raw

PATCH 1/2 perf tools: Fix sample size bit operations

What we want is to count the number of bits in the mask, not some other random operation written in the middle of the night.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/event.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/perf/util/event.c +1 -1
index 17c1c3c..d3fa7e4 100644
@@ -42,7 +42,7 @@ int perf_sample_size(u64 sample_type)
 	int i;
 
 	for (i = 0; i < 64; i++) {
-		if ((mask << i) & 1)
+		if (mask & (1 << i))
 			size++;
 	}
 
Signature · 1 line
1.7.3.2
#5 lore raw

PATCH 2/2 perf tools: Fix ommitted mmap data update on remap

In reply to Frederic Weisbecker

Commit eac9eacee16
"perf tools: Check we are able to read the event size on mmap" brought a check to ensure we can read the size of the event before dereferencing it, and do a remap otherwise to move the buffer forward.

However that remap was ommitting all the necessary work to update the new page offset, head, and to unmap previous pages, etc...

To fix this, gather all the code that fetches the event in a seperate helper which does all the necessary checks about the header/event size and tells us anytime a remap is needed.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/session.c |   39 ++++++++++++++++++++++++++-------------
 1 files changed, 26 insertions(+), 13 deletions(-)
tools/perf/util/session.c +26 -13
index 948327d..64500fc 100644
@@ -960,6 +960,30 @@ out_err:
 	return err;
 }
 
+static union perf_event *
+fetch_mmaped_event(struct perf_session *session,
+		   u64 head, size_t mmap_size, char *buf)
+{
+	union perf_event *event;
+
+	/*
+	 * Ensure we have enough space remaining to read
+	 * the size of the event in the headers.
+	 */
+	if (head + sizeof(event->header) > mmap_size)
+		return NULL;
+
+	event = (union perf_event *)(buf + head);
+
+	if (session->header.needs_swap)
+		perf_event_header__bswap(&event->header);
+
+	if (head + event->header.size > mmap_size)
+		return NULL;
+
+	return event;
+}
+
 int __perf_session__process_events(struct perf_session *session,
 				   u64 data_offset, u64 data_size,
 				   u64 file_size, struct perf_event_ops *ops)
@@ -1014,19 +1038,8 @@ remap:
 	file_pos = file_offset + head;
 
 more:
-	/*
-	 * Ensure we have enough space remaining to read
-	 * the size of the event in the headers.
-	 */
-	if (head + sizeof(event->header) > mmap_size)
-		goto remap;
-
-	event = (union perf_event *)(buf + head);
-
-	if (session->header.needs_swap)
-		perf_event_header__bswap(&event->header);
-
-	if (head + event->header.size > mmap_size) {
+	event = fetch_mmaped_event(session, head, mmap_size, buf);
+	if (!event) {
 		if (mmaps[map_idx]) {
 			munmap(mmaps[map_idx], mmap_size);
 			mmaps[map_idx] = NULL;
Signature · 1 line
1.7.3.2
#7 lore raw
In reply to Frederic Weisbecker
Quoted 11 lines · Frederic Weisbecker

Ingo,

Please pull the perf/urgent branch that can be found at:

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
perf/urgent

Thanks,
Frederic
---

Quoted 8 lines

Frederic Weisbecker (2):
perf tools: Fix sample size bit operations perf tools: Fix ommitted mmap data update on remap

 tools/perf/util/event.c   |    2 +-
 tools/perf/util/session.c |   39 ++++++++++++++++++++++++++-------------
 2 files changed, 27 insertions(+), 14 deletions(-)

Pulled, thanks a lot Frederic!

Ingo

#8 lore raw
Quoted 14 lines · Ingo Molnar
Quoted 11 lines · Frederic Weisbecker

Ingo,

Please pull the perf/urgent branch that can be found at:

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
perf/urgent

Thanks,
Frederic
---

Quoted 8 lines
Quoted 8 lines

Frederic Weisbecker (2):
perf tools: Fix sample size bit operations perf tools: Fix ommitted mmap data update on remap

 tools/perf/util/event.c   |    2 +-
 tools/perf/util/session.c |   39 ++++++++++++++++++++++++++-------------
 2 files changed, 27 insertions(+), 14 deletions(-)

Hm, you broke perf top:

PerfTop: 0 irqs/sec kernel:-nan% exact: -nan% [1000Hz cycles], (all, 16 CPUs)
-------------------------------------------------------------------------------------------------------

samples pcnt DSO
_______ _____

Can't parse sample, err = -14
Can't parse sample, err = -14
Can't parse sample, err = -14
Can't parse sample, err = -14
Can't parse sample, err = -14
Can't parse sample, err = -14

and perf record+report as well:

aldebaran:~/linux/linux> perf record -a sleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.119 MB perf.data (~5201 samples) ] aldebaran:~/linux/linux> perf report --stdio
The perf.data file has no samples!

so i'm unpulling these bits.

Thanks,

Ingo

#2 lore raw

Re: [PATCH 1/2] perf tools: Fix sample size bit operations

In reply to Frederic Weisbecker
Quoted 18 lines · Frederic Weisbecker

What we want is to count the number of bits in the mask, not some other random operation written in the middle of the night.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/event.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/perf/util/event.c +0 -0
index 17c1c3c..d3fa7e4 100644

tools/perf/util/event.c int perf_sample_size(u64 sample_type) · line 47

Quoted 7 lines
tools/perf/util/event.c +1 -1
@@ -42,7 +42,7 @@ int perf_sample_size(u64 sample_type)
 	int i;
 
 	for (i = 0; i < 64; i++) {
-		if ((mask << i) & 1)
+		if (mask & (1 << i))
 			size++;
 	}

I fixed this to be 1UL and applied your fixes out of email - perf top and perf report works fine now, so this was a 64-bitness bug (you probably used a 32-bit system for testing?).

Btw., shouldnt this use hweight() or such?

Thanks,

Ingo

#6 lore raw

[tip:perf/urgent] perf tools: Fix ommitted mmap data update on remap

In reply to Frederic Weisbecker
Commit-ID:  998bedc8c56c6869de457c845cbd328592e5e82e
Gitweb:     http://git.kernel.org/tip/998bedc8c56c6869de457c845cbd328592e5e82e
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Mon, 23 May 2011 13:06:28 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 23 May 2011 13:22:57 +0200

perf tools: Fix ommitted mmap data update on remap

Commit eac9eacee16 "perf tools: Check we are able to read the event size on mmap" brought a check to ensure we can read the size of the event before dereferencing it, and do a remap otherwise to move the buffer forward.

However that remap was ommitting all the necessary work to update the new page offset, head, and to unmap previous pages, etc...

To fix this, gather all the code that fetches the event in a seperate helper which does all the necessary checks about the header/event size and tells us anytime a remap is needed.

---
 tools/perf/util/session.c |   39 ++++++++++++++++++++++++++-------------
 1 files changed, 26 insertions(+), 13 deletions(-)
tools/perf/util/session.c +26 -13
index 948327d..64500fc 100644
@@ -960,6 +960,30 @@ out_err:
 	return err;
 }
 
+static union perf_event *
+fetch_mmaped_event(struct perf_session *session,
+		   u64 head, size_t mmap_size, char *buf)
+{
+	union perf_event *event;
+
+	/*
+	 * Ensure we have enough space remaining to read
+	 * the size of the event in the headers.
+	 */
+	if (head + sizeof(event->header) > mmap_size)
+		return NULL;
+
+	event = (union perf_event *)(buf + head);
+
+	if (session->header.needs_swap)
+		perf_event_header__bswap(&event->header);
+
+	if (head + event->header.size > mmap_size)
+		return NULL;
+
+	return event;
+}
+
 int __perf_session__process_events(struct perf_session *session,
 				   u64 data_offset, u64 data_size,
 				   u64 file_size, struct perf_event_ops *ops)
@@ -1014,19 +1038,8 @@ remap:
 	file_pos = file_offset + head;
 
 more:
-	/*
-	 * Ensure we have enough space remaining to read
-	 * the size of the event in the headers.
-	 */
-	if (head + sizeof(event->header) > mmap_size)
-		goto remap;
-
-	event = (union perf_event *)(buf + head);
-
-	if (session->header.needs_swap)
-		perf_event_header__bswap(&event->header);
-
-	if (head + event->header.size > mmap_size) {
+	event = fetch_mmaped_event(session, head, mmap_size, buf);
+	if (!event) {
 		if (mmaps[map_idx]) {
 			munmap(mmaps[map_idx], mmap_size);
 			mmaps[map_idx] = NULL;
#4 lore raw

[tip:perf/urgent] perf tools: Fix sample size bit operations

In reply to Frederic Weisbecker
Commit-ID:  3cb6d1540880e767d911b79eb49578de2190f428
Gitweb:     http://git.kernel.org/tip/3cb6d1540880e767d911b79eb49578de2190f428
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Mon, 23 May 2011 13:06:27 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 23 May 2011 13:26:36 +0200

perf tools: Fix sample size bit operations

What we want is to count the number of bits in the mask, not some other random operation written in the middle of the night.

Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Stephane Eranian <[email protected]>
[ Fixed perf_event__names[] alignment which was nearby and hurting my eyes ... ]
---
 tools/perf/util/event.c |   32 ++++++++++++++++----------------
 1 files changed, 16 insertions(+), 16 deletions(-)
tools/perf/util/event.c +16 -16
index 17c1c3c..252b72a 100644
@@ -9,21 +9,21 @@
 #include "thread_map.h"
 
 static const char *perf_event__names[] = {
-	[0]			 = "TOTAL",
-	[PERF_RECORD_MMAP]	 = "MMAP",
-	[PERF_RECORD_LOST]	 = "LOST",
-	[PERF_RECORD_COMM]	 = "COMM",
-	[PERF_RECORD_EXIT]	 = "EXIT",
-	[PERF_RECORD_THROTTLE]	 = "THROTTLE",
-	[PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
-	[PERF_RECORD_FORK]	 = "FORK",
-	[PERF_RECORD_READ]	 = "READ",
-	[PERF_RECORD_SAMPLE]	 = "SAMPLE",
-	[PERF_RECORD_HEADER_ATTR]	 = "ATTR",
-	[PERF_RECORD_HEADER_EVENT_TYPE]	 = "EVENT_TYPE",
-	[PERF_RECORD_HEADER_TRACING_DATA]	 = "TRACING_DATA",
-	[PERF_RECORD_HEADER_BUILD_ID]	 = "BUILD_ID",
-	[PERF_RECORD_FINISHED_ROUND]	 = "FINISHED_ROUND",
+	[0]					= "TOTAL",
+	[PERF_RECORD_MMAP]			= "MMAP",
+	[PERF_RECORD_LOST]			= "LOST",
+	[PERF_RECORD_COMM]			= "COMM",
+	[PERF_RECORD_EXIT]			= "EXIT",
+	[PERF_RECORD_THROTTLE]			= "THROTTLE",
+	[PERF_RECORD_UNTHROTTLE]		= "UNTHROTTLE",
+	[PERF_RECORD_FORK]			= "FORK",
+	[PERF_RECORD_READ]			= "READ",
+	[PERF_RECORD_SAMPLE]			= "SAMPLE",
+	[PERF_RECORD_HEADER_ATTR]		= "ATTR",
+	[PERF_RECORD_HEADER_EVENT_TYPE]		= "EVENT_TYPE",
+	[PERF_RECORD_HEADER_TRACING_DATA]	= "TRACING_DATA",
+	[PERF_RECORD_HEADER_BUILD_ID]		= "BUILD_ID",
+	[PERF_RECORD_FINISHED_ROUND]		= "FINISHED_ROUND",
 };
 
 const char *perf_event__name(unsigned int id)
@@ -42,7 +42,7 @@ int perf_sample_size(u64 sample_type)
 	int i;
 
 	for (i = 0; i < 64; i++) {
-		if ((mask << i) & 1)
+		if (mask & (1UL << i))
 			size++;
 	}
 
#3 lore raw

Re: [PATCH 1/2] perf tools: Fix sample size bit operations

In reply to Ingo Molnar
Quoted 23 lines · Ingo Molnar
Quoted 20 lines · Frederic Weisbecker

What we want is to count the number of bits in the mask, not some other random operation written in the middle of the night.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/event.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/perf/util/event.c +0 -0
index 17c1c3c..d3fa7e4 100644
@@ -42,7 +42,7 @@ int perf_sample_size(u64 sample_type)
 	int i;
 
Quoted 9 lines

tools/perf/util/event.c int perf_sample_size(u64 sample_type) · line 47

Quoted 5 lines
tools/perf/util/event.c +1 -1
 	for (i = 0; i < 64; i++) {
-		if ((mask << i) & 1)
+		if (mask & (1 << i))
 			size++;
 	}

I fixed this to be 1UL and applied your fixes out of email - perf top and perf report works fine now, so this was a 64-bitness bug (you probably used a 32-bit system for testing?).

Nope I was using a 64 machine for testing, but I only tested sched_switch events with callchains on perf report and perf script.

Perhaps that bug triggered only on perf top or with default cpu-cycle events.

Quoted 2 lines

Btw., shouldnt this use hweight() or such?

A real proper way would better have that yeah :)