Skip to contentSkip to conversation

perf_counter tools: remove glib dependency and fix bugs in kerneltop.c

PATCH In review Paul Mackerras 4 participants 12 messages Mar 24, 2009 7 messages not in the archive Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf_counter tools: remove glib dependency and fix bugs in kerneltop.c

The glib dependency in kerneltop.c is only for a little bit of list manipulation, and I find it inconvenient. This adds a 'next' field to struct source_line, which lets us link them together into a list. The code to do the linking ourselves turns out to be no longer or more difficult than using glib.

This also fixes a few other problems:

- We need to #include <limits.h> to get PATH_MAX on powerpc.

- We need to #include <linux/types.h> rather than have our own definitions of __u64 and __s64; on powerpc the installed headers define them to be unsigned long and long respectively, and if we have our own, different definition here that causes a compile error.

- This takes out the x86 setting of errno from -ret in sys_perf_counter_open. My experiments on x86 indicate that the glibc syscall() does this for us already.

- We had two CPU migration counters in the default set, which seems unnecessary; I changed one of them to a context switch counter.

- In perfstat mode we were printing CPU cycles and instructions as milliseconds, and the cpu clock and task clock counters as events. This fixes that.

- In perfstat mode we were still printing a blank line after the first counter, which was a holdover from when a task clock counter was automatically included as the first counter. This removes the blank line.

- On a test machine here, parse_symbols() and parse_vmlinux() were taking long enough (almost 0.5 seconds) for the mmap buffer to overflow before we got to the first mmap_read() call, so this moves them before we open all the counters.

- The error message if sys_perf_counter_open fails needs to use errno, not -fd[i][counter].

---
 Documentation/perf_counter/Makefile    |    2 +-
 Documentation/perf_counter/kerneltop.c |  112 +++++++++++++-------------------
 2 files changed, 46 insertions(+), 68 deletions(-)
Documentation/perf_counter/Makefile +1 -1
index 666da95..194b662 100644
@@ -3,7 +3,7 @@ BINS = kerneltop perfstat
 all: $(BINS)
 
 kerneltop: kerneltop.c ../../include/linux/perf_counter.h
-	cc -O6 -Wall -lrt `pkg-config --cflags --libs glib-2.0` -o $@ $<
+	cc -O6 -Wall -lrt -o $@ $<
 
 perfstat: kerneltop
 	ln -sf kerneltop perfstat
Documentation/perf_counter/kerneltop.c +45 -67

287 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Paul Mackerras

The glib dependency in kerneltop.c is only for a little bit of list manipulation, and I find it inconvenient. This adds a 'next' field to struct source_line, which lets us link them together into a list. The code to do the linking ourselves turns out to be no longer or more difficult than using glib.

Awesome!! now I can compile on my test machine as well (yum got stuck, it doesn't want to go fwd nor backwards, and I can't find time to reinstall the thing).

Quoted 4 lines

This also fixes a few other problems:

- We had two CPU migration counters in the default set, which seems unnecessary; I changed one of them to a context switch counter.

Ooh, my bad, copy/paste trouble I imagine.

Quoted 4 lines

- On a test machine here, parse_symbols() and parse_vmlinux() were taking long enough (almost 0.5 seconds) for the mmap buffer to overflow before we got to the first mmap_read() call, so this moves them before we open all the counters.

Ah, thanks for looking into that!

#2 lore raw
In reply to Paul Mackerras
Quoted 39 lines · Paul Mackerras

The glib dependency in kerneltop.c is only for a little bit of list manipulation, and I find it inconvenient. This adds a 'next' field to struct source_line, which lets us link them together into a list. The code to do the linking ourselves turns out to be no longer or more difficult than using glib.

This also fixes a few other problems:

- We need to #include <limits.h> to get PATH_MAX on powerpc.

- We need to #include <linux/types.h> rather than have our own definitions of __u64 and __s64; on powerpc the installed headers define them to be unsigned long and long respectively, and if we have our own, different definition here that causes a compile error.

- This takes out the x86 setting of errno from -ret in sys_perf_counter_open. My experiments on x86 indicate that the glibc syscall() does this for us already.

- We had two CPU migration counters in the default set, which seems unnecessary; I changed one of them to a context switch counter.

- In perfstat mode we were printing CPU cycles and instructions as milliseconds, and the cpu clock and task clock counters as events. This fixes that.

- In perfstat mode we were still printing a blank line after the first counter, which was a holdover from when a task clock counter was automatically included as the first counter. This removes the blank line.

- On a test machine here, parse_symbols() and parse_vmlinux() were taking long enough (almost 0.5 seconds) for the mmap buffer to overflow before we got to the first mmap_read() call, so this moves them before we open all the counters.

- The error message if sys_perf_counter_open fails needs to use errno, not -fd[i][counter].

Quoted 6 lines
---
 Documentation/perf_counter/Makefile    |    2 +-
 Documentation/perf_counter/kerneltop.c |  112 +++++++++++++-------------------
 2 files changed, 46 insertions(+), 68 deletions(-)

Very nice, thanks Paul!

Ingo

#3 lore raw
In reply to Paul Mackerras
Commit-ID:  6357003d6c8bc0056d79768692955da6d151880f
Gitweb:     http://git.kernel.org/tip/6357003d6c8bc0056d79768692955da6d151880f
Author:     Paul Mackerras <[email protected]>
AuthorDate: Tue, 24 Mar 2009 16:52:34 +1100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 24 Mar 2009 10:20:07 +0100

perf_counter tools: remove glib dependency and fix bugs in kerneltop.c

The glib dependency in kerneltop.c is only for a little bit of list manipulation, and I find it inconvenient. This adds a 'next' field to struct source_line, which lets us link them together into a list. The code to do the linking ourselves turns out to be no longer or more difficult than using glib.

This also fixes a few other problems:

- We need to #include <limits.h> to get PATH_MAX on powerpc.

- We need to #include <linux/types.h> rather than have our own definitions of __u64 and __s64; on powerpc the installed headers define them to be unsigned long and long respectively, and if we have our own, different definition here that causes a compile error.

- This takes out the x86 setting of errno from -ret in sys_perf_counter_open. My experiments on x86 indicate that the glibc syscall() does this for us already.

- We had two CPU migration counters in the default set, which seems unnecessary; I changed one of them to a context switch counter.

- In perfstat mode we were printing CPU cycles and instructions as milliseconds, and the cpu clock and task clock counters as events. This fixes that.

- In perfstat mode we were still printing a blank line after the first counter, which was a holdover from when a task clock counter was automatically included as the first counter. This removes the blank line.

- On a test machine here, parse_symbols() and parse_vmlinux() were taking long enough (almost 0.5 seconds) for the mmap buffer to overflow before we got to the first mmap_read() call, so this moves them before we open all the counters.

- The error message if sys_perf_counter_open fails needs to use errno, not -fd[i][counter].

LKML-Reference: <[email protected]>

---
 Documentation/perf_counter/Makefile    |    2 +-
 Documentation/perf_counter/kerneltop.c |  112 +++++++++++++-------------------
 2 files changed, 46 insertions(+), 68 deletions(-)
Documentation/perf_counter/Makefile +1 -1
index 666da95..194b662 100644
@@ -3,7 +3,7 @@ BINS = kerneltop perfstat
 all: $(BINS)
 
 kerneltop: kerneltop.c ../../include/linux/perf_counter.h
-	cc -O6 -Wall -lrt `pkg-config --cflags --libs glib-2.0` -o $@ $<
+	cc -O6 -Wall -lrt -o $@ $<
 
 perfstat: kerneltop
 	ln -sf kerneltop perfstat
Documentation/perf_counter/kerneltop.c +45 -67

287 lines not on this page · raw message

#4 lore raw

btw.,:

| Performance counter stats for 'ls':
|
|        1915281  task clock ticks     (events)
|
|              6  CPU migrations       (events)
|              6  CPU migrations       (events)
|            284  pagefaults           (events)
|       4.333761  CPU cycles           (msecs)
|       1.544153  instructions         (msecs)
|         620030  cache references     (events)
|          17632  cache misses         (events)
|
| Wall-clock time elapsed:     1.868138 msecs

the 'instructions' field is weird - it should not be millisecs but 'events'.

Ingo

#5 lore raw
Quoted 16 lines · Ingo Molnar

btw.,:

| Performance counter stats for 'ls':
|
|        1915281  task clock ticks     (events)
|
|              6  CPU migrations       (events)
|              6  CPU migrations       (events)
|            284  pagefaults           (events)
|       4.333761  CPU cycles           (msecs)
|       1.544153  instructions         (msecs)
|         620030  cache references     (events)
|          17632  cache misses         (events)
|
| Wall-clock time elapsed:     1.868138 msecs
Quoted 2 lines

the 'instructions' field is weird - it should not be millisecs but 'events'.

Is that with my patch applied? That was one of the things I fixed, or at least intended to fix :).

Paul.

#6 lore raw
Quoted 18 lines · Paul Mackerras
Quoted 16 lines · Ingo Molnar

btw.,:

| Performance counter stats for 'ls':
|
|        1915281  task clock ticks     (events)
|
|              6  CPU migrations       (events)
|              6  CPU migrations       (events)
|            284  pagefaults           (events)
|       4.333761  CPU cycles           (msecs)
|       1.544153  instructions         (msecs)
|         620030  cache references     (events)
|          17632  cache misses         (events)
|
| Wall-clock time elapsed:     1.868138 msecs
Quoted 5 lines
Quoted 2 lines

the 'instructions' field is weird - it should not be millisecs but 'events'.

Is that with my patch applied? That was one of the things I fixed, or at least intended to fix :).

ah. no. It was indeed with an ancient kernel 6 hours old or so :)

Ingo

#7 lore raw

[tip:perfcounters/core] perf_counter tools: remove glib dependency and fix bugs in kerneltop.c, fix poll()

In reply to Paul Mackerras
Commit-ID:  afc2a3523fc6664f3713b594daa1857f37ad87df
Gitweb:     http://git.kernel.org/tip/afc2a3523fc6664f3713b594daa1857f37ad87df
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Tue, 24 Mar 2009 10:50:24 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 24 Mar 2009 10:56:18 +0100

perf_counter tools: remove glib dependency and fix bugs in kerneltop.c, fix poll()

Quoted 6 lines · Paul Mackerras

I noticed the poll stuff is bogus - we have a 2D array of struct pollfds (MAX_NR_CPUS x MAX_COUNTERS), we fill in a sub-array (with the rest being uninitialized, since the array is on the stack) and then pass the first nr_cpus elements to poll. Not what we really meant, I suspect. :) Not even if we only have one counter, since it's the counter dimension that varies fastest.

This should fix the most obvious poll fubar.. not enough to fix the full problem though..

LKML-Reference: <[email protected]>

---
 Documentation/perf_counter/kerneltop.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
Documentation/perf_counter/kerneltop.c +6 -5
index ea13e4e..7ebde7a 100644
@@ -1157,10 +1157,10 @@ static void mmap_read(struct mmap_data *md)
 
 int main(int argc, char *argv[])
 {
-	struct pollfd event_array[MAX_NR_CPUS][MAX_COUNTERS];
+	struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
 	struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
 	struct perf_counter_hw_event hw_event;
-	int i, counter, group_fd;
+	int i, counter, group_fd, nr_poll = 0;
 	unsigned int cpu;
 	int ret;
 
@@ -1214,8 +1214,9 @@ int main(int argc, char *argv[])
 			if (group && group_fd == -1)
 				group_fd = fd[i][counter];
 
-			event_array[i][counter].fd = fd[i][counter];
-			event_array[i][counter].events = POLLIN;
+			event_array[nr_poll].fd = fd[i][counter];
+			event_array[nr_poll].events = POLLIN;
+			nr_poll++;
 
 			mmap_array[i][counter].counter = counter;
 			mmap_array[i][counter].prev = 0;
@@ -1247,7 +1248,7 @@ int main(int argc, char *argv[])
 		}
 
 		if (hits == events)
-			ret = poll(event_array[0], nr_cpus, 1000);
+			ret = poll(event_array, nr_poll, 1000);
 		hits = events;
 	}
 

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 1237888224.24918.159.camel@twins

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 1237892368.24918.160.camel@twins

#15 lore raw

PATCH 1/1 perf_counter: fix perf_poll()

The below does seem to fix things (along with the previous kerneltop.c patch),.. however now kerneltop is going splat like:

# ./kerneltop -e 1:2 -c 10
KernelTop refresh period: 2 seconds
ret: 1
 left: 0000000080200000
   ip: 000000008038d4ad
right: 00000000ffffffff
kerneltop: kerneltop.c:985: record_ip: Assertion `left <= ip && ip <= right' failed.
Aborted

Which isn't making much sense..

---
Only return a poll event when there's actually been one, poll_wait() doesn't actually wait for the waitq you pass it, it only enqueues you on it.

Only once all FDs have been iterated and non returned a poll-event will it schedule().

Also make it return POLL_HUP when there's not mmap() area to read from.

Further, fix a silly bug in the write code.

---
 include/linux/perf_counter.h |    1 +
 kernel/perf_counter.c        |   14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
include/linux/perf_counter.h +1 -0
index 2b5e66d..48212c1 100644
@@ -246,6 +246,7 @@ struct file;
 struct perf_mmap_data {
 	struct rcu_head			rcu_head;
 	int				nr_pages;
+	atomic_t			wakeup;
 	atomic_t			head;
 	struct perf_counter_mmap_page   *user_page;
 	void 				*data_pages[0];
kernel/perf_counter.c +0 -0
index 0dfe910..affe227 100644
@@ -1161,7 +1161,16 @@ perf_read(struct file *file, char __user *buf,

size_t count, loff_t *ppos)
static unsigned int perf_poll(struct file *file, poll_table *wait) {
struct perf_counter *counter = file->private_data;
- unsigned int events = POLLIN;
+ struct perf_mmap_data *data;
+ unsigned int events;
+
+ rcu_read_lock();
+ data = rcu_dereference(counter->data);
+ if (data)
+ events = atomic_xchg(&data->wakeup, 0);
+ else
+ events = POLL_HUP;
+ rcu_read_unlock();

poll_wait(file, &counter->waitq, wait);

@@ -1425,7 +1434,7 @@ static int perf_output_write(struct perf_counter *counter, int nmi,

do {
offset = head = atomic_read(&data->head);
- head += sizeof(u64);
+ head += size;
} while (atomic_cmpxchg(&data->head, offset, head) != offset);

wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
@@ -1446,6 +1455,7 @@ static int perf_output_write(struct perf_counter *counter, int nmi,
* generate a poll() wakeup for every page boundary crossed */
if (wakeup) {
+ atomic_xchg(&data->wakeup, POLL_IN);
__perf_counter_update_userpage(counter, data); if (nmi) {
counter->wakeup_pending = 1;

#16 lore raw

Re: [PATCH] perf_counter: fix perf_poll()

Quoted 12 lines · Peter Zijlstra

The below does seem to fix things (along with the previous kerneltop.c patch),.. however now kerneltop is going splat like:

# ./kerneltop -e 1:2 -c 10
KernelTop refresh period: 2 seconds
ret: 1
 left: 0000000080200000
   ip: 000000008038d4ad
right: 00000000ffffffff
kerneltop: kerneltop.c:985: record_ip: Assertion `left <= ip && ip <= right' failed.
Aborted
Quoted 1 line

Which isn't making much sense..

Seems to be an artefact of running a 32bit kerneltop on a 64bit kernel.. freshyl compiled 64bit kerneltop works just fine.

#17 lore raw

Re: [PATCH] perf_counter: fix perf_poll()

Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Peter Zijlstra

The below does seem to fix things (along with the previous kerneltop.c patch),.. however now kerneltop is going splat like:

# ./kerneltop -e 1:2 -c 10
KernelTop refresh period: 2 seconds
ret: 1
 left: 0000000080200000
   ip: 000000008038d4ad
right: 00000000ffffffff
kerneltop: kerneltop.c:985: record_ip: Assertion `left <= ip && ip <= right' failed.
Aborted
Quoted 4 lines
Quoted 1 line

Which isn't making much sense..

Seems to be an artefact of running a 32bit kerneltop on a 64bit kernel.. freshyl compiled 64bit kerneltop works just fine.

Looks like you discovered a real bug by accident :)

Ingo

#18 lore raw

[tip:perfcounters/core] perf_counter: fix perf_poll()

In reply to Peter Zijlstra
Commit-ID:  47a6aae483f6b98509a4565ad2d8916de77ee078
Gitweb:     http://git.kernel.org/tip/47a6aae483f6b98509a4565ad2d8916de77ee078
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Tue, 24 Mar 2009 13:18:16 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 24 Mar 2009 13:28:56 +0100

perf_counter: fix perf_poll()

Impact: fix kerneltop 100% CPU usage

Only return a poll event when there's actually been one, poll_wait() doesn't actually wait for the waitq you pass it, it only enqueues you on it.

Only once all FDs have been iterated and none of thm returned a poll-event will it schedule().

Also make it return POLL_HUP when there's not mmap() area to read from.

Further, fix a silly bug in the write code.

LKML-Reference: <1237897096.24918.181.camel@twins>

---
 include/linux/perf_counter.h |    1 +
 kernel/perf_counter.c        |   14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
include/linux/perf_counter.h +1 -0
index 2b5e66d..48212c1 100644
@@ -246,6 +246,7 @@ struct file;
 struct perf_mmap_data {
 	struct rcu_head			rcu_head;
 	int				nr_pages;
+	atomic_t			wakeup;
 	atomic_t			head;
 	struct perf_counter_mmap_page   *user_page;
 	void 				*data_pages[0];
kernel/perf_counter.c +12 -2
index 0dfe910..affe227 100644
@@ -1161,7 +1161,16 @@ perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
 static unsigned int perf_poll(struct file *file, poll_table *wait)
 {
 	struct perf_counter *counter = file->private_data;
-	unsigned int events = POLLIN;
+	struct perf_mmap_data *data;
+	unsigned int events;
+
+	rcu_read_lock();
+	data = rcu_dereference(counter->data);
+	if (data)
+		events = atomic_xchg(&data->wakeup, 0);
+	else
+		events = POLL_HUP;
+	rcu_read_unlock();
 
 	poll_wait(file, &counter->waitq, wait);
 
@@ -1425,7 +1434,7 @@ static int perf_output_write(struct perf_counter *counter, int nmi,
 
 	do {
 		offset = head = atomic_read(&data->head);
-		head += sizeof(u64);
+		head += size;
 	} while (atomic_cmpxchg(&data->head, offset, head) != offset);
 
 	wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
@@ -1446,6 +1455,7 @@ static int perf_output_write(struct perf_counter *counter, int nmi,
 	 * generate a poll() wakeup for every page boundary crossed
 	 */
 	if (wakeup) {
+		atomic_xchg(&data->wakeup, POLL_IN);
 		__perf_counter_update_userpage(counter, data);
 		if (nmi) {
 			counter->wakeup_pending = 1;