Path: blob/master/test/hotspot/gtest/metaspace/metaspaceGtestContexts.cpp
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "memory/metaspace/chunkManager.hpp"27#include "memory/metaspace/metaspaceSettings.hpp"28#include "metaspaceGtestCommon.hpp"29#include "metaspaceGtestContexts.hpp"3031using metaspace::Settings;3233void ChunkGtestContext::checked_alloc_chunk_0(Metachunk** p_return_value, chunklevel_t preferred_level, chunklevel_t max_level,34size_t min_committed_size) {3536*p_return_value = NULL;3738Metachunk* c = cm().get_chunk(preferred_level, max_level, min_committed_size);3940if (c != NULL) {4142ASSERT_LE(c->level(), max_level);43ASSERT_GE(c->level(), preferred_level);44ASSERT_GE(c->committed_words(), min_committed_size);45ASSERT_EQ(c->committed_words(), c->free_below_committed_words());46ASSERT_EQ(c->used_words(), (size_t)0);47ASSERT_TRUE(c->is_in_use());48ASSERT_FALSE(c->is_free());49ASSERT_FALSE(c->is_dead());50ASSERT_NULL(c->next());51ASSERT_NULL(c->prev());52if (c->level() == HIGHEST_CHUNK_LEVEL) {53ASSERT_TRUE(c->is_leaf_chunk());54} else {55ASSERT_FALSE(c->is_leaf_chunk());56}57if (c->level() == LOWEST_CHUNK_LEVEL) {58ASSERT_TRUE(c->is_root_chunk());59} else {60ASSERT_FALSE(c->is_root_chunk());61}62if (_num_chunks_allocated == 0) { // First chunk? We can make more assumptions63ASSERT_EQ(c->level(), preferred_level);64// Needs lock EXPECT_NULL(c->next_in_vs());65// Needs lock EXPECT_NULL(c->prev_in_vs());66ASSERT_TRUE(c->is_root_chunk() || c->is_leader());67}68if (Settings::new_chunks_are_fully_committed()) {69ASSERT_TRUE(c->is_fully_committed());70}7172_num_chunks_allocated++;7374}7576*p_return_value = c;7778}7980// Test pattern established when allocating from the chunk with allocate_from_chunk_with_tests().81void ChunkGtestContext::test_pattern(Metachunk* c, size_t word_size) {82check_range_for_pattern(c->base(), word_size, (uintx)c);83}8485void ChunkGtestContext::return_chunk(Metachunk* c) {86test_pattern(c);87c->set_in_use(); // Forestall assert in cm88cm().return_chunk(c);89}9091void ChunkGtestContext::allocate_from_chunk(MetaWord** p_return_value, Metachunk* c, size_t word_size) {9293size_t used_before = c->used_words();94size_t free_before = c->free_words();95size_t free_below_committed_before = c->free_below_committed_words();96const MetaWord* top_before = c->top();9798MetaWord* p = c->allocate(word_size);99EXPECT_NOT_NULL(p);100EXPECT_EQ(c->used_words(), used_before + word_size);101EXPECT_EQ(c->free_words(), free_before - word_size);102EXPECT_EQ(c->free_below_committed_words(), free_below_committed_before - word_size);103EXPECT_EQ(c->top(), top_before + word_size);104105// Old content should be preserved106test_pattern(c, used_before);107108// Fill newly allocated range too109fill_range_with_pattern(p, word_size, (uintx)c);110111*p_return_value = p;112}113114void ChunkGtestContext::commit_chunk_with_test(Metachunk* c, size_t additional_size) {115116size_t used_before = c->used_words();117size_t free_before = c->free_words();118const MetaWord* top_before = c->top();119120c->set_in_use();121bool b = c->ensure_committed_additional(additional_size);122EXPECT_TRUE(b);123124// We should have enough committed size now125EXPECT_GE(c->free_below_committed_words(), additional_size);126127// used, free, top should be unchanged.128EXPECT_EQ(c->used_words(), used_before);129EXPECT_EQ(c->free_words(), free_before);130EXPECT_EQ(c->top(), top_before);131132test_pattern(c, used_before);133134}135136void ChunkGtestContext::commit_chunk_expect_failure(Metachunk* c, size_t additional_size) {137138size_t used_before = c->used_words();139size_t free_before = c->free_words();140size_t free_below_committed_before = c->free_below_committed_words();141const MetaWord* top_before = c->top();142143c->set_in_use();144bool b = c->ensure_committed_additional(additional_size);145EXPECT_FALSE(b);146147// Nothing should have changed148EXPECT_EQ(c->used_words(), used_before);149EXPECT_EQ(c->free_words(), free_before);150EXPECT_EQ(c->free_below_committed_words(), free_below_committed_before);151EXPECT_EQ(c->top(), top_before);152153test_pattern(c, used_before);154155}156157void ChunkGtestContext::uncommit_chunk_with_test(Metachunk* c) {158if (c->word_size() >= Settings::commit_granule_words()) {159c->set_free(); // Forestall assert in uncommit160c->reset_used_words();161c->uncommit();162163EXPECT_EQ(c->free_below_committed_words(), (size_t)0);164EXPECT_EQ(c->used_words(), (size_t)0);165EXPECT_EQ(c->free_words(), c->word_size());166EXPECT_EQ(c->top(), c->base());167EXPECT_TRUE(c->is_fully_uncommitted());168}169}170171/////// SparseArray<T> ////////////////172173174175