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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/*
* Copyright (c) 2020, 2023 SAP SE. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. 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/freeChunkList.hpp"
#include "memory/metaspace/metachunk.hpp"
#include "memory/metaspace/metaspaceSettings.hpp"
#include "memory/metaspace/virtualSpaceNode.hpp"
#include "metaspaceGtestCommon.hpp"
#include "metaspaceGtestContexts.hpp"
#include "runtime/mutexLocker.hpp"
using metaspace::ChunkManager;
using metaspace::FreeChunkListVector;
using metaspace::Metachunk;
using metaspace::Settings;
using metaspace::VirtualSpaceNode;
using namespace metaspace::chunklevel;
// Test ChunkManager::get_chunk
TEST_VM(metaspace, get_chunk) {
ChunkGtestContext context(8 * M);
Metachunk* c = nullptr;
for (chunklevel_t pref_lvl = LOWEST_CHUNK_LEVEL; pref_lvl <= HIGHEST_CHUNK_LEVEL; pref_lvl++) {
for (chunklevel_t max_lvl = pref_lvl; max_lvl <= HIGHEST_CHUNK_LEVEL; max_lvl++) {
for (size_t min_committed_words = Settings::commit_granule_words();
min_committed_words <= word_size_for_level(max_lvl); min_committed_words *= 2) {
context.alloc_chunk_expect_success(&c, pref_lvl, max_lvl, min_committed_words);
context.return_chunk(c);
}
}
}
}
// Test ChunkManager::get_chunk, but with a commit limit.
TEST_VM(metaspace, get_chunk_with_commit_limit) {
// A commit limit that is smaller than the largest possible chunk size.
// Here we test different combinations of commit limit, preferred and highest chunk level, and min_committed_size.
for (size_t commit_limit_words = Settings::commit_granule_words();
commit_limit_words < MAX_CHUNK_WORD_SIZE * 2; commit_limit_words *= 2) {
ChunkGtestContext context(commit_limit_words);
Metachunk* c = nullptr;
for (chunklevel_t pref_lvl = LOWEST_CHUNK_LEVEL; pref_lvl <= HIGHEST_CHUNK_LEVEL; pref_lvl++) {
for (chunklevel_t max_lvl = pref_lvl; max_lvl <= HIGHEST_CHUNK_LEVEL; max_lvl++) {
for (size_t min_committed_words = Settings::commit_granule_words();
min_committed_words <= word_size_for_level(max_lvl); min_committed_words *= 2) {
// When should commit work? As long as min_committed_words is smaller than commit_limit_words.
bool commit_should_work = min_committed_words <= commit_limit_words;
// printf("commit_limit: %zu, min_committed_words: %zu"
// ", max chunk level: " CHKLVL_FORMAT ", preferred chunk level: " CHKLVL_FORMAT ", should work: %d\n",
// commit_limit_words, min_committed_words, max_lvl, pref_lvl, commit_should_work);
// fflush(stdout);
if (commit_should_work) {
context.alloc_chunk_expect_success(&c, pref_lvl, max_lvl, min_committed_words);
context.return_chunk(c);
} else {
context.alloc_chunk_expect_failure(pref_lvl, max_lvl, min_committed_words);
}
}
}
}
}
}
// Test that recommitting the used portion of a chunk will preserve the original content.
TEST_VM(metaspace, get_chunk_recommit) {
ChunkGtestContext context;
Metachunk* c = nullptr;
context.alloc_chunk_expect_success(&c, ROOT_CHUNK_LEVEL, ROOT_CHUNK_LEVEL, 0);
context.uncommit_chunk_with_test(c);
context.commit_chunk_with_test(c, Settings::commit_granule_words());
context.allocate_from_chunk(c, Settings::commit_granule_words());
c->ensure_committed(Settings::commit_granule_words());
check_range_for_pattern(c->base(), c->used_words(), (uintx)c);
c->ensure_committed(Settings::commit_granule_words() * 2);
check_range_for_pattern(c->base(), c->used_words(), (uintx)c);
context.return_chunk(c);
}
// Test ChunkManager::get_chunk, but with a reserve limit.
// (meaning, the underlying VirtualSpaceList cannot expand, like compressed class space).
TEST_VM(metaspace, get_chunk_with_reserve_limit) {
const size_t reserve_limit_words = word_size_for_level(ROOT_CHUNK_LEVEL);
const size_t commit_limit_words = 1024 * M; // just very high
ChunkGtestContext context(commit_limit_words, reserve_limit_words);
// Reserve limit works at root chunk size granularity: if the chunk manager cannot satisfy
// a request for a chunk from its freelists, it will acquire a new root chunk from the
// underlying virtual space list. If that list is full and cannot be expanded (think ccs)
// we should get an error.
// Testing this is simply testing a chunk allocation which should cause allocation of a new
// root chunk.
// Cause allocation of the firstone root chunk, should still work:
Metachunk* c = nullptr;
context.alloc_chunk_expect_success(&c, HIGHEST_CHUNK_LEVEL);
// and this should need a new root chunk and hence fail:
context.alloc_chunk_expect_failure(ROOT_CHUNK_LEVEL);
context.return_chunk(c);
}
// Test MetaChunk::allocate
TEST_VM(metaspace, chunk_allocate_full) {
ChunkGtestContext context;
for (chunklevel_t lvl = LOWEST_CHUNK_LEVEL; lvl <= HIGHEST_CHUNK_LEVEL; lvl++) {
Metachunk* c = nullptr;
context.alloc_chunk_expect_success(&c, lvl);
context.allocate_from_chunk(c, c->word_size());
context.return_chunk(c);
}
}
// Test MetaChunk::allocate
TEST_VM(metaspace, chunk_allocate_random) {
ChunkGtestContext context;
for (chunklevel_t lvl = LOWEST_CHUNK_LEVEL; lvl <= HIGHEST_CHUNK_LEVEL; lvl++) {
Metachunk* c = nullptr;
context.alloc_chunk_expect_success(&c, lvl);
context.uncommit_chunk_with_test(c); // start out fully uncommitted
RandSizeGenerator rgen(1, c->word_size() / 30);
bool stop = false;
while (!stop) {
const size_t s = rgen.get();
if (s <= c->free_words()) {
context.commit_chunk_with_test(c, s);
context.allocate_from_chunk(c, s);
} else {
stop = true;
}
}
context.return_chunk(c);
}
}
TEST_VM(metaspace, chunk_buddy_stuff) {
for (chunklevel_t l = ROOT_CHUNK_LEVEL + 1; l <= HIGHEST_CHUNK_LEVEL; l++) {
ChunkGtestContext context;
// Allocate two chunks; since we know the first chunk is the first in its area,
// it has to be a leader, and the next one of the same size its buddy.
// (Note: strictly speaking the ChunkManager does not promise any placement but
// we know how the placement works so these tests make sense).
Metachunk* c1 = nullptr;
context.alloc_chunk(&c1, CHUNK_LEVEL_1K);
EXPECT_TRUE(c1->is_leader());
Metachunk* c2 = nullptr;
context.alloc_chunk(&c2, CHUNK_LEVEL_1K);
EXPECT_FALSE(c2->is_leader());
// buddies are adjacent in memory
// (next/prev_in_vs needs lock)
{
MutexLocker fcl(Metaspace_lock, Mutex::_no_safepoint_check_flag);
EXPECT_EQ(c1->next_in_vs(), c2);
EXPECT_EQ(c1->end(), c2->base());
EXPECT_NULL(c1->prev_in_vs()); // since we know this is the first in the area
EXPECT_EQ(c2->prev_in_vs(), c1);
}
context.return_chunk(c1);
context.return_chunk(c2);
}
}
TEST_VM(metaspace, chunk_allocate_with_commit_limit) {
const size_t granule_sz = Settings::commit_granule_words();
const size_t commit_limit = granule_sz * 3;
ChunkGtestContext context(commit_limit);
// A big chunk, but uncommitted.
Metachunk* c = nullptr;
context.alloc_chunk_expect_success(&c, ROOT_CHUNK_LEVEL, ROOT_CHUNK_LEVEL, 0);
context.uncommit_chunk_with_test(c); // ... just to make sure.
// first granule...
context.commit_chunk_with_test(c, granule_sz);
context.allocate_from_chunk(c, granule_sz);
// second granule...
context.commit_chunk_with_test(c, granule_sz);
context.allocate_from_chunk(c, granule_sz);
// third granule...
context.commit_chunk_with_test(c, granule_sz);
context.allocate_from_chunk(c, granule_sz);
// This should fail now.
context.commit_chunk_expect_failure(c, granule_sz);
context.return_chunk(c);
}
// Test splitting a chunk
TEST_VM(metaspace, chunk_split_and_merge) {
// Split works like this:
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// | A |
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// | A' | b | c | d | e |
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// A original chunk (A) is split to form a target chunk (A') and as a result splinter
// chunks form (b..e). A' is the leader of the (A',b) pair, which is the leader of the
// ((A',b), c) pair and so on. In other words, A' will be a leader chunk, all splinter
// chunks are follower chunks.
//
// Merging reverses this operation:
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// | A | b | c | d | e |
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// | A' |
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// (A) will be merged with its buddy b, (A+b) with its buddy c and so on. The result
// chunk is A'.
// Note that merging also works, of course, if we were to start the merge at (b) (so,
// with a follower chunk, not a leader). Also, at any point in the merge
// process we may arrive at a follower chunk. So, the fact that in this test
// we only expect a leader merge is a feature of the test, and of the fact that we
// start each split test with a fresh ChunkTestsContext.
// Note: Splitting and merging chunks is usually done from within the ChunkManager and
// subject to a lot of assumptions and hence asserts. Here, we have to explicitly use
// VirtualSpaceNode::split/::merge and therefore have to observe rules:
// - both split and merge expect free chunks, so state has to be "free"
// - but that would trigger the "ideally merged" assertion in the RootChunkArea, so the
// original chunk has to be a root chunk, we cannot just split any chunk manually.
// - Also, after the split we have to completely re-merge to avoid triggering asserts
// in ~RootChunkArea()
// - finally we have to lock manually
ChunkGtestContext context;
const chunklevel_t orig_lvl = ROOT_CHUNK_LEVEL;
for (chunklevel_t target_lvl = orig_lvl + 1; target_lvl <= HIGHEST_CHUNK_LEVEL; target_lvl++) {
// Split a fully committed chunk. The resulting chunk should be fully
// committed as well, and have its content preserved.
Metachunk* c = nullptr;
context.alloc_chunk_expect_success(&c, orig_lvl);
// We allocate from this chunk to be able to completely paint the payload.
context.allocate_from_chunk(c, c->word_size());
const uintx canary = os::random();
fill_range_with_pattern(c->base(), c->word_size(), canary);
FreeChunkListVector splinters;
{
// Splitting/Merging chunks is usually done by the chunkmanager, and no explicit
// outside API exists. So we split/merge chunks via the underlying vs node, directly.
// This means that we have to go through some extra hoops to not trigger any asserts.
MutexLocker fcl(Metaspace_lock, Mutex::_no_safepoint_check_flag);
c->reset_used_words();
c->set_free();
c->vsnode()->split(target_lvl, c, &splinters);
}
DEBUG_ONLY(context.verify();)
EXPECT_EQ(c->level(), target_lvl);
EXPECT_TRUE(c->is_fully_committed());
EXPECT_FALSE(c->is_root_chunk());
EXPECT_TRUE(c->is_leader());
check_range_for_pattern(c->base(), c->word_size(), canary);
// I expect splinter chunks (one for each splinter level:
// e.g. splitting a 1M chunk to get a 64K chunk should yield splinters: [512K, 256K, 128K, 64K]
for (chunklevel_t l = LOWEST_CHUNK_LEVEL; l < HIGHEST_CHUNK_LEVEL; l++) {
const Metachunk* c2 = splinters.first_at_level(l);
if (l > orig_lvl && l <= target_lvl) {
EXPECT_NOT_NULL(c2);
EXPECT_EQ(c2->level(), l);
EXPECT_TRUE(c2->is_free());
EXPECT_TRUE(!c2->is_leader());
DEBUG_ONLY(c2->verify());
check_range_for_pattern(c2->base(), c2->word_size(), canary);
} else {
EXPECT_NULL(c2);
}
}
// Revert the split by using merge. This should result in all splinters coalescing
// to one chunk.
{
MutexLocker fcl(Metaspace_lock, Mutex::_no_safepoint_check_flag);
Metachunk* merged = c->vsnode()->merge(c, &splinters);
// the merged chunk should occupy the same address as the splinter
// since it should have been the leader in the split.
EXPECT_EQ(merged, c);
EXPECT_TRUE(merged->is_root_chunk() || merged->is_leader());
// Splitting should have arrived at the original chunk since none of the splinters are in use.
EXPECT_EQ(c->level(), orig_lvl);
// All splinters should have been removed from the list
EXPECT_EQ(splinters.num_chunks(), 0);
}
context.return_chunk(c);
}
}
TEST_VM(metaspace, chunk_enlarge_in_place) {
ChunkGtestContext context;
// Starting with the smallest chunk size, attempt to enlarge the chunk in place until we arrive
// at root chunk size. Since the state is clean, this should work.
Metachunk* c = nullptr;
context.alloc_chunk_expect_success(&c, HIGHEST_CHUNK_LEVEL);
chunklevel_t l = c->level();
while (l != ROOT_CHUNK_LEVEL) {
// commit and allocate from chunk to pattern it...
const size_t original_chunk_size = c->word_size();
context.commit_chunk_with_test(c, c->free_words());
context.allocate_from_chunk(c, c->free_words());
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();
EXPECT_TRUE(context.cm().attempt_enlarge_chunk(c));
EXPECT_EQ(l - 1, c->level());
EXPECT_EQ(c->word_size(), original_chunk_size * 2);
// Used words should not have changed
EXPECT_EQ(c->used_words(), used_before);
EXPECT_EQ(c->top(), top_before);
// free words should be expanded by the old size (since old chunk is doubled in size)
EXPECT_EQ(c->free_words(), free_before + original_chunk_size);
// free below committed can be larger but never smaller
EXPECT_GE(c->free_below_committed_words(), free_below_committed_before);
// Old content should be preserved
check_range_for_pattern(c->base(), original_chunk_size, (uintx)c);
l = c->level();
}
context.return_chunk(c);
}
|