Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Device-mapper Development (http://www.linux-archive.org/device-mapper-development/)
-   -   set pthread stack size to at least PTHREAD_STACK_MIN (http://www.linux-archive.org/device-mapper-development/262570-set-pthread-stack-size-least-pthread_stack_min.html)

Hannes Reinecke 03-13-2009 09:51 AM

set pthread stack size to at least PTHREAD_STACK_MIN
 
Benjamin Marzinski wrote:

Attempting to set the stacksize of a pthread to below
PTHREAD_STACK_MIN causes pthread_attr_setstacksize() to fail, which
means that the thread will use the default stack size. This fix
makes sure that multipathd never requests a stack size less than
PTHREAD_STACK_MIN.

Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
---
libmultipath/log_pthread.c | 6 +++++-
libmultipath/waiter.c | 5 ++++-
multipathd/main.c | 5 ++++-
3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/libmultipath/log_pthread.c b/libmultipath/log_pthread.c
index a1d4a10..5d2fe76 100644
--- a/libmultipath/log_pthread.c
+++ b/libmultipath/log_pthread.c
@@ -6,6 +6,7 @@
#include <stdarg.h>
#include <pthread.h>
#include <sys/mman.h>
+#include <limits.h>

#include <memory.h>

@@ -52,6 +53,7 @@ static void * log_thread (void * et)

void log_thread_start (void)

{
+ size_t stacksize = 64 * 1024;
pthread_attr_t attr;

logdbg(stderr,"enter log_thread_start
");

@@ -65,7 +67,9 @@ void log_thread_start (void)
pthread_cond_init(logev_cond, NULL);

pthread_attr_init(&attr);

- pthread_attr_setstacksize(&attr, 64 * 1024);
+ if (stacksize < PTHREAD_STACK_MIN)
+ stacksize = PTHREAD_STACK_MIN;
+ pthread_attr_setstacksize(&attr, stacksize);

if (log_init("multipathd", 0)) {

fprintf(stderr,"can't initialize log buffer
");

[ .. ]
Hmm. I don't quite agree. I run into the same problem, but having
discovered that we're not checking any return values at all here
we should rather do the prudent thing and check them once and for all.

I've chosen this approach:

diff --git a/libmultipath/log_pthread.c b/libmultipath/log_pthread.c
index 9e9aebe..c33480e 100644
--- a/libmultipath/log_pthread.c
+++ b/libmultipath/log_pthread.c
@@ -53,9 +53,30 @@ static void * log_thread (void * et)
void log_thread_start (void)
{
pthread_attr_t attr;
+ size_t stacksize;

logdbg(stderr,"enter log_thread_start
");

+ if (pthread_attr_init(&attr)) {
+ fprintf(stderr,"can't initialize log thread
");
+ exit(1);
+ }
+
+ if (pthread_attr_getstacksize(&attr, &stacksize) != 0)
+ stacksize = PTHREAD_STACK_MIN:
+
+ /* Check if the stacksize is large enough */
+ if (stacksize < (64 * 1024))
+ stacksize = 64 * 1024;
+
+ /* Set stacksize and try to reinitialize attr if failed */
+ if (stacksize > PTHREAD_STACK_MIN &&
+ pthread_attr_setstacksize(&attr, stacksize) != 0 &&
+ pthread_attr_init(&attr)) {
+ fprintf(stderr,"can't set log thread stack size
");
+ exit(1);
+ }
+
logq_lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
logev_lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
logev_cond = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
@@ -64,9 +85,6 @@ void log_thread_start (void)
pthread_mutex_init(logev_lock, NULL);
pthread_cond_init(logev_cond, NULL);

- pthread_attr_init(&attr);
- pthread_attr_setstacksize(&attr, 64 * 1024);
-
if (log_init("multipathd", 0)) {
fprintf(stderr,"can't initialize log buffer
");
exit(1);


This way we'll at least be notified if something goes wrong in
the future. We shouldn't make the same mistake again and
ignore error codes which don't happen to trigger now.

If agreed I'll post the full patch here.

Cheers,

Hannes
--
Dr. Hannes Reinecke zSeries & Storage
hare@suse.de +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel

Hannes Reinecke 03-16-2009 03:12 PM

set pthread stack size to at least PTHREAD_STACK_MIN
 
Benjamin Marzinski wrote:
[ .. ]


This approach doesn't doesn't actually fix the bug that I see. The
problem I was seeing is that setting the stacksize too small just causes
pthread_attr_setstacksize() to fail, leaving you with the default stack
size. On some architectures, the default stacksize is large, like 10Mb.
Since you start one waiter thread per multipath device, every 100
devices eats up 1Gb of memory. Your approach always uses the default
stack size, unless it's too small. I've never seen problems with the
stack being too small. Only too large. Maybe your experience has been
different.


Me neither. Makes me wonder if we _really_ need to set the stacksize.
After all, I'm not aware that we're having any excessive stack usage
somewhere. Maybe we can simplify it by removing the stack attribute
setting altogether?

I'll see if I can get the different stacksizes and just compare them
to the 'updated' setting. Maybe there's no big difference after all...


The other problem is that when I actually read the pthread_attr_init man
page (it can fail. who knew?), I saw that it can fail with ENOMEM. Also,
that it had a function to free it, and that the result of reinitializing
an attr that hadn't been freed was undefined. Clearly, this function
wasn't intended to be called over and over without ever freeing the
attr, which is how we've been using it in multipathd. So, in the spirit
of writing code to the interface, instead of to how it appears to be
currently implemented, how about this.

Hmm. You're not freeing the attribute for all non-logging threads neither.
Oversight?


Cheers,

Hannes
--
Dr. Hannes Reinecke zSeries & Storage
hare@suse.de +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel


All times are GMT. The time now is 11:27 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.