001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.misc;
011:
012: import java.util.HashMap;
013:
014: import org.eclipse.core.runtime.PerformanceStats;
015: import org.eclipse.core.runtime.Platform;
016: import org.eclipse.ui.PlatformUI;
017:
018: /**
019: * This class is used for monitoring performance events. Each performance
020: * event has an associated option in the org.eclipse.ui plugin's .options file
021: * that specifies an maximum acceptable duration for that event.
022: *
023: * @see org.eclipse.core.runtime.PerformanceStats
024: */
025: public class UIStats {
026:
027: private static HashMap operations = new HashMap();
028:
029: public static final int CREATE_PART = 0;
030:
031: public static final int CREATE_PART_CONTROL = 1;
032:
033: public static final int INIT_PART = 2;
034:
035: public static final int CREATE_PERSPECTIVE = 3;
036:
037: public static final int RESTORE_WORKBENCH = 4;
038:
039: public static final int START_WORKBENCH = 5;
040:
041: public static final int CREATE_PART_INPUT = 6;
042:
043: public static final int ACTIVATE_PART = 7;
044:
045: public static final int BRING_PART_TO_TOP = 8;
046:
047: public static final int NOTIFY_PART_LISTENERS = 9;
048:
049: public static final int SWITCH_PERSPECTIVE = 10;
050:
051: public static final int NOTIFY_PAGE_LISTENERS = 11;
052:
053: public static final int NOTIFY_PERSPECTIVE_LISTENERS = 12;
054:
055: public static final int UI_JOB = 13;
056:
057: public static final int CONTENT_TYPE_LOOKUP = 14;
058:
059: /**
060: * Change this value when you add a new event constant.
061: */
062: public static final int LAST_VALUE = CONTENT_TYPE_LOOKUP;
063:
064: private static boolean debug[] = new boolean[LAST_VALUE + 1];
065:
066: private static String[] events = new String[LAST_VALUE + 1];
067:
068: static {
069: events[CREATE_PART] = PlatformUI.PLUGIN_ID
070: + "/perf/part.create"; //$NON-NLS-1$
071: events[CREATE_PART_INPUT] = PlatformUI.PLUGIN_ID
072: + "/perf/part.input"; //$NON-NLS-1$
073: events[CREATE_PART_CONTROL] = PlatformUI.PLUGIN_ID
074: + "/perf/part.control"; //$NON-NLS-1$
075: events[INIT_PART] = PlatformUI.PLUGIN_ID + "/perf/part.init"; //$NON-NLS-1$
076: events[CREATE_PERSPECTIVE] = PlatformUI.PLUGIN_ID
077: + "/perf/perspective.create"; //$NON-NLS-1$
078: events[SWITCH_PERSPECTIVE] = PlatformUI.PLUGIN_ID
079: + "/perf/perspective.switch"; //$NON-NLS-1$
080: events[RESTORE_WORKBENCH] = PlatformUI.PLUGIN_ID
081: + "/perf/workbench.restore"; //$NON-NLS-1$
082: events[START_WORKBENCH] = PlatformUI.PLUGIN_ID
083: + "/perf/workbench.start"; //$NON-NLS-1$
084: events[ACTIVATE_PART] = PlatformUI.PLUGIN_ID
085: + "/perf/part.activate"; //$NON-NLS-1$
086: events[BRING_PART_TO_TOP] = PlatformUI.PLUGIN_ID
087: + "/perf/part.activate"; //$NON-NLS-1$
088: events[NOTIFY_PART_LISTENERS] = PlatformUI.PLUGIN_ID
089: + "/perf/part.listeners"; //$NON-NLS-1$
090: events[NOTIFY_PAGE_LISTENERS] = PlatformUI.PLUGIN_ID
091: + "/perf/page.listeners"; //$NON-NLS-1$
092: events[NOTIFY_PERSPECTIVE_LISTENERS] = PlatformUI.PLUGIN_ID
093: + "/perf/perspective.listeners"; //$NON-NLS-1$
094: events[UI_JOB] = PlatformUI.PLUGIN_ID + "/perf/uijob"; //$NON-NLS-1$
095: events[CONTENT_TYPE_LOOKUP] = PlatformUI.PLUGIN_ID
096: + "/perf/contentTypes"; //$NON-NLS-1$
097:
098: for (int i = 0; i <= LAST_VALUE; i++) {
099: //don't log any performance events if the general performance stats is disabled
100: if (events[i] != null && PerformanceStats.ENABLED) {
101: debug[i] = PerformanceStats.isEnabled(events[i]);
102: }
103: }
104: }
105:
106: /**
107: * Returns whether tracing of the given debug event is turned on.
108: *
109: * @param event The event id
110: * @return <code>true</code> if tracing of this event is turned on,
111: * and <code>false</code> otherwise.
112: */
113: public static boolean isDebugging(int event) {
114: return debug[event];
115: }
116:
117: /**
118: * Indicates the start of a performance event
119: *
120: * @param event The event id
121: * @param label The event label
122: */
123: public static void start(int event, String label) {
124: if (debug[event]) {
125: operations.put(event + label, new Long(System
126: .currentTimeMillis()));
127: }
128: }
129:
130: /**
131: * Indicates the end of a performance operation
132: *
133: * @param event The event id
134: * @param blame An object that is responsible for the event that occurred,
135: * or that uniquely describes the event that occurred
136: * @param label The event label
137: */
138: public static void end(int event, Object blame, String label) {
139: if (debug[event]) {
140: Long startTime = (Long) operations.remove(event + label);
141: if (startTime == null) {
142: return;
143: }
144: final long elapsed = System.currentTimeMillis()
145: - startTime.longValue();
146: // System.out.println("Time - " + //$NON-NLS-1$
147: // elapsed + events[event] + label);
148: PerformanceStats.getStats(events[event], blame).addRun(
149: elapsed, label);
150: }
151: }
152:
153: /**
154: * Special hook to signal that application startup is complete and the event
155: * loop has started running.
156: */
157: public static void startupComplete() {
158: // We use a runtime debug option here for backwards compatibility (bug 96672)
159: // Note that this value is only relevant if the workspace chooser is not used.
160: String option = Platform.getDebugOption(Platform.PI_RUNTIME
161: + "/debug"); //$NON-NLS-1$
162: if (option == null || !"true".equalsIgnoreCase(option)) { //$NON-NLS-1$
163: return;
164: }
165: String startString = System.getProperty("eclipse.startTime"); //$NON-NLS-1$
166: if (startString == null) {
167: return;
168: }
169: try {
170: long start = Long.parseLong(startString);
171: long end = System.currentTimeMillis();
172: System.out
173: .println("Startup complete: " + (end - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
174: } catch (NumberFormatException e) {
175: //this is just debugging code -- ok to swallow exception
176: }
177: }
178: }
|