Skip to content

Commit 547ce03

Browse files
author
Afshin Zafari
committed
8337217: Port VirtualMemoryTracker to use VMATree
Reviewed-by: jsjolen, gziemski
1 parent f07f5ce commit 547ce03

27 files changed

+892
-888
lines changed

src/hotspot/share/nmt/memBaseline.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ bool MemBaseline::baseline_allocation_sites() {
160160

161161
// Virtual memory allocation sites
162162
VirtualMemoryAllocationWalker virtual_memory_walker;
163-
if (!VirtualMemoryTracker::walk_virtual_memory(&virtual_memory_walker)) {
163+
if (!VirtualMemoryTracker::Instance::walk_virtual_memory(&virtual_memory_walker)) {
164164
return false;
165165
}
166166

src/hotspot/share/nmt/memMapPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class CachedNMTInformation : public VirtualMemoryWalker {
157157

158158
// Iterate all NMT virtual memory regions and fill this cache.
159159
bool fill_from_nmt() {
160-
return VirtualMemoryTracker::walk_virtual_memory(this);
160+
return VirtualMemoryTracker::Instance::walk_virtual_memory(this);
161161
}
162162
};
163163

src/hotspot/share/nmt/memReporter.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
*
2323
*/
2424
#include "cds/filemap.hpp"
25+
#include "logging/log.hpp"
2526
#include "memory/metaspace.hpp"
2627
#include "memory/metaspaceUtils.hpp"
2728
#include "nmt/mallocTracker.hpp"
2829
#include "nmt/memTag.hpp"
2930
#include "nmt/memReporter.hpp"
3031
#include "nmt/memTracker.hpp"
3132
#include "nmt/memoryFileTracker.hpp"
33+
#include "nmt/regionsTree.hpp"
34+
#include "nmt/regionsTree.inline.hpp"
3235
#include "nmt/threadStackTracker.hpp"
3336
#include "nmt/virtualMemoryTracker.hpp"
3437
#include "utilities/debug.hpp"
@@ -432,34 +435,45 @@ void MemDetailReporter::report_virtual_memory_region(const ReservedMemoryRegion*
432435
}
433436

434437
if (all_committed) {
435-
CommittedRegionIterator itr = reserved_rgn->iterate_committed_regions();
436-
const CommittedMemoryRegion* committed_rgn = itr.next();
437-
if (committed_rgn->size() == reserved_rgn->size() && committed_rgn->call_stack()->equals(*stack)) {
438-
// One region spanning the entire reserved region, with the same stack trace.
439-
// Don't print this regions because the "reserved and committed" line above
440-
// already indicates that the region is committed.
441-
assert(itr.next() == nullptr, "Unexpectedly more than one regions");
438+
bool reserved_and_committed = false;
439+
VirtualMemoryTracker::Instance::tree()->visit_committed_regions(*reserved_rgn,
440+
[&](CommittedMemoryRegion& committed_rgn) {
441+
if (committed_rgn.equals(*reserved_rgn)) {
442+
// One region spanning the entire reserved region, with the same stack trace.
443+
// Don't print this regions because the "reserved and committed" line above
444+
// already indicates that the region is committed.
445+
reserved_and_committed = true;
446+
return false;
447+
}
448+
return true;
449+
});
450+
451+
if (reserved_and_committed) {
442452
return;
443453
}
444454
}
445455

446-
CommittedRegionIterator itr = reserved_rgn->iterate_committed_regions();
447-
const CommittedMemoryRegion* committed_rgn;
448-
while ((committed_rgn = itr.next()) != nullptr) {
456+
auto print_committed_rgn = [&](const CommittedMemoryRegion& crgn) {
449457
// Don't report if size is too small
450-
if (amount_in_current_scale(committed_rgn->size()) == 0) continue;
451-
stack = committed_rgn->call_stack();
458+
if (amount_in_current_scale(crgn.size()) == 0) return;
459+
stack = crgn.call_stack();
452460
out->cr();
453461
INDENT_BY(8,
454-
print_virtual_memory_region("committed", committed_rgn->base(), committed_rgn->size());
462+
print_virtual_memory_region("committed", crgn.base(), crgn.size());
455463
if (stack->is_empty()) {
456464
out->cr();
457465
} else {
458466
out->print_cr(" from");
459-
INDENT_BY(4, stack->print_on(out);)
467+
INDENT_BY(4, _stackprinter.print_stack(stack);)
460468
}
461469
)
462-
}
470+
};
471+
472+
VirtualMemoryTracker::Instance::tree()->visit_committed_regions(*reserved_rgn,
473+
[&](CommittedMemoryRegion& crgn) {
474+
print_committed_rgn(crgn);
475+
return true;
476+
});
463477
}
464478

465479
void MemDetailReporter::report_memory_file_allocations() {

src/hotspot/share/nmt/memTracker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void MemTracker::initialize() {
7171
_baseline.initialize();
7272
if (!MallocTracker::initialize(level) ||
7373
!MemoryFileTracker::Instance::initialize(level) ||
74-
!VirtualMemoryTracker::initialize(level)) {
74+
!VirtualMemoryTracker::Instance::initialize(level)) {
7575
assert(false, "NMT initialization failed");
7676
level = NMT_off;
7777
log_warning(nmt)("NMT initialization failed. NMT disabled.");
@@ -126,7 +126,7 @@ void MemTracker::final_report(outputStream* output) {
126126
bool MemTracker::print_containing_region(const void* p, outputStream* out) {
127127
return enabled() &&
128128
(MallocTracker::print_pointer_information(p, out) ||
129-
VirtualMemoryTracker::print_containing_region(p, out));
129+
VirtualMemoryTracker::Instance::print_containing_region(p, out));
130130
}
131131

132132
void MemTracker::report(bool summary_only, outputStream* output, size_t scale) {

src/hotspot/share/nmt/memTracker.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,23 @@ class MemTracker : AllStatic {
132132
if (!enabled()) return;
133133
if (addr != nullptr) {
134134
NmtVirtualMemoryLocker nvml;
135-
VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, mem_tag);
135+
VirtualMemoryTracker::Instance::add_reserved_region((address)addr, size, stack, mem_tag);
136136
}
137137
}
138138

139139
static inline void record_virtual_memory_release(void* addr, size_t size) {
140140
assert_post_init();
141141
if (!enabled()) return;
142142
if (addr != nullptr) {
143-
VirtualMemoryTracker::remove_released_region((address)addr, size);
143+
VirtualMemoryTracker::Instance::remove_released_region((address)addr, size);
144144
}
145145
}
146146

147147
static inline void record_virtual_memory_uncommit(void* addr, size_t size) {
148148
assert_post_init();
149149
if (!enabled()) return;
150150
if (addr != nullptr) {
151-
VirtualMemoryTracker::remove_uncommitted_region((address)addr, size);
151+
VirtualMemoryTracker::Instance::remove_uncommitted_region((address)addr, size);
152152
}
153153
}
154154

@@ -158,8 +158,8 @@ class MemTracker : AllStatic {
158158
if (!enabled()) return;
159159
if (addr != nullptr) {
160160
NmtVirtualMemoryLocker nvml;
161-
VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, mem_tag);
162-
VirtualMemoryTracker::add_committed_region((address)addr, size, stack);
161+
VirtualMemoryTracker::Instance::add_reserved_region((address)addr, size, stack, mem_tag);
162+
VirtualMemoryTracker::Instance::add_committed_region((address)addr, size, stack);
163163
}
164164
}
165165

@@ -169,7 +169,7 @@ class MemTracker : AllStatic {
169169
if (!enabled()) return;
170170
if (addr != nullptr) {
171171
NmtVirtualMemoryLocker nvml;
172-
VirtualMemoryTracker::add_committed_region((address)addr, size, stack);
172+
VirtualMemoryTracker::Instance::add_committed_region((address)addr, size, stack);
173173
}
174174
}
175175

@@ -217,7 +217,7 @@ class MemTracker : AllStatic {
217217
if (!enabled()) return;
218218
if (addr != nullptr) {
219219
NmtVirtualMemoryLocker nvml;
220-
VirtualMemoryTracker::split_reserved_region((address)addr, size, split, mem_tag, split_tag);
220+
VirtualMemoryTracker::Instance::split_reserved_region((address)addr, size, split, mem_tag, split_tag);
221221
}
222222
}
223223

@@ -230,7 +230,7 @@ class MemTracker : AllStatic {
230230
if (!enabled()) return;
231231
if (addr != nullptr) {
232232
NmtVirtualMemoryLocker nvml;
233-
VirtualMemoryTracker::set_reserved_region_type((address)addr, size, mem_tag);
233+
VirtualMemoryTracker::Instance::set_reserved_region_tag((address)addr, size, mem_tag);
234234
}
235235
}
236236

src/hotspot/share/nmt/memoryFileTracker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void MemoryFileTracker::print_report_on(const MemoryFile* file, outputStream* st
7373
if (prev == nullptr) {
7474
// Must be first node.
7575
prev = current;
76-
return;
76+
return true;
7777
}
7878
#ifdef ASSERT
7979
if (broken_start != nullptr && prev->val().out.mem_tag() != current->val().in.mem_tag()) {
@@ -96,6 +96,7 @@ void MemoryFileTracker::print_report_on(const MemoryFile* file, outputStream* st
9696
stream->cr();
9797
}
9898
prev = current;
99+
return true;
99100
});
100101
#ifdef ASSERT
101102
if (broken_start != nullptr) {

src/hotspot/share/nmt/memoryFileTracker.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#include "memory/allocation.hpp"
2929
#include "nmt/nmtCommon.hpp"
3030
#include "nmt/nmtNativeCallStackStorage.hpp"
31-
#include "nmt/virtualMemoryTracker.hpp"
3231
#include "nmt/vmatree.hpp"
32+
#include "nmt/virtualMemoryTracker.hpp"
3333
#include "runtime/os.inline.hpp"
3434
#include "utilities/growableArray.hpp"
3535
#include "utilities/nativeCallStack.hpp"

src/hotspot/share/nmt/nmtTreap.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ template<typename K, typename V, typename COMPARATOR, typename ALLOCATOR>
5555
class Treap {
5656
friend class NMTVMATreeTest;
5757
friend class NMTTreapTest;
58+
friend class VMTWithVMATreeTest;
5859
public:
5960
class TreapNode {
6061
friend Treap;
@@ -212,12 +213,13 @@ class Treap {
212213
seen_count++;
213214
if (last_seen == nullptr) {
214215
last_seen = node;
215-
return;
216+
return true;
216217
}
217218
if (COMPARATOR::cmp(last_seen->key(), node->key()) > 0) {
218219
failed = false;
219220
}
220221
last_seen = node;
222+
return true;
221223
});
222224
assert(seen_count == _node_count, "the number of visited nodes do not match with the number of stored nodes");
223225
assert(!failed, "keys was not monotonically strongly increasing when visiting in order");
@@ -382,7 +384,9 @@ class Treap {
382384
head = head->left();
383385
}
384386
head = to_visit.pop();
385-
f(head);
387+
if (!f(head)) {
388+
return;
389+
}
386390
head = head->right();
387391
}
388392
}
@@ -409,7 +413,9 @@ class Treap {
409413
const int cmp_from = COMPARATOR::cmp(head->key(), from);
410414
const int cmp_to = COMPARATOR::cmp(head->key(), to);
411415
if (cmp_from >= 0 && cmp_to < 0) {
412-
f(head);
416+
if (!f(head)) {
417+
return;
418+
}
413419
}
414420
if (cmp_to < 0) {
415421
head = head->right();

src/hotspot/share/nmt/nmtUsage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "nmt/nmtCommon.hpp"
3030
#include "nmt/nmtUsage.hpp"
3131
#include "nmt/threadStackTracker.hpp"
32-
#include "nmt/virtualMemoryTracker.hpp"
3332

3433
// Enabled all options for snapshot.
3534
const NMTUsageOptions NMTUsage::OptionsAll = { true, true, true };
@@ -48,7 +47,7 @@ void NMTUsage::walk_thread_stacks() {
4847
// much memory had been committed if they are backed by virtual memory. This
4948
// needs to happen before we take the snapshot of the virtual memory since it
5049
// will update this information.
51-
VirtualMemoryTracker::snapshot_thread_stacks();
50+
VirtualMemoryTracker::Instance::snapshot_thread_stacks();
5251
}
5352

5453
void NMTUsage::update_malloc_usage() {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. 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 it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 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 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
#include "nmt/regionsTree.hpp"
25+
26+
VMATree::SummaryDiff RegionsTree::commit_region(address addr, size_t size, const NativeCallStack& stack) {
27+
return commit_mapping((VMATree::position)addr, size, make_region_data(stack, mtNone), /*use tag inplace*/ true);
28+
}
29+
30+
VMATree::SummaryDiff RegionsTree::uncommit_region(address addr, size_t size) {
31+
return uncommit_mapping((VMATree::position)addr, size, make_region_data(NativeCallStack::empty_stack(), mtNone));
32+
}
33+
34+
#ifdef ASSERT
35+
void RegionsTree::NodeHelper::print_on(outputStream* st) {
36+
auto st_str = [&](VMATree::StateType s){
37+
return s == VMATree::StateType::Released ? "Rl" :
38+
s == VMATree::StateType::Reserved ? "Rv" : "Cm";
39+
};
40+
st->print_cr("pos: " INTPTR_FORMAT " "
41+
"%s, %s <|> %s, %s",
42+
p2i((address)position()),
43+
st_str(in_state()),
44+
NMTUtil::tag_to_name(in_tag()),
45+
st_str(out_state()),
46+
NMTUtil::tag_to_name(out_tag())
47+
);
48+
}
49+
50+
void RegionsTree::print_on(outputStream* st) {
51+
visit_in_order([&](Node* node) {
52+
NodeHelper curr(node);
53+
curr.print_on(st);
54+
return true;
55+
});
56+
}
57+
#endif

0 commit comments

Comments
 (0)