< prev index next >

src/hotspot/share/memory/metaspace/testHelpers.cpp

Print this page

 37 
 38 namespace metaspace {
 39 
 40 ///// MetaspaceTestArena //////
 41 
 42 MetaspaceTestArena::MetaspaceTestArena(Mutex* lock, MetaspaceArena* arena) :
 43   _lock(lock),
 44   _arena(arena)
 45 {}
 46 
 47 MetaspaceTestArena::~MetaspaceTestArena() {
 48   {
 49     MutexLocker fcl(_lock, Mutex::_no_safepoint_check_flag);
 50     delete _arena;
 51   }
 52   delete _lock;
 53 }
 54 
 55 MetaWord* MetaspaceTestArena::allocate(size_t word_size) {
 56   MutexLocker fcl(_lock, Mutex::_no_safepoint_check_flag);
 57   return _arena->allocate(word_size);





 58 }
 59 
 60 void MetaspaceTestArena::deallocate(MetaWord* p, size_t word_size) {
 61   MutexLocker fcl(_lock, Mutex::_no_safepoint_check_flag);
 62   return _arena->deallocate(p, word_size);
 63 }
 64 
 65 ///// MetaspaceTestArea //////
 66 
 67 MetaspaceTestContext::MetaspaceTestContext(const char* name, size_t commit_limit, size_t reserve_limit) :
 68   _name(name),
 69   _reserve_limit(reserve_limit),
 70   _commit_limit(commit_limit),
 71   _context(nullptr),
 72   _commit_limiter(commit_limit == 0 ? max_uintx : commit_limit), // commit_limit == 0 -> no limit
 73   _used_words_counter(),
 74   _rs()
 75 {
 76   assert(is_aligned(reserve_limit, Metaspace::reserve_alignment_words()), "reserve_limit (" SIZE_FORMAT ") "
 77                     "not aligned to metaspace reserve alignment (" SIZE_FORMAT ")",
 78                     reserve_limit, Metaspace::reserve_alignment_words());
 79   if (reserve_limit > 0) {
 80     // have reserve limit -> non-expandable context
 81     _rs = ReservedSpace(reserve_limit * BytesPerWord, Metaspace::reserve_alignment(), os::vm_page_size());
 82     _context = MetaspaceContext::create_nonexpandable_context(name, _rs, &_commit_limiter);
 83   } else {
 84     // no reserve limit -> expandable vslist
 85     _context = MetaspaceContext::create_expandable_context(name, &_commit_limiter);
 86   }
 87 
 88 }
 89 
 90 MetaspaceTestContext::~MetaspaceTestContext() {
 91   DEBUG_ONLY(verify();)
 92   MutexLocker fcl(Metaspace_lock, Mutex::_no_safepoint_check_flag);
 93   delete _context;
 94   if (_rs.is_reserved()) {
 95     _rs.release();
 96   }
 97 }
 98 
 99 // Create an arena, feeding off this area.
100 MetaspaceTestArena* MetaspaceTestContext::create_arena(Metaspace::MetaspaceType type) {
101   const ArenaGrowthPolicy* growth_policy = ArenaGrowthPolicy::policy_for_space_type(type, false);
102   Mutex* lock = new Mutex(Monitor::nosafepoint, "MetaspaceTestArea_lock");
103   MetaspaceArena* arena = nullptr;
104   {
105     MutexLocker ml(lock,  Mutex::_no_safepoint_check_flag);
106     arena = new MetaspaceArena(_context->cm(), growth_policy, &_used_words_counter, _name);
107   }
108   return new MetaspaceTestArena(lock, arena);
109 }
110 
111 void MetaspaceTestContext::purge_area() {
112   _context->cm()->purge();
113 }
114 
115 #ifdef ASSERT
116 void MetaspaceTestContext::verify() const {
117   if (_context != nullptr) {
118     _context->verify();
119   }
120 }
121 #endif
122 
123 void MetaspaceTestContext::print_on(outputStream* st) const {
124   _context->print_on(st);
125 }
126 













127 } // namespace metaspace
128 

 37 
 38 namespace metaspace {
 39 
 40 ///// MetaspaceTestArena //////
 41 
 42 MetaspaceTestArena::MetaspaceTestArena(Mutex* lock, MetaspaceArena* arena) :
 43   _lock(lock),
 44   _arena(arena)
 45 {}
 46 
 47 MetaspaceTestArena::~MetaspaceTestArena() {
 48   {
 49     MutexLocker fcl(_lock, Mutex::_no_safepoint_check_flag);
 50     delete _arena;
 51   }
 52   delete _lock;
 53 }
 54 
 55 MetaWord* MetaspaceTestArena::allocate(size_t word_size) {
 56   MutexLocker fcl(_lock, Mutex::_no_safepoint_check_flag);
 57   MetaBlock result, wastage;
 58   result = _arena->allocate(word_size, wastage);
 59   if (wastage.is_nonempty()) {
 60     _arena->deallocate(wastage);
 61   }
 62   return result.base();
 63 }
 64 
 65 void MetaspaceTestArena::deallocate(MetaWord* p, size_t word_size) {
 66   MutexLocker fcl(_lock, Mutex::_no_safepoint_check_flag);
 67   _arena->deallocate(MetaBlock(p, word_size));
 68 }
 69 
 70 ///// MetaspaceTestArea //////
 71 
 72 MetaspaceTestContext::MetaspaceTestContext(const char* name, size_t commit_limit, size_t reserve_limit) :
 73   _name(name),
 74   _reserve_limit(reserve_limit),
 75   _commit_limit(commit_limit),
 76   _context(nullptr),
 77   _commit_limiter(commit_limit == 0 ? max_uintx : commit_limit), // commit_limit == 0 -> no limit

 78   _rs()
 79 {
 80   assert(is_aligned(reserve_limit, Metaspace::reserve_alignment_words()), "reserve_limit (" SIZE_FORMAT ") "
 81                     "not aligned to metaspace reserve alignment (" SIZE_FORMAT ")",
 82                     reserve_limit, Metaspace::reserve_alignment_words());
 83   if (reserve_limit > 0) {
 84     // have reserve limit -> non-expandable context
 85     _rs = ReservedSpace(reserve_limit * BytesPerWord, Metaspace::reserve_alignment(), os::vm_page_size());
 86     _context = MetaspaceContext::create_nonexpandable_context(name, _rs, &_commit_limiter);
 87   } else {
 88     // no reserve limit -> expandable vslist
 89     _context = MetaspaceContext::create_expandable_context(name, &_commit_limiter);
 90   }
 91 
 92 }
 93 
 94 MetaspaceTestContext::~MetaspaceTestContext() {
 95   DEBUG_ONLY(verify();)
 96   MutexLocker fcl(Metaspace_lock, Mutex::_no_safepoint_check_flag);
 97   delete _context;
 98   if (_rs.is_reserved()) {
 99     _rs.release();
100   }
101 }
102 
103 // Create an arena, feeding off this area.
104 MetaspaceTestArena* MetaspaceTestContext::create_arena(Metaspace::MetaspaceType type) {
105   const ArenaGrowthPolicy* growth_policy = ArenaGrowthPolicy::policy_for_space_type(type, false);
106   Mutex* lock = new Mutex(Monitor::nosafepoint, "MetaspaceTestArea_lock");
107   MetaspaceArena* arena = nullptr;
108   {
109     MutexLocker ml(lock,  Mutex::_no_safepoint_check_flag);
110     arena = new MetaspaceArena(_context, growth_policy, Metaspace::min_allocation_alignment_words, _name);
111   }
112   return new MetaspaceTestArena(lock, arena);
113 }
114 
115 void MetaspaceTestContext::purge_area() {
116   _context->cm()->purge();
117 }
118 
119 #ifdef ASSERT
120 void MetaspaceTestContext::verify() const {
121   if (_context != nullptr) {
122     _context->verify();
123   }
124 }
125 #endif
126 
127 void MetaspaceTestContext::print_on(outputStream* st) const {
128   _context->print_on(st);
129 }
130 
131 size_t MetaspaceTestContext::used_words() const {
132   return _context->used_words_counter()->get();
133 }
134 
135 size_t MetaspaceTestContext::committed_words() const {
136   assert(_commit_limiter.committed_words() == _context->committed_words(), "Sanity");
137   return _context->committed_words();
138 }
139 
140 size_t MetaspaceTestContext::reserved_words() const {
141   return _context->reserved_words();
142 }
143 
144 } // namespace metaspace
145 
< prev index next >