Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/share/actions/BaseAction.java
41161 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.compilercontrol.share.actions;2425import compiler.compilercontrol.share.pool.PoolHelper;26import compiler.compilercontrol.share.scenario.State;27import jdk.test.lib.util.Pair;28import jdk.test.lib.process.ProcessTools;2930import java.io.BufferedReader;31import java.io.IOException;32import java.io.InputStreamReader;33import java.io.OutputStreamWriter;34import java.io.PrintWriter;35import java.lang.reflect.Executable;36import java.net.InetAddress;37import java.net.Socket;38import java.util.Arrays;39import java.util.HashMap;40import java.util.List;41import java.util.ListIterator;42import java.util.Map;43import java.util.concurrent.Callable;44import java.util.stream.Collectors;4546public class BaseAction {47private static final List<Pair<Executable, Callable<?>>> METHODS;48private static final Map<String, Executable> METHODS_NAMES;4950static {51METHODS = new PoolHelper().getAllMethods();52METHODS_NAMES = METHODS.stream().collect(Collectors.toMap(53pair -> pair.first.toGenericString(),54pair -> pair.first));55}5657public static void main(String[] args) {58new BaseAction().communicate(args);59}6061/*62* args[0] is a port to connect63* args[1] is an optional parameter that shows that the state map should be64* passed65*/66protected void communicate(String[] args) {67if (args.length < 1) {68throw new Error("TESTBUG: requires port as parameter: "69+ Arrays.toString(args));70}71boolean getStates = false;72if (args.length == 2) {73if ("states".equals(args[1])) {74getStates = true;75} else {76throw new Error("TESTBUG: incorrect argument: "+ args[1]);77}78}79long pid;80try {81pid = ProcessTools.getProcessId();82} catch (Exception e) {83throw new Error("Could not determine own pid", e);84}85int port = Integer.parseInt(args[0]);86System.out.println("INFO: Client connection port = " + port);87List<String> lines;88try (89Socket socket = new Socket(InetAddress.getLocalHost(), port);90BufferedReader in = new BufferedReader(91new InputStreamReader(socket.getInputStream()));92PrintWriter out = new PrintWriter(93new OutputStreamWriter(socket.getOutputStream()))) {94// send own pid to execute jcmd if needed95out.println(String.valueOf(pid));96out.flush();97if (getStates) {98lines = in.lines().collect(Collectors.toList());99check(decodeMap(lines));100} else {101in.readLine();102}103} catch (IOException e) {104throw new Error("Error on performing network operation", e);105}106}107108private Map<Executable, State> decodeMap(List<String> lines) {109if (lines == null || lines.size() == 0) {110throw new Error("TESTBUG: unexpected lines list");111}112Map<Executable, State> stateMap = new HashMap<>();113int startIndex = 0;114ListIterator<String> iterator = lines.listIterator();115while (iterator.hasNext()) {116int index = iterator.nextIndex();117String next = iterator.next();118switch (next) {119case "{" :120startIndex = index;121break;122case "}" :123// method name goes after {124Executable executable = METHODS_NAMES.get(lines.get(125++startIndex));126// state description starts after method127State state = State.fromString(lines.subList(++startIndex,128index).toArray(new String[index - startIndex]));129stateMap.put(executable, state);130break;131}132}133return stateMap;134}135136protected void check(Map<Executable, State> methodStates) {137// Check each method from the pool138METHODS.forEach(pair -> {139Executable x = pair.first;140CompileAction.checkCompiled(x, methodStates.get(x));141});142}143}144145146