diff options
author | 2019-11-18 17:52:55 +0000 | |
---|---|---|
committer | 2019-11-18 20:19:43 +0000 | |
commit | 8ba9738d55c11dfb7f85a7de9b1d6a4e1225ad20 (patch) | |
tree | 5aabe6d6af38514af0822ab9529a39f542aaa175 /openjdkjvmti/alloc_manager.cc | |
parent | 0d508a01106746e0d8865752850f4f03bcce1e01 (diff) |
Revert^2 "Initial support for adding virtuals with structural redefinition"
This reverts commit d0e354e54da2f2e79b6fc2ff0e13d53c74976b08.
We were incorrectly continuing to use an ObjPtr over a suspend point
(in this case a CollectGarbageInternal). This led for the ObjPtr to be
invalidated. Instead we should have been using a Handle (that already
exists).
Reason for revert: Fixed stale ObjPtr use.
Test: ./test.py --host
Test: ./test/testrunner/run_build_test_target.py -j80 art-debug-gc
Bug: 134162467
Bug: 144168550
Bug: 144590579
Change-Id: Ied9856a707d377e97d34c7450376caf2a0b83255
Diffstat (limited to 'openjdkjvmti/alloc_manager.cc')
-rw-r--r-- | openjdkjvmti/alloc_manager.cc | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/openjdkjvmti/alloc_manager.cc b/openjdkjvmti/alloc_manager.cc new file mode 100644 index 0000000000..597ab05ba0 --- /dev/null +++ b/openjdkjvmti/alloc_manager.cc @@ -0,0 +1,187 @@ + +/* Copyright (C) 2019 The Android Open Source Project + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file implements interfaces from the file jvmti.h. This implementation + * is licensed under the same terms as the file jvmti.h. The + * copyright and license information for the file jvmti.h follows. + * + * Copyright (c) 2003, 2011, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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 "alloc_manager.h" + +#include <atomic> +#include <sstream> + +#include "base/logging.h" +#include "gc/allocation_listener.h" +#include "gc/heap.h" +#include "handle.h" +#include "mirror/class-inl.h" +#include "runtime.h" +#include "scoped_thread_state_change-inl.h" +#include "scoped_thread_state_change.h" +#include "thread-current-inl.h" +#include "thread_list.h" +#include "thread_pool.h" + +namespace openjdkjvmti { + +template<typename T> +void AllocationManager::PauseForAllocation(art::Thread* self, T msg) { + // The suspension can pause us for arbitrary times. We need to do it to sleep unfortunately. So we + // do test, suspend, test again, sleep, repeat. + std::string cause; + const bool is_logging = VLOG_IS_ON(plugin); + while (true) { + // We always return when there is no pause and we are runnable. + art::Thread* pausing_thread = allocations_paused_thread_.load(std::memory_order_seq_cst); + if (LIKELY(pausing_thread == nullptr || pausing_thread == self)) { + return; + } + if (UNLIKELY(is_logging && cause.empty())) { + cause = msg(); + } + art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended); + art::MutexLock mu(self, alloc_listener_mutex_); + pausing_thread = allocations_paused_thread_.load(std::memory_order_seq_cst); + CHECK_NE(pausing_thread, self) << "We should always be setting pausing_thread = self!" + << " How did this happen? " << *self; + if (pausing_thread != nullptr) { + VLOG(plugin) << "Suspending " << *self << " due to " << cause << ". Allocation pause " + << "initiated by " << *pausing_thread; + alloc_pause_cv_.Wait(self); + } + } +} + +extern AllocationManager* gAllocManager; +AllocationManager* AllocationManager::Get() { + return gAllocManager; +} + +void JvmtiAllocationListener::ObjectAllocated(art::Thread* self, + art::ObjPtr<art::mirror::Object>* obj, + size_t cnt) { + auto cb = manager_->callback_; + if (cb != nullptr && manager_->callback_enabled_.load(std::memory_order_seq_cst)) { + cb->ObjectAllocated(self, obj, cnt); + } +} + +bool JvmtiAllocationListener::HasPreAlloc() const { + return manager_->allocations_paused_thread_.load(std::memory_order_seq_cst) != nullptr; +} + +void JvmtiAllocationListener::PreObjectAllocated(art::Thread* self, + art::MutableHandle<art::mirror::Class> type, + size_t* byte_count) { + manager_->PauseForAllocation(self, [&]() REQUIRES_SHARED(art::Locks::mutator_lock_) { + std::ostringstream oss; + oss << "allocating " << *byte_count << " bytes of type " << type->PrettyClass(); + return oss.str(); + }); + if (!type->IsVariableSize()) { + *byte_count = type->GetObjectSize(); + } +} + +AllocationManager::AllocationManager() + : alloc_listener_(nullptr), + alloc_listener_mutex_("JVMTI Alloc listener", + art::LockLevel::kPostUserCodeSuspensionTopLevelLock), + alloc_pause_cv_("JVMTI Allocation Pause Condvar", alloc_listener_mutex_) { + alloc_listener_.reset(new JvmtiAllocationListener(this)); +} + +void AllocationManager::DisableAllocationCallback(art::Thread* self) { + callback_enabled_.store(false); + DecrListenerInstall(self); +} + +void AllocationManager::EnableAllocationCallback(art::Thread* self) { + IncrListenerInstall(self); + callback_enabled_.store(true); +} + +void AllocationManager::SetAllocListener(AllocationCallback* callback) { + CHECK(callback_ == nullptr) << "Already setup!"; + callback_ = callback; + alloc_listener_.reset(new JvmtiAllocationListener(this)); +} + +void AllocationManager::RemoveAllocListener() { + callback_enabled_.store(false, std::memory_order_seq_cst); + callback_ = nullptr; +} + +void AllocationManager::DecrListenerInstall(art::Thread* self) { + art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended); + art::MutexLock mu(self, alloc_listener_mutex_); + // We don't need any particular memory-order here since we're under the lock, they aren't + // changing. + if (--listener_refcount_ == 0) { + art::Runtime::Current()->GetHeap()->RemoveAllocationListener(); + } +} + +void AllocationManager::IncrListenerInstall(art::Thread* self) { + art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended); + art::MutexLock mu(self, alloc_listener_mutex_); + // We don't need any particular memory-order here since we're under the lock, they aren't + // changing. + if (listener_refcount_++ == 0) { + art::Runtime::Current()->GetHeap()->SetAllocationListener(alloc_listener_.get()); + } +} + +void AllocationManager::PauseAllocations(art::Thread* self) { + art::Thread* null_thr = nullptr; + IncrListenerInstall(self); + do { + PauseForAllocation(self, []() { return "request to pause allocations on other threads"; }); + } while (allocations_paused_thread_.compare_exchange_strong( + null_thr, self, std::memory_order_seq_cst)); + // Make sure everything else can see this and isn't in the middle of final allocation. + // Force every thread to either be suspended or pass through a barrier. + art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended); + art::Barrier barrier(0); + art::FunctionClosure fc([&](art::Thread* thr ATTRIBUTE_UNUSED) { + barrier.Pass(art::Thread::Current()); + }); + size_t requested = art::Runtime::Current()->GetThreadList()->RunCheckpoint(&fc); + barrier.Increment(self, requested); +} + +void AllocationManager::ResumeAllocations(art::Thread* self) { + CHECK_EQ(allocations_paused_thread_.load(), self) << "not paused! "; + DecrListenerInstall(self); + art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended); + art::MutexLock mu(self, alloc_listener_mutex_); + allocations_paused_thread_.store(nullptr, std::memory_order_seq_cst); + alloc_pause_cv_.Broadcast(self); +} + +} // namespace openjdkjvmti |