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/g1CodeRootSetTable.hpp"
  26 #include "gc/g1/g1CodeCacheRemSet.hpp"
  27 #include "unittest.hpp"
  28 
  29 class G1CodeRootSetTest : public ::testing::Test {
  30  public:
  31 
  32   size_t threshold(G1CodeRootSet* root_set) {
  33     return root_set->Threshold;
  34   }
  35 
  36   G1CodeRootSetTable* purge_list() {
  37     return G1CodeRootSetTable::_purge_list;
  38   }
  39 };
  40 
  41 TEST_VM_F(G1CodeRootSetTest, g1_code_cache_rem_set) {
  42   G1CodeRootSet root_set;
  43 
  44   ASSERT_TRUE(root_set.is_empty()) << "Code root set must be initially empty "
  45           "but is not.";
  46 
  47   ASSERT_EQ(G1CodeRootSet::static_mem_size(), sizeof (void*)) <<
  48           "The code root set's static memory usage is incorrect, "
  49           << G1CodeRootSet::static_mem_size() << " bytes";
  50 
  51   root_set.add((nmethod*) 1);
  52   ASSERT_EQ(root_set.length(), (size_t) 1) << "Added exactly one element, but"
  53           " set contains " << root_set.length() << " elements";
  54 
  55   const size_t num_to_add = (size_t) threshold(&root_set) + 1;
  56 
  57   for (size_t i = 1; i <= num_to_add; i++) {
  58     root_set.add((nmethod*) 1);
  59   }
  60   ASSERT_EQ(root_set.length(), (size_t) 1)
  61           << "Duplicate detection should not have increased the set size but "
  62           << "is " << root_set.length();
  63 
  64   for (size_t i = 2; i <= num_to_add; i++) {
  65     root_set.add((nmethod*) (uintptr_t) (i));
  66   }
  67 
  68   ASSERT_EQ(root_set.length(), num_to_add)
  69           << "After adding in total " << num_to_add << " distinct code roots, "
  70           "they need to be in the set, but there are only " << root_set.length();
  71 
  72   ASSERT_NE(purge_list(), (G1CodeRootSetTable*) NULL)
  73           << "should have grown to large hashtable";
  74 
  75   size_t num_popped = 0;
  76   for (size_t i = 1; i <= num_to_add; i++) {
  77     bool removed = root_set.remove((nmethod*) i);
  78     if (removed) {
  79       num_popped += 1;
  80     } else {
  81       break;
  82     }
  83   }
  84   ASSERT_EQ(num_popped, num_to_add)
  85           << "Managed to pop " << num_popped << " code roots, but only "
  86           << num_to_add << " were added";
  87   ASSERT_NE(purge_list(), (G1CodeRootSetTable*) NULL)
  88           << "should have grown to large hashtable";
  89 
  90   G1CodeRootSet::purge();
  91 
  92   ASSERT_EQ(purge_list(), (G1CodeRootSetTable*) NULL)
  93           << "should have purged old small tables";
  94 }