1 /*
   2  * Copyright (c) 2014, 2019, 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/g1/g1NUMA.inline.hpp"
  27 #include "gc/g1/g1PageBasedVirtualSpace.hpp"
  28 #include "gc/shared/workgroup.hpp"
  29 #include "oops/markWord.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/atomic.hpp"
  32 #include "runtime/os.inline.hpp"
  33 #include "services/memTracker.hpp"
  34 #include "utilities/align.hpp"
  35 #include "utilities/bitMap.inline.hpp"
  36 
  37 G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size, MemoryType type) :
  38   _low_boundary(NULL), _high_boundary(NULL), _tail_size(0), _page_size(0),
  39   _committed(mtGC), _dirty(mtGC), _special(false), _executable(false), _numa(NULL) {
  40   initialize_with_page_size(rs, used_size, page_size, type);
  41 }
  42 
  43 void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size, MemoryType type) {
  44   guarantee(rs.is_reserved(), "Given reserved space must have been reserved already.");
  45 
  46   vmassert(_low_boundary == NULL, "VirtualSpace already initialized");
  47   vmassert(page_size > 0, "Page size must be non-zero.");
  48 
  49   guarantee(is_aligned(rs.base(), page_size),
  50             "Reserved space base " PTR_FORMAT " is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), page_size);
  51   guarantee(is_aligned(used_size, os::vm_page_size()),
  52             "Given used reserved space size needs to be OS page size aligned (%d bytes) but is " SIZE_FORMAT, os::vm_page_size(), used_size);
  53   guarantee(used_size <= rs.size(),
  54             "Used size of reserved space " SIZE_FORMAT " bytes is smaller than reservation at " SIZE_FORMAT " bytes", used_size, rs.size());
  55   guarantee(is_aligned(rs.size(), page_size),
  56             "Expected that the virtual space is size aligned, but " SIZE_FORMAT " is not aligned to page size " SIZE_FORMAT, rs.size(), page_size);
  57 
  58   _low_boundary  = rs.base();
  59   _high_boundary = _low_boundary + used_size;
  60 
  61   _special = rs.special();
  62   _executable = rs.executable();
  63 
  64   _page_size = page_size;
  65 
  66   vmassert(_committed.size() == 0, "virtual space initialized more than once");
  67   BitMap::idx_t size_in_pages = rs.size() / page_size;
  68   _committed.initialize(size_in_pages);
  69   if (_special) {
  70     _dirty.initialize(size_in_pages);
  71   }
  72 
  73   _tail_size = used_size % _page_size;
  74 
  75   // Set _numa only if:
  76   //   1) This space is for java heap.
  77   //   2) There are multiple memory nodes because some OSes allow enabling UseNUMA.
  78   if (type == mtJavaHeap && G1MemoryNodeManager::mgr()->num_active_nodes() > 1) {
  79     _numa = G1NUMA::numa();
  80   }
  81 }
  82 
  83 G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() {
  84   // This does not release memory it never reserved.
  85   // Caller must release via rs.release();
  86   _low_boundary           = NULL;
  87   _high_boundary          = NULL;
  88   _special                = false;
  89   _executable             = false;
  90   _page_size              = 0;
  91   _tail_size              = 0;
  92   _numa                   = NULL;
  93 }
  94 
  95 size_t G1PageBasedVirtualSpace::committed_size() const {
  96   size_t result = _committed.count_one_bits() * _page_size;
  97   // The last page might not be in full.
  98   if (is_last_page_partial() && _committed.at(_committed.size() - 1)) {
  99     result -= _page_size - _tail_size;
 100   }
 101   return result;
 102 }
 103 
 104 size_t G1PageBasedVirtualSpace::reserved_size() const {
 105   return pointer_delta(_high_boundary, _low_boundary, sizeof(char));
 106 }
 107 
 108 size_t G1PageBasedVirtualSpace::uncommitted_size()  const {
 109   return reserved_size() - committed_size();
 110 }
 111 
 112 void G1PageBasedVirtualSpace::commit_and_set_special() {
 113   commit_internal(addr_to_page_index(_low_boundary), addr_to_page_index(_high_boundary));
 114   _special = true;
 115   _dirty.initialize(reserved_size()/_page_size);
 116 }
 117 
 118 size_t G1PageBasedVirtualSpace::addr_to_page_index(char* addr) const {
 119   return (addr - _low_boundary) / _page_size;
 120 }
 121 
 122 bool G1PageBasedVirtualSpace::is_area_committed(size_t start_page, size_t size_in_pages) const {
 123   size_t end_page = start_page + size_in_pages;
 124   return _committed.get_next_zero_offset(start_page, end_page) >= end_page;
 125 }
 126 
 127 bool G1PageBasedVirtualSpace::is_area_uncommitted(size_t start_page, size_t size_in_pages) const {
 128   size_t end_page = start_page + size_in_pages;
 129   return _committed.get_next_one_offset(start_page, end_page) >= end_page;
 130 }
 131 
 132 char* G1PageBasedVirtualSpace::page_start(size_t index) const {
 133   return _low_boundary + index * _page_size;
 134 }
 135 
 136 bool G1PageBasedVirtualSpace::is_after_last_page(size_t index) const {
 137   guarantee(index <= _committed.size(),
 138             "Given boundary page " SIZE_FORMAT " is beyond managed page count " SIZE_FORMAT, index, _committed.size());
 139   return index == _committed.size();
 140 }
 141 
 142 void G1PageBasedVirtualSpace::commit_preferred_pages(size_t start, size_t num_pages) {
 143   vmassert(num_pages > 0, "No full pages to commit");
 144   vmassert(start + num_pages <= _committed.size(),
 145            "Tried to commit area from page " SIZE_FORMAT " to page " SIZE_FORMAT " "
 146            "that is outside of managed space of " SIZE_FORMAT " pages",
 147            start, start + num_pages, _committed.size());
 148 
 149   char* start_addr = page_start(start);
 150   size_t size = num_pages * _page_size;
 151 
 152   os::commit_memory_or_exit(start_addr, size, _page_size, _executable,
 153                             err_msg("Failed to commit area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".",
 154                             p2i(start_addr), p2i(start_addr + size), size));
 155 }
 156 
 157 void G1PageBasedVirtualSpace::commit_tail() {
 158   vmassert(_tail_size > 0, "The size of the tail area must be > 0 when reaching here");
 159 
 160   char* const aligned_end_address = align_down(_high_boundary, _page_size);
 161   os::commit_memory_or_exit(aligned_end_address, _tail_size, os::vm_page_size(), _executable,
 162                             err_msg("Failed to commit tail area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".",
 163                             p2i(aligned_end_address), p2i(_high_boundary), _tail_size));
 164 }
 165 
 166 void G1PageBasedVirtualSpace::commit_internal(size_t start_page, size_t end_page) {
 167   guarantee(start_page < end_page,
 168             "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
 169   guarantee(end_page <= _committed.size(),
 170             "Given end page " SIZE_FORMAT " is beyond end of managed page amount of " SIZE_FORMAT, end_page, _committed.size());
 171 
 172   size_t pages = end_page - start_page;
 173   bool need_to_commit_tail = is_after_last_page(end_page) && is_last_page_partial();
 174 
 175   // If we have to commit some (partial) tail area, decrease the amount of pages to avoid
 176   // committing that in the full-page commit code.
 177   if (need_to_commit_tail) {
 178     pages--;
 179   }
 180 
 181   if (pages > 0) {
 182     commit_preferred_pages(start_page, pages);
 183   }
 184 
 185   if (need_to_commit_tail) {
 186     commit_tail();
 187   }
 188 }
 189 
 190 char* G1PageBasedVirtualSpace::bounded_end_addr(size_t end_page) const {
 191   return MIN2(_high_boundary, page_start(end_page));
 192 }
 193 
 194 void G1PageBasedVirtualSpace::pretouch_internal(size_t start_page, size_t end_page) {
 195   guarantee(start_page < end_page,
 196             "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
 197 
 198   os::pretouch_memory(page_start(start_page), bounded_end_addr(end_page), _page_size);
 199 }
 200 
 201 bool G1PageBasedVirtualSpace::commit(size_t start_page, size_t size_in_pages) {
 202   // We need to make sure to commit all pages covered by the given area.
 203   guarantee(is_area_uncommitted(start_page, size_in_pages), "Specified area is not uncommitted");
 204 
 205   bool zero_filled = true;
 206   size_t end_page = start_page + size_in_pages;
 207 
 208   if (_special) {
 209     // Check for dirty pages and update zero_filled if any found.
 210     if (_dirty.get_next_one_offset(start_page, end_page) < end_page) {
 211       zero_filled = false;
 212       _dirty.clear_range(start_page, end_page);
 213     }
 214   } else {
 215     commit_internal(start_page, end_page);
 216   }
 217   _committed.set_range(start_page, end_page);
 218 
 219   if (_numa != NULL) {
 220     char* start_addr = page_start(start_page);
 221     size_t size_in_bytes = size_in_pages * _page_size;
 222     _numa->request_memory_on_node((address)start_addr, size_in_bytes);
 223   }
 224 
 225   return zero_filled;
 226 }
 227 
 228 void G1PageBasedVirtualSpace::uncommit_internal(size_t start_page, size_t end_page) {
 229   guarantee(start_page < end_page,
 230             "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
 231 
 232   char* start_addr = page_start(start_page);
 233   os::uncommit_memory(start_addr, pointer_delta(bounded_end_addr(end_page), start_addr, sizeof(char)));
 234 }
 235 
 236 void G1PageBasedVirtualSpace::uncommit(size_t start_page, size_t size_in_pages) {
 237   guarantee(is_area_committed(start_page, size_in_pages), "checking");
 238 
 239   size_t end_page = start_page + size_in_pages;
 240   if (_special) {
 241     // Mark that memory is dirty. If committed again the memory might
 242     // need to be cleared explicitly.
 243     _dirty.set_range(start_page, end_page);
 244   } else {
 245     uncommit_internal(start_page, end_page);
 246   }
 247 
 248   _committed.clear_range(start_page, end_page);
 249 }
 250 
 251 class G1PretouchTask : public AbstractGangTask {
 252 private:
 253   char* volatile _cur_addr;
 254   char* const _start_addr;
 255   char* const _end_addr;
 256   size_t _page_size;
 257 public:
 258   G1PretouchTask(char* start_address, char* end_address, size_t page_size) :
 259     AbstractGangTask("G1 PreTouch"),
 260     _cur_addr(start_address),
 261     _start_addr(start_address),
 262     _end_addr(end_address),
 263     _page_size(0) {
 264 #ifdef LINUX
 265     _page_size = UseTransparentHugePages ? (size_t)os::vm_page_size(): page_size;
 266 #else
 267     _page_size = page_size;
 268 #endif
 269   }
 270 
 271   virtual void work(uint worker_id) {
 272     size_t const actual_chunk_size = MAX2(chunk_size(), _page_size);
 273     while (true) {
 274       char* touch_addr = Atomic::add(actual_chunk_size, &_cur_addr) - actual_chunk_size;
 275       if (touch_addr < _start_addr || touch_addr >= _end_addr) {
 276         break;
 277       }
 278       char* end_addr = touch_addr + MIN2(actual_chunk_size, pointer_delta(_end_addr, touch_addr, sizeof(char)));
 279       os::pretouch_memory(touch_addr, end_addr, _page_size);
 280     }
 281   }
 282 
 283   static size_t chunk_size() { return PreTouchParallelChunkSize; }
 284 };
 285 
 286 void G1PageBasedVirtualSpace::pretouch(size_t start_page, size_t size_in_pages, WorkGang* pretouch_gang) {
 287   G1PretouchTask cl(page_start(start_page), bounded_end_addr(start_page + size_in_pages), _page_size);
 288 
 289   if (pretouch_gang != NULL) {
 290     size_t num_chunks = MAX2((size_t)1, size_in_pages * _page_size / MAX2(G1PretouchTask::chunk_size(), _page_size));
 291 
 292     uint num_workers = MIN2((uint)num_chunks, pretouch_gang->active_workers());
 293     log_debug(gc, heap)("Running %s with %u workers for " SIZE_FORMAT " work units pre-touching " SIZE_FORMAT "B.",
 294                         cl.name(), num_workers, num_chunks, size_in_pages * _page_size);
 295     pretouch_gang->run_task(&cl, num_workers);
 296   } else {
 297     log_debug(gc, heap)("Running %s pre-touching " SIZE_FORMAT "B.",
 298                         cl.name(), size_in_pages * _page_size);
 299     cl.work(0);
 300   }
 301 }
 302 
 303 bool G1PageBasedVirtualSpace::contains(const void* p) const {
 304   return _low_boundary <= (const char*) p && (const char*) p < _high_boundary;
 305 }
 306 
 307 #ifndef PRODUCT
 308 void G1PageBasedVirtualSpace::print_on(outputStream* out) {
 309   out->print   ("Virtual space:");
 310   if (_special) out->print(" (pinned in memory)");
 311   out->cr();
 312   out->print_cr(" - committed: " SIZE_FORMAT, committed_size());
 313   out->print_cr(" - reserved:  " SIZE_FORMAT, reserved_size());
 314   out->print_cr(" - preferred page size: " SIZE_FORMAT, _page_size);
 315   out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT ", " PTR_FORMAT "]",  p2i(_low_boundary), p2i(_high_boundary));
 316 }
 317 
 318 void G1PageBasedVirtualSpace::print() {
 319   print_on(tty);
 320 }
 321 #endif