Skip to content

Commit dea9274

Browse files
author
Sonia Zaldana Calles
committedJul 11, 2024
8332125: [nmt] Totals in diff report should print out total malloc and mmap diffs
Reviewed-by: stuefe, jsjolen
1 parent 5c612c2 commit dea9274

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
 

‎src/hotspot/share/nmt/memReporter.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,35 @@ void MemSummaryDiffReporter::report_diff() {
487487
print_virtual_memory_diff(_current_baseline.total_reserved_memory(),
488488
_current_baseline.total_committed_memory(), _early_baseline.total_reserved_memory(),
489489
_early_baseline.total_committed_memory());
490+
out->cr();
491+
out->cr();
492+
493+
// malloc diff
494+
const size_t early_malloced_bytes =
495+
_early_baseline.malloc_memory_snapshot()->total();
496+
const size_t early_count =
497+
_early_baseline.malloc_memory_snapshot()->total_count();
498+
const size_t current_malloced_bytes =
499+
_current_baseline.malloc_memory_snapshot()->total();
500+
const size_t current_count =
501+
_current_baseline.malloc_memory_snapshot()->total_count();
502+
print_malloc_diff(current_malloced_bytes, current_count, early_malloced_bytes,
503+
early_count, mtNone);
504+
out->cr();
505+
out->cr();
490506

507+
// mmap diff
508+
out->print("mmap: ");
509+
const size_t early_reserved =
510+
_early_baseline.virtual_memory_snapshot()->total_reserved();
511+
const size_t early_committed =
512+
_early_baseline.virtual_memory_snapshot()->total_committed();
513+
const size_t current_reserved =
514+
_current_baseline.virtual_memory_snapshot()->total_reserved();
515+
const size_t current_committed =
516+
_current_baseline.virtual_memory_snapshot()->total_committed();
517+
print_virtual_memory_diff(current_reserved, current_committed, early_reserved,
518+
early_committed);
491519
out->cr();
492520
out->cr();
493521

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2024, Red Hat Inc.
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+
* @test
27+
* @bug 8332125
28+
* @summary Test to verify correctness of total malloc and mmap diffs
29+
* @key randomness
30+
* @library /test/lib
31+
* @modules java.base/jdk.internal.misc
32+
* java.management
33+
* @build jdk.test.whitebox.WhiteBox
34+
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
35+
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=summary -Xms32m -Xmx32m TotalMallocMmapDiffTest
36+
*
37+
*/
38+
39+
import jdk.test.lib.process.OutputAnalyzer;
40+
41+
import jdk.test.whitebox.WhiteBox;
42+
43+
public class TotalMallocMmapDiffTest {
44+
private static final WhiteBox wb = WhiteBox.getWhiteBox();
45+
private static final long ALLOCATE_SIZE = 250 * 1024 * 1024; // 250MB
46+
private static final double FUDGE_FACTOR = 0.2;
47+
private static final double UPPER_BOUND = ALLOCATE_SIZE * (1 + FUDGE_FACTOR);
48+
private static final double LOWER_BOUND = ALLOCATE_SIZE * (1 - FUDGE_FACTOR);
49+
50+
public static void main(String[] args) throws Exception {
51+
52+
// Get baseline
53+
OutputAnalyzer output = NMTTestUtils.startJcmdVMNativeMemory("baseline=true", "scale=1");
54+
output.shouldContain("Baseline taken");
55+
56+
// Allocate some memory via malloc
57+
long addr = wb.NMTMalloc(ALLOCATE_SIZE);
58+
59+
// Virtually reserve and commit memory
60+
addr = wb.NMTReserveMemory(ALLOCATE_SIZE);
61+
wb.NMTCommitMemory(addr, ALLOCATE_SIZE);
62+
63+
// Get NMT diff
64+
output = NMTTestUtils.startJcmdVMNativeMemory("summary.diff", "scale=1");
65+
66+
// Verify malloc diff accounts for memory allocation with a fudge factor
67+
long mallocDiff = getMallocDiff(output);
68+
if (mallocDiff < LOWER_BOUND || mallocDiff > UPPER_BOUND) {
69+
throw new Exception("Total malloc diff is incorrect. " +
70+
"Expected malloc diff range: [" + LOWER_BOUND + " - " + UPPER_BOUND + "]" +
71+
"Actual malloc diff: " + mallocDiff);
72+
}
73+
74+
// Verify mmap diff accounts for reserve and commit
75+
long reservedDiff = getReservedDiff(output);
76+
long committedDiff = getCommittedDiff(output);
77+
if (reservedDiff < LOWER_BOUND || reservedDiff > UPPER_BOUND) {
78+
throw new Exception("mmap reserved diff is incorrect. " +
79+
"Expected reserved diff range: [" + LOWER_BOUND + " - " + UPPER_BOUND + "]" +
80+
"Actual reserved diff: " + reservedDiff);
81+
}
82+
if (committedDiff < LOWER_BOUND || committedDiff > UPPER_BOUND) {
83+
throw new Exception("mmap committed diff is incorrect. " +
84+
"Expected committed diff range: [" + LOWER_BOUND + " - " + UPPER_BOUND + "]" +
85+
"Actual committed diff: " + committedDiff);
86+
}
87+
88+
}
89+
90+
private static long getMallocDiff(OutputAnalyzer output) {
91+
// First match should be global malloc diff
92+
String malloc = output.firstMatch("malloc=\\d+ \\+(\\d+)", 1);
93+
return Long.parseLong(malloc);
94+
}
95+
96+
private static long getReservedDiff(OutputAnalyzer output) {
97+
// First match should be global mmap diff
98+
String reservedDiff = output.firstMatch("mmap: reserved=\\d+ \\+(\\d+)", 1);
99+
return Long.parseLong(reservedDiff);
100+
}
101+
102+
private static long getCommittedDiff(OutputAnalyzer output) {
103+
// First match should be global mmap diff
104+
String committedDiff = output.firstMatch("mmap: reserved=\\d+ \\+\\d+, committed=\\d+ \\+(\\d+)", 1);
105+
return Long.parseLong(committedDiff);
106+
}
107+
}

0 commit comments

Comments
 (0)
Please sign in to comment.