File: TestJNICriticalStressTest.java

package info (click to toggle)
openjdk-25 25.0.1%2B8-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • 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 (232 lines) | stat: -rw-r--r-- 8,347 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2023, 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.
 */

/*
 * @test G1 Full GC execution when JNICritical is active
 * @summary Check that Full GC calls are not ignored if concurrent with an active GCLocker.
 * @bug 8057586
 * @requires vm.gc.G1
 * @modules java.base/jdk.internal.misc
 * @library /test/lib
 * @build jdk.test.whitebox.WhiteBox
 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseG1GC -Xms1g -Xmx1g -Xlog:gc TestJNICriticalStressTest 30 4 4 G1
 */

 /*
 * @test Parallel Full GC execution when JNICritical is active
 * @bug 8057586
 * @requires vm.gc.Parallel
 * @modules java.base/jdk.internal.misc
 * @library /test/lib
 * @build jdk.test.whitebox.WhiteBox
 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseParallelGC -Xms1g -Xmx1g -Xlog:gc TestJNICriticalStressTest 30 4 4 Parallel
 */

/*
 * @test Serial Full GC execution when JNICritical is active
 * @bug 8057586
 * @requires vm.gc.Serial
 * @modules java.base/jdk.internal.misc
 * @library /test/lib
 * @build jdk.test.whitebox.WhiteBox
 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseSerialGC -Xms1g -Xmx1g -Xlog:gc TestJNICriticalStressTest 30 4 4 Serial
 */

import jdk.test.lib.Asserts;
import jdk.test.whitebox.WhiteBox;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.Deflater;

/**
 * Test verifies that Full GC calls are not ignored if concurrent with an active GCLocker.
 *
 * The test checks that at least a full gc is executed in the duration of a WhiteBox.fullGC() call;
 * either by the calling thread or a concurrent thread.
 */
public class TestJNICriticalStressTest {
    private static final WhiteBox wb = WhiteBox.getWhiteBox();

    static private final int LARGE_MAP_SIZE = 64 * 1024;

    static private final int MAP_ARRAY_LENGTH = 4;
    static private final int MAP_SIZE = 1024;

    static private final int BYTE_ARRAY_LENGTH = 16 * 1024;

    static private final long SYSTEM_GC_PERIOD_MS = 5 * 1000;
    static private long gcCountBefore = 0;
    static private GarbageCollectorMXBean collector = null;

    static private void println(String str) { System.out.println(str); }
    static private void exit(int code)      { System.exit(code);       }

    static Map<Integer,String> populateMap(int size) {
        Map<Integer,String> map = new HashMap<Integer,String>();
        for (int i = 0; i < size; i += 1) {
            Integer keyInt = Integer.valueOf(i);
            String valStr = "value is [" + i + "]";
            map.put(keyInt,valStr);
        }
        return map;
    }

    static private class AllocatingWorker implements Runnable {
        private final Object[] array = new Object[MAP_ARRAY_LENGTH];
        private int arrayIndex = 0;

        private void doStep() {
            Map<Integer,String> map = populateMap(MAP_SIZE);
            array[arrayIndex] = map;
            arrayIndex = (arrayIndex + 1) % MAP_ARRAY_LENGTH;
        }

        public void run() {
            while (true) {
                doStep();
            }
        }
    }

    static private class JNICriticalWorker implements Runnable {
        private int count;

        private void doStep() {
            byte[] inputArray = new byte[BYTE_ARRAY_LENGTH];
            for (int i = 0; i < inputArray.length; i += 1) {
                inputArray[i] = (byte) (count + i);
            }

            Deflater deflater = new Deflater();
            deflater.setInput(inputArray);
            deflater.finish();

            byte[] outputArray = new byte[2 * inputArray.length];
            deflater.deflate(outputArray);
            deflater.end();

            count += 1;
        }

        public void run() {
            while (true) {
                doStep();
            }
        }
    }

    static private class SystemGCWorker implements Runnable {
        public void run() {
            long fullGcCounts = 0;
            while (true) {
                try {
                    Thread.sleep(SYSTEM_GC_PERIOD_MS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }

                long gcCountBefore = collector.getCollectionCount();
                wb.fullGC();
                long gcCountAfter = collector.getCollectionCount();

                Asserts.assertLessThan(gcCountBefore, gcCountAfter, "Triggered more Full GCs than expected");
            }
        }
    }

    static public Map<Integer,String> largeMap;

    public static void main(String... args) throws Exception {
        if (args.length < 4) {
            println("usage: JNICriticalStressTest <duration sec> <alloc threads> <jni critical threads> <gc name>");
            exit(-1);
        }

        long durationSec = Long.parseLong(args[0]);
        int allocThreadNum = Integer.parseInt(args[1]);
        int jniCriticalThreadNum = Integer.parseInt(args[2]);

        StringBuilder OldGCName = new StringBuilder();
        switch (args[3]) {
            case "G1":
                OldGCName.append("G1 Old Generation");
                break;
            case "Parallel":
                OldGCName.append("PS MarkSweep");
                break;
            case "Serial":
                OldGCName.append("MarkSweepCompact");
                break;
            default:
                throw new RuntimeException("Unsupported GC selected");
        }

        List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();

        for (int i = 0; i < collectors.size(); i++) {
            if (collectors.get(i).getName().contains(OldGCName.toString())) {
                collector = collectors.get(i);
                break;
            }
        }

        if (collector == null) {
            throw new RuntimeException(OldGCName.toString() + " not found");
        }

        println("Running for " + durationSec + " secs");

        largeMap = populateMap(LARGE_MAP_SIZE);

        // Start threads to allocate memory, this will trigger both GCLocker initiated
        // garbage collections (GCs) and regular GCs. Thus increasing the likelihood of
        // having different types of GCs happening concurrently with the System.gc call.
        println("Starting " + allocThreadNum + " allocating threads");
        for (int i = 0; i < allocThreadNum; i += 1) {
            new Thread(new AllocatingWorker()).start();
        }

        println("Starting " + jniCriticalThreadNum + " jni critical threads");
        for (int i = 0; i < jniCriticalThreadNum; i += 1) {
            new Thread(new JNICriticalWorker()).start();
        }

        new Thread(new SystemGCWorker()).start();

        long durationMS = (long) (1000 * durationSec);
        try {
            Thread.sleep(durationMS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            exit(-1);
        }
   }
}