From: Andi Kleen <andi@firstfloor.org>
To: greg@kroah.com
Cc: linux-kernel@vger.kernel.org, Andi Kleen <ak@linux.intel.com>
Subject: [PATCH 2/3] DEBUGFS: Add per cpu counters
Date: Tue, 13 Dec 2011 13:45:33 -0800 [thread overview]
Message-ID: <1323812733-7520-2-git-send-email-andi@firstfloor.org> (raw)
In-Reply-To: <1323812733-7520-1-git-send-email-andi@firstfloor.org>
From: Andi Kleen <ak@linux.intel.com>
Add a simple way to export per cpu counters in debugfs.
This is done using a section, so that they can be declared using
a simple macro with minimal typing.
OPEN: modules are not supported yet.
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
fs/debugfs/Makefile | 2 +-
fs/debugfs/counter.c | 73 +++++++++++++++++++++++++++++++++++++
include/asm-generic/vmlinux.lds.h | 6 +++
include/linux/debugfs.h | 27 ++++++++++++++
4 files changed, 107 insertions(+), 1 deletions(-)
create mode 100644 fs/debugfs/counter.c
diff --git a/fs/debugfs/Makefile b/fs/debugfs/Makefile
index 840c456..21be8d3 100644
--- a/fs/debugfs/Makefile
+++ b/fs/debugfs/Makefile
@@ -1,4 +1,4 @@
-debugfs-objs := inode.o file.o
+debugfs-objs := inode.o file.o counter.o
obj-$(CONFIG_DEBUG_FS) += debugfs.o
diff --git a/fs/debugfs/counter.c b/fs/debugfs/counter.c
new file mode 100644
index 0000000..2372faf
--- /dev/null
+++ b/fs/debugfs/counter.c
@@ -0,0 +1,73 @@
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
+#include <linux/percpu.h>
+#include <linux/kernel.h>
+
+/* OPEN: implement module support */
+
+extern struct debugfs_counter __start___debugfs[], __stop___debugfs[];
+
+static unsigned sum_counter(unsigned __percpu *counter)
+{
+ int cpu;
+ unsigned sum = 0;
+
+ for_each_online_cpu (cpu)
+ sum += per_cpu(*counter, cpu);
+ return sum;
+}
+
+static int
+dump_counters(struct seq_file *m,
+ struct debugfs_counter *cnt, struct debugfs_counter *stop,
+ char *fn)
+{
+ int n = 0;
+ for (; cnt < stop; cnt++)
+ if (fn[0] == cnt->fn[0] && !strcmp(cnt->fn, fn))
+ n += seq_printf(m, "%s: %u\n",
+ cnt->name, sum_counter(cnt->ptr));
+ return n;
+}
+
+static int show_debugfs_counter(struct seq_file *m, void *arg)
+{
+ int n;
+ n = dump_counters(m, __start___debugfs, __stop___debugfs, m->private);
+ return n;
+}
+
+static int debugfs_counter_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, show_debugfs_counter, inode->i_private);
+}
+
+static const struct file_operations debugfs_counter_fops = {
+ .open = debugfs_counter_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release
+};
+
+static void
+init_counters(struct debugfs_counter *cnt, struct debugfs_counter *stop)
+{
+ const char *last = NULL;
+
+ for (; cnt < stop; cnt++) {
+ if (cnt->fn == last)
+ continue;
+ last = cnt->fn;
+
+ /* Ignore error. Nothing we can do. */
+ debugfs_create_file(cnt->fn, 0444, NULL, (void *)cnt->fn,
+ &debugfs_counter_fops);
+ }
+}
+
+static int init_debugfs_counters(void)
+{
+ init_counters(__start___debugfs, __stop___debugfs);
+ return 0;
+}
+module_init(init_debugfs_counters);
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index b5e2e4c..7a54092 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -272,6 +272,12 @@
\
TRACEDATA \
\
+ __debugfs : AT(ADDR(__debugfs) - LOAD_OFFSET) { \
+ VMLINUX_SYMBOL(__start___debugfs) = .; \
+ *(__debugfs) \
+ VMLINUX_SYMBOL(__stop___debugfs) = .; \
+ } \
+ \
/* Kernel symbol table: Normal symbols */ \
__ksymtab : AT(ADDR(__ksymtab) - LOAD_OFFSET) { \
VMLINUX_SYMBOL(__start___ksymtab) = .; \
diff --git a/include/linux/debugfs.h b/include/linux/debugfs.h
index e7d9b20..a243b05 100644
--- a/include/linux/debugfs.h
+++ b/include/linux/debugfs.h
@@ -18,6 +18,7 @@
#include <linux/fs.h>
#include <linux/types.h>
+#include <linux/percpu.h>
struct file_operations;
@@ -76,6 +77,24 @@ struct dentry *debugfs_create_blob(const char *name, mode_t mode,
bool debugfs_initialized(void);
+struct debugfs_counter {
+ unsigned __percpu *ptr;
+ const char *fn;
+ const char *name;
+} __attribute__((aligned(sizeof(char *))));
+
+/* Note: static doesn't work unlike DEFINE_PERCPU. Sorry. */
+#define DEFINE_DEBUGFS_COUNTER(name_, file) \
+ DEFINE_PER_CPU(unsigned, name_ ## _counter); \
+ struct debugfs_counter name_ ## _pcpu_counter __used \
+ __attribute__((aligned(sizeof(char *)),section("__debugfs"),unused)) \
+ = { .ptr = &name_ ## _counter, .fn = file, .name = #name_ }; \
+
+#define DECLARE_DEBUGFS_COUNTER(name) \
+ struct debugfs_counter name ## _pcpu_counter
+
+#define debugfs_counter_inc(name) this_cpu_inc(name ## _counter)
+
#else
#include <linux/err.h>
@@ -193,6 +212,14 @@ static inline bool debugfs_initialized(void)
return false;
}
+
+struct debugfs_counter {
+};
+
+#define DEFINE_DEBUGFS_COUNTER(name, file)
+#define DECLARE_DEBUGFS_COUNTER(name)
+#define debugfs_counter_inc(name) do {} while(0)
+
#endif
#endif
--
1.7.7.3
next prev parent reply other threads:[~2011-12-13 21:45 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-13 21:45 [PATCH 1/3] DEBUGFS: Automatically create parents for debugfs files v2 Andi Kleen
2011-12-13 21:45 ` Andi Kleen [this message]
2011-12-13 22:43 ` [PATCH 2/3] DEBUGFS: Add per cpu counters Thomas Gleixner
2011-12-13 22:56 ` Andi Kleen
2011-12-13 23:08 ` Steven Rostedt
2011-12-13 23:15 ` Mathieu Desnoyers
2011-12-13 23:16 ` Steven Rostedt
2011-12-13 23:17 ` Thomas Gleixner
2011-12-13 22:22 ` [PATCH 1/3] DEBUGFS: Automatically create parents for debugfs files v2 Cyrill Gorcunov
2011-12-13 22:27 ` Andi Kleen
2011-12-13 23:03 ` Thomas Gleixner
2011-12-13 23:11 ` Andi Kleen
2011-12-13 23:16 ` Thomas Gleixner
-- strict thread matches above, loose matches on Subject: below --
2011-12-02 20:02 Andi Kleen
2011-12-02 20:02 ` [PATCH 2/3] DEBUGFS: Add per cpu counters Andi Kleen
2011-12-12 22:20 ` Greg KH
2011-12-02 18:43 Add simple way to add statistic counters to debugfs Andi Kleen
2011-12-02 18:43 ` [PATCH 2/3] DEBUGFS: Add per cpu counters Andi Kleen
2011-12-06 11:44 ` Wu Fengguang
2011-12-06 17:17 ` Andi Kleen
2011-12-09 3:00 ` Wu Fengguang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1323812733-7520-2-git-send-email-andi@firstfloor.org \
--to=andi@firstfloor.org \
--cc=ak@linux.intel.com \
--cc=greg@kroah.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.