0N/A/*
2362N/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
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A *
2362N/A */
0N/A
0N/A#ifndef SHARE_VM_MEMORY_SHAREDHEAP_HPP
1870N/A#define SHARE_VM_MEMORY_SHAREDHEAP_HPP
0N/A
0N/A#include "gc_interface/collectedHeap.hpp"
0N/A#include "gc_implementation/shared/gcHeapSummary.hpp"
0N/A#include "memory/generation.hpp"
0N/A#include "memory/permGen.hpp"
0N/A
0N/A// A "SharedHeap" is an implementation of a java heap for HotSpot. This
0N/A// is an abstract class: there may be many different kinds of heaps. This
0N/A// class defines the functions that a heap must implement, and contains
0N/A// infrastructure common to all heaps.
1870N/A
0N/Aclass PermGen;
0N/Aclass Generation;
0N/Aclass BarrierSet;
0N/Aclass GenRemSet;
1266N/Aclass Space;
0N/Aclass SpaceClosure;
0N/Aclass OopClosure;
0N/Aclass OopsInGenClosure;
0N/Aclass ObjectClosure;
0N/Aclass SubTasksDone;
0N/Aclass WorkGang;
0N/Aclass FlexibleWorkGang;
0N/Aclass CollectorPolicy;
0N/Aclass KlassHandle;
0N/A
2037N/A// Note on use of FlexibleWorkGang's for GC.
2037N/A// There are three places where task completion is determined.
0N/A// In
1870N/A// 1) ParallelTaskTerminator::offer_termination() where _n_threads
1870N/A// must be set to the correct value so that count of workers that
1870N/A// have offered termination will exactly match the number
1870N/A// working on the task. Tasks such as those derived from GCTask
1870N/A// use ParallelTaskTerminator's. Tasks that want load balancing
0N/A// by work stealing use this method to gauge completion.
0N/A// 2) SubTasksDone has a variable _n_threads that is used in
0N/A// all_tasks_completed() to determine completion. all_tasks_complete()
0N/A// counts the number of tasks that have been done and then reset
0N/A// the SubTasksDone so that it can be used again. When the number of
0N/A// tasks is set to the number of GC workers, then _n_threads must
0N/A// be set to the number of active GC workers. G1CollectedHeap,
0N/A// HRInto_G1RemSet, GenCollectedHeap and SharedHeap have SubTasksDone.
0N/A// This seems too many.
0N/A// 3) SequentialSubTasksDone has an _n_threads that is used in
0N/A// a way similar to SubTasksDone and has the same dependency on the
0N/A// number of active GC workers. CompactibleFreeListSpace and Space
0N/A// have SequentialSubTasksDone's.
0N/A// Example of using SubTasksDone and SequentialSubTasksDone
0N/A// G1CollectedHeap::g1_process_strong_roots() calls
0N/A// process_strong_roots(false, // no scoping; this is parallel code
0N/A// collecting_perm_gen, so,
0N/A// &buf_scan_non_heap_roots,
0N/A// &eager_scan_code_roots,
0N/A// &buf_scan_perm);
0N/A// which delegates to SharedHeap::process_strong_roots() and uses
0N/A// SubTasksDone* _process_strong_tasks to claim tasks.
1870N/A// process_strong_roots() calls
1870N/A// rem_set()->younger_refs_iterate(perm_gen(), perm_blk);
0N/A// to scan the card table and which eventually calls down into
0N/A// CardTableModRefBS::par_non_clean_card_iterate_work(). This method
0N/A// uses SequentialSubTasksDone* _pst to claim tasks.
0N/A// Both SubTasksDone and SequentialSubTasksDone call their method
0N/A// all_tasks_completed() to count the number of GC workers that have
0N/A// finished their work. That logic is "when all the workers are
1870N/A// finished the tasks are finished".
1870N/A//
1870N/A// The pattern that appears in the code is to set _n_threads
0N/A// to a value > 1 before a task that you would like executed in parallel
0N/A// and then to set it to 0 after that task has completed. A value of
0N/A// 0 is a "special" value in set_n_threads() which translates to
0N/A// setting _n_threads to 1.
0N/A//
0N/A// Some code uses _n_terminiation to decide if work should be done in
0N/A// parallel. The notorious possibly_parallel_oops_do() in threads.cpp
0N/A// is an example of such code. Look for variable "is_par" for other
0N/A// examples.
0N/A//
0N/A// The active_workers is not reset to 0 after a parallel phase. It's
0N/A// value may be used in later phases and in one instance at least
1870N/A// (the parallel remark) it has to be used (the parallel remark depends
1870N/A// on the partitioning done in the previous parallel scavenge).
0N/A
0N/Aclass SharedHeap : public CollectedHeap {
0N/A friend class VMStructs;
0N/A
0N/A friend class VM_GC_Operation;
0N/A friend class VM_CGC_Operation;
0N/A
0N/Aprivate:
0N/A // For claiming strong_roots tasks.
0N/A SubTasksDone* _process_strong_tasks;
0N/A
0N/Aprotected:
0N/A // There should be only a single instance of "SharedHeap" in a program.
0N/A // This is enforced with the protected constructor below, which will also
0N/A // set the static pointer "_sh" to that instance.
0N/A static SharedHeap* _sh;
0N/A
0N/A // All heaps contain a "permanent generation." This is some ways
0N/A // similar to a generation in a generational system, in other ways not.
0N/A // See the "PermGen" class.
0N/A PermGen* _perm_gen;
0N/A
0N/A // and the Gen Remembered Set, at least one good enough to scan the perm
0N/A // gen.
0N/A GenRemSet* _rem_set;
0N/A
0N/A // A gc policy, controls global gc resource issues
0N/A CollectorPolicy *_collector_policy;
0N/A
0N/A // See the discussion below, in the specification of the reader function
0N/A // for this variable.
0N/A int _strong_roots_parity;
0N/A
0N/A // If we're doing parallel GC, use this gang of threads.
0N/A FlexibleWorkGang* _workers;
1870N/A
1870N/A // Full initialization is done in a concrete subtype's "initialize"
0N/A // function.
1870N/A SharedHeap(CollectorPolicy* policy_);
0N/A
0N/A // Returns true if the calling thread holds the heap lock,
1870N/A // or the calling thread is a par gc thread and the heap_lock is held
1870N/A // by the vm thread doing a gc operation.
0N/A bool heap_lock_held_for_gc();
0N/A // True if the heap_lock is held by the a non-gc thread invoking a gc
0N/A // operation.
0N/A bool _thread_holds_heap_lock_for_gc;
0N/A
0N/Apublic:
0N/A static SharedHeap* heap() { return _sh; }
0N/A
0N/A CollectorPolicy *collector_policy() const { return _collector_policy; }
0N/A
0N/A void set_barrier_set(BarrierSet* bs);
0N/A SubTasksDone* process_strong_tasks() { return _process_strong_tasks; }
0N/A
0N/A // Does operations required after initialization has been done.
0N/A virtual void post_initialize();
0N/A
0N/A // Initialization of ("weak") reference processing support
0N/A virtual void ref_processing_init();
0N/A
0N/A void set_perm(PermGen* perm_gen) { _perm_gen = perm_gen; }
0N/A
0N/A // This function returns the "GenRemSet" object that allows us to scan
0N/A // generations; at least the perm gen, possibly more in a fully
0N/A // generational heap.
0N/A GenRemSet* rem_set() { return _rem_set; }
0N/A
0N/A // These function return the "permanent" generation, in which
0N/A // reflective objects are allocated and stored. Two versions, the second
0N/A // of which returns the view of the perm gen as a generation.
0N/A PermGen* perm() const { return _perm_gen; }
0N/A Generation* perm_gen() const { return _perm_gen->as_gen(); }
0N/A
0N/A // Iteration functions.
0N/A void oop_iterate(OopClosure* cl) = 0;
0N/A
0N/A // Same as above, restricted to a memory region.
0N/A virtual void oop_iterate(MemRegion mr, OopClosure* cl) = 0;
0N/A
0N/A // Iterate over all objects allocated since the last collection, calling
0N/A // "cl->do_object" on each. The heap must have been initialized properly
0N/A // to support this function, or else this call will fail.
2037N/A virtual void object_iterate_since_last_GC(ObjectClosure* cl) = 0;
2037N/A
2037N/A // Iterate over all spaces in use in the heap, in an undefined order.
2037N/A virtual void space_iterate(SpaceClosure* cl) = 0;
2037N/A
2037N/A // A SharedHeap will contain some number of spaces. This finds the
2037N/A // space whose reserved area contains the given address, or else returns
2037N/A // NULL.
0N/A virtual Space* space_containing(const void* addr) const = 0;
0N/A
0N/A bool no_gc_in_progress() { return !is_gc_active(); }
0N/A
0N/A // Some collectors will perform "process_strong_roots" in parallel.
0N/A // Such a call will involve claiming some fine-grained tasks, such as
0N/A // scanning of threads. To make this process simpler, we provide the
0N/A // "strong_roots_parity()" method. Collectors that start parallel tasks
0N/A // whose threads invoke "process_strong_roots" must
0N/A // call "change_strong_roots_parity" in sequential code starting such a
0N/A // task. (This also means that a parallel thread may only call
0N/A // process_strong_roots once.)
0N/A //
0N/A // For calls to process_strong_roots by sequential code, the parity is
0N/A // updated automatically.
2551N/A //
0N/A // The idea is that objects representing fine-grained tasks, such as
0N/A // threads, will contain a "parity" field. A task will is claimed in the
0N/A // current "process_strong_roots" call only if its parity field is the
0N/A // same as the "strong_roots_parity"; task claiming is accomplished by
0N/A // updating the parity field to the strong_roots_parity with a CAS.
0N/A //
0N/A // If the client meats this spec, then strong_roots_parity() will have
0N/A // the following properties:
0N/A // a) to return a different value than was returned before the last
0N/A // call to change_strong_roots_parity, and
0N/A // c) to never return a distinguished value (zero) with which such
0N/A // task-claiming variables may be initialized, to indicate "never
0N/A // claimed".
0N/A private:
0N/A void change_strong_roots_parity();
0N/A public:
0N/A int strong_roots_parity() { return _strong_roots_parity; }
0N/A
0N/A // Call these in sequential code around process_strong_roots.
0N/A // strong_roots_prologue calls change_strong_roots_parity, if
0N/A // parallel tasks are enabled.
0N/A class StrongRootsScope : public MarkingCodeBlobClosure::MarkScope {
0N/A public:
0N/A StrongRootsScope(SharedHeap* outer, bool activate = true);
0N/A ~StrongRootsScope();
0N/A };
0N/A friend class StrongRootsScope;
0N/A
0N/A enum ScanningOption {
0N/A SO_None = 0x0,
0N/A SO_AllClasses = 0x1,
2998N/A SO_SystemClasses = 0x2,
0N/A SO_Strings = 0x4,
0N/A SO_CodeCache = 0x8
0N/A };
1870N/A
1870N/A FlexibleWorkGang* workers() const { return _workers; }
0N/A
0N/A // Invoke the "do_oop" method the closure "roots" on all root locations.
0N/A // If "collecting_perm_gen" is false, then roots that may only contain
1870N/A // references to permGen objects are not scanned; instead, in that case,
1870N/A // the "perm_blk" closure is applied to all outgoing refs in the
0N/A // permanent generation. The "so" argument determines which of roots
0N/A // the closure is applied to:
0N/A // "SO_None" does none;
0N/A // "SO_AllClasses" applies the closure to all entries in the SystemDictionary;
0N/A // "SO_SystemClasses" to all the "system" classes and loaders;
1870N/A // "SO_Strings" applies the closure to all entries in StringTable;
1870N/A // "SO_CodeCache" applies the closure to all elements of the CodeCache.
0N/A void process_strong_roots(bool activate_scope,
0N/A bool collecting_perm_gen,
0N/A ScanningOption so,
0N/A OopClosure* roots,
0N/A CodeBlobClosure* code_roots,
0N/A OopsInGenClosure* perm_blk);
0N/A
0N/A // Apply "blk" to all the weak roots of the system. These include
0N/A // JNI weak roots, the code cache, system dictionary, symbol table,
0N/A // string table.
0N/A void process_weak_roots(OopClosure* root_closure,
0N/A CodeBlobClosure* code_roots,
0N/A OopClosure* non_root_closure);
0N/A
0N/A // The functions below are helper functions that a subclass of
0N/A // "SharedHeap" can use in the implementation of its virtual
0N/A // functions.
0N/A
0N/Apublic:
0N/A
0N/A // Do anything common to GC's.
0N/A virtual void gc_prologue(bool full) = 0;
0N/A virtual void gc_epilogue(bool full) = 0;
0N/A
0N/A // Sets the number of parallel threads that will be doing tasks
0N/A // (such as process strong roots) subsequently.
0N/A virtual void set_par_threads(uint t);
0N/A
0N/A int n_termination();
0N/A void set_n_termination(int t);
0N/A
0N/A //
0N/A // New methods from CollectedHeap
0N/A //
0N/A
0N/A size_t permanent_capacity() const {
0N/A assert(perm_gen(), "NULL perm gen");
0N/A return perm_gen()->capacity();
0N/A }
0N/A
0N/A size_t permanent_used() const {
0N/A assert(perm_gen(), "NULL perm gen");
0N/A return perm_gen()->used();
0N/A }
0N/A
0N/A VirtualSpaceSummary create_perm_gen_space_summary() {
0N/A HeapWord* start = perm_gen()->reserved().start();
0N/A return VirtualSpaceSummary(start, (HeapWord*)((uintptr_t)start + perm_gen()->capacity()), perm_gen()->reserved().end());
0N/A }
0N/A
0N/A bool is_in_permanent(const void *p) const {
0N/A assert(perm_gen(), "NULL perm gen");
0N/A return perm_gen()->is_in_reserved(p);
0N/A }
0N/A
0N/A // Different from is_in_permanent in that is_in_permanent
0N/A // only checks if p is in the reserved area of the heap
0N/A // and this checks to see if it in the commited area.
0N/A // This is typically used by things like the forte stackwalker
0N/A // during verification of suspicious frame values.
0N/A bool is_permanent(const void *p) const {
0N/A assert(perm_gen(), "NULL perm gen");
0N/A return perm_gen()->is_in(p);
0N/A }
0N/A
0N/A HeapWord* permanent_mem_allocate(size_t size) {
0N/A assert(perm_gen(), "NULL perm gen");
1266N/A return _perm_gen->mem_allocate(size);
0N/A }
0N/A
0N/A void permanent_oop_iterate(OopClosure* cl) {
0N/A assert(perm_gen(), "NULL perm gen");
0N/A _perm_gen->oop_iterate(cl);
0N/A }
0N/A
0N/A void permanent_object_iterate(ObjectClosure* cl) {
0N/A assert(perm_gen(), "NULL perm gen");
0N/A _perm_gen->object_iterate(cl);
0N/A }
0N/A
0N/A // Some utilities.
0N/A void print_size_transition(outputStream* out,
0N/A size_t bytes_before,
0N/A size_t bytes_after,
0N/A size_t capacity);
0N/A};
1870N/A
1870N/A#endif // SHARE_VM_MEMORY_SHAREDHEAP_HPP
1870N/A