1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023 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.
*
*/
#include "memory/metaspace/chunkManager.hpp"
#include "memory/metaspace/metaspaceSettings.hpp"
#include "metaspaceGtestCommon.hpp"
#include "metaspaceGtestContexts.hpp"
using metaspace::Settings;
void ChunkGtestContext::checked_alloc_chunk_0(Metachunk** p_return_value, chunklevel_t preferred_level, chunklevel_t max_level,
size_t min_committed_size) {
*p_return_value = nullptr;
Metachunk* c = cm().get_chunk(preferred_level, max_level, min_committed_size);
if (c != nullptr) {
ASSERT_LE(c->level(), max_level);
ASSERT_GE(c->level(), preferred_level);
ASSERT_GE(c->committed_words(), min_committed_size);
ASSERT_EQ(c->committed_words(), c->free_below_committed_words());
ASSERT_EQ(c->used_words(), (size_t)0);
ASSERT_TRUE(c->is_in_use());
ASSERT_FALSE(c->is_free());
ASSERT_FALSE(c->is_dead());
ASSERT_NULL(c->next());
ASSERT_NULL(c->prev());
if (c->level() == HIGHEST_CHUNK_LEVEL) {
ASSERT_TRUE(c->is_leaf_chunk());
} else {
ASSERT_FALSE(c->is_leaf_chunk());
}
if (c->level() == LOWEST_CHUNK_LEVEL) {
ASSERT_TRUE(c->is_root_chunk());
} else {
ASSERT_FALSE(c->is_root_chunk());
}
if (_num_chunks_allocated == 0) { // First chunk? We can make more assumptions
ASSERT_EQ(c->level(), preferred_level);
// Needs lock EXPECT_NULL(c->next_in_vs());
// Needs lock EXPECT_NULL(c->prev_in_vs());
ASSERT_TRUE(c->is_root_chunk() || c->is_leader());
}
_num_chunks_allocated++;
}
*p_return_value = c;
}
// Test pattern established when allocating from the chunk with allocate_from_chunk_with_tests().
void ChunkGtestContext::test_pattern(Metachunk* c, size_t word_size) {
check_range_for_pattern(c->base(), word_size, (uintx)c);
}
void ChunkGtestContext::return_chunk(Metachunk* c) {
test_pattern(c);
c->set_in_use(); // Forestall assert in cm
cm().return_chunk(c);
}
void ChunkGtestContext::allocate_from_chunk(MetaWord** p_return_value, Metachunk* c, size_t word_size) {
size_t used_before = c->used_words();
size_t free_before = c->free_words();
size_t free_below_committed_before = c->free_below_committed_words();
const MetaWord* top_before = c->top();
MetaWord* p = c->allocate(word_size);
EXPECT_NOT_NULL(p);
EXPECT_EQ(c->used_words(), used_before + word_size);
EXPECT_EQ(c->free_words(), free_before - word_size);
EXPECT_EQ(c->free_below_committed_words(), free_below_committed_before - word_size);
EXPECT_EQ(c->top(), top_before + word_size);
// Old content should be preserved
test_pattern(c, used_before);
// Fill newly allocated range too
fill_range_with_pattern(p, word_size, (uintx)c);
*p_return_value = p;
}
void ChunkGtestContext::commit_chunk_with_test(Metachunk* c, size_t additional_size) {
size_t used_before = c->used_words();
size_t free_before = c->free_words();
const MetaWord* top_before = c->top();
c->set_in_use();
bool b = c->ensure_committed_additional(additional_size);
EXPECT_TRUE(b);
// We should have enough committed size now
EXPECT_GE(c->free_below_committed_words(), additional_size);
// used, free, top should be unchanged.
EXPECT_EQ(c->used_words(), used_before);
EXPECT_EQ(c->free_words(), free_before);
EXPECT_EQ(c->top(), top_before);
test_pattern(c, used_before);
}
void ChunkGtestContext::commit_chunk_expect_failure(Metachunk* c, size_t additional_size) {
size_t used_before = c->used_words();
size_t free_before = c->free_words();
size_t free_below_committed_before = c->free_below_committed_words();
const MetaWord* top_before = c->top();
c->set_in_use();
bool b = c->ensure_committed_additional(additional_size);
EXPECT_FALSE(b);
// Nothing should have changed
EXPECT_EQ(c->used_words(), used_before);
EXPECT_EQ(c->free_words(), free_before);
EXPECT_EQ(c->free_below_committed_words(), free_below_committed_before);
EXPECT_EQ(c->top(), top_before);
test_pattern(c, used_before);
}
void ChunkGtestContext::uncommit_chunk_with_test(Metachunk* c) {
if (c->word_size() >= Settings::commit_granule_words()) {
c->set_free(); // Forestall assert in uncommit
c->reset_used_words();
c->uncommit();
EXPECT_EQ(c->free_below_committed_words(), (size_t)0);
EXPECT_EQ(c->used_words(), (size_t)0);
EXPECT_EQ(c->free_words(), c->word_size());
EXPECT_EQ(c->top(), c->base());
EXPECT_TRUE(c->is_fully_uncommitted());
}
}
/////// SparseArray<T> ////////////////
|