KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > Timer


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot;
28 import soot.options.*;
29
30 import soot.util.*;
31 import java.util.*;
32
33 /** Utility class providing a timer. Used for profiling various
34  * phases of Sootification. */

35 public class Timer
36 {
37     private long duration;
38     private long startTime;
39     private boolean hasStarted;
40         
41     private String JavaDoc name;
42         
43     
44     /** Creates a new timer with the given name. */
45     public Timer(String JavaDoc name)
46     {
47         this.name = name;
48         duration = 0;
49     }
50     
51     /** Creates a new timer. */
52     public Timer()
53     {
54         this("unnamed");
55     }
56     
57     /** Starts the given timer. */
58     public void start()
59     {
60         // Subtract garbage collection time
61
if(!G.v().Timer_isGarbageCollecting && Options.v() != null && Options.v().subtract_gc() && ((G.v().Timer_count++ % 4) == 0))
62             {
63                 // garbage collects only every 4 calls to avoid round off errors
64

65                 G.v().Timer_isGarbageCollecting = true;
66             
67                 G.v().Timer_forcedGarbageCollectionTimer.start();
68                 
69                 // Stop all outstanding timers
70
{
71                     Iterator timerIt = G.v().Timer_outstandingTimers.iterator();
72                     
73                     while(timerIt.hasNext())
74                     {
75                         Timer t = (Timer) timerIt.next();
76                         
77                         t.end();
78                     }
79                 }
80                 
81                 System.gc();
82         
83                 // Start all outstanding timers
84
{
85                     Iterator timerIt = G.v().Timer_outstandingTimers.iterator();
86                     
87                     while(timerIt.hasNext())
88                     {
89                         Timer t = (Timer) timerIt.next();
90                         
91                         t.start();
92                     }
93                 }
94                 
95                 G.v().Timer_forcedGarbageCollectionTimer.end();
96                 
97                 G.v().Timer_isGarbageCollecting = false;
98             }
99                         
100         
101         startTime = System.currentTimeMillis();
102         
103         if(hasStarted)
104             throw new RuntimeException JavaDoc("timer " + name + " has already been started!");
105         else
106             hasStarted = true;
107         
108         
109         if(!G.v().Timer_isGarbageCollecting)
110         {
111             G.v().Timer_outstandingTimers.add(this);
112         }
113             
114     }
115
116     /** Returns the name of the current timer. */
117     public String JavaDoc toString()
118     {
119         return name;
120     }
121     
122     /** Stops the current timer. */
123     public void end()
124     {
125         if(!hasStarted)
126             throw new RuntimeException JavaDoc("timer " + name + " has not been started!");
127         else
128             hasStarted = false;
129         
130         duration += System.currentTimeMillis() - startTime;
131         
132         
133         if(!G.v().Timer_isGarbageCollecting)
134         {
135             G.v().Timer_outstandingTimers.remove(this);
136         }
137     }
138
139     /** Returns the sum of the intervals start()-end() of the current timer. */
140     public long getTime()
141     {
142         return duration;
143     }
144 }
145
146
147
148
149
Popular Tags