Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 1695 → Rev 1696

/dongfang_FC_rewrite_tool/src/dongfang/mkt/main/MKProfiler.java
0,0 → 1,216
package dongfang.mkt.main;
 
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
 
import dongfang.mkt.comm.FrameQueue;
import dongfang.mkt.comm.MKConnection;
import dongfang.mkt.comm.serial.RXTXSerialPort;
import dongfang.mkt.frames.Frame;
import dongfang.mkt.frames.ProfilerLabelRequestFrame;
import dongfang.mkt.frames.ProfilerLabelResponseFrame;
import dongfang.mkt.frames.ProfilerRequestFrame;
import dongfang.mkt.frames.ProfilerResponseFrame;
 
public class MKProfiler {
private final static int NUM_ENTRIES = 16;
private static final PrintStream STDERR = System.out;
// private static final FrameFactory ff = MKVersion.getFrameFactory(null);
 
interface ProfilerLogger {
void setLabel(int index, String label);
void start(String timestamp) throws IOException;
void log(ProfilerResponseFrame f, long timestamp) throws IOException;
void end() throws IOException;
}
 
static class ConsoleProfilerLogger implements ProfilerLogger {
String[] labels = new String[NUM_ENTRIES];
 
public void setLabel(int channel, String label) {
labels[channel] = label;
}
public void start(String logId) {}
 
public void log(ProfilerResponseFrame f, long timestamp) {
if (f == null) {
System.out.println("Oops, null response frame.");
return;
}
int l = f.getActivities()==null ? 0 : f.getActivities().length;
for (int i = 0; i < l; i++) {
String label = labels[i] == null ? ("Activity " + i) : labels[i];
System.out.println(label + ":\t" + f.asFraction(i) * 100.0);
}
}
 
public void end() {}
}
 
static class XMLProfilerLogger implements ProfilerLogger {
String[] labels = new String[NUM_ENTRIES];
Writer w;
String encoding;
 
XMLProfilerLogger(OutputStream out, String encoding) throws IOException {
w = new OutputStreamWriter(out, encoding);
this.encoding = encoding;
}
 
public void setLabel(int channel, String label) {
labels[channel] = label.trim();
}
 
public void start(String logId) throws IOException {
w.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
w.write("<mk-profilerdata time=\"" + logId + "\">\n");
}
 
public void log(ProfilerResponseFrame f, long timestamp)
throws IOException {
if (f==null) return;
w.write(" <dataset timestamp=\"" + timestamp + "\">\n");
for (int i = 0; i < NUM_ENTRIES; i++) {
w.write(" <data offset=\"" + i + "\" label=\"" + labels[i]
+ "\" value=\"" + f.asFraction(i)*100.0 + "\"/>\n");
}
w.write(" </dataset>\n");
}
 
public void end() throws IOException {
w.write("</mk-profilerdata>\n");
w.flush();
w.close();
}
}
 
static class CompositeProfilerLogger implements ProfilerLogger {
List<ProfilerLogger> loggers = new ArrayList<ProfilerLogger>(2);
 
public void setLabel(int index, String label) {
for (ProfilerLogger l : loggers)
l.setLabel(index, label);
}
 
public void log(ProfilerResponseFrame f, long timestamp) throws IOException {
for (ProfilerLogger l : loggers)
l.log(f, timestamp);
}
 
public void start(String logId) throws IOException {
for (ProfilerLogger l : loggers)
l.start(logId);
}
 
public void end() throws IOException {
for (ProfilerLogger l : loggers)
l.end();
}
 
void add(ProfilerLogger l) {
loggers.add(l);
}
}
 
private static ProfilerLogger createLogger(OutputStream out, String encoding)
throws IOException {
ProfilerLogger consoleLogger = new ConsoleProfilerLogger();
XMLProfilerLogger xmlLogger = new XMLProfilerLogger(out, encoding);
CompositeProfilerLogger logger = new CompositeProfilerLogger();
logger.add(consoleLogger);
logger.add(xmlLogger);
return logger;
}
 
private void prepareLoggers(final FrameQueue q, final ProfilerLogger logger)
throws IOException {
new Thread() {
public void run() {
}
}.start();
}
 
private void profile(final FrameQueue q, final ProfilerLogger logger, final int interval) throws IOException {
LinkedList<Integer> missing = new LinkedList<Integer>();
 
for (int i = 0; i < NUM_ENTRIES; i++) {
missing.add(i);
}
 
int tries = 0;
while (!missing.isEmpty() && tries < 300) {
int i = missing.get(0);
tries++;
ProfilerLabelRequestFrame f2 = //ff.createAnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i);
new ProfilerLabelRequestFrame(Frame.FC_ADDRESS, i);
try {
q.sendRequest(f2);
ProfilerLabelResponseFrame rf = (ProfilerLabelResponseFrame) q.getResponseFor(f2, 5000);
if (rf != null) {
logger.setLabel(i, rf.getLabelAsString());
missing.remove(0);
} else {
String unknown = "activity " + i;
logger.setLabel(i, unknown);
missing.add(i);
}
} catch (IOException ex) {
}
}
 
ProfilerRequestFrame f = // ff.createDebugRequestFrame(Frame.FC_ADDRESS);
new ProfilerRequestFrame(Frame.FC_ADDRESS);
f.setAutoSendInterval(interval);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
q.sendRequest(f);
Long firsttimestamp = null;
while (!in.ready())
try {
// q.output(f);
ProfilerResponseFrame rf = (ProfilerResponseFrame) q.getResponseFor(f, 1000);
// System.out.println(rf);
long timestamp = System.currentTimeMillis();
 
if (firsttimestamp == null) {
firsttimestamp = timestamp;
timestamp = 0;
} else {
timestamp -= firsttimestamp;
}
logger.log(rf, timestamp);
} catch (IOException ex) {
STDERR.println(ex);
}
}
 
public static void main(String[] args) throws IOException {
MKProfiler test = new MKProfiler();
 
MKConnection port = new RXTXSerialPort();
port.init(null);
 
FrameQueue q = new FrameQueue(port);
String encoding = "iso-8859-1";
 
OutputStream fout = new FileOutputStream("profile.xml");
 
ProfilerLogger logger = createLogger(fout, encoding);
logger.start(new Date().toGMTString());
test.profile(q, logger, 10);
logger.end();
fout.close();
}
}