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