File: SegmentedCodeCacheDtraceTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (312 lines) | stat: -rw-r--r-- 12,687 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2014, 2016, 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 SegmentedCodeCacheDtraceTest
 * @bug 8015774
 * @summary testing of dtrace for segmented code cache
 * @requires os.family=="solaris"
 * @modules java.base/jdk.internal.misc
 * @library /test/lib /
 *
 * @build sun.hotspot.WhiteBox
 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
 * @run main/othervm/timeout=600 -Xbootclasspath/a:.
 *     -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 *     compiler.codecache.dtrace.SegmentedCodeCacheDtraceTest
 */

package compiler.codecache.dtrace;

import compiler.testlibrary.CompilerUtils;
import jdk.test.lib.Asserts;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.Utils;

import java.io.IOException;
import java.lang.reflect.Executable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class SegmentedCodeCacheDtraceTest {

    private static final String WORKER_CLASS_NAME
            = SegmentedCodeCacheDtraceTestWorker.class.getName();
    private static final String JAVA_OPTS = " -XX:+DTraceMethodProbes "
            + "-Xbootclasspath/a:" + System.getProperty("test.classes") + " "
            + "-XX:+UnlockDiagnosticVMOptions "
            + "-XX:+WhiteBoxAPI -XX:+SegmentedCodeCache "
            + "-XX:CompileCommand=compileonly,"
            + WORKER_CLASS_NAME + "::* "
            + " -classpath " + System.getProperty("test.class.path") + " "
            + String.join(" ", Utils.getTestJavaOpts());
    private static final String DTRACE_SCRIPT
            = "SegmentedCodeCacheDtraceTestScript.d";
    private static final List<Executable> MLIST =
            SegmentedCodeCacheDtraceTestWorker.TESTED_METHODS_LIST;
    private static final int WORKER_METHODS_COUNT = MLIST.size();

    private void runTest(TestCombination tc) {
        String params = MLIST.stream()
                .map(Executable::getName)
                .map(x -> tc.data.get(x).compileLevel + " " + tc.data.get(x).isInlined)
                .collect(Collectors.joining(" "));
        DtraceRunner runner = new DtraceRunner();
        runner.runDtrace(JDKToolFinder.getTestJDKTool("java"), JAVA_OPTS,
                WORKER_CLASS_NAME, params, Paths.get(System.getProperty("test.src"),
                        DTRACE_SCRIPT).toString(),
                DtraceRunner.PERMIT_DESTRUCTIVE_ACTIONS_DTRACE_OPTION,
                new SegmentedCodeCacheDtraceResultsAnalyzer());
    }

    private static TestCombination generateUniqueCombination(
            int[] availableLevels, Set<TestCombination> combinations) {
        int len = availableLevels.length;
        /* first, check if we're out of combinations. */
        int maxCombinationsCount
                = (1 << WORKER_METHODS_COUNT)
                * (int) Math.pow(len, WORKER_METHODS_COUNT);
        if (combinations.size() == maxCombinationsCount) {
            return null;
        }
        Random r = Utils.getRandomInstance();
        while (combinations.size() < maxCombinationsCount) {
            int levels[] = new int[WORKER_METHODS_COUNT];
            boolean inlines[] = new boolean[WORKER_METHODS_COUNT];
            for (int i = 0; i < WORKER_METHODS_COUNT; i++) {
                levels[i] = availableLevels[r.nextInt(len)];
                inlines[i] = r.nextBoolean();
            }
            TestCombination tc = new TestCombination(levels, inlines);
            if (combinations.add(tc)) {
                return tc;
            }
        }
        return null;
    }

    public static void main(String args[]) {
        int iterations
                = Integer.getInteger("jdk.test.lib.iterations", 1);
        if (!DtraceRunner.dtraceAvailable()) {
            System.out.println("INFO: There is no dtrace avaiable. Skipping.");
            return;
        }
        int[] availableLevels = CompilerUtils.getAvailableCompilationLevels();
        // adding one more entry(zero) for interpeter
        availableLevels
                = Arrays.copyOf(availableLevels, availableLevels.length + 1);
        Set<TestCombination> combinations = new HashSet<>();
        for (int i = 0; i < iterations; i++) {
            TestCombination tc
                    = generateUniqueCombination(availableLevels, combinations);
            if (tc == null) {
                System.out.println("INFO: no more combinations available");
                return;
            } else {
                System.out.println("INFO: Running testcase for: " + tc);
                new SegmentedCodeCacheDtraceTest().runTest(tc);
            }
        }
    }

    private static class MethodData {

        public final int compileLevel;
        public final boolean isInlined;
        public final String name;

        public MethodData(String name, int compileLevel, boolean isInlined) {
            this.name = name;
            this.compileLevel = compileLevel;
            this.isInlined = isInlined;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null || !(o instanceof MethodData)) {
                return false;
            }
            MethodData md = (MethodData) o;
            return md.compileLevel == compileLevel
                    && md.isInlined == isInlined
                    && md.name.equals(name);
        }

        @Override
        public int hashCode() {
            return 100 * name.hashCode() + 10 * compileLevel + (isInlined ? 1 : 0);
        }

        @Override
        public String toString() {
            return name + " " + compileLevel + " " + isInlined;
        }
    }

    private static class TestCombination {

        private final Map<String, MethodData> data;

        public TestCombination(int compLevels[], boolean inlines[]) {
            Map<String, MethodData> d = new HashMap<>();
            for (int i = 0; i < MLIST.size(); i++) {
                d.put(MLIST.get(i).getName(), new MethodData(MLIST.get(i).getName(),
                        compLevels[i], inlines[i]));
            }
            data = Collections.unmodifiableMap(d);
        }

        @Override
        public boolean equals(Object o) {
            if (o == null || !(o instanceof TestCombination)) {
                return false;
            }
            TestCombination second = (TestCombination) o;
            return second.data.equals(data);
        }

        @Override
        public int hashCode() {
            int sum = 0;
            for (MethodData md : data.values()) {
                sum += md.hashCode();
            }
            return sum;
        }

        private String getMethodDescString(MethodData md) {
            return (md == null)
                    ? null
                    : String.format("Method %s compilation level %d and %s",
                            md.name, md.compileLevel,
                            md.isInlined ? "inlined" : "not inlined");
        }

        @Override
        public String toString() {
            return data.values().stream().map(m -> getMethodDescString(m))
                    .collect(Collectors.joining(Utils.NEW_LINE,
                                    "Combination: ", ""));
        }
    }

    private class SegmentedCodeCacheDtraceResultsAnalyzer
            implements DtraceResultsAnalyzer {

        private static final int EXPECTED_MATCH_COUNT = 2;

        private final Pattern checkPattern;

        public SegmentedCodeCacheDtraceResultsAnalyzer() {
            String workerClassRegExp = "\\s*" + WORKER_CLASS_NAME + "\\.";
            String delimeter = "\\(\\)V\\*?" + workerClassRegExp;
            String suffix = "test\\(\\)V\\*?" + workerClassRegExp
                    + "main\\(\\[Ljava\\/lang\\/String;\\)V";
            StringBuilder sb = new StringBuilder(workerClassRegExp);
            // method order is important, so, going from list tail to head,
            // accoring to call order representation in stacktrace
            for (int i = MLIST.size() - 1; i > -1; i--) {
                sb.append(MLIST.get(i).getName()).append(delimeter);
            }
            sb.append(suffix);
            checkPattern = Pattern.compile(sb.toString());
            /* such pattern match should pass on a stacktrace like
             CPU     ID                    FUNCTION:NAME
             0  53573 __1cNSharedRuntimeTdtrace_method_entry6FpnKJavaThread_pnGMethod__i_:method-entry ustack:

             libjvm.so`__1cNSharedRuntimeTdtrace_method_entry6FpnKJavaThread_pnGMethod__i_+0x39c
             SegmentedCodeCacheDtraceTestWorker.baz()V*
             SegmentedCodeCacheDtraceTestWorker.bar()V
             SegmentedCodeCacheDtraceTestWorker.foo()V*
             SegmentedCodeCacheDtraceTestWorker.test()V
             SegmentedCodeCacheDtraceTestWorker.main([Ljava/lang/String;)V
             0xffffffff6b0004b8
             libjvm.so`__1cJJavaCallsLcall_helper6FpnJJavaValue_pnMmethodHandle_pnRJavaCallArguments_pnGThread__v_+0x94c
             libjvm.so`__1cRjni_invoke_static6FpnHJNIEnv__pnJJavaValue_pnI_jobject_nLJNICallType_pnK_jmethodID_pnSJNI_ArgumentPusher_pnGThread__v_+0xa64
             libjvm.so`jni_CallStaticVoidMethod+0x508
             libjli.so`JavaMain+0x584
             libc.so.1`_lwp_start
             jstack:

             libjvm.so`__1cNSharedRuntimeTdtrace_method_entry6FpnKJavaThread_pnGMethod__i_+0x39c
             SegmentedCodeCacheDtraceTestWorker.baz()V*
             SegmentedCodeCacheDtraceTestWorker.bar()V
             SegmentedCodeCacheDtraceTestWorker.foo()V*
             SegmentedCodeCacheDtraceTestWorker.test()V
             SegmentedCodeCacheDtraceTestWorker.main([Ljava/lang/String;)V
             0xffffffff6b0004b8
             libjvm.so`__1cJJavaCallsLcall_helper6FpnJJavaValue_pnMmethodHandle_pnRJavaCallArguments_pnGThread__v_+0x94c
             libjvm.so`__1cRjni_invoke_static6FpnHJNIEnv__pnJJavaValue_pnI_jobject_nLJNICallType_pnK_jmethodID_pnSJNI_ArgumentPusher_pnGThread__v_+0xa64
             libjvm.so`jni_CallStaticVoidMethod+0x508
             libjli.so`JavaMain+0x584
             libc.so.1`_lwp_start
             */
        }

        protected List<String> loadLog(String dtraceOutFile) throws IOException {
            return Files.readAllLines(Paths.get(dtraceOutFile));
        }

        @Override
        public void analyze(OutputAnalyzer oa, String dtraceOutFilePath) {
            oa.shouldHaveExitValue(0);
            List<String> dOut;
            try {
                dOut = loadLog(dtraceOutFilePath);
            } catch (IOException e) {
                throw new Error("Can't load log", e);
            }
            StringBuilder allDtraceOutput = new StringBuilder();
            for (String entry : dOut) {
                allDtraceOutput.append(entry);
            }
            int matchCount = getMatchCount(allDtraceOutput.toString());
            Asserts.assertEQ(matchCount, EXPECTED_MATCH_COUNT,
                    "Unexpected output match amount. expected: "
                    + EXPECTED_MATCH_COUNT + " but found " + matchCount);
        }

        protected int getMatchCount(String source) {
            Matcher m = checkPattern.matcher(source);
            int matchCount = 0;
            while (m.find()) {
                matchCount++;
            }
            return matchCount;
        }
    }
}