1 /*
2  * Copyright (c) 2001, 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/parallel/mutableSpace.hpp"
27 #include "gc/shared/spaceDecorator.hpp"
28 #include "memory/iterator.inline.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "runtime/atomic.hpp"
31 #include "runtime/orderAccess.hpp"
32 #include "runtime/safepoint.hpp"
33 #include "runtime/thread.hpp"
34 #include "utilities/align.hpp"
35 #include "utilities/macros.hpp"
36 
MutableSpace(size_t alignment)37 MutableSpace::MutableSpace(size_t alignment): ImmutableSpace(), _top(NULL), _alignment(alignment) {
38   assert(MutableSpace::alignment() % os::vm_page_size() == 0,
39          "Space should be aligned");
40   _mangler = new MutableSpaceMangler(this);
41 }
42 
~MutableSpace()43 MutableSpace::~MutableSpace() {
44   delete _mangler;
45 }
46 
numa_setup_pages(MemRegion mr,bool clear_space)47 void MutableSpace::numa_setup_pages(MemRegion mr, bool clear_space) {
48   if (!mr.is_empty()) {
49     size_t page_size = UseLargePages ? alignment() : os::vm_page_size();
50     HeapWord *start = align_up(mr.start(), page_size);
51     HeapWord *end =   align_down(mr.end(), page_size);
52     if (end > start) {
53       size_t size = pointer_delta(end, start, sizeof(char));
54       if (clear_space) {
55         // Prefer page reallocation to migration.
56         os::free_memory((char*)start, size, page_size);
57       }
58       os::numa_make_global((char*)start, size);
59     }
60   }
61 }
62 
pretouch_pages(MemRegion mr)63 void MutableSpace::pretouch_pages(MemRegion mr) {
64   os::pretouch_memory(mr.start(), mr.end());
65 }
66 
initialize(MemRegion mr,bool clear_space,bool mangle_space,bool setup_pages)67 void MutableSpace::initialize(MemRegion mr,
68                               bool clear_space,
69                               bool mangle_space,
70                               bool setup_pages) {
71 
72   assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()),
73          "invalid space boundaries");
74 
75   if (setup_pages && (UseNUMA || AlwaysPreTouch)) {
76     // The space may move left and right or expand/shrink.
77     // We'd like to enforce the desired page placement.
78     MemRegion head, tail;
79     if (last_setup_region().is_empty()) {
80       // If it's the first initialization don't limit the amount of work.
81       head = mr;
82       tail = MemRegion(mr.end(), mr.end());
83     } else {
84       // Is there an intersection with the address space?
85       MemRegion intersection = last_setup_region().intersection(mr);
86       if (intersection.is_empty()) {
87         intersection = MemRegion(mr.end(), mr.end());
88       }
89       // All the sizes below are in words.
90       size_t head_size = 0, tail_size = 0;
91       if (mr.start() <= intersection.start()) {
92         head_size = pointer_delta(intersection.start(), mr.start());
93       }
94       if(intersection.end() <= mr.end()) {
95         tail_size = pointer_delta(mr.end(), intersection.end());
96       }
97       // Limit the amount of page manipulation if necessary.
98       if (NUMASpaceResizeRate > 0 && !AlwaysPreTouch) {
99         const size_t change_size = head_size + tail_size;
100         const float setup_rate_words = NUMASpaceResizeRate >> LogBytesPerWord;
101         head_size = MIN2((size_t)(setup_rate_words * head_size / change_size),
102                          head_size);
103         tail_size = MIN2((size_t)(setup_rate_words * tail_size / change_size),
104                          tail_size);
105       }
106       head = MemRegion(intersection.start() - head_size, intersection.start());
107       tail = MemRegion(intersection.end(), intersection.end() + tail_size);
108     }
109     assert(mr.contains(head) && mr.contains(tail), "Sanity");
110 
111     if (UseNUMA) {
112       numa_setup_pages(head, clear_space);
113       numa_setup_pages(tail, clear_space);
114     }
115 
116     if (AlwaysPreTouch) {
117       pretouch_pages(head);
118       pretouch_pages(tail);
119     }
120 
121     // Remember where we stopped so that we can continue later.
122     set_last_setup_region(MemRegion(head.start(), tail.end()));
123   }
124 
125   set_bottom(mr.start());
126   // When expanding concurrently with callers of cas_allocate, setting end
127   // makes the new space available for allocation by other threads.  So this
128   // assignment must follow all other configuration and initialization that
129   // might be done for expansion.
130   OrderAccess::release_store(end_addr(), mr.end());
131 
132   if (clear_space) {
133     clear(mangle_space);
134   }
135 }
136 
clear(bool mangle_space)137 void MutableSpace::clear(bool mangle_space) {
138   set_top(bottom());
139   if (ZapUnusedHeapArea && mangle_space) {
140     mangle_unused_area();
141   }
142 }
143 
144 #ifndef PRODUCT
check_mangled_unused_area(HeapWord * limit)145 void MutableSpace::check_mangled_unused_area(HeapWord* limit) {
146   mangler()->check_mangled_unused_area(limit);
147 }
148 
check_mangled_unused_area_complete()149 void MutableSpace::check_mangled_unused_area_complete() {
150   mangler()->check_mangled_unused_area_complete();
151 }
152 
153 // Mangle only the unused space that has not previously
154 // been mangled and that has not been allocated since being
155 // mangled.
mangle_unused_area()156 void MutableSpace::mangle_unused_area() {
157   mangler()->mangle_unused_area();
158 }
159 
mangle_unused_area_complete()160 void MutableSpace::mangle_unused_area_complete() {
161   mangler()->mangle_unused_area_complete();
162 }
163 
mangle_region(MemRegion mr)164 void MutableSpace::mangle_region(MemRegion mr) {
165   SpaceMangler::mangle_region(mr);
166 }
167 
set_top_for_allocations(HeapWord * v)168 void MutableSpace::set_top_for_allocations(HeapWord* v) {
169   mangler()->set_top_for_allocations(v);
170 }
171 
set_top_for_allocations()172 void MutableSpace::set_top_for_allocations() {
173   mangler()->set_top_for_allocations(top());
174 }
175 #endif
176 
177 // This version requires locking. */
allocate(size_t size)178 HeapWord* MutableSpace::allocate(size_t size) {
179   assert(Heap_lock->owned_by_self() ||
180          (SafepointSynchronize::is_at_safepoint() &&
181           Thread::current()->is_VM_thread()),
182          "not locked");
183   HeapWord* obj = top();
184   if (pointer_delta(end(), obj) >= size) {
185     HeapWord* new_top = obj + size;
186     set_top(new_top);
187     assert(is_object_aligned(obj) && is_object_aligned(new_top),
188            "checking alignment");
189     return obj;
190   } else {
191     return NULL;
192   }
193 }
194 
195 // This version is lock-free.
cas_allocate(size_t size)196 HeapWord* MutableSpace::cas_allocate(size_t size) {
197   do {
198     // Read top before end, else the range check may pass when it shouldn't.
199     // If end is read first, other threads may advance end and top such that
200     // current top > old end and current top + size > current end.  Then
201     // pointer_delta underflows, allowing installation of top > current end.
202     HeapWord* obj = OrderAccess::load_acquire(top_addr());
203     if (pointer_delta(end(), obj) >= size) {
204       HeapWord* new_top = obj + size;
205       HeapWord* result = Atomic::cmpxchg(new_top, top_addr(), obj);
206       // result can be one of two:
207       //  the old top value: the exchange succeeded
208       //  otherwise: the new value of the top is returned.
209       if (result != obj) {
210         continue; // another thread beat us to the allocation, try again
211       }
212       assert(is_object_aligned(obj) && is_object_aligned(new_top),
213              "checking alignment");
214       return obj;
215     } else {
216       return NULL;
217     }
218   } while (true);
219 }
220 
221 // Try to deallocate previous allocation. Returns true upon success.
cas_deallocate(HeapWord * obj,size_t size)222 bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {
223   HeapWord* expected_top = obj + size;
224   return Atomic::cmpxchg(obj, top_addr(), expected_top) == expected_top;
225 }
226 
oop_iterate(OopIterateClosure * cl)227 void MutableSpace::oop_iterate(OopIterateClosure* cl) {
228   HeapWord* obj_addr = bottom();
229   HeapWord* t = top();
230   // Could call objects iterate, but this is easier.
231   while (obj_addr < t) {
232     obj_addr += oop(obj_addr)->oop_iterate_size(cl);
233   }
234 }
235 
object_iterate(ObjectClosure * cl)236 void MutableSpace::object_iterate(ObjectClosure* cl) {
237   HeapWord* p = bottom();
238   while (p < top()) {
239     cl->do_object(oop(p));
240     p += oop(p)->size();
241   }
242 }
243 
print_short() const244 void MutableSpace::print_short() const { print_short_on(tty); }
print_short_on(outputStream * st) const245 void MutableSpace::print_short_on( outputStream* st) const {
246   st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K,
247             (int) ((double) used_in_bytes() * 100 / capacity_in_bytes()));
248 }
249 
print() const250 void MutableSpace::print() const { print_on(tty); }
print_on(outputStream * st) const251 void MutableSpace::print_on(outputStream* st) const {
252   MutableSpace::print_short_on(st);
253   st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")",
254                  p2i(bottom()), p2i(top()), p2i(end()));
255 }
256 
verify()257 void MutableSpace::verify() {
258   HeapWord* p = bottom();
259   HeapWord* t = top();
260   HeapWord* prev_p = NULL;
261   while (p < t) {
262     oop(p)->verify();
263     prev_p = p;
264     p += oop(p)->size();
265   }
266   guarantee(p == top(), "end of last object must match end of space");
267 }
268