--- old/src/hotspot/os/linux/os_linux.cpp 2019-07-01 10:42:06.649149457 -0700 +++ new/src/hotspot/os/linux/os_linux.cpp 2019-07-01 10:42:06.321144877 -0700 @@ -826,37 +826,42 @@ _get_minstack_func == NULL ? "failed" : "succeeded"); } +static bool tls_size_inited = false; +static size_t tls_size = 0; // Returns the size of the static TLS area glibc puts on thread stacks. static size_t get_static_tls_area_size(const pthread_attr_t *attr) { - size_t tls_size = 0; - if (_get_minstack_func != NULL) { - // Obtain the pthread minstack size by calling __pthread_get_minstack. - size_t minstack_size = _get_minstack_func(attr); + if (!tls_size_inited) { + tls_size_inited = true; - // Remove non-TLS area size included in minstack size returned - // by __pthread_get_minstack() to get the static TLS size. - // In glibc before 2.27, minstack size includes guard_size. - // In glibc 2.27 and later, guard_size is automatically added - // to the stack size by pthread_create and is no longer included - // in minstack size. In both cases, the guard_size is taken into - // account, so there is no need to adjust the result for that. - // - // Although __pthread_get_minstack() is a private glibc function, - // it is expected to have a stable behavior across future glibc - // versions while glibc still allocats the static TLS blocks off - // the stack. Following is glibc 2.28 __pthread_get_minstack(): - // - // size_t - // __pthread_get_minstack (const pthread_attr_t *attr) - // { - // return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN; - // } - // - // - // The following 'minstack_size > os::vm_page_size() + PTHREAD_STACK_MIN' - // if check is done for precaution. - if (minstack_size > (size_t)os::vm_page_size() + PTHREAD_STACK_MIN) { - tls_size = minstack_size - os::vm_page_size() - PTHREAD_STACK_MIN; + if (_get_minstack_func != NULL) { + // Obtain the pthread minstack size by calling __pthread_get_minstack. + size_t minstack_size = _get_minstack_func(attr); + + // Remove non-TLS area size included in minstack size returned + // by __pthread_get_minstack() to get the static TLS size. + // In glibc before 2.27, minstack size includes guard_size. + // In glibc 2.27 and later, guard_size is automatically added + // to the stack size by pthread_create and is no longer included + // in minstack size. In both cases, the guard_size is taken into + // account, so there is no need to adjust the result for that. + // + // Although __pthread_get_minstack() is a private glibc function, + // it is expected to have a stable behavior across future glibc + // versions while glibc still allocats the static TLS blocks off + // the stack. Following is glibc 2.28 __pthread_get_minstack(): + // + // size_t + // __pthread_get_minstack (const pthread_attr_t *attr) + // { + // return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN; + // } + // + // + // The following 'minstack_size > os::vm_page_size() + PTHREAD_STACK_MIN' + // if check is done for precaution. + if (minstack_size > (size_t)os::vm_page_size() + PTHREAD_STACK_MIN) { + tls_size = minstack_size - os::vm_page_size() - PTHREAD_STACK_MIN; + } } }