|
| 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