Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/bufferingOopClosure.cpp
38920 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc_implementation/g1/bufferingOopClosure.hpp"26#include "memory/iterator.hpp"27#include "utilities/debug.hpp"2829/////////////// Unit tests ///////////////3031#ifndef PRODUCT3233class TestBufferingOopClosure {3435// Helper class to fake a set of oop*s and narrowOop*s.36class FakeRoots {37public:38// Used for sanity checking of the values passed to the do_oops functions in the test.39static const uintptr_t NarrowOopMarker = uintptr_t(1) << (BitsPerWord -1);4041int _num_narrow;42int _num_full;43void** _narrow;44void** _full;4546FakeRoots(int num_narrow, int num_full) :47_num_narrow(num_narrow),48_num_full(num_full),49_narrow((void**)::malloc(sizeof(void*) * num_narrow)),50_full((void**)::malloc(sizeof(void*) * num_full)) {5152for (int i = 0; i < num_narrow; i++) {53_narrow[i] = (void*)(NarrowOopMarker + (uintptr_t)i);54}55for (int i = 0; i < num_full; i++) {56_full[i] = (void*)(uintptr_t)i;57}58}5960~FakeRoots() {61::free(_narrow);62::free(_full);63}6465void oops_do_narrow_then_full(OopClosure* cl) {66for (int i = 0; i < _num_narrow; i++) {67cl->do_oop((narrowOop*)_narrow[i]);68}69for (int i = 0; i < _num_full; i++) {70cl->do_oop((oop*)_full[i]);71}72}7374void oops_do_full_then_narrow(OopClosure* cl) {75for (int i = 0; i < _num_full; i++) {76cl->do_oop((oop*)_full[i]);77}78for (int i = 0; i < _num_narrow; i++) {79cl->do_oop((narrowOop*)_narrow[i]);80}81}8283void oops_do_mixed(OopClosure* cl) {84int i;85for (i = 0; i < _num_full && i < _num_narrow; i++) {86cl->do_oop((oop*)_full[i]);87cl->do_oop((narrowOop*)_narrow[i]);88}89for (int j = i; j < _num_full; j++) {90cl->do_oop((oop*)_full[i]);91}92for (int j = i; j < _num_narrow; j++) {93cl->do_oop((narrowOop*)_narrow[i]);94}95}9697static const int MaxOrder = 2;9899void oops_do(OopClosure* cl, int do_oop_order) {100switch(do_oop_order) {101case 0:102oops_do_narrow_then_full(cl);103break;104case 1:105oops_do_full_then_narrow(cl);106break;107case 2:108oops_do_mixed(cl);109break;110default:111oops_do_narrow_then_full(cl);112break;113}114}115};116117class CountOopClosure : public OopClosure {118int _narrow_oop_count;119int _full_oop_count;120public:121CountOopClosure() : _narrow_oop_count(0), _full_oop_count(0) {}122void do_oop(narrowOop* p) {123assert((uintptr_t(p) & FakeRoots::NarrowOopMarker) != 0,124"The narrowOop was unexpectedly not marked with the NarrowOopMarker");125_narrow_oop_count++;126}127128void do_oop(oop* p){129assert((uintptr_t(p) & FakeRoots::NarrowOopMarker) == 0,130"The oop was unexpectedly marked with the NarrowOopMarker");131_full_oop_count++;132}133134int narrow_oop_count() { return _narrow_oop_count; }135int full_oop_count() { return _full_oop_count; }136int all_oop_count() { return _narrow_oop_count + _full_oop_count; }137};138139class DoNothingOopClosure : public OopClosure {140public:141void do_oop(narrowOop* p) {}142void do_oop(oop* p) {}143};144145static void testCount(int num_narrow, int num_full, int do_oop_order) {146FakeRoots fr(num_narrow, num_full);147148CountOopClosure coc;149BufferingOopClosure boc(&coc);150151fr.oops_do(&boc, do_oop_order);152153boc.done();154155#define assert_testCount(got, expected) \156assert((got) == (expected), \157err_msg("Expected: %d, got: %d, when running testCount(%d, %d, %d)", \158(got), (expected), num_narrow, num_full, do_oop_order))159160assert_testCount(num_narrow, coc.narrow_oop_count());161assert_testCount(num_full, coc.full_oop_count());162assert_testCount(num_narrow + num_full, coc.all_oop_count());163}164165static void testCount() {166int buffer_length = BufferingOopClosure::BufferLength;167168for (int order = 0; order < FakeRoots::MaxOrder; order++) {169testCount(0, 0, order);170testCount(10, 0, order);171testCount(0, 10, order);172testCount(10, 10, order);173testCount(buffer_length, 10, order);174testCount(10, buffer_length, order);175testCount(buffer_length, buffer_length, order);176testCount(buffer_length + 1, 10, order);177testCount(10, buffer_length + 1, order);178testCount(buffer_length + 1, buffer_length, order);179testCount(buffer_length, buffer_length + 1, order);180testCount(buffer_length + 1, buffer_length + 1, order);181}182}183184static void testIsBufferEmptyOrFull(int num_narrow, int num_full, bool expect_empty, bool expect_full) {185FakeRoots fr(num_narrow, num_full);186187DoNothingOopClosure cl;188BufferingOopClosure boc(&cl);189190fr.oops_do(&boc, 0);191192#define assert_testIsBufferEmptyOrFull(got, expected) \193assert((got) == (expected), \194err_msg("Expected: %d, got: %d. testIsBufferEmptyOrFull(%d, %d, %s, %s)", \195(got), (expected), num_narrow, num_full, \196BOOL_TO_STR(expect_empty), BOOL_TO_STR(expect_full)))197198assert_testIsBufferEmptyOrFull(expect_empty, boc.is_buffer_empty());199assert_testIsBufferEmptyOrFull(expect_full, boc.is_buffer_full());200}201202static void testIsBufferEmptyOrFull() {203int bl = BufferingOopClosure::BufferLength;204205testIsBufferEmptyOrFull(0, 0, true, false);206testIsBufferEmptyOrFull(1, 0, false, false);207testIsBufferEmptyOrFull(0, 1, false, false);208testIsBufferEmptyOrFull(1, 1, false, false);209testIsBufferEmptyOrFull(10, 0, false, false);210testIsBufferEmptyOrFull(0, 10, false, false);211testIsBufferEmptyOrFull(10, 10, false, false);212testIsBufferEmptyOrFull(0, bl, false, true);213testIsBufferEmptyOrFull(bl, 0, false, true);214testIsBufferEmptyOrFull(bl/2, bl/2, false, true);215testIsBufferEmptyOrFull(bl-1, 1, false, true);216testIsBufferEmptyOrFull(1, bl-1, false, true);217// Processed218testIsBufferEmptyOrFull(bl+1, 0, false, false);219testIsBufferEmptyOrFull(bl*2, 0, false, true);220}221222static void testEmptyAfterDone(int num_narrow, int num_full) {223FakeRoots fr(num_narrow, num_full);224225DoNothingOopClosure cl;226BufferingOopClosure boc(&cl);227228fr.oops_do(&boc, 0);229230// Make sure all get processed.231boc.done();232233assert(boc.is_buffer_empty(),234err_msg("Should be empty after call to done(). testEmptyAfterDone(%d, %d)",235num_narrow, num_full));236}237238static void testEmptyAfterDone() {239int bl = BufferingOopClosure::BufferLength;240241testEmptyAfterDone(0, 0);242testEmptyAfterDone(1, 0);243testEmptyAfterDone(0, 1);244testEmptyAfterDone(1, 1);245testEmptyAfterDone(10, 0);246testEmptyAfterDone(0, 10);247testEmptyAfterDone(10, 10);248testEmptyAfterDone(0, bl);249testEmptyAfterDone(bl, 0);250testEmptyAfterDone(bl/2, bl/2);251testEmptyAfterDone(bl-1, 1);252testEmptyAfterDone(1, bl-1);253// Processed254testEmptyAfterDone(bl+1, 0);255testEmptyAfterDone(bl*2, 0);256}257258public:259static void test() {260testCount();261testIsBufferEmptyOrFull();262testEmptyAfterDone();263}264};265266void TestBufferingOopClosure_test() {267TestBufferingOopClosure::test();268}269270#endif271272273