Quellcode-Bibliothek test_blocktree.cpp
Sprache: C
/* * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2020 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
using metaspace::BlockTree; using metaspace::MemRangeCounter;
// Small helper. Given a 0-terminated array of sizes, a feeder buffer and a tree, // add blocks of these sizes to the tree in the order they appear in the array. staticvoid create_nodes(const size_t sizes[], FeederBuffer& fb, BlockTree& bt) { for (int i = 0; sizes[i] > 0; i ++) {
size_t s = sizes[i];
MetaWord* p = fb.get(s);
bt.add_block(p, s);
}
}
size_t real_size = 0;
MetaWord* p = NULL;
MetaWord arr[10000];
ASSERT_LE(BlockTree::MinWordSize, (size_t)6); // Sanity check. Adjust if Node is changed.
const size_t minws = BlockTree::MinWordSize;
// remove_block from empty tree should yield nothing
p = bt.remove_block(minws, &real_size);
EXPECT_NULL(p);
EXPECT_0(real_size);
CHECK_BT_CONTENT(bt, 0, 0);
// Add some blocks and retrieve them right away.
size_t sizes[] = {
minws, // smallest possible
minws + 10,
1024,
4711,
0
};
for (int i = 0; sizes[i] > 0; i++) {
bt.add_block(arr, sizes[i]);
CHECK_BT_CONTENT(bt, 1, sizes[i]);
// Helper for test_find_nearest_fit_with_tree. // Out of an array of sizes return the closest upper match to a requested size. // Returns SIZE_MAX if none found. static size_t helper_find_nearest_fit(const size_t sizes[], size_t request_size) {
size_t best = SIZE_MAX; for (int i = 0; sizes[i] > 0; i++) { if (sizes[i] >= request_size && sizes[i] < best) {
best = sizes[i];
}
} return best;
}
// Given a sequence of (0-terminated) sizes, add blocks of those sizes to the tree in the order given. Then, ask // for a request size and check that it is the expected result. staticvoid test_find_nearest_fit_with_tree(const size_t sizes[], size_t request_size) {
for (int i = BlockTree::MinWordSize; i <= 60; i ++) {
test_find_nearest_fit_with_tree(sizes, i);
}
}
// Test repeated adding and removing of blocks of the same size, which // should exercise the list-part of the tree.
TEST_VM(metaspace, BlockTree_basic_siblings)
{
BlockTree bt;
FeederBuffer fb(4 * K);
CHECK_BT_CONTENT(bt, 0, 0);
const size_t test_size = BlockTree::MinWordSize; constint num = 10;
for (int i = 0; i < num; i++) {
bt.add_block(fb.get(test_size), test_size);
CHECK_BT_CONTENT(bt, i + 1, (i + 1) * test_size);
}
DEBUG_ONLY(bt.verify();)
for (int i = num; i > 0; i --) {
size_t real_size = 4711;
MetaWord* p = bt.remove_block(test_size, &real_size);
EXPECT_TRUE(fb.is_valid_pointer(p));
EXPECT_EQ(real_size, (size_t)test_size);
CHECK_BT_CONTENT(bt, i - 1, (i - 1) * test_size);
}
// Test that an overwritten node would result in an assert and a printed tree
TEST_VM_ASSERT_MSG(metaspace, BlockTree_overwriter_test, ".*failed: Invalid node") { staticconst size_t sizes1[] = { 30, 17, 0 }; staticconst size_t sizes2[] = { 12, 12, 0 };
BlockTree bt;
FeederBuffer fb(4 * K);
// some nodes...
create_nodes(sizes1, fb, bt);
// a node we will break...
MetaWord* p_broken = fb.get(12);
bt.add_block(p_broken, 12);
// some more nodes...
create_nodes(sizes2, fb, bt);
// overwrite node memory (only the very first byte), then verify tree. // Verification should catch the broken canary, print the tree, // then assert.
LOG("Will break node at " PTR_FORMAT ".", p2i(p_broken));
tty->print_cr("Death test, please ignore the following \"Invalid node\" printout.");
*((char*)p_broken) = '\0';
bt.verify();
} #endif
// Feed the whole feeder buffer to the trees, according to feeding_pattern. void feed_all(feeding_pattern_t feeding_pattern) {
MetaWord* p = NULL; unsigned added = 0;
// If we feed in small graining, we cap the number of blocks to limit test duration. constunsigned max_blocks = 2000;
size_t old_feeding_size = feeding_pattern == right_left ? _rgen.max() : _rgen.min(); do {
size_t s = 0; switch (feeding_pattern) { case scatter: // fill completely random
s =_rgen.get(); break; case left_right: // fill in ascending order to provoke a misformed tree.
s = MIN2(_rgen.get(), old_feeding_size);
old_feeding_size = s; break; case right_left: // same, but descending.
s = MAX2(_rgen.get(), old_feeding_size);
old_feeding_size = s; break;
}
// Get a block from the feeder buffer; feed it alternatingly to either tree.
p = _fb.get(s); if (p != NULL) { int which = added % 2;
added++;
_bt[which].add_block(p, s);
_cnt[which].add(s);
CHECK_COUNTERS
}
} while (p != NULL && added < max_blocks);
DEBUG_ONLY(verify_trees();)
// Trees should contain the same number of nodes (+-1)
EXPECT_TRUE(_bt[0].count() == _bt[1].count() ||
_bt[0].count() == _bt[1].count() + 1);
}
void ping_pong_loop(int iterations) {
// We loop and in each iteration randomly retrieve a block from one tree and add it to another. for (int i = 0; i < iterations; i++) { int taker = 0; int giver = 1; if ((os::random() % 10) > 5) {
giver = 0; taker = 1;
}
size_t s =_rgen.get();
size_t real_size = 0;
MetaWord* p = _bt[giver].remove_block(s, &real_size); if (p != NULL) {
ASSERT_TRUE(_fb.is_valid_range(p, real_size));
ASSERT_GE(real_size, s);
_bt[taker].add_block(p, real_size);
_cnt[giver].sub(real_size);
_cnt[taker].add(real_size);
CHECK_COUNTERS;
}
// Drain the trees. While draining, observe the order of the drained items. void drain_all() {
for (int which = 0; which < 2; which++) {
BlockTree* bt = _bt + which;
size_t last_size = 0; while (!bt->is_empty()) {
// We only query for the minimal size. Actually returned size should be // monotonously growing since remove_block should always return the closest fit.
size_t real_size = 4711;
MetaWord* p = bt->remove_block(BlockTree::MinWordSize, &real_size);
ASSERT_TRUE(_fb.is_valid_range(p, real_size));
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.21Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.