001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide.application;
011:
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.core.runtime.jobs.Job;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.SWTException;
019: import org.eclipse.swt.widgets.Display;
020: import org.eclipse.swt.widgets.Event;
021: import org.eclipse.swt.widgets.Listener;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.application.IWorkbenchConfigurer;
024: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
025: import org.eclipse.ui.internal.ide.Policy;
026:
027: /**
028: * The idle helper detects when the system is idle in order to perform garbage
029: * collection in a way that minimizes impact on responsiveness of the UI.
030: * The algorithm for determining when to perform a garbage collection
031: * is as follows:
032: *
033: * - Never gc if there is a test harness present
034: * - Don't gc if background jobs are running
035: * - Don't gc if the keyboard or mouse have been active within IDLE_INTERVAL
036: * - Don't gc if there has been a GC within the minimum gc interval (system property PROP_GC_INTERVAL)
037: * - After a gc, don't gc again until (duration * GC_DELAY_MULTIPLIER) has elapsed.
038: * For example, if a GC takes 100ms and the multiplier is 60, don't gc for at least five seconds
039: * - Never gc again if any single gc takes longer than system property PROP_GC_MAX
040: */
041: class IDEIdleHelper {
042:
043: /**
044: * The default minimum time between garbage collections.
045: */
046: private static final int DEFAULT_GC_INTERVAL = 60000;
047:
048: /**
049: * The default maximum duration for a garbage collection, beyond which
050: * the explicit gc mechanism is automatically disabled.
051: */
052: private static final int DEFAULT_GC_MAX = 8000;
053:
054: /**
055: * The multiple of the last gc duration before we will consider doing
056: * another one.
057: */
058: private static final int GC_DELAY_MULTIPLIER = 60;
059:
060: /**
061: * The time interval of no keyboard or mouse events after which the system
062: * is considered idle.
063: */
064: private static final int IDLE_INTERVAL = 5000;
065:
066: /**
067: * The name of the boolean system property that specifies whether explicit
068: * garbage collection is enabled.
069: */
070: private static final String PROP_GC = "ide.gc"; //$NON-NLS-1$
071:
072: /**
073: * The name of the integer system property that specifies the minimum time
074: * interval in milliseconds between garbage collections.
075: */
076: private static final String PROP_GC_INTERVAL = "ide.gc.interval"; //$NON-NLS-1$
077:
078: /**
079: * The name of the integer system property that specifies the maximum
080: * duration for a garbage collection. If this duration is ever exceeded, the
081: * explicit gc mechanism is disabled for the remainder of the session.
082: */
083: private static final String PROP_GC_MAX = "ide.gc.max"; //$NON-NLS-1$
084:
085: protected IWorkbenchConfigurer configurer;
086:
087: private Listener idleListener;
088:
089: /**
090: * The last time we garbage collected.
091: */
092: private long lastGC = System.currentTimeMillis();
093:
094: /**
095: * The maximum gc duration. If this value is exceeded, the
096: * entire explicit gc mechanism is disabled.
097: */
098: private int maxGC = DEFAULT_GC_MAX;
099: /**
100: * The minimum time interval until the next garbage collection
101: */
102: private int minGCInterval = DEFAULT_GC_INTERVAL;
103:
104: /**
105: * The time interval until the next garbage collection
106: */
107: private int nextGCInterval = DEFAULT_GC_INTERVAL;
108:
109: private Job gcJob;
110:
111: private Runnable handler;
112:
113: /**
114: * Creates and initializes the idle handler
115: * @param aConfigurer The workbench configurer.
116: */
117: IDEIdleHelper(IWorkbenchConfigurer aConfigurer) {
118: this .configurer = aConfigurer;
119: //don't gc while running tests because performance tests are sensitive to timing (see bug 121562)
120: if (PlatformUI.getTestableObject().getTestHarness() != null) {
121: return;
122: }
123: String enabled = System.getProperty(PROP_GC);
124: //gc is turned on by default if property is missing
125: if (enabled != null
126: && enabled.equalsIgnoreCase(Boolean.FALSE.toString())) {
127: return;
128: }
129: //init gc interval
130: Integer 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() {
146: public void run() {
147: if (!display.isDisposed()
148: && !configurer.getWorkbench().isClosing()) {
149: int nextInterval;
150: final long start = System.currentTimeMillis();
151: //don't garbage collect if background jobs are running
152: if (!Platform.getJobManager().isIdle()) {
153: nextInterval = IDLE_INTERVAL;
154: } else if ((start - lastGC) < nextGCInterval) {
155: //don't garbage collect if we have collected within the specific interval
156: nextInterval = nextGCInterval
157: - (int) (start - lastGC);
158: } else {
159: gcJob.schedule();
160: nextInterval = minGCInterval;
161: }
162: display.timerExec(nextInterval, this );
163: }
164: }
165: };
166: idleListener = new Listener() {
167: public void handleEvent(Event event) {
168: display.timerExec(IDLE_INTERVAL, handler);
169: }
170: };
171: display.addFilter(SWT.KeyUp, idleListener);
172: display.addFilter(SWT.MouseUp, idleListener);
173: }
174:
175: /**
176: * Creates the job that performs garbage collection
177: */
178: private void createGarbageCollectionJob() {
179: gcJob = new Job(IDEWorkbenchMessages.IDEIdleHelper_backgroundGC) {
180: protected IStatus run(IProgressMonitor monitor) {
181: final Display display = configurer.getWorkbench()
182: .getDisplay();
183: if (display != null && !display.isDisposed()) {
184: final long start = System.currentTimeMillis();
185: System.gc();
186: System.runFinalization();
187: lastGC = start;
188: final int duration = (int) (System
189: .currentTimeMillis() - start);
190: if (Policy.DEBUG_GC) {
191: System.out
192: .println("Explicit GC took: " + duration); //$NON-NLS-1$
193: }
194: if (duration > maxGC) {
195: if (Policy.DEBUG_GC) {
196: System.out
197: .println("Further explicit GCs disabled due to long GC"); //$NON-NLS-1$
198: }
199: shutdown();
200: } else {
201: //if the gc took a long time, ensure the next gc doesn't happen for awhile
202: nextGCInterval = Math.max(minGCInterval,
203: GC_DELAY_MULTIPLIER * duration);
204: if (Policy.DEBUG_GC) {
205: System.out
206: .println("Next GC to run in: " + nextGCInterval); //$NON-NLS-1$
207: }
208: }
209: }
210: return Status.OK_STATUS;
211: }
212: };
213: gcJob.setSystem(true);
214: }
215:
216: /**
217: * Shuts down the idle helper, removing any installed listeners, etc.
218: */
219: void shutdown() {
220: if (idleListener == null) {
221: return;
222: }
223: final Display display = configurer.getWorkbench().getDisplay();
224: if (display != null && !display.isDisposed()) {
225: try {
226: display.asyncExec(new Runnable() {
227: public void run() {
228: display.timerExec(-1, handler);
229: display.removeFilter(SWT.KeyUp, idleListener);
230: display.removeFilter(SWT.MouseUp, idleListener);
231: }
232: });
233: } catch (SWTException ex) {
234: // ignore (display might be disposed)
235: }
236: }
237: }
238: }
|