Skip to content

Commit 7ac88d2

Browse files
Replace ThreadCritical with a specific NMT lock
1 parent 66945e5 commit 7ac88d2

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

src/hotspot/share/nmt/mallocTracker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ void MemoryCounter::update_peak(size_t size, size_t cnt) {
6262
}
6363

6464
void MallocMemorySnapshot::copy_to(MallocMemorySnapshot* s) {
65-
// Use ThreadCritical to make sure that mtChunks don't get deallocated while the
65+
// Use NmtGuard to make sure that mtChunks don't get deallocated while the
6666
// copy is going on, because their size is adjusted using this
6767
// buffer in make_adjustment().
68-
ThreadCritical tc;
68+
NmtGuard guard;
6969
s->_all_mallocs = _all_mallocs;
7070
size_t total_size = 0;
7171
size_t total_count = 0;

src/hotspot/share/nmt/memTracker.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,6 @@
3131
#include "nmt/threadStackTracker.hpp"
3232
#include "nmt/virtualMemoryTracker.hpp"
3333
#include "runtime/mutexLocker.hpp"
34-
#include "runtime/threadCritical.hpp"
3534
#include "utilities/debug.hpp"
3635
#include "utilities/nativeCallStack.hpp"
3736

@@ -125,7 +124,7 @@ class MemTracker : AllStatic {
125124
assert_post_init();
126125
if (!enabled()) return;
127126
if (addr != nullptr) {
128-
ThreadCritical tc;
127+
NmtGuard guard;
129128
VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, flag);
130129
}
131130
}
@@ -151,7 +150,7 @@ class MemTracker : AllStatic {
151150
assert_post_init();
152151
if (!enabled()) return;
153152
if (addr != nullptr) {
154-
ThreadCritical tc;
153+
NmtGuard guard;
155154
VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, flag);
156155
VirtualMemoryTracker::add_committed_region((address)addr, size, stack);
157156
}
@@ -162,7 +161,7 @@ class MemTracker : AllStatic {
162161
assert_post_init();
163162
if (!enabled()) return;
164163
if (addr != nullptr) {
165-
ThreadCritical tc;
164+
NmtGuard guard;
166165
VirtualMemoryTracker::add_committed_region((address)addr, size, stack);
167166
}
168167
}
@@ -210,7 +209,7 @@ class MemTracker : AllStatic {
210209
assert_post_init();
211210
if (!enabled()) return;
212211
if (addr != nullptr) {
213-
ThreadCritical tc;
212+
NmtGuard guard;
214213
VirtualMemoryTracker::split_reserved_region((address)addr, size, split, flag, split_flag);
215214
}
216215
}
@@ -219,7 +218,7 @@ class MemTracker : AllStatic {
219218
assert_post_init();
220219
if (!enabled()) return;
221220
if (addr != nullptr) {
222-
ThreadCritical tc;
221+
NmtGuard guard;
223222
VirtualMemoryTracker::set_reserved_region_type((address)addr, flag);
224223
}
225224
}

src/hotspot/share/nmt/nmtCommon.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,6 +36,9 @@ NMTUtil::S NMTUtil::_strings[] = {
3636
MEMORY_TYPES_DO(MEMORY_TYPE_DECLARE_NAME)
3737
};
3838

39+
Semaphore NmtGuard::_nmt_semaphore(1);
40+
intx NmtGuard::owner_id(-1);
41+
3942
const char* NMTUtil::scale_name(size_t scale) {
4043
switch(scale) {
4144
case 1: return "";

src/hotspot/share/nmt/nmtCommon.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "memory/allStatic.hpp"
3131
#include "nmt/memflags.hpp"
32+
#include "runtime/os.hpp"
33+
#include "runtime/semaphore.hpp"
3234
#include "utilities/align.hpp"
3335
#include "utilities/globalDefinitions.hpp"
3436

@@ -137,5 +139,24 @@ class NMTUtil : AllStatic {
137139
static S _strings[mt_number_of_types];
138140
};
139141

142+
class NmtGuard : public StackObj {
143+
private:
144+
static Semaphore _nmt_semaphore;
145+
static intx owner_id;
146+
public:
147+
NmtGuard() {
148+
intx tid = os::current_thread_id();
149+
assert(tid != owner_id, "We already own the NMT lock. Not reentrant.");
150+
_nmt_semaphore.wait();
151+
owner_id = tid;
152+
}
153+
~NmtGuard() {
154+
owner_id = -1;
155+
_nmt_semaphore.signal();
156+
}
157+
static void assert_locked(){
158+
assert(!_nmt_semaphore.trywait(), "NMT lock should be acquired in this section.");
159+
}
160+
};
140161

141162
#endif // SHARE_NMT_NMTCOMMON_HPP

src/hotspot/share/nmt/nmtUsage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "nmt/nmtUsage.hpp"
2929
#include "nmt/threadStackTracker.hpp"
3030
#include "nmt/virtualMemoryTracker.hpp"
31-
#include "runtime/threadCritical.hpp"
3231

3332
// Enabled all options for snapshot.
3433
const NMTUsageOptions NMTUsage::OptionsAll = { true, true, true };
@@ -53,7 +52,7 @@ void NMTUsage::walk_thread_stacks() {
5352
void NMTUsage::update_malloc_usage() {
5453
// Thread critical needed keep values in sync, total area size
5554
// is deducted from mtChunk in the end to give correct values.
56-
ThreadCritical tc;
55+
NmtGuard guard;
5756
const MallocMemorySnapshot* ms = MallocMemorySummary::as_snapshot();
5857

5958
size_t total_arena_size = 0;

src/hotspot/share/nmt/threadStackTracker.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "nmt/threadStackTracker.hpp"
3030
#include "nmt/virtualMemoryTracker.hpp"
3131
#include "runtime/os.hpp"
32-
#include "runtime/threadCritical.hpp"
3332
#include "utilities/align.hpp"
3433
#include "utilities/debug.hpp"
3534
#include "utilities/globalDefinitions.hpp"
@@ -53,7 +52,7 @@ void ThreadStackTracker::new_thread_stack(void* base, size_t size, const NativeC
5352
assert(base != nullptr, "Should have been filtered");
5453
align_thread_stack_boundaries_inward(base, size);
5554

56-
ThreadCritical tc;
55+
NmtGuard guard;
5756
VirtualMemoryTracker::add_reserved_region((address)base, size, stack, mtThreadStack);
5857
_thread_count++;
5958
}
@@ -63,7 +62,7 @@ void ThreadStackTracker::delete_thread_stack(void* base, size_t size) {
6362
assert(base != nullptr, "Should have been filtered");
6463
align_thread_stack_boundaries_inward(base, size);
6564

66-
ThreadCritical tc;
65+
NmtGuard guard;
6766
VirtualMemoryTracker::remove_released_region((address)base, size);
6867
_thread_count--;
6968
}

src/hotspot/share/nmt/virtualMemoryTracker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "nmt/threadStackTracker.hpp"
3131
#include "nmt/virtualMemoryTracker.hpp"
3232
#include "runtime/os.hpp"
33-
#include "runtime/threadCritical.hpp"
3433
#include "utilities/ostream.hpp"
3534

3635
VirtualMemorySnapshot VirtualMemorySummary::_snapshot;
@@ -621,6 +620,7 @@ class SnapshotThreadStackWalker : public VirtualMemoryWalker {
621620
SnapshotThreadStackWalker() {}
622621

623622
bool do_allocation_site(const ReservedMemoryRegion* rgn) {
623+
NmtGuard::assert_locked();
624624
if (rgn->flag() == mtThreadStack) {
625625
address stack_bottom = rgn->thread_stack_uncommitted_bottom();
626626
address committed_start;
@@ -661,7 +661,7 @@ void VirtualMemoryTracker::snapshot_thread_stacks() {
661661

662662
bool VirtualMemoryTracker::walk_virtual_memory(VirtualMemoryWalker* walker) {
663663
assert(_reserved_regions != nullptr, "Sanity check");
664-
ThreadCritical tc;
664+
NmtGuard guard;
665665
// Check that the _reserved_regions haven't been deleted.
666666
if (_reserved_regions != nullptr) {
667667
LinkedListNode<ReservedMemoryRegion>* head = _reserved_regions->head();

0 commit comments

Comments
 (0)