0N/A/*
4592N/A * Copyright (c) 2000, 2013, 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/symbolTable.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "code/codeCache.hpp"
1879N/A#include "gc_interface/collectedHeap.inline.hpp"
1879N/A#include "memory/sharedHeap.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/fprofiler.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#include "services/management.hpp"
1879N/A#include "utilities/copy.hpp"
1879N/A#include "utilities/workgroup.hpp"
0N/A
0N/ASharedHeap* SharedHeap::_sh;
0N/A
0N/A// The set of potentially parallel tasks in strong root scanning.
0N/Aenum SH_process_strong_roots_tasks {
0N/A SH_PS_Universe_oops_do,
0N/A SH_PS_JNIHandles_oops_do,
0N/A SH_PS_ObjectSynchronizer_oops_do,
0N/A SH_PS_FlatProfiler_oops_do,
0N/A SH_PS_Management_oops_do,
0N/A SH_PS_SystemDictionary_oops_do,
0N/A SH_PS_jvmti_oops_do,
0N/A SH_PS_CodeCache_oops_do,
0N/A // Leave this one last.
0N/A SH_PS_NumElements
0N/A};
0N/A
0N/ASharedHeap::SharedHeap(CollectorPolicy* policy_) :
0N/A CollectedHeap(),
0N/A _collector_policy(policy_),
0N/A _perm_gen(NULL), _rem_set(NULL),
0N/A _strong_roots_parity(0),
0N/A _process_strong_tasks(new SubTasksDone(SH_PS_NumElements)),
1753N/A _workers(NULL)
0N/A{
0N/A if (_process_strong_tasks == NULL || !_process_strong_tasks->valid()) {
0N/A vm_exit_during_initialization("Failed necessary allocation.");
0N/A }
0N/A _sh = this; // ch is static, should be set only once.
0N/A if ((UseParNewGC ||
342N/A (UseConcMarkSweepGC && CMSParallelRemarkEnabled) ||
342N/A UseG1GC) &&
0N/A ParallelGCThreads > 0) {
1753N/A _workers = new FlexibleWorkGang("Parallel GC Threads", ParallelGCThreads,
342N/A /* are_GC_task_threads */true,
342N/A /* are_ConcurrentGC_threads */false);
0N/A if (_workers == NULL) {
0N/A vm_exit_during_initialization("Failed necessary allocation.");
1753N/A } else {
1753N/A _workers->initialize_workers();
0N/A }
0N/A }
0N/A}
0N/A
2941N/Aint SharedHeap::n_termination() {
2941N/A return _process_strong_tasks->n_threads();
2941N/A}
2941N/A
2941N/Avoid SharedHeap::set_n_termination(int t) {
2941N/A _process_strong_tasks->set_n_threads(t);
2941N/A}
2941N/A
342N/Abool SharedHeap::heap_lock_held_for_gc() {
342N/A Thread* t = Thread::current();
342N/A return Heap_lock->owned_by_self()
342N/A || ( (t->is_GC_task_thread() || t->is_VM_thread())
342N/A && _thread_holds_heap_lock_for_gc);
342N/A}
0N/A
3008N/Avoid SharedHeap::set_par_threads(uint t) {
1753N/A assert(t == 0 || !UseSerialGC, "Cannot have parallel threads");
0N/A _n_par_threads = t;
1753N/A _process_strong_tasks->set_n_threads(t);
0N/A}
0N/A
0N/Aclass AssertIsPermClosure: public OopClosure {
0N/Apublic:
113N/A virtual void do_oop(oop* p) {
0N/A assert((*p) == NULL || (*p)->is_perm(), "Referent should be perm.");
0N/A }
113N/A virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
0N/A};
0N/Astatic AssertIsPermClosure assert_is_perm_closure;
0N/A
2474N/A#ifdef ASSERT
2474N/Aclass AssertNonScavengableClosure: public OopClosure {
2474N/Apublic:
2474N/A virtual void do_oop(oop* p) {
2474N/A assert(!Universe::heap()->is_in_partial_collection(*p),
2474N/A "Referent should not be scavengable."); }
2474N/A virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
2474N/A};
2474N/Astatic AssertNonScavengableClosure assert_is_non_scavengable_closure;
2474N/A#endif
2474N/A
0N/Avoid SharedHeap::change_strong_roots_parity() {
0N/A // Also set the new collection parity.
0N/A assert(_strong_roots_parity >= 0 && _strong_roots_parity <= 2,
0N/A "Not in range.");
0N/A _strong_roots_parity++;
0N/A if (_strong_roots_parity == 3) _strong_roots_parity = 1;
0N/A assert(_strong_roots_parity >= 1 && _strong_roots_parity <= 2,
0N/A "Not in range.");
0N/A}
0N/A
989N/ASharedHeap::StrongRootsScope::StrongRootsScope(SharedHeap* outer, bool activate)
989N/A : MarkScope(activate)
989N/A{
989N/A if (_active) {
989N/A outer->change_strong_roots_parity();
4592N/A // Zero the claimed high water mark in the StringTable
4592N/A StringTable::clear_parallel_claimed_index();
989N/A }
989N/A}
989N/A
989N/ASharedHeap::StrongRootsScope::~StrongRootsScope() {
989N/A // nothing particular
989N/A}
989N/A
989N/Avoid SharedHeap::process_strong_roots(bool activate_scope,
989N/A bool collecting_perm_gen,
0N/A ScanningOption so,
0N/A OopClosure* roots,
989N/A CodeBlobClosure* code_roots,
0N/A OopsInGenClosure* perm_blk) {
989N/A StrongRootsScope srs(this, activate_scope);
0N/A // General strong roots.
989N/A assert(_strong_roots_parity != 0, "must have called prologue code");
2941N/A // _n_termination for _process_strong_tasks should be set up stream
2941N/A // in a method not running in a GC worker. Otherwise the GC worker
2941N/A // could be trying to change the termination condition while the task
2941N/A // is executing in another GC worker.
0N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_Universe_oops_do)) {
0N/A Universe::oops_do(roots);
0N/A // Consider perm-gen discovered lists to be strong.
0N/A perm_gen()->ref_processor()->weak_oops_do(roots);
0N/A }
0N/A // Global (strong) JNI handles
0N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_JNIHandles_oops_do))
0N/A JNIHandles::oops_do(roots);
4592N/A
0N/A // All threads execute this; the individual threads are task groups.
4592N/A if (CollectedHeap::use_parallel_gc_threads()) {
989N/A Threads::possibly_parallel_oops_do(roots, code_roots);
0N/A } else {
989N/A Threads::oops_do(roots, code_roots);
0N/A }
4592N/A
0N/A if (!_process_strong_tasks-> is_task_claimed(SH_PS_ObjectSynchronizer_oops_do))
0N/A ObjectSynchronizer::oops_do(roots);
0N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_FlatProfiler_oops_do))
0N/A FlatProfiler::oops_do(roots);
0N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_Management_oops_do))
0N/A Management::oops_do(roots);
0N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_jvmti_oops_do))
0N/A JvmtiExport::oops_do(roots);
0N/A
0N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_SystemDictionary_oops_do)) {
0N/A if (so & SO_AllClasses) {
0N/A SystemDictionary::oops_do(roots);
2390N/A } else if (so & SO_SystemClasses) {
2390N/A SystemDictionary::always_strong_oops_do(roots);
2390N/A }
0N/A }
0N/A
4592N/A // All threads execute the following. A specific chunk of buckets
4592N/A // from the StringTable are the individual tasks.
4592N/A if (so & SO_Strings || (!collecting_perm_gen && !JavaObjectsInPerm)) {
4592N/A if (CollectedHeap::use_parallel_gc_threads()) {
4592N/A StringTable::possibly_parallel_oops_do(roots);
4592N/A } else {
2226N/A StringTable::oops_do(roots);
2226N/A }
4592N/A }
4592N/A if (JavaObjectsInPerm) {
4592N/A // Verify the string table contents are in the perm gen
4592N/A if (CollectedHeap::use_parallel_gc_threads()) {
4592N/A NOT_PRODUCT(StringTable::possibly_parallel_oops_do(&assert_is_perm_closure));
4592N/A } else {
2226N/A NOT_PRODUCT(StringTable::oops_do(&assert_is_perm_closure));
2226N/A }
0N/A }
0N/A
0N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_CodeCache_oops_do)) {
989N/A if (so & SO_CodeCache) {
989N/A // (Currently, CMSCollector uses this to do intermediate-strength collections.)
989N/A assert(collecting_perm_gen, "scanning all of code cache");
989N/A assert(code_roots != NULL, "must supply closure for code cache");
989N/A if (code_roots != NULL) {
989N/A CodeCache::blobs_do(code_roots);
989N/A }
989N/A } else if (so & (SO_SystemClasses|SO_AllClasses)) {
989N/A if (!collecting_perm_gen) {
989N/A // If we are collecting from class statics, but we are not going to
989N/A // visit all of the CodeCache, collect from the non-perm roots if any.
989N/A // This makes the code cache function temporarily as a source of strong
989N/A // roots for oops, until the next major collection.
989N/A //
989N/A // If collecting_perm_gen is true, we require that this phase will call
989N/A // CodeCache::do_unloading. This will kill off nmethods with expired
989N/A // weak references, such as stale invokedynamic targets.
989N/A CodeCache::scavenge_root_nmethods_do(code_roots);
989N/A }
989N/A }
2474N/A // Verify that the code cache contents are not subject to
2474N/A // movement by a scavenging collection.
2474N/A DEBUG_ONLY(CodeBlobToOopClosure assert_code_is_non_scavengable(&assert_is_non_scavengable_closure, /*do_marking=*/ false));
2474N/A DEBUG_ONLY(CodeCache::asserted_non_scavengable_nmethods_do(&assert_code_is_non_scavengable));
0N/A }
0N/A
0N/A if (!collecting_perm_gen) {
0N/A // All threads perform this; coordination is handled internally.
0N/A
0N/A rem_set()->younger_refs_iterate(perm_gen(), perm_blk);
0N/A }
0N/A _process_strong_tasks->all_tasks_completed();
0N/A}
0N/A
0N/Aclass AlwaysTrueClosure: public BoolObjectClosure {
0N/Apublic:
0N/A void do_object(oop p) { ShouldNotReachHere(); }
0N/A bool do_object_b(oop p) { return true; }
0N/A};
0N/Astatic AlwaysTrueClosure always_true;
0N/A
0N/Aclass SkipAdjustingSharedStrings: public OopClosure {
0N/A OopClosure* _clo;
0N/Apublic:
0N/A SkipAdjustingSharedStrings(OopClosure* clo) : _clo(clo) {}
0N/A
113N/A virtual void do_oop(oop* p) {
0N/A oop o = (*p);
0N/A if (!o->is_shared_readwrite()) {
0N/A _clo->do_oop(p);
0N/A }
0N/A }
113N/A virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
0N/A};
0N/A
0N/A// Unmarked shared Strings in the StringTable (which got there due to
0N/A// being in the constant pools of as-yet unloaded shared classes) were
0N/A// not marked and therefore did not have their mark words preserved.
0N/A// These entries are also deliberately not purged from the string
0N/A// table during unloading of unmarked strings. If an identity hash
0N/A// code was computed for any of these objects, it will not have been
0N/A// cleared to zero during the forwarding process or by the
0N/A// RecursiveAdjustSharedObjectClosure, and will be confused by the
0N/A// adjusting process as a forwarding pointer. We need to skip
0N/A// forwarding StringTable entries which contain unmarked shared
0N/A// Strings. Actually, since shared strings won't be moving, we can
0N/A// just skip adjusting any shared entries in the string table.
0N/A
0N/Avoid SharedHeap::process_weak_roots(OopClosure* root_closure,
989N/A CodeBlobClosure* code_roots,
0N/A OopClosure* non_root_closure) {
0N/A // Global (weak) JNI handles
0N/A JNIHandles::weak_oops_do(&always_true, root_closure);
0N/A
989N/A CodeCache::blobs_do(code_roots);
0N/A if (UseSharedSpaces && !DumpSharedSpaces) {
0N/A SkipAdjustingSharedStrings skip_closure(root_closure);
0N/A StringTable::oops_do(&skip_closure);
0N/A } else {
0N/A StringTable::oops_do(root_closure);
0N/A }
0N/A}
0N/A
0N/Avoid SharedHeap::set_barrier_set(BarrierSet* bs) {
0N/A _barrier_set = bs;
0N/A // Cached barrier set for fast access in oops
0N/A oopDesc::set_bs(bs);
0N/A}
0N/A
0N/Avoid SharedHeap::post_initialize() {
0N/A ref_processing_init();
0N/A}
0N/A
0N/Avoid SharedHeap::ref_processing_init() {
0N/A perm_gen()->ref_processor_init();
0N/A}
0N/A
0N/A// Some utilities.
342N/Avoid SharedHeap::print_size_transition(outputStream* out,
342N/A size_t bytes_before,
0N/A size_t bytes_after,
0N/A size_t capacity) {
342N/A out->print(" %d%s->%d%s(%d%s)",
0N/A byte_size_in_proper_unit(bytes_before),
0N/A proper_unit_for_byte_size(bytes_before),
0N/A byte_size_in_proper_unit(bytes_after),
0N/A proper_unit_for_byte_size(bytes_after),
0N/A byte_size_in_proper_unit(capacity),
0N/A proper_unit_for_byte_size(capacity));
0N/A}