Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1696 | - | 1 | package dongfang.mkt.main; |
2 | |||
3 | import java.io.BufferedReader; |
||
4 | import java.io.FileOutputStream; |
||
5 | import java.io.IOException; |
||
6 | import java.io.InputStreamReader; |
||
7 | import java.io.OutputStream; |
||
8 | import java.io.OutputStreamWriter; |
||
9 | import java.io.PrintStream; |
||
10 | import java.io.Writer; |
||
11 | import java.util.ArrayList; |
||
12 | import java.util.Date; |
||
13 | import java.util.LinkedList; |
||
14 | import java.util.List; |
||
15 | |||
16 | import dongfang.mkt.comm.FrameQueue; |
||
17 | import dongfang.mkt.comm.MKConnection; |
||
18 | import dongfang.mkt.comm.serial.RXTXSerialPort; |
||
19 | import dongfang.mkt.frames.Frame; |
||
20 | import dongfang.mkt.frames.ProfilerLabelRequestFrame; |
||
21 | import dongfang.mkt.frames.ProfilerLabelResponseFrame; |
||
22 | import dongfang.mkt.frames.ProfilerRequestFrame; |
||
23 | import dongfang.mkt.frames.ProfilerResponseFrame; |
||
24 | |||
25 | public class MKProfiler { |
||
26 | |||
27 | private final static int NUM_ENTRIES = 16; |
||
28 | |||
29 | private static final PrintStream STDERR = System.out; |
||
30 | // private static final FrameFactory ff = MKVersion.getFrameFactory(null); |
||
31 | |||
32 | interface ProfilerLogger { |
||
33 | void setLabel(int index, String label); |
||
34 | void start(String timestamp) throws IOException; |
||
35 | void log(ProfilerResponseFrame f, long timestamp) throws IOException; |
||
36 | void end() throws IOException; |
||
37 | } |
||
38 | |||
39 | static class ConsoleProfilerLogger implements ProfilerLogger { |
||
40 | String[] labels = new String[NUM_ENTRIES]; |
||
41 | |||
42 | public void setLabel(int channel, String label) { |
||
43 | labels[channel] = label; |
||
44 | } |
||
45 | |||
46 | public void start(String logId) {} |
||
47 | |||
48 | public void log(ProfilerResponseFrame f, long timestamp) { |
||
49 | if (f == null) { |
||
50 | System.out.println("Oops, null response frame."); |
||
51 | return; |
||
52 | } |
||
53 | int l = f.getActivities()==null ? 0 : f.getActivities().length; |
||
54 | for (int i = 0; i < l; i++) { |
||
55 | String label = labels[i] == null ? ("Activity " + i) : labels[i]; |
||
56 | System.out.println(label + ":\t" + f.asFraction(i) * 100.0); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | public void end() {} |
||
61 | } |
||
62 | |||
63 | static class XMLProfilerLogger implements ProfilerLogger { |
||
64 | String[] labels = new String[NUM_ENTRIES]; |
||
65 | Writer w; |
||
66 | String encoding; |
||
67 | |||
68 | XMLProfilerLogger(OutputStream out, String encoding) throws IOException { |
||
69 | w = new OutputStreamWriter(out, encoding); |
||
70 | this.encoding = encoding; |
||
71 | } |
||
72 | |||
73 | public void setLabel(int channel, String label) { |
||
74 | labels[channel] = label.trim(); |
||
75 | } |
||
76 | |||
77 | public void start(String logId) throws IOException { |
||
78 | w.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"); |
||
79 | w.write("<mk-profilerdata time=\"" + logId + "\">\n"); |
||
80 | } |
||
81 | |||
82 | public void log(ProfilerResponseFrame f, long timestamp) |
||
83 | throws IOException { |
||
84 | if (f==null) return; |
||
85 | w.write(" <dataset timestamp=\"" + timestamp + "\">\n"); |
||
86 | for (int i = 0; i < NUM_ENTRIES; i++) { |
||
87 | w.write(" <data offset=\"" + i + "\" label=\"" + labels[i] |
||
88 | + "\" value=\"" + f.asFraction(i)*100.0 + "\"/>\n"); |
||
89 | } |
||
90 | w.write(" </dataset>\n"); |
||
91 | } |
||
92 | |||
93 | public void end() throws IOException { |
||
94 | w.write("</mk-profilerdata>\n"); |
||
95 | w.flush(); |
||
96 | w.close(); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | static class CompositeProfilerLogger implements ProfilerLogger { |
||
101 | List<ProfilerLogger> loggers = new ArrayList<ProfilerLogger>(2); |
||
102 | |||
103 | public void setLabel(int index, String label) { |
||
104 | for (ProfilerLogger l : loggers) |
||
105 | l.setLabel(index, label); |
||
106 | } |
||
107 | |||
108 | public void log(ProfilerResponseFrame f, long timestamp) throws IOException { |
||
109 | for (ProfilerLogger l : loggers) |
||
110 | l.log(f, timestamp); |
||
111 | } |
||
112 | |||
113 | public void start(String logId) throws IOException { |
||
114 | for (ProfilerLogger l : loggers) |
||
115 | l.start(logId); |
||
116 | } |
||
117 | |||
118 | public void end() throws IOException { |
||
119 | for (ProfilerLogger l : loggers) |
||
120 | l.end(); |
||
121 | } |
||
122 | |||
123 | void add(ProfilerLogger l) { |
||
124 | loggers.add(l); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | private static ProfilerLogger createLogger(OutputStream out, String encoding) |
||
129 | throws IOException { |
||
130 | ProfilerLogger consoleLogger = new ConsoleProfilerLogger(); |
||
131 | XMLProfilerLogger xmlLogger = new XMLProfilerLogger(out, encoding); |
||
132 | CompositeProfilerLogger logger = new CompositeProfilerLogger(); |
||
133 | logger.add(consoleLogger); |
||
134 | logger.add(xmlLogger); |
||
135 | return logger; |
||
136 | } |
||
137 | |||
138 | private void prepareLoggers(final FrameQueue q, final ProfilerLogger logger) |
||
139 | throws IOException { |
||
140 | new Thread() { |
||
141 | public void run() { |
||
142 | } |
||
143 | }.start(); |
||
144 | } |
||
145 | |||
146 | private void profile(final FrameQueue q, final ProfilerLogger logger, final int interval) throws IOException { |
||
147 | LinkedList<Integer> missing = new LinkedList<Integer>(); |
||
148 | |||
149 | for (int i = 0; i < NUM_ENTRIES; i++) { |
||
150 | missing.add(i); |
||
151 | } |
||
152 | |||
153 | int tries = 0; |
||
154 | while (!missing.isEmpty() && tries < 300) { |
||
155 | int i = missing.get(0); |
||
156 | tries++; |
||
157 | ProfilerLabelRequestFrame f2 = //ff.createAnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i); |
||
158 | new ProfilerLabelRequestFrame(Frame.FC_ADDRESS, i); |
||
159 | try { |
||
160 | q.sendRequest(f2); |
||
161 | ProfilerLabelResponseFrame rf = (ProfilerLabelResponseFrame) q.getResponseFor(f2, 5000); |
||
162 | if (rf != null) { |
||
163 | logger.setLabel(i, rf.getLabelAsString()); |
||
164 | missing.remove(0); |
||
165 | } else { |
||
166 | String unknown = "activity " + i; |
||
167 | logger.setLabel(i, unknown); |
||
168 | missing.add(i); |
||
169 | } |
||
170 | } catch (IOException ex) { |
||
171 | } |
||
172 | } |
||
173 | |||
174 | ProfilerRequestFrame f = // ff.createDebugRequestFrame(Frame.FC_ADDRESS); |
||
175 | new ProfilerRequestFrame(Frame.FC_ADDRESS); |
||
176 | f.setAutoSendInterval(interval); |
||
177 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
||
178 | q.sendRequest(f); |
||
179 | Long firsttimestamp = null; |
||
180 | while (!in.ready()) |
||
181 | try { |
||
182 | // q.output(f); |
||
183 | ProfilerResponseFrame rf = (ProfilerResponseFrame) q.getResponseFor(f, 1000); |
||
184 | // System.out.println(rf); |
||
185 | long timestamp = System.currentTimeMillis(); |
||
186 | |||
187 | if (firsttimestamp == null) { |
||
188 | firsttimestamp = timestamp; |
||
189 | timestamp = 0; |
||
190 | } else { |
||
191 | timestamp -= firsttimestamp; |
||
192 | } |
||
193 | logger.log(rf, timestamp); |
||
194 | } catch (IOException ex) { |
||
195 | STDERR.println(ex); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | public static void main(String[] args) throws IOException { |
||
200 | MKProfiler test = new MKProfiler(); |
||
201 | |||
202 | MKConnection port = new RXTXSerialPort(); |
||
203 | port.init(null); |
||
204 | |||
205 | FrameQueue q = new FrameQueue(port); |
||
206 | String encoding = "iso-8859-1"; |
||
207 | |||
208 | OutputStream fout = new FileOutputStream("profile.xml"); |
||
209 | |||
210 | ProfilerLogger logger = createLogger(fout, encoding); |
||
211 | logger.start(new Date().toGMTString()); |
||
212 | test.profile(q, logger, 10); |
||
213 | logger.end(); |
||
214 | fout.close(); |
||
215 | } |
||
216 | } |