Skip to content

Commit e139005

Browse files
committed
8333994: NMT: call stacks should show source information
Reviewed-by: jsjolen, gziemski
1 parent b88af94 commit e139005

8 files changed

+181
-63
lines changed

src/hotspot/share/nmt/memReporter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int MemDetailReporter::report_malloc_sites() {
337337
continue;
338338
}
339339
const NativeCallStack* stack = malloc_site->call_stack();
340-
stack->print_on(out);
340+
_stackprinter.print_stack(stack);
341341
MEMFLAGS flag = malloc_site->flag();
342342
assert(NMTUtil::flag_is_valid(flag) && flag != mtNone,
343343
"Must have a valid memory type");
@@ -374,7 +374,7 @@ int MemDetailReporter::report_virtual_memory_allocation_sites() {
374374
continue;
375375
}
376376
const NativeCallStack* stack = virtual_memory_site->call_stack();
377-
stack->print_on(out);
377+
_stackprinter.print_stack(stack);
378378
INDENT_BY(29,
379379
out->print("(");
380380
print_total(virtual_memory_site->reserved(), virtual_memory_site->committed());
@@ -428,7 +428,7 @@ void MemDetailReporter::report_virtual_memory_region(const ReservedMemoryRegion*
428428
out->cr();
429429
} else {
430430
out->print_cr(" from");
431-
INDENT_BY(4, stack->print_on(out);)
431+
INDENT_BY(4, _stackprinter.print_stack(stack);)
432432
}
433433

434434
if (all_committed) {
@@ -869,7 +869,7 @@ void MemDetailDiffReporter::diff_malloc_site(const NativeCallStack* stack, size_
869869
return;
870870
}
871871

872-
stack->print_on(out);
872+
_stackprinter.print_stack(stack);
873873
INDENT_BY(28,
874874
out->print("(");
875875
print_malloc_diff(current_size, current_count, early_size, early_count, flags);
@@ -904,7 +904,7 @@ void MemDetailDiffReporter::diff_virtual_memory_site(const NativeCallStack* stac
904904
return;
905905
}
906906

907-
stack->print_on(out);
907+
_stackprinter.print_stack(stack);
908908
INDENT_BY(28,
909909
out->print("(mmap: ");
910910
print_virtual_memory_diff(current_reserved, current_committed, early_reserved, early_committed);

src/hotspot/share/nmt/memReporter.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
#include "memory/metaspace.hpp"
2929
#include "nmt/mallocTracker.hpp"
3030
#include "nmt/memBaseline.hpp"
31+
#include "nmt/nativeCallStackPrinter.hpp"
3132
#include "nmt/nmtCommon.hpp"
3233
#include "nmt/virtualMemoryTracker.hpp"
34+
#include "utilities/nativeCallStack.hpp"
3335

3436
/*
3537
* Base class that provides helpers
@@ -149,11 +151,11 @@ class MemSummaryReporter : public MemReporterBase {
149151
class MemDetailReporter : public MemSummaryReporter {
150152
private:
151153
MemBaseline& _baseline;
152-
154+
NativeCallStackPrinter _stackprinter;
153155
public:
154156
MemDetailReporter(MemBaseline& baseline, outputStream* output, size_t scale = default_scale) :
155157
MemSummaryReporter(baseline, output, scale),
156-
_baseline(baseline) { }
158+
_baseline(baseline), _stackprinter(output) { }
157159

158160
// Generate detail report.
159161
// The report contains summary and detail sections.
@@ -229,10 +231,12 @@ class MemSummaryDiffReporter : public MemReporterBase {
229231
* both baselines have to be detail baseline.
230232
*/
231233
class MemDetailDiffReporter : public MemSummaryDiffReporter {
234+
NativeCallStackPrinter _stackprinter;
232235
public:
233236
MemDetailDiffReporter(MemBaseline& early_baseline, MemBaseline& current_baseline,
234237
outputStream* output, size_t scale = default_scale) :
235-
MemSummaryDiffReporter(early_baseline, current_baseline, output, scale) { }
238+
MemSummaryDiffReporter(early_baseline, current_baseline, output, scale),
239+
_stackprinter(output) { }
236240

237241
// Generate detail comparison report
238242
virtual void report_diff();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2024, Red Hat, Inc. All rights reserved.
3+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21+
* or visit www.oracle.com if you need additional information or have any
22+
* questions.
23+
*
24+
*/
25+
26+
#include "precompiled.hpp"
27+
#include "logging/log.hpp"
28+
#include "nmt/nativeCallStackPrinter.hpp"
29+
#include "utilities/globalDefinitions.hpp"
30+
#include "utilities/nativeCallStack.hpp"
31+
#include "utilities/ostream.hpp"
32+
33+
NativeCallStackPrinter::NativeCallStackPrinter(outputStream* out) :
34+
_text_storage(mtNMT, Arena::Tag::tag_other, 128 * K), _out(out)
35+
{}
36+
37+
void NativeCallStackPrinter::print_stack(const NativeCallStack* stack) const {
38+
for (int i = 0; i < NMT_TrackingStackDepth; i++) {
39+
const address pc = stack->get_frame(i);
40+
if (pc == nullptr) {
41+
break;
42+
}
43+
bool created = false;
44+
const char** cached_frame_text = _cache.put_if_absent(pc, &created);
45+
if (created) {
46+
stringStream ss(4 * K);
47+
stack->print_frame(&ss, pc);
48+
const size_t len = ss.size();
49+
char* store = NEW_ARENA_ARRAY(&_text_storage, char, len + 1);
50+
memcpy(store, ss.base(), len + 1);
51+
(*cached_frame_text) = store;
52+
}
53+
_out->print_raw_cr(*cached_frame_text);
54+
}
55+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2024, Red Hat, Inc. All rights reserved.
3+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21+
* or visit www.oracle.com if you need additional information or have any
22+
* questions.
23+
*
24+
*/
25+
26+
#ifndef SHARE_NMT_NATIVECALLSTACKPRINTER_HPP
27+
#define SHARE_NMT_NATIVECALLSTACKPRINTER_HPP
28+
29+
#include "memory/arena.hpp"
30+
#include "nmt/memflags.hpp"
31+
#include "utilities/globalDefinitions.hpp"
32+
#include "utilities/resourceHash.hpp"
33+
34+
class outputStream;
35+
class NativeCallStack;
36+
37+
// This is a text cache for NativeCallStack frames by PC. When printing tons of
38+
// NativeCallStack instances (e.g. during NMT detail reports), printing through
39+
// this printer speeds up frame description resolution by quite a bit.
40+
class NativeCallStackPrinter {
41+
// Cache-related data are mutable to be able to use NativeCallStackPrinter as
42+
// inline member in classes with const printing methods.
43+
mutable Arena _text_storage;
44+
mutable ResourceHashtable<address, const char*, 293, AnyObj::C_HEAP, mtNMT> _cache;
45+
outputStream* const _out;
46+
public:
47+
NativeCallStackPrinter(outputStream* out);
48+
void print_stack(const NativeCallStack* stack) const;
49+
};
50+
51+
#endif // SHARE_NMT_NATIVECALLSTACKPRINTER_HPP

src/hotspot/share/nmt/virtualMemoryTracker.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "memory/metaspaceStats.hpp"
2727
#include "memory/metaspaceUtils.hpp"
2828
#include "nmt/memTracker.hpp"
29+
#include "nmt/nativeCallStackPrinter.hpp"
2930
#include "nmt/threadStackTracker.hpp"
3031
#include "nmt/virtualMemoryTracker.hpp"
3132
#include "runtime/os.hpp"
@@ -679,16 +680,17 @@ class PrintRegionWalker : public VirtualMemoryWalker {
679680
private:
680681
const address _p;
681682
outputStream* _st;
683+
NativeCallStackPrinter _stackprinter;
682684
public:
683685
PrintRegionWalker(const void* p, outputStream* st) :
684-
_p((address)p), _st(st) { }
686+
_p((address)p), _st(st), _stackprinter(st) { }
685687

686688
bool do_allocation_site(const ReservedMemoryRegion* rgn) {
687689
if (rgn->contain_address(_p)) {
688690
_st->print_cr(PTR_FORMAT " in mmap'd memory region [" PTR_FORMAT " - " PTR_FORMAT "], tag %s",
689691
p2i(_p), p2i(rgn->base()), p2i(rgn->base() + rgn->size()), NMTUtil::flag_to_enum_name(rgn->flag()));
690692
if (MemTracker::tracking_level() == NMT_detail) {
691-
rgn->call_stack()->print_on(_st);
693+
_stackprinter.print_stack(rgn->call_stack());
692694
_st->cr();
693695
}
694696
return false;

src/hotspot/share/utilities/nativeCallStack.cpp

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 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
@@ -72,44 +72,48 @@ int NativeCallStack::frames() const {
7272
}
7373

7474
// Decode and print this call path
75-
void NativeCallStack::print_on(outputStream* out) const {
76-
DEBUG_ONLY(assert_not_fake();)
77-
address pc;
75+
76+
void NativeCallStack::print_frame(outputStream* out, address pc) const {
7877
char buf[1024];
7978
int offset;
80-
if (is_empty()) {
81-
out->print("[BOOTSTRAP]");
82-
} else {
83-
for (int frame = 0; frame < NMT_TrackingStackDepth; frame ++) {
84-
pc = get_frame(frame);
85-
if (pc == nullptr) break;
86-
out->print("[" PTR_FORMAT "]", p2i(pc));
87-
// Print function and library; shorten library name to just its last component
88-
// for brevity, and omit it completely for libjvm.so
89-
bool function_printed = false;
90-
if (os::dll_address_to_function_name(pc, buf, sizeof(buf), &offset)) {
91-
out->print("%s+0x%x", buf, offset);
92-
function_printed = true;
93-
}
94-
if ((!function_printed || !os::address_is_in_vm(pc)) &&
95-
os::dll_address_to_library_name(pc, buf, sizeof(buf), &offset)) {
96-
const char* libname = strrchr(buf, os::file_separator()[0]);
97-
if (libname != nullptr) {
98-
libname++;
99-
} else {
100-
libname = buf;
101-
}
102-
out->print(" in %s", libname);
103-
if (!function_printed) {
104-
out->print("+0x%x", offset);
105-
}
79+
int line;
80+
const bool pc_in_VM = os::address_is_in_vm(pc);
81+
out->print("[" PTR_FORMAT "]", p2i(pc));
82+
// Print function and library; shorten library name to just its last component
83+
// for brevity, and omit it completely for libjvm.so
84+
bool function_printed = false;
85+
if (os::dll_address_to_function_name(pc, buf, sizeof(buf), &offset)) {
86+
out->print("%s+0x%x", buf, offset);
87+
function_printed = true;
88+
if (Decoder::get_source_info(pc, buf, sizeof(buf), &line, false)) {
89+
// For intra-vm functions, we omit the full path
90+
const char* s = buf;
91+
if (pc_in_VM) {
92+
s = strrchr(s, os::file_separator()[0]);
93+
s = (s != nullptr) ? s + 1 : buf;
10694
}
107-
108-
// Note: we deliberately omit printing source information here. NativeCallStack::print_on()
109-
// can be called thousands of times as part of NMT detail reporting, and source printing
110-
// can slow down reporting by a factor of 5 or more depending on platform (see JDK-8296931).
111-
112-
out->cr();
95+
out->print(" (%s:%d)", s, line);
96+
}
97+
}
98+
if ((!function_printed || !pc_in_VM) &&
99+
os::dll_address_to_library_name(pc, buf, sizeof(buf), &offset)) {
100+
const char* libname = strrchr(buf, os::file_separator()[0]);
101+
if (libname != nullptr) {
102+
libname++;
103+
} else {
104+
libname = buf;
105+
}
106+
out->print(" in %s", libname);
107+
if (!function_printed) {
108+
out->print("+0x%x", offset);
113109
}
114110
}
115111
}
112+
113+
void NativeCallStack::print_on(outputStream* out) const {
114+
DEBUG_ONLY(assert_not_fake();)
115+
for (int i = 0; i < NMT_TrackingStackDepth && _stack[i] != nullptr; i++) {
116+
print_frame(out, _stack[i]);
117+
}
118+
out->cr();
119+
}

src/hotspot/share/utilities/nativeCallStack.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "memory/allocation.hpp"
2929
#include "nmt/nmtCommon.hpp"
3030
#include "utilities/ostream.hpp"
31+
#include "utilities/resourceHash.hpp"
3132

3233
/*
3334
* This class represents a native call path (does not include Java frame)
@@ -123,6 +124,7 @@ class NativeCallStack : public StackObj {
123124
return (unsigned int)hash;
124125
}
125126

127+
void print_frame(outputStream* out, address pc) const;
126128
void print_on(outputStream* out) const;
127129
};
128130

test/hotspot/jtreg/runtime/NMT/CheckForProperDetailStackTrace.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 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
@@ -58,6 +58,8 @@ public class CheckForProperDetailStackTrace {
5858
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
5959
private static final Path MODS_DIR = Paths.get(TEST_CLASSES, "mods");
6060

61+
private static final boolean expectSourceInformation = Platform.isLinux() || Platform.isWindows();
62+
6163
/* The stack trace we look for by default. Note that :: has been replaced by .*
6264
to make sure it matches even if the symbol is not unmangled.
6365
*/
@@ -121,29 +123,27 @@ public static void main(String args[]) throws Exception {
121123
// It's ok for ARM not to have symbols, because it does not support NMT detail
122124
// when targeting thumb2. It's also ok for Windows not to have symbols, because
123125
// they are only available if the symbols file is included with the build.
124-
if (Platform.isWindows() || Platform.isARM()) {
125-
return; // we are done
126+
if (!Platform.isWindows() && !Platform.isARM()) {
127+
output.reportDiagnosticSummary();
128+
throw new RuntimeException("Expected symbol missing from output: " + expectedSymbol);
126129
}
127-
output.reportDiagnosticSummary();
128-
throw new RuntimeException("Expected symbol missing from output: " + expectedSymbol);
129130
}
130131

131132
// Make sure the expected NMT detail stack trace is found
132133
System.out.println("Looking for a stack matching:");
133-
if (okToHaveAllocateHeap) {
134-
System.out.print(stackTraceAllocateHeap);
135-
if (stackTraceMatches(stackTraceAllocateHeap, output)) {
136-
return;
137-
}
138-
} else {
139-
System.out.print(stackTraceDefault);
140-
if (stackTraceMatches(stackTraceDefault, output)) {
141-
return;
134+
String toMatch = okToHaveAllocateHeap ? stackTraceAllocateHeap : stackTraceDefault;
135+
if (!stackTraceMatches(toMatch, output)) {
136+
output.reportDiagnosticSummary();
137+
throw new RuntimeException("Expected stack trace missing from output");
138+
}
139+
140+
System.out.println("Looking for source information:");
141+
if (expectSourceInformation) {
142+
if (!stackTraceMatches(".*moduleEntry.cpp.*", output)) {
143+
output.reportDiagnosticSummary();
144+
throw new RuntimeException("Expected source information missing from output");
142145
}
143146
}
144-
// Failed to match so dump all the output
145-
output.reportDiagnosticSummary();
146-
throw new RuntimeException("Expected stack trace missing from output");
147147
}
148148

149149
public static boolean stackTraceMatches(String stackTrace, OutputAnalyzer output) {

0 commit comments

Comments
 (0)