1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/g1/g1CodeCacheRemSet.internal.hpp"
  26 #include "gc/g1/g1CodeCacheRemSet.hpp"
  27 #include "unittest.hpp"
  28 
  29 class G1CodeRootSetTest : public ::testing::Test {
  30  private:
  31   G1CodeRootSet root_set;
  32 
  33  public:
  34 
  35   size_t getThreshold() {
  36     return root_set.Threshold;
  37   }
  38 
  39   CodeRootSetTable* getPurgeList() {
  40     return CodeRootSetTable::_purge_list;
  41   }
  42 
  43   G1CodeRootSet* getG1CodeRootSet() {
  44     return &root_set;
  45   }
  46 };
  47 
  48 TEST_VM_F(G1CodeRootSetTest, g1_code_cache_rem_set) {
  49   G1CodeRootSet* root_set = getG1CodeRootSet();
  50 
  51   ASSERT_TRUE(root_set->is_empty()) << "Code root set must be initially empty "
  52           "but is not.";
  53 
  54   ASSERT_EQ(G1CodeRootSet::static_mem_size(), sizeof (void*)) <<
  55           "The code root set's static memory usage is incorrect, "
  56           << G1CodeRootSet::static_mem_size() << " bytes";
  57 
  58   root_set->add((nmethod*) 1);
  59   ASSERT_EQ(root_set->length(), (size_t) 1) << "Added exactly one element, but"
  60           " set contains " << root_set->length() << " elements";
  61 
  62   const size_t num_to_add = (size_t) getThreshold() + 1;
  63 
  64   for (size_t i = 1; i <= num_to_add; i++) {
  65     root_set->add((nmethod*) 1);
  66   }
  67   ASSERT_EQ(root_set->length(), (size_t) 1)
  68           << "Duplicate detection should not have increased the set size but "
  69           << "is " << root_set->length();
  70 
  71   for (size_t i = 2; i <= num_to_add; i++) {
  72     root_set->add((nmethod*) (uintptr_t) (i));
  73   }
  74 
  75   ASSERT_EQ(root_set->length(), num_to_add)
  76           << "After adding in total " << num_to_add << " distinct code roots, "
  77           "they need to be in the set, but there are only " << root_set->length();
  78 
  79   ASSERT_NE(getPurgeList(), (CodeRootSetTable*) NULL)
  80           << "should have grown to large hashtable";
  81 
  82   size_t num_popped = 0;
  83   for (size_t i = 1; i <= num_to_add; i++) {
  84     bool removed = root_set->remove((nmethod*) i);
  85     if (removed) {
  86       num_popped += 1;
  87     } else {
  88       break;
  89     }
  90   }
  91   ASSERT_EQ(num_popped, num_to_add)
  92           << "Managed to pop " << num_popped << " code roots, but only "
  93           << num_to_add << " were added";
  94   ASSERT_NE(getPurgeList(), (CodeRootSetTable*) NULL)
  95           << "should have grown to large hashtable";
  96 
  97   G1CodeRootSet::purge();
  98 
  99   ASSERT_EQ(getPurgeList(), (CodeRootSetTable*) NULL)
 100           << "should have purged old small tables";
 101 }