aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorPeter Zijlstra <a.p.zijlstra@chello.nl>2009-03-30 19:07:05 +0200
committerIngo Molnar <mingo@elte.hu>2009-04-06 09:30:38 +0200
commit0a4a93919bdc5cee48fe4367591e8e0449c1086c (patch)
tree0f0d7ac3ee4f61c1e7e53f0ba4e3b01e0e4ac728 /kernel
parent195564390210977954fe4ef45b39cdee34f41b59 (diff)
perf_counter: executable mmap() information
Currently the profiling information returns userspace IPs but no way to correlate them to userspace code. Userspace could look into /proc/$pid/maps but that might not be current or even present anymore at the time of analyzing the IPs. Therefore provide means to track the mmap information and provide it in the output stream. XXX: only covers mmap()/munmap(), mremap() and mprotect() are missing. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Acked-by: Paul Mackerras <paulus@samba.org> Cc: Andrew Morton <akpm@linux-foundation.org> Orig-LKML-Reference: <20090330171023.417259499@chello.nl> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/perf_counter.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/kernel/perf_counter.c b/kernel/perf_counter.c
index c95e92329b9..f35e89e3d6a 100644
--- a/kernel/perf_counter.c
+++ b/kernel/perf_counter.c
@@ -25,6 +25,7 @@
#include <linux/anon_inodes.h>
#include <linux/kernel_stat.h>
#include <linux/perf_counter.h>
+#include <linux/dcache.h>
#include <asm/irq_regs.h>
@@ -1844,6 +1845,150 @@ void perf_counter_output(struct perf_counter *counter,
}
/*
+ * mmap tracking
+ */
+
+struct perf_mmap_event {
+ struct file *file;
+ char *file_name;
+ int file_size;
+
+ struct {
+ struct perf_event_header header;
+
+ u32 pid;
+ u32 tid;
+ u64 start;
+ u64 len;
+ u64 pgoff;
+ } event;
+};
+
+static void perf_counter_mmap_output(struct perf_counter *counter,
+ struct perf_mmap_event *mmap_event)
+{
+ struct perf_output_handle handle;
+ int size = mmap_event->event.header.size;
+ int ret = perf_output_begin(&handle, counter, size);
+
+ if (ret)
+ return;
+
+ perf_output_put(&handle, mmap_event->event);
+ perf_output_copy(&handle, mmap_event->file_name,
+ mmap_event->file_size);
+ perf_output_end(&handle, 0);
+}
+
+static int perf_counter_mmap_match(struct perf_counter *counter,
+ struct perf_mmap_event *mmap_event)
+{
+ if (counter->hw_event.mmap &&
+ mmap_event->event.header.type == PERF_EVENT_MMAP)
+ return 1;
+
+ if (counter->hw_event.munmap &&
+ mmap_event->event.header.type == PERF_EVENT_MUNMAP)
+ return 1;
+
+ return 0;
+}
+
+static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
+ struct perf_mmap_event *mmap_event)
+{
+ struct perf_counter *counter;
+
+ if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
+ return;
+
+ rcu_read_lock();
+ list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
+ if (perf_counter_mmap_match(counter, mmap_event))
+ perf_counter_mmap_output(counter, mmap_event);
+ }
+ rcu_read_unlock();
+}
+
+static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
+{
+ struct perf_cpu_context *cpuctx;
+ struct file *file = mmap_event->file;
+ unsigned int size;
+ char tmp[16];
+ char *buf = NULL;
+ char *name;
+
+ if (file) {
+ buf = kzalloc(PATH_MAX, GFP_KERNEL);
+ if (!buf) {
+ name = strncpy(tmp, "//enomem", sizeof(tmp));
+ goto got_name;
+ }
+ name = dentry_path(file->f_dentry, buf, PATH_MAX);
+ if (IS_ERR(name)) {
+ name = strncpy(tmp, "//toolong", sizeof(tmp));
+ goto got_name;
+ }
+ } else {
+ name = strncpy(tmp, "//anon", sizeof(tmp));
+ goto got_name;
+ }
+
+got_name:
+ size = ALIGN(strlen(name), sizeof(u64));
+
+ mmap_event->file_name = name;
+ mmap_event->file_size = size;
+
+ mmap_event->event.header.size = sizeof(mmap_event->event) + size;
+
+ cpuctx = &get_cpu_var(perf_cpu_context);
+ perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
+ put_cpu_var(perf_cpu_context);
+
+ perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
+
+ kfree(buf);
+}
+
+void perf_counter_mmap(unsigned long addr, unsigned long len,
+ unsigned long pgoff, struct file *file)
+{
+ struct perf_mmap_event mmap_event = {
+ .file = file,
+ .event = {
+ .header = { .type = PERF_EVENT_MMAP, },
+ .pid = current->group_leader->pid,
+ .tid = current->pid,
+ .start = addr,
+ .len = len,
+ .pgoff = pgoff,
+ },
+ };
+
+ perf_counter_mmap_event(&mmap_event);
+}
+
+void perf_counter_munmap(unsigned long addr, unsigned long len,
+ unsigned long pgoff, struct file *file)
+{
+ struct perf_mmap_event mmap_event = {
+ .file = file,
+ .event = {
+ .header = { .type = PERF_EVENT_MUNMAP, },
+ .pid = current->group_leader->pid,
+ .tid = current->pid,
+ .start = addr,
+ .len = len,
+ .pgoff = pgoff,
+ },
+ };
+
+ perf_counter_mmap_event(&mmap_event);
+}
+
+/*
* Generic software counter infrastructure
*/