Skip to contentSkip to conversation

generic software counters

PATCH In review Peter Zijlstra 2 participants 8 messages Mar 10, 2009

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH 1/4 perf_counter: software counter event infrastructure

Provide generic software counter infrastructure that supports software events.

This will be used to allow sample based profiling based on software events such as pagefaults. The current infrastructure can only provide a count of such events, no place information.

---
 include/linux/perf_counter.h |    8 +
 kernel/perf_counter.c        |  206 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 214 insertions(+)
include/linux/perf_counter.h +8 -0
Index: linux-2.6/include/linux/perf_counter.h
===================================================================
@@ -126,6 +126,7 @@ struct hw_perf_counter {
 	unsigned long			counter_base;
 	int				nmi;
 	unsigned int			idx;
+	atomic64_t			count; /* software */
 	atomic64_t			prev_count;
 	u64				irq_period;
 	atomic64_t			period_left;
@@ -283,6 +284,8 @@ static inline int is_software_counter(st
 	return !counter->hw_event.raw && counter->hw_event.type < 0;
 }
 
+extern void perf_swcounter_event(enum hw_event_types, u64, struct pt_regs *);
+
 #else
 static inline void
 perf_counter_task_sched_in(struct task_struct *task, int cpu)		{ }
@@ -299,6 +302,11 @@ static inline void hw_perf_restore(u64 c
 static inline u64 hw_perf_save_disable(void)		      { return 0; }
 static inline int perf_counter_task_disable(void)	{ return -EINVAL; }
 static inline int perf_counter_task_enable(void)	{ return -EINVAL; }
+
+static inline void
+perf_swcounter_event(enum hw_event_types event, u64 nr, struct pt_regs *regs)
+{
+}
 #endif
 
 #endif /* __KERNEL__ */
kernel/perf_counter.c +206 -0

249 lines not on this page · raw message

#2 lore raw

PATCH 2/4 perf_counter: provide pagefault software events

In reply to Peter Zijlstra

We use the generic software counter infrastructure to provide page fault events.

---
 arch/powerpc/mm/fault.c |    3 ++
 arch/x86/mm/fault.c     |    3 ++
 kernel/perf_counter.c   |   53 ++----------------------------------------------
 3 files changed, 9 insertions(+), 50 deletions(-)
kernel/perf_counter.c +3 -50
Index: linux-2.6/kernel/perf_counter.c
===================================================================
@@ -1601,57 +1601,10 @@ static const struct hw_perf_counter_ops 
  * Software counter: page faults
  */
 
-#ifdef CONFIG_VM_EVENT_COUNTERS
-#define cpu_page_faults()	__get_cpu_var(vm_event_states).event[PGFAULT]
-#else
-#define cpu_page_faults()	0
-#endif
-
-static u64 get_page_faults(struct perf_counter *counter)
-{
-	struct task_struct *curr = counter->ctx->task;
-
-	if (curr)
-		return curr->maj_flt + curr->min_flt;
-	return cpu_page_faults();
-}
-
-static void page_faults_perf_counter_update(struct perf_counter *counter)
-{
-	u64 prev, now;
-	s64 delta;
-
-	prev = atomic64_read(&counter->hw.prev_count);
-	now = get_page_faults(counter);
-
-	atomic64_set(&counter->hw.prev_count, now);
-
-	delta = now - prev;
-
-	atomic64_add(delta, &counter->count);
-}
-
-static void page_faults_perf_counter_read(struct perf_counter *counter)
-{
-	page_faults_perf_counter_update(counter);
-}
-
-static int page_faults_perf_counter_enable(struct perf_counter *counter)
-{
-	if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
-		atomic64_set(&counter->hw.prev_count, get_page_faults(counter));
-	return 0;
-}
-
-static void page_faults_perf_counter_disable(struct perf_counter *counter)
-{
-	page_faults_perf_counter_update(counter);
-}
-
 static const struct hw_perf_counter_ops perf_ops_page_faults = {
-	.enable		= page_faults_perf_counter_enable,
-	.disable	= page_faults_perf_counter_disable,
-	.read		= page_faults_perf_counter_read,
+	.enable		= perf_swcounter_enable,
+	.disable	= perf_swcounter_disable,
+	.read		= perf_swcounter_read,
 };
 
 /*
arch/powerpc/mm/fault.c +3 -0
Index: linux-2.6/arch/powerpc/mm/fault.c
===================================================================
@@ -29,6 +29,7 @@
 #include <linux/module.h>
 #include <linux/kprobes.h>
 #include <linux/kdebug.h>
+#include <linux/perf_counter.h>
 
 #include <asm/firmware.h>
 #include <asm/page.h>
@@ -170,6 +171,8 @@ int __kprobes do_page_fault(struct pt_re
 		die("Weird page fault", regs, SIGSEGV);
 	}
 
+	perf_swcount_event(PERF_COUNT_PAGE_FAULTS, 1, regs);
+
 	/* When running in the kernel we expect faults to occur only to
 	 * addresses in user space.  All other faults represent errors in the
 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
arch/x86/mm/fault.c +3 -0
Index: linux-2.6/arch/x86/mm/fault.c
===================================================================
@@ -27,6 +27,7 @@
 #include <linux/tty.h>
 #include <linux/smp.h>
 #include <linux/mm.h>
+#include <linux/perf_counter.h>
 
 #include <asm-generic/sections.h>
 
@@ -1056,6 +1057,8 @@ do_page_fault(struct pt_regs *regs, unsi
 	if (unlikely(error_code & PF_RSVD))
 		pgtable_bad(regs, error_code, address);
 
+	perf_swcounter_event(PERF_COUNT_PAGE_FAULTS, 1, regs);
+
 	/*
 	 * If we're in an interrupt, have no user context or are running
 	 * in an atomic region then we must not take the fault:
#3 lore raw

Re: [PATCH 2/4] perf_counter: provide pagefault software events

Quoted 13 lines · Peter Zijlstra

Index: linux-2.6/arch/powerpc/mm/fault.c
===================================================================

arch/powerpc/mm/fault.c +1 -0
@@ -29,6 +29,7 @@
 #include <linux/module.h>
 #include <linux/kprobes.h>
 #include <linux/kdebug.h>
+#include <linux/perf_counter.h>
 
 #include <asm/firmware.h>
 #include <asm/page.h>

arch/powerpc/mm/fault.c int __kprobes do_page_fault(struct pt_re · line 174

Quoted 4 lines
arch/powerpc/mm/fault.c +1 -0
@@ -170,6 +171,8 @@ int __kprobes do_page_fault(struct pt_re
 		die("Weird page fault", regs, SIGSEGV);
 	}
 
+	perf_swcount_event(PERF_COUNT_PAGE_FAULTS, 1, regs);

Wow, that's really nice - this way we can display a profile of pagefault events in KernelTop - as if it was a real cycles or other hw event based profile. Have you tried it out?

Ingo

#4 lore raw

Re: [PATCH 2/4] perf_counter: provide pagefault software events

Quoted 18 lines · Ingo Molnar
Quoted 16 lines · Peter Zijlstra

Index: linux-2.6/arch/powerpc/mm/fault.c
===================================================================

arch/powerpc/mm/fault.c +1 -0
@@ -29,6 +29,7 @@
 #include <linux/module.h>
 #include <linux/kprobes.h>
 #include <linux/kdebug.h>
+#include <linux/perf_counter.h>
 
 #include <asm/firmware.h>
 #include <asm/page.h>
@@ -170,6 +171,8 @@ int __kprobes do_page_fault(struct pt_re
 		die("Weird page fault", regs, SIGSEGV);
 	}
 
Quoted 5 lines

arch/powerpc/mm/fault.c int __kprobes do_page_fault(struct pt_re · line 174

Quoted 1 line
arch/powerpc/mm/fault.c +1 -0
+	perf_swcount_event(PERF_COUNT_PAGE_FAULTS, 1, regs);

Wow, that's really nice - this way we can display a profile of pagefault events in KernelTop - as if it was a real cycles or other hw event based profile. Have you tried it out?

Yeah, but unsurprising all pagefaults I generated were in userspace, so kerneltop stayed empty.

Found that perfstat went bokers though, forgot to handle the case where there's no overflow interrupt.

/me goes poke at the code a bit more.

#5 lore raw

PATCH 3/4 perf_counter: x86: fix 32bit irq_period assumption.

In reply to Peter Zijlstra

No need to assume the irq_period is 32bit.

---
 arch/x86/kernel/cpu/perf_counter.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/perf_counter.c +1 -1
Index: linux-2.6/arch/x86/kernel/cpu/perf_counter.c
===================================================================
@@ -449,7 +449,7 @@ __hw_perf_counter_set_period(struct perf
 			     struct hw_perf_counter *hwc, int idx)
 {
 	s64 left = atomic64_read(&hwc->period_left);
-	s32 period = hwc->irq_period;
+	s64 period = hwc->irq_period;
 	int err;
 
 	/*
#6 lore raw

PATCH 4/4 perf_counter: use list_move_tail

In reply to Peter Zijlstra

Instead of del/add use a move list-op.

---
 kernel/perf_counter.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/perf_counter.c +1 -2
Index: linux-2.6/kernel/perf_counter.c
===================================================================
@@ -89,8 +89,7 @@ list_del_counter(struct perf_counter *co
 	list_for_each_entry_safe(sibling, tmp,
 				 &counter->sibling_list, list_entry) {
 
-		list_del_init(&sibling->list_entry);
-		list_add_tail(&sibling->list_entry, &ctx->counter_list);
+		list_move_tail(&sibling->list_entry, &ctx->counter_list);
 		sibling->group_leader = sibling;
 	}
 }