File: TestStressArrayCopy.java

package info (click to toggle)
openjdk-11 11.0.28%2B6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,124 kB
  • sloc: java: 5,208,481; xml: 1,192,267; cpp: 1,138,346; ansic: 461,925; javascript: 162,416; sh: 16,738; objc: 13,729; python: 4,757; asm: 3,570; makefile: 2,965; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (232 lines) | stat: -rw-r--r-- 8,396 bytes parent folder | download | duplicates (5)
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) 2021, Red Hat, Inc. 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 compiler.arraycopy.stress;

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

import jdk.test.lib.Platform;
import jdk.test.lib.Utils;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;

import sun.hotspot.cpuinfo.CPUInfo;

/**
 * @test
 * @library /test/lib
 * @build compiler.arraycopy.stress.AbstractStressArrayCopy
 *        compiler.arraycopy.stress.StressBooleanArrayCopy
 *        compiler.arraycopy.stress.StressByteArrayCopy
 *        compiler.arraycopy.stress.StressCharArrayCopy
 *        compiler.arraycopy.stress.StressShortArrayCopy
 *        compiler.arraycopy.stress.StressIntArrayCopy
 *        compiler.arraycopy.stress.StressFloatArrayCopy
 *        compiler.arraycopy.stress.StressLongArrayCopy
 *        compiler.arraycopy.stress.StressDoubleArrayCopy
 *        compiler.arraycopy.stress.StressObjectArrayCopy
 *        sun.hotspot.WhiteBox
 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 *
 * @run main/othervm/timeout=7200
 *      -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 *      compiler.arraycopy.stress.TestStressArrayCopy
 */
public class TestStressArrayCopy {

    // These tests are remarkably memory bandwidth hungry. Running multiple
    // configs in parallel makes sense only when running a single test in
    // isolation, and only on machines with many memory channels. In common
    // testing, or even running all arraycopy stress tests at once, overloading
    // the system with many configs become counter-productive very quickly.
    //
    // Default to 1/4 of the CPUs, and allow users to override.
    static final int MAX_PARALLELISM = Integer.getInteger("maxParallelism",
        Math.max(1, Runtime.getRuntime().availableProcessors() / 4));

    private static List<String> mix(List<String> o, String... mix) {
        List<String> n = new ArrayList<>(o);
        for (String m : mix) {
            n.add(m);
        }
        return n;
    }

    private static List<List<String>> product(List<List<String>> list, String... mix) {
        List<List<String>> newList = new ArrayList<>();
        for (List<String> c : list) {
            for (String m : mix) {
                newList.add(mix(c, m));
            }
        }
        return newList;
    }

    private static List<List<String>> alternate(List<List<String>> list, String opt) {
        return product(list, "-XX:+" + opt, "-XX:-" + opt);
    }

    private static boolean containsFuzzy(List<String> list, String sub) {
        for (String s : list) {
            if (s.contains(sub)) return true;
        }
        return false;
    }

    public static void main(String... args) throws Exception {
        List<List<String>> configs = new ArrayList<>();
        List<String> cpuFeatures = CPUInfo.getFeatures();

        if (Platform.isX64() || Platform.isX86()) {
            // If CPU features were not found, provide a default config.
            if (cpuFeatures.isEmpty()) {
                configs.add(new ArrayList());
            }

            // Otherwise, select the tests that make sense on current platform.
            if (containsFuzzy(cpuFeatures, "avx512")) {
                configs.add(List.of("-XX:UseAVX=3"));
            }
            if (containsFuzzy(cpuFeatures, "avx2")) {
                configs.add(List.of("-XX:UseAVX=2"));
            }
            if (containsFuzzy(cpuFeatures, "avx")) {
                configs.add(List.of("-XX:UseAVX=1"));
            }
            if (containsFuzzy(cpuFeatures, "sse4")) {
                configs.add(List.of("-XX:UseAVX=0", "-XX:UseSSE=4"));
            }
            if (containsFuzzy(cpuFeatures, "sse3")) {
                configs.add(List.of("-XX:UseAVX=0", "-XX:UseSSE=3"));
            }
            if (containsFuzzy(cpuFeatures, "sse2")) {
                configs.add(List.of("-XX:UseAVX=0", "-XX:UseSSE=2"));
            }

            // x86_64 always has UseSSE >= 2. These lower configurations only
            // make sense for x86_32.
            if (Platform.isX86()) {
                if (containsFuzzy(cpuFeatures, "sse")) {
                    configs.add(List.of("-XX:UseAVX=0", "-XX:UseSSE=1"));
                }

                configs.add(List.of("-XX:UseAVX=0", "-XX:UseSSE=0"));
            }

            // Alternate configs with other flags
            if (Platform.isX64()) {
                configs = alternate(configs, "UseCompressedOops");
            }
            configs = alternate(configs, "UseUnalignedLoadStores");

        } else if (Platform.isAArch64()) {
            // AArch64.
            configs.add(new ArrayList());

            // Alternate configs with other flags
            configs = alternate(configs, "UseCompressedOops");
            configs = alternate(configs, "UseSIMDForMemoryOps");
        } else {
            // Generic config.
            configs.add(new ArrayList());
        }

        String[] classNames = {
            "compiler.arraycopy.stress.StressBooleanArrayCopy",
            "compiler.arraycopy.stress.StressByteArrayCopy",
            "compiler.arraycopy.stress.StressCharArrayCopy",
            "compiler.arraycopy.stress.StressShortArrayCopy",
            "compiler.arraycopy.stress.StressIntArrayCopy",
            "compiler.arraycopy.stress.StressFloatArrayCopy",
            "compiler.arraycopy.stress.StressLongArrayCopy",
            "compiler.arraycopy.stress.StressDoubleArrayCopy",
            "compiler.arraycopy.stress.StressObjectArrayCopy",
        };

        ArrayList<Fork> forks = new ArrayList<>();
        int jobs = 0;

        for (List<String> c : configs) {
            for (String className : classNames) {
                // Start a new job
                {
                    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(mix(c, "-Xmx256m", className).toArray(new String[0]));
                    Process p = pb.start();
                    OutputAnalyzer oa = new OutputAnalyzer(p);
                    forks.add(new Fork(p, oa));
                    jobs++;
                }

                // Wait for the completion of other jobs
                while (jobs >= MAX_PARALLELISM) {
                    Fork f = findDone(forks);
                    if (f != null) {
                        OutputAnalyzer oa = f.oa();
                        oa.shouldHaveExitValue(0);
                        forks.remove(f);
                        jobs--;
                    } else {
                        // Nothing is done, wait a little.
                        Thread.sleep(200);
                    }
                }
            }
        }

        // Drain the rest
        for (Fork f : forks) {
            OutputAnalyzer oa = f.oa();
            oa.shouldHaveExitValue(0);
        }
    }

    private static Fork findDone(List<Fork> forks) {
        for (Fork f : forks) {
            if (!f.p().isAlive()) {
                return f;
            }
        }
        return null;
    }

    private static class Fork {
        private final Process p;
        private final OutputAnalyzer oa;

        public Fork(Process p, OutputAnalyzer oa) {
            this.p = p;
            this.oa = oa;
        }

        public Process p() {
            return p;
        }

        public OutputAnalyzer oa() {
            return oa;
        }
    }

}