Skip to contentSkip to conversation

Cell OProfile: SPU mutex lock fix, version 4

PATCH In review Carl Love 2 participants 4 messages Aug 1, 2008 – Aug 8, 2008 PowerPC

lore
Share & tools
lore

Carl Love

author
#0 lore raw

PATCH 1/2 Cell OProfile: SPU mutex lock fix, version 4

Version 4 of the SPU mutex lock fix.

The issue is the SPU code is not holding the kernel mutex lock while adding samples to the kernel buffer.

This patch creates per SPU buffers to hold the data. Data is added to the buffers from in interrupt context. The data is periodically pushed to the kernel buffer via a new Oprofile function oprofile_put_buff(). The oprofile_put_buff() function is called via a work queue enabling the funtion to acquire the mutex lock.

The existing user controls for adjusting the per CPU buffer size is used to control the size of the per SPU buffers. Similarly, overflows of the SPU buffers are reported by incrementing the per CPU buffer stats. This eliminates the need to have architecture specific controls for the per SPU buffers which is not acceptable to the OProfile user tool maintainer.

The export of the oprofile add_event_entry() is removed as it is no longer needed given this patch.

Note, this patch has not addressed the issue of indexing arrays by the spu number. This still needs to be fixed as the spu numbering is not guarenteed to be 0 to max_num_spus-1.

drivers/oprofile/cpu_buffer.c +14 -1
Index: Cell_kernel_6_26_2008/drivers/oprofile/cpu_buffer.c
===================================================================
@@ -37,11 +37,24 @@ static int work_enabled;
 void free_cpu_buffers(void)
 {
 	int i;
- 
+
 	for_each_online_cpu(i)
 		vfree(per_cpu(cpu_buffer, i).buffer);
 }
 
+unsigned long oprofile_get_cpu_buffer_size(void)
+{
+	return fs_cpu_buffer_size;
+}
+
+void oprofile_cpu_buffer_inc_smpl_lost(void)
+{
+	struct oprofile_cpu_buffer *cpu_buf
+		= &__get_cpu_var(cpu_buffer);
+
+	cpu_buf->sample_lost_overflow++;
+}
+
 int alloc_cpu_buffers(void)
 {
 	int i;
include/linux/oprofile.h +9 -7
Index: Cell_kernel_6_26_2008/include/linux/oprofile.h
===================================================================
@@ -84,13 +84,6 @@ int oprofile_arch_init(struct oprofile_o
 void oprofile_arch_exit(void);
 
 /**
- * Add data to the event buffer.
- * The data passed is free-form, but typically consists of
- * file offsets, dcookies, context information, and ESCAPE codes.
- */
-void add_event_entry(unsigned long data);
-
-/**
  * Add a sample. This may be called from any context. Pass
  * smp_processor_id() as cpu.
  */
@@ -160,5 +153,14 @@ int oprofilefs_ulong_from_user(unsigned 
 
 /** lock for read/write safety */
 extern spinlock_t oprofilefs_lock;
+
+/**
+ * Add the contents of a circular buffer to the event buffer.
+ */
+void oprofile_put_buff(unsigned long *buf,unsigned int start,
+                       unsigned int stop, unsigned int max);
+
+unsigned long oprofile_get_cpu_buffer_size(void);
+void oprofile_cpu_buffer_inc_smpl_lost(void);
  
 #endif /* OPROFILE_H */
arch/powerpc/oprofile/cell/spu_profiler.c +2 -2
Index: Cell_kernel_6_26_2008/arch/powerpc/oprofile/cell/spu_profiler.c
===================================================================
@@ -23,12 +23,11 @@
 
 static u32 *samples;
 
-static int spu_prof_running;
+int spu_prof_running;
 static unsigned int profiling_interval;
 
 #define NUM_SPU_BITS_TRBUF 16
 #define SPUS_PER_TB_ENTRY   4
-#define SPUS_PER_NODE	     8
 
 #define SPU_PC_MASK	     0xFFFF
 
@@ -208,6 +207,7 @@ int start_spu_profiling(unsigned int cyc
 
 	spu_prof_running = 1;
 	hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
+	schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);
 
 	return 0;
 }
arch/powerpc/oprofile/cell/spu_task_sync.c +227 -24

311 lines not on this page · raw message

drivers/oprofile/event_buffer.h +7 -0
Index: Cell_kernel_6_26_2008/drivers/oprofile/event_buffer.h
===================================================================
@@ -17,6 +17,13 @@ int alloc_event_buffer(void);
 
 void free_event_buffer(void);
  
+/**
+ * Add data to the event buffer.
+ * The data passed is free-form, but typically consists of
+ * file offsets, dcookies, context information, and ESCAPE codes.
+ */
+void add_event_entry(unsigned long data);
+
 /* wake up the process sleeping on the event file */
 void wake_up_buffer_waiter(void);
 
arch/powerpc/oprofile/cell/pr_util.h +19 -0
Index: Cell_kernel_6_26_2008/arch/powerpc/oprofile/cell/pr_util.h
===================================================================
@@ -24,6 +24,11 @@
 #define SKIP_GENERIC_SYNC 0
 #define SYNC_START_ERROR -1
 #define DO_GENERIC_SYNC 1
+#define SPUS_PER_NODE   8
+#define DEFAULT_TIMER_EXPIRE  (HZ / 10)
+
+extern struct delayed_work spu_work;
+extern int spu_prof_running;
 
 struct spu_overlay_info {	/* map of sections within an SPU overlay */
 	unsigned int vma;	/* SPU virtual memory address from elf */
@@ -62,6 +67,20 @@ struct vma_to_fileoffset_map {	/* map of
 
 };
 
+/* Put head and tail for the spu buffer into a structure to keep
+ * them in the same cache line.
+ */
+struct head_tail {
+	unsigned int head;
+	unsigned int tail;
+};
+
+struct spu_buffers {
+	unsigned long *buff[MAX_NUMNODES * SPUS_PER_NODE];
+	struct head_tail index[MAX_NUMNODES * SPUS_PER_NODE];
+};
+
+
 /* The three functions below are for maintaining and accessing
  * the vma-to-fileoffset map.
  */
drivers/oprofile/buffer_sync.c +33 -0
Index: Cell_kernel_6_26_2008/drivers/oprofile/buffer_sync.c
===================================================================
@@ -551,3 +551,36 @@ void sync_buffer(int cpu)
 
 	mutex_unlock(&buffer_mutex);
 }
+
+/* The function can be used to add a buffer worth of data directly to
+ * the kernel buffer. The buffer is assumed to be a circular buffer.
+ * Take the entries from index start and end at index end, wrapping
+ * at max_entries.
+ */
+void oprofile_put_buff(unsigned long *buf, unsigned int start,
+		       unsigned int stop, unsigned int max)
+{
+	int i;
+
+	/* Check the args */
+	if (stop > max) {
+		printk(KERN_ERR "oprofile: oprofile_put_buff(), illegal "\
+		       "arguments; stop greater then max."\
+		       " stop = %u, max = %u.\n",
+		       stop, max);
+		return;
+	}
+
+	i = start;
+
+	mutex_lock(&buffer_mutex);
+	while (i != stop) {
+		add_event_entry(buf[i++]);
+
+		if (i >= max)
+			i = 0;
+	}
+
+	mutex_unlock(&buffer_mutex);
+}
+
#1 lore raw
Quoted 2 lines · Carl Love

The issue is the SPU code is not holding the kernel mutex lock while adding samples to the kernel buffer.

Thanks for your patch, and sorry for not replying earlier. It looks good from a functionality perspective, I just have some style comments left that I hope we can address quickly.

Since this is still a bug fix (though a rather large one), I guess we can should get it into 2.6.27-rc3.

	Arnd <><

arch/powerpc/oprofile/cell/spu_task_sync.c static DEFINE_SPINLOCK(buffer_lock); · line 41

Quoted 13 lines

Index: Cell_kernel_6_26_2008/arch/powerpc/oprofile/cell/spu_task_sync.c
===================================================================

arch/powerpc/oprofile/cell/spu_task_sync.c +4 -1
@@ -35,7 +35,106 @@ static DEFINE_SPINLOCK(buffer_lock);
 static DEFINE_SPINLOCK(cache_lock);
 static int num_spu_nodes;
 int spu_prof_num_nodes;
-int last_guard_val[MAX_NUMNODES * 8];
+int last_guard_val[MAX_NUMNODES * SPUS_PER_NODE];
+static int spu_ctx_sw_seen[MAX_NUMNODES * SPUS_PER_NODE];
+static int sync_start_registered;
+static int delayed_work_init;

You don't need the delayed_work_init variable. Just initialize the work queue in your init function to be sure it's always right.

I think you should also try to remove sync_start_registered, these global state variables can easily get annoying when you try to change something.
AFAICT, spu_sync_stop does not get called unless spu_sync_start was successful, so the sync_start_registered variable is redundant.

Quoted 18 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +18 -0
+static int oprofile_spu_buff_create(void)
+{
+	int spu;
+
+	max_spu_buff = oprofile_get_cpu_buffer_size();
+
+	for (spu = 0; spu < num_spu_nodes; spu++) {
+		/* create circular buffers to store the data in.
+		 * use locks to manage accessing the buffers
+ 		 */
+		spu_buff.index[spu].head = 0;
+		spu_buff.index[spu].tail = 0;
+
+		/*
+		 * Create a buffer for each SPU.  Can't reliably
+		 * create a single buffer for all spus due to not
+		 * enough contiguous kernel memory.
+		 */

arch/powerpc/oprofile/cell/spu_task_sync.c static int number_of_online_nodes(void) · line 496

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+
+		spu_buff.buff[spu] = kzalloc((max_spu_buff
+					      * sizeof(unsigned long)),
+					     GFP_KERNEL);
+
+		if (!spu_buff.buff[spu]) {
+			printk(KERN_ERR "SPU_PROF: "
+			       "%s, line %d:  oprofile_spu_buff_create " \
+		       "failed to allocate spu buffer %d.\n",
+			       __FUNCTION__, __LINE__, spu);

The formatting of the printk line is a little unconventional. You certainly don't need the '\' at the end of the line.
Also, please don't use __FUNCTION__ in new code, but instead of the standard c99 __func__ symbol. The __LINE__ macro is fine.

Quoted 4 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +4 -0
+
+			/* release the spu buffers that have been allocated */
+			while (spu >= 0) {
+				if (spu_buff.buff[spu]) {

arch/powerpc/oprofile/cell/spu_task_sync.c static int number_of_online_nodes(void) · line 510

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+					kfree(spu_buff.buff[spu]);
+					spu_buff.buff[spu] = 0;
+				}
+				spu--;
+			}
+			return 1;
+		}
+	}
+	return 0;
+}

The convention for a function would be to return -ENOMEM here instead of 1.

Quoted 15 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +1 -1
 /* The main purpose of this function is to synchronize
  * OProfile with SPUFS by registering to be notified of
  * SPU task switches.
@@ -372,30 +521,50 @@ static int number_of_online_nodes(void)
  */
 int spu_sync_start(void)
 {
-	int k;
+	int spu;
 	int ret = SKIP_GENERIC_SYNC;
 	int register_ret;
 	unsigned long flags = 0;
 
 	spu_prof_num_nodes = number_of_online_nodes();
 	num_spu_nodes = spu_prof_num_nodes * 8;

arch/powerpc/oprofile/cell/spu_task_sync.c static int number_of_online_nodes(void) · line 540

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+	delayed_work_init = 0;
+
+	/* create buffer for storing the SPU data to put in
+	 * the kernel buffer.
+	 */
+	if (oprofile_spu_buff_create()) {
+		ret = -ENOMEM;
+		sync_start_registered = 0;
+		goto out;
+	}

consequently, this becomes

	ret = oprofile_spu_buff_create();
	if (ret)
		goto out;
Quoted 12 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +11 -1
-out:
+
+	/* remove scheduled work queue item rather then waiting
+	 * for every queued entry to execute.  Then flush pending
+	 * system wide buffer to event buffer.  Only try to
+	 * remove if it was scheduled.  Get kernel errors otherwise.
+	 */
+	if (delayed_work_init)
+		cancel_delayed_work(&spu_work);
+
+	for (k = 0; k < num_spu_nodes; k++) {
+		spu_ctx_sw_seen[k] = 0;

arch/powerpc/oprofile/cell/spu_task_sync.c out: · line 681

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+
+		/* spu_sys_buff will be null if there was a problem
+		 * allocating the buffer.  Only delete if it exists.
+		 */
+
+		if (spu_buff.buff[k]) {
+			kfree(spu_buff.buff[k]);
+			spu_buff.buff[k] = 0;
+		}
+	}

The if(spu_buff.buff[k]) is redundant, kfree(NULL) is valid, so you should simplify this.

arch/powerpc/oprofile/cell/spu_task_sync.c static int number_of_online_nodes(void)

Quoted 13 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +13 -0
+/* Put head and tail for the spu buffer into a structure to keep
+ * them in the same cache line.
+ */
+struct head_tail {
+	unsigned int head;
+	unsigned int tail;
+};
+
+struct spu_buffers {
+	unsigned long *buff[MAX_NUMNODES * SPUS_PER_NODE];
+	struct head_tail index[MAX_NUMNODES * SPUS_PER_NODE];
+};
+

Did you measure a problem in the cache layout here? A simpler array of

struct spu_buffer {
	int last_guard_val;
	int spu_ctx_sw_seen;
	unsigned long *buff;
	unsigned int head, tail;
};

would otherwise be more reasonable, mostly for readability.

Quoted 8 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +8 -0
+/* The function can be used to add a buffer worth of data directly to
+ * the kernel buffer. The buffer is assumed to be a circular buffer.
+ * Take the entries from index start and end at index end, wrapping
+ * at max_entries.
+ */
+void oprofile_put_buff(unsigned long *buf, unsigned int start,
+		       unsigned int stop, unsigned int max)
+{

arch/powerpc/oprofile/cell/spu_task_sync.c

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+	int i;
+
+	/* Check the args */
+	if (stop > max) {
+		printk(KERN_ERR "oprofile: oprofile_put_buff(), illegal "\
+		       "arguments; stop greater then max."\
+		       " stop = %u, max = %u.\n",
+		       stop, max);
+		return;
+	}

hmm, this is not a good interface. Better return -EINVAL in case of error, or, even better, don't call this function with invalid arguments. Note that in the kernel, the rule is that you expect other kernel code to be written correctly, and user space code to call you with any combination of invalid arguments.

	Arnd <><
author
#2 lore raw
Quoted 31 lines · Arnd Bergmann
Quoted 2 lines · Carl Love

The issue is the SPU code is not holding the kernel mutex lock while adding samples to the kernel buffer.

Thanks for your patch, and sorry for not replying earlier. It looks good from a functionality perspective, I just have some style comments left that I hope we can address quickly.

Since this is still a bug fix (though a rather large one), I guess we can should get it into 2.6.27-rc3.

	Arnd <><

arch/powerpc/oprofile/cell/spu_task_sync.c static DEFINE_SPINLOCK(buffer_lock); · line 41

Quoted 13 lines

Index: Cell_kernel_6_26_2008/arch/powerpc/oprofile/cell/spu_task_sync.c
===================================================================

arch/powerpc/oprofile/cell/spu_task_sync.c +4 -1
@@ -35,7 +35,106 @@ static DEFINE_SPINLOCK(buffer_lock);
 static DEFINE_SPINLOCK(cache_lock);
 static int num_spu_nodes;
 int spu_prof_num_nodes;
-int last_guard_val[MAX_NUMNODES * 8];
+int last_guard_val[MAX_NUMNODES * SPUS_PER_NODE];
+static int spu_ctx_sw_seen[MAX_NUMNODES * SPUS_PER_NODE];
+static int sync_start_registered;
+static int delayed_work_init;

You don't need the delayed_work_init variable. Just initialize the work queue in your init function to be sure it's always right.

Quoted 7 lines

I think you should also try to remove sync_start_registered, these global state variables can easily get annoying when you try to change something.
AFAICT, spu_sync_stop does not get called unless spu_sync_start was successful, so the sync_start_registered variable is redundant.

I was able to remove the delayed_work_init variable with a little code restructuring.

I worked on the sync_start_registered variable. I had put it in because I thought that the call to spu_switch_event_unregister() call for a non registered event was giving me a kernel error. I retested this and it does look like it is OK. So it looks safe to remove the sync_start_registered variable.

The rest of your comments were fairly easy to address.

Quoted 163 lines
Quoted 18 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +18 -0
+static int oprofile_spu_buff_create(void)
+{
+	int spu;
+
+	max_spu_buff = oprofile_get_cpu_buffer_size();
+
+	for (spu = 0; spu < num_spu_nodes; spu++) {
+		/* create circular buffers to store the data in.
+		 * use locks to manage accessing the buffers
+ 		 */
+		spu_buff.index[spu].head = 0;
+		spu_buff.index[spu].tail = 0;
+
+		/*
+		 * Create a buffer for each SPU.  Can't reliably
+		 * create a single buffer for all spus due to not
+		 * enough contiguous kernel memory.
+		 */

arch/powerpc/oprofile/cell/spu_task_sync.c static int number_of_online_nodes(void) · line 496

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+
+		spu_buff.buff[spu] = kzalloc((max_spu_buff
+					      * sizeof(unsigned long)),
+					     GFP_KERNEL);
+
+		if (!spu_buff.buff[spu]) {
+			printk(KERN_ERR "SPU_PROF: "
+			       "%s, line %d:  oprofile_spu_buff_create " \
+		       "failed to allocate spu buffer %d.\n",
+			       __FUNCTION__, __LINE__, spu);

The formatting of the printk line is a little unconventional. You certainly don't need the '\' at the end of the line.
Also, please don't use __FUNCTION__ in new code, but instead of the standard c99 __func__ symbol. The __LINE__ macro is fine.

Quoted 4 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +4 -0
+
+			/* release the spu buffers that have been allocated */
+			while (spu >= 0) {
+				if (spu_buff.buff[spu]) {

arch/powerpc/oprofile/cell/spu_task_sync.c static int number_of_online_nodes(void) · line 510

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+					kfree(spu_buff.buff[spu]);
+					spu_buff.buff[spu] = 0;
+				}
+				spu--;
+			}
+			return 1;
+		}
+	}
+	return 0;
+}

The convention for a function would be to return -ENOMEM here instead of 1.

Quoted 15 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +1 -1
 /* The main purpose of this function is to synchronize
  * OProfile with SPUFS by registering to be notified of
  * SPU task switches.
@@ -372,30 +521,50 @@ static int number_of_online_nodes(void)
  */
 int spu_sync_start(void)
 {
-	int k;
+	int spu;
 	int ret = SKIP_GENERIC_SYNC;
 	int register_ret;
 	unsigned long flags = 0;
 
 	spu_prof_num_nodes = number_of_online_nodes();
 	num_spu_nodes = spu_prof_num_nodes * 8;

arch/powerpc/oprofile/cell/spu_task_sync.c static int number_of_online_nodes(void) · line 540

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+	delayed_work_init = 0;
+
+	/* create buffer for storing the SPU data to put in
+	 * the kernel buffer.
+	 */
+	if (oprofile_spu_buff_create()) {
+		ret = -ENOMEM;
+		sync_start_registered = 0;
+		goto out;
+	}

consequently, this becomes

	ret = oprofile_spu_buff_create();
	if (ret)
		goto out;
Quoted 12 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +11 -1
-out:
+
+	/* remove scheduled work queue item rather then waiting
+	 * for every queued entry to execute.  Then flush pending
+	 * system wide buffer to event buffer.  Only try to
+	 * remove if it was scheduled.  Get kernel errors otherwise.
+	 */
+	if (delayed_work_init)
+		cancel_delayed_work(&spu_work);
+
+	for (k = 0; k < num_spu_nodes; k++) {
+		spu_ctx_sw_seen[k] = 0;

arch/powerpc/oprofile/cell/spu_task_sync.c out: · line 681

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+
+		/* spu_sys_buff will be null if there was a problem
+		 * allocating the buffer.  Only delete if it exists.
+		 */
+
+		if (spu_buff.buff[k]) {
+			kfree(spu_buff.buff[k]);
+			spu_buff.buff[k] = 0;
+		}
+	}

The if(spu_buff.buff[k]) is redundant, kfree(NULL) is valid, so you should simplify this.

arch/powerpc/oprofile/cell/spu_task_sync.c static int number_of_online_nodes(void)

Quoted 13 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +13 -0
+/* Put head and tail for the spu buffer into a structure to keep
+ * them in the same cache line.
+ */
+struct head_tail {
+	unsigned int head;
+	unsigned int tail;
+};
+
+struct spu_buffers {
+	unsigned long *buff[MAX_NUMNODES * SPUS_PER_NODE];
+	struct head_tail index[MAX_NUMNODES * SPUS_PER_NODE];
+};
+

Did you measure a problem in the cache layout here? A simpler array of

struct spu_buffer {
	int last_guard_val;
	int spu_ctx_sw_seen;
	unsigned long *buff;
	unsigned int head, tail;
};

would otherwise be more reasonable, mostly for readability.

Quoted 8 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +8 -0
+/* The function can be used to add a buffer worth of data directly to
+ * the kernel buffer. The buffer is assumed to be a circular buffer.
+ * Take the entries from index start and end at index end, wrapping
+ * at max_entries.
+ */
+void oprofile_put_buff(unsigned long *buf, unsigned int start,
+		       unsigned int stop, unsigned int max)
+{

arch/powerpc/oprofile/cell/spu_task_sync.c

Quoted 10 lines
arch/powerpc/oprofile/cell/spu_task_sync.c +10 -0
+	int i;
+
+	/* Check the args */
+	if (stop > max) {
+		printk(KERN_ERR "oprofile: oprofile_put_buff(), illegal "\
+		       "arguments; stop greater then max."\
+		       " stop = %u, max = %u.\n",
+		       stop, max);
+		return;
+	}

hmm, this is not a good interface. Better return -EINVAL in case of error, or, even better, don't call this function with invalid arguments. Note that in the kernel, the rule is that you expect other kernel code to be written correctly, and user space code to call you with any combination of invalid arguments.

	Arnd <><
author
#3 lore raw

Re: [PATCH 1/2] Repost Cell OProfile: SPU mutex lock fix, version 4

In reply to Arnd Bergmann

Version 4 of the SPU mutex lock fix.

Updated to address Arnd's comments.

The issue is the SPU code is not holding the kernel mutex lock while adding samples to the kernel buffer.

This patch creates per SPU buffers to hold the data. Data is added to the buffers from in interrupt context. The data is periodically pushed to the kernel buffer via a new Oprofile function oprofile_put_buff(). The oprofile_put_buff() function is called via a work queue enabling the funtion to acquire the mutex lock.

The existing user controls for adjusting the per CPU buffer size is used to control the size of the per SPU buffers. Similarly, overflows of the SPU buffers are reported by incrementing the per CPU buffer stats. This eliminates the need to have architecture specific controls for the per SPU buffers which is not acceptable to the OProfile user tool maintainer.

The export of the oprofile add_event_entry() is removed as it is no longer needed given this patch.

Note, this patch has not addressed the issue of indexing arrays by the spu number. This still needs to be fixed as the spu numbering is not guarenteed to be 0 to max_num_spus-1.

drivers/oprofile/cpu_buffer.c +14 -1
Index: Cell_kernel_6_26_2008/drivers/oprofile/cpu_buffer.c
===================================================================
@@ -37,11 +37,24 @@ static int work_enabled;
 void free_cpu_buffers(void)
 {
 	int i;
- 
+
 	for_each_online_cpu(i)
 		vfree(per_cpu(cpu_buffer, i).buffer);
 }
 
+unsigned long oprofile_get_cpu_buffer_size(void)
+{
+	return fs_cpu_buffer_size;
+}
+
+void oprofile_cpu_buffer_inc_smpl_lost(void)
+{
+	struct oprofile_cpu_buffer *cpu_buf
+		= &__get_cpu_var(cpu_buffer);
+
+	cpu_buf->sample_lost_overflow++;
+}
+
 int alloc_cpu_buffers(void)
 {
 	int i;
include/linux/oprofile.h +9 -7
Index: Cell_kernel_6_26_2008/include/linux/oprofile.h
===================================================================
@@ -84,13 +84,6 @@ int oprofile_arch_init(struct oprofile_o
 void oprofile_arch_exit(void);
 
 /**
- * Add data to the event buffer.
- * The data passed is free-form, but typically consists of
- * file offsets, dcookies, context information, and ESCAPE codes.
- */
-void add_event_entry(unsigned long data);
-
-/**
  * Add a sample. This may be called from any context. Pass
  * smp_processor_id() as cpu.
  */
@@ -160,5 +153,14 @@ int oprofilefs_ulong_from_user(unsigned 
 
 /** lock for read/write safety */
 extern spinlock_t oprofilefs_lock;
+
+/**
+ * Add the contents of a circular buffer to the event buffer.
+ */
+void oprofile_put_buff(unsigned long *buf,unsigned int start,
+                       unsigned int stop, unsigned int max);
+
+unsigned long oprofile_get_cpu_buffer_size(void);
+void oprofile_cpu_buffer_inc_smpl_lost(void);
  
 #endif /* OPROFILE_H */
arch/powerpc/oprofile/cell/spu_profiler.c +2 -2
Index: Cell_kernel_6_26_2008/arch/powerpc/oprofile/cell/spu_profiler.c
===================================================================
@@ -23,12 +23,11 @@
 
 static u32 *samples;
 
-static int spu_prof_running;
+int spu_prof_running;
 static unsigned int profiling_interval;
 
 #define NUM_SPU_BITS_TRBUF 16
 #define SPUS_PER_TB_ENTRY   4
-#define SPUS_PER_NODE	     8
 
 #define SPU_PC_MASK	     0xFFFF
 
@@ -208,6 +207,7 @@ int start_spu_profiling(unsigned int cyc
 
 	spu_prof_running = 1;
 	hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
+	schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);
 
 	return 0;
 }
arch/powerpc/oprofile/cell/spu_task_sync.c +212 -26

300 lines not on this page · raw message

drivers/oprofile/event_buffer.h +7 -0
Index: Cell_kernel_6_26_2008/drivers/oprofile/event_buffer.h
===================================================================
@@ -17,6 +17,13 @@ int alloc_event_buffer(void);
 
 void free_event_buffer(void);
  
+/**
+ * Add data to the event buffer.
+ * The data passed is free-form, but typically consists of
+ * file offsets, dcookies, context information, and ESCAPE codes.
+ */
+void add_event_entry(unsigned long data);
+
 /* wake up the process sleeping on the event file */
 void wake_up_buffer_waiter(void);
 
arch/powerpc/oprofile/cell/pr_util.h +13 -0
Index: Cell_kernel_6_26_2008/arch/powerpc/oprofile/cell/pr_util.h
===================================================================
@@ -24,6 +24,11 @@
 #define SKIP_GENERIC_SYNC 0
 #define SYNC_START_ERROR -1
 #define DO_GENERIC_SYNC 1
+#define SPUS_PER_NODE   8
+#define DEFAULT_TIMER_EXPIRE  (HZ / 10)
+
+extern struct delayed_work spu_work;
+extern int spu_prof_running;
 
 struct spu_overlay_info {	/* map of sections within an SPU overlay */
 	unsigned int vma;	/* SPU virtual memory address from elf */
@@ -62,6 +67,14 @@ struct vma_to_fileoffset_map {	/* map of
 
 };
 
+struct spu_buffer {
+	int last_guard_val;
+	int ctx_sw_seen;
+	unsigned long *buff;
+        unsigned int head, tail;
+};
+
+
 /* The three functions below are for maintaining and accessing
  * the vma-to-fileoffset map.
  */
drivers/oprofile/buffer_sync.c +24 -0
Index: Cell_kernel_6_26_2008/drivers/oprofile/buffer_sync.c
===================================================================
@@ -551,3 +551,27 @@ void sync_buffer(int cpu)
 
 	mutex_unlock(&buffer_mutex);
 }
+
+/* The function can be used to add a buffer worth of data directly to
+ * the kernel buffer. The buffer is assumed to be a circular buffer.
+ * Take the entries from index start and end at index end, wrapping
+ * at max_entries.
+ */
+void oprofile_put_buff(unsigned long *buf, unsigned int start,
+		       unsigned int stop, unsigned int max)
+{
+	int i;
+
+	i = start;
+
+	mutex_lock(&buffer_mutex);
+	while (i != stop) {
+		add_event_entry(buf[i++]);
+
+		if (i >= max)
+			i = 0;
+	}
+
+	mutex_unlock(&buffer_mutex);
+}
+