1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package nsk.share.gc;
import java.io.*;
import java.util.*;
import nsk.share.*;
import nsk.share.gc.gp.*;
import nsk.share.test.ExecutionController;
/**
* <tt>Algorithms</tt> class collects main algorithms that are used in
* GC testing.
*/
public class Algorithms {
/** Number of threads that one CPU can manage. */
public final static long THREADS_MANAGED_BY_ONE_CPU = 100;
/** Number of threads that one block of memory can manage. */
public final static long THREADS_MANAGED_BY_ONE_BLOCK = 200;
/** Default maximum number of elements in array. */
public final static int MAX_ARRAY_SIZE = 65535;
// Block of memory is 64M
private final static long BLOCK_SIZE = 64 * 1024 * 1024; // 64M
// Minimal memory chunk size to eat
private final static int MIN_MEMORY_CHUNK = 512; // Bytes
// Number of attempts to print a string. Print may fail because of
// OutOfMemoryError, so this constat specifies the number of attemts
// to print despite of OOME.
private final static int ATTEMPTS_TO_PRINT = 3;
// This object stores any Failure that is thrown in Gourmand class
// and used in eatMemory(int) method
private static Failure failure = null;
// This object is intended for wait()
private static Object object = new Object();
/**
* Default constructor.
*/
private Algorithms() {}
/**
* Stresses memory by allocating arrays of bytes. The method allocates
* objects in the same thread and does not invoke GC explicitly by
* calling <tt>System.gc()</tt>.
* <p>
*
* Note that this method can throw Failure if any exception
* is thrown while eating memory. To avoid OOM while allocating
* exception we preallocate it before the lunch starts. It means
* that exception stack trace does not correspond to the place
* where exception is thrown, but points at start of the method.
*
* This method uses nsk.share.test.Stresser class to control
* it's execution. Consumed number of iterations depends on
* available memory.
*
* @throws <tt>nsk.share.Failure</tt> if any unexpected exception is
* thrown during allocating of the objects.
*
* @see nsk.share.test.Stresser
*/
public static void eatMemory(ExecutionController stresser) {
GarbageUtils.eatMemory(stresser, 50, MIN_MEMORY_CHUNK, 2);
}
/**
* Calculates and returns recomended number of threads to start in the
* particular machine (with particular amount of memory and number of CPUs).
* The returned value is minimum of two values:
* {@link #THREADS_MANAGED_BY_ONE_CPU} * (number of processors) and
* {@link #THREADS_MANAGED_BY_ONE_BLOCK} * (number of blocks of the memory).
*
* @return recomended number of threads to start.
*
*/
public static int getThreadsCount() {
Runtime runtime = Runtime.getRuntime();
int processors = runtime.availableProcessors();
long maxMemory = runtime.maxMemory();
long blocks = Math.round((double) maxMemory / BLOCK_SIZE);
return (int) Math.min(THREADS_MANAGED_BY_ONE_CPU * processors,
THREADS_MANAGED_BY_ONE_BLOCK * blocks);
}
/**
* Returns the number of processors available to the Java virtual machine.
*
* @return number of processors available to the Java virtual machine.
*
* @see Runtime#availableProcessors
*
*/
public static int availableProcessors() {
Runtime runtime = Runtime.getRuntime();
return runtime.availableProcessors();
}
/**
* Makes a few attempts to print the string into specified PrintStream.
* If <code>PrintStream.println(String)</code> throws OutOfMemoryError,
* the method waits for a few milliseconds and repeats the attempt.
*
* @param out PrintStream to print the string.
* @param s the string to print.
*/
public static void tryToPrintln(PrintStream out, String s) {
for (int i = 0; i < ATTEMPTS_TO_PRINT; i++) {
try {
out.println(s);
// The string is printed into the PrintStream
return;
} catch (OutOfMemoryError e) {
// Catch the error and wait for a while
synchronized(object) {
try {
object.wait(500);
} catch (InterruptedException ie) {
// Ignore the exception
}
} // synchronized
}
}
} // tryToPrintln()
/**
* Makes a few attempts to print each stack trace of <code>Throwable</code>
* into specified PrintStream. If <code>PrintStream.println(String)</code>
* throws OutOfMemoryError, the method waits for a few milliseconds and
* repeats the attempt.
*
* @param out PrintStream to print the string.
* @param t the throwable to print.
*
* @see #tryToPrintln
*/
public static void tryToPrintStack(PrintStream out, Throwable t) {
StackTraceElement[] trace = t.getStackTrace();
for (int i = 0; i < trace.length; i++) {
for (int j = 0; j < ATTEMPTS_TO_PRINT; j++) {
try {
out.println(trace[i].toString());
// The string is printed into the PrintStream
return;
} catch (OutOfMemoryError e) {
// Catch the error and wait for a while
synchronized(object) {
try {
object.wait(500);
} catch (InterruptedException ie) {
// Ignore the exception
}
} // synchronized
} // try
} // for j
} // for i
} // tryToPrintStack()
/**
* Returns recommended size for an array. Default implemetation returns
* minimum between <code>size</code> and
* {@link #MAX_ARRAY_SIZE MAX_ARRAY_SIZE}.
*
* @return recommended size for an array.
*
*/
public static int getArraySize(long size) {
long min = Math.min(MAX_ARRAY_SIZE, size);
return (int) min;
} // getArraySize()
} // class Algorithms
|