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