duke@435: /* stefank@5515: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARNEWGENERATION_HPP stefank@2314: #define SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARNEWGENERATION_HPP stefank@2314: sla@5237: #include "gc_implementation/shared/gcTrace.hpp" johnc@3982: #include "gc_implementation/shared/parGCAllocBuffer.hpp" sla@5237: #include "gc_implementation/shared/copyFailedInfo.hpp" stefank@2314: #include "memory/defNewGeneration.hpp" stefank@5515: #include "memory/padded.hpp" stefank@2314: #include "utilities/taskqueue.hpp" stefank@2314: duke@435: class ChunkArray; duke@435: class ParScanWithoutBarrierClosure; duke@435: class ParScanWithBarrierClosure; duke@435: class ParRootScanWithoutBarrierClosure; duke@435: class ParRootScanWithBarrierTwoGensClosure; duke@435: class ParEvacuateFollowersClosure; duke@435: duke@435: // It would be better if these types could be kept local to the .cpp file, duke@435: // but they must be here to allow ParScanClosure::do_oop_work to be defined duke@435: // in genOopClosures.inline.hpp. duke@435: jcoomes@2020: typedef Padded ObjToScanQueue; zgu@3900: typedef GenericTaskQueueSet ObjToScanQueueSet; duke@435: duke@435: class ParKeepAliveClosure: public DefNewGeneration::KeepAliveClosure { coleenp@548: private: duke@435: ParScanWeakRefClosure* _par_cl; coleenp@548: protected: coleenp@548: template void do_oop_work(T* p); duke@435: public: duke@435: ParKeepAliveClosure(ParScanWeakRefClosure* cl); coleenp@548: virtual void do_oop(oop* p); coleenp@548: virtual void do_oop(narrowOop* p); duke@435: }; duke@435: duke@435: // The state needed by thread performing parallel young-gen collection. duke@435: class ParScanThreadState { duke@435: friend class ParScanThreadStateSet; coleenp@548: private: duke@435: ObjToScanQueue *_work_queue; zgu@3900: Stack* const _overflow_stack; duke@435: duke@435: ParGCAllocBuffer _to_space_alloc_buffer; duke@435: duke@435: ParScanWithoutBarrierClosure _to_space_closure; // scan_without_gc_barrier duke@435: ParScanWithBarrierClosure _old_gen_closure; // scan_with_gc_barrier duke@435: ParRootScanWithoutBarrierClosure _to_space_root_closure; // scan_root_without_gc_barrier stefank@6992: // One of these two will be passed to process_roots, which will duke@435: // set its generation. The first is for two-gen configs where the duke@435: // old gen collects the perm gen; the second is for arbitrary configs. duke@435: // The second isn't used right now (it used to be used for the train, an duke@435: // incremental collector) but the declaration has been left as a reminder. duke@435: ParRootScanWithBarrierTwoGensClosure _older_gen_closure; duke@435: // This closure will always be bound to the old gen; it will be used duke@435: // in evacuate_followers. duke@435: ParRootScanWithBarrierTwoGensClosure _old_gen_root_closure; // scan_old_root_with_gc_barrier duke@435: ParEvacuateFollowersClosure _evacuate_followers; duke@435: DefNewGeneration::IsAliveClosure _is_alive_closure; duke@435: ParScanWeakRefClosure _scan_weak_ref_closure; duke@435: ParKeepAliveClosure _keep_alive_closure; duke@435: duke@435: duke@435: Space* _to_space; duke@435: Space* to_space() { return _to_space; } duke@435: ysr@1114: ParNewGeneration* _young_gen; ysr@1114: ParNewGeneration* young_gen() const { return _young_gen; } ysr@1114: duke@435: Generation* _old_gen; duke@435: Generation* old_gen() { return _old_gen; } duke@435: duke@435: HeapWord *_young_old_boundary; duke@435: duke@435: int _hash_seed; duke@435: int _thread_num; duke@435: ageTable _ageTable; duke@435: duke@435: bool _to_space_full; duke@435: jcoomes@2065: #if TASKQUEUE_STATS jcoomes@2065: size_t _term_attempts; jcoomes@2065: size_t _overflow_refills; jcoomes@2065: size_t _overflow_refill_objs; jcoomes@2065: #endif // TASKQUEUE_STATS duke@435: ysr@1580: // Stats for promotion failure sla@5237: PromotionFailedInfo _promotion_failed_info; ysr@1580: duke@435: // Timing numbers. duke@435: double _start; duke@435: double _start_strong_roots; duke@435: double _strong_roots_time; duke@435: double _start_term; duke@435: double _term_time; duke@435: duke@435: // Helper for trim_queues. Scans subset of an array and makes duke@435: // remainder available for work stealing. duke@435: void scan_partial_array_and_push_remainder(oop obj); duke@435: duke@435: // In support of CMS' parallel rescan of survivor space. duke@435: ChunkArray* _survivor_chunk_array; duke@435: ChunkArray* survivor_chunk_array() { return _survivor_chunk_array; } duke@435: duke@435: void record_survivor_plab(HeapWord* plab_start, size_t plab_word_size); duke@435: duke@435: ParScanThreadState(Space* to_space_, ParNewGeneration* gen_, duke@435: Generation* old_gen_, int thread_num_, ysr@1130: ObjToScanQueueSet* work_queue_set_, zgu@3900: Stack* overflow_stacks_, ysr@1130: size_t desired_plab_sz_, duke@435: ParallelTaskTerminator& term_); duke@435: coleenp@548: public: duke@435: ageTable* age_table() {return &_ageTable;} duke@435: duke@435: ObjToScanQueue* work_queue() { return _work_queue; } duke@435: duke@435: ParGCAllocBuffer* to_space_alloc_buffer() { duke@435: return &_to_space_alloc_buffer; duke@435: } duke@435: duke@435: ParEvacuateFollowersClosure& evacuate_followers_closure() { return _evacuate_followers; } duke@435: DefNewGeneration::IsAliveClosure& is_alive_closure() { return _is_alive_closure; } duke@435: ParScanWeakRefClosure& scan_weak_ref_closure() { return _scan_weak_ref_closure; } duke@435: ParKeepAliveClosure& keep_alive_closure() { return _keep_alive_closure; } duke@435: ParScanClosure& older_gen_closure() { return _older_gen_closure; } duke@435: ParRootScanWithoutBarrierClosure& to_space_root_closure() { return _to_space_root_closure; }; duke@435: duke@435: // Decrease queue size below "max_size". duke@435: void trim_queues(int max_size); duke@435: ysr@1114: // Private overflow stack usage zgu@3900: Stack* overflow_stack() { return _overflow_stack; } ysr@1114: bool take_from_overflow_stack(); ysr@1114: void push_on_overflow_stack(oop p); ysr@1114: duke@435: // Is new_obj a candidate for scan_partial_array_and_push_remainder method. duke@435: inline bool should_be_partially_scanned(oop new_obj, oop old_obj) const; duke@435: duke@435: int* hash_seed() { return &_hash_seed; } duke@435: int thread_num() { return _thread_num; } duke@435: duke@435: // Allocate a to-space block of size "sz", or else return NULL. duke@435: HeapWord* alloc_in_to_space_slow(size_t word_sz); duke@435: duke@435: HeapWord* alloc_in_to_space(size_t word_sz) { duke@435: HeapWord* obj = to_space_alloc_buffer()->allocate(word_sz); duke@435: if (obj != NULL) return obj; duke@435: else return alloc_in_to_space_slow(word_sz); duke@435: } duke@435: duke@435: HeapWord* young_old_boundary() { return _young_old_boundary; } duke@435: duke@435: void set_young_old_boundary(HeapWord *boundary) { duke@435: _young_old_boundary = boundary; duke@435: } duke@435: duke@435: // Undo the most recent allocation ("obj", of "word_sz"). duke@435: void undo_alloc_in_to_space(HeapWord* obj, size_t word_sz); duke@435: ysr@1580: // Promotion failure stats sla@5237: void register_promotion_failure(size_t sz) { sla@5237: _promotion_failed_info.register_copy_failure(sz); ysr@1580: } sla@5237: PromotionFailedInfo& promotion_failed_info() { sla@5237: return _promotion_failed_info; sla@5237: } sla@5237: bool promotion_failed() { sla@5237: return _promotion_failed_info.has_failed(); sla@5237: } sla@5237: void print_promotion_failure_size(); ysr@1580: jcoomes@2065: #if TASKQUEUE_STATS jcoomes@2065: TaskQueueStats & taskqueue_stats() const { return _work_queue->stats; } duke@435: jcoomes@2065: size_t term_attempts() const { return _term_attempts; } jcoomes@2065: size_t overflow_refills() const { return _overflow_refills; } jcoomes@2065: size_t overflow_refill_objs() const { return _overflow_refill_objs; } jcoomes@2065: jcoomes@2065: void note_term_attempt() { ++_term_attempts; } jcoomes@2065: void note_overflow_refill(size_t objs) { jcoomes@2065: ++_overflow_refills; _overflow_refill_objs += objs; duke@435: } duke@435: jcoomes@2065: void reset_stats(); jcoomes@2065: #endif // TASKQUEUE_STATS jcoomes@2065: duke@435: void start_strong_roots() { duke@435: _start_strong_roots = os::elapsedTime(); duke@435: } duke@435: void end_strong_roots() { duke@435: _strong_roots_time += (os::elapsedTime() - _start_strong_roots); duke@435: } jcoomes@2065: double strong_roots_time() const { return _strong_roots_time; } duke@435: void start_term_time() { jcoomes@2065: TASKQUEUE_STATS_ONLY(note_term_attempt()); duke@435: _start_term = os::elapsedTime(); duke@435: } duke@435: void end_term_time() { duke@435: _term_time += (os::elapsedTime() - _start_term); duke@435: } jcoomes@2065: double term_time() const { return _term_time; } duke@435: jcoomes@2065: double elapsed_time() const { duke@435: return os::elapsedTime() - _start; duke@435: } duke@435: }; duke@435: duke@435: class ParNewGenTask: public AbstractGangTask { coleenp@548: private: coleenp@548: ParNewGeneration* _gen; coleenp@548: Generation* _next_gen; coleenp@548: HeapWord* _young_old_boundary; duke@435: class ParScanThreadStateSet* _state_set; duke@435: duke@435: public: duke@435: ParNewGenTask(ParNewGeneration* gen, duke@435: Generation* next_gen, duke@435: HeapWord* young_old_boundary, duke@435: ParScanThreadStateSet* state_set); duke@435: duke@435: HeapWord* young_old_boundary() { return _young_old_boundary; } duke@435: jmasa@3357: void work(uint worker_id); jmasa@3294: jmasa@3294: // Reset the terminator in ParScanThreadStateSet for jmasa@3294: // "active_workers" threads. jmasa@3294: virtual void set_for_termination(int active_workers); duke@435: }; duke@435: duke@435: class KeepAliveClosure: public DefNewGeneration::KeepAliveClosure { coleenp@548: protected: coleenp@548: template void do_oop_work(T* p); duke@435: public: duke@435: KeepAliveClosure(ScanWeakRefClosure* cl); coleenp@548: virtual void do_oop(oop* p); coleenp@548: virtual void do_oop(narrowOop* p); duke@435: }; duke@435: duke@435: class EvacuateFollowersClosureGeneral: public VoidClosure { coleenp@548: private: coleenp@548: GenCollectedHeap* _gch; coleenp@548: int _level; coleenp@548: OopsInGenClosure* _scan_cur_or_nonheap; coleenp@548: OopsInGenClosure* _scan_older; coleenp@548: public: coleenp@548: EvacuateFollowersClosureGeneral(GenCollectedHeap* gch, int level, coleenp@548: OopsInGenClosure* cur, coleenp@548: OopsInGenClosure* older); coleenp@548: virtual void do_void(); duke@435: }; duke@435: duke@435: // Closure for scanning ParNewGeneration. duke@435: // Same as ScanClosure, except does parallel GC barrier. duke@435: class ScanClosureWithParBarrier: public ScanClosure { coleenp@548: protected: coleenp@548: template void do_oop_work(T* p); coleenp@548: public: duke@435: ScanClosureWithParBarrier(ParNewGeneration* g, bool gc_barrier); coleenp@548: virtual void do_oop(oop* p); coleenp@548: virtual void do_oop(narrowOop* p); duke@435: }; duke@435: duke@435: // Implements AbstractRefProcTaskExecutor for ParNew. duke@435: class ParNewRefProcTaskExecutor: public AbstractRefProcTaskExecutor { coleenp@548: private: coleenp@548: ParNewGeneration& _generation; coleenp@548: ParScanThreadStateSet& _state_set; coleenp@548: public: duke@435: ParNewRefProcTaskExecutor(ParNewGeneration& generation, duke@435: ParScanThreadStateSet& state_set) duke@435: : _generation(generation), _state_set(state_set) duke@435: { } duke@435: duke@435: // Executes a task using worker threads. duke@435: virtual void execute(ProcessTask& task); duke@435: virtual void execute(EnqueueTask& task); duke@435: // Switch to single threaded mode. duke@435: virtual void set_single_threaded_mode(); duke@435: }; duke@435: duke@435: duke@435: // A Generation that does parallel young-gen collection. duke@435: duke@435: class ParNewGeneration: public DefNewGeneration { duke@435: friend class ParNewGenTask; duke@435: friend class ParNewRefProcTask; duke@435: friend class ParNewRefProcTaskExecutor; duke@435: friend class ParScanThreadStateSet; ysr@969: friend class ParEvacuateFollowersClosure; duke@435: coleenp@548: private: ysr@1130: // The per-worker-thread work queues duke@435: ObjToScanQueueSet* _task_queues; duke@435: ysr@1130: // Per-worker-thread local overflow stacks zgu@3900: Stack* _overflow_stacks; ysr@1130: duke@435: // Desired size of survivor space plab's duke@435: PLABStats _plab_stats; duke@435: duke@435: // A list of from-space images of to-be-scanned objects, threaded through duke@435: // klass-pointers (klass information already copied to the forwarded duke@435: // image.) Manipulated with CAS. duke@435: oop _overflow_list; ysr@969: NOT_PRODUCT(ssize_t _num_par_pushes;) duke@435: duke@435: // If true, older generation does not support promotion undo, so avoid. duke@435: static bool _avoid_promotion_undo; duke@435: duke@435: // This closure is used by the reference processor to filter out duke@435: // references to live referent. duke@435: DefNewGeneration::IsAliveClosure _is_alive_closure; duke@435: duke@435: static oop real_forwardee_slow(oop obj); duke@435: static void waste_some_time(); duke@435: duke@435: // Preserve the mark of "obj", if necessary, in preparation for its mark duke@435: // word being overwritten with a self-forwarding-pointer. duke@435: void preserve_mark_if_necessary(oop obj, markOop m); duke@435: sla@5237: void handle_promotion_failed(GenCollectedHeap* gch, ParScanThreadStateSet& thread_state_set, ParNewTracer& gc_tracer); sla@5237: duke@435: protected: duke@435: duke@435: bool _survivor_overflow; duke@435: duke@435: bool avoid_promotion_undo() { return _avoid_promotion_undo; } duke@435: void set_avoid_promotion_undo(bool v) { _avoid_promotion_undo = v; } duke@435: duke@435: bool survivor_overflow() { return _survivor_overflow; } duke@435: void set_survivor_overflow(bool v) { _survivor_overflow = v; } duke@435: coleenp@548: public: duke@435: ParNewGeneration(ReservedSpace rs, size_t initial_byte_size, int level); duke@435: duke@435: ~ParNewGeneration() { duke@435: for (uint i = 0; i < ParallelGCThreads; i++) duke@435: delete _task_queues->queue(i); duke@435: duke@435: delete _task_queues; duke@435: } duke@435: duke@435: virtual void ref_processor_init(); duke@435: virtual Generation::Name kind() { return Generation::ParNew; } duke@435: virtual const char* name() const; duke@435: virtual const char* short_name() const { return "ParNew"; } duke@435: duke@435: // override duke@435: virtual bool refs_discovery_is_mt() const { duke@435: assert(UseParNewGC, "ParNewGeneration only when UseParNewGC"); duke@435: return ParallelGCThreads > 1; duke@435: } duke@435: duke@435: // Make the collection virtual. duke@435: virtual void collect(bool full, duke@435: bool clear_all_soft_refs, duke@435: size_t size, duke@435: bool is_tlab); duke@435: duke@435: // This needs to be visible to the closure function. duke@435: // "obj" is the object to be copied, "m" is a recent value of its mark duke@435: // that must not contain a forwarding pointer (though one might be duke@435: // inserted in "obj"s mark word by a parallel thread). duke@435: inline oop copy_to_survivor_space(ParScanThreadState* par_scan_state, duke@435: oop obj, size_t obj_sz, markOop m) { duke@435: if (_avoid_promotion_undo) { duke@435: return copy_to_survivor_space_avoiding_promotion_undo(par_scan_state, duke@435: obj, obj_sz, m); duke@435: } duke@435: duke@435: return copy_to_survivor_space_with_undo(par_scan_state, obj, obj_sz, m); duke@435: } duke@435: duke@435: oop copy_to_survivor_space_avoiding_promotion_undo(ParScanThreadState* par_scan_state, duke@435: oop obj, size_t obj_sz, markOop m); duke@435: duke@435: oop copy_to_survivor_space_with_undo(ParScanThreadState* par_scan_state, duke@435: oop obj, size_t obj_sz, markOop m); duke@435: ysr@969: // in support of testing overflow code ysr@969: NOT_PRODUCT(int _overflow_counter;) ysr@969: NOT_PRODUCT(bool should_simulate_overflow();) ysr@969: ysr@1114: // Accessor for overflow list ysr@1114: oop overflow_list() { return _overflow_list; } ysr@1114: duke@435: // Push the given (from-space) object on the global overflow list. ysr@969: void push_on_overflow_list(oop from_space_obj, ParScanThreadState* par_scan_state); duke@435: duke@435: // If the global overflow list is non-empty, move some tasks from it ysr@1114: // onto "work_q" (which need not be empty). No more than 1/4 of the ysr@1114: // available space on "work_q" is used. duke@435: bool take_from_overflow_list(ParScanThreadState* par_scan_state); ysr@1114: bool take_from_overflow_list_work(ParScanThreadState* par_scan_state); duke@435: duke@435: // The task queues to be used by parallel GC threads. duke@435: ObjToScanQueueSet* task_queues() { duke@435: return _task_queues; duke@435: } duke@435: duke@435: PLABStats* plab_stats() { duke@435: return &_plab_stats; duke@435: } duke@435: duke@435: size_t desired_plab_sz() { duke@435: return _plab_stats.desired_plab_sz(); duke@435: } duke@435: duke@435: static oop real_forwardee(oop obj); duke@435: duke@435: DEBUG_ONLY(static bool is_legal_forward_ptr(oop p);) duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARNEWGENERATION_HPP