psCompactionManager.cpp 7.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2010, 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
 *
 */

#include "incls/_precompiled.incl"
#include "incls/_psCompactionManager.cpp.incl"

PSOldGen*            ParCompactionManager::_old_gen = NULL;
ParCompactionManager**  ParCompactionManager::_manager_array = NULL;
OopTaskQueueSet*     ParCompactionManager::_stack_array = NULL;
31 32
ParCompactionManager::ObjArrayTaskQueueSet*
  ParCompactionManager::_objarray_queues = NULL;
D
duke 已提交
33 34
ObjectStartArray*    ParCompactionManager::_start_array = NULL;
ParMarkBitMap*       ParCompactionManager::_mark_bitmap = NULL;
35
RegionTaskQueueSet*  ParCompactionManager::_region_array = NULL;
D
duke 已提交
36 37 38 39 40 41 42 43 44 45 46

ParCompactionManager::ParCompactionManager() :
    _action(CopyAndUpdate) {

  ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");

  _old_gen = heap->old_gen();
  _start_array = old_gen()->start_array();

  marking_stack()->initialize();
47
  _objarray_stack.initialize();
48
  region_stack()->initialize();
D
duke 已提交
49 50 51 52 53 54

  // Note that _revisit_klass_stack is allocated out of the
  // C heap (as opposed to out of ResourceArena).
  int size =
    (SystemDictionary::number_of_classes() * 2) * 2 / ParallelGCThreads;
  _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray<Klass*>(size, true);
Y
ysr 已提交
55 56 57
  // From some experiments (#klass/k)^2 for k = 10 seems a better fit, but this will
  // have to do for now until we are able to investigate a more optimal setting.
  _revisit_mdo_stack = new (ResourceObj::C_HEAP) GrowableArray<DataLayout*>(size*2, true);
D
duke 已提交
58 59 60 61
}

ParCompactionManager::~ParCompactionManager() {
  delete _revisit_klass_stack;
Y
ysr 已提交
62
  delete _revisit_mdo_stack;
D
duke 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  // _manager_array and _stack_array are statics
  // shared with all instances of ParCompactionManager
  // should not be deallocated.
}

void ParCompactionManager::initialize(ParMarkBitMap* mbm) {
  assert(PSParallelCompact::gc_task_manager() != NULL,
    "Needed for initialization");

  _mark_bitmap = mbm;

  uint parallel_gc_threads = PSParallelCompact::gc_task_manager()->workers();

  assert(_manager_array == NULL, "Attempt to initialize twice");
  _manager_array = NEW_C_HEAP_ARRAY(ParCompactionManager*, parallel_gc_threads+1 );
78
  guarantee(_manager_array != NULL, "Could not allocate manager_array");
D
duke 已提交
79 80

  _stack_array = new OopTaskQueueSet(parallel_gc_threads);
81 82 83
  guarantee(_stack_array != NULL, "Could not allocate stack_array");
  _objarray_queues = new ObjArrayTaskQueueSet(parallel_gc_threads);
  guarantee(_objarray_queues != NULL, "Could not allocate objarray_queues");
84
  _region_array = new RegionTaskQueueSet(parallel_gc_threads);
85
  guarantee(_region_array != NULL, "Could not allocate region_array");
D
duke 已提交
86 87 88 89 90 91

  // Create and register the ParCompactionManager(s) for the worker threads.
  for(uint i=0; i<parallel_gc_threads; i++) {
    _manager_array[i] = new ParCompactionManager();
    guarantee(_manager_array[i] != NULL, "Could not create ParCompactionManager");
    stack_array()->register_queue(i, _manager_array[i]->marking_stack());
92
    _objarray_queues->register_queue(i, &_manager_array[i]->_objarray_stack);
93
    region_array()->register_queue(i, _manager_array[i]->region_stack());
D
duke 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  }

  // The VMThread gets its own ParCompactionManager, which is not available
  // for work stealing.
  _manager_array[parallel_gc_threads] = new ParCompactionManager();
  guarantee(_manager_array[parallel_gc_threads] != NULL,
    "Could not create ParCompactionManager");
  assert(PSParallelCompact::gc_task_manager()->workers() != 0,
    "Not initialized?");
}

bool ParCompactionManager::should_update() {
  assert(action() != NotValid, "Action is not set");
  return (action() == ParCompactionManager::Update) ||
         (action() == ParCompactionManager::CopyAndUpdate) ||
         (action() == ParCompactionManager::UpdateAndCopy);
}

bool ParCompactionManager::should_copy() {
  assert(action() != NotValid, "Action is not set");
  return (action() == ParCompactionManager::Copy) ||
         (action() == ParCompactionManager::CopyAndUpdate) ||
         (action() == ParCompactionManager::UpdateAndCopy);
}

bool ParCompactionManager::should_verify_only() {
  assert(action() != NotValid, "Action is not set");
  return action() == ParCompactionManager::VerifyUpdate;
}

bool ParCompactionManager::should_reset_only() {
  assert(action() != NotValid, "Action is not set");
  return action() == ParCompactionManager::ResetObjects;
}

ParCompactionManager*
ParCompactionManager::gc_thread_compaction_manager(int index) {
  assert(index >= 0 && index < (int)ParallelGCThreads, "index out of range");
  assert(_manager_array != NULL, "Sanity");
  return _manager_array[index];
}

void ParCompactionManager::reset() {
  for(uint i=0; i<ParallelGCThreads+1; i++) {
    manager_array(i)->revisit_klass_stack()->clear();
Y
ysr 已提交
139
    manager_array(i)->revisit_mdo_stack()->clear();
D
duke 已提交
140 141 142
  }
}

143
void ParCompactionManager::follow_marking_stacks() {
D
duke 已提交
144
  do {
145
    // Drain the overflow stack first, to allow stealing from the marking stack.
146
    oop obj;
147 148
    while (marking_stack()->pop_overflow(obj)) {
      obj->follow_contents(this);
D
duke 已提交
149 150 151 152 153
    }
    while (marking_stack()->pop_local(obj)) {
      obj->follow_contents(this);
    }

154
    // Process ObjArrays one at a time to avoid marking stack bloat.
155
    ObjArrayTask task;
156
    if (_objarray_stack.pop_overflow(task)) {
157 158
      objArrayKlass* const k = (objArrayKlass*)task.obj()->blueprint();
      k->oop_follow_contents(this, task.obj(), task.index());
159
    } else if (_objarray_stack.pop_local(task)) {
160 161 162 163 164 165
      objArrayKlass* const k = (objArrayKlass*)task.obj()->blueprint();
      k->oop_follow_contents(this, task.obj(), task.index());
    }
  } while (!marking_stacks_empty());

  assert(marking_stacks_empty(), "Sanity");
D
duke 已提交
166 167
}

168
void ParCompactionManager::drain_region_stacks() {
D
duke 已提交
169
  do {
170 171 172
    // Drain overflow stack first so other threads can steal.
    size_t region_index;
    while (region_stack()->pop_overflow(region_index)) {
173
      PSParallelCompact::fill_and_update_region(this, region_index);
D
duke 已提交
174 175
    }

176 177
    while (region_stack()->pop_local(region_index)) {
      PSParallelCompact::fill_and_update_region(this, region_index);
D
duke 已提交
178
    }
179
  } while (!region_stack()->is_empty());
D
duke 已提交
180 181 182 183
}

#ifdef ASSERT
bool ParCompactionManager::stacks_have_been_allocated() {
Y
ysr 已提交
184 185
  return (revisit_klass_stack()->data_addr() != NULL &&
          revisit_mdo_stack()->data_addr() != NULL);
D
duke 已提交
186 187
}
#endif