mutableSpace.cpp revision 11857:d0fbf661cc16
1254721Semaste/*
2254721Semaste * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
3254721Semaste * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254721Semaste *
5254721Semaste * This code is free software; you can redistribute it and/or modify it
6254721Semaste * under the terms of the GNU General Public License version 2 only, as
7254721Semaste * published by the Free Software Foundation.
8254721Semaste *
9254721Semaste * This code is distributed in the hope that it will be useful, but WITHOUT
10254721Semaste * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11254721Semaste * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12254721Semaste * version 2 for more details (a copy is included in the LICENSE file that
13296417Sdim * accompanied this code).
14296417Sdim *
15296417Sdim * You should have received a copy of the GNU General Public License version
16296417Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17254721Semaste * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18288943Sdim *
19314564Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20254721Semaste * or visit www.oracle.com if you need additional information or have any
21314564Sdim * questions.
22254721Semaste *
23254721Semaste */
24288943Sdim
25254721Semaste#include "precompiled.hpp"
26254721Semaste#include "gc/parallel/mutableSpace.hpp"
27254721Semaste#include "gc/shared/spaceDecorator.hpp"
28254721Semaste#include "oops/oop.inline.hpp"
29254721Semaste#include "runtime/atomic.hpp"
30254721Semaste#include "runtime/safepoint.hpp"
31254721Semaste#include "runtime/thread.hpp"
32280031Sdim#include "utilities/macros.hpp"
33280031Sdim
34314564SdimMutableSpace::MutableSpace(size_t alignment): ImmutableSpace(), _top(NULL), _alignment(alignment) {
35254721Semaste  assert(MutableSpace::alignment() % os::vm_page_size() == 0,
36314564Sdim         "Space should be aligned");
37314564Sdim  _mangler = new MutableSpaceMangler(this);
38314564Sdim}
39314564Sdim
40314564SdimMutableSpace::~MutableSpace() {
41314564Sdim  delete _mangler;
42314564Sdim}
43314564Sdim
44314564Sdimvoid MutableSpace::numa_setup_pages(MemRegion mr, bool clear_space) {
45314564Sdim  if (!mr.is_empty()) {
46254721Semaste    size_t page_size = UseLargePages ? alignment() : os::vm_page_size();
47314564Sdim    HeapWord *start = (HeapWord*)round_to((intptr_t) mr.start(), page_size);
48314564Sdim    HeapWord *end =  (HeapWord*)round_down((intptr_t) mr.end(), page_size);
49314564Sdim    if (end > start) {
50314564Sdim      size_t size = pointer_delta(end, start, sizeof(char));
51254721Semaste      if (clear_space) {
52314564Sdim        // Prefer page reallocation to migration.
53254721Semaste        os::free_memory((char*)start, size, page_size);
54314564Sdim      }
55254721Semaste      os::numa_make_global((char*)start, size);
56314564Sdim    }
57314564Sdim  }
58314564Sdim}
59314564Sdim
60296417Sdimvoid MutableSpace::pretouch_pages(MemRegion mr) {
61314564Sdim  os::pretouch_memory(mr.start(), mr.end());
62288943Sdim}
63314564Sdim
64288943Sdimvoid MutableSpace::initialize(MemRegion mr,
65314564Sdim                              bool clear_space,
66288943Sdim                              bool mangle_space,
67314564Sdim                              bool setup_pages) {
68288943Sdim
69314564Sdim  assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()),
70254721Semaste         "invalid space boundaries");
71314564Sdim
72314564Sdim  if (setup_pages && (UseNUMA || AlwaysPreTouch)) {
73254721Semaste    // The space may move left and right or expand/shrink.
74314564Sdim    // We'd like to enforce the desired page placement.
75280031Sdim    MemRegion head, tail;
76314564Sdim    if (last_setup_region().is_empty()) {
77296417Sdim      // If it's the first initialization don't limit the amount of work.
78314564Sdim      head = mr;
79314564Sdim      tail = MemRegion(mr.end(), mr.end());
80314564Sdim    } else {
81314564Sdim      // Is there an intersection with the address space?
82314564Sdim      MemRegion intersection = last_setup_region().intersection(mr);
83314564Sdim      if (intersection.is_empty()) {
84314564Sdim        intersection = MemRegion(mr.end(), mr.end());
85314564Sdim      }
86314564Sdim      // All the sizes below are in words.
87314564Sdim      size_t head_size = 0, tail_size = 0;
88296417Sdim      if (mr.start() <= intersection.start()) {
89314564Sdim        head_size = pointer_delta(intersection.start(), mr.start());
90314564Sdim      }
91314564Sdim      if(intersection.end() <= mr.end()) {
92314564Sdim        tail_size = pointer_delta(mr.end(), intersection.end());
93314564Sdim      }
94314564Sdim      // Limit the amount of page manipulation if necessary.
95314564Sdim      if (NUMASpaceResizeRate > 0 && !AlwaysPreTouch) {
96314564Sdim        const size_t change_size = head_size + tail_size;
97314564Sdim        const float setup_rate_words = NUMASpaceResizeRate >> LogBytesPerWord;
98314564Sdim        head_size = MIN2((size_t)(setup_rate_words * head_size / change_size),
99314564Sdim                         head_size);
100314564Sdim        tail_size = MIN2((size_t)(setup_rate_words * tail_size / change_size),
101314564Sdim                         tail_size);
102314564Sdim      }
103314564Sdim      head = MemRegion(intersection.start() - head_size, intersection.start());
104254721Semaste      tail = MemRegion(intersection.end(), intersection.end() + tail_size);
105314564Sdim    }
106314564Sdim    assert(mr.contains(head) && mr.contains(tail), "Sanity");
107314564Sdim
108314564Sdim    if (UseNUMA) {
109314564Sdim      numa_setup_pages(head, clear_space);
110314564Sdim      numa_setup_pages(tail, clear_space);
111314564Sdim    }
112314564Sdim
113314564Sdim    if (AlwaysPreTouch) {
114314564Sdim      pretouch_pages(head);
115314564Sdim      pretouch_pages(tail);
116314564Sdim    }
117314564Sdim
118314564Sdim    // Remember where we stopped so that we can continue later.
119314564Sdim    set_last_setup_region(MemRegion(head.start(), tail.end()));
120314564Sdim  }
121314564Sdim
122314564Sdim  set_bottom(mr.start());
123314564Sdim  set_end(mr.end());
124314564Sdim
125314564Sdim  if (clear_space) {
126314564Sdim    clear(mangle_space);
127314564Sdim  }
128314564Sdim}
129314564Sdim
130314564Sdimvoid MutableSpace::clear(bool mangle_space) {
131314564Sdim  set_top(bottom());
132314564Sdim  if (ZapUnusedHeapArea && mangle_space) {
133314564Sdim    mangle_unused_area();
134314564Sdim  }
135314564Sdim}
136314564Sdim
137314564Sdim#ifndef PRODUCT
138314564Sdimvoid MutableSpace::check_mangled_unused_area(HeapWord* limit) {
139314564Sdim  mangler()->check_mangled_unused_area(limit);
140314564Sdim}
141314564Sdim
142314564Sdimvoid MutableSpace::check_mangled_unused_area_complete() {
143314564Sdim  mangler()->check_mangled_unused_area_complete();
144314564Sdim}
145314564Sdim
146314564Sdim// Mangle only the unused space that has not previously
147314564Sdim// been mangled and that has not been allocated since being
148314564Sdim// mangled.
149314564Sdimvoid MutableSpace::mangle_unused_area() {
150314564Sdim  mangler()->mangle_unused_area();
151314564Sdim}
152314564Sdim
153314564Sdimvoid MutableSpace::mangle_unused_area_complete() {
154314564Sdim  mangler()->mangle_unused_area_complete();
155314564Sdim}
156314564Sdim
157314564Sdimvoid MutableSpace::mangle_region(MemRegion mr) {
158314564Sdim  SpaceMangler::mangle_region(mr);
159314564Sdim}
160314564Sdim
161314564Sdimvoid MutableSpace::set_top_for_allocations(HeapWord* v) {
162314564Sdim  mangler()->set_top_for_allocations(v);
163314564Sdim}
164254721Semaste
165314564Sdimvoid MutableSpace::set_top_for_allocations() {
166314564Sdim  mangler()->set_top_for_allocations(top());
167314564Sdim}
168314564Sdim#endif
169314564Sdim
170314564Sdim// This version requires locking. */
171314564SdimHeapWord* MutableSpace::allocate(size_t size) {
172314564Sdim  assert(Heap_lock->owned_by_self() ||
173314564Sdim         (SafepointSynchronize::is_at_safepoint() &&
174314564Sdim          Thread::current()->is_VM_thread()),
175314564Sdim         "not locked");
176314564Sdim  HeapWord* obj = top();
177314564Sdim  if (pointer_delta(end(), obj) >= size) {
178314564Sdim    HeapWord* new_top = obj + size;
179314564Sdim    set_top(new_top);
180314564Sdim    assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
181314564Sdim           "checking alignment");
182314564Sdim    return obj;
183314564Sdim  } else {
184314564Sdim    return NULL;
185314564Sdim  }
186314564Sdim}
187314564Sdim
188314564Sdim// This version is lock-free.
189314564SdimHeapWord* MutableSpace::cas_allocate(size_t size) {
190314564Sdim  do {
191314564Sdim    HeapWord* obj = top();
192314564Sdim    if (pointer_delta(end(), obj) >= size) {
193314564Sdim      HeapWord* new_top = obj + size;
194314564Sdim      HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
195314564Sdim      // result can be one of two:
196314564Sdim      //  the old top value: the exchange succeeded
197314564Sdim      //  otherwise: the new value of the top is returned.
198314564Sdim      if (result != obj) {
199314564Sdim        continue; // another thread beat us to the allocation, try again
200314564Sdim      }
201314564Sdim      assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
202314564Sdim             "checking alignment");
203314564Sdim      return obj;
204314564Sdim    } else {
205314564Sdim      return NULL;
206314564Sdim    }
207314564Sdim  } while (true);
208314564Sdim}
209314564Sdim
210314564Sdim// Try to deallocate previous allocation. Returns true upon success.
211314564Sdimbool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {
212314564Sdim  HeapWord* expected_top = obj + size;
213314564Sdim  return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top;
214314564Sdim}
215314564Sdim
216314564Sdimvoid MutableSpace::oop_iterate_no_header(OopClosure* cl) {
217314564Sdim  HeapWord* obj_addr = bottom();
218314564Sdim  HeapWord* t = top();
219314564Sdim  // Could call objects iterate, but this is easier.
220314564Sdim  while (obj_addr < t) {
221254721Semaste    obj_addr += oop(obj_addr)->oop_iterate_no_header(cl);
222314564Sdim  }
223314564Sdim}
224314564Sdim
225314564Sdimvoid MutableSpace::object_iterate(ObjectClosure* cl) {
226314564Sdim  HeapWord* p = bottom();
227254721Semaste  while (p < top()) {
228296417Sdim    cl->do_object(oop(p));
229314564Sdim    p += oop(p)->size();
230314564Sdim  }
231314564Sdim}
232314564Sdim
233314564Sdimvoid MutableSpace::print_short() const { print_short_on(tty); }
234314564Sdimvoid MutableSpace::print_short_on( outputStream* st) const {
235314564Sdim  st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K,
236314564Sdim            (int) ((double) used_in_bytes() * 100 / capacity_in_bytes()));
237314564Sdim}
238314564Sdim
239314564Sdimvoid MutableSpace::print() const { print_on(tty); }
240314564Sdimvoid MutableSpace::print_on(outputStream* st) const {
241314564Sdim  MutableSpace::print_short_on(st);
242314564Sdim  st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")",
243314564Sdim                 p2i(bottom()), p2i(top()), p2i(end()));
244314564Sdim}
245314564Sdim
246314564Sdimvoid MutableSpace::verify() {
247314564Sdim  HeapWord* p = bottom();
248314564Sdim  HeapWord* t = top();
249314564Sdim  HeapWord* prev_p = NULL;
250314564Sdim  while (p < t) {
251314564Sdim    oop(p)->verify();
252314564Sdim    prev_p = p;
253254721Semaste    p += oop(p)->size();
254314564Sdim  }
255314564Sdim  guarantee(p == top(), "end of last object must match end of space");
256314564Sdim}
257314564Sdim