2
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "classfile/javaClasses.hpp"
27
#include "classfile/vmSymbols.hpp"
28
#include "memory/metaspace.hpp"
29
#include "memory/metaspaceUtils.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 "runtime/mutexLocker.hpp"
38
#include "services/lowMemoryDetector.hpp"
39
#include "services/management.hpp"
40
#include "services/memoryManager.hpp"
41
#include "services/memoryPool.hpp"
42
#include "utilities/globalDefinitions.hpp"
43
#include "utilities/macros.hpp"
45
MemoryPool::MemoryPool(const char* name,
49
bool support_usage_threshold,
50
bool support_gc_threshold) :
53
_initial_size(init_size),
55
_available_for_allocation(true),
59
_after_gc_usage(init_size, 0, 0, max_size),
60
// usage threshold supports both high and low threshold
61
_usage_threshold(new ThresholdSupport(support_usage_threshold, support_usage_threshold)),
62
// gc usage threshold supports only high threshold
63
_gc_usage_threshold(new ThresholdSupport(support_gc_threshold, support_gc_threshold)),
67
_memory_pool_obj_initialized(false)
70
bool MemoryPool::is_pool(instanceHandle pool) const {
71
if (Atomic::load_acquire(&_memory_pool_obj_initialized)) {
72
return pool() == _memory_pool_obj.resolve();
78
void MemoryPool::add_manager(MemoryManager* mgr) {
79
assert(_num_managers < MemoryPool::max_num_managers, "_num_managers exceeds the max");
80
if (_num_managers < MemoryPool::max_num_managers) {
81
_managers[_num_managers] = mgr;
87
// Returns an instanceOop of a MemoryPool object.
88
// It creates a MemoryPool instance when the first time
89
// this function is called.
90
instanceOop MemoryPool::get_memory_pool_instance(TRAPS) {
91
// Lazily create the pool object.
92
// Must do an acquire so as to force ordering of subsequent
93
// loads from anything _memory_pool_obj points to or implies.
94
if (!Atomic::load_acquire(&_memory_pool_obj_initialized)) {
95
// It's ok for more than one thread to execute the code up to the locked region.
96
// Extra pool instances will just be gc'ed.
97
InstanceKlass* ik = Management::sun_management_ManagementFactoryHelper_klass(CHECK_NULL);
99
Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL);
100
jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
101
jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
103
JavaValue result(T_OBJECT);
104
JavaCallArguments args;
105
args.push_oop(pool_name); // Argument 1
106
args.push_int((int) is_heap()); // Argument 2
108
Symbol* method_name = vmSymbols::createMemoryPool_name();
109
Symbol* signature = vmSymbols::createMemoryPool_signature();
111
args.push_long(usage_threshold_value); // Argument 3
112
args.push_long(gc_usage_threshold_value); // Argument 4
114
JavaCalls::call_static(&result,
121
// Verify we didn't get a null pool. If that could happen then we'd
122
// need to return immediately rather than continuing on and recording the
123
// pool has been created.
124
oop p = result.get_oop();
125
guarantee(p != nullptr, "Pool creation returns null");
126
instanceHandle pool(THREAD, (instanceOop)p);
128
// Allocate global handle outside lock, to avoid any lock nesting issues
129
// with the Management_lock.
130
OopHandle pool_handle(Universe::vm_global(), pool());
132
// Get lock since another thread may have created and installed the instance.
133
MutexLocker ml(THREAD, Management_lock);
135
if (Atomic::load(&_memory_pool_obj_initialized)) {
136
// Some other thread won the race. Release the handle we allocated and
137
// use the other one. Relaxed load is sufficient because flag update is
139
pool_handle.release(Universe::vm_global());
141
// Record the object we created via call_special.
142
assert(_memory_pool_obj.is_empty(), "already set pool obj");
143
_memory_pool_obj = pool_handle;
144
// Record pool has been created. Release matching unlocked acquire, to
145
// safely publish the pool object.
146
Atomic::release_store(&_memory_pool_obj_initialized, true);
150
return (instanceOop)_memory_pool_obj.resolve();
153
inline static size_t get_max_value(size_t val1, size_t val2) {
154
return (val1 > val2 ? val1 : val2);
157
void MemoryPool::record_peak_memory_usage() {
158
// Caller in JDK is responsible for synchronization -
159
// acquire the lock for this memory pool before calling VM
160
MemoryUsage usage = get_memory_usage();
161
size_t peak_used = get_max_value(usage.used(), _peak_usage.used());
162
size_t peak_committed = get_max_value(usage.committed(), _peak_usage.committed());
163
size_t peak_max_size = get_max_value(usage.max_size(), _peak_usage.max_size());
165
_peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size);
168
static void set_sensor_obj_at(SensorInfo** sensor_ptr, instanceHandle sh) {
169
assert(*sensor_ptr == nullptr, "Should be called only once");
170
SensorInfo* sensor = new SensorInfo();
171
sensor->set_sensor(sh());
172
*sensor_ptr = sensor;
175
void MemoryPool::set_usage_sensor_obj(instanceHandle sh) {
176
set_sensor_obj_at(&_usage_sensor, sh);
179
void MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) {
180
set_sensor_obj_at(&_gc_usage_sensor, sh);
183
CodeHeapPool::CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold) :
184
MemoryPool(name, NonHeap, codeHeap->capacity(), codeHeap->max_capacity(),
185
support_usage_threshold, false), _codeHeap(codeHeap) {
188
MemoryUsage CodeHeapPool::get_memory_usage() {
189
size_t used = used_in_bytes();
190
OrderAccess::acquire(); // ensure possible cache expansion in CodeCache::allocate is seen
191
size_t committed = _codeHeap->capacity();
192
size_t maxSize = (available_for_allocation() ? max_size() : 0);
194
return MemoryUsage(initial_size(), used, committed, maxSize);
197
MetaspacePool::MetaspacePool() :
198
MemoryPool("Metaspace", NonHeap, 0, calculate_max_size(), true, false) { }
200
MemoryUsage MetaspacePool::get_memory_usage() {
201
MetaspaceCombinedStats stats = MetaspaceUtils::get_combined_statistics();
202
return MemoryUsage(initial_size(), stats.used(), stats.committed(), max_size());
205
size_t MetaspacePool::used_in_bytes() {
206
return MetaspaceUtils::used_bytes();
209
size_t MetaspacePool::calculate_max_size() const {
210
return !FLAG_IS_DEFAULT(MaxMetaspaceSize) ? MaxMetaspaceSize :
211
MemoryUsage::undefined_size();
214
CompressedKlassSpacePool::CompressedKlassSpacePool() :
215
MemoryPool("Compressed Class Space", NonHeap, 0, CompressedClassSpaceSize, true, false) { }
217
size_t CompressedKlassSpacePool::used_in_bytes() {
218
return MetaspaceUtils::used_bytes(Metaspace::ClassType);
221
MemoryUsage CompressedKlassSpacePool::get_memory_usage() {
222
MetaspaceStats stats = MetaspaceUtils::get_statistics(Metaspace::ClassType);
223
return MemoryUsage(initial_size(), stats.used(), stats.committed(), max_size());