test/hotspot/gtest/metaspace/test_blockListArray.cpp
branchstuefe-new-metaspace-branch
changeset 59271 1558266946de
parent 59238 6ce12ce00d3e
--- a/test/hotspot/gtest/metaspace/test_blockListArray.cpp	Mon Nov 25 16:35:14 2019 +0100
+++ b/test/hotspot/gtest/metaspace/test_blockListArray.cpp	Tue Nov 26 11:29:20 2019 +0100
@@ -31,7 +31,7 @@
 
 TEST_VM(metaspace, BlockListFreeMap_mask_basic) {
   // Basic tests
-  metaspace::BlockListFreeMap map;
+  metaspace::BlockListArrayMask map;
   EXPECT_TRUE(map.all_zero());
   for (int i = 0; i < map.size(); i ++) {
     map.set_bit(i);
@@ -43,13 +43,13 @@
 }
 
 TEST_VM(metaspace, BlockListFreeMap_mask_find_next_set_bit) {
-  metaspace::BlockListFreeMap map;
+  metaspace::BlockListArrayMask map;
   EXPECT_TRUE(map.all_zero());
   for (int i = 0; i < map.size(); i ++) {
     map.set_bit(i);
     for (int j = 0; j < i; j ++) {
       int n = map.find_next_set_bit(j);
-      if (j <= i) {
+      if (j < i) {
         EXPECT_EQ(n, i);
       } else {
         EXPECT_EQ(n, -1);
@@ -59,3 +59,115 @@
   }
 }
 
+#define CHECK_BLA_CONTENT(BLA, NUM_EXPECTED, SIZE_EXPECTED) \
+{ \
+  metaspace::block_stats_t stat; \
+  memset(&stat, 0xFF, sizeof(stat)); \
+  BLA.statistics(&stat); \
+  ASSERT_EQ(stat.num_blocks, (int)NUM_EXPECTED); \
+  ASSERT_EQ(stat.word_size, (size_t)SIZE_EXPECTED); \
+  if (NUM_EXPECTED == 0) { \
+	  ASSERT_TRUE(BLA.is_empty()); \
+  } else { \
+	  ASSERT_FALSE(BLA.is_empty()); \
+  } \
+}
+
+
+
+TEST_VM(metaspace, BlockListArray_basic) {
+
+  metaspace::BlockListArray<100, 5, 20> bla;
+  ASSERT_EQ(bla.maximal_word_size(), (size_t)200);
+  ASSERT_EQ(bla.minimal_word_size(), (size_t)100);
+
+  CHECK_BLA_CONTENT(bla, 0, 0);
+
+  // Put something into the bla and check bla.
+  // Take something out of the bla; any allocation smaller
+  // than the one block in it shall succeed.
+  MetaWord tmp[1024];
+
+  for (size_t feeding_size = 100; feeding_size < 200; feeding_size ++) {
+    for (size_t l = 100; l < 200; l ++) {
+      LOG(SIZE_FORMAT "-" SIZE_FORMAT, feeding_size, l);
+
+      bla.put(tmp, feeding_size);
+      CHECK_BLA_CONTENT(bla, 1, feeding_size);
+
+      metaspace::block_t* b = bla.get(l);
+
+      if (l <= feeding_size) {
+        // We expect the get() to work and return the block we just put in
+        // if the size we ask for is smaller than the size we put in.
+        ASSERT_NOT_NULL(b);
+        ASSERT_EQ((MetaWord*) b, tmp);
+        ASSERT_EQ(b->size, feeding_size);
+        CHECK_BLA_CONTENT(bla, 0, 0);
+        memset(b, 0xDE, b->size * sizeof(MetaWord));
+      } else {
+        // Otherwise we expect the bla to be unchanged.
+        assert(b == NULL, "s");
+        ASSERT_NULL(b);
+        CHECK_BLA_CONTENT(bla, 1, feeding_size);
+      }
+      DEBUG_ONLY(bla.verify();)
+
+      // Regardless of bla's state, empty it out for the next iteration.
+      bla.get(feeding_size);
+      CHECK_BLA_CONTENT(bla, 0, 0);
+    }
+  }
+}
+
+TEST_VM(metaspace, BlockListArray_fill_and_drain) {
+
+  metaspace::BlockListArray<100, 5, 20> bla;
+  ASSERT_EQ(bla.maximal_word_size(), (size_t)200);
+  ASSERT_EQ(bla.minimal_word_size(), (size_t)100);
+
+  CHECK_BLA_CONTENT(bla, 0, 0);
+
+  // Now feed it some memory:
+  FeederBuffer fb(16 * K);
+  RandSizeGenerator rgen(100, 200);
+  int num_fed = 0;
+  size_t size_fed = 0;
+  MetaWord* p = NULL;
+  do {
+    const size_t s = rgen.get();
+    p = fb.get(s);
+    if (p != NULL) {
+      num_fed ++;
+      size_fed += s;
+      bla.put(p, s);
+      CHECK_BLA_CONTENT(bla, num_fed, size_fed);
+    }
+  } while (p != NULL);
+
+  DEBUG_ONLY(bla.verify();)
+
+  // Now remove memory until empty:
+  int num_retrieved = 0;
+  size_t size_retrieved = 0;
+  metaspace::block_t* b = NULL;
+  do {
+    const size_t s = rgen.get();
+    metaspace::block_t* b = bla.get(s);
+    if (p != NULL) {
+      ASSERT_GE(b->size, s);
+      num_retrieved ++;
+      size_retrieved += b->size;
+      memset(b, 0xDE, b->size * BytesPerWord);
+      CHECK_BLA_CONTENT(bla, num_fed - num_retrieved,
+                             size_fed - size_retrieved);
+      ASSERT_LE(num_retrieved, num_fed);
+      ASSERT_LE(size_retrieved, size_fed);
+    }
+
+  } while (p != NULL);
+
+  DEBUG_ONLY(bla.verify();)
+
+}
+