Skip to contentSkip to conversation

oprofile: arch-independent code for stack trace sampling

PATCH In review Greg Banks 3 participants 4 messages Nov 9, 2004

lore
Share & tools
lore

Greg Banks

author
#0 lore raw

PATCH 2/11 oprofile: arch-independent code for stack trace sampling

Signature · 3 lines
-- 
Greg Banks, R&D Software Engineer, SGI Australian Software Group.
I don't speak for SGI.

oprofile arch-independent updates, including some internal API changes and support for stack trace sampling.

---

 drivers/oprofile/buffer_sync.c    |   57 ++++++++++++---
 drivers/oprofile/cpu_buffer.c     |  141 ++++++++++++++++++++++++++++++--------
 drivers/oprofile/cpu_buffer.h     |    6 +
 drivers/oprofile/event_buffer.h   |    2 
 drivers/oprofile/oprof.c          |   74 +++++++++++--------
 drivers/oprofile/oprof.h          |    5 +
 drivers/oprofile/oprofile_files.c |   42 +++++++++--
 drivers/oprofile/oprofile_stats.c |    4 +
 drivers/oprofile/oprofile_stats.h |    1 
 drivers/oprofile/timer_int.c      |   19 +----
 include/linux/oprofile.h          |   20 ++++-
 11 files changed, 279 insertions(+), 92 deletions(-)
drivers/oprofile/oprofile_files.c +37 -5
Index: linux/drivers/oprofile/oprofile_files.c
===================================================================
@@ -18,6 +18,37 @@ unsigned long fs_buffer_size = 131072;
 unsigned long fs_cpu_buffer_size = 8192;
 unsigned long fs_buffer_watershed = 32768; /* FIXME: tune */
 
+static ssize_t depth_read(struct file * file, char * buf, size_t count, loff_t * offset)
+{
+	return oprofilefs_ulong_to_user(backtrace_depth, buf, count, offset);
+}
+
+ 
+static ssize_t depth_write(struct file * file, char const * buf, size_t count, loff_t * offset)
+{
+	unsigned long val;
+	int retval;
+
+	if (*offset)
+		return -EINVAL;
+
+	retval = oprofilefs_ulong_from_user(&val, buf, count);
+	if (retval)
+		return retval;
+  
+	retval = oprofile_set_backtrace(val);
+
+	if (retval)
+		return retval;
+	return count;
+}
+
+
+static struct file_operations depth_fops = {
+	.read		= depth_read,
+	.write		= depth_write
+};
+
  
 static ssize_t pointer_size_read(struct file * file, char __user * buf, size_t count, loff_t * offset)
 {
@@ -32,7 +63,7 @@ static struct file_operations pointer_si
 
 static ssize_t cpu_type_read(struct file * file, char __user * buf, size_t count, loff_t * offset)
 {
-	return oprofilefs_str_to_user(oprofile_ops->cpu_type, buf, count, offset);
+	return oprofilefs_str_to_user(oprofile_ops.cpu_type, buf, count, offset);
 }
  
  
@@ -47,7 +78,7 @@ static ssize_t enable_read(struct file *
 }
 
 
-static ssize_t enable_write(struct file *file, char const __user * buf, size_t count, loff_t * offset)
+static ssize_t enable_write(struct file * file, char const __user * buf, size_t count, loff_t * offset)
 {
 	unsigned long val;
 	int retval;
@@ -76,7 +107,7 @@ static struct file_operations enable_fop
 };
 
 
-static ssize_t dump_write(struct file *file, char const __user * buf, size_t count, loff_t * offset)
+static ssize_t dump_write(struct file * file, char const __user * buf, size_t count, loff_t * offset)
 {
 	wake_up_buffer_waiter();
 	return count;
@@ -96,8 +127,9 @@ void oprofile_create_files(struct super_
 	oprofilefs_create_ulong(sb, root, "buffer_watershed", &fs_buffer_watershed);
 	oprofilefs_create_ulong(sb, root, "cpu_buffer_size", &fs_cpu_buffer_size);
 	oprofilefs_create_file(sb, root, "cpu_type", &cpu_type_fops); 
+	oprofilefs_create_file(sb, root, "backtrace_depth", &depth_fops);
 	oprofilefs_create_file(sb, root, "pointer_size", &pointer_size_fops);
 	oprofile_create_stats_files(sb, root);
-	if (oprofile_ops->create_files)
-		oprofile_ops->create_files(sb, root);
+	if (oprofile_ops.create_files)
+		oprofile_ops.create_files(sb, root);
 }
drivers/oprofile/oprofile_stats.h +1 -0
Index: linux/drivers/oprofile/oprofile_stats.h
===================================================================
@@ -15,6 +15,7 @@
 struct oprofile_stat_struct {
 	atomic_t sample_lost_no_mm;
 	atomic_t sample_lost_no_mapping;
+	atomic_t bt_lost_no_mapping;
 	atomic_t event_lost_overflow;
 };
 
include/linux/oprofile.h +15 -5
Index: linux/include/linux/oprofile.h
===================================================================
@@ -20,6 +20,7 @@
 struct super_block;
 struct dentry;
 struct file_operations;
+struct pt_regs;
  
 /* Operations structure to be filled in */
 struct oprofile_operations {
@@ -34,6 +35,8 @@ struct oprofile_operations {
 	int (*start)(void);
 	/* Stop delivering interrupts. */
 	void (*stop)(void);
+	/* Initiate a stack backtrace. Optional. */
+	void (*backtrace)(struct pt_regs * const regs, unsigned int depth);
 	/* CPU identification string. */
 	char * cpu_type;
 };
@@ -41,11 +44,11 @@ struct oprofile_operations {
 /**
  * One-time initialisation. *ops must be set to a filled-in
  * operations structure. This is called even in timer interrupt
- * mode.
+ * mode so an arch can set a backtrace callback.
  *
- * Return 0 on success.
+ * If an error occurs, the fields should be left untouched.
  */
-int oprofile_arch_init(struct oprofile_operations ** ops);
+void oprofile_arch_init(struct oprofile_operations * ops);
  
 /**
  * One-time exit/cleanup for the arch.
@@ -56,8 +59,15 @@ void oprofile_arch_exit(void);
  * Add a sample. This may be called from any context. Pass
  * smp_processor_id() as cpu.
  */
-extern void oprofile_add_sample(unsigned long eip, unsigned int is_kernel, 
-	unsigned long event, int cpu);
+void oprofile_add_sample(struct pt_regs * const regs, unsigned long event);
+
+/* Use this instead when the PC value is not from the regs. Doesn't
+ * backtrace. */
+void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event);
+
+/* add a backtrace entry, to be called from the ->backtrace callback */
+void oprofile_add_trace(unsigned long eip);
+
 
 /**
  * Create a file of the given name as a child of the given root, with
drivers/oprofile/cpu_buffer.c +115 -26

210 lines not on this page · raw message

drivers/oprofile/oprof.c +44 -30

127 lines not on this page · raw message

drivers/oprofile/cpu_buffer.h +6 -0
Index: linux/drivers/oprofile/cpu_buffer.h
===================================================================
@@ -37,9 +37,11 @@ struct oprofile_cpu_buffer {
 	unsigned long buffer_size;
 	struct task_struct * last_task;
 	int last_is_kernel;
+	int tracing;
 	struct op_sample * buffer;
 	unsigned long sample_received;
 	unsigned long sample_lost_overflow;
+	unsigned long backtrace_aborted;
 	int cpu;
 	struct work_struct work;
 } ____cacheline_aligned;
@@ -48,4 +50,8 @@ extern struct oprofile_cpu_buffer cpu_bu
 
 void cpu_buffer_reset(struct oprofile_cpu_buffer * cpu_buf);
 
+/* transient events for the CPU buffer -> event buffer */
+#define CPU_IS_KERNEL 1
+#define CPU_TRACE_BEGIN 2
+
 #endif /* OPROFILE_CPU_BUFFER_H */
drivers/oprofile/buffer_sync.c +46 -11

136 lines not on this page · raw message

drivers/oprofile/timer_int.c +5 -14
Index: linux/drivers/oprofile/timer_int.c
===================================================================
@@ -17,10 +17,7 @@
  
 static int timer_notify(struct pt_regs *regs)
 {
-	int cpu = smp_processor_id();
-	unsigned long eip = profile_pc(regs);
- 
-	oprofile_add_sample(eip, !user_mode(regs), 0, cpu);
+ 	oprofile_add_sample(regs, 0);
 	return 0;
 }
 
@@ -36,15 +33,9 @@ static void timer_stop(void)
 }
 
 
-static struct oprofile_operations timer_ops = {
-	.start	= timer_start,
-	.stop	= timer_stop,
-	.cpu_type = "timer"
-};
-
- 
-void __init timer_init(struct oprofile_operations ** ops)
+void __init timer_init(struct oprofile_operations * ops)
 {
-	*ops = &timer_ops;
-	printk(KERN_INFO "oprofile: using timer interrupt.\n");
+	ops->start = timer_start;
+	ops->stop = timer_stop;
+	ops->cpu_type = "timer";
 }
drivers/oprofile/oprofile_stats.c +4 -0
Index: linux/drivers/oprofile/oprofile_stats.c
===================================================================
@@ -59,6 +59,8 @@ void oprofile_create_stats_files(struct 
 			&cpu_buf->sample_received);
 		oprofilefs_create_ro_ulong(sb, cpudir, "sample_lost_overflow",
 			&cpu_buf->sample_lost_overflow);
+		oprofilefs_create_ro_ulong(sb, cpudir, "backtrace_aborted",
+			&cpu_buf->backtrace_aborted);
 	}
  
 	oprofilefs_create_ro_atomic(sb, dir, "sample_lost_no_mm",
@@ -67,4 +69,6 @@ void oprofile_create_stats_files(struct 
 		&oprofile_stats.sample_lost_no_mapping);
 	oprofilefs_create_ro_atomic(sb, dir, "event_lost_overflow",
 		&oprofile_stats.event_lost_overflow);
+	oprofilefs_create_ro_atomic(sb, dir, "bt_lost_no_mapping",
+		&oprofile_stats.bt_lost_no_mapping);
 }
drivers/oprofile/event_buffer.h +2 -0
Index: linux/drivers/oprofile/event_buffer.h
===================================================================
@@ -32,6 +32,8 @@ void wake_up_buffer_waiter(void);
 #define KERNEL_EXIT_SWITCH_CODE		5
 #define MODULE_LOADED_CODE		6
 #define CTX_TGID_CODE			7
+#define TRACE_BEGIN_CODE		8
+#define TRACE_END_CODE			9
  
 /* add data to the event buffer */
 void add_event_entry(unsigned long data);
drivers/oprofile/oprof.h +4 -1
Index: linux/drivers/oprofile/oprof.h
===================================================================
@@ -24,12 +24,15 @@ struct oprofile_operations;
 extern unsigned long fs_buffer_size;
 extern unsigned long fs_cpu_buffer_size;
 extern unsigned long fs_buffer_watershed;
-extern struct oprofile_operations * oprofile_ops;
+extern struct oprofile_operations oprofile_ops;
 extern unsigned long oprofile_started;
+extern unsigned long backtrace_depth;
  
 struct super_block;
 struct dentry;
 
 void oprofile_create_files(struct super_block * sb, struct dentry * root);
+
+int oprofile_set_backtrace(unsigned long depth);
  
 #endif /* OPROF_H */
#1 lore raw

Review

Quoted 2 lines · Greg Banks
(quoted diff) +1 -0
+	struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];

oprofile is currently doing suspicious things with smp_processor_id() in premptible reasons. Is this patch compounding things?

author
#2 lore raw
Quoted 6 lines · Andrew Morton

Review

Quoted 2 lines · Greg Banks
(quoted diff) +1 -0
+	struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];

oprofile is currently doing suspicious things with smp_processor_id() in premptible reasons. Is this patch compounding things?

It's not changing the contexts where smp_processor_id() is called, just pushing it down one level from a bunch of interrupt handlers to the 2 oprofile sampling functions they call. If it was busted before it's no more nor less busted now.

I presume the perceived problem is that with CONFIG_PREEMPT=y the thread can be pre-empted onto another CPU? If it makes everyone happier I can sprinkle a few preempt_disable()s around, but I'd prefer to do it in a subsequent patch rather than respin this.

Greg.

Signature · 2 lines
Greg Banks, R&D Software Engineer, SGI Australian Software Group.
I don't speak for SGI.
#3 lore raw
Quoted 8 lines · Greg Banks
Quoted 2 lines

oprofile is currently doing suspicious things with smp_processor_id() in premptible reasons. Is this patch compounding things?

It's not changing the contexts where smp_processor_id() is called, just pushing it down one level from a bunch of interrupt handlers to the 2 oprofile sampling functions they call. If it was busted before it's no more nor less busted now.

Quoted 4 lines

I presume the perceived problem is that with CONFIG_PREEMPT=y the thread can be pre-empted onto another CPU? If it makes everyone happier I can sprinkle a few preempt_disable()s around, but I'd prefer to do it in a subsequent patch rather than respin this.

Andrew: basically the warning is false, there is no bug in this code.

we don't want to use preempt_disable(). Instead we want some way to get a CPU ID and then carry on in pre-emptible fashion. It's only used to index into an array, and if we get pre-empted onto another CPU it's not a major deal.

(Yes, this breaks with CPU hotplug, but so does the rest of OProfile and I've yet to see a sensible API for handling this, that is a ctor/dtor style API)

regards
john