Skip to content

Commit 2329ce7

Browse files
committed
8202073: MetaspaceAllocationTest gtest shall lock during space creation
Reviewed-by: coleenp
1 parent 04e986f commit 2329ce7

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/hotspot/share/memory/metaspace.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,6 +3378,7 @@ void SpaceManager::track_metaspace_memory_usage() {
33783378
}
33793379

33803380
MetaWord* SpaceManager::grow_and_allocate(size_t word_size) {
3381+
assert_lock_strong(_lock);
33813382
assert(vs_list()->current_virtual_space() != NULL,
33823383
"Should have been set");
33833384
assert(current_chunk() == NULL ||
@@ -3553,6 +3554,7 @@ void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
35533554
// Adds a chunk to the list of chunks in use.
35543555
void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
35553556

3557+
assert_lock_strong(_lock);
35563558
assert(new_chunk != NULL, "Should not be NULL");
35573559
assert(new_chunk->next() == NULL, "Should not be on a list");
35583560

test/hotspot/gtest/memory/test_metaspace_allocation.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "memory/allocation.inline.hpp"
2727
#include "memory/metaspace.hpp"
2828
#include "runtime/mutex.hpp"
29+
#include "runtime/mutexLocker.hpp"
2930
#include "runtime/os.hpp"
3031
#include "utilities/align.hpp"
3132
#include "utilities/debug.hpp"
@@ -104,7 +105,12 @@ class MetaspaceAllocationTest : public ::testing::Test {
104105
// Let every ~10th space be an anonymous one to test different allocation patterns.
105106
const Metaspace::MetaspaceType msType = (os::random() % 100 < 10) ?
106107
Metaspace::AnonymousMetaspaceType : Metaspace::StandardMetaspaceType;
107-
_spaces[i].space = new ClassLoaderMetaspace(_spaces[i].lock, msType);
108+
{
109+
// Pull lock during space creation, since this is what happens in the VM too
110+
// (see ClassLoaderData::metaspace_non_null(), which we mimick here).
111+
MutexLockerEx ml(_spaces[i].lock, Mutex::_no_safepoint_check_flag);
112+
_spaces[i].space = new ClassLoaderMetaspace(_spaces[i].lock, msType);
113+
}
108114
_spaces[i].allocated = 0;
109115
ASSERT_TRUE(_spaces[i].space != NULL);
110116
}
@@ -171,6 +177,7 @@ class MetaspaceAllocationTest : public ::testing::Test {
171177
} else {
172178
size = os::random() % 64;
173179
}
180+
// Note: In contrast to space creation, no need to lock here. ClassLoaderMetaspace::allocate() will lock itself.
174181
MetaWord* const p = _spaces[index].space->allocate(size, mdType);
175182
if (p == NULL) {
176183
// We very probably did hit the metaspace "until-gc" limit.
@@ -196,6 +203,7 @@ class MetaspaceAllocationTest : public ::testing::Test {
196203
force_switch = true;
197204
} else {
198205
assert(_spaces[index].space != NULL && _spaces[index].allocated > 0, "Sanity");
206+
// Note: do not lock here. In the "wild" (the VM), we do not so either (see ~ClassLoaderData()).
199207
delete _spaces[index].space;
200208
_spaces[index].space = NULL;
201209
_spaces[index].allocated = 0;

0 commit comments

Comments
 (0)