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/workgroup.hpp"
 29 #include "memory/allocation.inline.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "runtime/atomic.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 void PreservedMarks::restore() {
 36   while (!_stack.is_empty()) {
 37     const OopAndMarkWord elem = _stack.pop();
 38     elem.set_mark();
 39   }
 40   assert_empty();
 41 }
 42 
 43 // TODO: This method is unused, except in the gunit test. Change the test
 44 // to exercise the updated method below instead, and remove this one.
 45 void PreservedMarks::adjust_during_full_gc() {
 46   StackIterator<OopAndMarkWord, mtGC> iter(_stack);
 47   while (!iter.is_empty()) {
 48     OopAndMarkWord* elem = iter.next_addr();
 49 
 50     oop obj = elem->get_oop();
 51     if (obj->is_forwarded()) {
 52       elem->set_oop(obj->forwardee());
 53     }
 54   }
 55 }
 56 
 57 void PreservedMarks::adjust_during_full_gc(const SlidingForwarding* const forwarding) {
 58   StackIterator<OopAndMarkWord, mtGC> iter(_stack);
 59   while (!iter.is_empty()) {
 60     OopAndMarkWord* elem = iter.next_addr();
 61 
 62     oop obj = elem->get_oop();
 63     if (obj->is_forwarded()) {
 64       elem->set_oop(forwarding->forwardee(obj));
 65     }
 66   }
 67 }
 68 
 69 void PreservedMarks::restore_and_increment(volatile size_t* const total_size_addr) {
 70   const size_t stack_size = size();
 71   restore();
 72   // Only do the atomic add if the size is > 0.
 73   if (stack_size > 0) {
 74     Atomic::add(total_size_addr, stack_size);
 75   }
 76 }
 77 
 78 #ifndef PRODUCT
 79 void PreservedMarks::assert_empty() {
 80   assert(_stack.is_empty(), "stack expected to be empty, size = " SIZE_FORMAT,
 81          _stack.size());
 82   assert(_stack.cache_size() == 0,
 83          "stack expected to have no cached segments, cache size = " SIZE_FORMAT,
 84          _stack.cache_size());
 85 }
 86 #endif // ndef PRODUCT
 87 
 88 void RemoveForwardedPointerClosure::do_object(oop obj) {
 89   if (obj->is_forwarded()) {
 90 #ifdef _LP64
 91     if (UseCompactObjectHeaders) {
 92       oop forwardee = obj->forwardee();
 93       markWord header = forwardee->mark();
 94       if (header.has_displaced_mark_helper()) {
 95         header = header.displaced_mark_helper();
 96       }
 97       assert(UseCompressedClassPointers, "assume +UseCompressedClassPointers");
 98       narrowKlass nklass = header.narrow_klass();
 99       obj->set_mark(markWord::prototype().set_narrow_klass(nklass));
100     } else
101 #endif
102     {
103       PreservedMarks::init_forwarded_mark(obj);
104     }
105   }
106 }
107 
108 void PreservedMarksSet::init(uint num) {
109   assert(_stacks == nullptr && _num == 0, "do not re-initialize");
110   assert(num > 0, "pre-condition");
111   if (_in_c_heap) {
112     _stacks = NEW_C_HEAP_ARRAY(Padded<PreservedMarks>, num, mtGC);
113   } else {
114     _stacks = NEW_RESOURCE_ARRAY(Padded<PreservedMarks>, num);
115   }
116   for (uint i = 0; i < num; i += 1) {
117     ::new (_stacks + i) PreservedMarks();
118   }
119   _num = num;
120 
121   assert_empty();
122 }
123 
124 class RestorePreservedMarksTask : public AbstractGangTask {
125   PreservedMarksSet* const _preserved_marks_set;
126   SequentialSubTasksDone _sub_tasks;
127   volatile size_t _total_size;
128 #ifdef ASSERT
129   size_t _total_size_before;
130 #endif // ASSERT
131 
132 public:
133   void work(uint worker_id) override {
134     uint task_id = 0;
135     while (_sub_tasks.try_claim_task(task_id)) {
136       _preserved_marks_set->get(task_id)->restore_and_increment(&_total_size);
137     }
138   }
139 
140   RestorePreservedMarksTask(PreservedMarksSet* preserved_marks_set)
141     : AbstractGangTask("Restore Preserved Marks"),
142       _preserved_marks_set(preserved_marks_set),
143       _sub_tasks(preserved_marks_set->num()),
144       _total_size(0)
145       DEBUG_ONLY(COMMA _total_size_before(0)) {
146 #ifdef ASSERT
147     // This is to make sure the total_size we'll calculate below is correct.
148     for (uint i = 0; i < _preserved_marks_set->num(); ++i) {
149       _total_size_before += _preserved_marks_set->get(i)->size();
150     }
151 #endif // ASSERT
152   }
153 
154   ~RestorePreservedMarksTask() {
155     assert(_total_size == _total_size_before, "total_size = %zu before = %zu", _total_size, _total_size_before);
156 
157     log_trace(gc)("Restored %zu marks", _total_size);
158   }
159 };
160 
161 void PreservedMarksSet::restore(WorkGang* workers) {
162   {
163     RestorePreservedMarksTask cl(this);
164     if (workers == nullptr) {
165       cl.work(0);
166     } else {
167       workers->run_task(&cl);
168     }
169   }
170 
171   assert_empty();
172 }
173 
174 AbstractGangTask* PreservedMarksSet::create_task() {
175   return new RestorePreservedMarksTask(this);
176 }
177 
178 void PreservedMarksSet::reclaim() {
179   assert_empty();
180 
181   for (uint i = 0; i < _num; i += 1) {
182     _stacks[i].~Padded<PreservedMarks>();
183   }
184 
185   if (_in_c_heap) {
186     FREE_C_HEAP_ARRAY(Padded<PreservedMarks>, _stacks);
187   } else {
188     // the array was resource-allocated, so nothing to do
189   }
190   _stacks = nullptr;
191   _num = 0;
192 }
193 
194 #ifndef PRODUCT
195 void PreservedMarksSet::assert_empty() {
196   assert(_stacks != nullptr && _num > 0, "should have been initialized");
197   for (uint i = 0; i < _num; i += 1) {
198     get(i)->assert_empty();
199   }
200 }
201 #endif // ndef PRODUCT