stefank@6969: /* stefank@6969: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. stefank@6969: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. stefank@6969: * stefank@6969: * This code is free software; you can redistribute it and/or modify it stefank@6969: * under the terms of the GNU General Public License version 2 only, as stefank@6969: * published by the Free Software Foundation. stefank@6969: * stefank@6969: * This code is distributed in the hope that it will be useful, but WITHOUT stefank@6969: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or stefank@6969: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License stefank@6969: * version 2 for more details (a copy is included in the LICENSE file that stefank@6969: * accompanied this code). stefank@6969: * stefank@6969: * You should have received a copy of the GNU General Public License version stefank@6969: * 2 along with this work; if not, write to the Free Software Foundation, stefank@6969: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. stefank@6969: * stefank@6969: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA stefank@6969: * or visit www.oracle.com if you need additional information or have any stefank@6969: * questions. stefank@6969: * stefank@6969: */ stefank@6969: stefank@6969: #include "precompiled.hpp" stefank@6969: #include "gc_implementation/g1/bufferingOopClosure.hpp" stefank@6969: #include "memory/iterator.hpp" stefank@6969: #include "utilities/debug.hpp" stefank@6969: stefank@6969: /////////////// Unit tests /////////////// stefank@6969: stefank@6969: #ifndef PRODUCT stefank@6969: stefank@6969: class TestBufferingOopClosure { stefank@6969: stefank@6969: // Helper class to fake a set of oop*s and narrowOop*s. stefank@6969: class FakeRoots { stefank@6969: public: stefank@6969: // Used for sanity checking of the values passed to the do_oops functions in the test. stefank@6969: static const uintptr_t NarrowOopMarker = uintptr_t(1) << (BitsPerWord -1); stefank@6969: stefank@6969: int _num_narrow; stefank@6969: int _num_full; stefank@6969: void** _narrow; stefank@6969: void** _full; stefank@6969: stefank@6969: FakeRoots(int num_narrow, int num_full) : stefank@6969: _num_narrow(num_narrow), stefank@6969: _num_full(num_full), stefank@6969: _narrow((void**)::malloc(sizeof(void*) * num_narrow)), stefank@6969: _full((void**)::malloc(sizeof(void*) * num_full)) { stefank@6969: stefank@6969: for (int i = 0; i < num_narrow; i++) { stefank@6969: _narrow[i] = (void*)(NarrowOopMarker + (uintptr_t)i); stefank@6969: } stefank@6969: for (int i = 0; i < num_full; i++) { stefank@6969: _full[i] = (void*)(uintptr_t)i; stefank@6969: } stefank@6969: } stefank@6969: stefank@6969: ~FakeRoots() { stefank@6969: ::free(_narrow); stefank@6969: ::free(_full); stefank@6969: } stefank@6969: stefank@6969: void oops_do_narrow_then_full(OopClosure* cl) { stefank@6969: for (int i = 0; i < _num_narrow; i++) { stefank@6969: cl->do_oop((narrowOop*)_narrow[i]); stefank@6969: } stefank@6969: for (int i = 0; i < _num_full; i++) { stefank@6969: cl->do_oop((oop*)_full[i]); stefank@6969: } stefank@6969: } stefank@6969: stefank@6969: void oops_do_full_then_narrow(OopClosure* cl) { stefank@6969: for (int i = 0; i < _num_full; i++) { stefank@6969: cl->do_oop((oop*)_full[i]); stefank@6969: } stefank@6969: for (int i = 0; i < _num_narrow; i++) { stefank@6969: cl->do_oop((narrowOop*)_narrow[i]); stefank@6969: } stefank@6969: } stefank@6969: stefank@6969: void oops_do_mixed(OopClosure* cl) { stefank@6969: int i; stefank@6969: for (i = 0; i < _num_full && i < _num_narrow; i++) { stefank@6969: cl->do_oop((oop*)_full[i]); stefank@6969: cl->do_oop((narrowOop*)_narrow[i]); stefank@6969: } stefank@6969: for (int j = i; j < _num_full; j++) { stefank@6969: cl->do_oop((oop*)_full[i]); stefank@6969: } stefank@6969: for (int j = i; j < _num_narrow; j++) { stefank@6969: cl->do_oop((narrowOop*)_narrow[i]); stefank@6969: } stefank@6969: } stefank@6969: stefank@6969: static const int MaxOrder = 2; stefank@6969: stefank@6969: void oops_do(OopClosure* cl, int do_oop_order) { stefank@6969: switch(do_oop_order) { stefank@6969: case 0: stefank@6969: oops_do_narrow_then_full(cl); stefank@6969: break; stefank@6969: case 1: stefank@6969: oops_do_full_then_narrow(cl); stefank@6969: break; stefank@6969: case 2: stefank@6969: oops_do_mixed(cl); stefank@6969: break; stefank@6969: default: stefank@6969: oops_do_narrow_then_full(cl); stefank@6969: break; stefank@6969: } stefank@6969: } stefank@6969: }; stefank@6969: stefank@6969: class CountOopClosure : public OopClosure { stefank@6969: int _narrow_oop_count; stefank@6969: int _full_oop_count; stefank@6969: public: stefank@6969: CountOopClosure() : _narrow_oop_count(0), _full_oop_count(0) {} stefank@6969: void do_oop(narrowOop* p) { stefank@6969: assert((uintptr_t(p) & FakeRoots::NarrowOopMarker) != 0, stefank@6969: "The narrowOop was unexpectedly not marked with the NarrowOopMarker"); stefank@6969: _narrow_oop_count++; stefank@6969: } stefank@6969: stefank@6969: void do_oop(oop* p){ stefank@6969: assert((uintptr_t(p) & FakeRoots::NarrowOopMarker) == 0, stefank@6969: "The oop was unexpectedly marked with the NarrowOopMarker"); stefank@6969: _full_oop_count++; stefank@6969: } stefank@6969: stefank@6969: int narrow_oop_count() { return _narrow_oop_count; } stefank@6969: int full_oop_count() { return _full_oop_count; } stefank@6969: int all_oop_count() { return _narrow_oop_count + _full_oop_count; } stefank@6969: }; stefank@6969: stefank@6969: class DoNothingOopClosure : public OopClosure { stefank@6969: public: stefank@6969: void do_oop(narrowOop* p) {} stefank@6969: void do_oop(oop* p) {} stefank@6969: }; stefank@6969: stefank@6969: static void testCount(int num_narrow, int num_full, int do_oop_order) { stefank@6969: FakeRoots fr(num_narrow, num_full); stefank@6969: stefank@6969: CountOopClosure coc; stefank@6969: BufferingOopClosure boc(&coc); stefank@6969: stefank@6969: fr.oops_do(&boc, do_oop_order); stefank@6969: stefank@6969: boc.done(); stefank@6969: stefank@6969: #define assert_testCount(got, expected) \ stefank@6969: assert((got) == (expected), \ stefank@6969: err_msg("Expected: %d, got: %d, when running testCount(%d, %d, %d)", \ stefank@6969: (got), (expected), num_narrow, num_full, do_oop_order)) stefank@6969: stefank@6969: assert_testCount(num_narrow, coc.narrow_oop_count()); stefank@6969: assert_testCount(num_full, coc.full_oop_count()); stefank@6969: assert_testCount(num_narrow + num_full, coc.all_oop_count()); stefank@6969: } stefank@6969: stefank@6969: static void testCount() { stefank@6969: int buffer_length = BufferingOopClosure::BufferLength; stefank@6969: stefank@6969: for (int order = 0; order < FakeRoots::MaxOrder; order++) { stefank@6969: testCount(0, 0, order); stefank@6969: testCount(10, 0, order); stefank@6969: testCount(0, 10, order); stefank@6969: testCount(10, 10, order); stefank@6969: testCount(buffer_length, 10, order); stefank@6969: testCount(10, buffer_length, order); stefank@6969: testCount(buffer_length, buffer_length, order); stefank@6969: testCount(buffer_length + 1, 10, order); stefank@6969: testCount(10, buffer_length + 1, order); stefank@6969: testCount(buffer_length + 1, buffer_length, order); stefank@6969: testCount(buffer_length, buffer_length + 1, order); stefank@6969: testCount(buffer_length + 1, buffer_length + 1, order); stefank@6969: } stefank@6969: } stefank@6969: stefank@6969: static void testIsBufferEmptyOrFull(int num_narrow, int num_full, bool expect_empty, bool expect_full) { stefank@6969: FakeRoots fr(num_narrow, num_full); stefank@6969: stefank@6969: DoNothingOopClosure cl; stefank@6969: BufferingOopClosure boc(&cl); stefank@6969: stefank@6969: fr.oops_do(&boc, 0); stefank@6969: stefank@6969: #define assert_testIsBufferEmptyOrFull(got, expected) \ stefank@6969: assert((got) == (expected), \ stefank@6969: err_msg("Expected: %d, got: %d. testIsBufferEmptyOrFull(%d, %d, %s, %s)", \ stefank@6969: (got), (expected), num_narrow, num_full, \ stefank@6969: BOOL_TO_STR(expect_empty), BOOL_TO_STR(expect_full))) stefank@6969: stefank@6969: assert_testIsBufferEmptyOrFull(expect_empty, boc.is_buffer_empty()); stefank@6969: assert_testIsBufferEmptyOrFull(expect_full, boc.is_buffer_full()); stefank@6969: } stefank@6969: stefank@6969: static void testIsBufferEmptyOrFull() { stefank@6969: int bl = BufferingOopClosure::BufferLength; stefank@6969: stefank@6969: testIsBufferEmptyOrFull(0, 0, true, false); stefank@6969: testIsBufferEmptyOrFull(1, 0, false, false); stefank@6969: testIsBufferEmptyOrFull(0, 1, false, false); stefank@6969: testIsBufferEmptyOrFull(1, 1, false, false); stefank@6969: testIsBufferEmptyOrFull(10, 0, false, false); stefank@6969: testIsBufferEmptyOrFull(0, 10, false, false); stefank@6969: testIsBufferEmptyOrFull(10, 10, false, false); stefank@6969: testIsBufferEmptyOrFull(0, bl, false, true); stefank@6969: testIsBufferEmptyOrFull(bl, 0, false, true); stefank@6969: testIsBufferEmptyOrFull(bl/2, bl/2, false, true); stefank@6969: testIsBufferEmptyOrFull(bl-1, 1, false, true); stefank@6969: testIsBufferEmptyOrFull(1, bl-1, false, true); stefank@6969: // Processed stefank@6969: testIsBufferEmptyOrFull(bl+1, 0, false, false); stefank@6969: testIsBufferEmptyOrFull(bl*2, 0, false, true); stefank@6969: } stefank@6969: stefank@6969: static void testEmptyAfterDone(int num_narrow, int num_full) { stefank@6969: FakeRoots fr(num_narrow, num_full); stefank@6969: stefank@6969: DoNothingOopClosure cl; stefank@6969: BufferingOopClosure boc(&cl); stefank@6969: stefank@6969: fr.oops_do(&boc, 0); stefank@6969: stefank@6969: // Make sure all get processed. stefank@6969: boc.done(); stefank@6969: stefank@6969: assert(boc.is_buffer_empty(), stefank@6969: err_msg("Should be empty after call to done(). testEmptyAfterDone(%d, %d)", stefank@6969: num_narrow, num_full)); stefank@6969: } stefank@6969: stefank@6969: static void testEmptyAfterDone() { stefank@6969: int bl = BufferingOopClosure::BufferLength; stefank@6969: stefank@6969: testEmptyAfterDone(0, 0); stefank@6969: testEmptyAfterDone(1, 0); stefank@6969: testEmptyAfterDone(0, 1); stefank@6969: testEmptyAfterDone(1, 1); stefank@6969: testEmptyAfterDone(10, 0); stefank@6969: testEmptyAfterDone(0, 10); stefank@6969: testEmptyAfterDone(10, 10); stefank@6969: testEmptyAfterDone(0, bl); stefank@6969: testEmptyAfterDone(bl, 0); stefank@6969: testEmptyAfterDone(bl/2, bl/2); stefank@6969: testEmptyAfterDone(bl-1, 1); stefank@6969: testEmptyAfterDone(1, bl-1); stefank@6969: // Processed stefank@6969: testEmptyAfterDone(bl+1, 0); stefank@6969: testEmptyAfterDone(bl*2, 0); stefank@6969: } stefank@6969: stefank@6969: public: stefank@6969: static void test() { stefank@6969: testCount(); stefank@6969: testIsBufferEmptyOrFull(); stefank@6969: testEmptyAfterDone(); stefank@6969: } stefank@6969: }; stefank@6969: stefank@6969: void TestBufferingOopClosure_test() { stefank@6969: TestBufferingOopClosure::test(); stefank@6969: } stefank@6969: stefank@6969: #endif