xs: set read_thread stacksize
PATCH 1/1 xs: set read_thread stacksize
xs_watch() creates a thread to wake watchers using default attributes. The stacksize can be quite large (8 MB on Linux), applications that link against xenstore end up having a larger memory footprint than necessary.
tools/xenstore/xs.c +20 -0
@@ -705,11 +705,31 @@ bool xs_watch(struct xs_handle *h, const/* We dynamically create a reader thread on demand. */ mutex_lock (&h->request_mutex);if (!h->read_thr_exists) { +#if _POSIX_THREAD_ATTR_STACKSIZE >0 +pthread_attr_t attr; + +if (pthread_attr_init (&attr) !=0 ) { +mutex_unlock (&h->request_mutex); +return false; + } +if (pthread_attr_setstacksize (&attr,16 *1024 ) !=0 ) { +pthread_attr_destroy (&attr); +mutex_unlock (&h->request_mutex); +return false; + } + +if (pthread_create (&h->read_thr, &attr, read_thread, h) !=0 ) { +pthread_attr_destroy (&attr); +#else if (pthread_create (&h->read_thr,NULL , read_thread, h) !=0 ) { +#endif mutex_unlock (&h->request_mutex);return false; } h->read_thr_exists =1 ; +#if _POSIX_THREAD_ATTR_STACKSIZE >0 +pthread_attr_destroy (&attr); +#endif }mutex_unlock (&h->request_mutex);#endif
Quoted 10 lines · Simon Rowe
xs_watch()creates a thread to wake watchers using default attributes. The stacksize can be quite large (8 MB on Linux), applications that link against xenstore end up having a larger memory footprint than necessary.Signed-off-by Simon Rowe <[email protected]>diff -r
53e0571f94e4-r0cf61ed6ce86tools/xenstore/xs.c
tools/xenstore/xs.c bool xs_watch(struct xs_handle *h, const · line 708
Quoted 4 lines
tools/xenstore/xs.c +1
-0@@ -705,11 +705,31 @@ bool xs_watch(struct xs_handle *h, const/* We dynamically create a reader thread on demand. */ mutex_lock (&h->request_mutex);if (!h->read_thr_exists) { +#if _POSIX_THREAD_ATTR_STACKSIZE >0
This #if seems unnecessary. pthread_attr_setsetstacksize() doesn't appear to be an optional.
tools/xenstore/xs.c bool xs_watch(struct xs_handle *h, const · line 715
Quoted 7 lines
tools/xenstore/xs.c +7
-0+pthread_attr_t attr; + +if (pthread_attr_init (&attr) !=0 ) { +mutex_unlock (&h->request_mutex); +return false; + } +if (pthread_attr_setstacksize (&attr,16 *1024 ) !=0 ) {
#define for this value?
David
Quoted 7 lines · David Vrabel
Quoted 6 lines · Simon Rowe
xs_watch()creates a thread to wake watchers using default attributes. The stacksize can be quite large (8 MB on Linux), applications that link against xenstore end up having a larger memory footprint than necessary.Signed-off-by Simon Rowe <[email protected]>
Quoted 11 lines
tools/xenstore/xs.c bool xs_watch(struct xs_handle *h, const · line 708
Quoted 8 lines
diff -r
53e0571f94e4-r0cf61ed6ce86tools/xenstore/xs.ctools/xenstore/xs.c +1
-0@@ -705,11 +705,31 @@ bool xs_watch(struct xs_handle *h, const/* We dynamically create a reader thread on demand. */ mutex_lock (&h->request_mutex);if (!h->read_thr_exists) { +#if _POSIX_THREAD_ATTR_STACKSIZE >0 This #if seems unnecessary.
pthread_attr_setsetstacksize()doesn't appear to be an optional.
...and if it were then autoconf is the way to figure that out now, unless _POSIX_THREAD_ATTR_STACKSIZE is specified somewhere (which I doubt).
Also if it is only pthread_attr_setstacksize which is optional, rather than pthread_attr_* generally, then the #if could be pulled into just surround that call, presuming there is no harm in a "NULL" attr.
Quoted 9 lines
tools/xenstore/xs.c bool xs_watch(struct xs_handle *h, const · line 715
Quoted 7 lines
tools/xenstore/xs.c +7
-0+pthread_attr_t attr; + +if (pthread_attr_init (&attr) !=0 ) { +mutex_unlock (&h->request_mutex); +return false; + } +if (pthread_attr_setstacksize (&attr,16 *1024 ) !=0 ) {#define for this value?
Yes, please.
Ian.
Quoted 7 lines
David
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 3 lines · On Tuesday 29 May 2012 20:39:33 Ian Campbell wrote:
...and if it were then autoconf is the way to figure that out now, unless
_POSIX_THREAD_ATTR_STACKSIZEis specified somewhere (which I doubt).
I was following the recommendation of the POSIX Threads: Semi-FAQ which states
5.2 How can I determine if a system supports the Stack Attribute(s)?
If the header file unistd.h defines the symbolic constant _POSIX_THREAD_ATTR_STACKSIZE to a value greater than 0, the implementation should support the getting and setting of the Stack Size Attribute. If it defined to a value of 200112L then the current specification is supported.
If this needs to be done via autoconf let me know.
Quoted 3 lines
Also if it is only
pthread_attr_setstacksizewhich is optional, rather than pthread_attr_* generally, then the #if could be pulled into just surround that call, presuming there is no harm in a "NULL" attr.
I don't quite get you, do you mean only protect the actual pthread_attr_setstacksize() call with #ifdef and therefore always call pthread_attr_init()?
Quoted 11 lines
Quoted 9 lines
tools/xenstore/xs.c bool xs_watch(struct xs_handle *h, const · line 715
Quoted 7 lines
tools/xenstore/xs.c +7
-0+pthread_attr_t attr; + +if (pthread_attr_init (&attr) !=0 ) { +mutex_unlock (&h->request_mutex); +return false; + } +if (pthread_attr_setstacksize (&attr,16 *1024 ) !=0 ) {#define for this value?
Yes, please.
Will do,
Simon
--
[1] http://www.cognitus.net/html/howto/pthreadSemiFAQ_5.html#s5_1
Quoted 11 lines · Simon Rowe
Quoted 3 lines · On Tuesday 29 May 2012 20:39:33 Ian Campbell wrote:
...and if it were then autoconf is the way to figure that out now, unless
_POSIX_THREAD_ATTR_STACKSIZEis specified somewhere (which I doubt).I was following the recommendation of the POSIX Threads: Semi-FAQ which states
5.2 How can I determine if a system supports the Stack Attribute(s)?
Quoted 7 lines
If the header file
unistd.hdefines the symbolic constant_POSIX_THREAD_ATTR_STACKSIZEto a value greater than 0, the implementation should support the getting and setting of the Stack Size Attribute. If it defined to a value of 200112L then the current specification is supported.If this needs to be done via autoconf let me know.
If this little trick applies to both NetBSD and uclibc too then I guess it would be OK, otherwise I think autoconf is necessary.
Quoted 7 lines
Quoted 3 lines
Also if it is only
pthread_attr_setstacksizewhich is optional, rather than pthread_attr_* generally, then the #if could be pulled into just surround that call, presuming there is no harm in a "NULL" attr.I don't quite get you, do you mean only protect the actual
pthread_attr_setstacksize()call with #ifdef and therefore always callpthread_attr_init()?
Yes, that'll reduce the ifdeffery and inparticular removes the double
if (pthread_create(&h->read_thr, NULL, read_thread, h) != 0) {in either half, which is bit tricky to follow and is going to be prone to drift across the two sides.
Quoted 2 lines · On Wednesday 30 May 2012 10:40:15 Ian Campbell wrote:
If this little trick applies to both NetBSD and uclibc too then I guess it would be OK, otherwise I think autoconf is necessary.
It doesn't look to my untrained eye that xenstore is autoconf-aware. The makefile unilaterally sets USE_PTHREAD for example.
Shall I just drop this test for now and if/when xenstore is updated to use autoconf it can be addressed then?
Simon
Quoted 7 lines · Simon Rowe
Quoted 2 lines · On Wednesday 30 May 2012 10:40:15 Ian Campbell wrote:
If this little trick applies to both NetBSD and uclibc too then I guess it would be OK, otherwise I think autoconf is necessary.
It doesn't look to my untrained eye that xenstore is autoconf-aware. The makefile unilaterally sets
USE_PTHREADfor example.
It has autoconf stuff available to it, I think, it just hasn't had cause to use it yet...
(USE_PTHREAD is a bit of an odd one anyway, it refers only to the client library/cmdline tools and is for building against libc's which don't have pthreads)
Quoted 2 lines
Shall I just drop this test for now and if/when xenstore is updated to use autoconf it can be addressed then?
I'd like to here from Roger about what this means for NetBSD and uclibc, if it works on those then I think it is fine to do this.
Ian.
Quoted 15 lines · Ian Campbell
Quoted 6 lines · Simon Rowe
Quoted 2 lines · On Wednesday 30 May 2012 10:40:15 Ian Campbell wrote:
If this little trick applies to both NetBSD and uclibc too then I guess it would be OK, otherwise I think autoconf is necessary.
It doesn't look to my untrained eye that xenstore is autoconf-aware. The makefile unilaterally sets
USE_PTHREADfor example.It has autoconf stuff available to it, I think, it just hasn't had cause to use it yet...
(
USE_PTHREADis a bit of an odd one anyway, it refers only to the client library/cmdline tools and is for building against libc's which don't have pthreads)
Quoted 5 lines
Quoted 2 lines
Shall I just drop this test for now and if/when xenstore is updated to use autoconf it can be addressed then?
I'd like to here from Roger about what this means for NetBSD and uclibc, if it works on those then I think it is fine to do this.
Sorry for the delay, I just received this today (don't know why). I've been looking at NetBSD and uClibc, and both have pthread_attr_setstacksize.
What I don't really like is the hardcoded (16 * 1024) value, how do you know this is greater than PTHREAD_STACK_MIN?
Frankly I don't understand why do we have to touch this, even if you requested 256MB of stack it won't we allocated until you get a page fault, so you are only using the physical memory you need.
Quoted 16 lines · Roger Pau Monne
Quoted 15 lines · Ian Campbell
Quoted 6 lines · Simon Rowe
Quoted 2 lines · On Wednesday 30 May 2012 10:40:15 Ian Campbell wrote:
If this little trick applies to both NetBSD and uclibc too then I guess it would be OK, otherwise I think autoconf is necessary.
It doesn't look to my untrained eye that xenstore is autoconf-aware. The makefile unilaterally sets
USE_PTHREADfor example.It has autoconf stuff available to it, I think, it just hasn't had cause to use it yet...
(
USE_PTHREADis a bit of an odd one anyway, it refers only to the client library/cmdline tools and is for building against libc's which don't have pthreads)
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Shall I just drop this test for now and if/when xenstore is updated to use autoconf it can be addressed then?
I'd like to here from Roger about what this means for NetBSD and uclibc, if it works on those then I think it is fine to do this.
Sorry for the delay, I just received this today (don't know why).
It seemed to have been stuck in my outbox, I thought it was another unrelated mail (which I sent this morning) so I hit go.
BTW, I think this patch went in already...
Quoted 5 lines
I've
been looking at NetBSD and uClibc, and both havepthread_attr_setstacksize.What I don't really like is the hardcoded (16 * 1024) value, how do you know this is greater than
PTHREAD_STACK_MIN?
pthread_attr_setstacksize(1) specifically says that PTHREAD_STACK_MIN is 16K, but I don't know if that is a real specified thing or just Linux bias in the manpage.
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_attr_setstacksize.html doesn't seem to say anything about the value of PTHREAD_STACK_MIN.
How about we fix this when we come across a system which has a smaller minimum stack?
Quoted 3 lines
Frankly I don't understand why do we have to touch this, even if you requested 256MB of stack it won't we allocated until you get a page fault, so you are only using the physical memory you need.
With 256M stack 4 threads would take up 1G of your virtual address space (regardless of whether it is populated or not), and 12 threads would take up 3G -- which is the whole virtual address space of a 32 bit process, which is rather limiting.
Ian.
Quoted 30 lines · Ian Campbell
Quoted 15 lines · Roger Pau Monne
Quoted 14 lines · Ian Campbell
Quoted 6 lines · Simon Rowe
Quoted 2 lines · On Wednesday 30 May 2012 10:40:15 Ian Campbell wrote:
If this little trick applies to both NetBSD and uclibc too then I guess it would be OK, otherwise I think autoconf is necessary.
It doesn't look to my untrained eye that xenstore is autoconf-aware. The makefile unilaterally sets
USE_PTHREADfor example.It has autoconf stuff available to it, I think, it just hasn't had cause to use it yet...
(
USE_PTHREADis a bit of an odd one anyway, it refers only to the client library/cmdline tools and is for building against libc's which don't have pthreads)Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Shall I just drop this test for now and if/when xenstore is updated to use autoconf it can be addressed then?
I'd like to here from Roger about what this means for NetBSD and uclibc, if it works on those then I think it is fine to do this.
Sorry for the delay, I just received this today (don't know why).
It seemed to have been stuck in my outbox, I thought it was another unrelated mail (which I sent this morning) so I hit go.
BTW, I think this patch went in already...
Quoted 3 lines
I've
been looking at NetBSD and uClibc, and both havepthread_attr_setstacksize.
Quoted 6 lines
Quoted 2 lines
What I don't really like is the hardcoded (16 * 1024) value, how do you know this is greater than
PTHREAD_STACK_MIN?
pthread_attr_setstacksize(1)specifically says thatPTHREAD_STACK_MINis 16K, but I don't know if that is a real specified thing or just Linux bias in the manpage.
PTHREAD_STACK_MIN it's also present on NetBSD and uClibc, so I guess we should use PTHREAD_STACK_MIN (or PTHREAD_STACK_MIN * 2) if the default stack size has to be changed to some sensible value (which I still don't think it has to). Can we guarantee PTHREAD_STACK_MIN is not going to change to something greater than 16k thus breaking this patch?
Quoted 5 lines
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_attr_setstacksize.html doesn't seem to say anything about the value of
PTHREAD_STACK_MIN.How about we fix this when we come across a system which has a smaller minimum stack?
Quoted 8 lines
Quoted 3 lines
Frankly I don't understand why do we have to touch this, even if you requested 256MB of stack it won't we allocated until you get a page fault, so you are only using the physical memory you need.
With 256M stack 4 threads would take up 1G of your virtual address space (regardless of whether it is populated or not), and 12 threads would take up 3G -- which is the whole virtual address space of a 32 bit process, which is rather limiting.
This should be set by the OS to a sensible value that allows creating a reasonable number of threads, given that the default in Linux is 8MB, it will allow you to create 375 threads on a 32bit system, which looks like a pretty high number to me. Anyway limiting the stack size of a single thread won't make much of difference regarding this, because all the other threads will still take the default value.
Quoted 38 lines · Roger Pau Monne
Quoted 30 lines · Ian Campbell
Quoted 15 lines · Roger Pau Monne
Quoted 14 lines · Ian Campbell
Quoted 6 lines · Simon Rowe
Quoted 2 lines · On Wednesday 30 May 2012 10:40:15 Ian Campbell wrote:
If this little trick applies to both NetBSD and uclibc too then I guess it would be OK, otherwise I think autoconf is necessary.
It doesn't look to my untrained eye that xenstore is autoconf-aware. The makefile unilaterally sets
USE_PTHREADfor example.It has autoconf stuff available to it, I think, it just hasn't had cause to use it yet...
(
USE_PTHREADis a bit of an odd one anyway, it refers only to the client library/cmdline tools and is for building against libc's which don't have pthreads)Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Shall I just drop this test for now and if/when xenstore is updated to use autoconf it can be addressed then?
I'd like to here from Roger about what this means for NetBSD and uclibc, if it works on those then I think it is fine to do this.
Sorry for the delay, I just received this today (don't know why).
It seemed to have been stuck in my outbox, I thought it was another unrelated mail (which I sent this morning) so I hit go.
BTW, I think this patch went in already...
Quoted 3 lines
I've
been looking at NetBSD and uClibc, and both havepthread_attr_setstacksize.Quoted 6 lines
Quoted 2 lines
What I don't really like is the hardcoded (16 * 1024) value, how do you know this is greater than
PTHREAD_STACK_MIN?
pthread_attr_setstacksize(1)specifically says thatPTHREAD_STACK_MINis 16K, but I don't know if that is a real specified thing or just Linux bias in the manpage.
Quoted 5 lines
PTHREAD_STACK_MINit's also present on NetBSD and uClibc, so I guess we should usePTHREAD_STACK_MIN(orPTHREAD_STACK_MIN* 2) if the default stack size has to be changed to some sensible value (which I still don't think it has to). Can we guaranteePTHREAD_STACK_MINis not going to change to something greater than 16k thus breaking this patch?
Using PTHREAD_STACK_MIN sounds like a good idea, since this patch is in can you send an incremental update? You could do max(PTHREAD_STACK_MIN, 16*1204) perhaps?
Quoted 14 lines
Quoted 5 lines
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_attr_setstacksize.html doesn't seem to say anything about the value of
PTHREAD_STACK_MIN.How about we fix this when we come across a system which has a smaller minimum stack?
Quoted 8 lines
Quoted 3 lines
Frankly I don't understand why do we have to touch this, even if you requested 256MB of stack it won't we allocated until you get a page fault, so you are only using the physical memory you need.
With 256M stack 4 threads would take up 1G of your virtual address space (regardless of whether it is populated or not), and 12 threads would take up 3G -- which is the whole virtual address space of a 32 bit process, which is rather limiting.
Quoted 4 lines
This should be set by the OS to a sensible value that allows creating a reasonable number of threads, given that the default in Linux is 8MB, it will allow you to create 375 threads on a 32bit system, which looks like a pretty high number to me.
It won't be that high in practice, due to all the other users of virtual memory as well as fragmentation (i.e. libraries get loaded all over the place and perhaps don't leave large contiguous chunks etc).
Quoted 3 lines
Anyway limiting the stack size of a single
thread won't make much of difference regarding this, because all the other threads will still take the default value.
I don't recall if I was the original author of this patch or if I was just involved at the time but this limit really did get hit on XenServer. IIRC care is taken in whichever process it was to reduce all the threads to reasonable stack sizes. Its only polite that libxenstore as a library minimises it's stack usage in order that users of the library can in turn minimise it's own footprint.
Ian.
Quoted 3 lines · Ian Campbell
Using
PTHREAD_STACK_MINsounds like a good idea, since this patch is in can you send an incremental update? You could domax(PTHREAD_STACK_MIN, 16*1204)perhaps?
If we are trying to reduce stack as much as possible shouldn't we use PTHREAD_STACK_MIN directly?
Quoted 7 lines · Roger Pau Monne
Quoted 3 lines · Ian Campbell
Using
PTHREAD_STACK_MINsounds like a good idea, since this patch is in can you send an incremental update? You could domax(PTHREAD_STACK_MIN, 16*1204)perhaps?If we are trying to reduce stack as much as possible shouldn't we use
PTHREAD_STACK_MINdirectly?
My thinking was that 16K has been validated as a minimum. I/we don't know what would happen e.g. if PTHREAD_STACK_MIN was 8K without doing further investigative work.
Ian.
Roger Pau Monne writes ("Re: [Xen-devel] [PATCH] xs: set read_thread stacksize"):
Quoted 3 lines
Frankly I don't understand why do we have to touch this, even if you requested 256MB of stack it won't we allocated until you get a page fault, so you are only using the physical memory you need.
It uses up a lot of address space on 32-bit if nothing else.
Ian.