[PATCH] snprintf_batch for sequence printing in /proc/stat

From: KAMEZAWA Hiroyuki
Date: Thu Jan 26 2012 - 04:57:19 EST


In /proc/stat, a sequence of numbers are printed out under the same
format. Reading /proc/stat consumes 20% of cpu just for decoding the format.
This patch adds a decode-once + print a sequence function for helping
/proc/stat.

New (special) function is snprintf_batch().

for example, Following is a function to print 0..10 in a sequence
to given buffer.

unsigned long long my_next_val(void *iter, bool *last)
{
int x = *(int *)iter;
if (x == 11) {
last = true;
return 0;
}
*(int *)iter += 1;
return x;
}
{
...
int iter = 0;
snprintf_batch(buf, size, " %d", &iter, my_next_val);
}

Using this will reduce the cost for format decoding and
make /proc/stat faster, which shows tons of numbers in ascii..

With a python script which reads /proc/stat 1000 times.

* Before Patch
[kamezawa@bluextal test]$ time ./stat_check.py
real 0m0.154s
user 0m0.020s
sys 0m0.130s

* After Patch
[kamezawa@bluextal test]$ time ./stat_check.py

real 0m0.080s
user 0m0.029s
sys 0m0.048s

In my environ, size of /proc/stat is 2700 bytes.

Above score shows that patched one is almost twice fast....but in
general, with shell-scripting, fork/exec/mmap etc...cost is much
larger than cost of reading /proc/stat, benefit for usual users is doubtful

If an admin or a program check /proc/stat periodically with his script,
this improvement will give them better latency.

Note: the added snprinf_batch() can be used for other places as
/proc/<pid>/stat but I'm not sure whether this simple
iterator interface is enough or not.

Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
---
fs/proc/stat.c | 16 +++++++-
fs/seq_file.c | 18 +++++++++
include/linux/kernel.h | 4 ++
include/linux/seq_file.h | 3 +
lib/vsprintf.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 131 insertions(+), 2 deletions(-)

diff --git a/fs/proc/stat.c b/fs/proc/stat.c
index 121f77c..0687d45 100644
--- a/fs/proc/stat.c
+++ b/fs/proc/stat.c
@@ -49,6 +49,18 @@ static u64 get_iowait_time(int cpu)
return iowait;
}

+static unsigned long long get_next_kstat_irq(void *iter, bool *last)
+{
+ int pos = *(int *)iter;
+
+ if (pos == nr_irqs) {
+ *last = true;
+ return 0;
+ }
+ *(int *)iter = pos + 1;
+ return kstat_irqs(pos);
+}
+
static int show_stat(struct seq_file *p, void *v)
{
int i, j;
@@ -131,8 +143,8 @@ static int show_stat(struct seq_file *p, void *v)
seq_printf(p, "intr %llu", (unsigned long long)sum);

/* sum again ? it could be updated? */
- for_each_irq_nr(j)
- seq_printf(p, " %u", kstat_irqs(j));
+ j = 0;
+ seq_printnum_batch(p, " %u", &j, get_next_kstat_irq);

seq_printf(p,
"\nctxt %llu\n"
diff --git a/fs/seq_file.c b/fs/seq_file.c
index 4023d6b..fa367ce 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -386,6 +386,24 @@ int seq_printf(struct seq_file *m, const char *f, ...)
}
EXPORT_SYMBOL(seq_printf);

+int seq_printnum_batch(struct seq_file *m, const char *f, void *iter,
+ unsigned long long (*next_val)(void *, bool *))
+{
+ int len;
+
+ if (m->count < m->size) {
+ len = snprintf_batch(m->buf + m->count,
+ m->size - m->count, f, iter, next_val);
+ if (m->count + len < m->size) {
+ m->count += len;
+ return 0;
+ }
+ }
+ m->count = m->size;
+ return -1;
+}
+EXPORT_SYMBOL_GPL(seq_printnum_batch);
+
/**
* mangle_path - mangle and copy path to buffer beginning
* @s: buffer start
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index e834342..7306174 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -320,6 +320,10 @@ extern int sscanf(const char *, const char *, ...)
extern int vsscanf(const char *, const char *, va_list)
__attribute__ ((format (scanf, 2, 0)));

+/* special function for printing sequence values */
+extern int snprintf_batch(char *buf, size_t size, const char *fmt,
+ void *iter, unsigned long long (*next_val)(void *, bool *));
+
extern int get_option(char **str, int *pint);
extern char *get_options(const char *str, int nints, int *ints);
extern unsigned long long memparse(const char *ptr, char **retptr);
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index 44f1514..5ac464a 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -86,6 +86,9 @@ int seq_write(struct seq_file *seq, const void *data, size_t len);

__printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);

+int seq_printnum_batch(struct seq_file *seq, const char *fmt, void *iter,
+ unsigned long long (*next_val)(void *, bool *));
+
int seq_path(struct seq_file *, const struct path *, const char *);
int seq_dentry(struct seq_file *, struct dentry *, const char *);
int seq_path_root(struct seq_file *m, const struct path *path,
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 8e75003..03e45cc 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -2038,3 +2038,95 @@ int sscanf(const char *buf, const char *fmt, ...)
return i;
}
EXPORT_SYMBOL(sscanf);
+
+/*
+ * snprintf_batch - format a string with a give array and spacer
+ * @buf : The buffer to place the result info
+ * @size: The size of the buffer, including the trailing null space
+ * @fmt : The format for printing each numbers.(some limitations below)
+ * @iter:The array to be printed
+ * @array_size : the number of entries in array
+ *
+ * This function is for printing values given by a given function next_val()
+ * in a given format until next_val() finishes. Unlike printf, available
+ * format is very limited. The allowed fmt for this function is
+ * "a%?b".
+ * - a is a one charactor spacer printed before the value.
+ * - b is a one charactor spacer printed after the value
+ * - %? is an integer value. %d, %u, %x + other controls as 'h/hh/l/ll etc
+ * are available. But you can specify precision and field_width as argument.
+ *
+ * This function is useful when you need to print a sequence of values of
+ * the same type. This function stops when next_val() returns 'true' in
+ * the second argument.
+ *
+ * This function is originally designed for /proc/stat..
+ */
+
+int snprintf_batch(char *buf, size_t size, const char *fmt, void *iter,
+ unsigned long long (*next_val)(void *, bool *))
+{
+ char *str, *end;
+ struct printf_spec spec = {0};
+ unsigned long long num;
+ bool last = false;
+ char head, foot;
+ bool easy_zero_printing = false;
+ int read;
+
+ if (WARN_ON_ONCE((int) size < 0))
+ return 0;
+
+ str = buf;
+ end = buf + size;
+ /* Make sure end is always >= buf */
+ if (end < buf) {
+ end = ((void *)-1);
+ size = end - buf;
+ }
+
+ head = foot = 0;
+
+ if (*fmt != '%') {
+ head = *fmt;
+ fmt++;
+ }
+ /* decode happens only once */
+ read = format_decode(fmt, &spec);
+ fmt += read;
+ if (*fmt)
+ foot = *fmt;
+ /* if fmt is simple, we can show '0' easily */
+ if (spec.precision == -1 &&
+ spec.field_width == -1 &&
+ !(spec.flags & (ZEROPAD + PLUS + SPACE + SMALL + SPECIAL)))
+ easy_zero_printing = true;
+
+ while (str < end) {
+ num = next_val(iter, &last);
+ if (last)
+ break;
+ if (str < end && head != 0) {
+ *str = head;
+ str++;
+ }
+ /* optimization for /proc/stat */
+ if (num == 0 && easy_zero_printing) {
+ *str = '0';
+ str++;
+ } else
+ str = number(str, end, num, spec);
+ if (str < end && foot != 0) {
+ *str = foot;
+ str++;
+ }
+ }
+ if (size > 0) {
+ if (str < end)
+ *str = '0';
+ else
+ end[-1] = '0';
+ }
+ return str - buf;
+}
+EXPORT_SYMBOL_GPL(snprintf_batch);
--
1.7.4.1




--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/