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