1 /*
  2  * Copyright (c) 2016, 2021, 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/slidingForwarding.inline.hpp"
 28 #include "gc/shared/workerThread.hpp"
 29 #include "gc/shared/workerUtils.hpp"
 30 #include "memory/allocation.inline.hpp"
 31 #include "memory/resourceArea.hpp"
 32 #include "oops/oop.inline.hpp"
 33 #include "runtime/atomic.hpp"
 34 #include "utilities/macros.hpp"
 35 
 36 void PreservedMarks::restore() {
 37   while (!_stack.is_empty()) {
 38     const PreservedMark elem = _stack.pop();
 39     elem.set_mark();
 40   }
 41   assert_empty();
 42 }
 43 
 44 void PreservedMarks::adjust_preserved_mark(PreservedMark* elem) {
 45   oop obj = elem->get_oop();
 46   if (SlidingForwarding::is_forwarded(obj)) {
 47     elem->set_oop(SlidingForwarding::forwardee(obj));
 48   }
 49 }
 50 
 51 void PreservedMarks::adjust_during_full_gc() {
 52   StackIterator<PreservedMark, mtGC> iter(_stack);
 53   while (!iter.is_empty()) {
 54     PreservedMark* elem = iter.next_addr();
 55     adjust_preserved_mark(elem);
 56   }
 57 }
 58 
 59 void PreservedMarks::restore_and_increment(volatile size_t* const total_size_addr) {
 60   const size_t stack_size = size();
 61   restore();
 62   // Only do the atomic add if the size is > 0.
 63   if (stack_size > 0) {
 64     Atomic::add(total_size_addr, stack_size);
 65   }
 66 }
 67 
 68 #ifndef PRODUCT
 69 void PreservedMarks::assert_empty() {
 70   assert(_stack.is_empty(), "stack expected to be empty, size = " SIZE_FORMAT,
 71          _stack.size());
 72   assert(_stack.cache_size() == 0,
 73          "stack expected to have no cached segments, cache size = " SIZE_FORMAT,
 74          _stack.cache_size());
 75 }
 76 #endif // ndef PRODUCT
 77 
 78 void PreservedMarksSet::init(uint num) {
 79   assert(_stacks == nullptr && _num == 0, "do not re-initialize");
 80   assert(num > 0, "pre-condition");
 81   if (_in_c_heap) {
 82     _stacks = NEW_C_HEAP_ARRAY(Padded<PreservedMarks>, num, mtGC);
 83   } else {
 84     _stacks = NEW_RESOURCE_ARRAY(Padded<PreservedMarks>, num);
 85   }
 86   for (uint i = 0; i < num; i += 1) {
 87     ::new (_stacks + i) PreservedMarks();
 88   }
 89   _num = num;
 90 
 91   assert_empty();
 92 }
 93 
 94 class RestorePreservedMarksTask : public WorkerTask {
 95   PreservedMarksSet* const _preserved_marks_set;
 96   SequentialSubTasksDone _sub_tasks;
 97   volatile size_t _total_size;
 98 #ifdef ASSERT
 99   size_t _total_size_before;
100 #endif // ASSERT
101 
102 public:
103   void work(uint worker_id) override {
104     uint task_id = 0;
105     while (_sub_tasks.try_claim_task(task_id)) {
106       _preserved_marks_set->get(task_id)->restore_and_increment(&_total_size);
107     }
108   }
109 
110   RestorePreservedMarksTask(PreservedMarksSet* preserved_marks_set)
111     : WorkerTask("Restore Preserved Marks"),
112       _preserved_marks_set(preserved_marks_set),
113       _sub_tasks(preserved_marks_set->num()),
114       _total_size(0)
115       DEBUG_ONLY(COMMA _total_size_before(0)) {
116 #ifdef ASSERT
117     // This is to make sure the total_size we'll calculate below is correct.
118     for (uint i = 0; i < _preserved_marks_set->num(); ++i) {
119       _total_size_before += _preserved_marks_set->get(i)->size();
120     }
121 #endif // ASSERT
122   }
123 
124   ~RestorePreservedMarksTask() {
125     assert(_total_size == _total_size_before, "total_size = %zu before = %zu", _total_size, _total_size_before);
126     size_t mem_size = _total_size * (sizeof(oop) + sizeof(markWord));
127     log_trace(gc)("Restored %zu marks, occupying %zu %s", _total_size,
128                                                           byte_size_in_proper_unit(mem_size),
129                                                           proper_unit_for_byte_size(mem_size));
130   }
131 };
132 
133 void PreservedMarksSet::restore(WorkerThreads* workers) {
134   {
135     RestorePreservedMarksTask cl(this);
136     if (workers == nullptr) {
137       cl.work(0);
138     } else {
139       workers->run_task(&cl);
140     }
141   }
142 
143   assert_empty();
144 }
145 
146 WorkerTask* PreservedMarksSet::create_task() {
147   return new RestorePreservedMarksTask(this);
148 }
149 
150 void PreservedMarksSet::reclaim() {
151   assert_empty();
152 
153   for (uint i = 0; i < _num; i += 1) {
154     _stacks[i].~Padded<PreservedMarks>();
155   }
156 
157   if (_in_c_heap) {
158     FREE_C_HEAP_ARRAY(Padded<PreservedMarks>, _stacks);
159   } else {
160     // the array was resource-allocated, so nothing to do
161   }
162   _stacks = nullptr;
163   _num = 0;
164 }
165 
166 #ifndef PRODUCT
167 void PreservedMarksSet::assert_empty() {
168   assert(_stacks != nullptr && _num > 0, "should have been initialized");
169   for (uint i = 0; i < _num; i += 1) {
170     get(i)->assert_empty();
171   }
172 }
173 #endif // ndef PRODUCT