--- linux-2.6.30.perfctr26/CREDITS.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/CREDITS	2009-06-10 12:36:14.000000000 +0200
@@ -2764,6 +2764,7 @@ N: Mikael Pettersson
 E: mikpe@it.uu.se
 W: http://user.it.uu.se/~mikpe/linux/
 D: Miscellaneous fixes
+D: Performance-monitoring counters driver
 
 N: Reed H. Petty
 E: rhp@draper.net
--- linux-2.6.30.perfctr26/Documentation/ioctl/ioctl-number.txt.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/Documentation/ioctl/ioctl-number.txt	2009-06-10 12:36:14.000000000 +0200
@@ -195,6 +195,8 @@ Code	Seq#	Include File		Comments
 0xB1	00-1F	PPPoX			<mailto:mostrows@styx.uwaterloo.ca>
 0xCB	00-1F	CBM serial IEC bus	in development:
 					<mailto:michael.klein@puffin.lb.shuttle.de>
+0xD0	all	performance counters	see drivers/perfctr/
+					<mailto:mikpe@it.uu.se>
 0xDD	00-3F	ZFCP device driver	see drivers/s390/scsi/
 					<mailto:aherrman@de.ibm.com>
 0xF3	00-3F	video/sisfb.h		sisfb (in development)
--- linux-2.6.30.perfctr26/MAINTAINERS.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/MAINTAINERS	2009-06-10 12:36:14.000000000 +0200
@@ -4392,6 +4392,12 @@ S:	Maintained
 F:	include/linux/delayacct.h
 F:	kernel/delayacct.c
 
+PERFORMANCE-MONITORING COUNTERS DRIVER
+P:	Mikael Pettersson
+M:	mikpe@it.uu.se
+W:	http://user.it.uu.se/~mikpe/linux/perfctr/
+S:	Maintained
+
 PERSONALITY HANDLING
 P:	Christoph Hellwig
 M:	hch@infradead.org
--- linux-2.6.30.perfctr26/arch/arm/Kconfig.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/arm/Kconfig	2009-06-10 12:36:14.000000000 +0200
@@ -732,6 +732,10 @@ config IWMMXT
 	  Enable support for iWMMXt context switching at run time if
 	  running on a CPU that supports it.
 
+if CPU_XSCALE
+source drivers/perfctr/Kconfig
+endif
+
 #  bool 'Use XScale PMU as timer source' CONFIG_XSCALE_PMU_TIMER
 config XSCALE_PMU
 	bool
--- linux-2.6.30.perfctr26/arch/arm/include/asm/processor.h.~1~	2009-03-24 18:00:31.000000000 +0100
+++ linux-2.6.30.perfctr26/arch/arm/include/asm/processor.h	2009-06-10 12:36:14.000000000 +0200
@@ -50,6 +50,10 @@ struct thread_struct {
 	unsigned long		error_code;
 							/* debugging	  */
 	struct debug_info	debug;
+
+#ifdef CONFIG_PERFCTR_VIRTUAL
+	struct vperfctr		*perfctr;		/* performance counters */
+#endif
 };
 
 #define INIT_THREAD  {	}
--- linux-2.6.30.perfctr26/arch/arm/include/asm/system.h.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/arm/include/asm/system.h	2009-06-10 12:36:14.000000000 +0200
@@ -215,7 +215,9 @@ extern struct task_struct *__switch_to(s
 
 #define switch_to(prev,next,last)					\
 do {									\
+	perfctr_suspend_thread(&(prev)->thread);			\
 	last = __switch_to(prev,task_thread_info(prev), task_thread_info(next));	\
+	perfctr_resume_thread(&(current)->thread);			\
 } while (0)
 
 #if defined(CONFIG_CPU_SA1100) || defined(CONFIG_CPU_SA110)
--- linux-2.6.30.perfctr26/arch/arm/kernel/process.c.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/arm/kernel/process.c	2009-06-10 12:36:14.000000000 +0200
@@ -26,6 +26,7 @@
 #include <linux/cpu.h>
 #include <linux/elfcore.h>
 #include <linux/pm.h>
+#include <linux/perfctr.h>
 #include <linux/tick.h>
 #include <linux/utsname.h>
 #include <linux/uaccess.h>
@@ -273,6 +274,7 @@ void show_regs(struct pt_regs * regs)
  */
 void exit_thread(void)
 {
+	perfctr_exit_thread(&current->thread);
 }
 
 ATOMIC_NOTIFIER_HEAD(thread_notify_head);
@@ -318,6 +320,8 @@ copy_thread(unsigned long clone_flags, u
 	if (clone_flags & CLONE_SETTLS)
 		thread->tp_value = regs->ARM_r3;
 
+	perfctr_copy_task(p, regs);
+
 	return 0;
 }
 
--- linux-2.6.30.perfctr26/arch/powerpc/include/asm/processor.h.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/powerpc/include/asm/processor.h	2009-06-10 12:36:14.000000000 +0200
@@ -197,6 +197,9 @@ struct thread_struct {
 	unsigned long	spefscr;	/* SPE & eFP status */
 	int		used_spe;	/* set if process has used spe */
 #endif /* CONFIG_SPE */
+#ifdef CONFIG_PERFCTR_VIRTUAL
+	struct vperfctr *perfctr;	/* performance counters */
+#endif
 };
 
 #define ARCH_MIN_TASKALIGN 16
--- linux-2.6.30.perfctr26/arch/powerpc/kernel/process.c.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/powerpc/kernel/process.c	2009-06-10 12:36:14.000000000 +0200
@@ -30,6 +30,7 @@
 #include <linux/init_task.h>
 #include <linux/module.h>
 #include <linux/kallsyms.h>
+#include <linux/perfctr.h>
 #include <linux/mqueue.h>
 #include <linux/hardirq.h>
 #include <linux/utsname.h>
@@ -409,8 +410,10 @@ struct task_struct *__switch_to(struct t
 	 * window where the kernel stack SLB and the kernel stack are out
 	 * of sync. Hard disable here.
 	 */
+	perfctr_suspend_thread(&prev->thread);
 	hard_irq_disable();
 	last = _switch(old_thread, new_thread);
+	perfctr_resume_thread(&current->thread);
 
 	local_irq_restore(flags);
 
@@ -550,6 +553,7 @@ void show_regs(struct pt_regs * regs)
 void exit_thread(void)
 {
 	discard_lazy_cpu_state();
+	perfctr_exit_thread(&current->thread);
 }
 
 void flush_thread(void)
@@ -676,6 +680,8 @@ int copy_thread(unsigned long clone_flag
 	kregs->nip = (unsigned long)ret_from_fork;
 #endif
 
+	perfctr_copy_task(p, regs);
+
 	return 0;
 }
 
--- linux-2.6.30.perfctr26/arch/powerpc/platforms/Kconfig.cputype.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/powerpc/platforms/Kconfig.cputype	2009-06-10 12:36:14.000000000 +0200
@@ -276,4 +276,8 @@ config NOT_COHERENT_CACHE
 config CHECK_CACHE_COHERENCY
 	bool
 
+if PPC32
+source "drivers/perfctr/Kconfig"
+endif
+
 endmenu
--- linux-2.6.30.perfctr26/arch/x86/Kconfig.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/Kconfig	2009-06-10 12:36:14.000000000 +0200
@@ -1424,6 +1424,8 @@ config CC_STACKPROTECTOR
 	  detected and for those versions, this configuration option is
 	  ignored. (and a warning is printed during bootup)
 
+source "drivers/perfctr/Kconfig"
+
 source kernel/Kconfig.hz
 
 config KEXEC
--- linux-2.6.30.perfctr26/arch/x86/include/asm/irq_vectors.h.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/include/asm/irq_vectors.h	2009-06-10 12:36:14.000000000 +0200
@@ -106,10 +106,14 @@
  */
 #define LOCAL_TIMER_VECTOR		0xef
 
+#if !defined(CONFIG_PERF_COUNTERS)
+#define LOCAL_PERFCTR_VECTOR		0xee
+#else
 /*
  * Performance monitoring interrupt vector:
  */
 #define LOCAL_PERF_VECTOR		0xee
+#endif
 
 /*
  * Generic system vector for platform specific use
@@ -117,7 +121,7 @@
 #define GENERIC_INTERRUPT_VECTOR	0xed
 
 /*
- * First APIC vector available to drivers: (vectors 0x30-0xee) we
+ * First APIC vector available to drivers: (vectors 0x30-0xec) we
  * start at 0x31(0x41) to spread out vectors evenly between priority
  * levels. (0x80 is the syscall vector)
  */
--- linux-2.6.30.perfctr26/arch/x86/include/asm/processor.h.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/include/asm/processor.h	2009-06-10 12:36:14.000000000 +0200
@@ -458,6 +458,8 @@ struct thread_struct {
 	unsigned long		iopl;
 	/* Max allowed port in the bitmap, in bytes: */
 	unsigned		io_bitmap_max;
+/* performance counters */
+	struct vperfctr		*perfctr;
 /* MSR_IA32_DEBUGCTLMSR value to switch in if TIF_DEBUGCTLMSR is set.  */
 	unsigned long	debugctlmsr;
 #ifdef CONFIG_X86_DS
--- linux-2.6.30.perfctr26/arch/x86/include/asm/system.h.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/include/asm/system.h	2009-06-10 12:36:14.000000000 +0200
@@ -55,6 +55,7 @@ do {									\
 	 */								\
 	unsigned long ebx, ecx, edx, esi, edi;				\
 									\
+	perfctr_suspend_thread(&(prev)->thread);			\
 	asm volatile("pushfl\n\t"		/* save    flags */	\
 		     "pushl %%ebp\n\t"		/* save    EBP   */	\
 		     "movl %%esp,%[prev_sp]\n\t"	/* save    ESP   */ \
@@ -123,7 +124,8 @@ do {									\
 #endif	/* CC_STACKPROTECTOR */
 
 /* Save restore flags to clear handle leaking NT */
-#define switch_to(prev, next, last) \
+#define switch_to(prev, next, last) do { \
+	perfctr_suspend_thread(&(prev)->thread); \
 	asm volatile(SAVE_CONTEXT					  \
 	     "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */	  \
 	     "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */	  \
@@ -146,7 +148,8 @@ do {									\
 	       [thread_info] "i" (offsetof(struct task_struct, stack)),   \
 	       [current_task] "m" (per_cpu_var(current_task))		  \
 	       __switch_canary_iparam					  \
-	     : "memory", "cc" __EXTRA_CLOBBER)
+	     : "memory", "cc" __EXTRA_CLOBBER); \
+} while (0)
 #endif
 
 #ifdef __KERNEL__
--- linux-2.6.30.perfctr26/arch/x86/kernel/entry_32.S.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/kernel/entry_32.S	2009-06-10 12:36:14.000000000 +0200
@@ -812,6 +812,23 @@ ENDPROC(name)
 /* The include is where all of the SMP etc. interrupts come from */
 #include <asm/entry_arch.h>
 
+#if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_KPERFCTR)
+ENTRY(perfctr_interrupt)
+	RING0_INT_FRAME
+	pushl $~(LOCAL_PERFCTR_VECTOR)
+	CFI_ADJUST_CFA_OFFSET 4
+	SAVE_ALL
+	TRACE_IRQS_OFF
+	pushl %esp
+	CFI_ADJUST_CFA_OFFSET 4
+	call smp_perfctr_interrupt
+	addl $4, %esp
+	CFI_ADJUST_CFA_OFFSET -4
+	jmp ret_from_intr
+	CFI_ENDPROC
+ENDPROC(perfctr_interrupt)
+#endif
+
 ENTRY(coprocessor_error)
 	RING0_INT_FRAME
 	pushl $0
--- linux-2.6.30.perfctr26/arch/x86/kernel/entry_64.S.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/kernel/entry_64.S	2009-06-10 12:36:14.000000000 +0200
@@ -1025,6 +1025,11 @@ apicinterrupt ERROR_APIC_VECTOR \
 apicinterrupt SPURIOUS_APIC_VECTOR \
 	spurious_interrupt smp_spurious_interrupt
 
+#if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_KPERFCTR)
+apicinterrupt LOCAL_PERFCTR_VECTOR \
+	perfctr_interrupt smp_perfctr_interrupt
+#endif
+
 /*
  * Exception entry points.
  */
--- linux-2.6.30.perfctr26/arch/x86/kernel/irqinit_32.c.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/kernel/irqinit_32.c	2009-06-10 12:36:14.000000000 +0200
@@ -20,6 +20,7 @@
 #include <asm/apic.h>
 #include <asm/setup.h>
 #include <asm/i8259.h>
+#include <asm/perfctr.h>
 #include <asm/traps.h>
 
 
@@ -189,6 +190,8 @@ void __init native_init_IRQ(void)
 	if (!acpi_ioapic)
 		setup_irq(2, &irq2);
 
+	perfctr_vector_init();
+
 	/*
 	 * Call quirks after call gates are initialised (usually add in
 	 * the architecture specific gates):
--- linux-2.6.30.perfctr26/arch/x86/kernel/irqinit_64.c.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/kernel/irqinit_64.c	2009-06-10 12:36:14.000000000 +0200
@@ -21,6 +21,7 @@
 #include <asm/pgtable.h>
 #include <asm/desc.h>
 #include <asm/apic.h>
+#include <asm/perfctr.h>
 #include <asm/i8259.h>
 
 /*
@@ -172,6 +173,8 @@ void __init native_init_IRQ(void)
 
 	apic_intr_init();
 
+	perfctr_vector_init();
+
 	if (!acpi_ioapic)
 		setup_irq(2, &irq2);
 }
--- linux-2.6.30.perfctr26/arch/x86/kernel/process.c.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/kernel/process.c	2009-06-10 12:36:14.000000000 +0200
@@ -8,6 +8,7 @@
 #include <linux/module.h>
 #include <linux/pm.h>
 #include <linux/clockchips.h>
+#include <linux/perfctr.h>
 #include <trace/power.h>
 #include <asm/system.h>
 #include <asm/apic.h>
@@ -83,6 +84,7 @@ void exit_thread(void)
 		put_cpu();
 		kfree(bp);
 	}
+	perfctr_exit_thread(t);
 
 	ds_exit_thread(current);
 }
--- linux-2.6.30.perfctr26/arch/x86/kernel/process_32.c.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/kernel/process_32.c	2009-06-10 12:36:14.000000000 +0200
@@ -29,6 +29,7 @@
 #include <linux/delay.h>
 #include <linux/reboot.h>
 #include <linux/init.h>
+#include <linux/perfctr.h>
 #include <linux/mc146818rtc.h>
 #include <linux/module.h>
 #include <linux/kallsyms.h>
@@ -265,6 +266,8 @@ int copy_thread(unsigned long clone_flag
 
 	task_user_gs(p) = get_user_gs(regs);
 
+	perfctr_copy_task(p, regs);
+
 	tsk = current;
 	if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
 		p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
@@ -427,6 +430,8 @@ __switch_to(struct task_struct *prev_p, 
 
 	percpu_write(current_task, next_p);
 
+	perfctr_resume_thread(next);
+
 	return prev_p;
 }
 
--- linux-2.6.30.perfctr26/arch/x86/kernel/process_64.c.~1~	2009-06-10 12:00:43.000000000 +0200
+++ linux-2.6.30.perfctr26/arch/x86/kernel/process_64.c	2009-06-10 12:36:14.000000000 +0200
@@ -32,6 +32,7 @@
 #include <linux/delay.h>
 #include <linux/module.h>
 #include <linux/ptrace.h>
+#include <linux/perfctr.h>
 #include <linux/random.h>
 #include <linux/notifier.h>
 #include <linux/kprobes.h>
@@ -309,6 +310,8 @@ int copy_thread(unsigned long clone_flag
 	savesegment(es, p->thread.es);
 	savesegment(ds, p->thread.ds);
 
+	perfctr_copy_task(p, regs);
+
 	if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
 		p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
 		if (!p->thread.io_bitmap_ptr) {
@@ -491,6 +494,9 @@ __switch_to(struct task_struct *prev_p, 
 	 */
 	if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
 		math_state_restore();
+
+	perfctr_resume_thread(next);
+
 	return prev_p;
 }
 
--- linux-2.6.30.perfctr26/drivers/Makefile.~1~	2009-06-10 12:00:44.000000000 +0200
+++ linux-2.6.30.perfctr26/drivers/Makefile	2009-06-10 12:36:14.000000000 +0200
@@ -94,6 +94,7 @@ obj-$(CONFIG_MEMSTICK)		+= memstick/
 obj-$(CONFIG_NEW_LEDS)		+= leds/
 obj-$(CONFIG_INFINIBAND)	+= infiniband/
 obj-$(CONFIG_SGI_SN)		+= sn/
+obj-$(CONFIG_KPERFCTR)		+= perfctr/
 obj-y				+= firmware/
 obj-$(CONFIG_CRYPTO)		+= crypto/
 obj-$(CONFIG_SUPERH)		+= sh/
--- linux-2.6.30.perfctr26/fs/exec.c.~1~	2009-06-10 12:00:46.000000000 +0200
+++ linux-2.6.30.perfctr26/fs/exec.c	2009-06-10 12:36:14.000000000 +0200
@@ -50,6 +50,7 @@
 #include <linux/tsacct_kern.h>
 #include <linux/cn_proc.h>
 #include <linux/audit.h>
+#include <linux/perfctr.h>
 #include <linux/tracehook.h>
 #include <linux/kmod.h>
 #include <linux/fsnotify.h>
@@ -971,6 +972,7 @@ int flush_old_exec(struct linux_binprm *
 	set_task_comm(current, tcomm);
 
 	current->flags &= ~PF_RANDOMIZE;
+	perfctr_flush_thread(&current->thread);
 	flush_thread();
 
 	/* Set the new mm task size. We have to do that late because it may
--- linux-2.6.30.perfctr26/kernel/exit.c.~1~	2009-06-10 12:00:47.000000000 +0200
+++ linux-2.6.30.perfctr26/kernel/exit.c	2009-06-10 12:36:14.000000000 +0200
@@ -30,6 +30,7 @@
 #include <linux/proc_fs.h>
 #include <linux/kthread.h>
 #include <linux/mempolicy.h>
+#include <linux/perfctr.h>
 #include <linux/taskstats_kern.h>
 #include <linux/delayacct.h>
 #include <linux/freezer.h>
@@ -206,6 +207,7 @@ repeat:
 			leader->exit_state = EXIT_DEAD;
 	}
 
+	perfctr_release_task(p);
 	write_unlock_irq(&tasklist_lock);
 	release_thread(p);
 	call_rcu(&p->rcu, delayed_put_task_struct);
--- linux-2.6.30.perfctr26/kernel/sched.c.~1~	2009-06-10 12:00:47.000000000 +0200
+++ linux-2.6.30.perfctr26/kernel/sched.c	2009-06-10 12:36:14.000000000 +0200
@@ -54,6 +54,7 @@
 #include <linux/cpu.h>
 #include <linux/cpuset.h>
 #include <linux/percpu.h>
+#include <linux/perfctr.h>
 #include <linux/kthread.h>
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
@@ -6646,6 +6647,8 @@ int set_cpus_allowed_ptr(struct task_str
 	struct rq *rq;
 	int ret = 0;
 
+	perfctr_set_cpus_allowed(p, *new_mask); /* XXX: convert to _ptr */
+
 	rq = task_rq_lock(p, &flags);
 	if (!cpumask_intersects(new_mask, cpu_online_mask)) {
 		ret = -EINVAL;
--- linux-2.6.30.perfctr26/kernel/timer.c.~1~	2009-06-10 12:00:47.000000000 +0200
+++ linux-2.6.30.perfctr26/kernel/timer.c	2009-06-10 12:36:14.000000000 +0200
@@ -33,6 +33,7 @@
 #include <linux/jiffies.h>
 #include <linux/posix-timers.h>
 #include <linux/cpu.h>
+#include <linux/perfctr.h>
 #include <linux/syscalls.h>
 #include <linux/delay.h>
 #include <linux/tick.h>
@@ -1114,6 +1115,7 @@ void update_process_times(int user_tick)
 
 	/* Note: this timer irq context must be accounted for as well. */
 	account_process_tick(p, user_tick);
+	perfctr_sample_thread(&p->thread);
 	run_local_timers();
 	if (rcu_pending(cpu))
 		rcu_check_callbacks(cpu, user_tick);
