Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/hashcode/HCHelper.java
40948 views
1
/*
2
* Copyright (c) 2011, 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
package gc.hashcode;
24
25
import java.util.ArrayList;
26
import java.util.Random;
27
28
/**
29
* Helper class for the hash code tests.
30
*/
31
public final class HCHelper {
32
33
/**
34
* Evacuation list 0 constant.
35
*/
36
public static final int EVAC_LIST_0 = 0;
37
/**
38
* Evacuation list 1 constant.
39
*/
40
public static final int EVAC_LIST_1 = 1;
41
/**
42
* Evacuation list 2 constant.
43
*/
44
public static final int EVAC_LIST_2 = 2;
45
/**
46
* Evacuation list 3 constant.
47
*/
48
public static final int EVAC_LIST_3 = 3;
49
/**
50
* Evacuation list 4 constant.
51
*/
52
public static final int EVAC_LIST_4 = 4;
53
/**
54
* Evacuation list 5 constant.
55
*/
56
public static final int EVAC_LIST_5 = 5;
57
/**
58
* Evacuation list 0 percentage constant.
59
*/
60
public static final double EVAC_SIZE_0 = 0.50;
61
/**
62
* Evacuation list 1 percentage constant.
63
*/
64
public static final double EVAC_SIZE_1 = 0.14;
65
/**
66
* Evacuation list 2 percentage constant.
67
*/
68
public static final double EVAC_SIZE_2 = 0.12;
69
/**
70
* Evacuation list 3 percentage constant.
71
*/
72
public static final double EVAC_SIZE_3 = 0.10;
73
/**
74
* Evacuation list 4 percentage constant.
75
*/
76
public static final double EVAC_SIZE_4 = 0.07;
77
/**
78
* Evacuation list 5 percentage constant.
79
*/
80
public static final double EVAC_SIZE_5 = 0.05;
81
82
/**
83
* Helper class that allocates memory and also tracks the original
84
* as well as current hash code.
85
*/
86
final class AllocObject {
87
private byte[] allocatedArray;
88
private int hashValue;
89
90
/**
91
* Create a new allocator object that allocates size bytes.
92
*
93
* @param size Number of bytes to allocate.
94
*/
95
AllocObject(int size) {
96
allocatedArray = new byte[size];
97
hashValue = allocatedArray.hashCode();
98
}
99
100
/**
101
* Get the stored hash code value.
102
*
103
* @return Stored hash code.
104
*/
105
int getStoredHashValue() {
106
return hashValue;
107
}
108
109
/**
110
* Get the current hash code value.
111
*
112
* @return Current hash code.
113
*/
114
int getCurrentHashValue() {
115
return allocatedArray.hashCode();
116
}
117
118
/**
119
* Get the size of the allocated object.
120
*
121
* @return Size of allocated object.
122
*/
123
int getAllocatedSize() {
124
return allocatedArray.length;
125
}
126
}
127
128
/**
129
* Helper class that holds all the allocation lists.
130
*/
131
final class AllocInfo {
132
private long allocatedSize;
133
private long numOfAllocedObjs;
134
private ArrayList safeList;
135
private ArrayList allocList;
136
private ArrayList evacList0;
137
private ArrayList evacList1;
138
private ArrayList evacList2;
139
private ArrayList evacList3;
140
private ArrayList evacList4;
141
private ArrayList evacList5;
142
143
/**
144
* Create the helper object.
145
*/
146
AllocInfo() {
147
allocatedSize = 0;
148
numOfAllocedObjs = 0;
149
safeList = new ArrayList();
150
allocList = new ArrayList();
151
evacList0 = new ArrayList();
152
evacList1 = new ArrayList();
153
evacList2 = new ArrayList();
154
evacList3 = new ArrayList();
155
evacList4 = new ArrayList();
156
evacList5 = new ArrayList();
157
}
158
159
/**
160
* Get the amount of memory allocated in total.
161
*
162
* @return Total allocated size.
163
*/
164
public long getAllocatedSize() {
165
return allocatedSize;
166
}
167
168
/**
169
* Set the amount of memory allocated in total.
170
*
171
* @param allocatedSize Total allocated size.
172
*/
173
public void setAllocatedSize(long allocatedSize) {
174
this.allocatedSize = allocatedSize;
175
}
176
177
/**
178
* Get total number of objects allocated.
179
*
180
* @return Number of objects allocated.
181
*/
182
public long getNumOfAllocedObjs() {
183
return numOfAllocedObjs;
184
}
185
186
/**
187
* Set total number of objects allocated.
188
*
189
* @param numOfAllocedObjs Number of objects allocated.
190
*/
191
public void setNumOfAllocedObjs(long numOfAllocedObjs) {
192
this.numOfAllocedObjs = numOfAllocedObjs;
193
}
194
195
/**
196
* Increase the number of objects allocated.
197
*/
198
public void incNumOfAllocedObjs() {
199
numOfAllocedObjs++;
200
}
201
202
/**
203
* Decrease the number of objects allocated.
204
*/
205
public void decNumOfAllocedObjs() {
206
numOfAllocedObjs--;
207
}
208
209
/**
210
* Get the safe list.
211
*
212
* @return ArrayList that contains the safe list.
213
*/
214
public ArrayList getSafeList() {
215
return safeList;
216
}
217
218
/**
219
* Get the alloc list.
220
*
221
* @return ArrayList that contains the alloc list.
222
*/
223
public ArrayList getAllocList() {
224
return allocList;
225
}
226
227
/**
228
* Get evacuation list 0.
229
*
230
* @return ArrayList that contains evacuation list 0.
231
*/
232
public ArrayList getEvacList0() {
233
return evacList0;
234
}
235
236
/**
237
* Get evacuation list 1.
238
*
239
* @return ArrayList that contains evacuation list 1.
240
*/
241
public ArrayList getEvacList1() {
242
return evacList1;
243
}
244
245
/**
246
* Get evacuation list 2.
247
*
248
* @return ArrayList that contains evacuation list 2.
249
*/
250
public ArrayList getEvacList2() {
251
return evacList2;
252
}
253
254
/**
255
* Get evacuation list 3.
256
*
257
* @return ArrayList that contains evacuation list 3.
258
*/
259
public ArrayList getEvacList3() {
260
return evacList3;
261
}
262
263
/**
264
* Get evacuation list 4.
265
*
266
* @return ArrayList that contains evacuation list 4.
267
*/
268
public ArrayList getEvacList4() {
269
return evacList4;
270
}
271
272
/**
273
* Get evacuation list 5.
274
*
275
* @return ArrayList that contains evacuation list 5.
276
*/
277
public ArrayList getEvacList5() {
278
return evacList5;
279
}
280
}
281
282
283
private int minSize;
284
private int maxSize;
285
private double percentToFill;
286
private int allocTrigSize;
287
private AllocInfo ai;
288
private Random rnd;
289
290
private long sizeLimit0;
291
private long sizeLimit1;
292
private long sizeLimit2;
293
private long sizeLimit3;
294
private long sizeLimit4;
295
private long sizeLimit5;
296
297
/**
298
* Create the helper class.
299
*
300
* @param minSize Minimum size of objects to allocate.
301
* @param maxSize Maximum size of objects to allocate.
302
* @param seed Random seed to use.
303
* @param percentToFill Percentage of the heap to fill.
304
* @param allocTrigSize Object size to use when triggering a GC.
305
*/
306
public HCHelper(int minSize, int maxSize, long seed,
307
double percentToFill, int allocTrigSize) {
308
this.minSize = minSize;
309
this.maxSize = maxSize;
310
this.percentToFill = percentToFill;
311
this.allocTrigSize = allocTrigSize;
312
ai = new AllocInfo();
313
rnd = new Random(seed);
314
315
sizeLimit0 = 0;
316
sizeLimit1 = 0;
317
sizeLimit2 = 0;
318
sizeLimit3 = 0;
319
sizeLimit4 = 0;
320
sizeLimit5 = 0;
321
}
322
323
/**
324
* Setup all the evacuation lists and fill them with objects.
325
*/
326
public void setupLists() {
327
Runtime r = Runtime.getRuntime();
328
long maxMem = r.maxMemory();
329
long safeMaxMem = (long) (maxMem * percentToFill);
330
sizeLimit0 = (long) (safeMaxMem * EVAC_SIZE_0);
331
sizeLimit1 = (long) (safeMaxMem * EVAC_SIZE_1);
332
sizeLimit2 = (long) (safeMaxMem * EVAC_SIZE_2);
333
sizeLimit3 = (long) (safeMaxMem * EVAC_SIZE_3);
334
sizeLimit4 = (long) (safeMaxMem * EVAC_SIZE_4);
335
sizeLimit5 = (long) (safeMaxMem * EVAC_SIZE_5);
336
337
// Fill the memory with objects
338
System.gc();
339
allocObjects(ai.getEvacList0(), sizeLimit0);
340
System.gc();
341
allocObjects(ai.getEvacList1(), sizeLimit1);
342
System.gc();
343
allocObjects(ai.getEvacList2(), sizeLimit2);
344
System.gc();
345
allocObjects(ai.getEvacList3(), sizeLimit3);
346
System.gc();
347
allocObjects(ai.getEvacList4(), sizeLimit4);
348
System.gc();
349
allocObjects(ai.getEvacList5(), sizeLimit5);
350
System.gc();
351
}
352
353
private void allocObjects(ArrayList al, long totalSizeLimit) {
354
long allocedSize = 0;
355
int multiplier = maxSize - minSize;
356
357
while (allocedSize < totalSizeLimit) {
358
int allocSize = minSize + (int) (rnd.nextDouble() * multiplier);
359
if (allocSize >= totalSizeLimit - allocedSize) {
360
allocSize = (int) (totalSizeLimit - allocedSize);
361
}
362
363
al.add(new AllocObject(allocSize));
364
allocedSize += allocSize;
365
}
366
}
367
368
/**
369
* Free all objects in a specific evacuation list.
370
*
371
* @param listNr The evacuation list to clear. Must be between 0 and 5.
372
*/
373
public void clearList(int listNr) {
374
if (listNr < EVAC_LIST_0 || listNr > EVAC_LIST_5) {
375
throw new IllegalArgumentException("List to removed bust be "
376
+ "between EVAC_LIST_0 and EVAC_LIST_5");
377
}
378
379
switch (listNr) {
380
case EVAC_LIST_0:
381
ai.getEvacList0().clear();
382
break;
383
case EVAC_LIST_1:
384
ai.getEvacList1().clear();
385
break;
386
case EVAC_LIST_2:
387
ai.getEvacList2().clear();
388
break;
389
case EVAC_LIST_3:
390
ai.getEvacList3().clear();
391
break;
392
case EVAC_LIST_4:
393
ai.getEvacList4().clear();
394
break;
395
case EVAC_LIST_5:
396
ai.getEvacList5().clear();
397
break;
398
default: // Should never occur, since we test the listNr param
399
break;
400
}
401
}
402
403
/**
404
* Verify the hash codes for a list of AllocObject:s.
405
*
406
* @param objList ArrayList containing AllocObject:s
407
* @return true if all hash codes are OK, otherwise false
408
*/
409
boolean verifyHashCodes(ArrayList objList) {
410
// Check the hash values
411
for (int i = 0; i < objList.size(); i++) {
412
AllocObject tmp = (AllocObject) objList.get(i);
413
if (tmp.getStoredHashValue() != tmp.getCurrentHashValue()) {
414
// At least one of the hash values mismatch, so the test failed
415
return false;
416
}
417
}
418
419
return true;
420
}
421
422
423
/**
424
* Verify the hash codes for all objects in all the lists.
425
*
426
* @return Success if all hash codes matches the original hash codes.
427
*/
428
public boolean verifyHashCodes() {
429
return verifyHashCodes(ai.getAllocList())
430
&& verifyHashCodes(ai.getSafeList())
431
&& verifyHashCodes(ai.getEvacList0())
432
&& verifyHashCodes(ai.getEvacList1())
433
&& verifyHashCodes(ai.getEvacList2())
434
&& verifyHashCodes(ai.getEvacList3())
435
&& verifyHashCodes(ai.getEvacList4())
436
&& verifyHashCodes(ai.getEvacList5());
437
}
438
439
/**
440
* Free all allocated objects from all the lists.
441
*/
442
public void cleanupLists() {
443
ai.getAllocList().clear();
444
ai.getSafeList().clear();
445
446
ai.getEvacList0().clear();
447
ai.getEvacList1().clear();
448
ai.getEvacList2().clear();
449
ai.getEvacList3().clear();
450
ai.getEvacList4().clear();
451
ai.getEvacList5().clear();
452
}
453
454
/**
455
* Get the size of evacuation list 0.
456
*
457
* @return Size of evacuation list 0.
458
*/
459
public long getEvac0Size() {
460
return sizeLimit0;
461
}
462
463
/**
464
* Get the size of evacuation list 1.
465
*
466
* @return Size of evacuation list 1.
467
*/
468
public long getEvac1Size() {
469
return sizeLimit1;
470
}
471
472
/**
473
* Get the size of evacuation list 2.
474
*
475
* @return Size of evacuation list 2.
476
*/
477
public long getEvac2Size() {
478
return sizeLimit2;
479
}
480
481
/**
482
* Get the size of evacuation list 3.
483
*
484
* @return Size of evacuation list 3.
485
*/
486
public long getEvac3Size() {
487
return sizeLimit3;
488
}
489
490
/**
491
* Get the size of evacuation list 4.
492
*
493
* @return Size of evacuation list 4.
494
*/
495
public long getEvac4Size() {
496
return sizeLimit4;
497
}
498
499
/**
500
* Get the size of evacuation list 5.
501
*
502
* @return Size of evacuation list 5.
503
*/
504
public long getEvac5Size() {
505
return sizeLimit5;
506
}
507
}
508
509