sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.priority2/src/com/nokia/carbide/cpp/pi/priority2/Priority2Plugin.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.priority2;
       
    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 activator class controls the plug-in life cycle
       
    46  */
       
    47 public class Priority2Plugin extends AbstractPiPlugin
       
    48 		implements ITrace, IClassReplacer, IReportable {
       
    49 
       
    50 	// The shared instance
       
    51 	private static Priority2Plugin plugin;
       
    52 	
       
    53 	private static DecimalFormat format = new DecimalFormat(Messages.getString("Priority2Plugin.zeroFormat")); //$NON-NLS-1$
       
    54 	
       
    55 	private NewPriTrace trace = null;
       
    56 	private Hashtable<Integer,String> priStringById;
       
    57 
       
    58 	private static void setPlugin(Priority2Plugin newPlugin)
       
    59 	{
       
    60 		plugin = newPlugin;
       
    61 	}
       
    62 
       
    63 	/**
       
    64 	 * The constructor
       
    65 	 */
       
    66 	public Priority2Plugin() {
       
    67 		setPlugin(this);
       
    68 	}
       
    69 
       
    70 	public Class getReplacedClass(String className)
       
    71 	{
       
    72 		if (   className.indexOf("com.nokia.carbide.cpp.pi.priority2.NewPriSample") != -1 //$NON-NLS-1$
       
    73 			|| className.indexOf("com.nokia.carbide.pi.priority2.NewPriSample") != -1 //$NON-NLS-1$
       
    74 			|| className.indexOf("fi.vtt.bappea.newPriTracePlugin.NewPriSample") != -1) //$NON-NLS-1$
       
    75 		{
       
    76 			return NewPriSample.class;
       
    77 		}
       
    78 		else if (   className.indexOf("[Lcom.nokia.carbide.cpp.pi.priority2.NewPriThread") != -1 //$NON-NLS-1$
       
    79 				 || className.indexOf("[Lcom.nokia.carbide.pi.priority2.NewPriThread") != -1 //$NON-NLS-1$
       
    80 				 || className.indexOf("[Lfi.vtt.bappea.newPriTracePlugin.NewPriThread") != -1) //$NON-NLS-1$
       
    81 		{
       
    82 			return NewPriThread[].class;
       
    83 		}
       
    84 		else if (   className.indexOf("com.nokia.carbide.cpp.pi.priority2.NewPriThread") != -1 //$NON-NLS-1$
       
    85 				 || className.indexOf("com.nokia.carbide.pi.priority2.NewPriThread") != -1 //$NON-NLS-1$
       
    86 				 || className.indexOf("fi.vtt.bappea.newPriTracePlugin.NewPriThread") != -1) //$NON-NLS-1$
       
    87 		{
       
    88 			return NewPriThread.class;
       
    89 		}
       
    90 		else if (   className.indexOf("com.nokia.carbide.cpp.pi.priority2.NewPriTrace") != -1 //$NON-NLS-1$
       
    91 				 || className.indexOf("com.nokia.carbide.pi.priority2.NewPriTrace") != -1 //$NON-NLS-1$
       
    92 				 || className.indexOf("fi.vtt.bappea.newPriTracePlugin.NewPriTrace") != -1) //$NON-NLS-1$
       
    93 		{
       
    94 			return NewPriTrace.class;
       
    95 		}
       
    96 		else return null;
       
    97 	}
       
    98 	
       
    99 	public Class getTraceClass() 
       
   100 	{
       
   101 		return NewPriTrace.class;
       
   102 	}
       
   103 
       
   104 	public void initialiseTrace(GenericTrace trace /*, ProfileVisualiser pv*/) 
       
   105 	{
       
   106 		if (trace == null)
       
   107 			return;
       
   108 		this.trace = (NewPriTrace)trace;
       
   109 
       
   110 	  	GenericThread[] threads =  this.trace.getThreads();
       
   111 		int threadCount = threads.length;
       
   112 
       
   113 		ArrayList<NewPriSample>[] priData = new ArrayList[threadCount];
       
   114 		Hashtable<Integer,ArrayList<NewPriSample>> priDataById = new Hashtable<Integer,ArrayList<NewPriSample>>();
       
   115 		priStringById = new Hashtable<Integer,String>();
       
   116 
       
   117 		//create priority data structure and inserts thread IDs into hashtable
       
   118 		for (int i = 0; i < threadCount; i++)
       
   119 		{
       
   120 			priData[i] = new ArrayList<NewPriSample>();
       
   121 			priDataById.put(threads[i].threadId, priData[i]);
       
   122 		}
       
   123 		
       
   124 		//insert priority data into hashtable based on thread id
       
   125 		for (Enumeration e = this.trace.getSamples(); e.hasMoreElements();)
       
   126 		{
       
   127 			NewPriSample tmp = (NewPriSample) e.nextElement();
       
   128 			priDataById.get(tmp.thread.threadId).add(tmp);
       
   129 		}
       
   130 		
       
   131 		for (Enumeration e = priDataById.keys(); e.hasMoreElements();)
       
   132 		{
       
   133 			Integer id = (Integer)e.nextElement();
       
   134 			priStringById.put(id, this.getPriorityString((ArrayList)priDataById.get(id)));
       
   135 		}
       
   136 		
       
   137 		priDataById.clear();
       
   138 
       
   139 		Enumeration e = PluginInitialiser.getPluginInstances("com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener"); //$NON-NLS-1$
       
   140 		if (e != null)
       
   141 		{
       
   142 			Event event = new Event();
       
   143 			event.data = priStringById;	
       
   144 
       
   145 			while (e.hasMoreElements())
       
   146 			{
       
   147 				IEventListener plugin = (IEventListener)e.nextElement();
       
   148 
       
   149 				plugin.receiveEvent("priority_init", event); //$NON-NLS-1$
       
   150 			}
       
   151 		}
       
   152 		
       
   153 	  	System.out.println(Messages.getString("Priority2Plugin.priorityTraceParsed")); //$NON-NLS-1$
       
   154 	}
       
   155 
       
   156 	public String getTraceName() {
       
   157 		return "Priority"; //$NON-NLS-1$
       
   158 	}
       
   159 
       
   160 	public int getTraceId() {
       
   161 		return 5;
       
   162 	}
       
   163 
       
   164 
       
   165 	public ParsedTraceData parseTraceFile(File file /*, ProgressBar progressBar*/) throws Exception 
       
   166 	{
       
   167 		 try
       
   168 	        {
       
   169 	            NewPriTraceParser priParser;
       
   170 	            priParser = new NewPriTraceParser(file);
       
   171 	            return priParser.parse(file);
       
   172 	        } catch (Exception e)
       
   173 	        {
       
   174 	            e.printStackTrace();
       
   175 	            throw e;
       
   176 	        }
       
   177 	}
       
   178 	
       
   179 	private String getPriorityString(ArrayList priorityList)
       
   180 	{
       
   181 		if (priorityList == null || priorityList.size() == 0)
       
   182 		{
       
   183 			return Messages.getString("Priority2Plugin.unsolvedPriority"); //$NON-NLS-1$
       
   184 		}
       
   185 		else
       
   186 		{
       
   187 			String priorityString = ""; //$NON-NLS-1$
       
   188 			int priority = -1;
       
   189 			for (Iterator i = priorityList.iterator(); i.hasNext(); )
       
   190 			{
       
   191 				NewPriSample sample = (NewPriSample)i.next();
       
   192 				if (priority != sample.priority)
       
   193 				{
       
   194 					priority = sample.priority;
       
   195 					priorityString += Messages.getString("Priority2Plugin.priorityString1") + format.format(priority) + Messages.getString("Priority2Plugin.priorityString2") + sample.sampleNum/1000.0 + Messages.getString("Priority2Plugin.priorityString3"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
   196 				}
       
   197 			}
       
   198 			return priorityString;
       
   199 		}
       
   200 	}
       
   201 
       
   202 	public Hashtable<Integer,Object> getSummaryTable(double startTime, double endTime) 
       
   203 	{
       
   204 		NewPriThread[] pts = (NewPriThread[])trace.getThreads();
       
   205 		Hashtable<Integer,String> tmpTable = new Hashtable<Integer,String>();
       
   206 		for (int i = 0; i < pts.length; i++)
       
   207 		{
       
   208 			tmpTable.put(pts[i].threadId, pts[i].processName + "::" + pts[i].threadName + "_" + pts[i].threadId);   //$NON-NLS-1$ //$NON-NLS-2$
       
   209 		}
       
   210 		if (this.priStringById != null)
       
   211 		{
       
   212 			Enumeration<Integer> e = priStringById.keys();
       
   213 			Hashtable<Integer,Object> summary = new Hashtable<Integer,Object>();
       
   214 			while (e.hasMoreElements())
       
   215 			{
       
   216 				Vector<Object> data = new Vector<Object>();
       
   217 				Integer key = e.nextElement();
       
   218 				data.add(tmpTable.get(key));
       
   219 				data.add(((String)priStringById.get(key)).trim());
       
   220 				summary.put(key, data);
       
   221 			}
       
   222 			return summary;
       
   223 		}
       
   224 		return null;
       
   225 	}
       
   226 
       
   227 	public String getGeneralInfo() 
       
   228 	{
       
   229 		return null;
       
   230 	}
       
   231 
       
   232 	public ArrayList<String> getColumnNames() 
       
   233 	{
       
   234 		ArrayList<String> names = new ArrayList<String>();
       
   235 		names.add(Messages.getString("Priority2Plugin.thread")); //$NON-NLS-1$
       
   236 		names.add(Messages.getString("Priority2Plugin.priority")); //$NON-NLS-1$
       
   237 		return names;
       
   238 	}
       
   239 	
       
   240 	public ArrayList<Boolean> getColumnSortTypes() 
       
   241 	{
       
   242 		ArrayList<Boolean> sortTypes = new ArrayList<Boolean>();
       
   243 		sortTypes.add(SORT_BY_NAME);
       
   244 		sortTypes.add(SORT_BY_NUMBER);
       
   245 		return sortTypes;
       
   246 	}
       
   247 
       
   248 	public String getActiveInfo(Object arg0, double startTime, double endTime) {
       
   249 		return null;
       
   250 	}
       
   251 
       
   252 	public MenuManager getReportGeneratorManager() {
       
   253 		return null;
       
   254 	}
       
   255 
       
   256 	/**
       
   257 	 * This method is called upon plug-in activation
       
   258 	 */
       
   259 	public void start(BundleContext context) throws Exception {
       
   260 		super.start(context);
       
   261 	}
       
   262 
       
   263 	/**
       
   264 	 * This method is called when the plug-in is stopped
       
   265 	 */
       
   266 	public void stop(BundleContext context) throws Exception {
       
   267 		super.stop(context);
       
   268 		setPlugin(null);
       
   269 	}
       
   270 
       
   271 	/**
       
   272 	 * Returns the shared instance
       
   273 	 */
       
   274 	public static Priority2Plugin getDefault() {
       
   275 		return plugin;
       
   276 	}
       
   277 
       
   278 	/**
       
   279 	 * Returns an image descriptor for the image file at the given
       
   280 	 * plug-in relative path.
       
   281 	 *
       
   282 	 * @param path the path
       
   283 	 * @return the image descriptor
       
   284 	 */
       
   285 	public static ImageDescriptor getImageDescriptor(String path) {
       
   286 		return AbstractPiPlugin.imageDescriptorFromPlugin("com.nokia.carbide.cpp.pi.priority2", path); //$NON-NLS-1$
       
   287 	}
       
   288 }