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