sharedHeap.cpp 9.5 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36
#include "precompiled.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "code/codeCache.hpp"
#include "gc_interface/collectedHeap.inline.hpp"
#include "memory/sharedHeap.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/fprofiler.hpp"
#include "runtime/java.hpp"
#include "services/management.hpp"
#include "utilities/copy.hpp"
#include "utilities/workgroup.hpp"
D
duke 已提交
37

38 39
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC

D
duke 已提交
40 41 42 43 44 45 46 47 48 49
SharedHeap* SharedHeap::_sh;

// The set of potentially parallel tasks in strong root scanning.
enum SH_process_strong_roots_tasks {
  SH_PS_Universe_oops_do,
  SH_PS_JNIHandles_oops_do,
  SH_PS_ObjectSynchronizer_oops_do,
  SH_PS_FlatProfiler_oops_do,
  SH_PS_Management_oops_do,
  SH_PS_SystemDictionary_oops_do,
50
  SH_PS_ClassLoaderDataGraph_oops_do,
D
duke 已提交
51 52 53 54 55 56 57 58 59
  SH_PS_jvmti_oops_do,
  SH_PS_CodeCache_oops_do,
  // Leave this one last.
  SH_PS_NumElements
};

SharedHeap::SharedHeap(CollectorPolicy* policy_) :
  CollectedHeap(),
  _collector_policy(policy_),
60
  _rem_set(NULL),
D
duke 已提交
61 62
  _strong_roots_parity(0),
  _process_strong_tasks(new SubTasksDone(SH_PS_NumElements)),
63
  _workers(NULL)
D
duke 已提交
64 65 66 67 68 69
{
  if (_process_strong_tasks == NULL || !_process_strong_tasks->valid()) {
    vm_exit_during_initialization("Failed necessary allocation.");
  }
  _sh = this;  // ch is static, should be set only once.
  if ((UseParNewGC ||
J
jmasa 已提交
70 71
      (UseConcMarkSweepGC && (CMSParallelInitialMarkEnabled ||
                              CMSParallelRemarkEnabled)) ||
72
       UseG1GC) &&
D
duke 已提交
73
      ParallelGCThreads > 0) {
74
    _workers = new FlexibleWorkGang("Parallel GC Threads", ParallelGCThreads,
75 76
                            /* are_GC_task_threads */true,
                            /* are_ConcurrentGC_threads */false);
D
duke 已提交
77 78
    if (_workers == NULL) {
      vm_exit_during_initialization("Failed necessary allocation.");
79 80
    } else {
      _workers->initialize_workers();
D
duke 已提交
81 82 83 84
    }
  }
}

85 86 87 88 89 90 91 92
int SharedHeap::n_termination() {
  return _process_strong_tasks->n_threads();
}

void SharedHeap::set_n_termination(int t) {
  _process_strong_tasks->set_n_threads(t);
}

93 94 95 96 97 98
bool SharedHeap::heap_lock_held_for_gc() {
  Thread* t = Thread::current();
  return    Heap_lock->owned_by_self()
         || (   (t->is_GC_task_thread() ||  t->is_VM_thread())
             && _thread_holds_heap_lock_for_gc);
}
D
duke 已提交
99

100
void SharedHeap::set_par_threads(uint t) {
101
  assert(t == 0 || !UseSerialGC, "Cannot have parallel threads");
D
duke 已提交
102
  _n_par_threads = t;
103
  _process_strong_tasks->set_n_threads(t);
D
duke 已提交
104 105
}

106 107 108 109 110 111 112 113 114 115 116
#ifdef ASSERT
class AssertNonScavengableClosure: public OopClosure {
public:
  virtual void do_oop(oop* p) {
    assert(!Universe::heap()->is_in_partial_collection(*p),
      "Referent should not be scavengable.");  }
  virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
};
static AssertNonScavengableClosure assert_is_non_scavengable_closure;
#endif

D
duke 已提交
117 118 119 120 121 122 123 124 125 126
void SharedHeap::change_strong_roots_parity() {
  // Also set the new collection parity.
  assert(_strong_roots_parity >= 0 && _strong_roots_parity <= 2,
         "Not in range.");
  _strong_roots_parity++;
  if (_strong_roots_parity == 3) _strong_roots_parity = 1;
  assert(_strong_roots_parity >= 1 && _strong_roots_parity <= 2,
         "Not in range.");
}

127 128 129 130 131
SharedHeap::StrongRootsScope::StrongRootsScope(SharedHeap* outer, bool activate)
  : MarkScope(activate)
{
  if (_active) {
    outer->change_strong_roots_parity();
132 133
    // Zero the claimed high water mark in the StringTable
    StringTable::clear_parallel_claimed_index();
134 135 136 137 138 139 140 141
  }
}

SharedHeap::StrongRootsScope::~StrongRootsScope() {
  // nothing particular
}

void SharedHeap::process_strong_roots(bool activate_scope,
D
duke 已提交
142 143
                                      ScanningOption so,
                                      OopClosure* roots,
144
                                      KlassClosure* klass_closure) {
145
  StrongRootsScope srs(this, activate_scope);
146

D
duke 已提交
147
  // General strong roots.
148
  assert(_strong_roots_parity != 0, "must have called prologue code");
149 150 151 152
  // _n_termination for _process_strong_tasks should be set up stream
  // in a method not running in a GC worker.  Otherwise the GC worker
  // could be trying to change the termination condition while the task
  // is executing in another GC worker.
D
duke 已提交
153 154 155 156 157 158
  if (!_process_strong_tasks->is_task_claimed(SH_PS_Universe_oops_do)) {
    Universe::oops_do(roots);
  }
  // Global (strong) JNI handles
  if (!_process_strong_tasks->is_task_claimed(SH_PS_JNIHandles_oops_do))
    JNIHandles::oops_do(roots);
159

160 161
  CodeBlobToOopClosure code_roots(roots, true);

162
  CLDToOopClosure roots_from_clds(roots);
163 164 165 166
  // If we limit class scanning to SO_SystemClasses we need to apply a CLD closure to
  // CLDs which are strongly reachable from the thread stacks.
  CLDToOopClosure* roots_from_clds_p = ((so & SO_SystemClasses) ? &roots_from_clds : NULL);
  // All threads execute this; the individual threads are task groups.
167
  if (CollectedHeap::use_parallel_gc_threads()) {
168
    Threads::possibly_parallel_oops_do(roots, roots_from_clds_p, &code_roots);
D
duke 已提交
169
  } else {
170
    Threads::oops_do(roots, roots_from_clds_p, &code_roots);
D
duke 已提交
171
  }
172

D
duke 已提交
173 174 175 176 177 178 179 180 181 182 183 184
  if (!_process_strong_tasks-> is_task_claimed(SH_PS_ObjectSynchronizer_oops_do))
    ObjectSynchronizer::oops_do(roots);
  if (!_process_strong_tasks->is_task_claimed(SH_PS_FlatProfiler_oops_do))
    FlatProfiler::oops_do(roots);
  if (!_process_strong_tasks->is_task_claimed(SH_PS_Management_oops_do))
    Management::oops_do(roots);
  if (!_process_strong_tasks->is_task_claimed(SH_PS_jvmti_oops_do))
    JvmtiExport::oops_do(roots);

  if (!_process_strong_tasks->is_task_claimed(SH_PS_SystemDictionary_oops_do)) {
    if (so & SO_AllClasses) {
      SystemDictionary::oops_do(roots);
185 186
    } else if (so & SO_SystemClasses) {
      SystemDictionary::always_strong_oops_do(roots);
187
    } else {
188
      fatal("We should always have selected either SO_AllClasses or SO_SystemClasses");
189
    }
D
duke 已提交
190 191
  }

192 193
  if (!_process_strong_tasks->is_task_claimed(SH_PS_ClassLoaderDataGraph_oops_do)) {
    if (so & SO_AllClasses) {
194
      ClassLoaderDataGraph::oops_do(roots, klass_closure, /* must_claim */ false);
195
    } else if (so & SO_SystemClasses) {
196
      ClassLoaderDataGraph::always_strong_oops_do(roots, klass_closure, /* must_claim */ true);
197 198 199
    }
  }

200 201 202 203 204 205
  // All threads execute the following. A specific chunk of buckets
  // from the StringTable are the individual tasks.
  if (so & SO_Strings) {
    if (CollectedHeap::use_parallel_gc_threads()) {
      StringTable::possibly_parallel_oops_do(roots);
    } else {
206 207
      StringTable::oops_do(roots);
    }
D
duke 已提交
208 209 210
  }

  if (!_process_strong_tasks->is_task_claimed(SH_PS_CodeCache_oops_do)) {
211
    if (so & SO_ScavengeCodeCache) {
212
      assert(&code_roots != NULL, "must supply closure for code cache");
213 214

      // We only visit parts of the CodeCache when scavenging.
215
      CodeCache::scavenge_root_nmethods_do(&code_roots);
216 217
    }
    if (so & SO_AllCodeCache) {
218
      assert(&code_roots != NULL, "must supply closure for code cache");
219

220 221
      // CMSCollector uses this to do intermediate-strength collections.
      // We scan the entire code cache, since CodeCache::do_unloading is not called.
222
      CodeCache::blobs_do(&code_roots);
223
    }
224 225 226 227
    // Verify that the code cache contents are not subject to
    // movement by a scavenging collection.
    DEBUG_ONLY(CodeBlobToOopClosure assert_code_is_non_scavengable(&assert_is_non_scavengable_closure, /*do_marking=*/ false));
    DEBUG_ONLY(CodeCache::asserted_non_scavengable_nmethods_do(&assert_code_is_non_scavengable));
D
duke 已提交
228 229 230 231 232 233 234 235 236 237 238
  }

  _process_strong_tasks->all_tasks_completed();
}

class AlwaysTrueClosure: public BoolObjectClosure {
public:
  bool do_object_b(oop p) { return true; }
};
static AlwaysTrueClosure always_true;

239
void SharedHeap::process_weak_roots(OopClosure* root_closure) {
D
duke 已提交
240 241
  // Global (weak) JNI handles
  JNIHandles::weak_oops_do(&always_true, root_closure);
242
}
D
duke 已提交
243 244 245 246 247 248 249 250

void SharedHeap::set_barrier_set(BarrierSet* bs) {
  _barrier_set = bs;
  // Cached barrier set for fast access in oops
  oopDesc::set_bs(bs);
}

void SharedHeap::post_initialize() {
251
  CollectedHeap::post_initialize();
D
duke 已提交
252 253 254
  ref_processing_init();
}

255
void SharedHeap::ref_processing_init() {}
D
duke 已提交
256 257

// Some utilities.
258 259
void SharedHeap::print_size_transition(outputStream* out,
                                       size_t bytes_before,
D
duke 已提交
260 261
                                       size_t bytes_after,
                                       size_t capacity) {
262
  out->print(" %d%s->%d%s(%d%s)",
D
duke 已提交
263 264 265 266 267 268 269
             byte_size_in_proper_unit(bytes_before),
             proper_unit_for_byte_size(bytes_before),
             byte_size_in_proper_unit(bytes_after),
             proper_unit_for_byte_size(bytes_after),
             byte_size_in_proper_unit(capacity),
             proper_unit_for_byte_size(capacity));
}