glibc/support
Adhemerval Zanella 0edbf12301 nptl: Invert the mmap/mprotect logic on allocated stacks (BZ#18988)
Current allocate_stack logic for create stacks is to first mmap all
the required memory with the desirable memory and then mprotect the
guard area with PROT_NONE if required.  Although it works as expected,
it pessimizes the allocation because it requires the kernel to actually
increase commit charge (it counts against the available physical/swap
memory available for the system).

The only issue is to actually check this change since side-effects are
really Linux specific and to actually account them it would require a
kernel specific tests to parse the system wide information.  On the kernel
I checked /proc/self/statm does not show any meaningful difference for
vmm and/or rss before and after thread creation.  I could only see
really meaningful information checking on system wide /proc/meminfo
between thread creation: MemFree, MemAvailable, and Committed_AS shows
large difference without the patch.  I think trying to use these
kind of information on a testcase is fragile.

The BZ#18988 reports shows that the commit pages are easily seen with
mlockall (MCL_FUTURE) (with lock all pages that become mapped in the
process) however a more straighfoward testcase shows that pthread_create
could be faster using this patch:

--
static const int inner_count = 256;
static const int outer_count = 128;

static
void *thread1(void *arg)
{
  return NULL;
}

static
void *sleeper(void *arg)
{
  pthread_t ts[inner_count];
  for (int i = 0; i < inner_count; i++)
    pthread_create (&ts[i], &a, thread1, NULL);
  for (int i = 0; i < inner_count; i++)
    pthread_join (ts[i], NULL);

  return NULL;
}

int main(void)
{
  pthread_attr_init(&a);
  pthread_attr_setguardsize(&a, 1<<20);
  pthread_attr_setstacksize(&a, 1134592);

  pthread_t ts[outer_count];
  for (int i = 0; i < outer_count; i++)
    pthread_create(&ts[i], &a, sleeper, NULL);
  for (int i = 0; i < outer_count; i++)
    pthread_join(ts[i], NULL);
    assert(r == 0);
  }
  return 0;
}

--

On x86_64 (4.4.0-45-generic, gcc 5.4.0) running the small benchtests
I see:

$ time ./test

real	0m3.647s
user	0m0.080s
sys	0m11.836s

While with the patch I see:

$ time ./test

real	0m0.696s
user	0m0.040s
sys	0m1.152s

So I added a pthread_create benchtest (thread_create) which check
the thread creation latency.  As for the simple benchtests, I saw
improvements in thread creation on all architectures I tested the
change.

Checked on x86_64-linux-gnu, i686-linux-gnu, aarch64-linux-gnu,
arm-linux-gnueabihf, powerpc64le-linux-gnu, sparc64-linux-gnu,
and sparcv9-linux-gnu.

	[BZ #18988]
	* benchtests/thread_create-inputs: New file.
	* benchtests/thread_create-source.c: Likewise.
	* support/xpthread_attr_setguardsize.c: Likewise.
	* support/Makefile (libsupport-routines): Add
	xpthread_attr_setguardsize object.
	* support/xthread.h: Add xpthread_attr_setguardsize prototype.
	* benchtests/Makefile (bench-pthread): Add thread_create.
	* nptl/allocatestack.c (allocate_stack): Call mmap with PROT_NONE and
	then mprotect the required area.
2017-06-14 17:22:35 -03:00
..
Makefile nptl: Invert the mmap/mprotect logic on allocated stacks (BZ#18988) 2017-06-14 17:22:35 -03:00
README
README-testing.c
capture_subprocess.h resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
check.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
check.h support: Expose TEST_VERIFY_EXIT behavior to GCC optimizers 2017-06-09 14:08:13 +02:00
check_addrinfo.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
check_dns_packet.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
check_hostent.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
check_netent.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
check_nss.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
delayed_exit.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
format_nss.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
ignore_stderr.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
namespace.h resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
oom_error.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
resolv_test.c Fix sys/socket.h namespace issues from sys/uio.h inclusion (bug 21426). 2017-04-25 17:52:47 +00:00
resolv_test.h resolv: Reduce EDNS payload size to 1200 bytes [BZ #21361] 2017-04-13 13:09:38 +02:00
run_diff.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
set_fortify_handler.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
support-xstat.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
support.h resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
support_become_root.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
support_can_chroot.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
support_capture_subprocess.c Add internal facility for dynamic array handling 2017-06-02 11:59:28 +02:00
support_capture_subprocess_check.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
support_enter_network_namespace.c support: Add error checking to close system calls [BZ #21244] 2017-03-15 13:33:40 +01:00
support_format_address_family.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
support_format_addrinfo.c support_format_addrinfo: Fix flags and canonname formatting 2017-05-11 11:51:34 +02:00
support_format_dns_packet.c support_format_dns_packet: Fix CNAME and multiple RR handling 2017-03-15 13:33:40 +01:00
support_format_herrno.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
support_format_hostent.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
support_format_netent.c support: struct netent portability fix for support_format_netent 2017-01-01 09:35:45 +01:00
support_isolate_in_subprocess.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
support_record_failure.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
support_run_diff.c support: Add error checking to close system calls [BZ #21244] 2017-03-15 13:33:40 +01:00
support_shared_allocate.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
support_test_main.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
support_test_verify_impl.c support: Expose TEST_VERIFY_EXIT behavior to GCC optimizers 2017-06-09 14:08:13 +02:00
support_write_file_string.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
temp_file-internal.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
temp_file.c support: Prevent multiple deletion of temporary files 2017-05-08 16:20:40 +02:00
temp_file.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
test-driver.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
test-driver.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
tst-support-namespace.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
tst-support_capture_subprocess.c Add internal facility for dynamic array handling 2017-06-02 11:59:28 +02:00
tst-support_format_dns_packet.c Another round of inclusion fixes for _ISOMAC testsuite. 2017-03-22 08:44:32 -04:00
tst-support_record_failure-2.sh Another round of inclusion fixes for _ISOMAC testsuite. 2017-03-22 08:44:32 -04:00
tst-support_record_failure.c Another round of inclusion fixes for _ISOMAC testsuite. 2017-03-22 08:44:32 -04:00
write_message.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xaccept.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xaccept4.c Assume that accept4 is always available and works 2017-04-19 07:44:48 +02:00
xasprintf.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xbind.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xcalloc.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xchroot.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
xclose.c support: Add error checking to close system calls [BZ #21244] 2017-03-15 13:33:40 +01:00
xconnect.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xdup2.c Add internal facility for dynamic array handling 2017-06-02 11:59:28 +02:00
xfclose.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xfopen.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xfork.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xgetsockname.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xlisten.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xmalloc.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xmemstream.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xmemstream.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xmkdir.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
xmmap.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xmunmap.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xopen.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
xpipe.c Add internal facility for dynamic array handling 2017-06-02 11:59:28 +02:00
xpoll.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_attr_destroy.c Bug 20116: Fix use after free in pthread_create() 2017-01-28 19:21:44 -05:00
xpthread_attr_init.c Bug 20116: Fix use after free in pthread_create() 2017-01-28 19:21:44 -05:00
xpthread_attr_setdetachstate.c Bug 20116: Fix use after free in pthread_create() 2017-01-28 19:21:44 -05:00
xpthread_attr_setguardsize.c nptl: Invert the mmap/mprotect logic on allocated stacks (BZ#18988) 2017-06-14 17:22:35 -03:00
xpthread_attr_setstacksize.c Bug 20116: Fix use after free in pthread_create() 2017-01-28 19:21:44 -05:00
xpthread_barrier_destroy.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_barrier_init.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_barrier_wait.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_cancel.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_check_return.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_cond_wait.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_create.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_detach.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_join.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_mutex_consistent.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_mutex_destroy.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_mutex_init.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_mutex_lock.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_mutex_unlock.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_mutexattr_destroy.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_mutexattr_init.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_mutexattr_setprotocol.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_mutexattr_setpshared.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_mutexattr_setrobust.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_mutexattr_settype.c nptl: Add tst-robust-fork 2017-01-27 06:53:20 +01:00
xpthread_once.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_sigmask.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_spin_lock.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xpthread_spin_unlock.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xrealloc.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xrecvfrom.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xsendto.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xsetsockopt.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xsignal.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xsocket.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xsocket.h Assume that accept4 is always available and works 2017-04-19 07:44:48 +02:00
xstdio.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xstrdup.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xthread.h nptl: Invert the mmap/mprotect logic on allocated stacks (BZ#18988) 2017-06-14 17:22:35 -03:00
xunistd.h resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
xwaitpid.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
xwrite.c support: Use %td for pointer difference in xwrite 2017-01-01 09:27:54 +01:00

README

This subdirectory contains infrastructure which is not put into
installed libraries, but may be linked into programs (installed or
not) and tests.

# Error-checking wrappers

These wrappers test for error return codes an terminate the process on
error.  They are declared in these header files:

* support.h
* xsignal.h
* xthread.h

In general, new wrappers should be added to support.h if possible.
However, support.h must remain fully compatible with C90 and therefore
cannot include headers which use identifers not reserved in C90.  If
the wrappers need additional types, additional headers such as
signal.h need to be introduced.

# Test framework

The test framework provides a main program for tests, including a
timeout for hanging tests.  See README-testing.c for a minimal
example, and test-driver.c for details how to use it.  The following
header files provide related declarations:

* check.h
* temp_file.h
* test-driver.h