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