1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, 2020 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 
  28 
  29 #include "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/metaspace/arenaGrowthPolicy.hpp"
  32 #include "memory/metaspace/chunkLevel.hpp"
  33 #include "memory/metaspace/chunkManager.hpp"
  34 #include "memory/metaspace/internStat.hpp"
  35 #include "memory/metaspace/metachunk.hpp"
  36 #include "memory/metaspace/metaspaceCommon.hpp"
  37 #include "memory/metaspace/metaspaceContext.hpp"
  38 #include "memory/metaspace/metaspaceStatistics.hpp"
  39 #include "memory/metaspace/settings.hpp"
  40 #include "memory/metaspace/virtualSpaceNode.hpp"
  41 #include "memory/metaspace/virtualSpaceList.hpp"
  42 #include "runtime/mutexLocker.hpp"
  43 #include "utilities/debug.hpp"
  44 #include "utilities/globalDefinitions.hpp"
  45 
  46 namespace metaspace {
  47 
  48 #define LOGFMT         "ChkMgr @" PTR_FORMAT " (%s)"
  49 #define LOGFMT_ARGS    p2i(this), this->_name
  50 
  51 // Return a single chunk to the freelist and adjust accounting. No merge is attempted.
  52 void ChunkManager::return_chunk_simple_locked(Metachunk* c) {
  53 
  54   assert_lock_strong(MetaspaceExpand_lock);
  55 
  56   DEBUG_ONLY(c->verify(false));
  57 
  58   const chunklevel_t lvl = c->level();
  59   _chunks.add(c);
  60   c->reset_used_words();
  61 
  62   // Tracing
  63   log_debug(metaspace)("ChunkManager %s: returned chunk " METACHUNK_FORMAT ".",
  64                        _name, METACHUNK_FORMAT_ARGS(c));
  65 
  66 }
  67 
  68 // Creates a chunk manager with a given name (which is for debug purposes only)
  69 // and an associated space list which will be used to request new chunks from
  70 // (see get_chunk())
  71 ChunkManager::ChunkManager(const char* name, VirtualSpaceList* space_list)
  72   : _vslist(space_list),
  73     _name(name),
  74     _chunks()
  75 {
  76 }
  77 
  78 // Given a chunk, split it into a target chunk of a smaller size (higher target level)
  79 //  and at least one, possible several splinter chunks.
  80 // The original chunk must be outside of the freelist and its state must be free.
  81 // The splinter chunks are added to the freelist.
  82 // The resulting target chunk will be located at the same address as the original
  83 //  chunk, but it will of course be smaller (of a higher level).
  84 // The committed areas within the original chunk carry over to the resulting
  85 //  chunks.
  86 void ChunkManager::split_chunk_and_add_splinters(Metachunk* c, chunklevel_t target_level) {
  87 
  88   assert_lock_strong(MetaspaceExpand_lock);
  89 
  90   assert(c->is_free(), "chunk to be split must be free.");
  91   assert(c->level() < target_level, "Target level must be higher than current level.");
  92   assert(c->prev() == NULL && c->next() == NULL, "Chunk must be outside of any list.");
  93 
  94   DEBUG_ONLY(chunklevel::check_valid_level(target_level);)
  95   DEBUG_ONLY(c->verify(true);)
  96 
  97   UL2(debug, "splitting chunk " METACHUNK_FORMAT " to " CHKLVL_FORMAT ".",
  98       METACHUNK_FORMAT_ARGS(c), target_level);
  99 
 100   DEBUG_ONLY(size_t committed_words_before = c->committed_words();)
 101 
 102   const chunklevel_t orig_level = c->level();
 103   c->vsnode()->split(target_level, c, &_chunks);
 104 
 105   // Splitting should never fail.
 106   assert(c->level() == target_level, "Sanity");
 107 
 108   // The size of the committed portion should not change (subject to the reduced chunk size of course)
 109 #ifdef ASSERT
 110   if (committed_words_before > c->word_size()) {
 111     assert(c->is_fully_committed(), "Sanity");
 112   } else {
 113     assert(c->committed_words() == committed_words_before, "Sanity");
 114   }
 115 #endif
 116 
 117   DEBUG_ONLY(c->verify(false));
 118 
 119   DEBUG_ONLY(verify_locked(true);)
 120 
 121   SOMETIMES(c->vsnode()->verify_locked(true);)
 122 
 123   InternalStats::inc_num_chunk_splits();
 124 
 125 }
 126 
 127 // On success, returns a chunk of level of <preferred_level>, but at most <max_level>.
 128 //  The first first <min_committed_words> of the chunk are guaranteed to be committed.
 129 // On error, will return NULL.
 130 //
 131 // This function may fail for two reasons:
 132 // - Either we are unable to reserve space for a new chunk (if the underlying VirtualSpaceList
 133 //   is non-expandable but needs expanding - aka out of compressed class space).
 134 // - Or, if the necessary space cannot be committed because we hit a commit limit.
 135 //   This may be either the GC threshold or MaxMetaspaceSize.
 136 Metachunk* ChunkManager::get_chunk(chunklevel_t preferred_level, chunklevel_t max_level, size_t min_committed_words) {
 137 
 138   assert(preferred_level <= max_level, "Sanity");
 139   assert(chunklevel::level_fitting_word_size(min_committed_words) >= max_level, "Sanity");
 140 
 141   MutexLocker fcl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
 142 
 143   DEBUG_ONLY(verify_locked(false);)
 144 
 145   DEBUG_ONLY(chunklevel::check_valid_level(max_level);)
 146   DEBUG_ONLY(chunklevel::check_valid_level(preferred_level);)
 147   assert(max_level >= preferred_level, "invalid level.");
 148 
 149   UL2(debug, "requested chunk: pref_level: " CHKLVL_FORMAT
 150      ", max_level: " CHKLVL_FORMAT ", min committed size: " SIZE_FORMAT ".",
 151      preferred_level, max_level, min_committed_words);
 152 
 153   // First, optimistically look for a chunk which is already committed far enough to hold min_word_size.
 154 
 155   // 1) Search best or smaller committed chunks (first attempt):
 156   //    Start at the preferred chunk size and work your way down (level up).
 157   //    But for now, only consider chunks larger than a certain threshold -
 158   //    this is to prevent large loaders (eg boot) from unnecessarily gobbling up
 159   //    all the tiny splinter chunks lambdas leave around.
 160   Metachunk* c = NULL;
 161   c = _chunks.search_chunk_ascending(preferred_level, MIN2((chunklevel_t)(preferred_level + 2), max_level), min_committed_words);
 162 
 163   // 2) Search larger committed chunks:
 164   //    If that did not yield anything, look at larger chunks, which may be committed. We would have to split
 165   //    them first, of course.
 166   if (c == NULL) {
 167     c = _chunks.search_chunk_descending(preferred_level, min_committed_words);
 168   }
 169 
 170   // 3) Search best or smaller committed chunks (second attempt):
 171   //    Repeat (1) but now consider even the tiniest chunks as long as they are large enough to hold the
 172   //    committed min size.
 173   if (c == NULL) {
 174     c = _chunks.search_chunk_ascending(preferred_level, max_level, min_committed_words);
 175   }
 176 
 177   // if we did not get anything yet, there are no free chunks commmitted enough. Repeat search but look for uncommitted chunks too:
 178 
 179   // 4) Search best or smaller chunks, can be uncommitted:
 180   if (c == NULL) {
 181     c = _chunks.search_chunk_ascending(preferred_level, max_level, 0);
 182   }
 183 
 184   // 5) Search a larger uncommitted chunk:
 185   if (c == NULL) {
 186     c = _chunks.search_chunk_descending(preferred_level, 0);
 187   }
 188 
 189   if (c != NULL) {
 190     UL(trace, "taken from freelist.");
 191   }
 192 
 193   // Failing all that, allocate a new root chunk from the connected virtual space.
 194   // This may fail if the underlying vslist cannot be expanded (e.g. compressed class space)
 195   if (c == NULL) {
 196     c = _vslist->allocate_root_chunk();
 197     if (c == NULL) {
 198       UL(info, "failed to get new root chunk.");
 199     } else {
 200       assert(c->level() == chunklevel::ROOT_CHUNK_LEVEL, "root chunk expected");
 201       UL(debug, "allocated new root chunk.");
 202     }
 203   }
 204 
 205   if (c == NULL) {
 206     // If we end up here, we found no match in the freelists and were unable to get a new
 207     // root chunk (so we used up all address space, e.g. out of CompressedClassSpace).
 208     UL2(info, "failed to get chunk (preferred level: " CHKLVL_FORMAT
 209        ", max level " CHKLVL_FORMAT ".", preferred_level, max_level);
 210     c = NULL;
 211   }
 212 
 213   if (c != NULL) {
 214 
 215     // Now we have a chunk.
 216     //  It may be larger than what the caller wanted, so we may want to split it. This should
 217     //  always work.
 218     if (c->level() < preferred_level) {
 219       split_chunk_and_add_splinters(c, preferred_level);
 220       assert(c->level() == preferred_level, "split failed?");
 221     }
 222 
 223     // Attempt to commit the chunk (depending on settings, we either fully commit it or just
 224     //  commit enough to get the caller going). That may fail if we hit a commit limit. In
 225     //  that case put the chunk back to the freelist (re-merging it with its neighbors if we
 226     //  did split it) and return NULL.
 227     const size_t to_commit = Settings::new_chunks_are_fully_committed() ? c->word_size() : min_committed_words;
 228     if (c->committed_words() < to_commit) {
 229       if (c->ensure_committed_locked(to_commit) == false) {
 230         UL2(info, "failed to commit " SIZE_FORMAT " words on chunk " METACHUNK_FORMAT ".",
 231             to_commit,  METACHUNK_FORMAT_ARGS(c));
 232         c->set_in_use(); // gets asserted in return_chunk().
 233         return_chunk_locked(c);
 234         c = NULL;
 235       }
 236     }
 237 
 238     if (c != NULL) {
 239 
 240       // Still here? We have now a good chunk, all is well.
 241       assert(c->committed_words() >= min_committed_words, "Sanity");
 242 
 243       // Any chunk returned from ChunkManager shall be marked as in use.
 244       c->set_in_use();
 245 
 246       UL2(debug, "handing out chunk " METACHUNK_FORMAT ".", METACHUNK_FORMAT_ARGS(c));
 247 
 248       InternalStats::inc_num_chunks_taken_from_freelist();
 249 
 250       SOMETIMES(c->vsnode()->verify_locked(true);)
 251 
 252     }
 253 
 254   }
 255 
 256   DEBUG_ONLY(verify_locked(false);)
 257 
 258   return c;
 259 
 260 }
 261 
 262 
 263 // Return a single chunk to the ChunkManager and adjust accounting. May merge chunk
 264 //  with neighbors.
 265 // As a side effect this removes the chunk from whatever list it has been in previously.
 266 // Happens after a Classloader was unloaded and releases its metaspace chunks.
 267 // !! Note: this may invalidate the chunk. Do not access the chunk after
 268 //    this function returns !!
 269 void ChunkManager::return_chunk(Metachunk* c) {
 270   MutexLocker fcl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
 271   return_chunk_locked(c);
 272 }
 273 
 274 // See return_chunk().
 275 void ChunkManager::return_chunk_locked(Metachunk* c) {
 276 
 277   assert_lock_strong(MetaspaceExpand_lock);
 278 
 279   UL2(debug, ": returning chunk " METACHUNK_FORMAT ".", METACHUNK_FORMAT_ARGS(c));
 280 
 281   DEBUG_ONLY(c->verify(true);)
 282 
 283   assert(contains_chunk(c) == false, "A chunk to be added to the freelist must not be in the freelist already.");
 284 
 285   assert(c->is_in_use(), "Unexpected chunk state");
 286   assert(!c->in_list(), "Remove from list first");
 287   c->set_free();
 288   c->reset_used_words();
 289 
 290   const chunklevel_t orig_lvl = c->level();
 291 
 292   Metachunk* merged = NULL;
 293   if (!c->is_root_chunk()) {
 294     // Only attempt merging if we are not of the lowest level already.
 295     merged = c->vsnode()->merge(c, &_chunks);
 296   }
 297 
 298   if (merged != NULL) {
 299 
 300     InternalStats::inc_num_chunk_merges();
 301 
 302     DEBUG_ONLY(merged->verify(false));
 303 
 304     // We did merge our chunk into a different chunk.
 305 
 306     // We did merge chunks and now have a bigger chunk.
 307     assert(merged->level() < orig_lvl, "Sanity");
 308 
 309     UL2(debug, "merged into chunk " METACHUNK_FORMAT ".", METACHUNK_FORMAT_ARGS(merged));
 310 
 311     c = merged;
 312 
 313   }
 314 
 315   if (Settings::uncommit_free_chunks() &&
 316       c->word_size() >= Settings::commit_granule_words())
 317   {
 318     UL2(debug, "uncommitting free chunk " METACHUNK_FORMAT ".", METACHUNK_FORMAT_ARGS(c));
 319     c->uncommit_locked();
 320   }
 321 
 322   return_chunk_simple_locked(c);
 323 
 324   DEBUG_ONLY(verify_locked(false);)
 325   SOMETIMES(c->vsnode()->verify_locked(true);)
 326 
 327   InternalStats::inc_num_chunks_returned_to_freelist();
 328 
 329 }
 330 
 331 // Given a chunk c, whose state must be "in-use" and must not be a root chunk, attempt to
 332 // enlarge it in place by claiming its trailing buddy.
 333 //
 334 // This will only work if c is the leader of the buddy pair and the trailing buddy is free.
 335 //
 336 // If successful, the follower chunk will be removed from the freelists, the leader chunk c will
 337 // double in size (level decreased by one).
 338 //
 339 // On success, true is returned, false otherwise.
 340 bool ChunkManager::attempt_enlarge_chunk(Metachunk* c) {
 341   MutexLocker fcl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
 342   return c->vsnode()->attempt_enlarge_chunk(c, &_chunks);
 343 }
 344 
 345 static void print_word_size_delta(outputStream* st, size_t word_size_1, size_t word_size_2) {
 346   if (word_size_1 == word_size_2) {
 347     print_scaled_words(st, word_size_1);
 348     st->print (" (no change)");
 349   } else {
 350     print_scaled_words(st, word_size_1);
 351     st->print("->");
 352     print_scaled_words(st, word_size_2);
 353     st->print(" (");
 354     if (word_size_2 <= word_size_1) {
 355       st->print("-");
 356       print_scaled_words(st, word_size_1 - word_size_2);
 357     } else {
 358       st->print("+");
 359       print_scaled_words(st, word_size_2 - word_size_1);
 360     }
 361     st->print(")");
 362   }
 363 }
 364 
 365 void ChunkManager::purge() {
 366 
 367   MutexLocker fcl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
 368 
 369   UL(info, ": reclaiming memory...");
 370 
 371   const size_t reserved_before = _vslist->reserved_words();
 372   const size_t committed_before = _vslist->committed_words();
 373   int num_nodes_purged = 0;
 374 
 375   // 1) purge virtual space list
 376   num_nodes_purged = _vslist->purge(&_chunks);
 377   InternalStats::inc_num_purges();
 378 
 379   // 2) uncommit free chunks
 380   if (Settings::uncommit_free_chunks()) {
 381     const chunklevel_t max_level =
 382         chunklevel::level_fitting_word_size(Settings::commit_granule_words());
 383     for (chunklevel_t l = chunklevel::LOWEST_CHUNK_LEVEL;
 384          l <= max_level;
 385          l ++)
 386     {
 387       // Since we uncommit all chunks at this level, we do not break the "committed chunks are
 388       //  at the front of the list" condition.
 389       for (Metachunk* c = _chunks.first_at_level(l); c != NULL; c = c->next()) {
 390         c->uncommit_locked();
 391       }
 392     }
 393   }
 394 
 395   const size_t reserved_after = _vslist->reserved_words();
 396   const size_t committed_after = _vslist->committed_words();
 397 
 398   // Print a nice report.
 399   if (reserved_after == reserved_before && committed_after == committed_before) {
 400     UL(info, "nothing reclaimed.");
 401   } else {
 402     LogTarget(Info, metaspace) lt;
 403     if (lt.is_enabled()) {
 404       LogStream ls(lt);
 405       ls.print_cr(LOGFMT ": finished reclaiming memory: ", LOGFMT_ARGS);
 406 
 407       ls.print("reserved: ");
 408       print_word_size_delta(&ls, reserved_before, reserved_after);
 409       ls.cr();
 410 
 411       ls.print("committed: ");
 412       print_word_size_delta(&ls, committed_before, committed_after);
 413       ls.cr();
 414 
 415       ls.print_cr("full nodes purged: %d", num_nodes_purged);
 416     }
 417   }
 418 
 419   DEBUG_ONLY(_vslist->verify_locked(true));
 420   DEBUG_ONLY(verify_locked(true));
 421 
 422 }
 423 
 424 // Convenience methods to return the global class-space chunkmanager
 425 //  and non-class chunkmanager, respectively.
 426 ChunkManager* ChunkManager::chunkmanager_class() {
 427   return MetaspaceContext::context_class() == NULL ? NULL : MetaspaceContext::context_class()->cm();
 428 }
 429 
 430 ChunkManager* ChunkManager::chunkmanager_nonclass() {
 431   return MetaspaceContext::context_nonclass() == NULL ? NULL : MetaspaceContext::context_nonclass()->cm();
 432 }
 433 
 434 // Update statistics.
 435 void ChunkManager::add_to_statistics(cm_stats_t* out) const {
 436 
 437   MutexLocker fcl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
 438 
 439   for (chunklevel_t l = chunklevel::ROOT_CHUNK_LEVEL; l <= chunklevel::HIGHEST_CHUNK_LEVEL; l ++) {
 440     out->num_chunks[l] += _chunks.num_chunks_at_level(l);
 441     out->committed_word_size[l] += _chunks.committed_word_size_at_level(l);
 442   }
 443 
 444   DEBUG_ONLY(out->verify();)
 445 
 446 }
 447 
 448 #ifdef ASSERT
 449 
 450 void ChunkManager::verify(bool slow) const {
 451   MutexLocker fcl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
 452   verify_locked(slow);
 453 }
 454 
 455 void ChunkManager::verify_locked(bool slow) const {
 456   assert_lock_strong(MetaspaceExpand_lock);
 457   assert(_vslist != NULL, "No vslist");
 458   _chunks.verify();
 459 }
 460 
 461 bool ChunkManager::contains_chunk(Metachunk* c) const {
 462   return _chunks.contains(c);
 463 }
 464 
 465 #endif // ASSERT
 466 
 467 void ChunkManager::print_on(outputStream* st) const {
 468   MutexLocker fcl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
 469   print_on_locked(st);
 470 }
 471 
 472 void ChunkManager::print_on_locked(outputStream* st) const {
 473   assert_lock_strong(MetaspaceExpand_lock);
 474   st->print_cr("cm %s: %d chunks, total word size: " SIZE_FORMAT ", committed word size: " SIZE_FORMAT, _name,
 475                total_num_chunks(), total_word_size(), _chunks.committed_word_size());
 476   _chunks.print_on(st);
 477 }
 478 
 479 } // namespace metaspace