diff --git a/src/hotspot/share/gc/z/zNMethodBarrier.cpp b/src/hotspot/share/gc/z/zNMethodBarrier.cpp new file mode 100644 index 0000000..bc8abfb --- /dev/null +++ b/src/hotspot/share/gc/z/zNMethodBarrier.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#include "precompiled.hpp" +#include "code/codeCache.hpp" +#include "code/nmethod.hpp" +#include "code/nmethodBarrier.hpp" +#include "gc/z/zGlobals.hpp" +#include "gc/z/zResurrection.inline.hpp" +#include "gc/z/zOopClosures.hpp" +#include "gc/z/zNMethodBarrier.hpp" +#include "gc/z/zThreadLocalData.hpp" +#include "logging/log.hpp" +#include "memory/resourceArea.hpp" +#include "runtime/interfaceSupport.inline.hpp" + +bool ZNMethodBarrier::enter(nmethod* nm, nmethodBarrier* barrier) { + nmethodBarrierState* state = nm->entry_barrier_state(); + + if (!state->enter()) { + log_trace(nmethod_barrier)("skipping critical zone for %p", nm); + return false; + } + + // this is a critical zone, all threads that have entered the zone must be done + // before any of the threads are allowed to exit + log_trace(nmethod_barrier)("entered critical zone for %p", nm); + return true; +} + +void ZNMethodBarrier::leave(nmethod* nm, nmethodBarrier* barrier) { + nmethodBarrierState* state = nm->entry_barrier_state(); + + state->leave(); + + log_trace(nmethod_barrier)("left critical zone for %p", nm); + if (barrier != NULL) { + state->wait(); + log_trace(nmethod_barrier)("wait over for %p", nm); + } +} + +bool ZNMethodBarrier::phantom_load(nmethod* nm, nmethodBarrier* barrier) { + assert(!nm->is_zombie(), "no zombies allowed"); +#ifdef ASSERT + bool verify = Thread::current()->is_Java_thread(); +#endif + debug_only(NoSafepointVerifier no_safepoints(verify, verify);) // Safepointing here could be fatal + + log_trace(nmethod_barrier)("nmethod entry barrier: " PTR_FORMAT, p2i(nm)); + + if (ZResurrection::is_blocked()) { + if (!nm->is_in_use() || nm->is_unloading()) { + return false; + } else { + if (enter(nm, barrier)) { + ZPhantomKeepAliveOopClosure keep_alive; + nm->oops_do(&keep_alive); + // Hope for the best, but plan for the worst. We do not yet know if + // any classes got unloaded. So we assume that could happen and clean + // more aggressively from mutators. + cleanup_nmethod(nm, /*unloading_occurred*/ true); + leave(nm, barrier); + } + } + } else { + if (enter(nm, barrier)) { + load_barrier(nm); + nm->fix_oop_relocations(); + leave(nm, barrier); + } + } + + barrier->disarm(); + + return true; +} + +void ZNMethodBarrier::load_barrier(nmethod* nm) { + ZLoadBarrierOopClosure closure; + nm->oops_do(&closure, false); +} + +bool ZNMethodBarrier::nmethod_barrier(nmethod* nm, nmethodBarrier* barrier) { + if (!nm->is_armed()) { + return true; + } + bool may_enter = phantom_load(nm, barrier); + if (!may_enter) { + guarantee(!nm->is_osr_method(), "Should not reach here"); + log_trace(nmethod_barrier)("Deoptimizing nmethod: " PTR_FORMAT, p2i(nm)); + barrier->deoptimize(); + } + return may_enter; +} + +bool ZNMethodBarrier::osr_nmethod_barrier(nmethod* nm) { + log_trace(nmethod_barrier)("Running osr nmethod entry barrier: " PTR_FORMAT, p2i(nm)); + + /* This check depends on the invariant that all nmethods that are deoptimized / made not entrant + * are NOT disarmed. + * This invariant is important because a method can be deoptimized after the method have been + * resolved / looked up by OSR by another thread. By not deoptimizing them we guarantee that + * a deoptimized method will always hit the barrier and come to the same conclusion - deoptimize + * */ + if (!nm->is_armed()) { + return true; + } + + nmethodBarrier nmbarrier(nm); + return phantom_load(nm, &nmbarrier); +} + +bool ZNMethodBarrier::on_nmethod_entry_barrier(nmethod* nm, nmethodBarrier* nmbarrier) { + if (nmbarrier == NULL) { + // Called on OSR entry + return osr_nmethod_barrier(nm); + } else { + // Called upon first entry after being armed + return nmethod_barrier(nm, nmbarrier); + } +} + +void ZNMethodBarrier::cleanup_nmethod(nmethod* nm, bool unloading_occurred) { + ResourceMark mark; + nm->unload_nmethod_caches(unloading_occurred); + nm->fix_oop_relocations(); +}