Skip to content

Commit

Permalink
8303852: current_stack_region() gets called twice unnecessarily
Browse files Browse the repository at this point in the history
Reviewed-by: stuefe, pchilanomate
  • Loading branch information
David Holmes committed Aug 23, 2023
1 parent c077be4 commit 4a50e87
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 221 deletions.
21 changes: 7 additions & 14 deletions src/hotspot/os/aix/os_aix.cpp
Expand Up @@ -2948,29 +2948,22 @@ void os::Aix::initialize_libperfstat() {
/////////////////////////////////////////////////////////////////////////////
// thread stack

// Get the current stack base from the OS (actually, the pthread library).
// Note: usually not page aligned.
address os::current_stack_base() {
AixMisc::stackbounds_t bounds;
bool rc = AixMisc::query_stack_bounds_for_current_thread(&bounds);
guarantee(rc, "Unable to retrieve stack bounds.");
return bounds.base;
}

// Get the current stack size from the OS (actually, the pthread library).
// Get the current stack base and size from the OS (actually, the pthread library).
// Note: base usually not page aligned.
// Returned size is such that (base - size) is always aligned to page size.
size_t os::current_stack_size() {
void os::current_stack_base_and_size(address* stack_base, size_t* stack_size) {
AixMisc::stackbounds_t bounds;
bool rc = AixMisc::query_stack_bounds_for_current_thread(&bounds);
guarantee(rc, "Unable to retrieve stack bounds.");
// Align the returned stack size such that the stack low address
*stack_base = bounds.base;

// Align the reported stack size such that the stack low address
// is aligned to page size (Note: base is usually not and we do not care).
// We need to do this because caller code will assume stack low address is
// page aligned and will place guard pages without checking.
address low = bounds.base - bounds.size;
address low_aligned = (address)align_up(low, os::vm_page_size());
size_t s = bounds.base - low_aligned;
return s;
*stack_size = bounds.base - low_aligned;
}

// Get the default path to the core file
Expand Down
39 changes: 14 additions & 25 deletions src/hotspot/os/linux/os_linux.cpp
Expand Up @@ -5370,19 +5370,22 @@ bool os::start_debugging(char *buf, int buflen) {
// | |/
// P2 +------------------------+ Thread::stack_base()
//
// ** P1 (aka bottom) and size (P2 = P1 - size) are the address and stack size
// ** P1 (aka bottom) and size are the address and stack size
// returned from pthread_attr_getstack().
// ** P2 (aka stack top or base) = P1 + size
// ** If adjustStackSizeForGuardPages() is true the guard pages have been taken
// out of the stack size given in pthread_attr. We work around this for
// threads created by the VM. We adjust bottom to be P1 and size accordingly.
//
#ifndef ZERO
static void current_stack_region(address * bottom, size_t * size) {
void os::current_stack_base_and_size(address* base, size_t* size) {
address bottom;
if (os::is_primordial_thread()) {
// primordial thread needs special handling because pthread_getattr_np()
// may return bogus value.
*bottom = os::Linux::initial_thread_stack_bottom();
*size = os::Linux::initial_thread_stack_size();
bottom = os::Linux::initial_thread_stack_bottom();
*size = os::Linux::initial_thread_stack_size();
*base = bottom + *size;
} else {
pthread_attr_t attr;

Expand All @@ -5397,42 +5400,28 @@ static void current_stack_region(address * bottom, size_t * size) {
}
}

if (pthread_attr_getstack(&attr, (void **)bottom, size) != 0) {
if (pthread_attr_getstack(&attr, (void **)&bottom, size) != 0) {
fatal("Cannot locate current stack attributes!");
}

*base = bottom + *size;

if (os::Linux::adjustStackSizeForGuardPages()) {
size_t guard_size = 0;
rslt = pthread_attr_getguardsize(&attr, &guard_size);
if (rslt != 0) {
fatal("pthread_attr_getguardsize failed with error = %d", rslt);
}
*bottom += guard_size;
*size -= guard_size;
bottom += guard_size;
*size -= guard_size;
}

pthread_attr_destroy(&attr);

}
assert(os::current_stack_pointer() >= *bottom &&
os::current_stack_pointer() < *bottom + *size, "just checking");
}

address os::current_stack_base() {
address bottom;
size_t size;
current_stack_region(&bottom, &size);
return (bottom + size);
assert(os::current_stack_pointer() >= bottom &&
os::current_stack_pointer() < *base, "just checking");
}

size_t os::current_stack_size() {
// This stack size includes the usable stack and HotSpot guard pages
// (for the threads that have Hotspot guard pages).
address bottom;
size_t size;
current_stack_region(&bottom, &size);
return size;
}
#endif

static inline struct timespec get_mtime(const char* filename) {
Expand Down
25 changes: 8 additions & 17 deletions src/hotspot/os/windows/os_windows.cpp
Expand Up @@ -425,40 +425,31 @@ int os::get_native_stack(address* stack, int frames, int toSkip) {
return captured;
}

// os::current_stack_base()
//
// Returns the base of the stack, which is the stack's
// starting address. This function must be called
// while running on the stack of the thread being queried.

address os::current_stack_base() {
void os::current_stack_base_and_size(address* stack_base, size_t* stack_size) {
MEMORY_BASIC_INFORMATION minfo;
address stack_bottom;
size_t stack_size;
size_t size;

VirtualQuery(&minfo, &minfo, sizeof(minfo));
stack_bottom = (address)minfo.AllocationBase;
stack_size = minfo.RegionSize;
stack_bottom = (address)minfo.AllocationBase;
size = minfo.RegionSize;

// Add up the sizes of all the regions with the same
// AllocationBase.
while (1) {
VirtualQuery(stack_bottom+stack_size, &minfo, sizeof(minfo));
VirtualQuery(stack_bottom + size, &minfo, sizeof(minfo));
if (stack_bottom == (address)minfo.AllocationBase) {
stack_size += minfo.RegionSize;
size += minfo.RegionSize;
} else {
break;
}
}
return stack_bottom + stack_size;
}

size_t os::current_stack_size() {
size_t sz;
MEMORY_BASIC_INFORMATION minfo;
VirtualQuery(&minfo, &minfo, sizeof(minfo));
sz = (size_t)os::current_stack_base() - (size_t)minfo.AllocationBase;
return sz;
*stack_base = stack_bottom + size;
*stack_size = size;
}

bool os::committed_in_range(address start, size_t size, address& committed_start, size_t& committed_size) {
Expand Down
38 changes: 13 additions & 25 deletions src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp
Expand Up @@ -354,22 +354,23 @@ size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
return s;
}

static void current_stack_region(address * bottom, size_t * size) {
void os::current_stack_base_and_size(address* base, size_t* size) {
address bottom;
#ifdef __APPLE__
pthread_t self = pthread_self();
void *stacktop = pthread_get_stackaddr_np(self);
*base = (address) pthread_get_stackaddr_np(self);
*size = pthread_get_stacksize_np(self);
*bottom = (address) stacktop - *size;
bottom = *base - *size;
#elif defined(__OpenBSD__)
stack_t ss;
int rslt = pthread_stackseg_np(pthread_self(), &ss);

if (rslt != 0)
fatal("pthread_stackseg_np failed with error = %d", rslt);

*bottom = (address)((char *)ss.ss_sp - ss.ss_size);
*size = ss.ss_size;
*base = (address) ss.ss_sp;
*size = ss.ss_size;
bottom = *base - *size;
#else
pthread_attr_t attr;

Expand All @@ -384,30 +385,17 @@ static void current_stack_region(address * bottom, size_t * size) {
if (rslt != 0)
fatal("pthread_attr_get_np failed with error = %d", rslt);

if (pthread_attr_getstackaddr(&attr, (void **)bottom) != 0 ||
pthread_attr_getstacksize(&attr, size) != 0) {
if (pthread_attr_getstackaddr(&attr, (void **)&bottom) != 0 ||
pthread_attr_getstacksize(&attr, size) != 0) {
fatal("Can not locate current stack attributes!");
}

*base = bottom + *size;

pthread_attr_destroy(&attr);
#endif
assert(os::current_stack_pointer() >= *bottom &&
os::current_stack_pointer() < *bottom + *size, "just checking");
}

address os::current_stack_base() {
address bottom;
size_t size;
current_stack_region(&bottom, &size);
return (bottom + size);
}

size_t os::current_stack_size() {
// stack size includes normal stack and HotSpot guard pages
address bottom;
size_t size;
current_stack_region(&bottom, &size);
return size;
assert(os::current_stack_pointer() >= bottom &&
os::current_stack_pointer() < *base, "just checking");
}

/////////////////////////////////////////////////////////////////////////////
Expand Down
44 changes: 17 additions & 27 deletions src/hotspot/os_cpu/bsd_x86/os_bsd_x86.cpp
Expand Up @@ -79,7 +79,7 @@
# include <pthread_np.h>
#endif

// needed by current_stack_region() workaround for Mavericks
// needed by current_stack_base_and_size() workaround for Mavericks
#if defined(__APPLE__)
# include <errno.h>
# include <sys/types.h>
Expand Down Expand Up @@ -709,13 +709,15 @@ size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
// | |/
// P2 +------------------------+ Thread::stack_base()
//
// ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from
// pthread_attr_getstack()
// ** P1 (aka bottom) and size are the address and stack size
// returned from pthread_attr_getstack().
// ** P2 (aka stack top or base) = P1 + size

static void current_stack_region(address * bottom, size_t * size) {
void os::current_stack_base_and_size(address* base, size_t* size) {
address bottom;
#ifdef __APPLE__
pthread_t self = pthread_self();
void *stacktop = pthread_get_stackaddr_np(self);
*base = (address) pthread_get_stackaddr_np(self);
*size = pthread_get_stacksize_np(self);
// workaround for OS X 10.9.0 (Mavericks)
// pthread_get_stacksize_np returns 128 pages even though the actual size is 2048 pages
Expand All @@ -738,16 +740,17 @@ static void current_stack_region(address * bottom, size_t * size) {
}
}
}
*bottom = (address) stacktop - *size;
bottom = *base - *size;
#elif defined(__OpenBSD__)
stack_t ss;
int rslt = pthread_stackseg_np(pthread_self(), &ss);

if (rslt != 0)
fatal("pthread_stackseg_np failed with error = %d", rslt);

*bottom = (address)((char *)ss.ss_sp - ss.ss_size);
*size = ss.ss_size;
*base = (address) ss.ss_sp;
*size = ss.ss_size;
bottom = *base - *size;
#else
pthread_attr_t attr;

Expand All @@ -762,30 +765,17 @@ static void current_stack_region(address * bottom, size_t * size) {
if (rslt != 0)
fatal("pthread_attr_get_np failed with error = %d", rslt);

if (pthread_attr_getstackaddr(&attr, (void **)bottom) != 0 ||
pthread_attr_getstacksize(&attr, size) != 0) {
if (pthread_attr_getstackaddr(&attr, (void **)&bottom) != 0 ||
pthread_attr_getstacksize(&attr, size) != 0) {
fatal("Can not locate current stack attributes!");
}

*base = bottom + *size;

pthread_attr_destroy(&attr);
#endif
assert(os::current_stack_pointer() >= *bottom &&
os::current_stack_pointer() < *bottom + *size, "just checking");
}

address os::current_stack_base() {
address bottom;
size_t size;
current_stack_region(&bottom, &size);
return (bottom + size);
}

size_t os::current_stack_size() {
// stack size includes normal stack and HotSpot guard pages
address bottom;
size_t size;
current_stack_region(&bottom, &size);
return size;
assert(os::current_stack_pointer() >= bottom &&
os::current_stack_pointer() < *base, "just checking");
}

/////////////////////////////////////////////////////////////////////////////
Expand Down

1 comment on commit 4a50e87

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.