]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/CompilationTimer.java
(refs #7250) Merging master, minor CHR bugfixes
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / compilation / CompilationTimer.java
1 package org.simantics.scl.compiler.compilation;
2
3 import java.util.ArrayList;
4
5 import gnu.trove.map.hash.TObjectLongHashMap;
6
7 public class CompilationTimer {
8     private long initialTime, previousTime;
9     private ArrayList<TimerEntry> entries = new ArrayList<TimerEntry>();
10     private static TObjectLongHashMap<String> GLOBAL_TIMES = new TObjectLongHashMap<String>();
11     
12     private static class TimerEntry {
13         public final String phaseName;
14         public final long time;
15         public final long cumulativeTime;
16         
17         public TimerEntry(String phaseName, long time, long cumulativeTime) {
18             this.phaseName = phaseName;
19             this.time = time;
20             this.cumulativeTime = cumulativeTime;
21         }        
22     }
23     
24     public CompilationTimer() {
25         initialTime = previousTime = System.nanoTime();
26     }
27     
28     public void phaseFinished(String phaseName) {
29         long time = System.nanoTime();
30         entries.add(new TimerEntry(phaseName, time-previousTime, time-initialTime));
31         previousTime = time;
32     }
33     
34     public void suspendTimer() {
35         long time = System.nanoTime();
36         initialTime -= time;
37         previousTime -= time;
38     }
39     
40     public void continueTimer() {
41         long time = System.nanoTime();
42         initialTime += time;
43         previousTime += time;
44     }
45     
46     public void report(String moduleName) {
47         synchronized(GLOBAL_TIMES) {
48             System.out.println(moduleName);
49             for(TimerEntry entry : entries) {
50                 long globalTime = GLOBAL_TIMES.adjustOrPutValue(entry.phaseName, entry.time, entry.time);
51                 System.out.println("    " + entry.phaseName + " " + entry.time*1e-6 
52                         + "ms (cumulative: " + entry.cumulativeTime*1e-6 + "ms, global: " + globalTime*1e-6 + ")");
53             }
54         }
55     }
56 }