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 #ifndef SHARE_MEMORY_METASPACE_METASPACE_TEST_HPP
  27 #define SHARE_MEMORY_METASPACE_METASPACE_TEST_HPP
  28 
  29 
  30 #include "memory/allocation.hpp"
  31 #include "memory/metaspace.hpp"
  32 #include "memory/metaspace/commitLimiter.hpp"
  33 #include "memory/metaspace/counter.hpp"
  34 #include "memory/metaspace/metaspaceContext.hpp"
  35 #include "memory/virtualspace.hpp"
  36 #include "utilities/globalDefinitions.hpp"
  37 
  38 // This is just convenience classes for metaspace-related tests
  39 //  (jtreg, via whitebox API, and gtests)
  40 
  41 class ReservedSpace;
  42 class Mutex;
  43 class outputStream;
  44 
  45 namespace metaspace {
  46 
  47 class MetaspaceContext;
  48 class MetaspaceArena;
  49 
  50 
  51 // Wraps a MetaspaceTestArena with its own lock for testing purposes.
  52 class MetaspaceTestArena : public CHeapObj<mtInternal> {
  53 
  54   Mutex* const _lock;
  55   MetaspaceArena* const _arena;
  56 
  57 public:
  58 
  59   const MetaspaceArena* arena() const {
  60     return _arena;
  61   }
  62 
  63   MetaspaceTestArena(Mutex* lock, MetaspaceArena* arena);
  64   ~MetaspaceTestArena();
  65 
  66   MetaWord* allocate(size_t word_size);
  67   void deallocate(MetaWord* p, size_t word_size);
  68 
  69 };
  70 
  71 
  72 // Wraps an instance of a MetaspaceContext together with some side objects for easy use in test beds (whitebox, gtests)
  73 class MetaspaceTestContext : public CHeapObj<mtInternal> {
  74 
  75   const char* const _name;
  76   const size_t _reserve_limit;
  77   const size_t _commit_limit;
  78 
  79   MetaspaceContext* _context;
  80   CommitLimiter _commit_limiter;
  81   SizeAtomicCounter _used_words_counter;
  82 
  83 public:
  84 
  85   // Note: limit == 0 means unlimited
  86   // Reserve limit > 0 simulates a non-expandable VirtualSpaceList (like CompressedClassSpace)
  87   // Commit limit > 0 simulates a limit to max commitable space (like MaxMetaspaceSize)
  88   MetaspaceTestContext(const char* name, size_t commit_limit = 0, size_t reserve_limit = 0);
  89   ~MetaspaceTestContext();
  90 
  91   // Create an arena, feeding off this area.
  92   MetaspaceTestArena* create_arena(Metaspace::MetaspaceType type);
  93 
  94   void purge_area();
  95 
  96   // Accessors
  97   const CommitLimiter& commit_limiter() const { return _commit_limiter; }
  98   const VirtualSpaceList& vslist() const      { return *(_context->vslist()); }
  99   ChunkManager& cm()                          { return *(_context->cm()); }
 100 
 101   // Returns reserve- and commit limit we run the test with (in the real world,
 102   // these would be equivalent to CompressedClassSpaceSize resp MaxMetaspaceSize)
 103   size_t reserve_limit() const    { return _reserve_limit == 0 ? max_uintx : 0; }
 104   size_t commit_limit() const     { return _commit_limit == 0 ? max_uintx : 0; }
 105 
 106   // Convenience function to retrieve total committed/used words
 107   size_t used_words() const       { return _used_words_counter.get(); }
 108   size_t committed_words() const  { return _commit_limiter.committed_words(); }
 109 
 110   DEBUG_ONLY(void verify(bool slow = false) const;)
 111 
 112   void print_on(outputStream* st) const;
 113 
 114 };
 115 
 116 
 117 } // namespace metaspace
 118 
 119 #endif // SHARE_MEMORY_METASPACE_METASPACE_TEST_HPP
 120