KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > application > IDEIdleHelper


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.application;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.SWTException;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.swt.widgets.Listener;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.application.IWorkbenchConfigurer;
25 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
26 import org.eclipse.ui.internal.ide.Policy;
27
28 /**
29  * The idle helper detects when the system is idle in order to perform garbage
30  * collection in a way that minimizes impact on responsiveness of the UI.
31  * The algorithm for determining when to perform a garbage collection
32  * is as follows:
33  *
34  * - Never gc if there is a test harness present
35  * - Don't gc if background jobs are running
36  * - Don't gc if the keyboard or mouse have been active within IDLE_INTERVAL
37  * - Don't gc if there has been a GC within the minimum gc interval (system property PROP_GC_INTERVAL)
38  * - After a gc, don't gc again until (duration * GC_DELAY_MULTIPLIER) has elapsed.
39  * For example, if a GC takes 100ms and the multiplier is 60, don't gc for at least five seconds
40  * - Never gc again if any single gc takes longer than system property PROP_GC_MAX
41  */

42 class IDEIdleHelper {
43
44     /**
45      * The default minimum time between garbage collections.
46      */

47     private static final int DEFAULT_GC_INTERVAL = 60000;
48
49     /**
50      * The default maximum duration for a garbage collection, beyond which
51      * the explicit gc mechanism is automatically disabled.
52      */

53     private static final int DEFAULT_GC_MAX = 8000;
54
55     /**
56      * The multiple of the last gc duration before we will consider doing
57      * another one.
58      */

59     private static final int GC_DELAY_MULTIPLIER = 60;
60
61     /**
62      * The time interval of no keyboard or mouse events after which the system
63      * is considered idle.
64      */

65     private static final int IDLE_INTERVAL = 5000;
66
67     /**
68      * The name of the boolean system property that specifies whether explicit
69      * garbage collection is enabled.
70      */

71     private static final String JavaDoc PROP_GC = "ide.gc"; //$NON-NLS-1$
72

73     /**
74      * The name of the integer system property that specifies the minimum time
75      * interval in milliseconds between garbage collections.
76      */

77     private static final String JavaDoc PROP_GC_INTERVAL = "ide.gc.interval"; //$NON-NLS-1$
78

79     /**
80      * The name of the integer system property that specifies the maximum
81      * duration for a garbage collection. If this duration is ever exceeded, the
82      * explicit gc mechanism is disabled for the remainder of the session.
83      */

84     private static final String JavaDoc PROP_GC_MAX = "ide.gc.max"; //$NON-NLS-1$
85

86     protected IWorkbenchConfigurer configurer;
87
88     private Listener idleListener;
89
90     /**
91      * The last time we garbage collected.
92      */

93     private long lastGC = System.currentTimeMillis();
94
95     /**
96      * The maximum gc duration. If this value is exceeded, the
97      * entire explicit gc mechanism is disabled.
98      */

99     private int maxGC = DEFAULT_GC_MAX;
100     /**
101      * The minimum time interval until the next garbage collection
102      */

103     private int minGCInterval = DEFAULT_GC_INTERVAL;
104
105     /**
106      * The time interval until the next garbage collection
107      */

108     private int nextGCInterval = DEFAULT_GC_INTERVAL;
109     
110     private Job gcJob;
111
112     private Runnable JavaDoc handler;
113     
114     /**
115      * Creates and initializes the idle handler
116      * @param aConfigurer The workbench configurer.
117      */

118     IDEIdleHelper(IWorkbenchConfigurer aConfigurer) {
119         this.configurer = aConfigurer;
120         //don't gc while running tests because performance tests are sensitive to timing (see bug 121562)
121
if (PlatformUI.getTestableObject().getTestHarness() != null) {
122             return;
123         }
124         String JavaDoc enabled = System.getProperty(PROP_GC);
125         //gc is turned on by default if property is missing
126
if (enabled != null && enabled.equalsIgnoreCase(Boolean.FALSE.toString())) {
127             return;
128         }
129         //init gc interval
130
Integer JavaDoc prop = Integer.getInteger(PROP_GC_INTERVAL);
131         if (prop != null && prop.intValue() >= 0) {
132             minGCInterval = nextGCInterval = prop.intValue();
133         }
134
135         //init max gc interval
136
prop = Integer.getInteger(PROP_GC_MAX);
137         if (prop != null) {
138             maxGC = prop.intValue();
139         }
140         
141         createGarbageCollectionJob();
142
143         //hook idle handler
144
final Display display = configurer.getWorkbench().getDisplay();
145         handler = new Runnable JavaDoc() {
146                     public void run() {
147                         if (!display.isDisposed() && !configurer.getWorkbench().isClosing()) {
148                             int nextInterval;
149                             final long start = System.currentTimeMillis();
150                             //don't garbage collect if background jobs are running
151
if (!Platform.getJobManager().isIdle()) {
152                                 nextInterval = IDLE_INTERVAL;
153                             } else if ((start - lastGC) < nextGCInterval) {
154                                 //don't garbage collect if we have collected within the specific interval
155
nextInterval = nextGCInterval - (int) (start - lastGC);
156                             } else {
157                                 gcJob.schedule();
158                                 nextInterval = minGCInterval;
159                             }
160                             display.timerExec(nextInterval, this);
161                         }
162                     }
163                 };
164         idleListener = new Listener() {
165             public void handleEvent(Event event) {
166                 display.timerExec(IDLE_INTERVAL, handler);
167             }
168         };
169         display.addFilter(SWT.KeyUp, idleListener);
170         display.addFilter(SWT.MouseUp, idleListener);
171     }
172
173     /**
174      * Creates the job that performs garbage collection
175      */

176     private void createGarbageCollectionJob() {
177         gcJob = new Job(IDEWorkbenchMessages.IDEIdleHelper_backgroundGC) {
178             protected IStatus run(IProgressMonitor monitor) {
179                 final Display display = configurer.getWorkbench().getDisplay();
180                 if (display != null && !display.isDisposed()) {
181                     final long start = System.currentTimeMillis();
182                     System.gc();
183                     System.runFinalization();
184                     lastGC = start;
185                     final int duration = (int) (System.currentTimeMillis() - start);
186                     if (Policy.DEBUG_GC) {
187                         System.out.println("Explicit GC took: " + duration); //$NON-NLS-1$
188
}
189                     if (duration > maxGC) {
190                         if (Policy.DEBUG_GC) {
191                             System.out.println("Further explicit GCs disabled due to long GC"); //$NON-NLS-1$
192
}
193                         shutdown();
194                     } else {
195                         //if the gc took a long time, ensure the next gc doesn't happen for awhile
196
nextGCInterval = Math.max(minGCInterval, GC_DELAY_MULTIPLIER * duration);
197                         if (Policy.DEBUG_GC) {
198                             System.out.println("Next GC to run in: " + nextGCInterval); //$NON-NLS-1$
199
}
200                     }
201                 }
202                 return Status.OK_STATUS;
203             }
204         };
205         gcJob.setSystem(true);
206     }
207
208     /**
209      * Shuts down the idle helper, removing any installed listeners, etc.
210      */

211     void shutdown() {
212         if (idleListener == null) {
213             return;
214         }
215         final Display display = configurer.getWorkbench().getDisplay();
216         if (display != null && !display.isDisposed()) {
217             try {
218                 display.asyncExec(new Runnable JavaDoc() {
219                     public void run() {
220                         display.timerExec(-1, handler);
221                         display.removeFilter(SWT.KeyUp, idleListener);
222                         display.removeFilter(SWT.MouseUp, idleListener);
223                     }
224                 });
225             } catch (SWTException ex) {
226                 // ignore (display might be disposed)
227
}
228         }
229     }
230 }
231
Popular Tags