package cl.niclabs.skandium.monitor;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.math.plot.Plot2DPanel;
/**
* The Concurrency Vs Time Monitor prints the number of active threads as
* a function of the time.
*/
class CvTMonitor extends AbstractMonitor {
private static final long serialVersionUID = 1L;
private boolean showCond;
MonitorController monitorc;
CvTMonitor(MonitorController monitorc) {
this.setTitle("Concurrency vs Time");
this.monitorc = monitorc;
this.setSize(400, 300);
}
/**
* Builds its view from the data collected during the execution of
* a program.
*/
@Override
public void build() throws IOException {
showCond = monitorc.getShowCond();
String line;
StringTokenizer token;
File file = monitorc.getLogFile();
BufferedReader log = new BufferedReader(new FileReader(file));
ArrayList<Double> time = new ArrayList<Double>();
ArrayList<Double> concurrency = new ArrayList<Double>();
double threads = 0;
while((line = log.readLine()) != null) {
token = new StringTokenizer(line);
double currentTime = Double.parseDouble(token.nextToken());
token.nextToken();
String when = token.nextToken(), where = token.nextToken();
if(where.compareTo("SKELETON") == 0
&& token.nextToken().compareTo("Seq") != 0)
continue;
if(where.compareTo("CONDITION") == 0 && !showCond)
continue;
if(when.compareTo("BEFORE") == 0) {
time.add(currentTime);
concurrency.add(++threads);
}
else {
time.add(currentTime);
concurrency.add(--threads);
}
}
log.close();
int size = time.size();
double[] X = new double[size], Y = new double[size];
for(int i = 0; i < size; i++) {
X[i] = time.get(i);
Y[i] = concurrency.get(i);
}
Plot2DPanel plot = new Plot2DPanel();
plot.addStaircasePlot("Concurrency vs Time", X, Y);
plot.setAxisLabels("Time", "Active threads");
this.setContentPane(plot);
this.setVisible(true);
}
}