1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 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 #include "memory/virtualspace.hpp"
  29 #include "memory/metaspace/virtualSpaceList.hpp"
  30 #include "memory/metaspace/chunkManager.hpp"
  31 #include "memory/metaspace/arenaGrowthPolicy.hpp"
  32 #include "memory/metaspace/commitLimiter.hpp"
  33 #include "memory/metaspace/metaspace_test.hpp"
  34 #include "memory/metaspace/metaspaceArena.hpp"
  35 #include "memory/metaspace/metaspaceContext.hpp"
  36 #include "memory/metaspace/runningCounters.hpp"
  37 #include "runtime/mutexLocker.hpp"
  38 #include "utilities/debug.hpp"
  39 #include "utilities/ostream.hpp"
  40 #include "utilities/globalDefinitions.hpp"
  41 
  42 namespace metaspace {
  43 
  44 ///// MetaspaceTestArena //////
  45 
  46 MetaspaceTestArena::MetaspaceTestArena(Mutex* lock, MetaspaceArena* arena)
  47   : _lock(lock), _arena(arena) {}
  48 
  49 MetaspaceTestArena::~MetaspaceTestArena() {
  50   delete _arena;
  51   delete _lock;
  52 }
  53 
  54 MetaWord* MetaspaceTestArena::allocate(size_t word_size) {
  55   return _arena->allocate(word_size);
  56 }
  57 
  58 void MetaspaceTestArena::deallocate(MetaWord* p, size_t word_size) {
  59   return _arena->deallocate(p, word_size);
  60 }
  61 
  62 ///// MetaspaceTestArea //////
  63 
  64 MetaspaceTestContext::MetaspaceTestContext(const char* name, size_t commit_limit, size_t reserve_limit)
  65   : _name(name), _reserve_limit(reserve_limit), _commit_limit(commit_limit),
  66     _context(NULL),
  67     _commit_limiter(commit_limit == 0 ? max_uintx : commit_limit), // commit_limit == 0 -> no limit
  68     _used_words_counter()
  69 {
  70   assert(is_aligned(reserve_limit, Metaspace::reserve_alignment_words()), "reserve_limit (" SIZE_FORMAT ") "
  71                     "not aligned to metaspace reserve alignment (" SIZE_FORMAT ")",
  72                     reserve_limit, Metaspace::reserve_alignment_words());
  73   if (reserve_limit > 0) {
  74     // have reserve limit -> non-expandable context
  75     ReservedSpace rs(reserve_limit * BytesPerWord, Metaspace::reserve_alignment(), false);
  76     _context = MetaspaceContext::create_nonexpandable_context(name, rs, &_commit_limiter);
  77   } else {
  78     // no reserve limit -> expandable vslist
  79     _context = MetaspaceContext::create_expandable_context(name, &_commit_limiter);
  80   }
  81 
  82 }
  83 
  84 MetaspaceTestContext::~MetaspaceTestContext() {
  85   DEBUG_ONLY(verify(true);)
  86   MutexLocker fcl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
  87   delete _context;
  88 }
  89 
  90 // Create an arena, feeding off this area.
  91 MetaspaceTestArena* MetaspaceTestContext::create_arena(Metaspace::MetaspaceType type) {
  92   const ArenaGrowthPolicy* growth_policy = ArenaGrowthPolicy::policy_for_space_type(type, false);
  93   Mutex* lock = new Mutex(Monitor::native, "MetaspaceTestArea-lock", false, Monitor::_safepoint_check_never);
  94   MetaspaceArena* arena = NULL;
  95   {
  96     MutexLocker ml(lock,  Mutex::_no_safepoint_check_flag);
  97     arena = new MetaspaceArena(_context->cm(), growth_policy, lock, &_used_words_counter, _name);
  98   }
  99   return new MetaspaceTestArena(lock, arena);
 100 }
 101 
 102 void MetaspaceTestContext::purge_area() {
 103   _context->cm()->purge();
 104 }
 105 
 106 #ifdef ASSERT
 107 void MetaspaceTestContext::verify(bool slow) const {
 108   if (_context != NULL) {
 109     _context->verify(slow);
 110   }
 111 }
 112 #endif
 113 
 114 void MetaspaceTestContext::print_on(outputStream* st) const {
 115   _context->print_on(st);
 116 }
 117 
 118 } // namespace metaspace
 119