public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Jan Stancek <jstancek@redhat.com>
Cc: ltp-list@lists.sourceforge.net
Subject: Re: [LTP] [PATCH] mem/oom: allocate memory using multiple threads
Date: Thu, 4 Jun 2015 14:34:24 +0200	[thread overview]
Message-ID: <20150604123423.GA21846@rei.suse.de> (raw)
In-Reply-To: <6638881b90112812abf856192c16986eb73fb10a.1433420296.git.jstancek@redhat.com>

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

      reply	other threads:[~2015-06-04 12:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-04 12:19 [LTP] [PATCH] mem/oom: allocate memory using multiple threads Jan Stancek
2015-06-04 12:34 ` Cyril Hrubis [this message]

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=20150604123423.GA21846@rei.suse.de \
    --to=chrubis@suse.cz \
    --cc=jstancek@redhat.com \
    --cc=ltp-list@lists.sourceforge.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox