1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 package gc.logging; 26 27 import jdk.test.lib.Utils; 28 29 import javax.management.InstanceNotFoundException; 30 import javax.management.MBeanException; 31 import javax.management.MBeanServer; 32 import javax.management.MalformedObjectNameException; 33 import javax.management.ObjectName; 34 import javax.management.ReflectionException; 35 import java.lang.management.ManagementFactory; 36 import java.util.LinkedList; 37 import java.util.List; 38 import java.util.Random; 39 40 41 /** 42 * @test TestUnifiedLoggingSwitchStress 43 * @summary Switches gc log level on fly while stressing memory/gc 44 * @key gc 45 * @key stress 46 * @requires !vm.flightRecorder 47 * @library /test/lib / 48 * @modules java.management java.base/jdk.internal.misc 49 * 50 * @run main/othervm -Xmx256M -Xms256M 51 * gc.logging.TestUnifiedLoggingSwitchStress 60 52 */ 53 54 class MemoryStresser implements Runnable { 55 public static volatile boolean shouldStop = false; 56 57 private final List<byte[]> liveObjects = new LinkedList<>(); 58 private final List<byte[]> liveHObjects = new LinkedList<>(); 59 private int maxSimpleAllocationMemory = 0; 60 private int usedMemory = 0; 61 62 /** 63 * Maximum amount of huge allocations 64 */ 65 private static int H_ALLOCATION_MAX_COUNT = 4; 66 /** 67 * Maximum regions in one huge allocation 68 */ 69 private static int H_ALLOCATION_REGION_SIZE = 2; 70 private static final int G1_REGION_SIZE = 1024 * 1024; 71 /** 72 * Maximum size of simple allocation 73 */ 74 private static final int MAX_SIMPLE_ALLOCATION_SIZE = (int) (G1_REGION_SIZE / 2 * 0.9); 75 76 /** 77 * Maximum size of dead (i.e. one which is made unreachable right after allocation) object 78 */ 79 private static final int DEAD_OBJECT_MAX_SIZE = G1_REGION_SIZE / 10; 80 private static final Random RND = Utils.getRandomInstance(); 81 82 /** 83 * @param maxMemory maximum memory that could be allocated 84 */ 85 public MemoryStresser(int maxMemory) { 86 maxSimpleAllocationMemory = maxMemory - G1_REGION_SIZE * H_ALLOCATION_MAX_COUNT * H_ALLOCATION_REGION_SIZE; 87 } 88 89 public final Runnable[] actions = new Runnable[]{ 90 // Huge allocation 91 () -> { 92 if (liveHObjects.size() < H_ALLOCATION_MAX_COUNT) { 93 int allocationSize = RND.nextInt((int) (G1_REGION_SIZE * (H_ALLOCATION_REGION_SIZE - 0.5) 94 * 0.9)); 95 liveHObjects.add(new byte[allocationSize + G1_REGION_SIZE / 2]); 96 } 97 }, 98 99 // Huge deallocation 100 () -> { 101 if (liveHObjects.size() > 0) { 102 int elementNum = RND.nextInt(liveHObjects.size()); 103 liveHObjects.remove(elementNum); 104 } 105 }, 106 107 // Simple allocation 108 () -> { 109 if (maxSimpleAllocationMemory - usedMemory != 0) { 110 int arraySize = RND.nextInt(Math.min(maxSimpleAllocationMemory - usedMemory, 111 MAX_SIMPLE_ALLOCATION_SIZE)); 112 if (arraySize != 0) { 113 liveObjects.add(new byte[arraySize]); 114 usedMemory += arraySize; 115 } 116 } 117 }, 118 119 // Simple deallocation 120 () -> { 121 if (liveObjects.size() != 0) { 122 int elementNum = RND.nextInt(liveObjects.size()); 123 int shouldFree = liveObjects.get(elementNum).length; 124 liveObjects.remove(elementNum); 125 usedMemory -= shouldFree; 126 } 127 }, 128 129 // Dead object allocation 130 () -> { 131 int size = RND.nextInt(DEAD_OBJECT_MAX_SIZE); 132 byte[] deadObject = new byte[size]; 133 } 134 }; 135 136 @Override 137 public void run() { 138 while (!shouldStop) { 139 actions[RND.nextInt(actions.length)].run(); 140 Thread.yield(); 141 } 142 143 System.out.println("Memory Stresser finished"); 144 } 145 } 146 147 class LogLevelSwitcher implements Runnable { 148 149 public static volatile boolean shouldStop = false; 150 private final int logCount; // how many various log files will be used 151 private final String logFilePrefix; // name of log file will be logFilePrefix + index 152 private final Random RND = Utils.getRandomInstance(); 153 private final MBeanServer MBS = ManagementFactory.getPlatformMBeanServer(); 154 155 /** 156 * @param logFilePrefix prefix for log files 157 * @param logCount amount of log files 158 */ 159 public LogLevelSwitcher(String logFilePrefix, int logCount) { 160 this.logCount = logCount; 161 this.logFilePrefix = logFilePrefix; 162 163 } 164 165 private static final String[] LOG_LEVELS = {"error", "warning", "info", "debug", "trace"}; 166 167 @Override 168 public void run() { 169 170 while (!shouldStop) { 171 int fileNum = RND.nextInt(logCount); 172 int logLevel = RND.nextInt(LOG_LEVELS.length); 173 174 String outputCommand = String.format("output=%s_%d.log", logFilePrefix, fileNum); 175 String logLevelCommand = "what='gc*=" + LOG_LEVELS[logLevel] + "'"; 176 177 try { 178 Object out = MBS.invoke(new ObjectName("com.sun.management:type=DiagnosticCommand"), 179 "vmLog", 180 new Object[]{new String[]{outputCommand, logLevelCommand}}, 181 new String[]{String[].class.getName()}); 182 183 if (!out.toString().isEmpty()) { 184 System.out.format("WARNING: Diagnostic command vmLog with arguments %s,%s returned not empty" 185 + " output %s\n", 186 outputCommand, logLevelCommand, out); 187 } 188 } catch (InstanceNotFoundException | MBeanException | ReflectionException | MalformedObjectNameException e) { 189 System.out.println("Got exception trying to change log level:" + e); 190 e.printStackTrace(); 191 throw new Error(e); 192 } 193 Thread.yield(); 194 } 195 System.out.println("Log Switcher finished"); 196 } 197 } 198 199 200 public class TestUnifiedLoggingSwitchStress { 201 /** 202 * Count of memory stressing threads 203 */ 204 private static final int MEMORY_STRESSERS_COUNT = 3; 205 /** 206 * Count of log switching threads 207 */ 208 private static final int LOG_LEVEL_SWITCHERS_COUNT = 2; 209 /** 210 * Count of log files created by each log switching thread 211 */ 212 private static final int LOG_FILES_COUNT = 2; 213 /** 214 * Maximum amount memory allocated by each stressing thread 215 */ 216 private static final int MAX_MEMORY_PER_STRESSER = (int) (Runtime.getRuntime().freeMemory() 217 / MEMORY_STRESSERS_COUNT * 0.7); 218 219 public static void main(String[] args) throws InterruptedException { 220 if (args.length != 1) { 221 throw new Error("Test Bug: Expected duration (in seconds) wasn't provided as command line argument"); 222 } 223 long duration = Integer.parseInt(args[0]) * 1000; 224 225 long startTime = System.currentTimeMillis(); 226 227 List<Thread> threads = new LinkedList<>(); 228 229 for (int i = 0; i < LOG_LEVEL_SWITCHERS_COUNT; i++) { 230 threads.add(new Thread(new LogLevelSwitcher("Output_" + i, LOG_FILES_COUNT))); 231 } 232 233 for (int i = 0; i < MEMORY_STRESSERS_COUNT; i++) { 234 threads.add(new Thread(new MemoryStresser(MAX_MEMORY_PER_STRESSER))); 235 } 236 237 threads.stream().forEach(Thread::start); 238 239 while (System.currentTimeMillis() - startTime < duration) { 240 Thread.yield(); 241 } 242 243 MemoryStresser.shouldStop = true; 244 LogLevelSwitcher.shouldStop = true; 245 } 246 }