Skip to content

Commit

Permalink
timeout for dot
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmera committed May 7, 2014
1 parent 468bb08 commit 2dd417c
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 28 deletions.
1 change: 1 addition & 0 deletions g2html.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/test_dir" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
9 changes: 9 additions & 0 deletions src/g2html/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ public class Config {
// JewelCli definitions for the configuration
@CommandLineInterface(application = "java -jar g2html.jar")
public interface G2HtmlArgs {
@Option (shortName = "n",longName = "num-threads", defaultValue = "2", exactly = 1,description = "Number of worker threads.")
Integer getNumThreads();

@Option (shortName = "d",longName = "dot-path", defaultValue = "dot", exactly = 1,description = "Path to the dot binary.")
String getDotPath();

@Option (longName = "dot-alternative-path", defaultValue = "sfdp", exactly = 1,description = "Path to an alternative dot binary.")
String getAlternativeDotPath();

@Option (longName = "dot-timeout", defaultValue = "2000", exactly = 1,description = "Timeout for graph processing (0 means no timeout).")
Integer getDotTimeout();

@Option (shortName = "c",longName = "cfg-dir", defaultValue = "cfgs", exactly = 1, description = "Path to 'exp.cfgdot' output directory.")
String getCfgDir();

Expand Down
4 changes: 2 additions & 2 deletions src/g2html/Glob.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import javax.xml.stream.XMLStreamWriter;
import java.util.logging.Logger;

public class Glob {
final public class Glob {

// parsing a global node has the effect that the node is copied to the globals file
static public void parseGlobNode(XMLStreamReader parser, XMLStreamWriter globals)
Expand All @@ -15,7 +15,7 @@ static public void parseGlobNode(XMLStreamReader parser, XMLStreamWriter globals

while(readcc.hasNext()){
if (readcc.getEventType()== XMLStreamConstants.END_ELEMENT &&
readcc.getLocalName()=="glob"){
readcc.getLocalName().equals("glob")){
break;
}
readcc.next();
Expand Down
3 changes: 2 additions & 1 deletion src/g2html/Loc.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.logging.Logger;
import java.util.regex.Pattern;

public class Loc {
final public class Loc {

// parsing a local node means copying the value into its own file
// but also remembering some details (reachability, line data, file path)
Expand All @@ -30,6 +30,7 @@ static public void parseLocNode(XMLStreamReader parser, Result res, ResultStats
// open a stream for the node file
File xmlOut = res.getNodeFile(id);
XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
XMLStreamWriter xmlOutStream = factory.createXMLStreamWriter(new BufferedOutputStream(new FileOutputStream(xmlOut)));

// write the header
Expand Down
6 changes: 4 additions & 2 deletions src/g2html/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
public static void main(String[] args) {
// Load configuration (default settings and/or from file)
Config.load(args);

// Start
long startTime = System.currentTimeMillis();
System.out.println("Create html files ...");
Log.println("Create html files ...");

// Generate files
Result res = null;
Expand All @@ -43,7 +45,7 @@ public static void main(String[] args) {
Log.println("Done parsing xml.");

// Thread pool for file processing tasks
ExecutorService executorService = Executors.newFixedThreadPool(6);
ExecutorService executorService = Executors.newFixedThreadPool(Config.conf.getNumThreads());

// Process found files/functions
for(String path : stats.allFiles()){
Expand Down
63 changes: 56 additions & 7 deletions src/g2html/ProcessDotFileToSvg.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

class Worker extends Thread {
final Process process;
Integer exit;
Worker(Process process) {
this.process = process;
}
public void run() {
try {
exit = process.waitFor();
} catch (InterruptedException ignore) {
}
}
}

public class ProcessDotFileToSvg implements Runnable {
private File to;
Expand All @@ -19,8 +34,7 @@ static boolean testDotPath(String dot){
try {
Process p = Runtime.getRuntime().exec(dot + " -V");
return p.waitFor()==0;
} catch (IOException _) {
} catch (InterruptedException _) {
} catch (IOException | InterruptedException ignored) {
}
return false;
}
Expand All @@ -29,15 +43,50 @@ static boolean testDotPath(String dot){
@Override
public void run() {
Log.printf("Starting:%s.\n", from.getPath());
String myCommand = Config.conf.getDotPath()+" "+from.getAbsolutePath()+" -Tsvg -o "+to.getAbsolutePath();
try {
String myCommand = Config.conf.getDotPath()+" "+from.getAbsolutePath()+" -Tsvg -o "+to.getAbsolutePath();
Log.printf("Executing: '%s'\n",myCommand);
Runtime.getRuntime().exec(myCommand).waitFor();
} catch (IOException e) {
Worker worker = new Worker(Runtime.getRuntime().exec(myCommand));
worker.start();
try {
worker.join(Config.conf.getDotTimeout());
if (!worker.isAlive()) {
Log.printf("Finished:%s.\n", from.getPath());
return;
}
} catch(InterruptedException ex) {
worker.interrupt();
Thread.currentThread().interrupt();
throw ex;
} finally {
worker.process.destroy();
}
String myCommand2 = Config.conf.getAlternativeDotPath()+" "+from.getAbsolutePath()+" -Tsvg -o "+to.getAbsolutePath();
Log.printf("Executing: '%s'\n",myCommand2);
worker = new Worker(Runtime.getRuntime().exec(myCommand2));
worker.start();
try {
worker.join(Config.conf.getDotTimeout());
if (!worker.isAlive()) {
Log.printf("Finished:%s.\n", from.getPath());
return;
}
} catch(InterruptedException ex) {
worker.interrupt();
Thread.currentThread().interrupt();
throw ex;
} finally {
worker.process.destroy();
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
}
try {
Result.copyResource("missing.svg",to);
} catch (IOException e) {
e.printStackTrace();
System.exit(255);
}
Log.printf("Finished:%s.\n", from.getPath());
}
}

17 changes: 8 additions & 9 deletions src/g2html/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ public static boolean deleteDirectory(File directory) {
if(directory.exists()){
File[] files = directory.listFiles();
if(null!=files){
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
}
return(directory.delete());
Expand Down Expand Up @@ -73,7 +72,7 @@ private static void copyFromJar(String f,File t) throws IOException{

// generic file copy
public static void copyFile( File from, File to ) throws IOException {
System.out.printf("copy resource from '%s' to '%s'\n", from, to);
Log.printf("copy resource from '%s' to '%s'\n", from, to);
if ( !to.exists() ) { to.createNewFile(); }

try (
Expand Down
2 changes: 1 addition & 1 deletion src/g2html/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.net.URLEncoder;
import java.util.regex.Pattern;

public class Structure {
final public class Structure {
// read the structure from the xml and store it in the databases
public static void parseFileNode(XMLStreamReader parser, ResultStats resultStats)
throws XMLStreamException {
Expand Down
6 changes: 3 additions & 3 deletions src/g2html/Warning.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.logging.Logger;
import java.util.regex.Pattern;

public class Warning {
final public class Warning {
private static int counter = 1;

// parse a wwarning node
Expand Down Expand Up @@ -35,13 +35,13 @@ static public void parseWarningNode(XMLStreamReader parser, Result res, ResultSt

// until the warning tag closes
if (readcc.getEventType()== XMLStreamConstants.END_ELEMENT &&
readcc.getLocalName()=="warning"){
readcc.getLocalName().equals("warning")){
break;
}

// for each text element, store the id of the warning with the text location
if (readcc.getEventType()== XMLStreamConstants.START_ELEMENT &&
readcc.getLocalName()=="text"){
readcc.getLocalName().equals("text")){
String path = readcc.getAttributeValue("","file");
String line = readcc.getAttributeValue("","line");
String shortFile = path.replaceAll("/", "%2F");
Expand Down
4 changes: 2 additions & 2 deletions src/g2html/XMLStreamCC.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import javax.xml.stream.util.StreamReaderDelegate;

// Useful for copying portions of xml files to other files
public class XMLStreamCC extends StreamReaderDelegate {
final public class XMLStreamCC extends StreamReaderDelegate {
private final XMLStreamReader reader;
private final XMLStreamWriter writer;

Expand Down Expand Up @@ -49,7 +49,7 @@ public void copyTag() throws XMLStreamException {
writer.writeCData(reader.getText());
break;
case XMLStreamConstants.CHARACTERS:
writer.writeCharacters(reader.getText());
writer.writeCharacters(reader.getTextCharacters(),reader.getTextStart(),reader.getTextLength());
break;
case XMLStreamConstants.COMMENT:
writer.writeComment(reader.getText());
Expand Down
2 changes: 1 addition & 1 deletion src/g2html/XmlResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.*;
import java.util.logging.Logger;

public class XmlResult {
final public class XmlResult {
// Parse the complete xml result file
static public ResultStats parse(Result res) throws IOException {
ResultStats resultStats = new ResultStats();
Expand Down

0 comments on commit 2dd417c

Please sign in to comment.