xref: /aosp_15_r20/art/openjdkjvmti/alloc_manager.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2019 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * This file implements interfaces from the file jvmti.h. This implementation
5*795d594fSAndroid Build Coastguard Worker  * is licensed under the same terms as the file jvmti.h.  The
6*795d594fSAndroid Build Coastguard Worker  * copyright and license information for the file jvmti.h follows.
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10*795d594fSAndroid Build Coastguard Worker  *
11*795d594fSAndroid Build Coastguard Worker  * This code is free software; you can redistribute it and/or modify it
12*795d594fSAndroid Build Coastguard Worker  * under the terms of the GNU General Public License version 2 only, as
13*795d594fSAndroid Build Coastguard Worker  * published by the Free Software Foundation.  Oracle designates this
14*795d594fSAndroid Build Coastguard Worker  * particular file as subject to the "Classpath" exception as provided
15*795d594fSAndroid Build Coastguard Worker  * by Oracle in the LICENSE file that accompanied this code.
16*795d594fSAndroid Build Coastguard Worker  *
17*795d594fSAndroid Build Coastguard Worker  * This code is distributed in the hope that it will be useful, but WITHOUT
18*795d594fSAndroid Build Coastguard Worker  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*795d594fSAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20*795d594fSAndroid Build Coastguard Worker  * version 2 for more details (a copy is included in the LICENSE file that
21*795d594fSAndroid Build Coastguard Worker  * accompanied this code).
22*795d594fSAndroid Build Coastguard Worker  *
23*795d594fSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License version
24*795d594fSAndroid Build Coastguard Worker  * 2 along with this work; if not, write to the Free Software Foundation,
25*795d594fSAndroid Build Coastguard Worker  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26*795d594fSAndroid Build Coastguard Worker  *
27*795d594fSAndroid Build Coastguard Worker  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28*795d594fSAndroid Build Coastguard Worker  * or visit www.oracle.com if you need additional information or have any
29*795d594fSAndroid Build Coastguard Worker  * questions.
30*795d594fSAndroid Build Coastguard Worker  */
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #ifndef ART_OPENJDKJVMTI_ALLOC_MANAGER_H_
33*795d594fSAndroid Build Coastguard Worker #define ART_OPENJDKJVMTI_ALLOC_MANAGER_H_
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker #include <jvmti.h>
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker #include <atomic>
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
40*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
41*795d594fSAndroid Build Coastguard Worker #include "gc/allocation_listener.h"
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker namespace art {
44*795d594fSAndroid Build Coastguard Worker template <typename T> class MutableHandle;
45*795d594fSAndroid Build Coastguard Worker template <typename T> class ObjPtr;
46*795d594fSAndroid Build Coastguard Worker class Thread;
47*795d594fSAndroid Build Coastguard Worker namespace mirror {
48*795d594fSAndroid Build Coastguard Worker class Class;
49*795d594fSAndroid Build Coastguard Worker class Object;
50*795d594fSAndroid Build Coastguard Worker }  // namespace mirror
51*795d594fSAndroid Build Coastguard Worker }  // namespace art
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker class AllocationManager;
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker class JvmtiAllocationListener : public art::gc::AllocationListener {
58*795d594fSAndroid Build Coastguard Worker  public:
JvmtiAllocationListener(AllocationManager * manager)59*795d594fSAndroid Build Coastguard Worker   explicit JvmtiAllocationListener(AllocationManager* manager) : manager_(manager) {}
60*795d594fSAndroid Build Coastguard Worker   void ObjectAllocated(art::Thread* self,
61*795d594fSAndroid Build Coastguard Worker                        art::ObjPtr<art::mirror::Object>* obj,
62*795d594fSAndroid Build Coastguard Worker                        size_t cnt) override REQUIRES_SHARED(art::Locks::mutator_lock_);
63*795d594fSAndroid Build Coastguard Worker   bool HasPreAlloc() const override REQUIRES_SHARED(art::Locks::mutator_lock_);
64*795d594fSAndroid Build Coastguard Worker   void PreObjectAllocated(art::Thread* self,
65*795d594fSAndroid Build Coastguard Worker                           art::MutableHandle<art::mirror::Class> type,
66*795d594fSAndroid Build Coastguard Worker                           size_t* byte_count) override REQUIRES_SHARED(art::Locks::mutator_lock_);
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker  private:
69*795d594fSAndroid Build Coastguard Worker   AllocationManager* manager_;
70*795d594fSAndroid Build Coastguard Worker };
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker class AllocationManager {
73*795d594fSAndroid Build Coastguard Worker  public:
74*795d594fSAndroid Build Coastguard Worker   class AllocationCallback {
75*795d594fSAndroid Build Coastguard Worker    public:
~AllocationCallback()76*795d594fSAndroid Build Coastguard Worker     virtual ~AllocationCallback() {}
77*795d594fSAndroid Build Coastguard Worker     virtual void ObjectAllocated(art::Thread* self,
78*795d594fSAndroid Build Coastguard Worker                                  art::ObjPtr<art::mirror::Object>* obj,
79*795d594fSAndroid Build Coastguard Worker                                  size_t byte_count) REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
80*795d594fSAndroid Build Coastguard Worker   };
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker   AllocationManager();
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker   void SetAllocListener(AllocationCallback* callback);
85*795d594fSAndroid Build Coastguard Worker   void RemoveAllocListener();
86*795d594fSAndroid Build Coastguard Worker 
87*795d594fSAndroid Build Coastguard Worker   static AllocationManager* Get();
88*795d594fSAndroid Build Coastguard Worker 
89*795d594fSAndroid Build Coastguard Worker   void PauseAllocations(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_);
90*795d594fSAndroid Build Coastguard Worker   void ResumeAllocations(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_);
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker   void EnableAllocationCallback(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_);
93*795d594fSAndroid Build Coastguard Worker   void DisableAllocationCallback(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_);
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker  private:
96*795d594fSAndroid Build Coastguard Worker   template<typename T>
97*795d594fSAndroid Build Coastguard Worker   void PauseForAllocation(art::Thread* self, T msg) REQUIRES_SHARED(art::Locks::mutator_lock_);
98*795d594fSAndroid Build Coastguard Worker   void IncrListenerInstall(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_);
99*795d594fSAndroid Build Coastguard Worker   void DecrListenerInstall(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_);
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker   AllocationCallback* callback_ = nullptr;
102*795d594fSAndroid Build Coastguard Worker   uint32_t listener_refcount_ GUARDED_BY(alloc_listener_mutex_) = 0;
103*795d594fSAndroid Build Coastguard Worker   std::atomic<bool> allocations_paused_ever_ = false;
104*795d594fSAndroid Build Coastguard Worker   std::atomic<art::Thread*> allocations_paused_thread_ = nullptr;
105*795d594fSAndroid Build Coastguard Worker   std::atomic<bool> callback_enabled_ = false;
106*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JvmtiAllocationListener> alloc_listener_ = nullptr;
107*795d594fSAndroid Build Coastguard Worker   art::Mutex alloc_listener_mutex_ ACQUIRED_AFTER(art::Locks::user_code_suspension_lock_);
108*795d594fSAndroid Build Coastguard Worker   art::ConditionVariable alloc_pause_cv_;
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker   friend class JvmtiAllocationListener;
111*795d594fSAndroid Build Coastguard Worker };
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker }  // namespace openjdkjvmti
114*795d594fSAndroid Build Coastguard Worker 
115*795d594fSAndroid Build Coastguard Worker #endif  // ART_OPENJDKJVMTI_ALLOC_MANAGER_H_
116