File: HCHelper.java

package info (click to toggle)
openjdk-25 25.0.1%2B8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 825,408 kB
  • sloc: java: 5,585,680; cpp: 1,333,948; xml: 1,321,242; ansic: 488,034; asm: 404,003; objc: 21,088; sh: 15,106; javascript: 13,265; python: 8,319; makefile: 2,518; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (507 lines) | stat: -rw-r--r-- 14,179 bytes parent folder | download | duplicates (19)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 * Copyright (c) 2011, 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 gc.hashcode;

import java.util.ArrayList;
import java.util.Random;

/**
 * Helper class for the hash code tests.
 */
public final class HCHelper {

    /**
     * Evacuation list 0 constant.
     */
    public static final int EVAC_LIST_0 = 0;
    /**
     * Evacuation list 1 constant.
     */
    public static final int EVAC_LIST_1 = 1;
    /**
     * Evacuation list 2 constant.
     */
    public static final int EVAC_LIST_2 = 2;
    /**
     * Evacuation list 3 constant.
     */
    public static final int EVAC_LIST_3 = 3;
    /**
     * Evacuation list 4 constant.
     */
    public static final int EVAC_LIST_4 = 4;
    /**
     * Evacuation list 5 constant.
     */
    public static final int EVAC_LIST_5 = 5;
    /**
     * Evacuation list 0 percentage constant.
     */
    public static final double EVAC_SIZE_0 = 0.50;
    /**
     * Evacuation list 1 percentage constant.
     */
    public static final double EVAC_SIZE_1 = 0.14;
    /**
     * Evacuation list 2 percentage constant.
     */
    public static final double EVAC_SIZE_2 = 0.12;
    /**
     * Evacuation list 3 percentage constant.
     */
    public static final double EVAC_SIZE_3 = 0.10;
    /**
     * Evacuation list 4 percentage constant.
     */
    public static final double EVAC_SIZE_4 = 0.07;
    /**
     * Evacuation list 5 percentage constant.
     */
    public static final double EVAC_SIZE_5 = 0.05;

    /**
     * Helper class that allocates memory and also tracks the original
     * as well as current hash code.
     */
    final class AllocObject {
        private byte[] allocatedArray;
        private int hashValue;

        /**
         * Create a new allocator object that allocates size bytes.
         *
         * @param size Number of bytes to allocate.
         */
        AllocObject(int size) {
            allocatedArray = new byte[size];
            hashValue = allocatedArray.hashCode();
        }

        /**
         * Get the stored hash code value.
         *
         * @return Stored hash code.
         */
        int getStoredHashValue() {
            return hashValue;
        }

        /**
         * Get the current hash code value.
         *
         * @return Current hash code.
         */
        int getCurrentHashValue() {
            return allocatedArray.hashCode();
        }

        /**
         * Get the size of the allocated object.
         *
         * @return Size of allocated object.
         */
        int getAllocatedSize() {
            return allocatedArray.length;
        }
    }

    /**
     * Helper class that holds all the allocation lists.
     */
    final class AllocInfo {
        private long allocatedSize;
        private long numOfAllocedObjs;
        private ArrayList safeList;
        private ArrayList allocList;
        private ArrayList evacList0;
        private ArrayList evacList1;
        private ArrayList evacList2;
        private ArrayList evacList3;
        private ArrayList evacList4;
        private ArrayList evacList5;

        /**
         * Create the helper object.
         */
        AllocInfo() {
            allocatedSize = 0;
            numOfAllocedObjs = 0;
            safeList = new ArrayList();
            allocList = new ArrayList();
            evacList0 = new ArrayList();
            evacList1 = new ArrayList();
            evacList2 = new ArrayList();
            evacList3 = new ArrayList();
            evacList4 = new ArrayList();
            evacList5 = new ArrayList();
        }

        /**
         * Get the amount of memory allocated in total.
         *
         * @return Total allocated size.
         */
        public long getAllocatedSize() {
            return allocatedSize;
        }

        /**
         * Set the amount of memory allocated in total.
         *
         * @param allocatedSize Total allocated size.
         */
        public void setAllocatedSize(long allocatedSize) {
            this.allocatedSize = allocatedSize;
        }

        /**
         * Get total number of objects allocated.
         *
         * @return Number of objects allocated.
         */
        public long getNumOfAllocedObjs() {
            return numOfAllocedObjs;
        }

        /**
         * Set total number of objects allocated.
         *
         * @param numOfAllocedObjs Number of objects allocated.
         */
        public void setNumOfAllocedObjs(long numOfAllocedObjs) {
            this.numOfAllocedObjs = numOfAllocedObjs;
        }

        /**
         * Increase the number of objects allocated.
         */
        public void incNumOfAllocedObjs() {
            numOfAllocedObjs++;
        }

        /**
         * Decrease the number of objects allocated.
         */
        public void decNumOfAllocedObjs() {
            numOfAllocedObjs--;
        }

        /**
         * Get the safe list.
         *
         * @return ArrayList that contains the safe list.
         */
        public ArrayList getSafeList() {
            return safeList;
        }

        /**
         * Get the alloc list.
         *
         * @return ArrayList that contains the alloc list.
         */
        public ArrayList getAllocList() {
            return allocList;
        }

        /**
         * Get evacuation list 0.
         *
         * @return ArrayList that contains evacuation list 0.
         */
        public ArrayList getEvacList0() {
            return evacList0;
        }

        /**
         * Get evacuation list 1.
         *
         * @return ArrayList that contains evacuation list 1.
         */
        public ArrayList getEvacList1() {
            return evacList1;
        }

        /**
         * Get evacuation list 2.
         *
         * @return ArrayList that contains evacuation list 2.
         */
        public ArrayList getEvacList2() {
            return evacList2;
        }

        /**
         * Get evacuation list 3.
         *
         * @return ArrayList that contains evacuation list 3.
         */
        public ArrayList getEvacList3() {
            return evacList3;
        }

        /**
         * Get evacuation list 4.
         *
         * @return ArrayList that contains evacuation list 4.
         */
        public ArrayList getEvacList4() {
            return evacList4;
        }

        /**
         * Get evacuation list 5.
         *
         * @return ArrayList that contains evacuation list 5.
         */
        public ArrayList getEvacList5() {
            return evacList5;
        }
    }


    private int minSize;
    private int maxSize;
    private double percentToFill;
    private int allocTrigSize;
    private AllocInfo ai;
    private Random rnd;

    private long sizeLimit0;
    private long sizeLimit1;
    private long sizeLimit2;
    private long sizeLimit3;
    private long sizeLimit4;
    private long sizeLimit5;

    /**
     * Create the helper class.
     *
     * @param minSize Minimum size of objects to allocate.
     * @param maxSize Maximum size of objects to allocate.
     * @param seed Random seed to use.
     * @param percentToFill Percentage of the heap to fill.
     * @param allocTrigSize Object size to use when triggering a GC.
     */
    public HCHelper(int minSize, int maxSize, long seed,
                    double percentToFill, int allocTrigSize) {
        this.minSize = minSize;
        this.maxSize = maxSize;
        this.percentToFill = percentToFill;
        this.allocTrigSize = allocTrigSize;
        ai = new AllocInfo();
        rnd = new Random(seed);

        sizeLimit0 = 0;
        sizeLimit1 = 0;
        sizeLimit2 = 0;
        sizeLimit3 = 0;
        sizeLimit4 = 0;
        sizeLimit5 = 0;
    }

    /**
     * Setup all the evacuation lists and fill them with objects.
     */
    public void setupLists() {
        Runtime r = Runtime.getRuntime();
        long maxMem = r.maxMemory();
        long safeMaxMem = (long) (maxMem * percentToFill);
        sizeLimit0 = (long) (safeMaxMem * EVAC_SIZE_0);
        sizeLimit1 = (long) (safeMaxMem * EVAC_SIZE_1);
        sizeLimit2 = (long) (safeMaxMem * EVAC_SIZE_2);
        sizeLimit3 = (long) (safeMaxMem * EVAC_SIZE_3);
        sizeLimit4 = (long) (safeMaxMem * EVAC_SIZE_4);
        sizeLimit5 = (long) (safeMaxMem * EVAC_SIZE_5);

        // Fill the memory with objects
        System.gc();
        allocObjects(ai.getEvacList0(), sizeLimit0);
        System.gc();
        allocObjects(ai.getEvacList1(), sizeLimit1);
        System.gc();
        allocObjects(ai.getEvacList2(), sizeLimit2);
        System.gc();
        allocObjects(ai.getEvacList3(), sizeLimit3);
        System.gc();
        allocObjects(ai.getEvacList4(), sizeLimit4);
        System.gc();
        allocObjects(ai.getEvacList5(), sizeLimit5);
        System.gc();
    }

    private void allocObjects(ArrayList al, long totalSizeLimit) {
        long allocedSize = 0;
        int multiplier = maxSize - minSize;

        while (allocedSize < totalSizeLimit) {
            int allocSize = minSize + (int) (rnd.nextDouble() * multiplier);
            if (allocSize >= totalSizeLimit - allocedSize) {
                allocSize = (int) (totalSizeLimit - allocedSize);
            }

            al.add(new AllocObject(allocSize));
            allocedSize += allocSize;
        }
    }

    /**
     * Free all objects in a specific evacuation list.
     *
     * @param listNr The evacuation list to clear. Must be between 0 and 5.
     */
    public void clearList(int listNr) {
        if (listNr < EVAC_LIST_0 || listNr > EVAC_LIST_5) {
            throw new IllegalArgumentException("List to removed bust be "
                    + "between EVAC_LIST_0 and EVAC_LIST_5");
        }

        switch (listNr) {
            case EVAC_LIST_0:
                ai.getEvacList0().clear();
                break;
            case EVAC_LIST_1:
                ai.getEvacList1().clear();
                break;
            case EVAC_LIST_2:
                ai.getEvacList2().clear();
                break;
            case EVAC_LIST_3:
                ai.getEvacList3().clear();
                break;
            case EVAC_LIST_4:
                ai.getEvacList4().clear();
                break;
            case EVAC_LIST_5:
                ai.getEvacList5().clear();
                break;
            default: // Should never occur, since we test the listNr param
                break;
        }
    }

    /**
     * Verify the hash codes for a list of AllocObject:s.
     *
     * @param objList ArrayList containing AllocObject:s
     * @return true if all hash codes are OK, otherwise false
     */
    boolean verifyHashCodes(ArrayList objList) {
        // Check the hash values
        for (int i = 0; i < objList.size(); i++) {
            AllocObject tmp = (AllocObject) objList.get(i);
            if (tmp.getStoredHashValue() != tmp.getCurrentHashValue()) {
                // At least one of the hash values mismatch, so the test failed
                return false;
            }
        }

        return true;
    }


    /**
     * Verify the hash codes for all objects in all the lists.
     *
     * @return Success if all hash codes matches the original hash codes.
     */
    public boolean verifyHashCodes() {
        return verifyHashCodes(ai.getAllocList())
                && verifyHashCodes(ai.getSafeList())
                && verifyHashCodes(ai.getEvacList0())
                && verifyHashCodes(ai.getEvacList1())
                && verifyHashCodes(ai.getEvacList2())
                && verifyHashCodes(ai.getEvacList3())
                && verifyHashCodes(ai.getEvacList4())
                && verifyHashCodes(ai.getEvacList5());
    }

    /**
     * Free all allocated objects from all the lists.
     */
    public void cleanupLists() {
        ai.getAllocList().clear();
        ai.getSafeList().clear();

        ai.getEvacList0().clear();
        ai.getEvacList1().clear();
        ai.getEvacList2().clear();
        ai.getEvacList3().clear();
        ai.getEvacList4().clear();
        ai.getEvacList5().clear();
    }

    /**
     * Get the size of evacuation list 0.
     *
     * @return Size of evacuation list 0.
     */
    public long getEvac0Size() {
        return sizeLimit0;
    }

    /**
     * Get the size of evacuation list 1.
     *
     * @return Size of evacuation list 1.
     */
    public long getEvac1Size() {
        return sizeLimit1;
    }

    /**
     * Get the size of evacuation list 2.
     *
     * @return Size of evacuation list 2.
     */
    public long getEvac2Size() {
        return sizeLimit2;
    }

    /**
     * Get the size of evacuation list 3.
     *
     * @return Size of evacuation list 3.
     */
    public long getEvac3Size() {
        return sizeLimit3;
    }

    /**
     * Get the size of evacuation list 4.
     *
     * @return Size of evacuation list 4.
     */
    public long getEvac4Size() {
        return sizeLimit4;
    }

    /**
     * Get the size of evacuation list 5.
     *
     * @return Size of evacuation list 5.
     */
    public long getEvac5Size() {
        return sizeLimit5;
    }
}