Path: blob/master/test/hotspot/jtreg/gc/logging/TestUnifiedLoggingSwitchStress.java
40942 views
/*1* Copyright (c) 2016, 2020, 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*/2223package gc.logging;2425import jdk.test.lib.Utils;2627import javax.management.InstanceNotFoundException;28import javax.management.MBeanException;29import javax.management.MBeanServer;30import javax.management.MalformedObjectNameException;31import javax.management.ObjectName;32import javax.management.ReflectionException;3334import static gc.testlibrary.Allocation.blackHole;3536import java.lang.management.ManagementFactory;37import java.util.LinkedList;38import java.util.List;39import java.util.Random;404142/**43* @test TestUnifiedLoggingSwitchStress44* @key stress randomness45* @summary Switches gc log level on fly while stressing memory/gc46* @requires !vm.flightRecorder47* @requires vm.gc != "Z"48* @library /test/lib /49* @modules java.management java.base/jdk.internal.misc50*51* @run main/othervm -Xmx256M -Xms256M52* gc.logging.TestUnifiedLoggingSwitchStress 6053*/5455class MemoryStresser implements Runnable {56public static volatile boolean shouldStop = false;5758private final List<byte[]> liveObjects = new LinkedList<>();59private final List<byte[]> liveHObjects = new LinkedList<>();60private int maxSimpleAllocationMemory = 0;61private int usedMemory = 0;6263/**64* Maximum amount of huge allocations65*/66private static int H_ALLOCATION_MAX_COUNT = 4;67/**68* Maximum regions in one huge allocation69*/70private static int H_ALLOCATION_REGION_SIZE = 2;71private static final int G1_REGION_SIZE = 1024 * 1024;72/**73* Maximum size of simple allocation74*/75private static final int MAX_SIMPLE_ALLOCATION_SIZE = (int) (G1_REGION_SIZE / 2 * 0.9);7677/**78* Maximum size of dead (i.e. one which is made unreachable right after allocation) object79*/80private static final int DEAD_OBJECT_MAX_SIZE = G1_REGION_SIZE / 10;81private final Random rnd = new Random(Utils.getRandomInstance().nextLong());8283/**84* @param maxMemory maximum memory that could be allocated85*/86public MemoryStresser(int maxMemory) {87maxSimpleAllocationMemory = maxMemory - G1_REGION_SIZE * H_ALLOCATION_MAX_COUNT * H_ALLOCATION_REGION_SIZE;88}8990public final Runnable[] actions = new Runnable[]{91// Huge allocation92() -> {93if (liveHObjects.size() < H_ALLOCATION_MAX_COUNT) {94int allocationSize = rnd.nextInt((int) (G1_REGION_SIZE * (H_ALLOCATION_REGION_SIZE - 0.5)95* 0.9));96liveHObjects.add(new byte[allocationSize + G1_REGION_SIZE / 2]);97}98},99100// Huge deallocation101() -> {102if (liveHObjects.size() > 0) {103int elementNum = rnd.nextInt(liveHObjects.size());104liveHObjects.remove(elementNum);105}106},107108// Simple allocation109() -> {110if (maxSimpleAllocationMemory - usedMemory != 0) {111int arraySize = rnd.nextInt(Math.min(maxSimpleAllocationMemory - usedMemory,112MAX_SIMPLE_ALLOCATION_SIZE));113if (arraySize != 0) {114liveObjects.add(new byte[arraySize]);115usedMemory += arraySize;116}117}118},119120// Simple deallocation121() -> {122if (liveObjects.size() != 0) {123int elementNum = rnd.nextInt(liveObjects.size());124int shouldFree = liveObjects.get(elementNum).length;125liveObjects.remove(elementNum);126usedMemory -= shouldFree;127}128},129130// Dead object allocation131() -> {132int size = rnd.nextInt(DEAD_OBJECT_MAX_SIZE);133blackHole(new byte[size]);134}135};136137@Override138public void run() {139while (!shouldStop) {140actions[rnd.nextInt(actions.length)].run();141Thread.yield();142}143144System.out.println("Memory Stresser finished");145}146}147148class LogLevelSwitcher implements Runnable {149150public static volatile boolean shouldStop = false;151private final int logCount; // how many various log files will be used152private final String logFilePrefix; // name of log file will be logFilePrefix + index153private final Random rnd;154private final MBeanServer MBS = ManagementFactory.getPlatformMBeanServer();155156/**157* @param logFilePrefix prefix for log files158* @param logCount amount of log files159*/160public LogLevelSwitcher(String logFilePrefix, int logCount) {161this.logCount = logCount;162this.logFilePrefix = logFilePrefix;163this.rnd = new Random(Utils.getRandomInstance().nextLong());164}165166private static final String[] LOG_LEVELS = {"error", "warning", "info", "debug", "trace"};167168@Override169public void run() {170171while (!shouldStop) {172int fileNum = rnd.nextInt(logCount);173int logLevel = rnd.nextInt(LOG_LEVELS.length);174175String outputCommand = String.format("output=%s_%d.log", logFilePrefix, fileNum);176String logLevelCommand = "what='gc*=" + LOG_LEVELS[logLevel] + "'";177178try {179Object out = MBS.invoke(new ObjectName("com.sun.management:type=DiagnosticCommand"),180"vmLog",181new Object[]{new String[]{outputCommand, logLevelCommand}},182new String[]{String[].class.getName()});183184if (!out.toString().isEmpty()) {185System.out.format("WARNING: Diagnostic command vmLog with arguments %s,%s returned not empty"186+ " output %s\n",187outputCommand, logLevelCommand, out);188}189} catch (InstanceNotFoundException | MBeanException | ReflectionException | MalformedObjectNameException e) {190System.out.println("Got exception trying to change log level:" + e);191e.printStackTrace();192throw new Error(e);193}194Thread.yield();195}196System.out.println("Log Switcher finished");197}198}199200201public class TestUnifiedLoggingSwitchStress {202/**203* Count of memory stressing threads204*/205private static final int MEMORY_STRESSERS_COUNT = 3;206/**207* Count of log switching threads208*/209private static final int LOG_LEVEL_SWITCHERS_COUNT = 2;210/**211* Count of log files created by each log switching thread212*/213private static final int LOG_FILES_COUNT = 2;214/**215* Maximum amount memory allocated by each stressing thread216*/217private static final int MAX_MEMORY_PER_STRESSER = (int) (Runtime.getRuntime().freeMemory()218/ MEMORY_STRESSERS_COUNT * 0.7);219220public static void main(String[] args) throws InterruptedException {221if (args.length != 1) {222throw new Error("Test Bug: Expected duration (in seconds) wasn't provided as command line argument");223}224long duration = Integer.parseInt(args[0]) * 1000;225226long startTime = System.currentTimeMillis();227228List<Thread> threads = new LinkedList<>();229230for (int i = 0; i < LOG_LEVEL_SWITCHERS_COUNT; i++) {231threads.add(new Thread(new LogLevelSwitcher("Output_" + i, LOG_FILES_COUNT)));232}233234for (int i = 0; i < MEMORY_STRESSERS_COUNT; i++) {235threads.add(new Thread(new MemoryStresser(MAX_MEMORY_PER_STRESSER)));236}237238threads.stream().forEach(Thread::start);239240while (System.currentTimeMillis() - startTime < duration) {241Thread.yield();242}243244MemoryStresser.shouldStop = true;245LogLevelSwitcher.shouldStop = true;246}247}248249250