test/hotspot/gtest/metaspace/test_commitmask.cpp
branchstuefe-new-metaspace-branch
changeset 58063 bdf136b8ae0e
child 58099 5aeb07390c74
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 
       
    26 #include "precompiled.hpp"
       
    27 #include "metaspace/metaspaceTestsCommon.hpp"
       
    28 
       
    29 static int get_random(int limit) { return os::random() % limit; }
       
    30 
       
    31 class CommitMaskTest {
       
    32   const MetaWord* const _base;
       
    33   const size_t _word_size;
       
    34 
       
    35   CommitMask _mask;
       
    36 
       
    37   void verify_mask() {
       
    38     // Note: we omit the touch test since we operate on fictional
       
    39     // memory
       
    40     DEBUG_ONLY(_mask.verify(true, false);)
       
    41   }
       
    42 
       
    43   // Return a random sub range within [_base.._base + word_size),
       
    44   // aligned to granule size
       
    45   const MetaWord* calc_random_subrange(size_t* p_word_size) {
       
    46     size_t l1 = get_random(_word_size);
       
    47     size_t l2 = get_random(_word_size);
       
    48     if (l1 > l2) {
       
    49       size_t l = l1;
       
    50       l1 = l2;
       
    51       l2 = l;
       
    52     }
       
    53     l1 = align_down(l1, Settings::commit_granule_words());
       
    54     l2 = align_up(l2, Settings::commit_granule_words());
       
    55 
       
    56     const MetaWord* p = _base + l1;
       
    57     const size_t len = l2 - l1;
       
    58 
       
    59     assert(p >= _base && p + len <= _base + _word_size,
       
    60            "Sanity");
       
    61     *p_word_size = len;
       
    62 
       
    63     return p;
       
    64   }
       
    65 
       
    66   void test1() {
       
    67 
       
    68     LOG("test1");
       
    69 
       
    70     // Commit everything
       
    71     size_t prior_committed = _mask.mark_range_as_committed(_base, _word_size);
       
    72     verify_mask();
       
    73     ASSERT_LE(prior_committed, _word_size); // We do not really know
       
    74 
       
    75     // Commit everything again, should be a noop
       
    76     prior_committed = _mask.mark_range_as_committed(_base, _word_size);
       
    77     verify_mask();
       
    78     ASSERT_EQ(prior_committed, _word_size);
       
    79 
       
    80     ASSERT_EQ(_mask.get_committed_size(),
       
    81               _word_size);
       
    82     ASSERT_EQ(_mask.get_committed_size_in_range(_base, _word_size),
       
    83               _word_size);
       
    84 
       
    85     for (const MetaWord* p = _base; p < _base + _word_size; p ++) {
       
    86       ASSERT_TRUE(_mask.is_committed_address(p));
       
    87     }
       
    88 
       
    89     // Now make an uncommitted hole
       
    90     size_t sr_word_size;
       
    91     const MetaWord* sr_base = calc_random_subrange(&sr_word_size);
       
    92     LOG("subrange " PTR_FORMAT "-" PTR_FORMAT ".",
       
    93         p2i(sr_base), p2i(sr_base + sr_word_size));
       
    94 
       
    95     size_t prior_uncommitted =
       
    96         _mask.mark_range_as_uncommitted(sr_base, sr_word_size);
       
    97     verify_mask();
       
    98     ASSERT_EQ(prior_uncommitted, (size_t)0);
       
    99 
       
   100     // Again, for fun, should be a noop now.
       
   101     prior_uncommitted = _mask.mark_range_as_uncommitted(sr_base, sr_word_size);
       
   102     verify_mask();
       
   103     ASSERT_EQ(prior_uncommitted, sr_word_size);
       
   104 
       
   105     ASSERT_EQ(_mask.get_committed_size_in_range(sr_base, sr_word_size),
       
   106               (size_t)0);
       
   107     ASSERT_EQ(_mask.get_committed_size(),
       
   108               _word_size - sr_word_size);
       
   109     ASSERT_EQ(_mask.get_committed_size_in_range(_base, _word_size),
       
   110               _word_size - sr_word_size);
       
   111     for (const MetaWord* p = _base; p < _base + _word_size; p ++) {
       
   112       if (p >= sr_base && p < sr_base + sr_word_size) {
       
   113         ASSERT_FALSE(_mask.is_committed_address(p));
       
   114       } else {
       
   115         ASSERT_TRUE(_mask.is_committed_address(p));
       
   116       }
       
   117     }
       
   118 
       
   119     // Recommit whole range
       
   120     prior_committed = _mask.mark_range_as_committed(_base, _word_size);
       
   121     verify_mask();
       
   122     ASSERT_EQ(prior_committed, _word_size - sr_word_size);
       
   123 
       
   124     ASSERT_EQ(_mask.get_committed_size_in_range(sr_base, sr_word_size),
       
   125               sr_word_size);
       
   126     ASSERT_EQ(_mask.get_committed_size(),
       
   127               _word_size);
       
   128     ASSERT_EQ(_mask.get_committed_size_in_range(_base, _word_size),
       
   129               _word_size);
       
   130     for (const MetaWord* p = _base; p < _base + _word_size; p ++) {
       
   131       ASSERT_TRUE(_mask.is_committed_address(p));
       
   132     }
       
   133 
       
   134   }
       
   135 
       
   136   void test2() {
       
   137 
       
   138     LOG("test2");
       
   139 
       
   140     // Uncommit everything
       
   141     size_t prior_uncommitted = _mask.mark_range_as_uncommitted(_base, _word_size);
       
   142     verify_mask();
       
   143     ASSERT_LE(prior_uncommitted, _word_size);
       
   144 
       
   145     // Uncommit everything again, should be a noop
       
   146     prior_uncommitted = _mask.mark_range_as_uncommitted(_base, _word_size);
       
   147     verify_mask();
       
   148     ASSERT_EQ(prior_uncommitted, _word_size);
       
   149 
       
   150     ASSERT_EQ(_mask.get_committed_size(),
       
   151         (size_t)0);
       
   152     ASSERT_EQ(_mask.get_committed_size_in_range(_base, _word_size),
       
   153         (size_t)0);
       
   154 
       
   155     // Now make an committed region
       
   156     size_t sr_word_size;
       
   157     const MetaWord* sr_base = calc_random_subrange(&sr_word_size);
       
   158     LOG("subrange " PTR_FORMAT "-" PTR_FORMAT ".",
       
   159         p2i(sr_base), p2i(sr_base + sr_word_size));
       
   160 
       
   161     ASSERT_EQ(_mask.get_committed_size_in_range(sr_base, sr_word_size),
       
   162               (size_t)0);
       
   163     for (const MetaWord* p = _base; p < _base + _word_size; p ++) {
       
   164       ASSERT_FALSE(_mask.is_committed_address(p));
       
   165     }
       
   166 
       
   167     size_t prior_committed = _mask.mark_range_as_committed(sr_base, sr_word_size);
       
   168     verify_mask();
       
   169     ASSERT_EQ(prior_committed, (size_t)0);
       
   170 
       
   171     // Again, for fun, should be a noop now.
       
   172     prior_committed = _mask.mark_range_as_committed(sr_base, sr_word_size);
       
   173     verify_mask();
       
   174     ASSERT_EQ(prior_committed, sr_word_size);
       
   175 
       
   176     ASSERT_EQ(_mask.get_committed_size_in_range(sr_base, sr_word_size),
       
   177         sr_word_size);
       
   178     ASSERT_EQ(_mask.get_committed_size(),
       
   179         sr_word_size);
       
   180     ASSERT_EQ(_mask.get_committed_size_in_range(_base, _word_size),
       
   181         sr_word_size);
       
   182     for (const MetaWord* p = _base; p < _base + _word_size; p ++) {
       
   183       if (p >= sr_base && p < sr_base + sr_word_size) {
       
   184         ASSERT_TRUE(_mask.is_committed_address(p));
       
   185       } else {
       
   186         ASSERT_FALSE(_mask.is_committed_address(p));
       
   187       }
       
   188     }
       
   189 
       
   190     // Re-uncommit whole range
       
   191     prior_uncommitted = _mask.mark_range_as_uncommitted(_base, _word_size);
       
   192     verify_mask();
       
   193     ASSERT_EQ(prior_uncommitted, _word_size - sr_word_size);
       
   194 
       
   195     EXPECT_EQ(_mask.get_committed_size_in_range(sr_base, sr_word_size),
       
   196         (size_t)0);
       
   197     EXPECT_EQ(_mask.get_committed_size(),
       
   198         (size_t)0);
       
   199     EXPECT_EQ(_mask.get_committed_size_in_range(_base, _word_size),
       
   200         (size_t)0);
       
   201     for (const MetaWord* p = _base; p < _base + _word_size; p ++) {
       
   202       ASSERT_FALSE(_mask.is_committed_address(p));
       
   203     }
       
   204 
       
   205   }
       
   206 
       
   207 
       
   208   void test3() {
       
   209 
       
   210     // arbitrary ranges are set and cleared and compared with the test map
       
   211     TestMap map(_word_size);
       
   212 
       
   213     _mask.clear_large();
       
   214 
       
   215     for (int run = 0; run < 100; run ++) {
       
   216 
       
   217       range_t r;
       
   218       calc_random_range(_word_size, &r, Settings::commit_granule_words());
       
   219 
       
   220       ASSERT_EQ(_mask.get_committed_size(), (size_t)map.get_num_set());
       
   221       ASSERT_EQ(_mask.get_committed_size_in_range(_base + r.from, r.to - r.from),
       
   222                 (size_t)map.get_num_set(r.from, r.to));
       
   223 
       
   224       if (os::random() % 100 < 50) {
       
   225         _mask.mark_range_as_committed(_base + r.from, r.to - r.from);
       
   226         map.set_range(r.from, r.to);
       
   227       } else {
       
   228         _mask.mark_range_as_uncommitted(_base + r.from, r.to - r.from);
       
   229         map.clear_range(r.from, r.to);
       
   230       }
       
   231 
       
   232       ASSERT_EQ(_mask.get_committed_size(), (size_t)map.get_num_set());
       
   233       ASSERT_EQ(_mask.get_committed_size_in_range(_base + r.from, r.to - r.from),
       
   234                 (size_t)map.get_num_set(r.from, r.to));
       
   235 
       
   236     }
       
   237 
       
   238   }
       
   239 
       
   240 
       
   241 public:
       
   242 
       
   243   CommitMaskTest(const MetaWord* base, size_t size)
       
   244     : _base(base), _word_size(size), _mask(base, size)
       
   245   {}
       
   246 
       
   247   void test() {
       
   248     LOG("mask range: " PTR_FORMAT "-" PTR_FORMAT
       
   249          " (" SIZE_FORMAT " words).",
       
   250          p2i(_base), p2i(_base + _word_size), _word_size);
       
   251     for (int i = 0; i < 5; i ++) {
       
   252       test1(); test2(); test3();
       
   253     }
       
   254   }
       
   255 
       
   256 
       
   257 };
       
   258 
       
   259 TEST(metaspace, commit_mask_basics) {
       
   260 
       
   261   const MetaWord* const base = (const MetaWord*) 0x100000;
       
   262 
       
   263   CommitMask mask1(base, Settings::commit_granule_words());
       
   264   ASSERT_EQ(mask1.size(), (BitMap::idx_t)1);
       
   265 
       
   266   CommitMask mask2(base, Settings::commit_granule_words() * 4);
       
   267   ASSERT_EQ(mask2.size(), (BitMap::idx_t)4);
       
   268 
       
   269   CommitMask mask3(base, Settings::commit_granule_words() * 43);
       
   270   ASSERT_EQ(mask3.size(), (BitMap::idx_t)43);
       
   271 
       
   272   mask3.mark_range_as_committed(base, Settings::commit_granule_words());
       
   273   mask3.mark_range_as_committed(base + (Settings::commit_granule_words() * 42), Settings::commit_granule_words());
       
   274 
       
   275   ASSERT_EQ(mask3.at(0), 1);
       
   276   for (int i = 1; i < 42; i ++) {
       
   277     ASSERT_EQ(mask3.at(i), 0);
       
   278   }
       
   279   ASSERT_EQ(mask3.at(42), 1);
       
   280 
       
   281 }
       
   282 
       
   283 TEST(metaspace, commit_mask_small) {
       
   284 
       
   285   const MetaWord* const base = (const MetaWord*) 0x100000;
       
   286 
       
   287   CommitMaskTest test(base, Settings::commit_granule_words());
       
   288   test.test();
       
   289 
       
   290 }
       
   291 
       
   292 TEST(metaspace, commit_mask_range) {
       
   293 
       
   294   const MetaWord* const base = (const MetaWord*) 0x100000;
       
   295   const size_t len = Settings::commit_granule_words() * 4;
       
   296   const MetaWord* const end = base + len;
       
   297   CommitMask mask(base, len);
       
   298 
       
   299   LOG("total range: " PTR_FORMAT "-" PTR_FORMAT "\n", p2i(base), p2i(end));
       
   300 
       
   301   size_t l = mask.mark_range_as_committed(base, len);
       
   302   ASSERT_LE(l, len);
       
   303 
       
   304   for (const MetaWord* p = base; p <= end - Settings::commit_granule_words();
       
   305        p += Settings::commit_granule_words()) {
       
   306     for (const MetaWord* p2 = p + Settings::commit_granule_words();
       
   307          p2 <= end; p2 += Settings::commit_granule_words()) {
       
   308       LOG(PTR_FORMAT "-" PTR_FORMAT "\n", p2i(p), p2i(p2));
       
   309       EXPECT_EQ(mask.get_committed_size_in_range(p, p2 - p),
       
   310                 (size_t)(p2 - p));
       
   311     }
       
   312   }
       
   313 
       
   314   l = mask.mark_range_as_uncommitted(base, len);
       
   315   ASSERT_EQ(l, (size_t)0);
       
   316 
       
   317   for (const MetaWord* p = base; p <= end - Settings::commit_granule_words();
       
   318        p += Settings::commit_granule_words()) {
       
   319     for (const MetaWord* p2 = p + Settings::commit_granule_words();
       
   320          p2 <= end; p2 += Settings::commit_granule_words()) {
       
   321       LOG(PTR_FORMAT "-" PTR_FORMAT "\n", p2i(p), p2i(p2));
       
   322       EXPECT_EQ(mask.get_committed_size_in_range(p, p2 - p),
       
   323                 (size_t)(0));
       
   324     }
       
   325   }
       
   326 
       
   327 }
       
   328 
       
   329 
       
   330 TEST(metaspace, commit_mask_random) {
       
   331 
       
   332   for (int i = 0; i < 5; i ++) {
       
   333 
       
   334     // make up a range out of thin air
       
   335     const MetaWord* const base =
       
   336         align_down( (const MetaWord*) ((uintptr_t) os::random() * os::random()),
       
   337                     Settings::commit_granule_bytes());
       
   338     const size_t len = align_up( 1 + (os::random() % M),
       
   339                     Settings::commit_granule_words());
       
   340 
       
   341     CommitMaskTest test(base, len);
       
   342     test.test();
       
   343 
       
   344   }
       
   345 
       
   346 }