1 /*
   2  * Copyright (c) 2016, 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 #include "precompiled.hpp"
  26 #include "gc/shared/preservedMarks.inline.hpp"
  27 #include "gc/shared/workgroup.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/resourceArea.hpp"
  30 
  31 #include "utilities/macros.hpp"
  32 #if INCLUDE_ALL_GCS
  33 #include "gc/parallel/gcTaskManager.hpp"
  34 #endif
  35 
  36 void PreservedMarks::restore() {
  37   while (!_stack.is_empty()) {
  38     const OopAndMarkOop elem = _stack.pop();
  39     elem.set_mark();
  40   }
  41   assert_empty();
  42 }
  43 
  44 #ifndef PRODUCT
  45 void PreservedMarks::assert_empty() {
  46   assert(_stack.is_empty(), "stack expected to be empty, size = "SIZE_FORMAT,
  47          _stack.size());
  48   assert(_stack.cache_size() == 0,
  49          "stack expected to have no cached segments, cache size = "SIZE_FORMAT,
  50          _stack.cache_size());
  51 }
  52 #endif // ndef PRODUCT
  53 
  54 void RemoveForwardedPointerClosure::do_object(oop obj) {
  55   if (obj->is_forwarded()) {
  56     PreservedMarks::init_forwarded_mark(obj);
  57   }
  58 }
  59 
  60 void PreservedMarksSet::init(uint num) {
  61   assert(_stacks == NULL && _num == 0, "do not re-initialize");
  62   assert(num > 0, "pre-condition");
  63   if (_in_c_heap) {
  64     _stacks = NEW_C_HEAP_ARRAY(Padded<PreservedMarks>, num, mtGC);
  65   } else {
  66     _stacks = NEW_RESOURCE_ARRAY(Padded<PreservedMarks>, num);
  67   }
  68   for (uint i = 0; i < num; i += 1) {
  69     ::new (_stacks + i) PreservedMarks();
  70   }
  71   _num = num;
  72 
  73   assert_empty();
  74 }
  75 
  76 class ParRestoreTask : public AbstractGangTask {
  77 private:
  78   PreservedMarksSet* const _preserved_marks_set;
  79   SequentialSubTasksDone _sub_tasks;
  80   volatile size_t* const _total_size_addr;
  81 
  82 public:
  83   virtual void work(uint worker_id) {
  84     uint task_id = 0;
  85     while (!_sub_tasks.is_task_claimed(/* reference */ task_id)) {
  86       PreservedMarks* const preserved_marks = _preserved_marks_set->get(task_id);
  87       const size_t size = preserved_marks->size();
  88       preserved_marks->restore();
  89       // Only do the atomic add if the size is > 0.
  90       if (size > 0) {
  91         Atomic::add(size, _total_size_addr);
  92       }
  93     }
  94     _sub_tasks.all_tasks_completed();
  95   }
  96 
  97   ParRestoreTask(uint worker_num,
  98                  PreservedMarksSet* preserved_marks_set,
  99                  volatile size_t* total_size_addr)
 100       : AbstractGangTask("Parallel Preserved Mark Restoration"),
 101         _preserved_marks_set(preserved_marks_set),
 102         _total_size_addr(total_size_addr) {
 103     _sub_tasks.set_n_threads(worker_num);
 104     _sub_tasks.set_n_tasks(preserved_marks_set->num());
 105   }
 106 };
 107 
 108 void PreservedMarksSet::restore_internal(WorkGang* workers,
 109                                          volatile size_t* total_size_addr) {
 110   assert(workers != NULL, "pre-condition");
 111   ParRestoreTask task(workers->active_workers(), this, total_size_addr);
 112   workers->run_task(&task);
 113 }
 114 
 115 #if INCLUDE_ALL_GCS
 116 class ParRestoreGCTask : public GCTask {
 117 private:
 118   const uint _id;
 119   PreservedMarksSet* const _preserved_marks_set;
 120   volatile size_t* const _total_size_addr;
 121 
 122 public:
 123   virtual char* name() { return (char*) "preserved mark restoration task"; }
 124 
 125   virtual void do_it(GCTaskManager* manager, uint which) {
 126     PreservedMarks* const preserved_marks = _preserved_marks_set->get(_id);
 127     const size_t size = preserved_marks->size();
 128     preserved_marks->restore();
 129     // Only do the atomic add if the size is > 0.
 130     if (size > 0) {
 131       Atomic::add(size, _total_size_addr);
 132     }
 133   }
 134 
 135   ParRestoreGCTask(uint id,
 136                    PreservedMarksSet* preserved_marks_set,
 137                    volatile size_t* total_size_addr)
 138     : _id(id),
 139       _preserved_marks_set(preserved_marks_set),
 140       _total_size_addr(total_size_addr) { }
 141 };
 142 
 143 void PreservedMarksSet::restore_internal(GCTaskManager* gc_task_manager,
 144                                          volatile size_t* total_size_addr) {
 145   // GCTask / GCTaskQueue are ResourceObjs
 146   ResourceMark rm;
 147 
 148   GCTaskQueue* q = GCTaskQueue::create();
 149   for (uint i = 0; i < num(); i += 1) {
 150     q->enqueue(new ParRestoreGCTask(i, this, total_size_addr));
 151   }
 152   gc_task_manager->execute_and_wait(q);
 153 }
 154 #endif
 155 
 156 void PreservedMarksSet::reclaim() {
 157   assert_empty();
 158 
 159   for (uint i = 0; i < _num; i += 1) {
 160     _stacks[i].~Padded<PreservedMarks>();
 161   }
 162 
 163   if (_in_c_heap) {
 164     FREE_C_HEAP_ARRAY(Padded<PreservedMarks>, _stacks);
 165   } else {
 166     // the array was resource-allocated, so nothing to do
 167   }
 168   _stacks = NULL;
 169   _num = 0;
 170 }
 171 
 172 #ifndef PRODUCT
 173 void PreservedMarksSet::assert_empty() {
 174   assert(_stacks != NULL && _num > 0, "should have been initialized");
 175   for (uint i = 0; i < _num; i += 1) {
 176     get(i)->assert_empty();
 177   }
 178 }
 179 #endif // ndef PRODUCT