Skip to content

Commit

Permalink
8303210: [linux, Windows] Make UseSystemMemoryBarrier available as pr…
Browse files Browse the repository at this point in the history
…oduct flag

Reviewed-by: dholmes, rehn
  • Loading branch information
TheRealMDoerr committed Apr 3, 2023
1 parent 336a23e commit 4de24cd
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/hotspot/cpu/arm/sharedRuntime_arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,9 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
__ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));

// make sure the store is observed before reading the SafepointSynchronize state and further mem refs
__ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad | MacroAssembler::StoreStore), Rtemp);
if (!UseSystemMemoryBarrier) {
__ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad | MacroAssembler::StoreStore), Rtemp);
}

__ safepoint_poll(R2, call_safepoint_runtime);
__ ldr_u32(R3, Address(Rthread, JavaThread::suspend_flags_offset()));
Expand Down
6 changes: 4 additions & 2 deletions src/hotspot/cpu/arm/templateInterpreterGenerator_arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,10 @@ address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
__ mov(Rtemp, _thread_in_native_trans);
__ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));

// Force this write out before the read below
__ membar(MacroAssembler::StoreLoad, Rtemp);
// Force this write out before the read below
if (!UseSystemMemoryBarrier) {
__ membar(MacroAssembler::StoreLoad, Rtemp);
}

// Protect the return value in the interleaved code: save it to callee-save registers.
__ mov(Rsaved_result_lo, R0);
Expand Down
4 changes: 3 additions & 1 deletion src/hotspot/cpu/riscv/downcallLinker_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ void DowncallStubGenerator::generate() {
__ sw(t0, Address(xthread, JavaThread::thread_state_offset()));

// Force this write out before the read below
__ membar(MacroAssembler::AnyAny);
if (!UseSystemMemoryBarrier) {
__ membar(MacroAssembler::AnyAny);
}

Label L_after_safepoint_poll;
Label L_safepoint_poll_slow_path;
Expand Down
5 changes: 4 additions & 1 deletion src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "prims/methodHandles.hpp"
#include "runtime/continuation.hpp"
#include "runtime/continuationEntry.inline.hpp"
#include "runtime/globals.hpp"
#include "runtime/jniHandles.hpp"
#include "runtime/safepointMechanism.hpp"
#include "runtime/sharedRuntime.hpp"
Expand Down Expand Up @@ -1746,7 +1747,9 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
__ sw(t0, Address(xthread, JavaThread::thread_state_offset()));

// Force this write out before the read below
__ membar(MacroAssembler::AnyAny);
if (!UseSystemMemoryBarrier) {
__ membar(MacroAssembler::AnyAny);
}

// check for safepoint operation in progress and/or pending suspend requests
{
Expand Down
5 changes: 4 additions & 1 deletion src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "runtime/arguments.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/globals.hpp"
#include "runtime/jniHandles.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/stubRoutines.hpp"
Expand Down Expand Up @@ -1159,7 +1160,9 @@ address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
__ sw(t0, Address(xthread, JavaThread::thread_state_offset()));

// Force this write out before the read below
__ membar(MacroAssembler::AnyAny);
if (!UseSystemMemoryBarrier) {
__ membar(MacroAssembler::AnyAny);
}

// check for safepoint operation in progress and/or pending suspend requests
{
Expand Down
4 changes: 3 additions & 1 deletion src/hotspot/cpu/s390/sharedRuntime_s390.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1838,7 +1838,9 @@ nmethod *SharedRuntime::generate_native_wrapper(MacroAssembler *masm,
save_native_result(masm, ret_type, workspace_slot_offset); // Make Z_R2 available as work reg.

// Force this write out before the read below.
__ z_fence();
if (!UseSystemMemoryBarrier) {
__ z_fence();
}

__ safepoint_poll(sync, Z_R1);

Expand Down
4 changes: 3 additions & 1 deletion src/hotspot/cpu/s390/templateInterpreterGenerator_s390.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,9 @@ address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
// synchronization is progress, and escapes.

__ set_thread_state(_thread_in_native_trans);
__ z_fence();
if (!UseSystemMemoryBarrier) {
__ z_fence();
}

// Now before we return to java we must look for a current safepoint
// (a new safepoint can not start since we entered native_trans).
Expand Down
8 changes: 5 additions & 3 deletions src/hotspot/cpu/x86/sharedRuntime_x86_32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1772,9 +1772,11 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
__ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native_trans);

// Force this write out before the read below
__ membar(Assembler::Membar_mask_bits(
Assembler::LoadLoad | Assembler::LoadStore |
Assembler::StoreLoad | Assembler::StoreStore));
if (!UseSystemMemoryBarrier) {
__ membar(Assembler::Membar_mask_bits(
Assembler::LoadLoad | Assembler::LoadStore |
Assembler::StoreLoad | Assembler::StoreStore));
}

if (AlwaysRestoreFPU) {
// Make sure the control word is correct.
Expand Down
6 changes: 3 additions & 3 deletions src/hotspot/os/linux/systemMemoryBarrier_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ static int membarrier(int cmd, unsigned int flags, int cpu_id) {
bool LinuxSystemMemoryBarrier::initialize() {
int ret = membarrier(MEMBARRIER_CMD_QUERY, 0, 0);
if (ret < 0) {
log_error(os)("MEMBARRIER_CMD_QUERY unsupported");
log_info(os)("MEMBARRIER_CMD_QUERY unsupported");
return false;
}
if (!(ret & MEMBARRIER_CMD_PRIVATE_EXPEDITED) ||
!(ret & MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED)) {
log_error(os)("MEMBARRIER PRIVATE_EXPEDITED unsupported");
log_info(os)("MEMBARRIER PRIVATE_EXPEDITED unsupported");
return false;
}
ret = membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0, 0);
guarantee_with_errno(ret == 0, "MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED failed");
log_info(os)("Using MEMBARRIER PRIVATE_EXPEDITED");
return true;
}

void LinuxSystemMemoryBarrier::emit() {
int s = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0);
guarantee_with_errno(s >= 0, "MEMBARRIER_CMD_PRIVATE_EXPEDITED failed");
}

3 changes: 3 additions & 0 deletions src/hotspot/share/runtime/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "utilities/parseInteger.hpp"
#include "utilities/powerOfTwo.hpp"
#include "utilities/stringUtils.hpp"
#include "utilities/systemMemoryBarrier.hpp"
#if INCLUDE_JFR
#include "jfr/jfr.hpp"
#endif
Expand Down Expand Up @@ -2153,6 +2154,8 @@ jint Arguments::parse_vm_init_args(const JavaVMInitArgs *vm_options_args,

os::init_container_support();

SystemMemoryBarrier::initialize();

// Do final processing now that all arguments have been parsed
result = finalize_vm_init_args(patch_mod_javabase);
if (result != JNI_OK) {
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/runtime/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,8 +1293,8 @@ const int ObjectAlignmentInBytes = 8;
"Delay in milliseconds for option SafepointTimeout") \
range(0, max_intx LP64_ONLY(/MICROUNITS)) \
\
product(bool, UseSystemMemoryBarrier, false, EXPERIMENTAL, \
"Try to enable system memory barrier") \
product(bool, UseSystemMemoryBarrier, false, \
"Try to enable system memory barrier if supported by OS") \
\
product(intx, NmethodSweepActivity, 4, \
"Removes cold nmethods from code cache if > 0. Higher values " \
Expand Down
9 changes: 0 additions & 9 deletions src/hotspot/share/runtime/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
#include "utilities/dtrace.hpp"
#include "utilities/events.hpp"
#include "utilities/macros.hpp"
#include "utilities/systemMemoryBarrier.hpp"
#include "utilities/vmError.hpp"
#if INCLUDE_JVMCI
#include "jvmci/jvmci.hpp"
Expand Down Expand Up @@ -552,14 +551,6 @@ jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
// crash Linux VM, see notes in os_linux.cpp.
main_thread->stack_overflow_state()->create_stack_guard_pages();

if (UseSystemMemoryBarrier) {
if (!SystemMemoryBarrier::initialize()) {
vm_shutdown_during_initialization("Failed to initialize the requested system memory barrier synchronization.");
return JNI_EINVAL;
}
log_debug(os)("Using experimental system memory barrier synchronization");
}

// Initialize Java-Level synchronization subsystem
ObjectMonitor::Initialize();
ObjectSynchronizer::initialize();
Expand Down
17 changes: 14 additions & 3 deletions src/hotspot/share/utilities/systemMemoryBarrier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#define SHARE_UTILITIES_SYSTEMMEMORYBARRIER_HPP

#include "logging/log.hpp"
#include "runtime/globals.hpp"
#include "runtime/globals_extension.hpp"
#include "utilities/globalDefinitions.hpp"

#if defined(LINUX)
Expand All @@ -38,7 +40,7 @@ typedef WindowsSystemMemoryBarrier SystemMemoryBarrierDefault;
class NoSystemMemoryBarrier {
public:
static bool initialize() {
log_error(os)("SystemMemoryBarrier not supported on this platform");
log_info(os)("SystemMemoryBarrier not supported on this platform");
return false;
}
static void emit() {
Expand All @@ -51,8 +53,17 @@ typedef NoSystemMemoryBarrier SystemMemoryBarrierDefault;
template <typename SystemMemoryBarrierImpl>
class SystemMemoryBarrierType : public AllStatic {
public:
static bool initialize() { return SystemMemoryBarrierImpl::initialize(); }
static void emit() { SystemMemoryBarrierImpl::emit(); }
static void initialize() {
if (UseSystemMemoryBarrier) {
if (!SystemMemoryBarrierImpl::initialize()) {
if (!FLAG_IS_DEFAULT(UseSystemMemoryBarrier)) {
warning("UseSystemMemoryBarrier specified, but not supported on this OS version. Use -Xlog:os=info for details.");
}
FLAG_SET_ERGO(UseSystemMemoryBarrier, false);
}
}
}
static void emit() { SystemMemoryBarrierImpl::emit(); }
};

typedef SystemMemoryBarrierType<SystemMemoryBarrierDefault> SystemMemoryBarrier;
Expand Down

1 comment on commit 4de24cd

@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.