Skip to content

Commit

Permalink
#2984 Avoids recreating executors in case of a "listening server" mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Mar 10, 2022
1 parent 06441d5 commit a667e60
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 102 deletions.
Expand Up @@ -13,7 +13,7 @@
import java.io.File;

import msi.gama.headless.job.ExperimentJob;
import msi.gama.headless.runtime.LocalSimulationRuntime;
import msi.gama.headless.runtime.ExecutorBasedSimulationRuntime;
import msi.gama.headless.runtime.SimulationRuntime;
import msi.gama.precompiler.GamlAnnotations.doc;
import msi.gama.precompiler.GamlAnnotations.facet;
Expand Down Expand Up @@ -101,7 +101,7 @@ public class HeadlessStatement extends AbstractStatement {
*/
public HeadlessStatement(final IDescription desc) {
super(desc);
processorQueue = new LocalSimulationRuntime(this.numberOfThread);
processorQueue = new ExecutorBasedSimulationRuntime(this.numberOfThread);
}

/**
Expand Down
175 changes: 97 additions & 78 deletions msi.gama.headless/src/msi/gama/headless/runtime/Application.java
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* Application.java, in msi.gama.headless, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* Application.java, in msi.gama.headless, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gama.headless.runtime;

Expand All @@ -16,20 +16,11 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -77,19 +68,19 @@ public class Application implements IApplication {

/** The Constant HELP_PARAMETER. */
final public static String HELP_PARAMETER = "-help";

/** The Constant GAMA_VERSION. */
final public static String GAMA_VERSION = "-version";

/** The Constant CONSOLE_PARAMETER. */
final public static String CONSOLE_PARAMETER = "-c";

/** The Constant VERBOSE_PARAMETER. */
final public static String VERBOSE_PARAMETER = "-v";

/** The Constant THREAD_PARAMETER. */
final public static String THREAD_PARAMETER = "-hpc";

/** The Constant SOCKET_PARAMETER. */
final public static String SOCKET_PARAMETER = "-socket";

Expand All @@ -98,51 +89,53 @@ public class Application implements IApplication {

/** The Constant VALIDATE_LIBRARY_PARAMETER. */
final public static String VALIDATE_LIBRARY_PARAMETER = "-validate";

/** The Constant TEST_LIBRARY_PARAMETER. */
final public static String TEST_LIBRARY_PARAMETER = "-test";

/** The Constant BUILD_XML_PARAMETER. */
final public static String BUILD_XML_PARAMETER = "-xml";

/** The Constant CHECK_MODEL_PARAMETER. */
final public static String CHECK_MODEL_PARAMETER = "-check";

/** The Constant RUN_LIBRARY_PARAMETER. */
final public static String RUN_LIBRARY_PARAMETER = "-runLibrary";

/** The Constant BATCH_PARAMETER. */
// -> Code still exist, but not documented nor use
final public static String BATCH_PARAMETER = "-batch";

/** The Constant GAML_PARAMETER. */
final public static String GAML_PARAMETER = "-gaml";

/** The Constant WRITE_XMI. */
final public static String WRITE_XMI = "-write-xmi";

/** The head less simulation. */
public static boolean headLessSimulation = false;
/** The number of thread. */
public int numberOfThread = -1;

// /** The number of thread. */
// public int numberOfThread = -1;

/** The socket. */
public int socket = -1;

/** The console mode. */
public boolean consoleMode = false;

/** The tunneling mode. */
public boolean tunnelingMode = false;

/** The verbose. */
public boolean verbose = false;

/** The processor queue. */
public SimulationRuntime processorQueue;
public final SimulationRuntime processorQueue = new ExecutorBasedSimulationRuntime();

/** The socket server. */
public GamaWebSocketServer socketServer;

/**
* Show version.
*/
Expand Down Expand Up @@ -188,7 +181,8 @@ private static void showHelp() {
/**
* Check parameters.
*
* @param args the args
* @param args
* the args
* @return true, if successful
*/
private boolean checkParameters(final List<String> args) {
Expand All @@ -198,8 +192,10 @@ private boolean checkParameters(final List<String> args) {
/**
* Check parameters.
*
* @param args the args
* @param apply the apply
* @param args
* the args
* @param apply
* the apply
* @return true, if successful
*/
private boolean checkParameters(final List<String> args, final boolean apply) {
Expand Down Expand Up @@ -244,8 +240,8 @@ private boolean checkParameters(final List<String> args, final boolean apply) {
size = size - 2;

// Change value only if function should apply parameter
this.numberOfThread =
apply ? Integer.parseInt(after(args, THREAD_PARAMETER)) : SimulationRuntime.UNDEFINED_QUEUE_SIZE;
processorQueue.setNumberOfThreads(
apply ? Integer.parseInt(after(args, THREAD_PARAMETER)) : SimulationRuntime.UNDEFINED_QUEUE_SIZE);
}

// Commands
Expand Down Expand Up @@ -294,8 +290,10 @@ private boolean checkParameters(final List<String> args, final boolean apply) {
/**
* Show error.
*
* @param errorCode the error code
* @param path the path
* @param errorCode
* the error code
* @param path
* the path
* @return true, if successful
*/
private static boolean showError(final int errorCode, final String path) {
Expand Down Expand Up @@ -334,14 +332,14 @@ public Object start(final IApplicationContext context) throws Exception {
// With GAMA run
// ========================
HeadlessSimulationLoader.preloadGAMA();

DEBUG.OFF();

// Debug runner
if (args.contains(VALIDATE_LIBRARY_PARAMETER)) return ModelLibraryValidator.getInstance().start();
if (args.contains(TEST_LIBRARY_PARAMETER)) return ModelLibraryTester.getInstance().start();
if (args.contains(RUN_LIBRARY_PARAMETER))
return ModelLibraryRunner.getInstance().start();
else if (args.contains(CHECK_MODEL_PARAMETER)) {
if (args.contains(RUN_LIBRARY_PARAMETER)) return ModelLibraryRunner.getInstance().start();
if (args.contains(CHECK_MODEL_PARAMETER)) {
ModelLibraryGenerator.start(this, args);
// else if (args.contains(WRITE_XMI)) {
// String outputDir = "all-resources";
Expand All @@ -367,7 +365,7 @@ else if (args.contains(CHECK_MODEL_PARAMETER)) {
runGamlSimulation(args);
} else if (args.contains(BUILD_XML_PARAMETER)) {
buildXML(args);
} else if (args.contains(SOCKET_PARAMETER)) {
} else if (args.contains(SOCKET_PARAMETER)) {
createSocketServer();
} else {
runSimulation(args);
Expand All @@ -379,8 +377,10 @@ else if (args.contains(CHECK_MODEL_PARAMETER)) {
/**
* After.
*
* @param args the args
* @param arg the arg
* @param args
* the args
* @param arg
* the arg
* @return the string
*/
public String after(final List<String> args, final String arg) {
Expand All @@ -392,11 +392,16 @@ public String after(final List<String> args, final String arg) {
/**
* Builds the XML.
*
* @param arg the arg
* @throws ParserConfigurationException the parser configuration exception
* @throws TransformerException the transformer exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws GamaHeadlessException the gama headless exception
* @param arg
* the arg
* @throws ParserConfigurationException
* the parser configuration exception
* @throws TransformerException
* the transformer exception
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws GamaHeadlessException
* the gama headless exception
*/
public void buildXML(final List<String> arg)
throws ParserConfigurationException, TransformerException, IOException, GamaHeadlessException {
Expand All @@ -421,13 +426,10 @@ public void buildXML(final List<String> arg)
}
}

if ( selectedJob.size() == 0) {
if (selectedJob.size() == 0) {
DEBUG.ON();
DEBUG.ERR(
"\n=== ERROR ==="
+ "\n\tGAMA is about to generate an empty XML file."
+ "\n\tIf you want to run a Batch experiment, please check the \"-batch\" flag."
);
DEBUG.ERR("\n=== ERROR ===" + "\n\tGAMA is about to generate an empty XML file."
+ "\n\tIf you want to run a Batch experiment, please check the \"-batch\" flag.");
System.exit(-1);
} else {
final Document dd = ExperimentationPlanFactory.buildXmlDocument(selectedJob);
Expand All @@ -445,12 +447,18 @@ public void buildXML(final List<String> arg)
/**
* Builds the XML for model library.
*
* @param modelPaths the model paths
* @param outputPath the output path
* @throws ParserConfigurationException the parser configuration exception
* @throws TransformerException the transformer exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws GamaHeadlessException the gama headless exception
* @param modelPaths
* the model paths
* @param outputPath
* the output path
* @throws ParserConfigurationException
* the parser configuration exception
* @throws TransformerException
* the transformer exception
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws GamaHeadlessException
* the gama headless exception
*/
public void buildXMLForModelLibrary(final ArrayList<File> modelPaths, final String outputPath)
throws ParserConfigurationException, TransformerException, IOException, GamaHeadlessException {
Expand All @@ -476,12 +484,14 @@ public void buildXMLForModelLibrary(final ArrayList<File> modelPaths, final Stri
/**
* Run XML for model library.
*
* @param xmlPath the xml path
* @throws FileNotFoundException the file not found exception
* @param xmlPath
* the xml path
* @throws FileNotFoundException
* the file not found exception
*/
public void runXMLForModelLibrary(final String xmlPath) throws FileNotFoundException {

processorQueue = new LocalSimulationRuntime();
// processorQueue = new ExecutorBasedSimulationRuntime();
final Reader in = new Reader(xmlPath);
in.parseXmlFile();
this.buildAndRunSimulation(in.getSimulation());
Expand All @@ -499,12 +509,15 @@ public void runXMLForModelLibrary(final String xmlPath) throws FileNotFoundExcep
/**
* Run simulation.
*
* @param args the args
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
* @param args
* the args
* @throws FileNotFoundException
* the file not found exception
* @throws InterruptedException
* the interrupted exception
*/
public void runSimulation(final List<String> args) throws FileNotFoundException, InterruptedException {
processorQueue = new LocalSimulationRuntime(this.numberOfThread);
// processorQueue.setNumberOfThreads(this.numberOfThread);

Reader in = null;
if (this.verbose && !this.tunnelingMode) { DEBUG.ON(); }
Expand All @@ -525,7 +538,8 @@ public void runSimulation(final List<String> args) throws FileNotFoundException,
/**
* Builds the and run simulation.
*
* @param sims the sims
* @param sims
* the sims
*/
public void buildAndRunSimulation(final Collection<ExperimentJob> sims) {
final Iterator<ExperimentJob> it = sims.iterator();
Expand Down Expand Up @@ -605,22 +619,27 @@ public void runGamlSimulation(final List<String> args) throws IOException, GamaH
Globals.OUTPUT_PATH = args.get(args.size() - 3);

selectedJob.setBufferedWriter(new XMLWriter(Globals.OUTPUT_PATH + "/" + Globals.OUTPUT_FILENAME + ".xml"));

int numberOfThreads;
if (args.contains(THREAD_PARAMETER)) {
this.numberOfThread = Integer.parseInt(after(args, THREAD_PARAMETER));
numberOfThreads = Integer.parseInt(after(args, THREAD_PARAMETER));
} else {
numberOfThread = SimulationRuntime.UNDEFINED_QUEUE_SIZE;
numberOfThreads = SimulationRuntime.UNDEFINED_QUEUE_SIZE;
}
processorQueue = new LocalSimulationRuntime(this.numberOfThread);
processorQueue.setNumberOfThreads(numberOfThreads);

processorQueue.pushSimulation(selectedJob);

System.exit(0);
}


public void createSocketServer() throws UnknownHostException {
socketServer = new GamaWebSocketServer(this.socket,this);
/**
* Creates the socket server.
*
* @throws UnknownHostException
* the unknown host exception
*/
public void createSocketServer() throws UnknownHostException {
socketServer = new GamaWebSocketServer(this.socket, this);
socketServer.endpoints.put("/compile", new CompileEndPoint());
socketServer.endpoints.put("/launch", new LaunchEndPoint());
socketServer.start();
Expand All @@ -632,12 +651,12 @@ public void createSocketServer() throws UnknownHostException {
while (true) {
String in = sysin.readLine();
socketServer.broadcast(in);
if (in.equals("exit")) {
if ("exit".equals(in)) {
socketServer.stop(1000);
break;
}
}
}catch(Exception ex) {
} catch (Exception ex) {
ex.printStackTrace();
}
}
Expand Down

0 comments on commit a667e60

Please sign in to comment.