1 /*
2 * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "memory/metaspace.hpp"
30 #include "memory/universe.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "oops/oopHandle.inline.hpp"
33 #include "runtime/atomic.hpp"
34 #include "runtime/globals_extension.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/javaCalls.hpp"
37 #include "services/lowMemoryDetector.hpp"
38 #include "services/management.hpp"
39 #include "services/memoryManager.hpp"
40 #include "services/memoryPool.hpp"
41 #include "utilities/globalDefinitions.hpp"
42 #include "utilities/macros.hpp"
43
MemoryPool(const char * name,PoolType type,size_t init_size,size_t max_size,bool support_usage_threshold,bool support_gc_threshold)44 MemoryPool::MemoryPool(const char* name,
45 PoolType type,
46 size_t init_size,
47 size_t max_size,
48 bool support_usage_threshold,
49 bool support_gc_threshold) :
50 _name(name),
51 _type(type),
52 _initial_size(init_size),
53 _max_size(max_size),
54 _available_for_allocation(true),
55 _managers(),
56 _num_managers(0),
57 _peak_usage(),
58 _after_gc_usage(init_size, 0, 0, max_size),
59 // usage threshold supports both high and low threshold
60 _usage_threshold(new ThresholdSupport(support_usage_threshold, support_usage_threshold)),
61 // gc usage threshold supports only high threshold
62 _gc_usage_threshold(new ThresholdSupport(support_gc_threshold, support_gc_threshold)),
63 _usage_sensor(),
64 _gc_usage_sensor(),
65 _memory_pool_obj()
66 {}
67
is_pool(instanceHandle pool) const68 bool MemoryPool::is_pool(instanceHandle pool) const {
69 return pool() == Atomic::load(&_memory_pool_obj).resolve();
70 }
71
add_manager(MemoryManager * mgr)72 void MemoryPool::add_manager(MemoryManager* mgr) {
73 assert(_num_managers < MemoryPool::max_num_managers, "_num_managers exceeds the max");
74 if (_num_managers < MemoryPool::max_num_managers) {
75 _managers[_num_managers] = mgr;
76 _num_managers++;
77 }
78 }
79
80
81 // Returns an instanceOop of a MemoryPool object.
82 // It creates a MemoryPool instance when the first time
83 // this function is called.
get_memory_pool_instance(TRAPS)84 instanceOop MemoryPool::get_memory_pool_instance(TRAPS) {
85 // Must do an acquire so as to force ordering of subsequent
86 // loads from anything _memory_pool_obj points to or implies.
87 oop pool_obj = Atomic::load_acquire(&_memory_pool_obj).resolve();
88 if (pool_obj == NULL) {
89 // It's ok for more than one thread to execute the code up to the locked region.
90 // Extra pool instances will just be gc'ed.
91 InstanceKlass* ik = Management::sun_management_ManagementFactoryHelper_klass(CHECK_NULL);
92
93 Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL);
94 jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
95 jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
96
97 JavaValue result(T_OBJECT);
98 JavaCallArguments args;
99 args.push_oop(pool_name); // Argument 1
100 args.push_int((int) is_heap()); // Argument 2
101
102 Symbol* method_name = vmSymbols::createMemoryPool_name();
103 Symbol* signature = vmSymbols::createMemoryPool_signature();
104
105 args.push_long(usage_threshold_value); // Argument 3
106 args.push_long(gc_usage_threshold_value); // Argument 4
107
108 JavaCalls::call_static(&result,
109 ik,
110 method_name,
111 signature,
112 &args,
113 CHECK_NULL);
114
115 instanceOop p = (instanceOop) result.get_jobject();
116 instanceHandle pool(THREAD, p);
117
118 {
119 // Get lock since another thread may have create the instance
120 MutexLocker ml(THREAD, Management_lock);
121
122 // Check if another thread has created the pool. We reload
123 // _memory_pool_obj here because some other thread may have
124 // initialized it while we were executing the code before the lock.
125 pool_obj = Atomic::load(&_memory_pool_obj).resolve();
126 if (pool_obj != NULL) {
127 return (instanceOop)pool_obj;
128 }
129
130 // Get the address of the object we created via call_special.
131 pool_obj = pool();
132
133 // Use store barrier to make sure the memory accesses associated
134 // with creating the pool are visible before publishing its address.
135 // The unlock will publish the store to _memory_pool_obj because
136 // it does a release first.
137 Atomic::release_store(&_memory_pool_obj, OopHandle(Universe::vm_global(), pool_obj));
138 }
139 }
140
141 return (instanceOop)pool_obj;
142 }
143
get_max_value(size_t val1,size_t val2)144 inline static size_t get_max_value(size_t val1, size_t val2) {
145 return (val1 > val2 ? val1 : val2);
146 }
147
record_peak_memory_usage()148 void MemoryPool::record_peak_memory_usage() {
149 // Caller in JDK is responsible for synchronization -
150 // acquire the lock for this memory pool before calling VM
151 MemoryUsage usage = get_memory_usage();
152 size_t peak_used = get_max_value(usage.used(), _peak_usage.used());
153 size_t peak_committed = get_max_value(usage.committed(), _peak_usage.committed());
154 size_t peak_max_size = get_max_value(usage.max_size(), _peak_usage.max_size());
155
156 _peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size);
157 }
158
set_sensor_obj_at(SensorInfo ** sensor_ptr,instanceHandle sh)159 static void set_sensor_obj_at(SensorInfo** sensor_ptr, instanceHandle sh) {
160 assert(*sensor_ptr == NULL, "Should be called only once");
161 SensorInfo* sensor = new SensorInfo();
162 sensor->set_sensor(sh());
163 *sensor_ptr = sensor;
164 }
165
set_usage_sensor_obj(instanceHandle sh)166 void MemoryPool::set_usage_sensor_obj(instanceHandle sh) {
167 set_sensor_obj_at(&_usage_sensor, sh);
168 }
169
set_gc_usage_sensor_obj(instanceHandle sh)170 void MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) {
171 set_sensor_obj_at(&_gc_usage_sensor, sh);
172 }
173
CodeHeapPool(CodeHeap * codeHeap,const char * name,bool support_usage_threshold)174 CodeHeapPool::CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold) :
175 MemoryPool(name, NonHeap, codeHeap->capacity(), codeHeap->max_capacity(),
176 support_usage_threshold, false), _codeHeap(codeHeap) {
177 }
178
get_memory_usage()179 MemoryUsage CodeHeapPool::get_memory_usage() {
180 size_t used = used_in_bytes();
181 size_t committed = _codeHeap->capacity();
182 size_t maxSize = (available_for_allocation() ? max_size() : 0);
183
184 return MemoryUsage(initial_size(), used, committed, maxSize);
185 }
186
MetaspacePool()187 MetaspacePool::MetaspacePool() :
188 MemoryPool("Metaspace", NonHeap, 0, calculate_max_size(), true, false) { }
189
get_memory_usage()190 MemoryUsage MetaspacePool::get_memory_usage() {
191 size_t committed = MetaspaceUtils::committed_bytes();
192 return MemoryUsage(initial_size(), used_in_bytes(), committed, max_size());
193 }
194
used_in_bytes()195 size_t MetaspacePool::used_in_bytes() {
196 return MetaspaceUtils::used_bytes();
197 }
198
calculate_max_size() const199 size_t MetaspacePool::calculate_max_size() const {
200 return !FLAG_IS_DEFAULT(MaxMetaspaceSize) ? MaxMetaspaceSize :
201 MemoryUsage::undefined_size();
202 }
203
CompressedKlassSpacePool()204 CompressedKlassSpacePool::CompressedKlassSpacePool() :
205 MemoryPool("Compressed Class Space", NonHeap, 0, CompressedClassSpaceSize, true, false) { }
206
used_in_bytes()207 size_t CompressedKlassSpacePool::used_in_bytes() {
208 return MetaspaceUtils::used_bytes(Metaspace::ClassType);
209 }
210
get_memory_usage()211 MemoryUsage CompressedKlassSpacePool::get_memory_usage() {
212 size_t committed = MetaspaceUtils::committed_bytes(Metaspace::ClassType);
213 return MemoryUsage(initial_size(), used_in_bytes(), committed, max_size());
214 }
215