sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.ipc/src/com/nokia/carbide/cpp/pi/ipc/IpcPlugin.java
changeset 5 844b047e260d
child 12 ae255c9aa552
equal deleted inserted replaced
4:615035072f7e 5:844b047e260d
       
     1 package com.nokia.carbide.cpp.pi.ipc;
       
     2 
       
     3 import java.io.File;
       
     4 import java.io.IOException;
       
     5 import java.net.URL;
       
     6 import java.util.HashMap;
       
     7 import java.util.Map;
       
     8 
       
     9 import org.eclipse.core.runtime.FileLocator;
       
    10 import org.eclipse.core.runtime.IPath;
       
    11 import org.eclipse.core.runtime.Path;
       
    12 import org.eclipse.swt.widgets.Event;
       
    13 import org.osgi.framework.Bundle;
       
    14 import org.osgi.framework.BundleContext;
       
    15 
       
    16 import com.nokia.carbide.cpp.internal.pi.analyser.NpiInstanceRepository;
       
    17 import com.nokia.carbide.cpp.internal.pi.analyser.ProfileVisualiser;
       
    18 import com.nokia.carbide.cpp.internal.pi.model.GenericTrace;
       
    19 import com.nokia.carbide.cpp.internal.pi.model.ParsedTraceData;
       
    20 import com.nokia.carbide.cpp.internal.pi.plugin.model.AbstractPiPlugin;
       
    21 import com.nokia.carbide.cpp.internal.pi.plugin.model.IClassReplacer;
       
    22 import com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener;
       
    23 import com.nokia.carbide.cpp.internal.pi.plugin.model.IFinalizeTrace;
       
    24 import com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace;
       
    25 import com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable;
       
    26 import com.nokia.carbide.cpp.internal.pi.visual.GraphDrawRequest;
       
    27 import com.nokia.carbide.cpp.pi.editors.PIPageEditor;
       
    28 import com.nokia.carbide.cpp.pi.internal.ipc.IpcSample;
       
    29 import com.nokia.carbide.cpp.pi.internal.ipc.IpcTrace;
       
    30 import com.nokia.carbide.cpp.pi.internal.ipc.IpcTraceParser;
       
    31 import com.nokia.carbide.cpp.pi.peccommon.PecCommonGuiManager;
       
    32 import com.nokia.carbide.cpp.pi.visual.IGenericTraceGraph;
       
    33 /**
       
    34  * The activator class controls the plug-in life cycle
       
    35  */
       
    36 public class IpcPlugin extends AbstractPiPlugin implements ITrace, IEventListener, IVisualizable, IFinalizeTrace, IClassReplacer {
       
    37 
       
    38 	/** number of graphs created (one on each page)*/
       
    39 	public static final int GRAPH_COUNT = 3;
       
    40 
       
    41 	/** The plug-in ID */
       
    42 	public static final String PLUGIN_ID = "com.nokia.carbide.cpp.pi.ipc"; //$NON-NLS-1$
       
    43 	
       
    44 	private static final String HELP_CONTEXT_ID = PIPageEditor.PI_ID + ".ipc";  //$NON-NLS-1$
       
    45 	/** context help id of the main page */
       
    46 	public static final String HELP_CONTEXT_ID_MAIN_PAGE = HELP_CONTEXT_ID + ".ipcPageContext";  //$NON-NLS-1$
       
    47 
       
    48 	/** the trace id for PEC traces */
       
    49 	private static final int PEC_TRACE_ID = 12;
       
    50 	
       
    51 	/** Persistable preference for the performance counter trace draw mode (show single graph or all graphs)  */
       
    52 	public static final String PECTRACE_DRAWMODE = "com.nokia.carbide.cpp.pi.ipc.drawmode";//$NON-NLS-1$
       
    53 	/** draw mode for showing all graphs */
       
    54 	public static final String PECTRACE_DRAWMODE_SHOW_ALL = "ShowAll";//$NON-NLS-1$
       
    55 
       
    56 	// The shared instance
       
    57 	private static IpcPlugin plugin;
       
    58 	
       
    59 	private static final Map<Integer, PecCommonGuiManager> guiManagerMap = new HashMap<Integer, PecCommonGuiManager>();
       
    60 
       
    61 	/**
       
    62 	 * The constructor
       
    63 	 */
       
    64 	public IpcPlugin() {
       
    65 		super();
       
    66 	}
       
    67 
       
    68 	/*
       
    69 	 * (non-Javadoc)
       
    70 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
       
    71 	 */
       
    72 	@Override
       
    73 	public void start(BundleContext context) throws Exception {
       
    74 		super.start(context);
       
    75 		plugin = this;
       
    76 	}
       
    77 
       
    78 	/*
       
    79 	 * (non-Javadoc)
       
    80 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
       
    81 	 */
       
    82 	@Override
       
    83 	public void stop(BundleContext context) throws Exception {
       
    84 		plugin = null;
       
    85 		super.stop(context);
       
    86 	}
       
    87 
       
    88 	/**
       
    89 	 * Returns the shared instance
       
    90 	 *
       
    91 	 * @return the shared instance
       
    92 	 */
       
    93 	public static IpcPlugin getDefault() {
       
    94 		return plugin;
       
    95 	}
       
    96 
       
    97 	/* (non-Javadoc)
       
    98 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace#getTraceClass()
       
    99 	 */
       
   100 	public Class getTraceClass() {
       
   101 		return IpcTrace.class;
       
   102 	}
       
   103 
       
   104 	/* (non-Javadoc)
       
   105 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace#initialiseTrace(com.nokia.carbide.cpp.internal.pi.model.GenericTrace)
       
   106 	 */
       
   107 	public void initialiseTrace(GenericTrace trace) {
       
   108 		if (!(trace instanceof IpcTrace)){
       
   109 			throw new IllegalArgumentException();
       
   110 		}
       
   111 			
       
   112 		IpcTrace parsedTrace = (IpcTrace)trace;
       
   113 		
       
   114 		int samplingInterval = 1; //in milliseconds
       
   115 		if (parsedTrace.samples.size() > 2) {
       
   116 			samplingInterval = (int) ((parsedTrace.samples.get(1)).sampleSynchTime - (parsedTrace.samples.get(0)).sampleSynchTime); 
       
   117 		}
       
   118 		parsedTrace.setSamplingInterval(samplingInterval);
       
   119 		
       
   120 		
       
   121 		NpiInstanceRepository.getInstance().activeUidAddTrace(PLUGIN_ID, trace);
       
   122 		
       
   123 		//create the GUI class which manages the graphs
       
   124 		int uid = NpiInstanceRepository.getInstance().activeUid();
       
   125 		guiManagerMap.put(uid, new PecCommonGuiManager(uid, parsedTrace, IpcPlugin.GRAPH_COUNT, Messages.IpcPlugin_0));
       
   126 	}
       
   127 
       
   128 	/* (non-Javadoc)
       
   129 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace#getTraceName()
       
   130 	 */
       
   131 	public String getTraceName() {
       
   132 		return "IPC"; //$NON-NLS-1$
       
   133 	}
       
   134 
       
   135 	/* (non-Javadoc)
       
   136 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace#getTraceTitle()
       
   137 	 */
       
   138 	public String getTraceTitle() {
       
   139 		return Messages.IpcPlugin_1;
       
   140 	}
       
   141 
       
   142 	/* (non-Javadoc)
       
   143 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace#getTraceId()
       
   144 	 */
       
   145 	public int getTraceId() {
       
   146 		return PEC_TRACE_ID;
       
   147 	}
       
   148 
       
   149 	/* (non-Javadoc)
       
   150 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace#parseTraceFile(java.io.File)
       
   151 	 */
       
   152 	public ParsedTraceData parseTraceFile(File file) throws Exception {
       
   153 		IpcTraceParser p = new IpcTraceParser();
       
   154 		ParsedTraceData ptd = p.parse(file);
       
   155 		
       
   156 		return ptd;
       
   157 	}
       
   158 
       
   159 	/* (non-Javadoc)
       
   160 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace#parseTraceFiles(java.io.File[])
       
   161 	 */
       
   162 	public ParsedTraceData parseTraceFiles(File[] files) throws Exception {
       
   163 		throw new UnsupportedOperationException();
       
   164 	}
       
   165 
       
   166 	/* (non-Javadoc)
       
   167 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener#receiveEvent(java.lang.String, org.eclipse.swt.widgets.Event)
       
   168 	 */
       
   169 	public void receiveEvent(String action, Event event) {
       
   170 		//no-op
       
   171 		
       
   172 	}
       
   173 
       
   174 	/* (non-Javadoc)
       
   175 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#arePagesCreated()
       
   176 	 */
       
   177 	public boolean arePagesCreated() {
       
   178 		return false; //PEC traces add graphs to existing pages
       
   179 	}
       
   180 
       
   181 	/* (non-Javadoc)
       
   182 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#createPage(int)
       
   183 	 */
       
   184 	public ProfileVisualiser createPage(int index) {
       
   185 		return null; //PEC traces add graphs to existing pages
       
   186 	}
       
   187 
       
   188 	/* (non-Javadoc)
       
   189 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#getCreatePageCount()
       
   190 	 */
       
   191 	public int getCreatePageCount() {
       
   192 		return 0; //PEC traces don't create pages
       
   193 	}
       
   194 
       
   195 	/* (non-Javadoc)
       
   196 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#getCreatePageIndex(int)
       
   197 	 */
       
   198 	public int getCreatePageIndex(int index) {
       
   199 		throw new UnsupportedOperationException(); //PEC traces don't create pages
       
   200 	}
       
   201 
       
   202 	/* (non-Javadoc)
       
   203 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#getDrawRequest(int)
       
   204 	 */
       
   205 	public GraphDrawRequest getDrawRequest(int graphIndex) {
       
   206 		return null;
       
   207 	}
       
   208 
       
   209 	/* (non-Javadoc)
       
   210 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#getGraphCount()
       
   211 	 */
       
   212 	public int getGraphCount() {
       
   213 		return GRAPH_COUNT;
       
   214 	}
       
   215 
       
   216 	/* (non-Javadoc)
       
   217 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#getLastSample(int)
       
   218 	 */
       
   219 	public Integer getLastSample(int graphIndex) {
       
   220 		IpcTrace trace = (IpcTrace) NpiInstanceRepository.getInstance().activeUidGetTrace(PLUGIN_ID);
       
   221 		if(trace == null){
       
   222 			return 0;
       
   223 		}
       
   224 		
       
   225 		return trace.getLastSampleNumber();
       
   226 	}
       
   227 
       
   228 	/* (non-Javadoc)
       
   229 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#getPageNumber(int)
       
   230 	 */
       
   231 	public int getPageNumber(int graphIndex) {
       
   232 		if (graphIndex == 0) {
       
   233 			return PIPageEditor.THREADS_PAGE;
       
   234 		} else if (graphIndex == 1) {
       
   235 			return PIPageEditor.BINARIES_PAGE;
       
   236 		} else if (graphIndex == 2) {
       
   237 			return PIPageEditor.FUNCTIONS_PAGE;
       
   238 		}
       
   239 
       
   240 		return PIPageEditor.NEXT_AVAILABLE_PAGE;
       
   241 	}
       
   242 
       
   243 	/* (non-Javadoc)
       
   244 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#getTraceGraph(int)
       
   245 	 */
       
   246 	public IGenericTraceGraph getTraceGraph(int graphIndex) {
       
   247 		int uid = NpiInstanceRepository.getInstance().activeUid();
       
   248 		PecCommonGuiManager guiManager = guiManagerMap.get(uid);
       
   249 		
       
   250 		if (guiManager != null){
       
   251 			return guiManager.getTraceGraph(graphIndex, HELP_CONTEXT_ID_MAIN_PAGE);			
       
   252 		}
       
   253 		
       
   254 		throw new IllegalStateException();
       
   255 	}
       
   256 
       
   257 	/* (non-Javadoc)
       
   258 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#setPageIndex(int, int)
       
   259 	 */
       
   260 	public void setPageIndex(int index, int pageIndex) {
       
   261 		//no-op
       
   262 	}
       
   263 
       
   264 	/* (non-Javadoc)
       
   265 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IVisualizable#setPagesCreated(boolean)
       
   266 	 */
       
   267 	public void setPagesCreated(boolean pagesCreated) {
       
   268 		//no-op
       
   269 	}
       
   270 	
       
   271 	/**
       
   272 	 * Returns a File corresponding to the given bundle relative path.
       
   273 	 * @param path the bundle relative path to resource to locate
       
   274 	 * @return the File corresponding to the given bundle relative path, or null
       
   275 	 * @throws IOException
       
   276 	 */
       
   277 	public File locateFileInBundle(final String path) throws IOException {
       
   278 		Bundle myBundle= getDefault().getBundle();
       
   279 		IPath ppath= new Path(path);
       
   280 		ppath= ppath.makeRelative();
       
   281 		URL[] urls= FileLocator.findEntries(myBundle, ppath);
       
   282 		if(urls.length != 1) {
       
   283 			return null;
       
   284 		}
       
   285 		return new File(FileLocator.toFileURL(urls[0]).getFile());
       
   286 	}
       
   287 
       
   288 	
       
   289 	/* (non-Javadoc)
       
   290 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IFinalizeTrace#runOnDispose()
       
   291 	 */
       
   292 	public void runOnDispose() {
       
   293 		// called when editor closes
       
   294 		
       
   295 		// Do any cleanup work here when the editor closes
       
   296 		int uid = NpiInstanceRepository.getInstance().activeUid();
       
   297 		guiManagerMap.remove(uid);
       
   298 	}
       
   299 
       
   300 	/* (non-Javadoc)
       
   301 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IFinalizeTrace#runOnPartOpened()
       
   302 	 */
       
   303 	public void runOnPartOpened() {
       
   304 		// nothing to do
       
   305 	}
       
   306 
       
   307 	/* (non-Javadoc)
       
   308 	 * @see com.nokia.carbide.cpp.internal.pi.plugin.model.IClassReplacer#getReplacedClass(java.lang.String)
       
   309 	 */
       
   310 	public Class getReplacedClass(String className) {
       
   311 		if (className.startsWith("com.nokia.carbide.cpp.pi.internal.ipc.IpcTrace")){ //$NON-NLS-1$
       
   312 			return IpcTrace.class;
       
   313 		} else if (className.startsWith("com.nokia.carbide.cpp.pi.internal.ipc.IpcSample")){//$NON-NLS-1$
       
   314 			return IpcSample.class;
       
   315 		}
       
   316 		return null;
       
   317 	}
       
   318 
       
   319 
       
   320 }