Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/gc/Algorithms.java
41161 views
1
/*
2
* Copyright (c) 2003, 2018, 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
package nsk.share.gc;
25
26
import java.io.*;
27
import java.util.*;
28
import nsk.share.*;
29
import nsk.share.gc.gp.*;
30
import nsk.share.test.ExecutionController;
31
32
/**
33
* <tt>Algorithms</tt> class collects main algorithms that are used in
34
* GC testing.
35
*/
36
public class Algorithms {
37
/** Number of threads that one CPU can manage. */
38
public final static long THREADS_MANAGED_BY_ONE_CPU = 100;
39
40
/** Number of threads that one block of memory can manage. */
41
public final static long THREADS_MANAGED_BY_ONE_BLOCK = 200;
42
43
/** Default maximum number of elements in array. */
44
public final static int MAX_ARRAY_SIZE = 65535;
45
46
// Block of memory is 64M
47
private final static long BLOCK_SIZE = 64 * 1024 * 1024; // 64M
48
49
// Minimal memory chunk size to eat
50
private final static int MIN_MEMORY_CHUNK = 512; // Bytes
51
52
// Number of attempts to print a string. Print may fail because of
53
// OutOfMemoryError, so this constat specifies the number of attemts
54
// to print despite of OOME.
55
private final static int ATTEMPTS_TO_PRINT = 3;
56
57
// This object stores any Failure that is thrown in Gourmand class
58
// and used in eatMemory(int) method
59
private static Failure failure = null;
60
61
// This object is intended for wait()
62
private static Object object = new Object();
63
64
/**
65
* Default constructor.
66
*/
67
private Algorithms() {}
68
69
/**
70
* Stresses memory by allocating arrays of bytes. The method allocates
71
* objects in the same thread and does not invoke GC explicitly by
72
* calling <tt>System.gc()</tt>.
73
* <p>
74
*
75
* Note that this method can throw Failure if any exception
76
* is thrown while eating memory. To avoid OOM while allocating
77
* exception we preallocate it before the lunch starts. It means
78
* that exception stack trace does not correspond to the place
79
* where exception is thrown, but points at start of the method.
80
*
81
* This method uses nsk.share.test.Stresser class to control
82
* it's execution. Consumed number of iterations depends on
83
* available memory.
84
*
85
* @throws <tt>nsk.share.Failure</tt> if any unexpected exception is
86
* thrown during allocating of the objects.
87
*
88
* @see nsk.share.test.Stresser
89
*/
90
public static void eatMemory(ExecutionController stresser) {
91
GarbageUtils.eatMemory(stresser, 50, MIN_MEMORY_CHUNK, 2);
92
}
93
94
/**
95
* Calculates and returns recomended number of threads to start in the
96
* particular machine (with particular amount of memory and number of CPUs).
97
* The returned value is minimum of two values:
98
* {@link #THREADS_MANAGED_BY_ONE_CPU} * (number of processors) and
99
* {@link #THREADS_MANAGED_BY_ONE_BLOCK} * (number of blocks of the memory).
100
*
101
* @return recomended number of threads to start.
102
*
103
*/
104
public static int getThreadsCount() {
105
Runtime runtime = Runtime.getRuntime();
106
int processors = runtime.availableProcessors();
107
long maxMemory = runtime.maxMemory();
108
long blocks = Math.round((double) maxMemory / BLOCK_SIZE);
109
110
return (int) Math.min(THREADS_MANAGED_BY_ONE_CPU * processors,
111
THREADS_MANAGED_BY_ONE_BLOCK * blocks);
112
}
113
114
/**
115
* Returns the number of processors available to the Java virtual machine.
116
*
117
* @return number of processors available to the Java virtual machine.
118
*
119
* @see Runtime#availableProcessors
120
*
121
*/
122
public static int availableProcessors() {
123
Runtime runtime = Runtime.getRuntime();
124
return runtime.availableProcessors();
125
}
126
127
/**
128
* Makes a few attempts to print the string into specified PrintStream.
129
* If <code>PrintStream.println(String)</code> throws OutOfMemoryError,
130
* the method waits for a few milliseconds and repeats the attempt.
131
*
132
* @param out PrintStream to print the string.
133
* @param s the string to print.
134
*/
135
public static void tryToPrintln(PrintStream out, String s) {
136
for (int i = 0; i < ATTEMPTS_TO_PRINT; i++) {
137
try {
138
out.println(s);
139
140
// The string is printed into the PrintStream
141
return;
142
} catch (OutOfMemoryError e) {
143
144
// Catch the error and wait for a while
145
synchronized(object) {
146
try {
147
object.wait(500);
148
} catch (InterruptedException ie) {
149
150
// Ignore the exception
151
}
152
} // synchronized
153
}
154
}
155
} // tryToPrintln()
156
157
/**
158
* Makes a few attempts to print each stack trace of <code>Throwable</code>
159
* into specified PrintStream. If <code>PrintStream.println(String)</code>
160
* throws OutOfMemoryError, the method waits for a few milliseconds and
161
* repeats the attempt.
162
*
163
* @param out PrintStream to print the string.
164
* @param t the throwable to print.
165
*
166
* @see #tryToPrintln
167
*/
168
public static void tryToPrintStack(PrintStream out, Throwable t) {
169
StackTraceElement[] trace = t.getStackTrace();
170
171
for (int i = 0; i < trace.length; i++) {
172
for (int j = 0; j < ATTEMPTS_TO_PRINT; j++) {
173
try {
174
out.println(trace[i].toString());
175
176
// The string is printed into the PrintStream
177
return;
178
} catch (OutOfMemoryError e) {
179
180
// Catch the error and wait for a while
181
synchronized(object) {
182
try {
183
object.wait(500);
184
} catch (InterruptedException ie) {
185
186
// Ignore the exception
187
}
188
} // synchronized
189
} // try
190
} // for j
191
} // for i
192
} // tryToPrintStack()
193
194
/**
195
* Returns recommended size for an array. Default implemetation returns
196
* minimum between <code>size</code> and
197
* {@link #MAX_ARRAY_SIZE MAX_ARRAY_SIZE}.
198
*
199
* @return recommended size for an array.
200
*
201
*/
202
public static int getArraySize(long size) {
203
long min = Math.min(MAX_ARRAY_SIZE, size);
204
return (int) min;
205
} // getArraySize()
206
} // class Algorithms
207
208