All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] mem/oom: allocate memory using multiple threads
@ 2015-06-04 12:19 Jan Stancek
  2015-06-04 12:34 ` Cyril Hrubis
  0 siblings, 1 reply; 2+ messages in thread
From: Jan Stancek @ 2015-06-04 12:19 UTC (permalink / raw)
  To: ltp-list

This is to make testcase faster on big systems, for example,
on Lenovo X3950 X6, 256GB RAM, 240 CPUs, 8 NUMA nodes
oom01 testcase would previously take ~4 hours to complete.
With this patch it takes ~16 minutes.

This change spawns CPU-1 (or at least 1) thread(s), which
allocate / fault-in memory in same way as before.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 testcases/kernel/mem/include/libmem.mk |  2 +-
 testcases/kernel/mem/lib/mem.c         | 59 +++++++++++++++++++++++++++-------
 2 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/testcases/kernel/mem/include/libmem.mk b/testcases/kernel/mem/include/libmem.mk
index 4503e78..bb5a163 100644
--- a/testcases/kernel/mem/include/libmem.mk
+++ b/testcases/kernel/mem/include/libmem.mk
@@ -24,7 +24,7 @@ LIBMEM_DIR		:= $(MEM_DIR)/lib
 LIBMEM			:= $(LIBMEM_DIR)/libmem.a
 FILTER_OUT_DIRS		:= $(LIBMEM_DIR)
 CFLAGS			+= -I$(MEM_SRCDIR)/include
-LDLIBS			+= $(NUMA_LIBS) -lmem -lltp
+LDLIBS			+= $(NUMA_LIBS) -lmem -lltp -lpthread
 LDFLAGS			+= -L$(LIBMEM_DIR)
 
 $(LIBMEM_DIR):
diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index 37cf18f..1862e26 100644
--- a/testcases/kernel/mem/lib/mem.c
+++ b/testcases/kernel/mem/lib/mem.c
@@ -12,6 +12,7 @@
 #if HAVE_NUMAIF_H
 #include <numaif.h>
 #endif
+#include <pthread.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
@@ -30,7 +31,8 @@ static int alloc_mem(long int length, int testcase)
 	char *s;
 	long i, pagesz = getpagesize();
 
-	tst_resm(TINFO, "allocating %ld bytes.", length);
+	tst_resm(TINFO, "thread (%lx), allocating %ld bytes.",
+		(unsigned long) pthread_self(), length);
 
 	s = mmap(NULL, length, PROT_READ | PROT_WRITE,
 		 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
@@ -49,18 +51,52 @@ static int alloc_mem(long int length, int testcase)
 	return 0;
 }
 
-static void test_alloc(int testcase, int lite)
+static void *child_alloc_thread(void *args)
 {
-	int ret;
+	int *testcase = args;
+	int ret = 0;
+
+	/* keep allocating until there's an error */
+	while (!ret)
+		ret = alloc_mem(LENGTH, *testcase);
+	exit(ret);
+}
+
+static void child_alloc(int testcase, int lite, int threads)
+{
+	int i;
+	pthread_t *th;
 
 	if (lite) {
-		ret = alloc_mem(TESTMEM + MB, testcase);
-	} else {
-		ret = 0;
-		while (!ret)
-			ret = alloc_mem(LENGTH, testcase);
+		int ret = alloc_mem(TESTMEM + MB, testcase);
+		exit(ret);
 	}
-	exit(ret);
+
+	th = malloc(sizeof(pthread_t) * threads);
+	if (!th) {
+		tst_resm(TINFO | TERRNO, "malloc");
+		goto out;
+	}
+
+	for (i = 0; i < threads; i++) {
+		TEST(pthread_create(&th[i], NULL, child_alloc_thread,
+			&testcase));
+		if (TEST_RETURN) {
+			tst_resm(TINFO | TRERRNO, "pthread_create");
+			goto out;
+		}
+	}
+
+	/* wait for one of threads to exit whole process */
+	for (i = 0; i < threads; i++) {
+		TEST(pthread_join(th[i], NULL));
+		if (TEST_RETURN) {
+			tst_resm(TINFO | TRERRNO, "pthread_join");
+			goto out;
+		}
+	}
+out:
+	exit(1);
 }
 
 /*
@@ -81,13 +117,14 @@ static void test_alloc(int testcase, int lite)
 void oom(int testcase, int lite, int retcode, int allow_sigkill)
 {
 	pid_t pid;
-	int status;
+	int status, threads;
 
 	switch (pid = fork()) {
 	case -1:
 		tst_brkm(TBROK | TERRNO, cleanup, "fork");
 	case 0:
-		test_alloc(testcase, lite);
+		threads = MAX(1, tst_ncpus() - 1);
+		child_alloc(testcase, lite, threads);
 	default:
 		break;
 	}
-- 
1.8.3.1


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH] mem/oom: allocate memory using multiple threads
  2015-06-04 12:19 [LTP] [PATCH] mem/oom: allocate memory using multiple threads Jan Stancek
@ 2015-06-04 12:34 ` Cyril Hrubis
  0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2015-06-04 12:34 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

Hi!
>  testcases/kernel/mem/include/libmem.mk |  2 +-
>  testcases/kernel/mem/lib/mem.c         | 59 +++++++++++++++++++++++++++-------
>  2 files changed, 49 insertions(+), 12 deletions(-)
> 
> diff --git a/testcases/kernel/mem/include/libmem.mk b/testcases/kernel/mem/include/libmem.mk
> index 4503e78..bb5a163 100644
> --- a/testcases/kernel/mem/include/libmem.mk
> +++ b/testcases/kernel/mem/include/libmem.mk
> @@ -24,7 +24,7 @@ LIBMEM_DIR		:= $(MEM_DIR)/lib
>  LIBMEM			:= $(LIBMEM_DIR)/libmem.a
>  FILTER_OUT_DIRS		:= $(LIBMEM_DIR)
>  CFLAGS			+= -I$(MEM_SRCDIR)/include
> -LDLIBS			+= $(NUMA_LIBS) -lmem -lltp
> +LDLIBS			+= $(NUMA_LIBS) -lmem -lltp -lpthread
>  LDFLAGS			+= -L$(LIBMEM_DIR)

You should add -pthread to CFLAGS instead, otherwise the code will fail
to compile on some distributions.

>  $(LIBMEM_DIR):
> diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
> index 37cf18f..1862e26 100644
> --- a/testcases/kernel/mem/lib/mem.c
> +++ b/testcases/kernel/mem/lib/mem.c
> @@ -12,6 +12,7 @@
>  #if HAVE_NUMAIF_H
>  #include <numaif.h>
>  #endif
> +#include <pthread.h>
>  #include <stdarg.h>
>  #include <stdio.h>
>  #include <string.h>
> @@ -30,7 +31,8 @@ static int alloc_mem(long int length, int testcase)
>  	char *s;
>  	long i, pagesz = getpagesize();
>  
> -	tst_resm(TINFO, "allocating %ld bytes.", length);
> +	tst_resm(TINFO, "thread (%lx), allocating %ld bytes.",
> +		(unsigned long) pthread_self(), length);
>  
>  	s = mmap(NULL, length, PROT_READ | PROT_WRITE,
>  		 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
> @@ -49,18 +51,52 @@ static int alloc_mem(long int length, int testcase)
>  	return 0;
>  }
>  
> -static void test_alloc(int testcase, int lite)
> +static void *child_alloc_thread(void *args)
>  {
> -	int ret;
> +	int *testcase = args;
> +	int ret = 0;
> +
> +	/* keep allocating until there's an error */
> +	while (!ret)
> +		ret = alloc_mem(LENGTH, *testcase);

You can do

 ret = alloc_mem(LENGHT, (long)args)

here and

 TEST(pthread_create(&th[i], NULL, child_alloc_thread, (void*)((long)testcase)));

in the caller.

But given that the caller joins the threads before it exits the function
the pointer to testcase will be vaild at the time this function is
executed.

> +	exit(ret);

And and it's a bit confusing that the pthread_join() is likely not
reached because the first finishing thread exits the child here.

But at lest there is a comment below.

Maybe we can just wait in the caller thread indefinitely.

> +}
> +
> +static void child_alloc(int testcase, int lite, int threads)
> +{
> +	int i;
> +	pthread_t *th;
>  
>  	if (lite) {
> -		ret = alloc_mem(TESTMEM + MB, testcase);
> -	} else {
> -		ret = 0;
> -		while (!ret)
> -			ret = alloc_mem(LENGTH, testcase);
> +		int ret = alloc_mem(TESTMEM + MB, testcase);
> +		exit(ret);
>  	}
> -	exit(ret);
> +
> +	th = malloc(sizeof(pthread_t) * threads);
> +	if (!th) {
> +		tst_resm(TINFO | TERRNO, "malloc");
> +		goto out;
> +	}
> +
> +	for (i = 0; i < threads; i++) {
> +		TEST(pthread_create(&th[i], NULL, child_alloc_thread,
> +			&testcase));
> +		if (TEST_RETURN) {
> +			tst_resm(TINFO | TRERRNO, "pthread_create");
> +			goto out;
> +		}
> +	}
> +
> +	/* wait for one of threads to exit whole process */
> +	for (i = 0; i < threads; i++) {
> +		TEST(pthread_join(th[i], NULL));
> +		if (TEST_RETURN) {
> +			tst_resm(TINFO | TRERRNO, "pthread_join");
> +			goto out;
> +		}
> +	}
> +out:
> +	exit(1);
>  }
>  
>  /*
> @@ -81,13 +117,14 @@ static void test_alloc(int testcase, int lite)
>  void oom(int testcase, int lite, int retcode, int allow_sigkill)
>  {
>  	pid_t pid;
> -	int status;
> +	int status, threads;
>  
>  	switch (pid = fork()) {
>  	case -1:
>  		tst_brkm(TBROK | TERRNO, cleanup, "fork");
>  	case 0:
> -		test_alloc(testcase, lite);
> +		threads = MAX(1, tst_ncpus() - 1);
> +		child_alloc(testcase, lite, threads);
>  	default:
>  		break;
>  	}
> -- 
> 1.8.3.1
> 
> 
> ------------------------------------------------------------------------------
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

end of thread, other threads:[~2015-06-04 12:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-04 12:19 [LTP] [PATCH] mem/oom: allocate memory using multiple threads Jan Stancek
2015-06-04 12:34 ` Cyril Hrubis

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.