linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Add a test program for variable page sizes in mmap/shmget v2
@ 2012-11-08 21:01 Andi Kleen
  2012-11-08 21:29 ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: Andi Kleen @ 2012-11-08 21:01 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, Andi Kleen

From: Andi Kleen <ak@linux.intel.com>

Not hooked up to the harness so far, because it usually needs
special boot options for 1GB pages.

v2: Add more sanity checks. Disable debug output. Improve comment.
    Delete shm segment by default.
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
 tools/testing/selftests/vm/Makefile    |    4 +-
 tools/testing/selftests/vm/thuge-gen.c |  254 ++++++++++++++++++++++++++++++++
 2 files changed, 256 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/vm/thuge-gen.c

diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index b336b24..7300d07 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -1,9 +1,9 @@
 # Makefile for vm selftests
 
 CC = $(CROSS_COMPILE)gcc
-CFLAGS = -Wall -Wextra
+CFLAGS = -Wall
 
-all: hugepage-mmap hugepage-shm  map_hugetlb
+all: hugepage-mmap hugepage-shm  map_hugetlb thuge-gen
 %: %.c
 	$(CC) $(CFLAGS) -o $@ $^
 
diff --git a/tools/testing/selftests/vm/thuge-gen.c b/tools/testing/selftests/vm/thuge-gen.c
new file mode 100644
index 0000000..3cb66a5
--- /dev/null
+++ b/tools/testing/selftests/vm/thuge-gen.c
@@ -0,0 +1,254 @@
+/* Test selecting other page sizes for mmap/shmget.
+   
+   Before running this huge pages for each huge page size must have been 
+   reserved.
+   For large pages beyond MAX_ORDER (like 1GB on x86) boot options must be used.
+   Also shmmax must be increased.
+   And you need to run as root to work around some weird permissions in shm. 
+   And nothing using huge pages should run in parallel.
+   When the program aborts you may need to clean up the shm segments with 
+   ipcrm -m by hand, like this
+   sudo ipcs | awk '$1 == "0x00000000" {print $2}' | xargs -n1 sudo ipcrm -m
+   (warning this will remove all if someone else uses them) */
+
+#define _GNU_SOURCE 1
+#include <sys/mman.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <sys/stat.h>
+#include <glob.h>
+#include <assert.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <string.h>
+
+#define err(x) perror(x), exit(1)
+
+#define MAP_HUGE_2MB    (21 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_1GB    (30 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_SHIFT  26
+#define MAP_HUGE_MASK   0x3f
+#define MAP_HUGETLB	0x40000
+
+#define SHM_HUGETLB     04000   /* segment will use huge TLB pages */
+#define SHM_HUGE_SHIFT  26
+#define SHM_HUGE_MASK   0x3f
+#define SHM_HUGE_2MB    (21 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_1GB    (30 << SHM_HUGE_SHIFT)
+
+#define NUM_PAGESIZES   5
+
+#define NUM_PAGES 4
+
+#define Dprintf(fmt...) // printf(fmt)
+
+unsigned long page_sizes[NUM_PAGESIZES];
+int num_page_sizes;
+
+int ilog2(unsigned long v)
+{
+	int l = 0;
+	while ((1UL << l) < v)
+		l++;
+	return l;
+}
+
+void find_pagesizes(void)
+{
+	glob_t g;
+	int i;
+	glob("/sys/kernel/mm/hugepages/hugepages-*kB", 0, NULL, &g);
+	assert(g.gl_pathc <= NUM_PAGESIZES);
+	for (i = 0; i < g.gl_pathc; i++) {
+		sscanf(g.gl_pathv[i], "/sys/kernel/mm/hugepages/hugepages-%lukB",
+				&page_sizes[i]);
+		page_sizes[i] <<= 10;
+		printf("Found %luMB\n", page_sizes[i] >> 20);
+	}
+	num_page_sizes = g.gl_pathc;
+	globfree(&g);
+}
+
+unsigned long default_huge_page_size(void)
+{
+	unsigned long hps = 0;
+	char *line = NULL;
+	size_t linelen = 0;
+	FILE *f = fopen("/proc/meminfo", "r");
+	if (!f)
+		return 0;
+	while (getline(&line, &linelen, f) > 0) { 
+		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
+			hps <<= 10;
+			break;
+		}
+	}
+	free(line);
+	return hps;
+}
+
+void show(unsigned long ps)
+{
+	char buf[100];
+	if (ps == getpagesize())
+		return;
+	printf("%luMB: ", ps >> 20);
+	fflush(stdout);
+	snprintf(buf, sizeof buf, 
+		"cat /sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
+		ps >> 10);
+	system(buf);
+}
+
+unsigned long read_sysfs(int warn, char *fmt, ...)
+{
+	char *line = NULL;
+	size_t linelen = 0;
+	char buf[100];
+	FILE *f;
+	va_list ap;
+	unsigned long val = 0;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, sizeof buf, fmt, ap); 
+	va_end(ap);
+
+	f = fopen(buf, "r");
+	if (!f) { 
+		if (warn) 
+			printf("missing %s\n", buf);
+		return 0;
+	}
+	if (getline(&line, &linelen, f) > 0) {
+		sscanf(line, "%lu", &val);
+	}
+	fclose(f);
+	free(line);
+	return val;
+}
+
+unsigned long read_free(unsigned long ps)
+{
+	return read_sysfs(ps != getpagesize(),
+			"/sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
+			ps >> 10);
+}
+
+void test_mmap(unsigned long size, unsigned flags)
+{
+	char *map;
+	unsigned long before, after;
+	int err;
+	
+	before = read_free(size);
+	map = mmap(NULL, size*NUM_PAGES, PROT_READ|PROT_WRITE, 
+			MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|flags, 0, 0);
+
+	if (map == (char *)-1) err("mmap");
+	memset(map, 0xff, size*NUM_PAGES);
+	after = read_free(size);
+	Dprintf("before %lu after %lu diff %ld size %lu\n",
+		before, after, before - after, size);
+	assert(size == getpagesize() || (before - after) == NUM_PAGES);
+	show(size);
+	err = munmap(map, size);
+	assert(!err);
+}
+
+void test_shmget(unsigned long size, unsigned flags)
+{
+	int id;
+	unsigned long before, after;
+	int err;
+
+	before = read_free(size);
+	id = shmget(IPC_PRIVATE, size * NUM_PAGES, IPC_CREAT|0600|flags);
+	if (id < 0) err("shmget");
+
+	struct shm_info i;
+	if (shmctl(id, SHM_INFO, (void *)&i) < 0) err("shmctl");
+	Dprintf("alloc %lu res %lu\n", i.shm_tot, i.shm_rss);
+
+
+	Dprintf("id %d\n", id);
+	char *map = shmat(id, NULL, 0600);
+	if (map == (char*)-1) err("shmat");
+
+	shmctl(id, IPC_RMID, NULL);
+
+	memset(map, 0xff, size*NUM_PAGES);
+	after = read_free(size);
+
+	Dprintf("before %lu after %lu diff %ld size %lu\n",
+		before, after, before - after, size);
+	assert(size == getpagesize() || (before - after) == NUM_PAGES);
+	show(size);
+	err = shmdt(map);
+	assert(!err);
+}
+
+void sanity_checks(void)
+{
+	int i;
+	unsigned long largest = getpagesize();
+
+	for (i = 0; i < num_page_sizes; i++) {
+		if (page_sizes[i] > largest)
+			largest = page_sizes[i];
+
+		if (read_free(page_sizes[i]) < NUM_PAGES) {
+			printf("Not enough huge pages for page size %lu MB, need %u\n", 
+				page_sizes[i] >> 20,
+				NUM_PAGES);
+			exit(0);
+		}
+	}
+
+	if (read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES * largest) {
+		printf("Please do echo %lu > /proc/sys/kernel/shmmax", largest * NUM_PAGES);
+		exit(0);
+	}
+
+#if defined(__x86_64__) 
+	if (largest != 1U<<30) {
+		printf("No GB pages available on x86-64\n" 
+		       "Please boot with hugepagesz=1G hugepages=%d\n", NUM_PAGES);
+		exit(0);
+	}
+#endif
+}
+
+int main(void)
+{
+	int i;
+	unsigned default_hps = default_huge_page_size();
+	
+	find_pagesizes();
+
+	sanity_checks();
+
+	for (i = 0; i < num_page_sizes; i++) {
+		unsigned long ps = page_sizes[i];
+		int arg = ilog2(ps) << MAP_HUGE_SHIFT;
+		printf("Testing %luMB mmap with shift %x\n", ps >> 20, arg);
+		test_mmap(ps, MAP_HUGETLB | arg);
+	}
+	printf("Testing default huge mmap\n");
+	test_mmap(default_hps, SHM_HUGETLB);
+	
+	puts("Testing non-huge shmget");
+	test_shmget(getpagesize(), 0);
+
+	for (i = 0; i < num_page_sizes; i++) {
+		unsigned long ps = page_sizes[i];
+		int arg = ilog2(ps) << SHM_HUGE_SHIFT;
+		printf("Testing %luMB shmget with shift %x\n", ps >> 20, arg);
+		test_shmget(ps, SHM_HUGETLB | arg);
+	}
+	puts("default huge shmget");
+	test_shmget(default_hps, SHM_HUGETLB);
+	
+	return 0;
+}
-- 
1.7.7.6

--
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/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] Add a test program for variable page sizes in mmap/shmget v2
  2012-11-08 21:01 [PATCH] Add a test program for variable page sizes in mmap/shmget v2 Andi Kleen
@ 2012-11-08 21:29 ` Andrew Morton
  2012-11-08 22:01   ` Andi Kleen
  2012-11-12  9:35   ` Dave Young
  0 siblings, 2 replies; 6+ messages in thread
From: Andrew Morton @ 2012-11-08 21:29 UTC (permalink / raw)
  To: Andi Kleen; +Cc: linux-mm, Andi Kleen, Dave Young

On Thu,  8 Nov 2012 13:01:26 -0800
Andi Kleen <andi@firstfloor.org> wrote:

> From: Andi Kleen <ak@linux.intel.com>
> 
> Not hooked up to the harness so far, because it usually needs
> special boot options for 1GB pages.

This isn't the case from my reading: we *can* hook it up now?

> index b336b24..7300d07 100644
> --- a/tools/testing/selftests/vm/Makefile
> +++ b/tools/testing/selftests/vm/Makefile
> @@ -1,9 +1,9 @@
>  # Makefile for vm selftests
>  
>  CC = $(CROSS_COMPILE)gcc
> -CFLAGS = -Wall -Wextra
> +CFLAGS = -Wall

Why this?  It doesn't change anything with my gcc so I think
I'll revert that.

>
> ...
>

Also...

I just tried a `make run_vmtests' and it fell on its face. 
There's a little comment in there saying "please run as root", but we
don't *want* that.  The selftests should be runnable as non-root and
should, where unavoidable, emit a warning and proceed if elevated
permissions are required.

I tried running it as root and my workstation hung, requiring a reboot.
Won't be doing that again.

Dave, could you please have a dig at this sometime?

--
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/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] Add a test program for variable page sizes in mmap/shmget v2
  2012-11-08 21:29 ` Andrew Morton
@ 2012-11-08 22:01   ` Andi Kleen
  2012-11-08 22:09     ` Andrew Morton
  2012-11-12  9:35   ` Dave Young
  1 sibling, 1 reply; 6+ messages in thread
From: Andi Kleen @ 2012-11-08 22:01 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Andi Kleen, linux-mm, Dave Young

On Thu, Nov 08, 2012 at 01:29:46PM -0800, Andrew Morton wrote:
> On Thu,  8 Nov 2012 13:01:26 -0800
> Andi Kleen <andi@firstfloor.org> wrote:
> 
> > From: Andi Kleen <ak@linux.intel.com>
> > 
> > Not hooked up to the harness so far, because it usually needs
> > special boot options for 1GB pages.
> 
> This isn't the case from my reading: we *can* hook it up now?

Yes, but also need to set the sysctls.

> > index b336b24..7300d07 100644
> > --- a/tools/testing/selftests/vm/Makefile
> > +++ b/tools/testing/selftests/vm/Makefile
> > @@ -1,9 +1,9 @@
> >  # Makefile for vm selftests
> >  
> >  CC = $(CROSS_COMPILE)gcc
> > -CFLAGS = -Wall -Wextra
> > +CFLAGS = -Wall
> 
> Why this?  It doesn't change anything with my gcc so I think
> I'll revert that.

There were lots of warnings with signed/unsigned comparisons on my gcc
(4.6) and since I personally consider those useless I just disabled
the warning.

> 
> I just tried a `make run_vmtests' and it fell on its face. 
> There's a little comment in there saying "please run as root", but we
> don't *want* that.  The selftests should be runnable as non-root and
> should, where unavoidable, emit a warning and proceed if elevated
> permissions are required.

My test requires root. That is only the ipc test requires root.
To be honest I have no idea why but I don't claim to understand
why ipcperms() does all the weird stuff it does.


> 
> I tried running it as root and my workstation hung, requiring a reboot.
> Won't be doing that again.

My test system didn't hang FWIW.

-Andi

-- 
ak@linux.intel.com -- Speaking for myself only

--
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/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] Add a test program for variable page sizes in mmap/shmget v2
  2012-11-08 22:01   ` Andi Kleen
@ 2012-11-08 22:09     ` Andrew Morton
  2012-11-09  0:38       ` Andi Kleen
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2012-11-08 22:09 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Andi Kleen, linux-mm, Dave Young

On Thu, 8 Nov 2012 14:01:50 -0800
Andi Kleen <ak@linux.intel.com> wrote:

> On Thu, Nov 08, 2012 at 01:29:46PM -0800, Andrew Morton wrote:
> > On Thu,  8 Nov 2012 13:01:26 -0800
> > Andi Kleen <andi@firstfloor.org> wrote:
> > 
> > > From: Andi Kleen <ak@linux.intel.com>
> > > 
> > > Not hooked up to the harness so far, because it usually needs
> > > special boot options for 1GB pages.
> > 
> > This isn't the case from my reading: we *can* hook it up now?
> 
> Yes, but also need to set the sysctls.

That doesn't sound terribly involved?

> > > index b336b24..7300d07 100644
> > > --- a/tools/testing/selftests/vm/Makefile
> > > +++ b/tools/testing/selftests/vm/Makefile
> > > @@ -1,9 +1,9 @@
> > >  # Makefile for vm selftests
> > >  
> > >  CC = $(CROSS_COMPILE)gcc
> > > -CFLAGS = -Wall -Wextra
> > > +CFLAGS = -Wall
> > 
> > Why this?  It doesn't change anything with my gcc so I think
> > I'll revert that.
> 
> There were lots of warnings with signed/unsigned comparisons on my gcc
> (4.6) and since I personally consider those useless I just disabled
> the warning.

eh, OK, I'll update the changelog.

> > 
> > I just tried a `make run_vmtests' and it fell on its face. 
> > There's a little comment in there saying "please run as root", but we
> > don't *want* that.  The selftests should be runnable as non-root and
> > should, where unavoidable, emit a warning and proceed if elevated
> > permissions are required.
> 
> My test requires root. That is only the ipc test requires root.
> To be honest I have no idea why but I don't claim to understand
> why ipcperms() does all the weird stuff it does.
> 
> 
> > 
> > I tried running it as root and my workstation hung, requiring a reboot.
> > Won't be doing that again.
> 
> My test system didn't hang FWIW.

It wasn't thuge-gen which hung.  It happened really early in
run_vmtests, perhaps setting nr_hugepages.

--
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/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] Add a test program for variable page sizes in mmap/shmget v2
  2012-11-08 22:09     ` Andrew Morton
@ 2012-11-09  0:38       ` Andi Kleen
  0 siblings, 0 replies; 6+ messages in thread
From: Andi Kleen @ 2012-11-09  0:38 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Andi Kleen, linux-mm, Dave Young

> > My test system didn't hang FWIW.
> 
> It wasn't thuge-gen which hung.  It happened really early in
> run_vmtests, perhaps setting nr_hugepages.

I mean it didn't hang for the full script.

Ah this causes compaction so if you have a lot of fragmented memory
it may run for a lot time with very long latencies, but inhibiting
page faults of other processes.

It probably would have recovered.

it's a general problem that others are complaining about too.

-Andi

-- 
ak@linux.intel.com -- Speaking for myself only

--
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/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] Add a test program for variable page sizes in mmap/shmget v2
  2012-11-08 21:29 ` Andrew Morton
  2012-11-08 22:01   ` Andi Kleen
@ 2012-11-12  9:35   ` Dave Young
  1 sibling, 0 replies; 6+ messages in thread
From: Dave Young @ 2012-11-12  9:35 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Andi Kleen, linux-mm, Andi Kleen

On 11/09/2012 05:29 AM, Andrew Morton wrote:

> On Thu,  8 Nov 2012 13:01:26 -0800
> Andi Kleen <andi@firstfloor.org> wrote:
> 
>> From: Andi Kleen <ak@linux.intel.com>
>>
>> Not hooked up to the harness so far, because it usually needs
>> special boot options for 1GB pages.
> 
> This isn't the case from my reading: we *can* hook it up now?
> 
>> index b336b24..7300d07 100644
>> --- a/tools/testing/selftests/vm/Makefile
>> +++ b/tools/testing/selftests/vm/Makefile
>> @@ -1,9 +1,9 @@
>>  # Makefile for vm selftests
>>  
>>  CC = $(CROSS_COMPILE)gcc
>> -CFLAGS = -Wall -Wextra
>> +CFLAGS = -Wall
> 
> Why this?  It doesn't change anything with my gcc so I think
> I'll revert that.
> 
>>
>> ...
>>
> 
> Also...
> 
> I just tried a `make run_vmtests' and it fell on its face. 
> There's a little comment in there saying "please run as root", but we
> don't *want* that.  The selftests should be runnable as non-root and
> should, where unavoidable, emit a warning and proceed if elevated
> permissions are required.


Hi andrew:
below code need root, I agree warning in case non-root is good, If you
like I can fix it.

echo $(( $lackpgs + $nr_hugepgs )) > /proc/sys/vm/nr_hugepages

> 
> I tried running it as root and my workstation hung, requiring a reboot.
> Won't be doing that again.
> 
> Dave, could you please have a dig at this sometime?


I can not reproduce the hang, will keep an eye on this.

-- 
Thanks
Dave

--
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/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

end of thread, other threads:[~2012-11-12  9:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-08 21:01 [PATCH] Add a test program for variable page sizes in mmap/shmget v2 Andi Kleen
2012-11-08 21:29 ` Andrew Morton
2012-11-08 22:01   ` Andi Kleen
2012-11-08 22:09     ` Andrew Morton
2012-11-09  0:38       ` Andi Kleen
2012-11-12  9:35   ` Dave Young

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).