Path: blob/master/test/hotspot/jtreg/compiler/codecache/OverflowCodeCacheTest.java
64474 views
/*1* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test OverflowCodeCacheTest25* @bug 8059550 827935626* @summary testing of code cache segments overflow27* @library /test/lib28* @modules java.base/jdk.internal.misc29* java.management30*31* @build sun.hotspot.WhiteBox32* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox33* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions34* -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*35* -XX:-SegmentedCodeCache -Xmixed36* compiler.codecache.OverflowCodeCacheTest CompilationDisabled37* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions38* -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*39* -XX:+SegmentedCodeCache -Xmixed40* compiler.codecache.OverflowCodeCacheTest CompilationDisabled41* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions42* -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache -Xmixed43* compiler.codecache.OverflowCodeCacheTest44*/4546package compiler.codecache;4748import jdk.test.lib.Asserts;49import sun.hotspot.WhiteBox;50import sun.hotspot.code.BlobType;51import sun.hotspot.code.CodeBlob;5253import java.lang.management.MemoryPoolMXBean;54import java.lang.reflect.Method;55import java.util.ArrayList;56import java.util.EnumSet;5758class Helper {59// Uncommon signature to prevent sharing and force creation of a new adapter60public void method(float a, float b, float c, Object o) { }61}6263public class OverflowCodeCacheTest {64private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();65private static boolean COMPILATION_DISABLED = false;6667public static void main(String[] args) {68COMPILATION_DISABLED = args.length > 0;69EnumSet<BlobType> blobTypes = BlobType.getAvailable();70for (BlobType type : blobTypes) {71new OverflowCodeCacheTest(type).test();72}73}7475private final BlobType type;76private final MemoryPoolMXBean bean;77private OverflowCodeCacheTest(BlobType type) {78this.type = type;79this.bean = type.getMemoryPool();80}8182private void test() {83System.out.printf("type %s%n", type);84System.out.println("allocating till possible...");85ArrayList<Long> blobs = new ArrayList<>();86int compilationActivityMode = -1;87// Lock compilation to be able to better control code cache space88WHITE_BOX.lockCompilation();89try {90long addr;91int size = (int) (getHeapSize() >> 7);92while ((addr = WHITE_BOX.allocateCodeBlob(size, type.id)) != 0) {93blobs.add(addr);9495BlobType actualType = CodeBlob.getCodeBlob(addr).code_blob_type;96if (actualType != type) {97// check we got allowed overflow handling98Asserts.assertTrue(type.allowTypeWhenOverflow(actualType),99type + " doesn't allow using " + actualType + " when overflow");100}101}102/* now, remember compilationActivityMode to check it later, after freeing, since we103possibly have no free cache for further work */104compilationActivityMode = WHITE_BOX.getCompilationActivityMode();105106// Use smallest allocation size to make sure all of the available space107// is filled up. Don't free these below to put some pressure on the sweeper.108while ((addr = WHITE_BOX.allocateCodeBlob(1, type.id)) != 0) { }109} finally {110try {111// Trigger creation of a new adapter for Helper::method112// which will fail because we are out of code cache space.113Helper helper = new Helper();114} catch (VirtualMachineError e) {115// Expected116}117// Free code cache space118for (Long blob : blobs) {119WHITE_BOX.freeCodeBlob(blob);120}121122// Convert some nmethods to zombie and then free them to re-enable compilation123WHITE_BOX.unlockCompilation();124WHITE_BOX.forceNMethodSweep();125WHITE_BOX.forceNMethodSweep();126127// Trigger compilation of Helper::method which will hit an assert because128// adapter creation failed above due to a lack of code cache space.129Helper helper = new Helper();130for (int i = 0; i < 100_000; i++) {131helper.method(0, 0, 0, null);132}133}134// Only check this if compilation is disabled, otherwise the sweeper might have135// freed enough nmethods to allow for re-enabling compilation.136if (COMPILATION_DISABLED) {137Asserts.assertNotEquals(compilationActivityMode, 1 /* run_compilation*/,138"Compilation must be disabled when CodeCache(CodeHeap) overflows");139}140}141142private long getHeapSize() {143return bean.getUsage().getMax();144}145146}147148149