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