1 /*
   2  * Copyright (c) 2014, 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/bufferingOopClosure.hpp"
  26 #include "memory/iterator.hpp"
  27 #include "unittest.hpp"
  28 
  29 class BufferingOopClosureTest : public ::testing::Test {
  30  public:
  31   // Helper class to fake a set of oop*s and narrowOop*s.
  32   class FakeRoots {
  33    public:
  34     // Used for sanity checking of the values passed to the do_oops functions in the test.
  35     static const uintptr_t NarrowOopMarker = uintptr_t(1) << (BitsPerWord -1);
  36 
  37     int    _num_narrow;
  38     int    _num_full;
  39     void** _narrow;
  40     void** _full;
  41 
  42     FakeRoots(int num_narrow, int num_full) :
  43         _num_narrow(num_narrow),
  44         _num_full(num_full),
  45         _narrow((void**)::malloc(sizeof(void*) * num_narrow)),
  46         _full((void**)::malloc(sizeof(void*) * num_full)) {
  47 
  48       for (int i = 0; i < num_narrow; i++) {
  49         _narrow[i] = (void*)(NarrowOopMarker + (uintptr_t)i);
  50       }
  51       for (int i = 0; i < num_full; i++) {
  52         _full[i] = (void*)(uintptr_t)i;
  53       }
  54     }
  55 
  56     ~FakeRoots() {
  57       ::free(_narrow);
  58       ::free(_full);
  59     }
  60 
  61     void oops_do_narrow_then_full(OopClosure* cl) {
  62       for (int i = 0; i < _num_narrow; i++) {
  63         cl->do_oop((narrowOop*)_narrow[i]);
  64       }
  65       for (int i = 0; i < _num_full; i++) {
  66         cl->do_oop((oop*)_full[i]);
  67       }
  68     }
  69 
  70     void oops_do_full_then_narrow(OopClosure* cl) {
  71       for (int i = 0; i < _num_full; i++) {
  72         cl->do_oop((oop*)_full[i]);
  73       }
  74       for (int i = 0; i < _num_narrow; i++) {
  75         cl->do_oop((narrowOop*)_narrow[i]);
  76       }
  77     }
  78 
  79     void oops_do_mixed(OopClosure* cl) {
  80       int i;
  81       for (i = 0; i < _num_full && i < _num_narrow; i++) {
  82         cl->do_oop((oop*)_full[i]);
  83         cl->do_oop((narrowOop*)_narrow[i]);
  84       }
  85       for (int j = i; j < _num_full; j++) {
  86         cl->do_oop((oop*)_full[i]);
  87       }
  88       for (int j = i; j < _num_narrow; j++) {
  89         cl->do_oop((narrowOop*)_narrow[i]);
  90       }
  91     }
  92 
  93     static const int MaxOrder = 2;
  94 
  95     void oops_do(OopClosure* cl, int do_oop_order) {
  96       switch(do_oop_order) {
  97         case 0:
  98           oops_do_narrow_then_full(cl);
  99           break;
 100         case 1:
 101           oops_do_full_then_narrow(cl);
 102           break;
 103         case 2:
 104           oops_do_mixed(cl);
 105           break;
 106         default:
 107           oops_do_narrow_then_full(cl);
 108           break;
 109       }
 110     }
 111   };
 112 
 113   class CountOopClosure : public OopClosure {
 114     int _narrow_oop_count;
 115     int _full_oop_count;
 116    public:
 117     CountOopClosure() : _narrow_oop_count(0), _full_oop_count(0) {}
 118     void do_oop(narrowOop* p) {
 119       EXPECT_NE(uintptr_t(0), (uintptr_t(p) & FakeRoots::NarrowOopMarker))
 120               << "The narrowOop was unexpectedly not marked with the NarrowOopMarker";
 121       _narrow_oop_count++;
 122     }
 123 
 124     void do_oop(oop* p){
 125       EXPECT_EQ(uintptr_t(0), (uintptr_t(p) & FakeRoots::NarrowOopMarker))
 126               << "The oop was unexpectedly marked with the NarrowOopMarker";
 127       _full_oop_count++;
 128     }
 129 
 130     int narrow_oop_count() { return _narrow_oop_count; }
 131     int full_oop_count()   { return _full_oop_count; }
 132     int all_oop_count()    { return _narrow_oop_count + _full_oop_count; }
 133   };
 134 
 135   class DoNothingOopClosure : public OopClosure {
 136    public:
 137     void do_oop(narrowOop* p) {}
 138     void do_oop(oop* p)       {}
 139   };
 140 
 141   static void testCount(int num_narrow, int num_full, int do_oop_order) {
 142     FakeRoots fr(num_narrow, num_full);
 143 
 144     CountOopClosure coc;
 145     BufferingOopClosure boc(&coc);
 146 
 147     fr.oops_do(&boc, do_oop_order);
 148 
 149     boc.done();
 150 
 151     EXPECT_EQ(num_narrow, coc.narrow_oop_count()) << "when running testCount("
 152             << num_narrow << ", " << num_full << ", " << do_oop_order << ")";
 153 
 154     EXPECT_EQ(num_full, coc.full_oop_count()) << "when running testCount("
 155             << num_narrow << ", " << num_full << ", " << do_oop_order << ")";
 156 
 157     EXPECT_EQ(num_narrow + num_full, coc.all_oop_count()) << "when running testCount("
 158             << num_narrow << ", " << num_full << ", " << do_oop_order << ")";
 159   }
 160 
 161   static void testIsBufferEmptyOrFull(int num_narrow, int num_full, bool expect_empty, bool expect_full) {
 162     FakeRoots fr(num_narrow, num_full);
 163 
 164     DoNothingOopClosure cl;
 165     BufferingOopClosure boc(&cl);
 166 
 167     fr.oops_do(&boc, 0);
 168 
 169     EXPECT_EQ(expect_empty, boc.is_buffer_empty())
 170             << "when running testIsBufferEmptyOrFull("
 171             << num_narrow << ", " << num_full << ", "
 172             << expect_empty << ", " << expect_full << ")";
 173 
 174     EXPECT_EQ(expect_full, boc.is_buffer_full())
 175             << "when running testIsBufferEmptyOrFull("
 176             << num_narrow << ", " << num_full << ", "
 177             << expect_empty << ", " << expect_full << ")";
 178   }
 179 
 180   static void testEmptyAfterDone(int num_narrow, int num_full) {
 181     FakeRoots fr(num_narrow, num_full);
 182 
 183     DoNothingOopClosure cl;
 184     BufferingOopClosure boc(&cl);
 185 
 186     fr.oops_do(&boc, 0);
 187 
 188     // Make sure all get processed.
 189     boc.done();
 190 
 191     EXPECT_TRUE(boc.is_buffer_empty()) << "Should be empty after call to done()."
 192             << " testEmptyAfterDone(" << num_narrow << ", " << num_full << ")";
 193   }
 194 
 195   static int get_buffer_length() {
 196     return BufferingOopClosure::BufferLength;
 197   }
 198 };
 199 
 200 TEST_VM_F(BufferingOopClosureTest, count_test) {
 201   int bl = BufferingOopClosureTest::get_buffer_length();
 202 
 203   for (int order = 0; order < FakeRoots::MaxOrder; order++) {
 204     testCount(0,      0,      order);
 205     testCount(10,     0,      order);
 206     testCount(0,      10,     order);
 207     testCount(10,     10,     order);
 208     testCount(bl,     10,     order);
 209     testCount(10,     bl,     order);
 210     testCount(bl,     bl,     order);
 211     testCount(bl + 1, 10,     order);
 212     testCount(10,     bl + 1, order);
 213     testCount(bl + 1, bl,     order);
 214     testCount(bl,     bl + 1, order);
 215     testCount(bl + 1, bl + 1, order);
 216   }
 217 }
 218 
 219 TEST_VM_F(BufferingOopClosureTest, buffer_empty_or_full) {
 220   int bl = BufferingOopClosureTest::get_buffer_length();
 221 
 222   testIsBufferEmptyOrFull(0,      0,      true,  false);
 223   testIsBufferEmptyOrFull(1,      0,      false, false);
 224   testIsBufferEmptyOrFull(0,      1,      false, false);
 225   testIsBufferEmptyOrFull(1,      1,      false, false);
 226   testIsBufferEmptyOrFull(10,     0,      false, false);
 227   testIsBufferEmptyOrFull(0,      10,     false, false);
 228   testIsBufferEmptyOrFull(10,     10,     false, false);
 229   testIsBufferEmptyOrFull(0,      bl,     false, true);
 230   testIsBufferEmptyOrFull(bl,     0,      false, true);
 231   testIsBufferEmptyOrFull(bl / 2, bl / 2, false, true);
 232   testIsBufferEmptyOrFull(bl - 1, 1,      false, true);
 233   testIsBufferEmptyOrFull(1,      bl - 1, false, true);
 234   // Processed
 235   testIsBufferEmptyOrFull(bl + 1, 0,      false, false);
 236   testIsBufferEmptyOrFull(bl * 2, 0,      false, true);
 237 }
 238 
 239 TEST_VM_F(BufferingOopClosureTest, empty_after_done) {
 240   int bl = BufferingOopClosureTest::get_buffer_length();
 241 
 242   testEmptyAfterDone(0,      0);
 243   testEmptyAfterDone(1,      0);
 244   testEmptyAfterDone(0,      1);
 245   testEmptyAfterDone(1,      1);
 246   testEmptyAfterDone(10,     0);
 247   testEmptyAfterDone(0,      10);
 248   testEmptyAfterDone(10,     10);
 249   testEmptyAfterDone(0,      bl);
 250   testEmptyAfterDone(bl,     0);
 251   testEmptyAfterDone(bl / 2, bl / 2);
 252   testEmptyAfterDone(bl - 1, 1);
 253   testEmptyAfterDone(1,      bl - 1);
 254   // Processed
 255   testEmptyAfterDone(bl + 1, 0);
 256   testEmptyAfterDone(bl * 2, 0);
 257 }