1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)sharedHeap.cpp       1.59 07/05/17 15:55:10 JVM"
   3 #endif
   4 /*
   5  * Copyright 2000-2007 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 # include "incls/_precompiled.incl"
  29 # include "incls/_sharedHeap.cpp.incl"
  30 
  31 SharedHeap* SharedHeap::_sh;
  32 
  33 // The set of potentially parallel tasks in strong root scanning.
  34 enum SH_process_strong_roots_tasks {
  35   SH_PS_Universe_oops_do,
  36   SH_PS_JNIHandles_oops_do,
  37   SH_PS_ObjectSynchronizer_oops_do,
  38   SH_PS_FlatProfiler_oops_do,
  39   SH_PS_Management_oops_do,
  40   SH_PS_SystemDictionary_oops_do,
  41   SH_PS_jvmti_oops_do,
  42   SH_PS_vmSymbols_oops_do,
  43   SH_PS_SymbolTable_oops_do,
  44   SH_PS_StringTable_oops_do,
  45   SH_PS_CodeCache_oops_do,
  46   // Leave this one last.
  47   SH_PS_NumElements
  48 };
  49 
  50 SharedHeap::SharedHeap(CollectorPolicy* policy_) :
  51   CollectedHeap(),
  52   _collector_policy(policy_),
  53   _perm_gen(NULL), _rem_set(NULL),
  54   _strong_roots_parity(0),
  55   _process_strong_tasks(new SubTasksDone(SH_PS_NumElements)),
  56   _workers(NULL), _n_par_threads(0)
  57 {
  58   if (_process_strong_tasks == NULL || !_process_strong_tasks->valid()) {
  59     vm_exit_during_initialization("Failed necessary allocation.");
  60   }
  61   _sh = this;  // ch is static, should be set only once.
  62   if ((UseParNewGC ||
  63       (UseConcMarkSweepGC && CMSParallelRemarkEnabled)) &&
  64       ParallelGCThreads > 0) {
  65     _workers = new WorkGang("Parallel GC Threads", ParallelGCThreads, true);
  66     if (_workers == NULL) {
  67       vm_exit_during_initialization("Failed necessary allocation.");
  68     }
  69   }
  70 }
  71 
  72 
  73 void SharedHeap::set_par_threads(int t) {
  74   _n_par_threads = t;
  75   _process_strong_tasks->set_par_threads(t);
  76 }
  77 
  78 class AssertIsPermClosure: public OopClosure {
  79 public:
  80   void do_oop(oop* p) {
  81     assert((*p) == NULL || (*p)->is_perm(), "Referent should be perm.");
  82   }
  83 };
  84 static AssertIsPermClosure assert_is_perm_closure;
  85 
  86 void SharedHeap::change_strong_roots_parity() {
  87   // Also set the new collection parity.
  88   assert(_strong_roots_parity >= 0 && _strong_roots_parity <= 2,
  89          "Not in range.");
  90   _strong_roots_parity++;
  91   if (_strong_roots_parity == 3) _strong_roots_parity = 1;
  92   assert(_strong_roots_parity >= 1 && _strong_roots_parity <= 2,
  93          "Not in range.");
  94 }
  95 
  96 void SharedHeap::process_strong_roots(bool collecting_perm_gen,
  97                                       ScanningOption so,
  98                                       OopClosure* roots,
  99                                       OopsInGenClosure* perm_blk) {
 100   // General strong roots.
 101   if (n_par_threads() == 0) change_strong_roots_parity();
 102   if (!_process_strong_tasks->is_task_claimed(SH_PS_Universe_oops_do)) {
 103     Universe::oops_do(roots);
 104     ReferenceProcessor::oops_do(roots);
 105     // Consider perm-gen discovered lists to be strong.
 106     perm_gen()->ref_processor()->weak_oops_do(roots);
 107   }
 108   // Global (strong) JNI handles
 109   if (!_process_strong_tasks->is_task_claimed(SH_PS_JNIHandles_oops_do))
 110     JNIHandles::oops_do(roots);
 111   // All threads execute this; the individual threads are task groups.
 112   if (ParallelGCThreads > 0) {
 113     Threads::possibly_parallel_oops_do(roots);
 114   } else {
 115     Threads::oops_do(roots);
 116   }
 117   if (!_process_strong_tasks-> is_task_claimed(SH_PS_ObjectSynchronizer_oops_do))
 118     ObjectSynchronizer::oops_do(roots);
 119   if (!_process_strong_tasks->is_task_claimed(SH_PS_FlatProfiler_oops_do))
 120     FlatProfiler::oops_do(roots);
 121   if (!_process_strong_tasks->is_task_claimed(SH_PS_Management_oops_do))
 122     Management::oops_do(roots);
 123   if (!_process_strong_tasks->is_task_claimed(SH_PS_jvmti_oops_do))
 124     JvmtiExport::oops_do(roots); 
 125 
 126   if (!_process_strong_tasks->is_task_claimed(SH_PS_SystemDictionary_oops_do)) {
 127     if (so & SO_AllClasses) {
 128       SystemDictionary::oops_do(roots);
 129     } else
 130       if (so & SO_SystemClasses) {
 131         SystemDictionary::always_strong_oops_do(roots);
 132       }
 133   }
 134 
 135   if (!_process_strong_tasks->is_task_claimed(SH_PS_SymbolTable_oops_do)) {
 136     if (so & SO_Symbols) {
 137       SymbolTable::oops_do(roots);
 138     }
 139     // Verify if the symbol table contents are in the perm gen
 140     NOT_PRODUCT(SymbolTable::oops_do(&assert_is_perm_closure));
 141   }
 142 
 143   if (!_process_strong_tasks->is_task_claimed(SH_PS_StringTable_oops_do)) {
 144      if (so & SO_Strings) {
 145        StringTable::oops_do(roots);
 146      }
 147     // Verify if the string table contents are in the perm gen
 148     NOT_PRODUCT(StringTable::oops_do(&assert_is_perm_closure));
 149   }
 150 
 151   if (!_process_strong_tasks->is_task_claimed(SH_PS_CodeCache_oops_do)) {
 152      if (so & SO_CodeCache) {
 153        CodeCache::oops_do(roots);
 154      }
 155     // Verify if the code cache contents are in the perm gen
 156     NOT_PRODUCT(CodeCache::oops_do(&assert_is_perm_closure));
 157   }
 158 
 159   // Roots that should point only into permanent generation.
 160   {
 161     OopClosure* blk = NULL;
 162     if (collecting_perm_gen) {
 163       blk = roots;
 164     } else {
 165       debug_only(blk = &assert_is_perm_closure);
 166     }
 167     if (blk != NULL) {
 168       if (!_process_strong_tasks->is_task_claimed(SH_PS_vmSymbols_oops_do))
 169         vmSymbols::oops_do(blk);
 170     }
 171   }
 172 
 173   if (!collecting_perm_gen) {
 174     // All threads perform this; coordination is handled internally.
 175 
 176     rem_set()->younger_refs_iterate(perm_gen(), perm_blk);
 177   }
 178   _process_strong_tasks->all_tasks_completed();
 179 }
 180 
 181 class AlwaysTrueClosure: public BoolObjectClosure {
 182 public:
 183   void do_object(oop p) { ShouldNotReachHere(); }
 184   bool do_object_b(oop p) { return true; }
 185 };
 186 static AlwaysTrueClosure always_true;
 187 
 188 class SkipAdjustingSharedStrings: public OopClosure {
 189   OopClosure* _clo;
 190 public:
 191   SkipAdjustingSharedStrings(OopClosure* clo) : _clo(clo) {}
 192 
 193   void do_oop(oop* p) {
 194     oop o = (*p);
 195     if (!o->is_shared_readwrite()) {
 196       _clo->do_oop(p);
 197     }
 198   }
 199 };
 200 
 201 // Unmarked shared Strings in the StringTable (which got there due to
 202 // being in the constant pools of as-yet unloaded shared classes) were
 203 // not marked and therefore did not have their mark words preserved.
 204 // These entries are also deliberately not purged from the string
 205 // table during unloading of unmarked strings. If an identity hash
 206 // code was computed for any of these objects, it will not have been
 207 // cleared to zero during the forwarding process or by the
 208 // RecursiveAdjustSharedObjectClosure, and will be confused by the
 209 // adjusting process as a forwarding pointer. We need to skip
 210 // forwarding StringTable entries which contain unmarked shared
 211 // Strings. Actually, since shared strings won't be moving, we can
 212 // just skip adjusting any shared entries in the string table.
 213 
 214 void SharedHeap::process_weak_roots(OopClosure* root_closure,
 215                                     OopClosure* non_root_closure) {
 216   // Global (weak) JNI handles
 217   JNIHandles::weak_oops_do(&always_true, root_closure);
 218 
 219   CodeCache::oops_do(non_root_closure);
 220   SymbolTable::oops_do(root_closure);
 221   if (UseSharedSpaces && !DumpSharedSpaces) {
 222     SkipAdjustingSharedStrings skip_closure(root_closure);
 223     StringTable::oops_do(&skip_closure);
 224   } else {
 225     StringTable::oops_do(root_closure);
 226   }
 227 }
 228 
 229 void SharedHeap::set_barrier_set(BarrierSet* bs) {
 230   _barrier_set = bs;
 231   // Cached barrier set for fast access in oops
 232   oopDesc::set_bs(bs);
 233 }
 234 
 235 void SharedHeap::post_initialize() {
 236   ref_processing_init();
 237 }
 238 
 239 void SharedHeap::ref_processing_init() {
 240   perm_gen()->ref_processor_init();
 241 }
 242 
 243 void SharedHeap::fill_region_with_object(MemRegion mr) {
 244   // Disable the posting of JVMTI VMObjectAlloc events as we
 245   // don't want the filling of tlabs with filler arrays to be
 246   // reported to the profiler.
 247   NoJvmtiVMObjectAllocMark njm;    
 248   
 249   // Disable low memory detector because there is no real allocation.
 250   LowMemoryDetectorDisabler lmd_dis;
 251 
 252   // It turns out that post_allocation_setup_array takes a handle, so the
 253   // call below contains an implicit conversion.  Best to free that handle
 254   // as soon as possible.
 255   HandleMark hm;
 256 
 257   size_t word_size = mr.word_size();
 258   size_t aligned_array_header_size =
 259     align_object_size(typeArrayOopDesc::header_size(T_INT));
 260 
 261   if (word_size >= aligned_array_header_size) {
 262     const size_t array_length =
 263       pointer_delta(mr.end(), mr.start()) -
 264       typeArrayOopDesc::header_size(T_INT);
 265     const size_t array_length_words =
 266       array_length * (HeapWordSize/sizeof(jint));
 267     post_allocation_setup_array(Universe::intArrayKlassObj(),
 268                                 mr.start(),
 269                                 mr.word_size(),
 270                                 (int)array_length_words);
 271 #ifdef ASSERT
 272     HeapWord* elt_words = (mr.start() + typeArrayOopDesc::header_size(T_INT));
 273     Copy::fill_to_words(elt_words, array_length, 0xDEAFBABE);
 274 #endif
 275   } else {
 276     assert(word_size == (size_t)oopDesc::header_size(), "Unaligned?");
 277     post_allocation_setup_obj(SystemDictionary::object_klass(),
 278                               mr.start(),
 279                               mr.word_size());
 280   }
 281 }
 282 
 283 // Some utilities.
 284 void SharedHeap::print_size_transition(size_t bytes_before,
 285                                        size_t bytes_after,
 286                                        size_t capacity) {
 287   tty->print(" %d%s->%d%s(%d%s)",
 288              byte_size_in_proper_unit(bytes_before),
 289              proper_unit_for_byte_size(bytes_before),
 290              byte_size_in_proper_unit(bytes_after),
 291              proper_unit_for_byte_size(bytes_after),
 292              byte_size_in_proper_unit(capacity),
 293              proper_unit_for_byte_size(capacity));  
 294 }