1 /*
   2  * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARNEWGENERATION_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARNEWGENERATION_HPP
  27 
  28 #include "gc_implementation/shared/parGCAllocBuffer.hpp"
  29 #include "memory/defNewGeneration.hpp"
  30 #include "utilities/taskqueue.hpp"
  31 
  32 class ChunkArray;
  33 class ParScanWithoutBarrierClosure;
  34 class ParScanWithBarrierClosure;
  35 class ParRootScanWithoutBarrierClosure;
  36 class ParRootScanWithBarrierTwoGensClosure;
  37 class ParEvacuateFollowersClosure;
  38 
  39 // It would be better if these types could be kept local to the .cpp file,
  40 // but they must be here to allow ParScanClosure::do_oop_work to be defined
  41 // in genOopClosures.inline.hpp.
  42 
  43 typedef Padded<OopTaskQueue> ObjToScanQueue;
  44 typedef GenericTaskQueueSet<ObjToScanQueue, mtGC> ObjToScanQueueSet;
  45 
  46 class ParKeepAliveClosure: public DefNewGeneration::KeepAliveClosure {
  47  private:
  48   ParScanWeakRefClosure* _par_cl;
  49  protected:
  50   template <class T> void do_oop_work(T* p);
  51  public:
  52   ParKeepAliveClosure(ParScanWeakRefClosure* cl);
  53   virtual void do_oop(oop* p);
  54   virtual void do_oop(narrowOop* p);
  55 };
  56 
  57 // The state needed by thread performing parallel young-gen collection.
  58 class ParScanThreadState {
  59   friend class ParScanThreadStateSet;
  60  private:
  61   ObjToScanQueue *_work_queue;
  62   Stack<oop, mtGC>* const _overflow_stack;
  63 
  64   ParGCAllocBuffer _to_space_alloc_buffer;
  65 
  66   ParScanWithoutBarrierClosure         _to_space_closure; // scan_without_gc_barrier
  67   ParScanWithBarrierClosure            _old_gen_closure; // scan_with_gc_barrier
  68   ParRootScanWithoutBarrierClosure     _to_space_root_closure; // scan_root_without_gc_barrier
  69   // One of these two will be passed to process_strong_roots, which will
  70   // set its generation.  The first is for two-gen configs where the
  71   // old gen collects the perm gen; the second is for arbitrary configs.
  72   // The second isn't used right now (it used to be used for the train, an
  73   // incremental collector) but the declaration has been left as a reminder.
  74   ParRootScanWithBarrierTwoGensClosure _older_gen_closure;
  75   // This closure will always be bound to the old gen; it will be used
  76   // in evacuate_followers.
  77   ParRootScanWithBarrierTwoGensClosure _old_gen_root_closure; // scan_old_root_with_gc_barrier
  78   ParEvacuateFollowersClosure          _evacuate_followers;
  79   DefNewGeneration::IsAliveClosure     _is_alive_closure;
  80   ParScanWeakRefClosure                _scan_weak_ref_closure;
  81   ParKeepAliveClosure                  _keep_alive_closure;
  82 
  83 
  84   Space* _to_space;
  85   Space* to_space() { return _to_space; }
  86 
  87   ParNewGeneration* _young_gen;
  88   ParNewGeneration* young_gen() const { return _young_gen; }
  89 
  90   Generation* _old_gen;
  91   Generation* old_gen() { return _old_gen; }
  92 
  93   HeapWord *_young_old_boundary;
  94 
  95   int _hash_seed;
  96   int _thread_num;
  97   ageTable _ageTable;
  98 
  99   bool _to_space_full;
 100 
 101 #if TASKQUEUE_STATS
 102   size_t _term_attempts;
 103   size_t _overflow_refills;
 104   size_t _overflow_refill_objs;
 105 #endif // TASKQUEUE_STATS
 106 
 107   // Stats for promotion failure
 108   size_t _promotion_failure_size;
 109 
 110   // Timing numbers.
 111   double _start;
 112   double _start_strong_roots;
 113   double _strong_roots_time;
 114   double _start_term;
 115   double _term_time;
 116 
 117   // Helper for trim_queues. Scans subset of an array and makes
 118   // remainder available for work stealing.
 119   void scan_partial_array_and_push_remainder(oop obj);
 120 
 121   // In support of CMS' parallel rescan of survivor space.
 122   ChunkArray* _survivor_chunk_array;
 123   ChunkArray* survivor_chunk_array() { return _survivor_chunk_array; }
 124 
 125   void record_survivor_plab(HeapWord* plab_start, size_t plab_word_size);
 126 
 127   ParScanThreadState(Space* to_space_, ParNewGeneration* gen_,
 128                      Generation* old_gen_, int thread_num_,
 129                      ObjToScanQueueSet* work_queue_set_,
 130                      Stack<oop, mtGC>* overflow_stacks_,
 131                      size_t desired_plab_sz_,
 132                      ParallelTaskTerminator& term_);
 133 
 134  public:
 135   ageTable* age_table() {return &_ageTable;}
 136 
 137   ObjToScanQueue* work_queue() { return _work_queue; }
 138 
 139   ParGCAllocBuffer* to_space_alloc_buffer() {
 140     return &_to_space_alloc_buffer;
 141   }
 142 
 143   ParEvacuateFollowersClosure&      evacuate_followers_closure() { return _evacuate_followers; }
 144   DefNewGeneration::IsAliveClosure& is_alive_closure() { return _is_alive_closure; }
 145   ParScanWeakRefClosure&            scan_weak_ref_closure() { return _scan_weak_ref_closure; }
 146   ParKeepAliveClosure&              keep_alive_closure() { return _keep_alive_closure; }
 147   ParScanClosure&                   older_gen_closure() { return _older_gen_closure; }
 148   ParRootScanWithoutBarrierClosure& to_space_root_closure() { return _to_space_root_closure; };
 149 
 150   // Decrease queue size below "max_size".
 151   void trim_queues(int max_size);
 152 
 153   // Private overflow stack usage
 154   Stack<oop, mtGC>* overflow_stack() { return _overflow_stack; }
 155   bool take_from_overflow_stack();
 156   void push_on_overflow_stack(oop p);
 157 
 158   // Is new_obj a candidate for scan_partial_array_and_push_remainder method.
 159   inline bool should_be_partially_scanned(oop new_obj, oop old_obj) const;
 160 
 161   int* hash_seed()  { return &_hash_seed; }
 162   int  thread_num() { return _thread_num; }
 163 
 164   // Allocate a to-space block of size "sz", or else return NULL.
 165   HeapWord* alloc_in_to_space_slow(size_t word_sz);
 166 
 167   HeapWord* alloc_in_to_space(size_t word_sz) {
 168     HeapWord* obj = to_space_alloc_buffer()->allocate(word_sz);
 169     if (obj != NULL) return obj;
 170     else return alloc_in_to_space_slow(word_sz);
 171   }
 172 
 173   HeapWord* young_old_boundary() { return _young_old_boundary; }
 174 
 175   void set_young_old_boundary(HeapWord *boundary) {
 176     _young_old_boundary = boundary;
 177   }
 178 
 179   // Undo the most recent allocation ("obj", of "word_sz").
 180   void undo_alloc_in_to_space(HeapWord* obj, size_t word_sz);
 181 
 182   // Promotion failure stats
 183   size_t promotion_failure_size() { return promotion_failure_size(); }
 184   void log_promotion_failure(size_t sz) {
 185     if (_promotion_failure_size == 0) {
 186       _promotion_failure_size = sz;
 187     }
 188   }
 189   void print_and_clear_promotion_failure_size();
 190 
 191 #if TASKQUEUE_STATS
 192   TaskQueueStats & taskqueue_stats() const { return _work_queue->stats; }
 193 
 194   size_t term_attempts() const             { return _term_attempts; }
 195   size_t overflow_refills() const          { return _overflow_refills; }
 196   size_t overflow_refill_objs() const      { return _overflow_refill_objs; }
 197 
 198   void note_term_attempt()                 { ++_term_attempts; }
 199   void note_overflow_refill(size_t objs)   {
 200     ++_overflow_refills; _overflow_refill_objs += objs;
 201   }
 202 
 203   void reset_stats();
 204 #endif // TASKQUEUE_STATS
 205 
 206   void start_strong_roots() {
 207     _start_strong_roots = os::elapsedTime();
 208   }
 209   void end_strong_roots() {
 210     _strong_roots_time += (os::elapsedTime() - _start_strong_roots);
 211   }
 212   double strong_roots_time() const { return _strong_roots_time; }
 213   void start_term_time() {
 214     TASKQUEUE_STATS_ONLY(note_term_attempt());
 215     _start_term = os::elapsedTime();
 216   }
 217   void end_term_time() {
 218     _term_time += (os::elapsedTime() - _start_term);
 219   }
 220   double term_time() const { return _term_time; }
 221 
 222   double elapsed_time() const {
 223     return os::elapsedTime() - _start;
 224   }
 225 };
 226 
 227 class ParNewGenTask: public AbstractGangTask {
 228  private:
 229   ParNewGeneration*            _gen;
 230   Generation*                  _next_gen;
 231   HeapWord*                    _young_old_boundary;
 232   class ParScanThreadStateSet* _state_set;
 233 
 234 public:
 235   ParNewGenTask(ParNewGeneration*      gen,
 236                 Generation*            next_gen,
 237                 HeapWord*              young_old_boundary,
 238                 ParScanThreadStateSet* state_set);
 239 
 240   HeapWord* young_old_boundary() { return _young_old_boundary; }
 241 
 242   void work(uint worker_id);
 243 
 244   // Reset the terminator in ParScanThreadStateSet for
 245   // "active_workers" threads.
 246   virtual void set_for_termination(int active_workers);
 247 };
 248 
 249 class KeepAliveClosure: public DefNewGeneration::KeepAliveClosure {
 250  protected:
 251   template <class T> void do_oop_work(T* p);
 252  public:
 253   KeepAliveClosure(ScanWeakRefClosure* cl);
 254   virtual void do_oop(oop* p);
 255   virtual void do_oop(narrowOop* p);
 256 };
 257 
 258 class EvacuateFollowersClosureGeneral: public VoidClosure {
 259  private:
 260   GenCollectedHeap* _gch;
 261   int               _level;
 262   OopsInGenClosure* _scan_cur_or_nonheap;
 263   OopsInGenClosure* _scan_older;
 264  public:
 265   EvacuateFollowersClosureGeneral(GenCollectedHeap* gch, int level,
 266                                   OopsInGenClosure* cur,
 267                                   OopsInGenClosure* older);
 268   virtual void do_void();
 269 };
 270 
 271 // Closure for scanning ParNewGeneration.
 272 // Same as ScanClosure, except does parallel GC barrier.
 273 class ScanClosureWithParBarrier: public ScanClosure {
 274  protected:
 275   template <class T> void do_oop_work(T* p);
 276  public:
 277   ScanClosureWithParBarrier(ParNewGeneration* g, bool gc_barrier);
 278   virtual void do_oop(oop* p);
 279   virtual void do_oop(narrowOop* p);
 280 };
 281 
 282 // Implements AbstractRefProcTaskExecutor for ParNew.
 283 class ParNewRefProcTaskExecutor: public AbstractRefProcTaskExecutor {
 284  private:
 285   ParNewGeneration&      _generation;
 286   ParScanThreadStateSet& _state_set;
 287  public:
 288   ParNewRefProcTaskExecutor(ParNewGeneration& generation,
 289                             ParScanThreadStateSet& state_set)
 290     : _generation(generation), _state_set(state_set)
 291   { }
 292 
 293   // Executes a task using worker threads.
 294   virtual void execute(ProcessTask& task);
 295   virtual void execute(EnqueueTask& task);
 296   // Switch to single threaded mode.
 297   virtual void set_single_threaded_mode();
 298 };
 299 
 300 
 301 // A Generation that does parallel young-gen collection.
 302 
 303 class ParNewGeneration: public DefNewGeneration {
 304   friend class ParNewGenTask;
 305   friend class ParNewRefProcTask;
 306   friend class ParNewRefProcTaskExecutor;
 307   friend class ParScanThreadStateSet;
 308   friend class ParEvacuateFollowersClosure;
 309 
 310  private:
 311   // The per-worker-thread work queues
 312   ObjToScanQueueSet* _task_queues;
 313 
 314   // Per-worker-thread local overflow stacks
 315   Stack<oop, mtGC>* _overflow_stacks;
 316 
 317   // Desired size of survivor space plab's
 318   PLABStats _plab_stats;
 319 
 320   // A list of from-space images of to-be-scanned objects, threaded through
 321   // klass-pointers (klass information already copied to the forwarded
 322   // image.)  Manipulated with CAS.
 323   oop _overflow_list;
 324   NOT_PRODUCT(ssize_t _num_par_pushes;)
 325 
 326   // If true, older generation does not support promotion undo, so avoid.
 327   static bool _avoid_promotion_undo;
 328 
 329   // This closure is used by the reference processor to filter out
 330   // references to live referent.
 331   DefNewGeneration::IsAliveClosure _is_alive_closure;
 332 
 333   static oop real_forwardee_slow(oop obj);
 334   static void waste_some_time();
 335 
 336   // Preserve the mark of "obj", if necessary, in preparation for its mark
 337   // word being overwritten with a self-forwarding-pointer.
 338   void preserve_mark_if_necessary(oop obj, markOop m);
 339 
 340  protected:
 341 
 342   bool _survivor_overflow;
 343 
 344   bool avoid_promotion_undo() { return _avoid_promotion_undo; }
 345   void set_avoid_promotion_undo(bool v) { _avoid_promotion_undo = v; }
 346 
 347   bool survivor_overflow() { return _survivor_overflow; }
 348   void set_survivor_overflow(bool v) { _survivor_overflow = v; }
 349 
 350   // Adjust the tenuring threshold.  See the implementation for
 351   // the details of the policy.
 352   virtual void adjust_desired_tenuring_threshold();
 353 
 354  public:
 355   ParNewGeneration(ReservedSpace rs, size_t initial_byte_size, int level);
 356 
 357   ~ParNewGeneration() {
 358     for (uint i = 0; i < ParallelGCThreads; i++)
 359         delete _task_queues->queue(i);
 360 
 361     delete _task_queues;
 362   }
 363 
 364   static bool in_use();
 365 
 366   virtual void ref_processor_init();
 367   virtual Generation::Name kind()        { return Generation::ParNew; }
 368   virtual const char* name() const;
 369   virtual const char* short_name() const { return "ParNew"; }
 370 
 371   // override
 372   virtual bool refs_discovery_is_mt()     const {
 373     assert(UseParNewGC, "ParNewGeneration only when UseParNewGC");
 374     return ParallelGCThreads > 1;
 375   }
 376 
 377   // Make the collection virtual.
 378   virtual void collect(bool   full,
 379                        bool   clear_all_soft_refs,
 380                        size_t size,
 381                        bool   is_tlab);
 382 
 383   // This needs to be visible to the closure function.
 384   // "obj" is the object to be copied, "m" is a recent value of its mark
 385   // that must not contain a forwarding pointer (though one might be
 386   // inserted in "obj"s mark word by a parallel thread).
 387   inline oop copy_to_survivor_space(ParScanThreadState* par_scan_state,
 388                              oop obj, size_t obj_sz, markOop m) {
 389     if (_avoid_promotion_undo) {
 390        return copy_to_survivor_space_avoiding_promotion_undo(par_scan_state,
 391                                                              obj, obj_sz, m);
 392     }
 393 
 394     return copy_to_survivor_space_with_undo(par_scan_state, obj, obj_sz, m);
 395   }
 396 
 397   oop copy_to_survivor_space_avoiding_promotion_undo(ParScanThreadState* par_scan_state,
 398                              oop obj, size_t obj_sz, markOop m);
 399 
 400   oop copy_to_survivor_space_with_undo(ParScanThreadState* par_scan_state,
 401                              oop obj, size_t obj_sz, markOop m);
 402 
 403   // in support of testing overflow code
 404   NOT_PRODUCT(int _overflow_counter;)
 405   NOT_PRODUCT(bool should_simulate_overflow();)
 406 
 407   // Accessor for overflow list
 408   oop overflow_list() { return _overflow_list; }
 409 
 410   // Push the given (from-space) object on the global overflow list.
 411   void push_on_overflow_list(oop from_space_obj, ParScanThreadState* par_scan_state);
 412 
 413   // If the global overflow list is non-empty, move some tasks from it
 414   // onto "work_q" (which need not be empty).  No more than 1/4 of the
 415   // available space on "work_q" is used.
 416   bool take_from_overflow_list(ParScanThreadState* par_scan_state);
 417   bool take_from_overflow_list_work(ParScanThreadState* par_scan_state);
 418 
 419   // The task queues to be used by parallel GC threads.
 420   ObjToScanQueueSet* task_queues() {
 421     return _task_queues;
 422   }
 423 
 424   PLABStats* plab_stats() {
 425     return &_plab_stats;
 426   }
 427 
 428   size_t desired_plab_sz() {
 429     return _plab_stats.desired_plab_sz();
 430   }
 431 
 432   static oop real_forwardee(oop obj);
 433 
 434   DEBUG_ONLY(static bool is_legal_forward_ptr(oop p);)
 435 };
 436 
 437 #endif // SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARNEWGENERATION_HPP