0N/A/*
1759N/A * Copyright (c) 2000, 2010, 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 "gc_implementation/shared/cSpaceCounters.hpp"
1879N/A#include "gc_implementation/shared/vmGCOperations.hpp"
1879N/A#include "gc_interface/collectedHeap.inline.hpp"
1879N/A#include "memory/blockOffsetTable.inline.hpp"
1879N/A#include "memory/compactPermGen.hpp"
1879N/A#include "memory/gcLocker.hpp"
1879N/A#include "memory/gcLocker.inline.hpp"
1879N/A#include "memory/genCollectedHeap.hpp"
1879N/A#include "memory/generation.inline.hpp"
1879N/A#include "memory/permGen.hpp"
1879N/A#include "memory/universe.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#include "runtime/vmThread.hpp"
0N/A
1759N/AHeapWord* PermGen::request_expand_and_allocate(Generation* gen, size_t size,
1759N/A GCCause::Cause prev_cause) {
1759N/A if (gen->capacity() < _capacity_expansion_limit ||
1759N/A prev_cause != GCCause::_no_gc || UseG1GC) { // last disjunct is a temporary hack for G1
1759N/A return gen->expand_and_allocate(size, false);
1759N/A }
1759N/A // We have reached the limit of capacity expansion where
1759N/A // we will not expand further until a GC is done; request denied.
1759N/A return NULL;
1759N/A}
1759N/A
139N/AHeapWord* PermGen::mem_allocate_in_gen(size_t size, Generation* gen) {
139N/A GCCause::Cause next_cause = GCCause::_permanent_generation_full;
139N/A GCCause::Cause prev_cause = GCCause::_no_gc;
480N/A unsigned int gc_count_before, full_gc_count_before;
480N/A HeapWord* obj;
139N/A
139N/A for (;;) {
480N/A {
480N/A MutexLocker ml(Heap_lock);
480N/A if ((obj = gen->allocate(size, false)) != NULL) {
480N/A return obj;
480N/A }
1759N/A // Attempt to expand and allocate the requested space:
1759N/A // specific subtypes may use specific policy to either expand
1759N/A // or not. The default policy (see above) is to expand until
1759N/A // _capacity_expansion_limit, and no further unless a GC is done.
1759N/A // Concurrent collectors may decide to kick off a concurrent
1759N/A // collection under appropriate conditions.
1759N/A obj = request_expand_and_allocate(gen, size, prev_cause);
1759N/A
480N/A if (obj != NULL || prev_cause == GCCause::_last_ditch_collection) {
480N/A return obj;
480N/A }
139N/A if (GC_locker::is_active_and_needs_gc()) {
139N/A // If this thread is not in a jni critical section, we stall
139N/A // the requestor until the critical section has cleared and
139N/A // GC allowed. When the critical section clears, a GC is
139N/A // initiated by the last thread exiting the critical section; so
139N/A // we retry the allocation sequence from the beginning of the loop,
139N/A // rather than causing more, now probably unnecessary, GC attempts.
139N/A JavaThread* jthr = JavaThread::current();
139N/A if (!jthr->in_critical()) {
139N/A MutexUnlocker mul(Heap_lock);
139N/A // Wait for JNI critical section to be exited
139N/A GC_locker::stall_until_clear();
139N/A continue;
139N/A } else {
139N/A if (CheckJNICalls) {
139N/A fatal("Possible deadlock due to allocating while"
139N/A " in jni critical section");
139N/A }
139N/A return NULL;
139N/A }
139N/A }
480N/A // Read the GC count while holding the Heap_lock
480N/A gc_count_before = SharedHeap::heap()->total_collections();
480N/A full_gc_count_before = SharedHeap::heap()->total_full_collections();
480N/A }
139N/A
480N/A // Give up heap lock above, VMThread::execute below gets it back
480N/A VM_GenCollectForPermanentAllocation op(size, gc_count_before, full_gc_count_before,
480N/A next_cause);
480N/A VMThread::execute(&op);
480N/A if (!op.prologue_succeeded() || op.gc_locked()) {
480N/A assert(op.result() == NULL, "must be NULL if gc_locked() is true");
480N/A continue; // retry and/or stall as necessary
480N/A }
480N/A obj = op.result();
480N/A assert(obj == NULL || SharedHeap::heap()->is_in_reserved(obj),
480N/A "result not in heap");
480N/A if (obj != NULL) {
139N/A return obj;
139N/A }
480N/A prev_cause = next_cause;
480N/A next_cause = GCCause::_last_ditch_collection;
139N/A }
139N/A}
139N/A
0N/ACompactingPermGen::CompactingPermGen(ReservedSpace rs,
0N/A ReservedSpace shared_rs,
0N/A size_t initial_byte_size,
0N/A GenRemSet* remset,
0N/A PermanentGenerationSpec* perm_spec)
0N/A{
0N/A CompactingPermGenGen* g =
0N/A new CompactingPermGenGen(rs, shared_rs, initial_byte_size, -1, remset,
0N/A NULL, perm_spec);
0N/A if (g == NULL)
0N/A vm_exit_during_initialization("Could not allocate a CompactingPermGen");
0N/A _gen = g;
0N/A
0N/A g->initialize_performance_counters();
0N/A
0N/A _capacity_expansion_limit = g->capacity() + MaxPermHeapExpansion;
0N/A}
0N/A
0N/AHeapWord* CompactingPermGen::mem_allocate(size_t size) {
139N/A return mem_allocate_in_gen(size, _gen);
0N/A}
0N/A
0N/Avoid CompactingPermGen::compute_new_size() {
0N/A size_t desired_capacity = align_size_up(_gen->used(), MinPermHeapExpansion);
0N/A if (desired_capacity < PermSize) {
0N/A desired_capacity = PermSize;
0N/A }
0N/A if (_gen->capacity() > desired_capacity) {
0N/A _gen->shrink(_gen->capacity() - desired_capacity);
0N/A }
1759N/A set_capacity_expansion_limit(_gen->capacity() + MaxPermHeapExpansion);
0N/A}