1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, 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 #include "precompiled.hpp"
  27 
  28 //#define LOG_PLEASE
  29 #include "metaspace/metaspaceTestsCommon.hpp"
  30 #include "metaspace/metaspaceTestContexts.hpp"
  31 #include "metaspace/metaspace_rangehelpers.hpp"
  32 
  33 TEST_VM(metaspace, metachunklist) {
  34 
  35   ChunkTestsContext helper;
  36 
  37   MetachunkList lst;
  38 
  39   Metachunk* chunks[10];
  40   size_t total_size = 0;
  41 
  42   for (int i = 0; i < 10; i ++) {
  43     Metachunk* c = NULL;
  44     helper.alloc_chunk_expect_success(&c, ChunkLevelRanges::all_chunks().random_value());
  45     chunks[i] = c;
  46     total_size += c->committed_words();
  47 
  48     lst.add(c);
  49     EXPECT_EQ(lst.first(), c);
  50 
  51     Metachunk* c2 = lst.remove_first();
  52     EXPECT_EQ(c, c2);
  53 
  54     EXPECT_EQ(lst.count(), i);
  55     lst.add(c);
  56     EXPECT_EQ(lst.count(), i + 1);
  57     EXPECT_EQ(lst.calc_committed_word_size(), total_size);
  58 
  59   }
  60 
  61   for (int i = 0; i < 10; i ++) {
  62     DEBUG_ONLY(EXPECT_TRUE(lst.contains(chunks[i]));)
  63   }
  64 
  65   for (int i = 0; i < 10; i ++) {
  66     Metachunk* c = lst.remove_first();
  67     DEBUG_ONLY(EXPECT_FALSE(lst.contains(c));)
  68     helper.return_chunk(c);
  69   }
  70 
  71   EXPECT_EQ(lst.count(), 0);
  72   EXPECT_EQ(lst.calc_committed_word_size(), (size_t)0);
  73 
  74 }
  75 
  76 
  77 TEST_VM(metaspace, freechunklist) {
  78 
  79   ChunkTestsContext helper;
  80 
  81   FreeChunkListVector lst;
  82 
  83   MemRangeCounter cnt;
  84   MemRangeCounter committed_cnt;
  85 
  86   // Add random chunks to list and check the counter apis (word_size, commited_word_size, num_chunks)
  87   // Make every other chunk randomly uncommitted, and later we check that committed chunks are sorted in at the front
  88   // of the lists.
  89   for (int i = 0; i < 100; i ++) {
  90     Metachunk* c = NULL;
  91     helper.alloc_chunk_expect_success(&c, ChunkLevelRanges::all_chunks().random_value());
  92     bool uncommitted_chunk = i % 3;
  93     if (uncommitted_chunk) {
  94       helper.uncommit_chunk_with_test(c);
  95       c->set_in_use();
  96     }
  97 
  98     lst.add(c);
  99 
 100     LOG("->" METACHUNK_FULL_FORMAT, METACHUNK_FULL_FORMAT_ARGS(c));
 101 
 102     cnt.add(c->word_size());
 103     committed_cnt.add(c->committed_words());
 104 
 105     EXPECT_EQ(lst.num_chunks(), (int)cnt.count());
 106     EXPECT_EQ(lst.word_size(), cnt.total_size());
 107     EXPECT_EQ(lst.committed_word_size(), committed_cnt.total_size());
 108   }
 109 
 110   // Drain each list separately
 111   for (chunklevel_t lvl = LOWEST_CHUNK_LEVEL; lvl <= HIGHEST_CHUNK_LEVEL; lvl ++) {
 112     Metachunk* c = lst.remove_first(lvl);
 113     bool found_uncommitted = false;
 114     while (c != NULL) {
 115 
 116       LOG("<-" METACHUNK_FULL_FORMAT, METACHUNK_FULL_FORMAT_ARGS(c));
 117 
 118       // Within a level, no committed chunk should follow an uncommitted chunk:
 119       if (found_uncommitted) {
 120         EXPECT_TRUE(c->is_fully_uncommitted());
 121       } else {
 122         found_uncommitted = c->is_fully_uncommitted();
 123       }
 124 
 125       cnt.sub(c->word_size());
 126       committed_cnt.sub(c->committed_words());
 127 
 128       EXPECT_EQ(lst.num_chunks(), (int)cnt.count());
 129       EXPECT_EQ(lst.word_size(), cnt.total_size());
 130       EXPECT_EQ(lst.committed_word_size(), committed_cnt.total_size());
 131 
 132       helper.return_chunk(c);
 133 
 134       c = lst.remove_first(lvl);
 135     }
 136   }
 137 
 138   // TODO
 139 }
 140