1 /*
   2  * Copyright (c) 2017, 2018, 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 "jfr/recorder/storage/jfrVirtualMemory.hpp"
  27 #include "runtime/orderAccess.hpp"
  28 #include "runtime/virtualspace.hpp"
  29 #include "runtime/os.hpp"
  30 #include "services/memTracker.hpp"
  31 #include "utilities/align.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 /*
  35  * A memory segment represents a virtual memory reservation.
  36  * It provides ways to commit and decommit physical storage
  37  * onto its virtual memory reservation.
  38  */
  39 
  40 class JfrVirtualMemorySegment : public JfrCHeapObj {
  41   friend class JfrVirtualMemoryManager;
  42  private:
  43   JfrVirtualMemorySegment* _next;
  44   char* _top;
  45   ReservedSpace _rs;
  46   VirtualSpace  _virtual_memory;
  47 
  48   // Convenience functions to access the underlying virtual space metadata
  49   const u1* committed_low()  const { return (const u1*)_virtual_memory.low(); }
  50   const u1* committed_high() const { return (const u1*)_virtual_memory.high(); }
  51   const u1* reserved_low() const { return (const u1*)_virtual_memory.low_boundary(); }
  52   const u1* reserved_high() const { return (const u1*)_virtual_memory.high_boundary(); }
  53   size_t reserved_words() const  { return _virtual_memory.reserved_size() / BytesPerWord; }
  54   size_t committed_words() const { return _virtual_memory.actual_committed_size() / BytesPerWord; }
  55   bool is_pre_committed() const { return _virtual_memory.special(); }
  56   VirtualSpace& virtual_space() { return _virtual_memory; }
  57 
  58   JfrVirtualMemorySegment();
  59   ~JfrVirtualMemorySegment();
  60 
  61   JfrVirtualMemorySegment* next() const { return _next; }
  62   void set_next(JfrVirtualMemorySegment* v) { _next = v; }
  63 
  64   // Returns true if requested size is available in the committed area
  65   bool is_available(size_t block_size_request_words) {
  66     return block_size_request_words <= pointer_delta(committed_high(), _top, sizeof(char*));
  67   }
  68 
  69   // allocation pointer committed memory
  70   char* top() const { return _top; }
  71   void inc_top(size_t size_in_words) {
  72     assert(is_available(size_in_words), "invariant");
  73     _top += size_in_words * BytesPerWord;
  74     assert(_top <= _virtual_memory.high(), "invariant");
  75   }
  76 
  77   // initialization is the virtual memory reservation
  78   bool initialize(size_t reservation_size_request_bytes);
  79   void* take_from_committed(size_t block_size_request_words);
  80 
  81   // Returns committed memory
  82   void* commit(size_t block_size_request_words) {
  83     return take_from_committed(block_size_request_words);
  84   }
  85 
  86   // Commit more memory in a reservation
  87   bool expand_by(size_t block_size_request_words);
  88 
  89   // Decommits all committed memory in this reservation segment.
  90   void decommit();
  91 };
  92 
  93 JfrVirtualMemorySegment::JfrVirtualMemorySegment() :
  94   _next(NULL),
  95   _top(NULL),
  96   _rs(),
  97   _virtual_memory() {}
  98 
  99 JfrVirtualMemorySegment::~JfrVirtualMemorySegment() {
 100   decommit();
 101   _rs.release();
 102 }
 103 
 104 bool JfrVirtualMemorySegment::initialize(size_t reservation_size_request_bytes) {
 105   assert(is_aligned(reservation_size_request_bytes, os::vm_allocation_granularity()), "invariant");
 106   _rs = ReservedSpace(reservation_size_request_bytes,
 107                       os::vm_allocation_granularity(),
 108                       UseLargePages && os::can_commit_large_page_memory(),
 109                       false);
 110   if (!_rs.is_reserved()) {
 111     return false;
 112   }
 113   assert(_rs.base() != NULL, "invariant");
 114   assert(_rs.size() != 0, "invariant");
 115   assert(is_aligned(_rs.base(), os::vm_allocation_granularity()), "invariant");
 116   assert(is_aligned(_rs.size(), os::vm_allocation_granularity()), "invariant");
 117   os::trace_page_sizes("Jfr", reservation_size_request_bytes,
 118                               reservation_size_request_bytes,
 119                               os::vm_page_size(),
 120                               _rs.base(),
 121                               _rs.size());
 122   MemTracker::record_virtual_memory_type((address)_rs.base(), mtTracing);
 123   assert(is_aligned(_rs.base(), os::vm_page_size()), "invariant");
 124   assert(is_aligned(_rs.size(), os::vm_page_size()), "invariant");
 125 
 126   // ReservedSpaces marked as special will have the entire memory
 127   // pre-committed. Setting a committed size will make sure that
 128   // committed_size and actual_committed_size agrees.
 129   const size_t pre_committed_size = _rs.special() ? _rs.size() : 0;
 130   const bool result = virtual_space().initialize_with_granularity(_rs, pre_committed_size, os::vm_page_size());
 131 
 132   if (result) {
 133     assert(virtual_space().committed_size() == virtual_space().actual_committed_size(),
 134       "Checking that the pre-committed memory was registered by the VirtualSpace");
 135     _top = virtual_space().low();
 136   }
 137   return result;
 138 }
 139 
 140 bool JfrVirtualMemorySegment::expand_by(size_t block_size_request_words) {
 141   size_t block_size_request_bytes = block_size_request_words * BytesPerWord;
 142   const size_t uncommitted = virtual_space().reserved_size() - virtual_space().actual_committed_size();
 143   if (uncommitted < block_size_request_bytes) {
 144     // commit whatever is left in the reservation
 145     block_size_request_bytes = uncommitted;
 146   }
 147   assert(is_aligned(block_size_request_bytes, os::vm_allocation_granularity()), "invariant");
 148   // commit block in reserved memory
 149   bool result = virtual_space().expand_by(block_size_request_bytes, false);
 150   assert(result, "Failed to commit memory");
 151   return result;
 152 }
 153 
 154 void JfrVirtualMemorySegment::decommit() {
 155   assert(_virtual_memory.committed_size() == _virtual_memory.actual_committed_size(),
 156     "The committed memory doesn't match the expanded memory.");
 157 
 158   const size_t committed_size = virtual_space().actual_committed_size();
 159   if (committed_size > 0) {
 160     virtual_space().shrink_by(committed_size);
 161   }
 162 
 163   assert(_virtual_memory.actual_committed_size() == 0, "invariant");
 164 }
 165 
 166 // Attempt to get a committed block
 167 void* JfrVirtualMemorySegment::take_from_committed(size_t block_size_request_words) {
 168   // The virtual spaces are always expanded by the
 169   // commit granularity to enforce the following condition.
 170   // Without this the is_available check will not work correctly.
 171   assert(_virtual_memory.committed_size() == _virtual_memory.actual_committed_size(),
 172     "The committed memory doesn't match the expanded memory.");
 173   if (!is_available(block_size_request_words)) {
 174     return NULL;
 175   }
 176   void* const block = top();
 177   assert(block != NULL, "invariant");
 178   inc_top(block_size_request_words);
 179   return block;
 180 }
 181 
 182 class JfrVirtualMemoryManager : public JfrCHeapObj {
 183  typedef JfrVirtualMemorySegment Segment;
 184  private:
 185   Segment* _segments;
 186   Segment* _current_segment;
 187   size_t _reservation_size_request_words;
 188   size_t _reservation_size_request_limit_words; // total reservation limit
 189 
 190   // Sum of reserved and committed memory in the segments
 191   size_t _current_reserved_words;
 192   size_t _current_committed_words;
 193 
 194   void link(Segment* segment);
 195   Segment* current();
 196 
 197   void inc_reserved_words(size_t words);
 198   void inc_committed_words(size_t words);
 199 
 200   bool new_segment(size_t reservation_size_request_words);
 201 
 202   bool expand_segment_by(Segment* segment, size_t block_size_request_words);
 203 
 204   bool expand_by(size_t block_size_request_words, size_t reservation_size_request_words);
 205   bool can_reserve() const;
 206 
 207  public:
 208   JfrVirtualMemoryManager();
 209   ~JfrVirtualMemoryManager();
 210 
 211   bool initialize(size_t reservation_size_request_words, size_t segment_count = 1);
 212   void* commit(size_t requested_block_size_words);
 213 
 214   bool is_full() const {
 215     return reserved_high() == committed_high();
 216   }
 217 
 218   const u1* committed_low() const { return _current_segment->committed_low(); }
 219   const u1* committed_high() const { return _current_segment->committed_high(); }
 220   const u1* reserved_low() const { return _current_segment->reserved_low(); }
 221   const u1* reserved_high() const { return _current_segment->reserved_high(); }
 222 };
 223 
 224 JfrVirtualMemoryManager::JfrVirtualMemoryManager() :
 225   _segments(NULL),
 226   _current_segment(NULL),
 227   _reservation_size_request_words(0),
 228   _reservation_size_request_limit_words(0),
 229   _current_reserved_words(0),
 230   _current_committed_words(0) {}
 231 
 232 JfrVirtualMemoryManager::~JfrVirtualMemoryManager() {
 233   JfrVirtualMemorySegment* segment = _segments;
 234   while (segment != NULL) {
 235     JfrVirtualMemorySegment* next_segment = segment->next();
 236     delete segment;
 237     segment = next_segment;
 238   }
 239 }
 240 
 241 // for now only allow a singleton segment per virtual memory client
 242 bool JfrVirtualMemoryManager::initialize(size_t reservation_size_request_words, size_t segment_count /* 1 */) {
 243   assert(is_aligned(reservation_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant");
 244   _reservation_size_request_words = reservation_size_request_words;
 245   assert(segment_count > 0, "invariant");
 246   _reservation_size_request_limit_words = reservation_size_request_words * segment_count;
 247   assert(is_aligned(_reservation_size_request_limit_words * BytesPerWord, os::vm_allocation_granularity()), "invariant");
 248   return new_segment(_reservation_size_request_words);
 249 }
 250 
 251 bool JfrVirtualMemoryManager::can_reserve() const  {
 252   return _reservation_size_request_limit_words == 0 ? true : _current_reserved_words < _reservation_size_request_limit_words;
 253 }
 254 
 255 // Allocate another segment and add it to the list.
 256 bool JfrVirtualMemoryManager::new_segment(size_t reservation_size_request_words) {
 257   assert(reservation_size_request_words > 0, "invariant");
 258   assert(is_aligned(reservation_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant");
 259   Segment* segment = new Segment();
 260   if (NULL == segment) {
 261     return false;
 262   }
 263   if (!segment->initialize(reservation_size_request_words * BytesPerWord)) {
 264     delete segment;
 265     return false;
 266   }
 267   assert(segment->reserved_words() == reservation_size_request_words,
 268     "Actual reserved memory size differs from requested reservation memory size");
 269   link(segment);
 270   return true;
 271 }
 272 
 273 bool JfrVirtualMemoryManager::expand_segment_by(JfrVirtualMemorySegment* segment, size_t block_size_request_words) {
 274   assert(segment != NULL, "invariant");
 275   const size_t before = segment->committed_words();
 276   const bool result = segment->expand_by(block_size_request_words);
 277   const size_t after = segment->committed_words();
 278   // after and before can be the same if the memory was pre-committed.
 279   assert(after >= before, "Inconsistency");
 280   inc_committed_words(after - before);
 281   return result;
 282 }
 283 
 284 void JfrVirtualMemoryManager::inc_reserved_words(size_t words) {
 285   _current_reserved_words += words;
 286 }
 287 
 288 JfrVirtualMemorySegment* JfrVirtualMemoryManager::current() {
 289   return _current_segment;
 290 }
 291 
 292 void JfrVirtualMemoryManager::inc_committed_words(size_t words) {
 293   _current_committed_words += words;
 294 }
 295 
 296 bool JfrVirtualMemoryManager::expand_by(size_t block_size_request_words, size_t reservation_size_request_words) {
 297   assert(is_aligned(block_size_request_words * BytesPerWord, os::vm_page_size()), "invariant");
 298   assert(is_aligned(block_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant");
 299   assert(is_aligned(reservation_size_request_words * BytesPerWord, os::vm_page_size()), "invariant");
 300   assert(is_aligned(reservation_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant");
 301   assert(block_size_request_words <= reservation_size_request_words, "invariant");
 302   // Attempt to commit more memory from the the current virtual space reservation.
 303   if (expand_segment_by(current(), block_size_request_words)) {
 304     return true;
 305   }
 306 
 307   // reached limit of what is allowed to be reserved?
 308   if (!can_reserve()) {
 309     return false;
 310   }
 311 
 312   // Get another segment.
 313   if (!new_segment(reservation_size_request_words)) {
 314     return false;
 315   }
 316 
 317   if (current()->is_pre_committed()) {
 318     // The memory was pre-committed, so we are done here.
 319     assert(block_size_request_words <= current()->committed_words(),
 320            "The new VirtualSpace was pre-committed, so it"
 321            "should be large enough to fit the alloc request.");
 322     return true;
 323   }
 324   return expand_segment_by(current(), block_size_request_words);
 325 }
 326 
 327 void JfrVirtualMemoryManager::link(JfrVirtualMemorySegment* segment) {
 328   assert(segment != NULL, "invariant");
 329   if (_segments == NULL) {
 330     _segments = segment;
 331   } else {
 332     assert(_current_segment != NULL, "invariant");
 333     assert(_segments == _current_segment, "invariant");
 334     _current_segment->set_next(segment);
 335   }
 336   _current_segment = segment;
 337   inc_reserved_words(segment->reserved_words());
 338   inc_committed_words(segment->committed_words());
 339 }
 340 
 341 void* JfrVirtualMemoryManager::commit(size_t block_size_request_words) {
 342   assert(is_aligned(block_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant");
 343   void* block = current()->commit(block_size_request_words);
 344   if (block != NULL) {
 345     return block;
 346   }
 347   assert(block == NULL, "invariant");
 348   if (is_full()) {
 349     return NULL;
 350   }
 351   assert(block_size_request_words <= _reservation_size_request_words, "invariant");
 352   if (expand_by(block_size_request_words, _reservation_size_request_words)) {
 353     block = current()->commit(block_size_request_words);
 354     assert(block != NULL, "The allocation was expected to succeed after the expansion");
 355   }
 356   return block;
 357 }
 358 
 359 JfrVirtualMemory::JfrVirtualMemory() :
 360   _vmm(NULL),
 361   _reserved_low(),
 362   _reserved_high(),
 363   _top(NULL),
 364   _commit_point(NULL),
 365   _physical_commit_size_request_words(0),
 366   _aligned_datum_size_bytes(0) {}
 367 
 368 JfrVirtualMemory::~JfrVirtualMemory() {
 369   assert(_vmm != NULL, "invariant");
 370   delete _vmm;
 371 }
 372 
 373 size_t JfrVirtualMemory::aligned_datum_size_bytes() const {
 374   return _aligned_datum_size_bytes;
 375 }
 376 
 377 static void adjust_allocation_ratio(size_t* const reservation_size_bytes, size_t* const commit_size_bytes) {
 378   assert(reservation_size_bytes != NULL, "invariant");
 379   assert(*reservation_size_bytes > 0, "invariant");
 380   assert(commit_size_bytes != NULL, "invariant");
 381   assert(*commit_size_bytes > 0, "invariant");
 382   assert(*reservation_size_bytes >= *commit_size_bytes, "invariant");
 383   assert(is_aligned(*reservation_size_bytes, os::vm_allocation_granularity()), "invariant");
 384   assert(is_aligned(*commit_size_bytes, os::vm_allocation_granularity()), "invariant");
 385 
 386   size_t reservation_size_units = *reservation_size_bytes / os::vm_allocation_granularity();
 387   size_t commit_size_units = *commit_size_bytes / os::vm_allocation_granularity();
 388   assert(reservation_size_units > 0, "invariant");
 389   assert(commit_size_units > 0, "invariant");
 390 
 391   size_t original_ratio_units = reservation_size_units / commit_size_units;
 392   size_t rem = reservation_size_units % commit_size_units;
 393   assert(original_ratio_units > 0, "invariant");
 394 
 395   if (rem > 0) {
 396     reservation_size_units -= rem % original_ratio_units;
 397     commit_size_units += rem / original_ratio_units;
 398   }
 399 
 400   assert(commit_size_units > 0, "invariant");
 401   assert(reservation_size_units % original_ratio_units == 0, "invariant");
 402   assert(original_ratio_units * commit_size_units == reservation_size_units , "invariant");
 403   assert(original_ratio_units == reservation_size_units / commit_size_units, "invariant");
 404   *reservation_size_bytes = reservation_size_units * os::vm_allocation_granularity();
 405   *commit_size_bytes = commit_size_units * os::vm_allocation_granularity();
 406   assert((*reservation_size_bytes % *commit_size_bytes) == 0, "invariant");
 407 }
 408 
 409 
 410 void* JfrVirtualMemory::initialize(size_t reservation_size_request_bytes,
 411                                    size_t block_size_request_bytes,
 412                                    size_t datum_size_bytes /* 1 */) {
 413   assert(_vmm == NULL, "invariant");
 414   _vmm = new JfrVirtualMemoryManager();
 415 
 416   if (_vmm == NULL) {
 417     return NULL;
 418   }
 419 
 420   assert(reservation_size_request_bytes > 0, "invariant");
 421   _aligned_datum_size_bytes = align_up(datum_size_bytes, BytesPerWord);
 422   assert(is_aligned(_aligned_datum_size_bytes, BytesPerWord), "invariant");
 423 
 424   reservation_size_request_bytes = ReservedSpace::allocation_align_size_up(reservation_size_request_bytes);
 425   assert(is_aligned(reservation_size_request_bytes, os::vm_allocation_granularity()), "invariant");
 426   assert(is_aligned(reservation_size_request_bytes, _aligned_datum_size_bytes), "invariant");
 427   block_size_request_bytes = MAX2(block_size_request_bytes, (size_t)os::vm_allocation_granularity());
 428   block_size_request_bytes = ReservedSpace::allocation_align_size_up(block_size_request_bytes);
 429   assert(is_aligned(block_size_request_bytes, os::vm_allocation_granularity()), "invariant");
 430   assert(is_aligned(block_size_request_bytes, _aligned_datum_size_bytes), "invariant");
 431   // adjustment to valid ratio in units of vm_allocation_granularity
 432   adjust_allocation_ratio(&reservation_size_request_bytes, &block_size_request_bytes);
 433   assert(is_aligned(reservation_size_request_bytes, os::vm_allocation_granularity()), "invariant");
 434   assert(is_aligned(reservation_size_request_bytes, _aligned_datum_size_bytes), "invariant");
 435   assert(is_aligned(block_size_request_bytes, os::vm_allocation_granularity()), "invariant");
 436   assert(is_aligned(block_size_request_bytes, _aligned_datum_size_bytes), "invariant");
 437   assert((reservation_size_request_bytes % block_size_request_bytes) == 0, "invariant");
 438   const size_t reservation_size_request_words = reservation_size_request_bytes / BytesPerWord;
 439   _physical_commit_size_request_words = block_size_request_bytes / BytesPerWord;
 440   // virtual memory reservation
 441   if (!_vmm->initialize(reservation_size_request_words)) {
 442     // is implicitly "full" if reservation fails
 443     assert(is_full(), "invariant");
 444     return NULL;
 445   }
 446   _reserved_low = (const u1*)_vmm->reserved_low();
 447   _reserved_high = (const u1*)_vmm->reserved_high();
 448   // reservation complete
 449   _top = (u1*)_vmm->committed_high();
 450   _commit_point = _top;
 451   assert(_reserved_low == _top, "invariant"); // initial empty state
 452   assert((size_t)(_reserved_high - _reserved_low) == reservation_size_request_bytes, "invariant");
 453   // initial commit
 454   commit_memory_block();
 455   return _top;
 456 }
 457 
 458 void* JfrVirtualMemory::commit(size_t block_size_request_words) {
 459   assert(_vmm != NULL, "invariant");
 460   assert(is_aligned(block_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant");
 461   return _vmm->commit(block_size_request_words);
 462 }
 463 
 464 bool JfrVirtualMemory::is_full() const {
 465   return _top == _reserved_high;
 466 }
 467 
 468 bool JfrVirtualMemory::is_empty() const {
 469   return _top == _reserved_low;
 470 }
 471 
 472 bool JfrVirtualMemory::commit_memory_block() {
 473   assert(_vmm != NULL, "invariant");
 474   assert(!is_full(), "invariant");
 475   assert(_top == _commit_point, "invariant");
 476 
 477   void* const block = _vmm->commit(_physical_commit_size_request_words);
 478   if (block != NULL) {
 479     _commit_point = _vmm->committed_high();
 480     return true;
 481   }
 482   // all reserved virtual memory is committed
 483   assert(block == NULL, "invariant");
 484   assert(_vmm->reserved_high() == _vmm->committed_high(), "invariant");
 485   return false;
 486 }
 487 
 488 void* JfrVirtualMemory::new_datum() {
 489   assert(_vmm != NULL, "invariant");
 490   assert(!is_full(), "invariant");
 491   if (_top == _commit_point) {
 492     if (!commit_memory_block()) {
 493       assert(is_full(), "invariant");
 494       return NULL;
 495     }
 496   }
 497   assert(_top + _aligned_datum_size_bytes <= _commit_point, "invariant");
 498   u1* allocation = _top;
 499   _top += _aligned_datum_size_bytes;
 500   assert(is_aligned(allocation, _aligned_datum_size_bytes), "invariant");
 501   return allocation;
 502 }
 503 
 504 void* JfrVirtualMemory::index_ptr(size_t index) {
 505   assert((index * _aligned_datum_size_bytes) + _reserved_low < _commit_point, "invariant");
 506   return (void*)((index * _aligned_datum_size_bytes) + _reserved_low);
 507 }
 508 
 509 void* JfrVirtualMemory::get(size_t index) {
 510   return index_ptr(index);
 511 }
 512 
 513 size_t JfrVirtualMemory::count() const {
 514   return (_top - _reserved_low) / _aligned_datum_size_bytes;
 515 }
 516 
 517 size_t JfrVirtualMemory::live_set() const {
 518   return _top - _reserved_low;
 519 }
 520 
 521 size_t JfrVirtualMemory::reserved_size() const {
 522   return _reserved_high - _reserved_low;
 523 }
 524 
 525 bool JfrVirtualMemory::compact(size_t index) {
 526   assert(index > 0, "invariant");
 527   assert(index <= reserved_size(), "invariant");
 528   const u1* low = static_cast<u1*>(index_ptr(index));
 529   const size_t block_size = _top - low;
 530   memcpy(const_cast<u1*>(_reserved_low), low, block_size);
 531   _top = const_cast<u1*>(_reserved_low) + block_size;
 532   assert(live_set() == block_size, "invariant");
 533   return true;
 534 }