0N/A/*
2273N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "classfile/vmSymbols.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/handles.inline.hpp"
1879N/A#include "runtime/javaCalls.hpp"
1879N/A#include "services/lowMemoryDetector.hpp"
1879N/A#include "services/management.hpp"
1879N/A#include "services/memoryManager.hpp"
1879N/A#include "services/memoryPool.hpp"
0N/A
0N/AMemoryPool::MemoryPool(const char* name,
0N/A PoolType type,
0N/A size_t init_size,
0N/A size_t max_size,
0N/A bool support_usage_threshold,
0N/A bool support_gc_threshold) {
0N/A _name = name;
0N/A _initial_size = init_size;
0N/A _max_size = max_size;
0N/A _memory_pool_obj = NULL;
0N/A _available_for_allocation = true;
0N/A _num_managers = 0;
0N/A _type = type;
0N/A
0N/A // initialize the max and init size of collection usage
0N/A _after_gc_usage = MemoryUsage(_initial_size, 0, 0, _max_size);
0N/A
0N/A _usage_sensor = NULL;
0N/A _gc_usage_sensor = NULL;
0N/A // usage threshold supports both high and low threshold
0N/A _usage_threshold = new ThresholdSupport(support_usage_threshold, support_usage_threshold);
0N/A // gc usage threshold supports only high threshold
0N/A _gc_usage_threshold = new ThresholdSupport(support_gc_threshold, support_gc_threshold);
0N/A}
0N/A
0N/Avoid MemoryPool::add_manager(MemoryManager* mgr) {
0N/A assert(_num_managers < MemoryPool::max_num_managers, "_num_managers exceeds the max");
0N/A if (_num_managers < MemoryPool::max_num_managers) {
0N/A _managers[_num_managers] = mgr;
0N/A _num_managers++;
0N/A }
0N/A}
0N/A
0N/A
0N/A// Returns an instanceHandle of a MemoryPool object.
0N/A// It creates a MemoryPool instance when the first time
0N/A// this function is called.
0N/AinstanceOop MemoryPool::get_memory_pool_instance(TRAPS) {
0N/A // Must do an acquire so as to force ordering of subsequent
0N/A // loads from anything _memory_pool_obj points to or implies.
0N/A instanceOop pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj);
0N/A if (pool_obj == NULL) {
0N/A // It's ok for more than one thread to execute the code up to the locked region.
0N/A // Extra pool instances will just be gc'ed.
0N/A klassOop k = Management::sun_management_ManagementFactory_klass(CHECK_NULL);
0N/A instanceKlassHandle ik(THREAD, k);
0N/A
0N/A Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL);
0N/A jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
0N/A jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
0N/A
0N/A JavaValue result(T_OBJECT);
0N/A JavaCallArguments args;
0N/A args.push_oop(pool_name); // Argument 1
0N/A args.push_int((int) is_heap()); // Argument 2
0N/A
2062N/A Symbol* method_name = vmSymbols::createMemoryPool_name();
2062N/A Symbol* signature = vmSymbols::createMemoryPool_signature();
0N/A
0N/A args.push_long(usage_threshold_value); // Argument 3
0N/A args.push_long(gc_usage_threshold_value); // Argument 4
0N/A
0N/A JavaCalls::call_static(&result,
0N/A ik,
0N/A method_name,
0N/A signature,
0N/A &args,
0N/A CHECK_NULL);
0N/A
0N/A instanceOop p = (instanceOop) result.get_jobject();
0N/A instanceHandle pool(THREAD, p);
0N/A
0N/A {
0N/A // Get lock since another thread may have create the instance
0N/A MutexLocker ml(Management_lock);
0N/A
0N/A // Check if another thread has created the pool. We reload
0N/A // _memory_pool_obj here because some other thread may have
0N/A // initialized it while we were executing the code before the lock.
0N/A //
0N/A // The lock has done an acquire, so the load can't float above it,
0N/A // but we need to do a load_acquire as above.
0N/A pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj);
0N/A if (pool_obj != NULL) {
0N/A return pool_obj;
0N/A }
0N/A
0N/A // Get the address of the object we created via call_special.
0N/A pool_obj = pool();
0N/A
0N/A // Use store barrier to make sure the memory accesses associated
0N/A // with creating the pool are visible before publishing its address.
0N/A // The unlock will publish the store to _memory_pool_obj because
0N/A // it does a release first.
0N/A OrderAccess::release_store_ptr(&_memory_pool_obj, pool_obj);
0N/A }
0N/A }
0N/A
0N/A return pool_obj;
0N/A}
0N/A
0N/Ainline static size_t get_max_value(size_t val1, size_t val2) {
0N/A return (val1 > val2 ? val1 : val2);
0N/A}
0N/A
0N/Avoid MemoryPool::record_peak_memory_usage() {
0N/A // Caller in JDK is responsible for synchronization -
0N/A // acquire the lock for this memory pool before calling VM
0N/A MemoryUsage usage = get_memory_usage();
0N/A size_t peak_used = get_max_value(usage.used(), _peak_usage.used());
0N/A size_t peak_committed = get_max_value(usage.committed(), _peak_usage.committed());
0N/A size_t peak_max_size = get_max_value(usage.max_size(), _peak_usage.max_size());
0N/A
0N/A _peak_usage = MemoryUsage(initial_size(), peak_used, peak_committed, peak_max_size);
0N/A}
0N/A
0N/Astatic void set_sensor_obj_at(SensorInfo** sensor_ptr, instanceHandle sh) {
0N/A assert(*sensor_ptr == NULL, "Should be called only once");
0N/A SensorInfo* sensor = new SensorInfo();
0N/A sensor->set_sensor(sh());
0N/A *sensor_ptr = sensor;
0N/A}
0N/A
0N/Avoid MemoryPool::set_usage_sensor_obj(instanceHandle sh) {
0N/A set_sensor_obj_at(&_usage_sensor, sh);
0N/A}
0N/A
0N/Avoid MemoryPool::set_gc_usage_sensor_obj(instanceHandle sh) {
0N/A set_sensor_obj_at(&_gc_usage_sensor, sh);
0N/A}
0N/A
0N/Avoid MemoryPool::oops_do(OopClosure* f) {
0N/A f->do_oop((oop*) &_memory_pool_obj);
0N/A if (_usage_sensor != NULL) {
0N/A _usage_sensor->oops_do(f);
0N/A }
0N/A if (_gc_usage_sensor != NULL) {
0N/A _gc_usage_sensor->oops_do(f);
0N/A }
0N/A}
0N/A
0N/AContiguousSpacePool::ContiguousSpacePool(ContiguousSpace* space,
0N/A const char* name,
0N/A PoolType type,
0N/A size_t max_size,
0N/A bool support_usage_threshold) :
0N/A CollectedMemoryPool(name, type, space->capacity(), max_size,
0N/A support_usage_threshold), _space(space) {
0N/A}
0N/A
0N/AMemoryUsage ContiguousSpacePool::get_memory_usage() {
0N/A size_t maxSize = (available_for_allocation() ? max_size() : 0);
0N/A size_t used = used_in_bytes();
0N/A size_t committed = _space->capacity();
0N/A
0N/A return MemoryUsage(initial_size(), used, committed, maxSize);
0N/A}
0N/A
0N/ASurvivorContiguousSpacePool::SurvivorContiguousSpacePool(DefNewGeneration* gen,
0N/A const char* name,
0N/A PoolType type,
0N/A size_t max_size,
0N/A bool support_usage_threshold) :
0N/A CollectedMemoryPool(name, type, gen->from()->capacity(), max_size,
0N/A support_usage_threshold), _gen(gen) {
0N/A}
0N/A
0N/AMemoryUsage SurvivorContiguousSpacePool::get_memory_usage() {
0N/A size_t maxSize = (available_for_allocation() ? max_size() : 0);
0N/A size_t used = used_in_bytes();
0N/A size_t committed = committed_in_bytes();
0N/A
0N/A return MemoryUsage(initial_size(), used, committed, maxSize);
0N/A}
0N/A
0N/A#ifndef SERIALGC
0N/ACompactibleFreeListSpacePool::CompactibleFreeListSpacePool(CompactibleFreeListSpace* space,
0N/A const char* name,
0N/A PoolType type,
0N/A size_t max_size,
0N/A bool support_usage_threshold) :
0N/A CollectedMemoryPool(name, type, space->capacity(), max_size,
0N/A support_usage_threshold), _space(space) {
0N/A}
0N/A
0N/AMemoryUsage CompactibleFreeListSpacePool::get_memory_usage() {
0N/A size_t maxSize = (available_for_allocation() ? max_size() : 0);
0N/A size_t used = used_in_bytes();
0N/A size_t committed = _space->capacity();
0N/A
0N/A return MemoryUsage(initial_size(), used, committed, maxSize);
0N/A}
0N/A#endif // SERIALGC
0N/A
0N/AGenerationPool::GenerationPool(Generation* gen,
0N/A const char* name,
0N/A PoolType type,
0N/A bool support_usage_threshold) :
0N/A CollectedMemoryPool(name, type, gen->capacity(), gen->max_capacity(),
0N/A support_usage_threshold), _gen(gen) {
0N/A}
0N/A
0N/AMemoryUsage GenerationPool::get_memory_usage() {
0N/A size_t used = used_in_bytes();
0N/A size_t committed = _gen->capacity();
0N/A size_t maxSize = (available_for_allocation() ? max_size() : 0);
0N/A
0N/A return MemoryUsage(initial_size(), used, committed, maxSize);
0N/A}
0N/A
0N/ACodeHeapPool::CodeHeapPool(CodeHeap* codeHeap, const char* name, bool support_usage_threshold) :
0N/A MemoryPool(name, NonHeap, codeHeap->capacity(), codeHeap->max_capacity(),
0N/A support_usage_threshold, false), _codeHeap(codeHeap) {
0N/A}
0N/A
0N/AMemoryUsage CodeHeapPool::get_memory_usage() {
0N/A size_t used = used_in_bytes();
0N/A size_t committed = _codeHeap->capacity();
0N/A size_t maxSize = (available_for_allocation() ? max_size() : 0);
0N/A
0N/A return MemoryUsage(initial_size(), used, committed, maxSize);
0N/A}