1*795d594fSAndroid Build Coastguard Worker
2*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2019 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*795d594fSAndroid Build Coastguard Worker *
5*795d594fSAndroid Build Coastguard Worker * This file implements interfaces from the file jvmti.h. This implementation
6*795d594fSAndroid Build Coastguard Worker * is licensed under the same terms as the file jvmti.h. The
7*795d594fSAndroid Build Coastguard Worker * copyright and license information for the file jvmti.h follows.
8*795d594fSAndroid Build Coastguard Worker *
9*795d594fSAndroid Build Coastguard Worker * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
10*795d594fSAndroid Build Coastguard Worker * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
11*795d594fSAndroid Build Coastguard Worker *
12*795d594fSAndroid Build Coastguard Worker * This code is free software; you can redistribute it and/or modify it
13*795d594fSAndroid Build Coastguard Worker * under the terms of the GNU General Public License version 2 only, as
14*795d594fSAndroid Build Coastguard Worker * published by the Free Software Foundation. Oracle designates this
15*795d594fSAndroid Build Coastguard Worker * particular file as subject to the "Classpath" exception as provided
16*795d594fSAndroid Build Coastguard Worker * by Oracle in the LICENSE file that accompanied this code.
17*795d594fSAndroid Build Coastguard Worker *
18*795d594fSAndroid Build Coastguard Worker * This code is distributed in the hope that it will be useful, but WITHOUT
19*795d594fSAndroid Build Coastguard Worker * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20*795d594fSAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21*795d594fSAndroid Build Coastguard Worker * version 2 for more details (a copy is included in the LICENSE file that
22*795d594fSAndroid Build Coastguard Worker * accompanied this code).
23*795d594fSAndroid Build Coastguard Worker *
24*795d594fSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License version
25*795d594fSAndroid Build Coastguard Worker * 2 along with this work; if not, write to the Free Software Foundation,
26*795d594fSAndroid Build Coastguard Worker * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27*795d594fSAndroid Build Coastguard Worker *
28*795d594fSAndroid Build Coastguard Worker * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
29*795d594fSAndroid Build Coastguard Worker * or visit www.oracle.com if you need additional information or have any
30*795d594fSAndroid Build Coastguard Worker * questions.
31*795d594fSAndroid Build Coastguard Worker */
32*795d594fSAndroid Build Coastguard Worker
33*795d594fSAndroid Build Coastguard Worker #include "alloc_manager.h"
34*795d594fSAndroid Build Coastguard Worker
35*795d594fSAndroid Build Coastguard Worker #include <atomic>
36*795d594fSAndroid Build Coastguard Worker #include <sstream>
37*795d594fSAndroid Build Coastguard Worker
38*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
39*795d594fSAndroid Build Coastguard Worker #include "gc/allocation_listener.h"
40*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
41*795d594fSAndroid Build Coastguard Worker #include "handle.h"
42*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
43*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
44*795d594fSAndroid Build Coastguard Worker #include "runtime_globals.h"
45*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
46*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change.h"
47*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
48*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
49*795d594fSAndroid Build Coastguard Worker #include "thread_pool.h"
50*795d594fSAndroid Build Coastguard Worker
51*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
52*795d594fSAndroid Build Coastguard Worker
53*795d594fSAndroid Build Coastguard Worker template<typename T>
PauseForAllocation(art::Thread * self,T msg)54*795d594fSAndroid Build Coastguard Worker void AllocationManager::PauseForAllocation(art::Thread* self, T msg) {
55*795d594fSAndroid Build Coastguard Worker // The suspension can pause us for arbitrary times. We need to do it to sleep unfortunately. So we
56*795d594fSAndroid Build Coastguard Worker // do test, suspend, test again, sleep, repeat.
57*795d594fSAndroid Build Coastguard Worker std::string cause;
58*795d594fSAndroid Build Coastguard Worker const bool is_logging = VLOG_IS_ON(plugin);
59*795d594fSAndroid Build Coastguard Worker while (true) {
60*795d594fSAndroid Build Coastguard Worker // We always return when there is no pause and we are runnable.
61*795d594fSAndroid Build Coastguard Worker art::Thread* pausing_thread = allocations_paused_thread_.load(std::memory_order_seq_cst);
62*795d594fSAndroid Build Coastguard Worker if (LIKELY(pausing_thread == nullptr || pausing_thread == self)) {
63*795d594fSAndroid Build Coastguard Worker return;
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(is_logging && cause.empty())) {
66*795d594fSAndroid Build Coastguard Worker cause = msg();
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
69*795d594fSAndroid Build Coastguard Worker art::MutexLock mu(self, alloc_listener_mutex_);
70*795d594fSAndroid Build Coastguard Worker pausing_thread = allocations_paused_thread_.load(std::memory_order_seq_cst);
71*795d594fSAndroid Build Coastguard Worker CHECK_NE(pausing_thread, self) << "We should always be setting pausing_thread = self!"
72*795d594fSAndroid Build Coastguard Worker << " How did this happen? " << *self;
73*795d594fSAndroid Build Coastguard Worker if (pausing_thread != nullptr) {
74*795d594fSAndroid Build Coastguard Worker VLOG(plugin) << "Suspending " << *self << " due to " << cause << ". Allocation pause "
75*795d594fSAndroid Build Coastguard Worker << "initiated by " << *pausing_thread;
76*795d594fSAndroid Build Coastguard Worker alloc_pause_cv_.Wait(self);
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker extern AllocationManager* gAllocManager;
Get()82*795d594fSAndroid Build Coastguard Worker AllocationManager* AllocationManager::Get() {
83*795d594fSAndroid Build Coastguard Worker return gAllocManager;
84*795d594fSAndroid Build Coastguard Worker }
85*795d594fSAndroid Build Coastguard Worker
ObjectAllocated(art::Thread * self,art::ObjPtr<art::mirror::Object> * obj,size_t cnt)86*795d594fSAndroid Build Coastguard Worker void JvmtiAllocationListener::ObjectAllocated(art::Thread* self,
87*795d594fSAndroid Build Coastguard Worker art::ObjPtr<art::mirror::Object>* obj,
88*795d594fSAndroid Build Coastguard Worker size_t cnt) {
89*795d594fSAndroid Build Coastguard Worker auto cb = manager_->callback_;
90*795d594fSAndroid Build Coastguard Worker if (cb != nullptr && manager_->callback_enabled_.load(std::memory_order_seq_cst)) {
91*795d594fSAndroid Build Coastguard Worker cb->ObjectAllocated(self, obj, cnt);
92*795d594fSAndroid Build Coastguard Worker }
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker
HasPreAlloc() const95*795d594fSAndroid Build Coastguard Worker bool JvmtiAllocationListener::HasPreAlloc() const {
96*795d594fSAndroid Build Coastguard Worker return manager_->allocations_paused_ever_.load(std::memory_order_seq_cst);
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker
PreObjectAllocated(art::Thread * self,art::MutableHandle<art::mirror::Class> type,size_t * byte_count)99*795d594fSAndroid Build Coastguard Worker void JvmtiAllocationListener::PreObjectAllocated(art::Thread* self,
100*795d594fSAndroid Build Coastguard Worker art::MutableHandle<art::mirror::Class> type,
101*795d594fSAndroid Build Coastguard Worker size_t* byte_count) {
102*795d594fSAndroid Build Coastguard Worker manager_->PauseForAllocation(self, [&]() REQUIRES_SHARED(art::Locks::mutator_lock_) {
103*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
104*795d594fSAndroid Build Coastguard Worker oss << "allocating " << *byte_count << " bytes of type " << type->PrettyClass();
105*795d594fSAndroid Build Coastguard Worker return oss.str();
106*795d594fSAndroid Build Coastguard Worker });
107*795d594fSAndroid Build Coastguard Worker if (!type->IsVariableSize()) {
108*795d594fSAndroid Build Coastguard Worker *byte_count =
109*795d594fSAndroid Build Coastguard Worker std::max(art::RoundUp(static_cast<size_t>(type->GetObjectSize()), art::kObjectAlignment),
110*795d594fSAndroid Build Coastguard Worker *byte_count);
111*795d594fSAndroid Build Coastguard Worker }
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker
AllocationManager()114*795d594fSAndroid Build Coastguard Worker AllocationManager::AllocationManager()
115*795d594fSAndroid Build Coastguard Worker : alloc_listener_(nullptr),
116*795d594fSAndroid Build Coastguard Worker alloc_listener_mutex_("JVMTI Alloc listener",
117*795d594fSAndroid Build Coastguard Worker art::LockLevel::kPostUserCodeSuspensionTopLevelLock),
118*795d594fSAndroid Build Coastguard Worker alloc_pause_cv_("JVMTI Allocation Pause Condvar", alloc_listener_mutex_) {
119*795d594fSAndroid Build Coastguard Worker alloc_listener_.reset(new JvmtiAllocationListener(this));
120*795d594fSAndroid Build Coastguard Worker }
121*795d594fSAndroid Build Coastguard Worker
DisableAllocationCallback(art::Thread * self)122*795d594fSAndroid Build Coastguard Worker void AllocationManager::DisableAllocationCallback(art::Thread* self) {
123*795d594fSAndroid Build Coastguard Worker callback_enabled_.store(false);
124*795d594fSAndroid Build Coastguard Worker DecrListenerInstall(self);
125*795d594fSAndroid Build Coastguard Worker }
126*795d594fSAndroid Build Coastguard Worker
EnableAllocationCallback(art::Thread * self)127*795d594fSAndroid Build Coastguard Worker void AllocationManager::EnableAllocationCallback(art::Thread* self) {
128*795d594fSAndroid Build Coastguard Worker IncrListenerInstall(self);
129*795d594fSAndroid Build Coastguard Worker callback_enabled_.store(true);
130*795d594fSAndroid Build Coastguard Worker }
131*795d594fSAndroid Build Coastguard Worker
SetAllocListener(AllocationCallback * callback)132*795d594fSAndroid Build Coastguard Worker void AllocationManager::SetAllocListener(AllocationCallback* callback) {
133*795d594fSAndroid Build Coastguard Worker CHECK(callback_ == nullptr) << "Already setup!";
134*795d594fSAndroid Build Coastguard Worker callback_ = callback;
135*795d594fSAndroid Build Coastguard Worker alloc_listener_.reset(new JvmtiAllocationListener(this));
136*795d594fSAndroid Build Coastguard Worker }
137*795d594fSAndroid Build Coastguard Worker
RemoveAllocListener()138*795d594fSAndroid Build Coastguard Worker void AllocationManager::RemoveAllocListener() {
139*795d594fSAndroid Build Coastguard Worker callback_enabled_.store(false, std::memory_order_seq_cst);
140*795d594fSAndroid Build Coastguard Worker callback_ = nullptr;
141*795d594fSAndroid Build Coastguard Worker }
142*795d594fSAndroid Build Coastguard Worker
DecrListenerInstall(art::Thread * self)143*795d594fSAndroid Build Coastguard Worker void AllocationManager::DecrListenerInstall(art::Thread* self) {
144*795d594fSAndroid Build Coastguard Worker art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
145*795d594fSAndroid Build Coastguard Worker art::MutexLock mu(self, alloc_listener_mutex_);
146*795d594fSAndroid Build Coastguard Worker // We don't need any particular memory-order here since we're under the lock, they aren't
147*795d594fSAndroid Build Coastguard Worker // changing.
148*795d594fSAndroid Build Coastguard Worker if (--listener_refcount_ == 0) {
149*795d594fSAndroid Build Coastguard Worker art::Runtime::Current()->GetHeap()->RemoveAllocationListener();
150*795d594fSAndroid Build Coastguard Worker }
151*795d594fSAndroid Build Coastguard Worker }
152*795d594fSAndroid Build Coastguard Worker
IncrListenerInstall(art::Thread * self)153*795d594fSAndroid Build Coastguard Worker void AllocationManager::IncrListenerInstall(art::Thread* self) {
154*795d594fSAndroid Build Coastguard Worker art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
155*795d594fSAndroid Build Coastguard Worker art::MutexLock mu(self, alloc_listener_mutex_);
156*795d594fSAndroid Build Coastguard Worker // We don't need any particular memory-order here since we're under the lock, they aren't
157*795d594fSAndroid Build Coastguard Worker // changing.
158*795d594fSAndroid Build Coastguard Worker if (listener_refcount_++ == 0) {
159*795d594fSAndroid Build Coastguard Worker art::Runtime::Current()->GetHeap()->SetAllocationListener(alloc_listener_.get());
160*795d594fSAndroid Build Coastguard Worker }
161*795d594fSAndroid Build Coastguard Worker }
162*795d594fSAndroid Build Coastguard Worker
PauseAllocations(art::Thread * self)163*795d594fSAndroid Build Coastguard Worker void AllocationManager::PauseAllocations(art::Thread* self) {
164*795d594fSAndroid Build Coastguard Worker art::Thread* null_thr = nullptr;
165*795d594fSAndroid Build Coastguard Worker // Unfortunately once we've paused allocations once we have to leave the listener and
166*795d594fSAndroid Build Coastguard Worker // PreObjectAlloc event enabled forever. This is to avoid an instance of the ABA problem. We need
167*795d594fSAndroid Build Coastguard Worker // to make sure that every thread gets a chance to see the PreObjectAlloc event at least once or
168*795d594fSAndroid Build Coastguard Worker // else it could miss the fact that the object its allocating had its size changed.
169*795d594fSAndroid Build Coastguard Worker //
170*795d594fSAndroid Build Coastguard Worker // Consider the following 2 threads. T1 is allocating an object of class K. It is suspended (by
171*795d594fSAndroid Build Coastguard Worker // user code) somewhere in the AllocObjectWithAllocator function, perhaps while doing a GC to
172*795d594fSAndroid Build Coastguard Worker // attempt to clear space. With that thread suspended on thread T2 we decide to structurally
173*795d594fSAndroid Build Coastguard Worker // redefine 'K', changing its size. To do this we insert this PreObjectAlloc event to check and
174*795d594fSAndroid Build Coastguard Worker // update the size of the class being allocated. This is done successfully. Now imagine if T2
175*795d594fSAndroid Build Coastguard Worker // removed the listener event then T1 subsequently resumes. T1 would see there is no
176*795d594fSAndroid Build Coastguard Worker // PreObjectAlloc event and so allocate using the old object size. This leads to it not allocating
177*795d594fSAndroid Build Coastguard Worker // enough. To prevent this we simply force every allocation after our first pause to go through
178*795d594fSAndroid Build Coastguard Worker // the PreObjectAlloc event.
179*795d594fSAndroid Build Coastguard Worker //
180*795d594fSAndroid Build Coastguard Worker // TODO Technically we could do better than this. We just need to be able to require that all
181*795d594fSAndroid Build Coastguard Worker // threads within allocation functions go through the PreObjectAlloc at least once after we turn
182*795d594fSAndroid Build Coastguard Worker // it on. This is easier said than done though since we don't want to place a marker on threads
183*795d594fSAndroid Build Coastguard Worker // (allocation is just too common) and we can't just have every thread go through the event since
184*795d594fSAndroid Build Coastguard Worker // there are some threads that never or almost never allocate. We would also need to ensure that
185*795d594fSAndroid Build Coastguard Worker // this thread doesn't pause waiting for all threads to pass the barrier since the other threads
186*795d594fSAndroid Build Coastguard Worker // might be suspended. We could accomplish this by storing callbacks on each thread that would do
187*795d594fSAndroid Build Coastguard Worker // the work. Honestly though this is a debug feature and it doesn't slow things down very much so
188*795d594fSAndroid Build Coastguard Worker // simply leaving it on forever is simpler and safer.
189*795d594fSAndroid Build Coastguard Worker bool expected = false;
190*795d594fSAndroid Build Coastguard Worker if (allocations_paused_ever_.compare_exchange_strong(expected, true, std::memory_order_seq_cst)) {
191*795d594fSAndroid Build Coastguard Worker IncrListenerInstall(self);
192*795d594fSAndroid Build Coastguard Worker }
193*795d594fSAndroid Build Coastguard Worker do {
194*795d594fSAndroid Build Coastguard Worker PauseForAllocation(self, []() { return "request to pause allocations on other threads"; });
195*795d594fSAndroid Build Coastguard Worker } while (!allocations_paused_thread_.compare_exchange_strong(
196*795d594fSAndroid Build Coastguard Worker null_thr, self, std::memory_order_seq_cst));
197*795d594fSAndroid Build Coastguard Worker // Make sure everything else can see this and isn't in the middle of final allocation.
198*795d594fSAndroid Build Coastguard Worker // Force every thread to either be suspended or pass through a barrier.
199*795d594fSAndroid Build Coastguard Worker art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
200*795d594fSAndroid Build Coastguard Worker art::Barrier barrier(0);
201*795d594fSAndroid Build Coastguard Worker art::FunctionClosure fc(
202*795d594fSAndroid Build Coastguard Worker [&]([[maybe_unused]] art::Thread* thr) { barrier.Pass(art::Thread::Current()); });
203*795d594fSAndroid Build Coastguard Worker size_t requested = art::Runtime::Current()->GetThreadList()->RunCheckpoint(&fc);
204*795d594fSAndroid Build Coastguard Worker barrier.Increment(self, requested);
205*795d594fSAndroid Build Coastguard Worker }
206*795d594fSAndroid Build Coastguard Worker
ResumeAllocations(art::Thread * self)207*795d594fSAndroid Build Coastguard Worker void AllocationManager::ResumeAllocations(art::Thread* self) {
208*795d594fSAndroid Build Coastguard Worker CHECK_EQ(allocations_paused_thread_.load(), self) << "not paused! ";
209*795d594fSAndroid Build Coastguard Worker // See above for why we don't decr the install count.
210*795d594fSAndroid Build Coastguard Worker CHECK(allocations_paused_ever_.load(std::memory_order_seq_cst));
211*795d594fSAndroid Build Coastguard Worker art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
212*795d594fSAndroid Build Coastguard Worker art::MutexLock mu(self, alloc_listener_mutex_);
213*795d594fSAndroid Build Coastguard Worker allocations_paused_thread_.store(nullptr, std::memory_order_seq_cst);
214*795d594fSAndroid Build Coastguard Worker alloc_pause_cv_.Broadcast(self);
215*795d594fSAndroid Build Coastguard Worker }
216*795d594fSAndroid Build Coastguard Worker
217*795d594fSAndroid Build Coastguard Worker } // namespace openjdkjvmti
218