@Override public void write(int b) throws IOException { write(new byte[]{(byte) b}, 0, 1); }
static SubstrateServerMessage receive(DataInputStream is) throws IOException { try { ServerCommand command = ServerCommand.values()[is.readInt()]; int length = is.readInt(); byte[] payload = new byte[length]; is.readFully(payload); return new SubstrateServerMessage(command, payload); } catch (EOFException ex) { return null; } }
public static void main(String[] argsArray) { System.exit(run(argsArray, System.out::print, System.err::print)); } }
public static int run(String[] argsArray, Consumer<byte[]> out, Consumer<byte[]> err) { Consumer<String> outln = s -> out.accept((s + "\n").getBytes()); final List<String> args = new ArrayList<>(Arrays.asList(argsArray)); if (args.size() < 1) { usage(outln); return EXIT_FAIL; } else if (args.size() == 1 && (args.get(0).equals("--help"))) { usage(outln); return EXIT_SUCCESS; } final Optional<String> command = extractArg(args, COMMAND_PREFIX).map(arg -> arg.substring(COMMAND_PREFIX.length())); final Optional<Integer> port = NativeImageBuildServer.extractPort(args); if (port.isPresent() && command.isPresent()) { ServerCommand serverCommand = ServerCommand.valueOf(command.get()); return sendRequest(serverCommand, String.join(" ", args).getBytes(), port.get(), out, err); } else { usage(outln); return EXIT_FAIL; } }
private boolean processRequest(Socket socket) { try { DataOutputStream output = new DataOutputStream(socket.getOutputStream()); DataInputStream input = new DataInputStream(socket.getInputStream()); try { return processCommand(socket, SubstrateServerMessage.receive(input)); } catch (Throwable t) { log("Execution failed: " + t + "\n"); t.printStackTrace(logOutput); sendExitStatus(output, 1); } } catch (IOException ioe) { log("Failed fetching the output stream."); } finally { closeConnection(socket); log("Connection with the client closed.\n"); // Remove the application class loader and save a GC on the next compilation System.gc(); System.runFinalization(); System.gc(); log("Available Memory: " + Runtime.getRuntime().freeMemory() + "\n"); } return true; }
DataInputStream is = new DataInputStream(svmClient.getInputStream())) { SubstrateServerMessage.send(new SubstrateServerMessage(command, payload), os); if (ServerCommand.GET_VERSION.equals(command)) { SubstrateServerMessage response = SubstrateServerMessage.receive(is); if (response != null) { outln.accept(new String(response.payload)); while ((serverCommand = SubstrateServerMessage.receive(is)) != null) { Consumer<byte[]> selectedConsumer; switch (serverCommand.command) { if (!ServerCommand.GET_VERSION.equals(command)) { errln.accept("Could not connect to image build server running on port " + port); errln.accept("Underlying exception: " + e);
@Override public void write(byte[] b, int off, int len) throws IOException { if (interrupted) { throw new ClosedByInterruptException(); } writing = true; try { SubstrateServerMessage message = new SubstrateServerMessage(command, b, off, len); SubstrateServerMessage.send(message, original); } finally { writing = false; } }
private int sendRequest(Consumer<byte[]> out, Consumer<byte[]> err, ServerCommand serverCommand, String... args) { List<String> argList = Arrays.asList(args); showVerboseMessage(verboseServer, "Sending to server ["); showVerboseMessage(verboseServer, serverCommand.toString()); if (argList.size() > 0) { showVerboseMessage(verboseServer, String.join(" \\\n", argList)); } showVerboseMessage(verboseServer, "]"); int exitCode = NativeImageBuildClient.sendRequest(serverCommand, String.join("\n", argList).getBytes(), port, out, err); showVerboseMessage(verboseServer, "Server returns: " + exitCode); return exitCode; }
private static void resetGlobalStateInLoggers() { LogManager.getLogManager().reset(); withGlobalStaticField("java.util.logging.Level$KnownLevel", "nameToLevels", NativeImageBuildServer::removeImageLoggers); withGlobalStaticField("java.util.logging.Level$KnownLevel", "intToLevels", NativeImageBuildServer::removeImageLoggers); }
private static Optional<String> extractLogFile(List<String> args) { Optional<String> portArg = extractArg(args, LOG_PREFIX); return portArg.map(arg -> arg.substring(LOG_PREFIX.length())); }
static void send(SubstrateServerMessage message, DataOutputStream os) throws IOException { os.writeInt(message.command.ordinal()); os.writeInt(message.length); os.write(message.payload, message.offset, message.length); os.flush(); }
private static boolean isSystemLoaderLogLevelEntry(Entry<?, ?> e) { return ((List<?>) e.getValue()).stream() .map(x -> getFieldValueOfObject("java.util.logging.Level$KnownLevel", "levelObject", x)) .allMatch(NativeImageBuildServer::isSystemClassLoader); }
private void closeServerSocket(ServerSocket serverSocket) { try { log("Terminating..."); terminated = true; serverSocket.close(); } catch (IOException e) { throw VMError.shouldNotReachHere(e); } }
private static void withGlobalStaticField(String className, String fieldName, FieldAction action) { try { Field field = Class.forName(className).getDeclaredField(fieldName); field.setAccessible(true); action.perform(field); field.setAccessible(false); } catch (NoSuchFieldException | ClassNotFoundException | IllegalAccessException e) { throw VMError.shouldNotReachHere("Static field " + fieldName + " of class " + className + " can't be reset. Underlying exception: " + e.getMessage()); } }
private static Integer executeCompilation(ArrayList<String> arguments) { final String[] classpath = NativeImageGeneratorRunner.extractImageClassPath(arguments); URLClassLoader imageClassLoader; ClassLoader applicationClassLoader = Thread.currentThread().getContextClassLoader(); try { imageClassLoader = NativeImageGeneratorRunner.installNativeImageClassLoader(classpath); final ImageBuildTask task = loadCompilationTask(arguments, imageClassLoader); try { tasks.add(task); return task.build(arguments.toArray(new String[arguments.size()]), classpath, imageClassLoader); } finally { tasks.remove(task); } } finally { Thread.currentThread().setContextClassLoader(applicationClassLoader); } }
private static void sendError(DataOutputStream output, String message) { try { SubstrateServerMessage.send(new SubstrateServerMessage(ServerCommand.WRITE_ERR, message.getBytes()), output); } catch (IOException e) { throw VMError.shouldNotReachHere(e); } }
private static void resetResourceBundle() { withGlobalStaticField("java.util.ResourceBundle", "cacheList", list -> ((ConcurrentHashMap<?, ?>) list.get(null)).clear()); }
static Optional<Integer> extractPort(List<String> args) { Optional<String> portArg = extractArg(args, PORT_PREFIX); try { return portArg.map(arg -> Integer.parseInt(arg.substring(PORT_PREFIX.length()))); } catch (Throwable ignored) { System.err.println("error: invalid port number format"); } return Optional.empty(); }
private static void sendExitStatus(DataOutputStream output, int exitStatus) { try { SubstrateServerMessage.send(new SubstrateServerMessage(ServerCommand.SEND_STATUS, ByteBuffer.allocate(4).putInt(exitStatus).array()), output); } catch (IOException e) { throw VMError.shouldNotReachHere(e); } }
private static void resetGlobalStateInGraal() { withGlobalStaticField("org.graalvm.compiler.nodes.NamedLocationIdentity$DB", "map", f -> ((EconomicSet<?>) f.get(null)).clear()); withGlobalStaticField("org.graalvm.compiler.debug.DebugContext$Immutable", "CACHE", f -> { Object[] cache = (Object[]) f.get(null); for (int i = 0; i < cache.length; i++) { cache[i] = null; } }); }