File: CheckControl.java

package info (click to toggle)
openjdk-11 11.0.22%2B7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,236 kB
  • sloc: java: 5,174,754; xml: 1,192,262; cpp: 1,133,036; ansic: 461,316; javascript: 162,554; sh: 16,859; objc: 13,683; python: 4,753; asm: 3,570; makefile: 2,894; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (246 lines) | stat: -rw-r--r-- 10,119 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2017, 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.concurrent_phase_control;

/*
 * Utility class that uses the WhiteBox concurrent GC phase control to
 * step through a provided sequence of phases, and verify that the
 * phases were actually reached as expected.
 *
 * To use:
 *
 * (1) The main test class has a main function which calls this helper
 * class's check() function with appropriate arguments for the
 * collector being tested.
 *
 * (2) The test program must provide access to WhiteBox, as it is used
 * by this support class.
 *
 * (4) The main test class should be invoked as a driver.  This
 * helper class's check() function will run its Executor class in a
 * subprocess, in order to capture its output for analysis.
 */

import sun.hotspot.WhiteBox;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public final class CheckControl {
    // gcName: The name of the GC, logged as "Using <name>" near the
    // beginning of the log output.
    //
    // gcOptions: Command line options for invoking the desired
    // collector and logging options to produce output that can be
    // matched against the regex patterns in the gcPhaseInfo pairs.
    //
    // gcPhaseInfo: An array of pairs of strings.  Each pair is a
    // phase name and a regex pattern for recognizing the associated
    // log message.  The regex pattern can be null if no log message
    // is associated with the named phase.  The test will iterate
    // through the array, requesting each phase in turn.
    public static void check(String gcName,
                             String[] gcOptions,
                             String[][] gcPhaseInfo) throws Exception {
        String[] stepPhases = new String[gcPhaseInfo.length];
        for (int i = 0; i < gcPhaseInfo.length; ++i) {
            stepPhases[i] = gcPhaseInfo[i][0];
        }
        String messages = executeTest(gcName, gcOptions, stepPhases);
        checkPhaseControl(messages, gcPhaseInfo);
    }

    private static void fail(String message) throws Exception {
        throw new RuntimeException(message);
    }

    private static final String requestPrefix = "Requesting concurrent phase: ";
    private static final String reachedPrefix = "Reached concurrent phase: ";

    private static String executeTest(String gcName,
                                      String[] gcOptions,
                                      String[] gcStepPhases) throws Exception {
        System.out.println("\n---------- Testing ---------");

        final String[] wb_arguments = {
            "-Xbootclasspath/a:.",
            "-XX:+UnlockDiagnosticVMOptions",
            "-XX:+WhiteBoxAPI"
        };

        List<String> arglist = new ArrayList<String>();
        Collections.addAll(arglist, wb_arguments);
        Collections.addAll(arglist, gcOptions);
        arglist.add(Executor.class.getName());
        Collections.addAll(arglist, gcStepPhases);
        String[] arguments = arglist.toArray(new String[arglist.size()]);

        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(arguments);
        OutputAnalyzer output = new OutputAnalyzer(pb.start());

        String messages = output.getStdout();
        System.out.println(messages);

        output.shouldHaveExitValue(0);
        output.shouldContain("Using " + gcName);

        return messages;
    }

    private static void checkPhaseControl(String messages,
                                          String[][] gcPhaseInfo)
        throws Exception
    {
        // Iterate through the phase sequence for the test, verifying
        // output contains appropriate sequences of request message,
        // log message for phase, and request reached message.  Note
        // that a log message for a phase may occur later than the
        // associated request reached message, or even the following
        // request message.

        Pattern nextReqP = Pattern.compile(requestPrefix);
        Matcher nextReqM = nextReqP.matcher(messages);

        Pattern nextReachP = Pattern.compile(reachedPrefix);
        Matcher nextReachM = nextReachP.matcher(messages);

        String pendingPhaseMessage = null;
        int pendingPhaseMessagePosition = -1;

        int position = 0;
        for (String[] phase: gcPhaseInfo) {
            String phaseName = phase[0];
            String phaseMsg = phase[1];

            System.out.println("Checking phase " + phaseName);

            // Update the "next" matchers to refer to the next
            // corresponding pair of request and reached messages.
            if (!nextReqM.find()) {
                fail("Didn't find next phase request");
            } else if ((position != 0) && (nextReqM.start() < nextReachM.end())) {
                fail("Next request before previous reached");
            } else if (!nextReachM.find()) {
                fail("Didn't find next phase reached");
            } else if (nextReachM.start() <= nextReqM.end()) {
                fail("Next request/reached misordered");
            }

            // Find the expected request message, and ensure it is the next.
            Pattern reqP = Pattern.compile(requestPrefix + phaseName);
            Matcher reqM = reqP.matcher(messages);
            if (!reqM.find(position)) {
                fail("Didn't find request for " + phaseName);
            } else if (reqM.start() != nextReqM.start()) {
                fail("Request mis-positioned for " + phaseName);
            }

            // Find the expected reached message, and ensure it is the next.
            Pattern reachP = Pattern.compile(reachedPrefix + phaseName);
            Matcher reachM = reachP.matcher(messages);
            if (!reachM.find(position)) {
                fail("Didn't find reached for " + phaseName);
            } else if (reachM.start() != nextReachM.start()) {
                fail("Reached mis-positioned for " + phaseName);
            }

            // If there is a pending log message (see below), ensure
            // it was before the current reached message.
            if (pendingPhaseMessage != null) {
                if (pendingPhaseMessagePosition >= reachM.start()) {
                    fail("Log message after next reached message: " +
                         pendingPhaseMessage);
                }
            }

            // If the phase has an associated logging message, verify
            // such a logging message is present following the
            // request, and otherwise positioned appropriately.  The
            // complication here is that the logging message
            // associated with a request might follow the reached
            // message, and even the next request message, if there is
            // a later request.  But it must preceed the next
            // logging message and the next reached message.
            boolean clearPendingPhaseMessage = true;
            if (phaseMsg != null) {
                Pattern logP = Pattern.compile("GC\\(\\d+\\)\\s+" + phaseMsg);
                Matcher logM = logP.matcher(messages);
                if (!logM.find(reqM.end())) {
                    fail("Didn't find message " + phaseMsg);
                }

                if (pendingPhaseMessage != null) {
                    if (pendingPhaseMessagePosition >= logM.start()) {
                        fail("Log messages out of order: " +
                             pendingPhaseMessage + " should preceed " +
                             phaseMsg);
                    }
                }

                if (reachM.end() <= logM.start()) {
                    clearPendingPhaseMessage = false;
                    pendingPhaseMessage = phaseMsg;
                    pendingPhaseMessagePosition = logM.end();
                }
            }
            if (clearPendingPhaseMessage) {
                pendingPhaseMessage = null;
                pendingPhaseMessagePosition = -1;
            }

            // Update position for start of next phase search.
            position = reachM.end();
        }
        // It's okay for there to be a leftover pending phase message.
        // We know it was found before the end of the log.
    }

    private static final class Executor {
        private static final WhiteBox WB = WhiteBox.getWhiteBox();

        private static void step(String phase) {
            System.out.println(requestPrefix + phase);
            WB.requestConcurrentGCPhase(phase);
            System.out.println(reachedPrefix + phase);
        }

        public static void main(String[] phases) throws Exception {
            // Iterate through set sequence of phases, reporting each.
            for (String phase: phases) {
                step(phase);
            }
            // Wait a little to allow a delayed logging message for
            // the final request/reached to be printed before exiting
            // the program.
            Thread.sleep(250);
        }
    }
}