test/hotspot/gtest/metaspace/test_metachunk.cpp
branchstuefe-new-metaspace-branch
changeset 58063 bdf136b8ae0e
child 58383 04d2c850ab4f
equal deleted inserted replaced
58062:65cad575ace3 58063:bdf136b8ae0e
       
     1 /*
       
     2  * Copyright (c) 2019, SAP SE. All rights reserved.
       
     3  * Copyright (c) 2019, Oracle and/or its affiliates. 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 #include "precompiled.hpp"
       
    26 #include "metaspace/metaspaceTestsCommon.hpp"
       
    27 
       
    28 
       
    29 
       
    30 class MetachunkTest {
       
    31 
       
    32   CommitLimiter _commit_limiter;
       
    33   VirtualSpaceList _vs_list;
       
    34   ChunkManager _cm;
       
    35 
       
    36   Metachunk* alloc_chunk(chklvl_t lvl) {
       
    37 
       
    38     Metachunk* c = _cm.get_chunk(lvl, lvl);
       
    39     EXPECT_NOT_NULL(c);
       
    40     EXPECT_EQ(c->level(), lvl);
       
    41     check_chunk(c);
       
    42 
       
    43     DEBUG_ONLY(c->verify(true);)
       
    44 
       
    45     return c;
       
    46   }
       
    47 
       
    48   void check_chunk(const Metachunk* c) const {
       
    49     EXPECT_LE(c->used_words(), c->committed_words());
       
    50     EXPECT_LE(c->committed_words(), c->word_size());
       
    51     EXPECT_NOT_NULL(c->base());
       
    52     EXPECT_TRUE(_vs_list.contains(c->base()));
       
    53     EXPECT_TRUE(is_aligned(c->base(), MAX_CHUNK_BYTE_SIZE));
       
    54     EXPECT_TRUE(is_aligned(c->word_size(), MAX_CHUNK_WORD_SIZE));
       
    55     EXPECT_TRUE(metaspace::chklvl::is_valid_level(c->level()));
       
    56 
       
    57     if (c->next() != NULL) EXPECT_EQ(c->next()->prev(), c);
       
    58     if (c->prev() != NULL) EXPECT_EQ(c->prev()->next(), c);
       
    59     if (c->next_in_vs() != NULL) EXPECT_EQ(c->next_in_vs()->prev_in_vs(), c);
       
    60     if (c->prev_in_vs() != NULL) EXPECT_EQ(c->prev_in_vs()->next_in_vs(), c);
       
    61 
       
    62     DEBUG_ONLY(c->verify(true);)
       
    63   }
       
    64 
       
    65 public:
       
    66 
       
    67   MetachunkTest(size_t commit_limit)
       
    68     : _commit_limiter(commit_limit),
       
    69       _vs_list("test_vs_list", &_commit_limiter),
       
    70       _cm("test_cm", &_vs_list)
       
    71   {
       
    72   }
       
    73 
       
    74   void test_random_allocs() {
       
    75 
       
    76     // Randomly alloc from a chunk until it is full.
       
    77     Metachunk* c = alloc_chunk(LOWEST_CHUNK_LEVEL);
       
    78 
       
    79     check_chunk(c);
       
    80 
       
    81     EXPECT_TRUE(c->is_in_use());
       
    82     EXPECT_EQ(c->used_words(), (size_t)0);
       
    83 
       
    84     // uncommit to start off with uncommitted chunk; then start allocating.
       
    85     c->set_free();
       
    86     c->uncommit();
       
    87     c->set_in_use();
       
    88 
       
    89     EXPECT_EQ(c->committed_words(), (size_t)0);
       
    90 
       
    91     RandSizeGenerator rgen(1, 256, 0.1f, 1024, 4096); // note: word sizes
       
    92     SizeCounter words_allocated;
       
    93 
       
    94     MetaWord* p = NULL;
       
    95     bool did_hit_commit_limit = false;
       
    96     do {
       
    97 
       
    98       const size_t alloc_size = align_up(rgen.get(), Metachunk::allocation_alignment_words);
       
    99 
       
   100       // Note: about net and raw sizes: these concepts only exist at the SpaceManager level.
       
   101       // At the chunk level (where we test here), we allocate exactly what we ask, in number of words.
       
   102 
       
   103       const bool may_hit_commit_limit =
       
   104           _commit_limiter.possible_expansion_words() <= align_up(alloc_size, Settings::commit_granule_words());
       
   105 
       
   106       p = c->allocate(alloc_size, &did_hit_commit_limit);
       
   107       LOG("Allocated " SIZE_FORMAT " words, chunk: " METACHUNK_FULL_FORMAT, alloc_size, METACHUNK_FULL_FORMAT_ARGS(c));
       
   108 
       
   109       check_chunk(c);
       
   110 
       
   111       if (p != NULL) {
       
   112         // From time to time deallocate to test deallocation. Since we do this on the very last allocation,
       
   113         // this should always work.
       
   114         if (os::random() % 100 > 95) {
       
   115           LOG("Test dealloc in place");
       
   116           EXPECT_TRUE(c->attempt_rollback_allocation(p, alloc_size));
       
   117         } else {
       
   118           fill_range_with_pattern(p, (uintx) this, alloc_size); // test that we can access this.
       
   119           words_allocated.increment_by(alloc_size);
       
   120           EXPECT_EQ(c->used_words(), words_allocated.get());
       
   121         }
       
   122       } else {
       
   123         // Allocating from a chunk can only fail for one of two reasons: Either the chunk is full, or
       
   124         // we attempted to increase the chunk's commit region and hit the commit limit.
       
   125         if (did_hit_commit_limit) {
       
   126           EXPECT_TRUE(may_hit_commit_limit);
       
   127         } else {
       
   128           // Chunk is full.
       
   129           EXPECT_LT(c->free_words(), alloc_size);
       
   130         }
       
   131       }
       
   132 
       
   133     } while(p != NULL);
       
   134 
       
   135     check_range_for_pattern(c->base(), (uintx) this, c->used_words());
       
   136 
       
   137     // At the end of the test return the chunk to the chunk manager to
       
   138     // avoid asserts on destruction time.
       
   139     _cm.return_chunk(c);
       
   140 
       
   141   }
       
   142 
       
   143 
       
   144 
       
   145 };
       
   146 
       
   147 
       
   148 
       
   149 TEST_VM(metaspace, metachunk_test_random_allocs_no_commit_limit) {
       
   150 
       
   151   // The test only allocates one root chunk and plays with it, so anything
       
   152   // above the size of a root chunk should not hit commit limit.
       
   153   MetachunkTest test(2 * MAX_CHUNK_WORD_SIZE);
       
   154   test.test_random_allocs();
       
   155 
       
   156 }
       
   157 
       
   158 TEST_VM(metaspace, metachunk_test_random_allocs_with_commit_limit) {
       
   159 
       
   160   // The test allocates one root chunk and plays with it, so a limit smaller
       
   161   // than root chunk size will be hit.
       
   162   MetachunkTest test(MAX_CHUNK_WORD_SIZE / 2);
       
   163   test.test_random_allocs();
       
   164 
       
   165 }