test/hotspot/gtest/memory/test_virtualSpaceNode.cpp
changeset 53955 d15d4d610de4
parent 53954 b5f4a8477a20
child 53956 d5bd4d5483e4
equal deleted inserted replaced
53954:b5f4a8477a20 53955:d15d4d610de4
     1 /*
       
     2  * Copyright (c) 2018, 2019, 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 #include "precompiled.hpp"
       
    25 #include "memory/metaspace/virtualSpaceList.hpp"
       
    26 #include "memory/metaspace/chunkManager.hpp"
       
    27 #include "runtime/mutexLocker.hpp"
       
    28 #include "utilities/formatBuffer.hpp"
       
    29 #include "unittest.hpp"
       
    30 
       
    31 // include as last, or otherwise we pull in an incompatible "assert" macro
       
    32 #include <vector>
       
    33 
       
    34 using namespace metaspace;
       
    35 
       
    36 namespace {
       
    37   static void chunk_up(size_t words_left, size_t& num_medium_chunks,
       
    38                                           size_t& num_small_chunks,
       
    39                                           size_t& num_specialized_chunks) {
       
    40     num_medium_chunks = words_left / MediumChunk;
       
    41     words_left = words_left % MediumChunk;
       
    42 
       
    43     num_small_chunks = words_left / SmallChunk;
       
    44     words_left = words_left % SmallChunk;
       
    45     // how many specialized chunks can we get?
       
    46     num_specialized_chunks = words_left / SpecializedChunk;
       
    47     ASSERT_EQ(0UL, words_left % SpecializedChunk) << "should be nothing left"
       
    48        << ", words_left = " << words_left
       
    49        << ", SpecializedChunk = " << SpecializedChunk;
       
    50   }
       
    51   static const size_t vsn_test_size_words = MediumChunk * 4;
       
    52   static const size_t vsn_test_size_bytes = vsn_test_size_words * BytesPerWord;
       
    53   class MetachunkRemover {
       
    54     Metachunk* const _m;
       
    55     ChunkManager* const _c;
       
    56    public:
       
    57     MetachunkRemover(Metachunk* m, ChunkManager* c) : _m(m), _c(c) { }
       
    58     ~MetachunkRemover() { _c->remove_chunk(_m); }
       
    59   };
       
    60 }
       
    61 
       
    62 class ChunkManagerTest {
       
    63  public:
       
    64   static size_t sum_free_chunks(ChunkManager* cm) {
       
    65       return cm->sum_free_chunks();
       
    66   }
       
    67   static size_t sum_free_chunks_count(ChunkManager* cm) {
       
    68       return cm->sum_free_chunks_count();
       
    69   }
       
    70   static ChunkList* free_chunks(ChunkManager* cm, ChunkIndex i) {
       
    71     return cm->free_chunks(i);
       
    72   }
       
    73 };
       
    74 
       
    75 // removes all the chunks added to the ChunkManager since creation of ChunkManagerRestorer
       
    76 class ChunkManagerRestorer {
       
    77   metaspace::ChunkManager* const _cm;
       
    78   std::vector<metaspace::Metachunk*>* _free_chunks[metaspace::NumberOfFreeLists];
       
    79   int _count_pre_existing;
       
    80 public:
       
    81   ChunkManagerRestorer(metaspace::ChunkManager* cm) : _cm(cm), _count_pre_existing(0) {
       
    82     _cm->locked_verify();
       
    83     for (metaspace::ChunkIndex i = metaspace::ZeroIndex; i < metaspace::NumberOfFreeLists; i = next_chunk_index(i)) {
       
    84       metaspace::ChunkList* l = ChunkManagerTest::free_chunks(_cm, i);
       
    85       _count_pre_existing += l->count();
       
    86       std::vector<metaspace::Metachunk*> *v = new std::vector<metaspace::Metachunk*>(l->count());
       
    87       metaspace::Metachunk* c = l->head();
       
    88       while (c) {
       
    89         v->push_back(c);
       
    90         c = c->next();
       
    91       }
       
    92       _free_chunks[i] = v;
       
    93     }
       
    94   }
       
    95   ~ChunkManagerRestorer() {
       
    96     _cm->locked_verify();
       
    97     for (metaspace::ChunkIndex i = metaspace::ZeroIndex; i < metaspace::NumberOfFreeLists; i = next_chunk_index(i)) {
       
    98       metaspace::ChunkList* l = ChunkManagerTest::free_chunks(_cm, i);
       
    99       std::vector<metaspace::Metachunk*> *v = _free_chunks[i];
       
   100       ssize_t count = l->count();
       
   101       for (ssize_t j = 0; j < count; j++) {
       
   102         metaspace::Metachunk* c = l->head();
       
   103         while (c) {
       
   104           bool found = false;
       
   105           for (size_t k = 0; k < v->size() && !found; k++) {
       
   106             found = (c == v->at(k));
       
   107           }
       
   108           if (found) {
       
   109             c = c->next();
       
   110           } else {
       
   111             _cm->remove_chunk(c);
       
   112             break;
       
   113           }
       
   114         }
       
   115       }
       
   116       delete _free_chunks[i];
       
   117       _free_chunks[i] = NULL;
       
   118    }
       
   119     int count_after_cleanup = 0;
       
   120     for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
       
   121       ChunkList* l = ChunkManagerTest::free_chunks(_cm, i);
       
   122       count_after_cleanup += l->count();
       
   123     }
       
   124     EXPECT_EQ(_count_pre_existing, count_after_cleanup);
       
   125     _cm->locked_verify();
       
   126   }
       
   127 };
       
   128 
       
   129 TEST_VM(VirtualSpaceNodeTest, sanity) {
       
   130   // The chunk sizes must be multiples of eachother, or this will fail
       
   131   STATIC_ASSERT(MediumChunk % SmallChunk == 0);
       
   132   STATIC_ASSERT(SmallChunk % SpecializedChunk == 0);
       
   133 
       
   134   // just in case STATIC_ASSERT doesn't work
       
   135   EXPECT_EQ(0, MediumChunk % SmallChunk);
       
   136   EXPECT_EQ(0, SmallChunk % SpecializedChunk);
       
   137 }
       
   138 
       
   139 TEST_VM(VirtualSpaceNodeTest, four_pages_vsn_is_committed_some_is_used_by_chunks) {
       
   140   const size_t page_chunks = 4 * (size_t)os::vm_page_size() / BytesPerWord;
       
   141   if (page_chunks >= MediumChunk) {
       
   142     SUCCEED() << "SKIP: This doesn't work for systems with vm_page_size >= 16K";
       
   143     return;
       
   144   }
       
   145   MutexLockerEx ml(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
       
   146   ChunkManager cm(false);
       
   147   VirtualSpaceNode vsn(false, vsn_test_size_bytes);
       
   148   ChunkManagerRestorer c(Metaspace::get_chunk_manager(false));
       
   149 
       
   150   vsn.initialize();
       
   151   EXPECT_TRUE(vsn.expand_by(page_chunks, page_chunks));
       
   152   vsn.get_chunk_vs(SmallChunk);
       
   153   vsn.get_chunk_vs(SpecializedChunk);
       
   154   vsn.retire(&cm);
       
   155 
       
   156   // committed - used = words left to retire
       
   157   const size_t words_left = page_chunks - SmallChunk - SpecializedChunk;
       
   158   size_t num_medium_chunks, num_small_chunks, num_spec_chunks;
       
   159   chunk_up(words_left, num_medium_chunks, num_small_chunks, num_spec_chunks);
       
   160 
       
   161   EXPECT_EQ(0UL, num_medium_chunks) << "should not get any medium chunks";
       
   162   // DISABLED: checks started to fail after 8198423
       
   163   // EXPECT_EQ((num_small_chunks + num_spec_chunks), ChunkManagerTest::sum_free_chunks_count(&cm)) << "should be space for 3 chunks";
       
   164   // EXPECT_EQ(words_left, ChunkManagerTest::sum_free_chunks(&cm)) << "sizes should add up";
       
   165 }
       
   166 
       
   167 TEST_VM(VirtualSpaceNodeTest, half_vsn_is_committed_humongous_chunk_is_used) {
       
   168   MutexLockerEx ml(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
       
   169   ChunkManager cm(false);
       
   170   VirtualSpaceNode vsn(false, vsn_test_size_bytes);
       
   171   ChunkManagerRestorer c(Metaspace::get_chunk_manager(false));
       
   172 
       
   173   vsn.initialize();
       
   174   EXPECT_TRUE(vsn.expand_by(MediumChunk * 2, MediumChunk * 2));
       
   175   // Humongous chunks will be aligned up to MediumChunk + SpecializedChunk
       
   176   vsn.get_chunk_vs(MediumChunk + SpecializedChunk);
       
   177   vsn.retire(&cm);
       
   178 
       
   179   const size_t words_left = MediumChunk * 2 - (MediumChunk + SpecializedChunk);
       
   180   size_t num_medium_chunks, num_small_chunks, num_spec_chunks;
       
   181   ASSERT_NO_FATAL_FAILURE(chunk_up(words_left, num_medium_chunks, num_small_chunks, num_spec_chunks));
       
   182 
       
   183   EXPECT_EQ(0UL, num_medium_chunks) << "should not get any medium chunks";
       
   184   // DISABLED: checks started to fail after 8198423
       
   185   // EXPECT_EQ((num_small_chunks + num_spec_chunks), ChunkManagerTest::sum_free_chunks_count(&cm)) << "should be space for 3 chunks";
       
   186   // EXPECT_EQ(words_left, ChunkManagerTest::sum_free_chunks(&cm)) << "sizes should add up";
       
   187 }
       
   188 
       
   189 TEST_VM(VirtualSpaceNodeTest, all_vsn_is_committed_half_is_used_by_chunks) {
       
   190   MutexLockerEx ml(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
       
   191   ChunkManager cm(false);
       
   192   VirtualSpaceNode vsn(false, vsn_test_size_bytes);
       
   193   ChunkManagerRestorer c(Metaspace::get_chunk_manager(false));
       
   194 
       
   195   vsn.initialize();
       
   196   EXPECT_TRUE(vsn.expand_by(vsn_test_size_words, vsn_test_size_words));
       
   197   vsn.get_chunk_vs(MediumChunk);
       
   198   vsn.get_chunk_vs(MediumChunk);
       
   199   vsn.retire(&cm);
       
   200 
       
   201   // DISABLED: checks started to fail after 8198423
       
   202   // EXPECT_EQ(2UL, ChunkManagerTest::sum_free_chunks_count(&cm)) << "should have been memory left for 2 chunks";
       
   203   // EXPECT_EQ(2UL * MediumChunk, ChunkManagerTest::sum_free_chunks(&cm)) << "sizes should add up";
       
   204 }
       
   205 
       
   206 TEST_VM(VirtualSpaceNodeTest, no_committed_memory) {
       
   207   MutexLockerEx ml(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
       
   208   ChunkManager cm(false);
       
   209   VirtualSpaceNode vsn(false, vsn_test_size_bytes);
       
   210   ChunkManagerRestorer c(Metaspace::get_chunk_manager(false));
       
   211 
       
   212   vsn.initialize();
       
   213   vsn.retire(&cm);
       
   214 
       
   215   ASSERT_EQ(0UL, ChunkManagerTest::sum_free_chunks_count(&cm)) << "did not commit any memory in the VSN";
       
   216 }
       
   217 
       
   218 TEST_VM(VirtualSpaceNodeTest, is_available_positive) {
       
   219   // Reserve some memory.
       
   220   VirtualSpaceNode vsn(false, os::vm_allocation_granularity());
       
   221   ASSERT_TRUE(vsn.initialize()) << "Failed to setup VirtualSpaceNode";
       
   222 
       
   223   // Commit some memory.
       
   224   size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
       
   225   ASSERT_TRUE(vsn.expand_by(commit_word_size, commit_word_size))
       
   226       << "Failed to commit, commit_word_size = " << commit_word_size;
       
   227 
       
   228   SCOPED_TRACE(err_msg("VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")",
       
   229       p2i(vsn.bottom()), p2i(vsn.end())).buffer());
       
   230 
       
   231   // Check that is_available accepts the committed size.
       
   232   EXPECT_TRUE(vsn.is_available(commit_word_size)) << " commit_word_size = " << commit_word_size;
       
   233 
       
   234   // Check that is_available accepts half the committed size.
       
   235   size_t expand_word_size = commit_word_size / 2;
       
   236   EXPECT_TRUE(vsn.is_available(expand_word_size)) << " expand_word_size = " << expand_word_size;
       
   237 }
       
   238 
       
   239 TEST_VM(VirtualSpaceNodeTest, is_available_negative) {
       
   240   // Reserve some memory.
       
   241   VirtualSpaceNode vsn(false, os::vm_allocation_granularity());
       
   242   ASSERT_TRUE(vsn.initialize()) << "Failed to setup VirtualSpaceNode";
       
   243 
       
   244   // Commit some memory.
       
   245   size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
       
   246   ASSERT_TRUE(vsn.expand_by(commit_word_size, commit_word_size))
       
   247       << "Failed to commit, commit_word_size = " << commit_word_size;
       
   248 
       
   249   SCOPED_TRACE(err_msg("VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")",
       
   250       p2i(vsn.bottom()), p2i(vsn.end())).buffer());
       
   251 
       
   252   // Check that is_available doesn't accept a too large size.
       
   253   size_t two_times_commit_word_size = commit_word_size * 2;
       
   254   EXPECT_FALSE(vsn.is_available(two_times_commit_word_size)) << " two_times_commit_word_size = " << two_times_commit_word_size;
       
   255 }
       
   256 
       
   257 TEST_VM(VirtualSpaceNodeTest, is_available_overflow) {
       
   258   // Reserve some memory.
       
   259   VirtualSpaceNode vsn(false, os::vm_allocation_granularity());
       
   260   ASSERT_TRUE(vsn.initialize()) << "Failed to setup VirtualSpaceNode";
       
   261 
       
   262   // Commit some memory.
       
   263   size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
       
   264   ASSERT_TRUE(vsn.expand_by(commit_word_size, commit_word_size))
       
   265       << "Failed to commit, commit_word_size = " << commit_word_size;
       
   266 
       
   267   SCOPED_TRACE(err_msg("VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")",
       
   268       p2i(vsn.bottom()), p2i(vsn.end())).buffer());
       
   269 
       
   270   // Calculate a size that will overflow the virtual space size.
       
   271   void* virtual_space_max = (void*)(uintptr_t)-1;
       
   272   size_t bottom_to_max = pointer_delta(virtual_space_max, vsn.bottom(), 1);
       
   273   size_t overflow_size = bottom_to_max + BytesPerWord;
       
   274   size_t overflow_word_size = overflow_size / BytesPerWord;
       
   275 
       
   276   EXPECT_FALSE(vsn.is_available(overflow_word_size)) << " overflow_word_size = " << overflow_word_size;
       
   277 }