sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.priority/src/com/nokia/carbide/cpp/pi/priority/PriorityPlugin.java
changeset 2 b9ab3b238396
child 5 844b047e260d
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.carbide.cpp.pi.priority;
       
    19 
       
    20 import java.io.File;
       
    21 import java.text.DecimalFormat;
       
    22 import java.util.ArrayList;
       
    23 import java.util.Enumeration;
       
    24 import java.util.Hashtable;
       
    25 import java.util.Iterator;
       
    26 import java.util.Vector;
       
    27 
       
    28 import org.eclipse.jface.action.MenuManager;
       
    29 import org.eclipse.jface.resource.ImageDescriptor;
       
    30 import org.eclipse.swt.widgets.Event;
       
    31 import org.osgi.framework.BundleContext;
       
    32 
       
    33 import com.nokia.carbide.cpp.internal.pi.manager.PluginInitialiser;
       
    34 import com.nokia.carbide.cpp.internal.pi.model.GenericThread;
       
    35 import com.nokia.carbide.cpp.internal.pi.model.GenericTrace;
       
    36 import com.nokia.carbide.cpp.internal.pi.model.ParsedTraceData;
       
    37 import com.nokia.carbide.cpp.internal.pi.plugin.model.AbstractPiPlugin;
       
    38 import com.nokia.carbide.cpp.internal.pi.plugin.model.IClassReplacer;
       
    39 import com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener;
       
    40 import com.nokia.carbide.cpp.internal.pi.plugin.model.IReportable;
       
    41 import com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace;
       
    42 
       
    43 
       
    44 /**
       
    45  * The main plugin class to be used in the desktop.
       
    46  */
       
    47 public class PriorityPlugin extends AbstractPiPlugin
       
    48 		implements ITrace, IClassReplacer, IReportable {
       
    49 
       
    50 	//The shared instance.
       
    51 	private static PriorityPlugin plugin;
       
    52 	
       
    53 	private static DecimalFormat format = new DecimalFormat(Messages.getString("PriorityPlugin.zeroFormat")); //$NON-NLS-1$
       
    54 
       
    55 	
       
    56 	private PriTrace trace = null;
       
    57 	private Hashtable<Integer,String> priStringById;
       
    58 
       
    59 	private static void setPlugin(PriorityPlugin newPlugin)
       
    60 	{
       
    61 		plugin = newPlugin;
       
    62 	}
       
    63 
       
    64 	/**
       
    65 	 * The constructor.
       
    66 	 */
       
    67 	public PriorityPlugin() {
       
    68 		setPlugin(this);
       
    69 	}
       
    70 
       
    71 	public Class getReplacedClass(String className)
       
    72 	{
       
    73 		if (   className.indexOf("com.nokia.carbide.cpp.pi.priority.PriSample") != -1 //$NON-NLS-1$
       
    74 			|| className.indexOf("com.nokia.carbide.pi.priority.PriSample") != -1 //$NON-NLS-1$
       
    75 			|| className.indexOf("fi.vtt.bappea.model.PriSample") != -1 //$NON-NLS-1$
       
    76 			|| className.indexOf("fi.vtt.bappea.priTracePlugin.PriSample") != -1) //$NON-NLS-1$
       
    77 		{
       
    78 			return PriSample.class;
       
    79 		}
       
    80 		else if (   className.indexOf("[Lcom.nokia.carbide.cpp.pi.priority.PriThread") != -1 //$NON-NLS-1$
       
    81 				 || className.indexOf("[Lcom.nokia.carbide.pi.priority.PriThread") != -1 //$NON-NLS-1$
       
    82 				 || className.indexOf("[Lfi.vtt.bappea.model.PriThread;") != -1 //$NON-NLS-1$
       
    83 				 || className.indexOf("[Lfi.vtt.bappea.priTracePlugin.PriThread") != -1) //$NON-NLS-1$
       
    84 		{
       
    85 			return PriThread[].class;
       
    86 		}
       
    87 		else if (   className.indexOf("com.nokia.carbide.cpp.pi.priority.PriThread") != -1 //$NON-NLS-1$
       
    88 				 || className.indexOf("com.nokia.carbide.pi.priority.PriThread") != -1 //$NON-NLS-1$
       
    89 				 || className.indexOf("fi.vtt.bappea.model.PriThread") != -1 //$NON-NLS-1$
       
    90 				 || className.indexOf("fi.vtt.bappea.priTracePlugin.PriThread") != -1) //$NON-NLS-1$
       
    91 		{
       
    92 			return PriThread.class;
       
    93 		}
       
    94 		else if (   className.indexOf("com.nokia.carbide.cpp.pi.priority.PriTrace") != -1 //$NON-NLS-1$
       
    95 				 || className.indexOf("com.nokia.carbide.pi.priority.PriTrace") != -1 //$NON-NLS-1$
       
    96 				 || className.indexOf("fi.vtt.bappea.model.PriTrace") != -1 //$NON-NLS-1$
       
    97 				 || className.indexOf("fi.vtt.bappea.priTracePlugin.PriTrace") != -1) //$NON-NLS-1$
       
    98 		{
       
    99 			return PriTrace.class;
       
   100 		}
       
   101 		else return null;
       
   102 	}
       
   103 	
       
   104 	public Class getTraceClass() 
       
   105 	{
       
   106 		return PriTrace.class;
       
   107 	}
       
   108 
       
   109 	public void initialiseTrace(GenericTrace trace /*, ProfileVisualiser pv*/) 
       
   110 	{
       
   111 		if (trace == null)
       
   112 			return;
       
   113 		this.trace = (PriTrace)trace;
       
   114 
       
   115 	  	GenericThread[] threads =  this.trace.getThreads();
       
   116 		int threadCount = threads.length;
       
   117 
       
   118 		ArrayList<PriSample>[] priData = new ArrayList[threadCount];
       
   119 		Hashtable<Integer,ArrayList<PriSample>> priDataById = new Hashtable<Integer,ArrayList<PriSample>>();
       
   120 		priStringById = new Hashtable<Integer,String>();
       
   121 
       
   122 		//create priority data structure and inserts thread IDs into hashtable
       
   123 		for (int i = 0; i < threadCount; i++)
       
   124 		{
       
   125 			priData[i] = new ArrayList<PriSample>();
       
   126 			priDataById.put(threads[i].threadId, priData[i]);
       
   127 		}
       
   128 		
       
   129 		//insert priority data into hashtable based on thread id
       
   130 		for (Enumeration e = this.trace.getSamples(); e.hasMoreElements();)
       
   131 		{
       
   132 			PriSample tmp = (PriSample) e.nextElement();
       
   133 			priDataById.get(tmp.thread.threadId).add(tmp);
       
   134 		}
       
   135 		
       
   136 		for (Enumeration e = priDataById.keys(); e.hasMoreElements();)
       
   137 		{
       
   138 			Integer id = (Integer)e.nextElement();
       
   139 			priStringById.put(id, this.getPriorityString((ArrayList)priDataById.get(id)));
       
   140 		}
       
   141 		
       
   142 		priDataById.clear();
       
   143 		
       
   144 
       
   145 		Enumeration e = PluginInitialiser.getPluginInstances("com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener"); //$NON-NLS-1$
       
   146 		if (e != null)
       
   147 		{
       
   148 			Event event = new Event();
       
   149 			event.data = priStringById;	
       
   150 
       
   151 			while (e.hasMoreElements())
       
   152 			{
       
   153 				IEventListener plugin = (IEventListener)e.nextElement();
       
   154 
       
   155 				plugin.receiveEvent("priority_init", event); //$NON-NLS-1$
       
   156 			}
       
   157 		}
       
   158 		
       
   159 	  	System.out.println(Messages.getString("PriorityPlugin.priorityTraceParsed")); //$NON-NLS-1$
       
   160 	}
       
   161 
       
   162 	public String getTraceName() {
       
   163 		return "Priority"; //$NON-NLS-1$
       
   164 	}
       
   165 
       
   166 	public int getTraceId() {
       
   167 		return 4;	//same id than MEM trace has because PRI trace is parsed from a MEM trace file
       
   168 	}
       
   169 
       
   170 	public ParsedTraceData parseTraceFile(File file /*, ProgressBar progressBar*/) throws Exception 
       
   171 	{
       
   172 		 try
       
   173 	        {
       
   174 	            PriTraceParser priParser;
       
   175 	            priParser = new PriTraceParser(file);
       
   176 	            return priParser.parse(file);
       
   177 	        } catch (Exception e)
       
   178 	        {
       
   179 	            e.printStackTrace();
       
   180 	            throw e;
       
   181 	        }
       
   182 	}
       
   183 	
       
   184 	private String getPriorityString(ArrayList priorityList)
       
   185 	{
       
   186 		if (priorityList == null || priorityList.size() == 0)
       
   187 		{
       
   188 			return Messages.getString("PriorityPlugin.unsolvedPriority"); //$NON-NLS-1$
       
   189 		}
       
   190 		else
       
   191 		{
       
   192 			String priorityString = ""; //$NON-NLS-1$
       
   193 			int priority = -1;
       
   194 			for (Iterator i = priorityList.iterator(); i.hasNext(); )
       
   195 			{
       
   196 				PriSample sample = (PriSample)i.next();
       
   197 				if (priority != sample.priority)
       
   198 				{
       
   199 					priority = sample.priority;
       
   200 					priorityString += Messages.getString("PriorityPlugin.priorityString1") + format.format(priority) + Messages.getString("PriorityPlugin.priorityString2") + sample.sampleNum/1000.0 + Messages.getString("PriorityPlugin.priorityString3"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
   201 				}
       
   202 			}
       
   203 			return priorityString;
       
   204 		}
       
   205 	}
       
   206 
       
   207 	public Hashtable<Integer,Object> getSummaryTable(double startTime, double endTime) 
       
   208 	{
       
   209 		PriThread[] pts = (PriThread[])trace.getThreads();
       
   210 		Hashtable<Integer,String> tmpTable = new Hashtable<Integer,String>();
       
   211 		for (int i = 0; i < pts.length; i++)
       
   212 		{
       
   213 			tmpTable.put(pts[i].threadId, pts[i].processName + "::" + pts[i].threadName + "_" + pts[i].threadId);   //$NON-NLS-1$ //$NON-NLS-2$
       
   214 		}
       
   215 		if (this.priStringById != null)
       
   216 		{
       
   217 			Enumeration<Integer> e = priStringById.keys();
       
   218 			Hashtable<Integer,Object> summary = new Hashtable<Integer,Object>();
       
   219 			while (e.hasMoreElements())
       
   220 			{
       
   221 				Vector<Object> data = new Vector<Object>();
       
   222 				Integer key = e.nextElement();
       
   223 				data.add(tmpTable.get(key));
       
   224 				data.add(((String)priStringById.get(key)).trim());
       
   225 				summary.put(key, data);
       
   226 			}
       
   227 			return summary;
       
   228 		}
       
   229 		return null;
       
   230 	}
       
   231 
       
   232 	public String getGeneralInfo() 
       
   233 	{
       
   234 		return null;
       
   235 	}
       
   236 
       
   237 	public ArrayList<String> getColumnNames() 
       
   238 	{
       
   239 		ArrayList<String> names = new ArrayList<String>();
       
   240 		names.add(Messages.getString("PriorityPlugin.thread")); //$NON-NLS-1$
       
   241 		names.add(Messages.getString("PriorityPlugin.priority")); //$NON-NLS-1$
       
   242 		return names;
       
   243 	}
       
   244 	
       
   245 	public ArrayList<Boolean> getColumnSortTypes() 
       
   246 	{
       
   247 		ArrayList<Boolean> sortTypes = new ArrayList<Boolean>();
       
   248 		sortTypes.add(SORT_BY_NAME);
       
   249 		sortTypes.add(SORT_BY_NUMBER);
       
   250 		return sortTypes;
       
   251 	}
       
   252 
       
   253 	public String getActiveInfo(Object arg0, double startTime, double endTime) {
       
   254 		return null;
       
   255 	}
       
   256 
       
   257 	public MenuManager getReportGeneratorManager() {
       
   258 		return null;
       
   259 	}
       
   260 
       
   261 	/**
       
   262 	 * This method is called upon plug-in activation
       
   263 	 */
       
   264 	public void start(BundleContext context) throws Exception {
       
   265 		super.start(context);
       
   266 	}
       
   267 
       
   268 	/**
       
   269 	 * This method is called when the plug-in is stopped
       
   270 	 */
       
   271 	public void stop(BundleContext context) throws Exception {
       
   272 		super.stop(context);
       
   273 		setPlugin(null);
       
   274 	}
       
   275 
       
   276 	/**
       
   277 	 * Returns the shared instance.
       
   278 	 */
       
   279 	public static PriorityPlugin getDefault() {
       
   280 		return plugin;
       
   281 	}
       
   282 
       
   283 	/**
       
   284 	 * Returns an image descriptor for the image file at the given
       
   285 	 * plug-in relative path.
       
   286 	 *
       
   287 	 * @param path the path
       
   288 	 * @return the image descriptor
       
   289 	 */
       
   290 	public static ImageDescriptor getImageDescriptor(String path) {
       
   291 		return AbstractPiPlugin.imageDescriptorFromPlugin("com.nokia.carbide.cpp.pi.priority", path); //$NON-NLS-1$
       
   292 	}
       
   293 }