Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/gc/Algorithms.java
41161 views
/*1* Copyright (c) 2003, 2018, 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 nsk.share.gc;2425import java.io.*;26import java.util.*;27import nsk.share.*;28import nsk.share.gc.gp.*;29import nsk.share.test.ExecutionController;3031/**32* <tt>Algorithms</tt> class collects main algorithms that are used in33* GC testing.34*/35public class Algorithms {36/** Number of threads that one CPU can manage. */37public final static long THREADS_MANAGED_BY_ONE_CPU = 100;3839/** Number of threads that one block of memory can manage. */40public final static long THREADS_MANAGED_BY_ONE_BLOCK = 200;4142/** Default maximum number of elements in array. */43public final static int MAX_ARRAY_SIZE = 65535;4445// Block of memory is 64M46private final static long BLOCK_SIZE = 64 * 1024 * 1024; // 64M4748// Minimal memory chunk size to eat49private final static int MIN_MEMORY_CHUNK = 512; // Bytes5051// Number of attempts to print a string. Print may fail because of52// OutOfMemoryError, so this constat specifies the number of attemts53// to print despite of OOME.54private final static int ATTEMPTS_TO_PRINT = 3;5556// This object stores any Failure that is thrown in Gourmand class57// and used in eatMemory(int) method58private static Failure failure = null;5960// This object is intended for wait()61private static Object object = new Object();6263/**64* Default constructor.65*/66private Algorithms() {}6768/**69* Stresses memory by allocating arrays of bytes. The method allocates70* objects in the same thread and does not invoke GC explicitly by71* calling <tt>System.gc()</tt>.72* <p>73*74* Note that this method can throw Failure if any exception75* is thrown while eating memory. To avoid OOM while allocating76* exception we preallocate it before the lunch starts. It means77* that exception stack trace does not correspond to the place78* where exception is thrown, but points at start of the method.79*80* This method uses nsk.share.test.Stresser class to control81* it's execution. Consumed number of iterations depends on82* available memory.83*84* @throws <tt>nsk.share.Failure</tt> if any unexpected exception is85* thrown during allocating of the objects.86*87* @see nsk.share.test.Stresser88*/89public static void eatMemory(ExecutionController stresser) {90GarbageUtils.eatMemory(stresser, 50, MIN_MEMORY_CHUNK, 2);91}9293/**94* Calculates and returns recomended number of threads to start in the95* particular machine (with particular amount of memory and number of CPUs).96* The returned value is minimum of two values:97* {@link #THREADS_MANAGED_BY_ONE_CPU} * (number of processors) and98* {@link #THREADS_MANAGED_BY_ONE_BLOCK} * (number of blocks of the memory).99*100* @return recomended number of threads to start.101*102*/103public static int getThreadsCount() {104Runtime runtime = Runtime.getRuntime();105int processors = runtime.availableProcessors();106long maxMemory = runtime.maxMemory();107long blocks = Math.round((double) maxMemory / BLOCK_SIZE);108109return (int) Math.min(THREADS_MANAGED_BY_ONE_CPU * processors,110THREADS_MANAGED_BY_ONE_BLOCK * blocks);111}112113/**114* Returns the number of processors available to the Java virtual machine.115*116* @return number of processors available to the Java virtual machine.117*118* @see Runtime#availableProcessors119*120*/121public static int availableProcessors() {122Runtime runtime = Runtime.getRuntime();123return runtime.availableProcessors();124}125126/**127* Makes a few attempts to print the string into specified PrintStream.128* If <code>PrintStream.println(String)</code> throws OutOfMemoryError,129* the method waits for a few milliseconds and repeats the attempt.130*131* @param out PrintStream to print the string.132* @param s the string to print.133*/134public static void tryToPrintln(PrintStream out, String s) {135for (int i = 0; i < ATTEMPTS_TO_PRINT; i++) {136try {137out.println(s);138139// The string is printed into the PrintStream140return;141} catch (OutOfMemoryError e) {142143// Catch the error and wait for a while144synchronized(object) {145try {146object.wait(500);147} catch (InterruptedException ie) {148149// Ignore the exception150}151} // synchronized152}153}154} // tryToPrintln()155156/**157* Makes a few attempts to print each stack trace of <code>Throwable</code>158* into specified PrintStream. If <code>PrintStream.println(String)</code>159* throws OutOfMemoryError, the method waits for a few milliseconds and160* repeats the attempt.161*162* @param out PrintStream to print the string.163* @param t the throwable to print.164*165* @see #tryToPrintln166*/167public static void tryToPrintStack(PrintStream out, Throwable t) {168StackTraceElement[] trace = t.getStackTrace();169170for (int i = 0; i < trace.length; i++) {171for (int j = 0; j < ATTEMPTS_TO_PRINT; j++) {172try {173out.println(trace[i].toString());174175// The string is printed into the PrintStream176return;177} catch (OutOfMemoryError e) {178179// Catch the error and wait for a while180synchronized(object) {181try {182object.wait(500);183} catch (InterruptedException ie) {184185// Ignore the exception186}187} // synchronized188} // try189} // for j190} // for i191} // tryToPrintStack()192193/**194* Returns recommended size for an array. Default implemetation returns195* minimum between <code>size</code> and196* {@link #MAX_ARRAY_SIZE MAX_ARRAY_SIZE}.197*198* @return recommended size for an array.199*200*/201public static int getArraySize(long size) {202long min = Math.min(MAX_ARRAY_SIZE, size);203return (int) min;204} // getArraySize()205} // class Algorithms206207208