1 11 package org.eclipse.core.internal.runtime; 12 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.core.runtime.PerformanceStats.PerformanceListener; 18 import org.eclipse.core.runtime.jobs.Job; 19 import org.eclipse.osgi.framework.log.FrameworkLog; 20 import org.osgi.framework.BundleContext; 21 import org.osgi.framework.ServiceReference; 22 23 27 public class PerformanceStatsProcessor extends Job { 28 private static final PerformanceStatsProcessor instance = new PerformanceStatsProcessor(); 29 30 private static final long SCHEDULE_DELAY = 2000; 31 32 35 private final ArrayList changes = new ArrayList (); 36 37 41 private final HashMap failures = new HashMap (); 42 43 46 private final org.eclipse.core.runtime.ListenerList listeners = new org.eclipse.core.runtime.ListenerList(); 47 48 private PlatformLogWriter log; 49 50 53 public static void addListener(PerformanceListener listener) { 54 instance.listeners.add(listener); 55 } 56 57 62 public static void changed(PerformanceStats stats) { 63 synchronized (instance) { 64 instance.changes.add(stats); 65 } 66 instance.schedule(SCHEDULE_DELAY); 67 } 68 69 77 public static void failed(PerformanceStats stats, String pluginId, long elapsed) { 78 synchronized (instance) { 79 instance.failures.put(stats, new Long (elapsed)); 80 } 81 instance.schedule(SCHEDULE_DELAY); 82 instance.logFailure(stats, pluginId, elapsed); 83 } 84 85 88 public static void printStats(PrintWriter out) { 89 90 long totalTime = 0; 91 int totalCount = 0; 92 PerformanceStats[] allStats = PerformanceStats.getAllStats(); 93 for (int i = 0; i < allStats.length; i++) { 94 PerformanceStats stats = allStats[i]; 95 totalTime += stats.getRunningTime(); 96 totalCount += stats.getRunCount(); 97 } 98 out.println("---------------------------------------------------------------"); for (int i = 0; i < allStats.length; i++) { 101 PerformanceStats stats = allStats[i]; 102 out.print("Event: "); out.print(stats.getEvent()); 104 out.print(" Blame: "); out.print(stats.getBlameString()); 106 if (stats.getContext() != null) { 107 out.print(" Context: "); out.print(stats.getContext()); 109 } 110 out.println(); 111 112 int runCount = stats.getRunCount(); 113 if (runCount > 0) { 114 out.print("Run count: "); out.print(Integer.toString(runCount)); 116 out.print(" ("); out.print(Integer.toString((int) (runCount * 100.0 / totalCount))); 118 out.println(" % of total)"); } 120 121 long runTime = stats.getRunningTime(); 122 if (runTime > 0) { 123 out.print("Duration (ms): "); out.print(Long.toString(runTime)); 125 out.print(" ("); out.print(Integer.toString((int) (runTime * 100.0 / totalTime))); 127 out.println(" % of total)"); } 129 out.println(""); } 131 } 132 133 136 public static void removeListener(PerformanceListener listener) { 137 instance.listeners.remove(listener); 138 } 139 140 143 private PerformanceStatsProcessor() { 144 super("Performance Stats"); setSystem(true); 146 setPriority(DECORATE); 147 BundleContext context = PlatformActivator.getContext(); 148 String filter = '(' + FrameworkLog.SERVICE_PERFORMANCE + '=' + Boolean.TRUE.toString() + ')'; 149 ServiceReference[] references; 150 FrameworkLog perfLog = null; 151 try { 152 references = context.getServiceReferences(FrameworkLog.class.getName(), filter); 153 if (references != null && references.length > 0) { 154 perfLog = (FrameworkLog) context.getService(references[0]); 156 IPath logLocation = Platform.getLogFileLocation(); 158 logLocation = logLocation.removeLastSegments(1).append("performance.log"); perfLog.setFile(logLocation.toFile(), false); 160 } 161 } catch (Exception e) { 162 IStatus error = new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, "Error loading performance log", e); InternalPlatform.getDefault().log(error); 164 } 165 if (perfLog == null) 167 perfLog = InternalPlatform.getDefault().getFrameworkLog(); 168 log = new PlatformLogWriter(perfLog); 169 } 170 171 174 private void logFailure(PerformanceStats stats, String pluginId, long elapsed) { 175 if (log == null) 177 return; 178 if (pluginId == null) 179 pluginId = Platform.PI_RUNTIME; 180 String msg = "Performance failure: " + stats.getEvent() + " blame: " + stats.getBlameString() + " context: " + stats.getContext() + " duration: " + elapsed; log.logging(new Status(IStatus.WARNING, pluginId, 1, msg, new RuntimeException ()), pluginId); 182 } 183 184 187 protected IStatus run(IProgressMonitor monitor) { 188 PerformanceStats[] events; 189 PerformanceStats[] failedEvents; 190 Long [] failedTimes; 191 synchronized (this) { 192 events = (PerformanceStats[]) changes.toArray(new PerformanceStats[changes.size()]); 193 changes.clear(); 194 failedEvents = (PerformanceStats[]) failures.keySet().toArray(new PerformanceStats[failures.size()]); 195 failedTimes = (Long []) failures.values().toArray(new Long [failures.size()]); 196 failures.clear(); 197 } 198 199 Object [] toNotify = listeners.getListeners(); 201 for (int i = 0; i < toNotify.length; i++) { 202 final PerformanceStats.PerformanceListener listener = ((PerformanceStats.PerformanceListener) toNotify[i]); 203 if (events.length > 0) 204 listener.eventsOccurred(events); 205 for (int j = 0; j < failedEvents.length; j++) 206 listener.eventFailed(failedEvents[j], failedTimes[j].longValue()); 207 } 208 schedule(SCHEDULE_DELAY); 209 return Status.OK_STATUS; 210 } 211 212 215 public boolean shouldRun() { 216 return !changes.isEmpty() || !failures.isEmpty(); 217 } 218 } | Popular Tags |