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