Debian Codegraph @

src/com/google/javascript/jscomp/PerformanceTracker.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Maps;
23 import com.google.javascript.jscomp.CompilerOptions.TracerMode;
24 import com.google.javascript.rhino.Node;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.PrintStream;
30 import java.util.ArrayDeque;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.Deque;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import java.util.zip.GZIPOutputStream;
39
40 /**
41 */
42 public class PerformanceTracker {
43
44 private final Node jsRoot;
45 private final boolean trackSize;
46 private final boolean trackGzSize;
47
48 // Keeps track of AST changes and computes code size estimation
49 // if there is any.
50 private final CodeChangeHandler codeChange = new CodeChangeHandler();
51
52 private int initCodeSize = 0;
53 private int initGzCodeSize = 0;
54
55 private int runtime = 0;
56 private int runs = 0;
57 private int changes = 0;
58 private int loopRuns = 0;
59 private int loopChanges = 0;
60 private int codeSize = 0;
61 private int gzCodeSize = 0;
62 private int diff = 0;
63 private int gzDiff = 0;
64
65 private Deque<Stats> currentPass = new ArrayDeque<Stats>();
66
67 /** Summary stats by pass name. */
68 private final Map<String, Stats> summary = Maps.newHashMap();
69
70 // To share with the rest of the program
71 private ImmutableMap<String, Stats> summaryCopy = null;
72
73 /** Stats for each run of a compiler pass. */
74 private final List<Stats> log = Lists.newArrayList();
75
76 /** For each pass, keep track of the runtime, the size changes, etc */
77 public static class Stats {
78 Stats(String pass, boolean iot) {
79 this.pass = pass;
80 this.isOneTime = iot;
81 }
82 public final String pass;
83 public final boolean isOneTime;
84 public long runtime = 0;
85 public int runs = 0;
86 public int changes = 0;
87 public int diff = 0;
88 public int gzDiff = 0;
89 public int size = 0;
90 public int gzSize = 0;
91 }
92
93 PerformanceTracker(Node jsRoot, TracerMode mode) {
94 this.jsRoot = jsRoot;
95 switch (mode) {
96 case TIMING_ONLY:
97 this.trackSize = false;
98 this.trackGzSize = false;
99 break;
100
101 case RAW_SIZE:
102 this.trackSize = true;
103 this.trackGzSize = false;
104 break;
105
106 case ALL:
107 this.trackSize = true;
108 this.trackGzSize = true;
109 break;
110
111 case OFF:
112 default:
113 throw new UnsupportedOperationException();
114 }
115 }
116
117 CodeChangeHandler getCodeChangeHandler() {
118 return codeChange;
119 }
120
121 void recordPassStart(String passName, boolean isOneTime) {
122 currentPass.push(new Stats(passName, isOneTime));
123 codeChange.reset();
124 }
125
126 /**
127 * Record that a pass has stopped.
128 *
129 * @param passName Short name of the pass.
130 * @param result Execution time.
131 */
132 void recordPassStop(String passName, long result) {
133 Stats logStats = currentPass.pop();
134 if (!passName.equals(logStats.pass)) {
135 throw new RuntimeException(passName + " is not running.");
136 }
137
138 // After parsing, initialize codeSize and gzCodeSize
139 if (passName.equals(Compiler.PARSING_PASS_NAME) && trackSize) {
140 CodeSizeEstimatePrinter printer = new CodeSizeEstimatePrinter();
141 CodeGenerator.forCostEstimation(printer).add(jsRoot);
142 initCodeSize = codeSize = printer.calcSize();
143 if (this.trackGzSize) {
144 initGzCodeSize = gzCodeSize = printer.calcZippedSize();
145 }
146 }
147
148 // Populate log and summary
149 log.add(logStats);
150 Stats summaryStats = summary.get(passName);
151 if (summaryStats == null) {
152 summaryStats = new Stats(passName, logStats.isOneTime);
153 summary.put(passName, summaryStats);
154 }
155
156 // Update fields that aren't related to code size
157 logStats.runtime = result;
158 logStats.runs = 1;
159 summaryStats.runtime += result;
160 summaryStats.runs += 1;
161 if (codeChange.hasCodeChanged()) {
162 logStats.changes = 1;
163 summaryStats.changes += 1;
164 }
165
166 // Update fields related to code size
167 if (codeChange.hasCodeChanged() && trackSize) {
168 int newSize = 0;
169 CodeSizeEstimatePrinter printer = new CodeSizeEstimatePrinter();
170 CodeGenerator.forCostEstimation(printer).add(jsRoot);
171 if (trackSize) {
172 newSize = printer.calcSize();
173 logStats.diff = codeSize - newSize;
174 summaryStats.diff += logStats.diff;
175 codeSize = summaryStats.size = logStats.size = newSize;
176 }
177 if (trackGzSize) {
178 newSize = printer.calcZippedSize();
179 logStats.gzDiff = gzCodeSize - newSize;
180 summaryStats.gzDiff += logStats.gzDiff;
181 gzCodeSize = summaryStats.gzSize = logStats.gzSize = newSize;
182 }
183 }
184 }
185
186 public int getRuntime() {
187 calcTotalStats();
188 return runtime;
189 }
190
191 public int getSize() {
192 calcTotalStats();
193 return codeSize;
194 }
195
196 public int getGzSize() {
197 calcTotalStats();
198 return gzCodeSize;
199 }
200
201 public ImmutableMap<String, Stats> getStats() {
202 calcTotalStats();
203 return summaryCopy;
204 }
205
206 class CmpEntries implements Comparator<Entry<String, Stats>> {
207 @Override
208 public int compare(Entry<String, Stats> e1, Entry<String, Stats> e2) {
209 return (int) (e1.getValue().runtime - e2.getValue().runtime);
210 }
211 }
212
213 private void calcTotalStats() {
214 // This method only does work the first time it's called
215 if (summaryCopy != null) {
216 return;
217 }
218 summaryCopy = ImmutableMap.copyOf(summary);
219 for (Entry<String, Stats> entry : summary.entrySet()) {
220 Stats stats = entry.getValue();
221 runtime += stats.runtime;
222 runs += stats.runs;
223 changes += stats.changes;
224 if (!stats.isOneTime) {
225 loopRuns += stats.runs;
226 loopChanges += stats.changes;
227 }
228 diff += stats.diff;
229 gzDiff += stats.gzDiff;
230 }
231 Preconditions.checkState(!trackSize || initCodeSize == diff + codeSize);
232 Preconditions.checkState(!trackGzSize ||
233 initGzCodeSize == gzDiff + gzCodeSize);
234 }
235
236 public void outputTracerReport(PrintStream pstr) {
237 JvmMetrics.maybeWriteJvmMetrics(pstr, "verbose:pretty:all");
238 OutputStreamWriter output = new OutputStreamWriter(pstr);
239 try {
240 calcTotalStats();
241
242 ArrayList<Entry<String, Stats>> a = new ArrayList<Entry<String, Stats>>();
243 for (Entry<String, Stats> entry : summary.entrySet()) {
244 a.add(entry);
245 }
246 Collections.sort(a, new CmpEntries());
247
248 output.write("Summary:\n" +
249 "pass,runtime,runs,changingRuns,reduction,gzReduction\n");
250 for (Entry<String, Stats> entry : a) {
251 String key = entry.getKey();
252 Stats stats = entry.getValue();
253 output.write(key + "," +
254 String.valueOf(stats.runtime) + "," +
255 String.valueOf(stats.runs) + "," +
256 String.valueOf(stats.changes) + "," +
257 String.valueOf(stats.diff) + "," +
258 String.valueOf(stats.gzDiff) + "\n");
259 }
260 output.write("\nTOTAL:" +
261 "\nRuntime(ms): " + String.valueOf(runtime) +
262 "\n#Runs: " + String.valueOf(runs) +
263 "\n#Changing runs: " + String.valueOf(changes) +
264 "\n#Loopable runs: " + String.valueOf(loopRuns) +
265 "\n#Changing loopable runs: " + String.valueOf(loopChanges) +
266 "\nReduction(bytes): " + String.valueOf(diff) +
267 "\nGzReduction(bytes): " + String.valueOf(gzDiff) +
268 "\nSize(bytes): " + String.valueOf(codeSize) +
269 "\nGzSize(bytes): " + String.valueOf(gzCodeSize) + "\n\n");
270
271 output.write("Log:\n" +
272 "pass,runtime,runs,changingRuns,reduction,gzReduction,size,gzSize\n");
273 for (Stats stats : log) {
274 output.write(stats.pass + "," +
275 String.valueOf(stats.runtime) + "," +
276 String.valueOf(stats.runs) + "," +
277 String.valueOf(stats.changes) + "," +
278 String.valueOf(stats.diff) + "," +
279 String.valueOf(stats.gzDiff) + "," +
280 String.valueOf(stats.size) + "," +
281 String.valueOf(stats.gzSize) + "\n");
282 }
283 output.write("\n");
284 output.close();
285 } catch (IOException e) {
286 e.printStackTrace();
287 }
288 }
289
290 /**
291 * Purely use to get a code size estimate and not generate any code at all.
292 */
293 private final class CodeSizeEstimatePrinter extends CodeConsumer {
294 private int size = 0;
295 private char lastChar = '\0';
296 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
297 private final GZIPOutputStream stream;
298
299 private CodeSizeEstimatePrinter() {
300 try {
301 stream = new GZIPOutputStream(output);
302 } catch (IOException e) {
303 throw new RuntimeException(e);
304 }
305 }
306
307 @Override
308 void append(String str) {
309 int len = str.length();
310 if (len > 0) {
311 size += len;
312 lastChar = str.charAt(len - 1);
313 if (trackGzSize) {
314 try {
315 stream.write(str.getBytes());
316 } catch (IOException e) {
317 throw new RuntimeException(e);
318 }
319 }
320 }
321 }
322
323 @Override
324 char getLastChar() {
325 return lastChar;
326 }
327
328 private int calcSize() {
329 return size;
330 }
331
332 // Called iff trackGzSize is true
333 private int calcZippedSize() {
334 try {
335 stream.finish();
336 stream.flush();
337 stream.close();
338 return output.size();
339 } catch (IOException e) {
340 throw new RuntimeException(e);
341 }
342 }
343 }
344 }