linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [RFC][PATCH 1/4] break up string_get_size()
@ 2011-09-30 18:02 Dave Hansen
  2011-09-30 18:02 ` [RFC][PATCH 2/4] break out unit selection from string_get_size() Dave Hansen
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Dave Hansen @ 2011-09-30 18:02 UTC (permalink / raw)
  To: linux-mm; +Cc: linux-kernel, rientjes, James.Bottomley, hpa, Dave Hansen


I started to break up string_get_size() in to some pieces
so that I could pick and choose which bits I wanted.  I
ended up not re-using this function.  But, I think this
still stands by itself since it makes the code much more
self-explanatory.

Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>
---

 linux-2.6.git-dave/lib/string_helpers.c |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff -puN lib/string_helpers.c~break-up-string_get_size lib/string_helpers.c
--- linux-2.6.git/lib/string_helpers.c~break-up-string_get_size	2011-09-30 10:58:32.909418688 -0700
+++ linux-2.6.git-dave/lib/string_helpers.c	2011-09-30 10:58:32.953418603 -0700
@@ -8,6 +8,18 @@
 #include <linux/module.h>
 #include <linux/string_helpers.h>
 
+static int calc_sf_digit_room(u64 size)
+{
+	u64 sf_cap;
+	int digits;
+
+	sf_cap = size;
+	for (digits = 0; sf_cap*10 < 1000; digits++)
+		sf_cap *= 10;
+
+	return digits;
+}
+
 /**
  * string_get_size - get the size in the specified units
  * @size:	The size to be converted
@@ -36,7 +48,7 @@ int string_get_size(u64 size, const enum
 		[STRING_UNITS_2] = 1024,
 	};
 	int i, j;
-	u64 remainder = 0, sf_cap;
+	u64 remainder = 0;
 	char tmp[8];
 
 	tmp[0] = '\0';
@@ -47,10 +59,7 @@ int string_get_size(u64 size, const enum
 			i++;
 		}
 
-		sf_cap = size;
-		for (j = 0; sf_cap*10 < 1000; j++)
-			sf_cap *= 10;
-
+		j = calc_sf_digit_room(size);
 		if (j) {
 			remainder *= 1000;
 			do_div(remainder, divisor[units]);
_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC][PATCH 2/4] break out unit selection from string_get_size()
  2011-09-30 18:02 [RFC][PATCH 1/4] break up string_get_size() Dave Hansen
@ 2011-09-30 18:02 ` Dave Hansen
  2011-09-30 18:54   ` H. Peter Anvin
  2011-09-30 18:02 ` [RFC][PATCH 3/4] add seq_print_size() function Dave Hansen
  2011-09-30 18:02 ` [RFC][PATCH 4/4] show page size in /proc/$pid/numa_maps Dave Hansen
  2 siblings, 1 reply; 5+ messages in thread
From: Dave Hansen @ 2011-09-30 18:02 UTC (permalink / raw)
  To: linux-mm; +Cc: linux-kernel, rientjes, James.Bottomley, hpa, Dave Hansen


string_get_size() can really only print things in a single
format.  You're always stuck with a space, and it will
always zero-pad the decimal places:

	4.00 KiB
	40.0 KiB
	400 KiB

Printing page sizes in decimal KiB does not make much sense
since they are always nice powers of two.  But,
string_get_size() does have some nice code for selecting
the right units and doing the division.

This breaks that nice code out so that we can reuse it.
find_size_units() is a bit of a funky function since it has
so many outputs.  I don't think it's _too_ crazy though.

Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>
---

 linux-2.6.git-dave/include/linux/string_helpers.h |    3 
 linux-2.6.git-dave/lib/string_helpers.c           |   67 +++++++++++++++-------
 2 files changed, 50 insertions(+), 20 deletions(-)

diff -puN include/linux/string_helpers.h~break-up-string_get_size-1 include/linux/string_helpers.h
--- linux-2.6.git/include/linux/string_helpers.h~break-up-string_get_size-1	2011-09-30 10:47:14.794753824 -0700
+++ linux-2.6.git-dave/include/linux/string_helpers.h	2011-09-30 10:47:14.806753800 -0700
@@ -10,6 +10,9 @@ enum string_size_units {
 	STRING_UNITS_2,		/* use binary powers of 2^10 */
 };
 
+u64 find_size_units(u64 *size, const enum string_size_units units,
+		const char **unit_str);
+
 int string_get_size(u64 size, enum string_size_units units,
 		    char *buf, int len);
 
diff -puN lib/string_helpers.c~break-up-string_get_size-1 lib/string_helpers.c
--- linux-2.6.git/lib/string_helpers.c~break-up-string_get_size-1	2011-09-30 10:47:14.798753816 -0700
+++ linux-2.6.git-dave/lib/string_helpers.c	2011-09-30 10:47:14.806753800 -0700
@@ -20,6 +20,49 @@ static int calc_sf_digit_room(u64 size)
 	return digits;
 }
 
+static const char *units_10[] = { "B", "kB", "MB", "GB", "TB", "PB",
+				   "EB", "ZB", "YB", NULL};
+static const char *units_2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB",
+				"EiB", "ZiB", "YiB", NULL };
+
+static const char **units_str[] = {
+		[STRING_UNITS_10] =  units_10,
+		[STRING_UNITS_2] = units_2,
+};
+static const unsigned int divisor[] = {
+		[STRING_UNITS_10] = 1000,
+		[STRING_UNITS_2] = 1024,
+};
+
+/**
+ * string_get_size - helper function for printing numbers
+ * @size:	number to be printed (modified)
+ * @units:	units to use (powers of 1000 or 1024)
+ * @unit_str:	unit is returned in this string
+ * returns:	remainder from divisions
+ *
+ * The goal here is to return a number in @size which has
+ * only 3 significant figures, along with the unit pointed
+ * to by unit_str which is necessary to make that happen.
+ *
+ * We return the remainder so that the caller does not have
+ * to repeat the division operations themselves.
+ */
+u64 find_size_units(u64 *size, const enum string_size_units units,
+		const char **unit_str)
+{
+	u64 remainder = 0;
+	int i = 0;
+
+	while (*size >= divisor[units] && units_str[units][i]) {
+		remainder = do_div(*size, divisor[units]);
+		i++;
+	}
+	*unit_str = units_str[units][i];
+	return remainder;
+}
+EXPORT_SYMBOL(find_size_units);
+
 /**
  * string_get_size - get the size in the specified units
  * @size:	The size to be converted
@@ -35,30 +78,14 @@ static int calc_sf_digit_room(u64 size)
 int string_get_size(u64 size, const enum string_size_units units,
 		    char *buf, int len)
 {
-	const char *units_10[] = { "B", "kB", "MB", "GB", "TB", "PB",
-				   "EB", "ZB", "YB", NULL};
-	const char *units_2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB",
-				 "EiB", "ZiB", "YiB", NULL };
-	const char **units_str[] = {
-		[STRING_UNITS_10] =  units_10,
-		[STRING_UNITS_2] = units_2,
-	};
-	const unsigned int divisor[] = {
-		[STRING_UNITS_10] = 1000,
-		[STRING_UNITS_2] = 1024,
-	};
-	int i, j;
+	int j;
 	u64 remainder = 0;
 	char tmp[8];
+	const char *unit_str = "";
 
 	tmp[0] = '\0';
-	i = 0;
 	if (size >= divisor[units]) {
-		while (size >= divisor[units] && units_str[units][i]) {
-			remainder = do_div(size, divisor[units]);
-			i++;
-		}
-
+		remainder = find_size_units(&size, units, &unit_str);
 		j = calc_sf_digit_room(size);
 		if (j) {
 			remainder *= 1000;
@@ -70,7 +97,7 @@ int string_get_size(u64 size, const enum
 	}
 
 	snprintf(buf, len, "%lld%s %s", (unsigned long long)size,
-		 tmp, units_str[units][i]);
+		 tmp, unit_str);
 
 	return 0;
 }
_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC][PATCH 3/4] add seq_print_size() function
  2011-09-30 18:02 [RFC][PATCH 1/4] break up string_get_size() Dave Hansen
  2011-09-30 18:02 ` [RFC][PATCH 2/4] break out unit selection from string_get_size() Dave Hansen
@ 2011-09-30 18:02 ` Dave Hansen
  2011-09-30 18:02 ` [RFC][PATCH 4/4] show page size in /proc/$pid/numa_maps Dave Hansen
  2 siblings, 0 replies; 5+ messages in thread
From: Dave Hansen @ 2011-09-30 18:02 UTC (permalink / raw)
  To: linux-mm; +Cc: linux-kernel, rientjes, James.Bottomley, hpa, Dave Hansen


In order to get nice, human-readable output, we are going to
use MiB/KiB, etc... in numa_maps.  Introduce a helper to do
the conversion from a raw integer over to a string.

I thought about doing this as a new printk() format specifier.
That would be interesting, but it's hard to argue with this
since it's so short and sweet.

Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>
---

 linux-2.6.git-dave/fs/seq_file.c            |   15 +++++++++++++++
 linux-2.6.git-dave/include/linux/seq_file.h |    3 +++
 2 files changed, 18 insertions(+)

diff -puN fs/seq_file.c~add-seq_print_size fs/seq_file.c
--- linux-2.6.git/fs/seq_file.c~add-seq_print_size	2011-09-30 10:52:22.302140723 -0700
+++ linux-2.6.git-dave/fs/seq_file.c	2011-09-30 10:52:22.322140685 -0700
@@ -386,6 +386,21 @@ int seq_printf(struct seq_file *m, const
 }
 EXPORT_SYMBOL(seq_printf);
 
+/*
+ * Prints output with MiB/MB/KB/etc... suffixes
+ */
+int seq_print_size(struct seq_file *seq, u64 size,
+			const enum string_size_units units)
+{
+	int ret;
+	u64 remainder;
+	char *size_str;
+	remainder = find_size_units(&size, units, &size_str);
+	ret += seq_printf(seq, "%llu", size);
+	ret += seq_puts(seq, size_str);
+	return ret;
+}
+
 /**
  *	mangle_path -	mangle and copy path to buffer beginning
  *	@s: buffer start
diff -puN include/linux/seq_file.h~add-seq_print_size include/linux/seq_file.h
--- linux-2.6.git/include/linux/seq_file.h~add-seq_print_size	2011-09-30 10:52:22.314140700 -0700
+++ linux-2.6.git-dave/include/linux/seq_file.h	2011-09-30 10:52:22.322140685 -0700
@@ -3,6 +3,7 @@
 
 #include <linux/types.h>
 #include <linux/string.h>
+#include <linux/string_helpers.h>
 #include <linux/mutex.h>
 #include <linux/cpumask.h>
 #include <linux/nodemask.h>
@@ -83,6 +84,8 @@ int seq_escape(struct seq_file *, const 
 int seq_putc(struct seq_file *m, char c);
 int seq_puts(struct seq_file *m, const char *s);
 int seq_write(struct seq_file *seq, const void *data, size_t len);
+int seq_print_size(struct seq_file *seq, u64 size,
+			const enum string_size_units units);
 
 int seq_printf(struct seq_file *, const char *, ...)
 	__attribute__ ((format (printf,2,3)));
_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC][PATCH 4/4] show page size in /proc/$pid/numa_maps
  2011-09-30 18:02 [RFC][PATCH 1/4] break up string_get_size() Dave Hansen
  2011-09-30 18:02 ` [RFC][PATCH 2/4] break out unit selection from string_get_size() Dave Hansen
  2011-09-30 18:02 ` [RFC][PATCH 3/4] add seq_print_size() function Dave Hansen
@ 2011-09-30 18:02 ` Dave Hansen
  2 siblings, 0 replies; 5+ messages in thread
From: Dave Hansen @ 2011-09-30 18:02 UTC (permalink / raw)
  To: linux-mm; +Cc: linux-kernel, rientjes, James.Bottomley, hpa, Dave Hansen


The output of /proc/$pid/numa_maps is in terms of number of pages
like anon=22 or dirty=54.  Here's some output:

7f4680000000 default file=/hugetlb/bigfile anon=50 dirty=50 N0=50
7f7659600000 default file=/anon_hugepage\040(deleted) anon=50 dirty=50 N0=50
7fff8d425000 default stack anon=50 dirty=50 N0=50

Looks like we have a stack and a couple of anonymous hugetlbfs
areas page which both use the same amount of memory.  They don't.

The 'bigfile' uses 1GB pages and takes up ~50GB of space.  The
anon_hugepage uses 2MB pages and takes up ~100MB of space while
the stack uses normal 4k pages.  You can go over to smaps to
figure out what the page size _really_ is with KernelPageSize
or MMUPageSize.  But, I think this is a pretty nasty and
counterintuitive interface as it stands.

The following patch adds a pagesize= field.  Note that this only
shows the kernel's notion of page size.  For transparent
hugepages, it still shows the base page size.  Here's some real
output.  Note the anon_hugepage in there.

# cat /proc/`pidof memknobs`/numa_maps
00400000 default file=/root/memknobs pagesize=4KiB dirty=3 active=2 N0=3
00602000 default file=/root/memknobs pagesize=4KiB anon=1 dirty=1 N0=1
00603000 default file=/root/memknobs pagesize=4KiB anon=1 dirty=1 N0=1
00604000 default heap pagesize=4KiB anon=6 dirty=6 N0=6
7f6766216000 default file=/lib/libc-2.9.so pagesize=4KiB mapped=98 mapmax=25 active=97 N0=98
7f676637e000 default file=/lib/libc-2.9.so
7f676657e000 default file=/lib/libc-2.9.so pagesize=4KiB anon=4 dirty=4 N0=4
7f6766582000 default file=/lib/libc-2.9.so pagesize=4KiB anon=1 dirty=1 N0=1
7f6766583000 default pagesize=4KiB anon=3 dirty=3 N0=3
7f6766588000 default file=/lib/ld-2.9.so pagesize=4KiB mapped=25 mapmax=24 N0=25
7f676679d000 default pagesize=4KiB anon=2 dirty=2 N0=2
7f67667a3000 default pagesize=4KiB anon=4 dirty=4 N0=4
7f67667a7000 default file=/lib/ld-2.9.so pagesize=4KiB anon=1 dirty=1 N0=1
7f67667a8000 default file=/lib/ld-2.9.so pagesize=4KiB anon=1 dirty=1 N0=1
7f6766800000 default file=/anon_hugepage\040(deleted) pagesize=2MiB anon=10 dirty=10 N0=10
7fff5b948000 default stack pagesize=4KiB anon=2 dirty=2 N0=2
7fff5b96d000 default

Signed-off-by: Dave Haneen <dave@linux.vnet.ibm.com>
---

 linux-2.6.git-dave/fs/proc/task_mmu.c |    5 +++++
 1 file changed, 5 insertions(+)

diff -puN fs/proc/task_mmu.c~show-page-size fs/proc/task_mmu.c
--- linux-2.6.git/fs/proc/task_mmu.c~show-page-size	2011-09-30 10:53:09.166048432 -0700
+++ linux-2.6.git-dave/fs/proc/task_mmu.c	2011-09-30 10:53:09.174048416 -0700
@@ -1044,6 +1044,11 @@ static int show_numa_map(struct seq_file
 	if (!md->pages)
 		goto out;
 
+	/* Only interesting for hugetlbfs pages.
+	 * Transparent hugepages are still pagesize=4k */
+	seq_puts(m, " pagesize=");
+	seq_print_size(m, vma_kernel_pagesize(vma), STRING_UNITS_2);
+
 	if (md->anon)
 		seq_printf(m, " anon=%lu", md->anon);
 
_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC][PATCH 2/4] break out unit selection from string_get_size()
  2011-09-30 18:02 ` [RFC][PATCH 2/4] break out unit selection from string_get_size() Dave Hansen
@ 2011-09-30 18:54   ` H. Peter Anvin
  0 siblings, 0 replies; 5+ messages in thread
From: H. Peter Anvin @ 2011-09-30 18:54 UTC (permalink / raw)
  To: Dave Hansen; +Cc: linux-mm, linux-kernel, rientjes, James.Bottomley

On 09/30/2011 11:02 AM, Dave Hansen wrote:
> string_get_size() can really only print things in a single
> format.  You're always stuck with a space, and it will
> always zero-pad the decimal places:
> 
> 	4.00 KiB
> 	40.0 KiB
> 	400 KiB
> 
> Printing page sizes in decimal KiB does not make much sense
> since they are always nice powers of two.  But,
> string_get_size() does have some nice code for selecting
> the right units and doing the division.
> 
> This breaks that nice code out so that we can reuse it.
> find_size_units() is a bit of a funky function since it has
> so many outputs.  I don't think it's _too_ crazy though.
> 
> Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>

For powers of two, wouldn't it make a lot more sense to just do ilog2()
to get the power of two and then the moral equivalent of:

	sprintf("%u %s", 1 << (pw2 % 10), units_str[pw2 / 10]);

	-hpa

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-09-30 18:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-30 18:02 [RFC][PATCH 1/4] break up string_get_size() Dave Hansen
2011-09-30 18:02 ` [RFC][PATCH 2/4] break out unit selection from string_get_size() Dave Hansen
2011-09-30 18:54   ` H. Peter Anvin
2011-09-30 18:02 ` [RFC][PATCH 3/4] add seq_print_size() function Dave Hansen
2011-09-30 18:02 ` [RFC][PATCH 4/4] show page size in /proc/$pid/numa_maps Dave Hansen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).