sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.call/src/com/nokia/carbide/cpp/pi/call/GfcFunctionItem.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.call;
       
    19 
       
    20 import java.io.Serializable;
       
    21 import java.util.ArrayList;
       
    22 import java.util.Enumeration;
       
    23 import java.util.Hashtable;
       
    24 import java.util.Vector;
       
    25 
       
    26 public class GfcFunctionItem implements Serializable
       
    27 {
       
    28 	private static final long serialVersionUID = -8082342532211331507L;
       
    29 	
       
    30 	// this function has been called by these functions
       
    31 	private Hashtable callerData;
       
    32 
       
    33 	// this function has called these functions
       
    34 	private Hashtable calleeData;
       
    35 
       
    36 	public long address;
       
    37 	public String name;
       
    38 	public String dllName;
       
    39 	private int totalCallerAmount; // really number of times this function is called
       
    40 	private int totalCalleeAmount;	// really number of times this function is the caller
       
    41 	private boolean percentageUpdated;
       
    42 
       
    43 	private double recursiveLoad = 0;
       
    44 	private double accumulatedRecursiveLoad = 0;
       
    45 
       
    46 	private double absoluteTotalPercentage = 0;
       
    47 	private double absoluteCallerPercentage = 0;
       
    48 	private double recursiveTotalPercentage = 0;
       
    49 	private double recursiveSampleCount = 0;
       
    50 
       
    51 	private int storedPercentageStart = -1;
       
    52 	private int storedPercentageEnd = -1;
       
    53 
       
    54 	private boolean isSymbolParsed;
       
    55 
       
    56 	public GfcFunctionItem(String name, long address, String dllName, boolean isSymbolParsed)
       
    57 	{
       
    58 		this.isSymbolParsed  = isSymbolParsed;
       
    59 		this.address         = address;
       
    60 		this.name            = name;
       
    61 		this.dllName         = dllName;
       
    62 		this.totalCallerAmount   = 0;
       
    63 		this.percentageUpdated = false;
       
    64 		this.callerData = new Hashtable<Long,GfcFunctionItemData>();
       
    65 		this.calleeData = new Hashtable<Long,GfcFunctionItemData>();
       
    66 	}
       
    67 
       
    68 	public boolean isSymbolParsed()
       
    69 	{
       
    70 		return this.isSymbolParsed;
       
    71 	}
       
    72 
       
    73 	public int isCalledCount() {
       
    74 		return this.totalCallerAmount;
       
    75 	}
       
    76 	
       
    77 	public int isCallerCount() {
       
    78 		return this.totalCalleeAmount;
       
    79 	}
       
    80 	
       
    81 	public double getAbsoluteTotalPercentage(int start,int end)
       
    82 	{
       
    83 		if (this.storedPercentageStart == start && this.storedPercentageEnd == end)
       
    84 		{
       
    85 			return this.absoluteTotalPercentage;
       
    86 		}
       
    87 		else return -1;
       
    88 	}
       
    89 
       
    90 	public double getAbsoluteCallerPercentage(int start,int end)
       
    91 	{
       
    92 		if (this.storedPercentageStart == start && this.storedPercentageEnd == end)
       
    93 		{
       
    94 			return this.absoluteCallerPercentage;
       
    95 		}
       
    96 		else return -1;
       
    97 	}
       
    98 
       
    99 	public double getRecursiveCallerPercentage(int start,int end)
       
   100 	{
       
   101 		if (this.storedPercentageStart == start && this.storedPercentageEnd == end)
       
   102 		{
       
   103 			return this.recursiveTotalPercentage;
       
   104 		}
       
   105 		else return -1;
       
   106 	}
       
   107 
       
   108 	public void storePercents(double total, double caller, double recursive, int start, int end)
       
   109 	{
       
   110 		this.absoluteCallerPercentage = caller;
       
   111 		this.absoluteTotalPercentage  = total;
       
   112 		this.recursiveTotalPercentage = recursive;
       
   113 		this.storedPercentageStart    = start;
       
   114 		this.storedPercentageEnd      = end;
       
   115 	}
       
   116 
       
   117 	public double getRecursiveCallerSamples(int recursion, double cumulativeValue, GfcFunctionItem exclude)
       
   118 	{
       
   119 		if (recursion > 10)
       
   120 			return 0;
       
   121 		recursion++;
       
   122 
       
   123 		GfcFunctionItem[] callees = this.getCalleeList();
       
   124 		Double[] calleePerc = this.getCalleePercentages();
       
   125 
       
   126 		double callActivity =
       
   127 			this.countSamplesWhereThisFunctionIsTheCaller() /
       
   128 			(this.countSamplesThatCallThisFunction() +
       
   129 			 this.countSamplesWhereThisFunctionIsTheCaller());
       
   130 
       
   131 		if (this.recursiveSampleCount == 0)
       
   132 		{
       
   133 			for (int i = 0; i < callees.length; i++)
       
   134 			{
       
   135 				GfcFunctionItem item = callees[i];
       
   136 				Double itemPerc = calleePerc[i];
       
   137 
       
   138 				if (item != this)
       
   139 				{
       
   140 					// get the direct value this function causes
       
   141 					this.recursiveSampleCount += (itemPerc.doubleValue() / 100) * item.countSamplesThatCallThisFunction();
       
   142 
       
   143 					// recursively get the values this function calls:
       
   144 					// itemPerc = percentage of the function in this functions call share (that sums 100%)
       
   145 					// callActivity = factor of samples related to this function, that indicate that this
       
   146 					// function has called another function (has been the caller). Call activity is
       
   147 					// 100% if all the samples related to this function are such that they have
       
   148 					// the pointer to this function only in the link register
       
   149 
       
   150 					this.recursiveSampleCount += (itemPerc.doubleValue() * callActivity / 100) *
       
   151 													item.getRecursiveCallerSamples(recursion, cumulativeValue, this);
       
   152 				}
       
   153 			}
       
   154 		}
       
   155 
       
   156 		cumulativeValue += this.recursiveSampleCount;
       
   157 
       
   158 		//System.out.println("rec "+recursion+" cum"+cumulativeValue);
       
   159 		return cumulativeValue;
       
   160 	}
       
   161 
       
   162 	public void addRecursiveLoad(double samples)
       
   163 	{
       
   164 		this.recursiveLoad += samples;
       
   165 		this.accumulatedRecursiveLoad += samples;
       
   166 	}
       
   167 
       
   168 	public void setRecursiveLoad(double load)
       
   169 	{
       
   170 		this.recursiveLoad = load;
       
   171 	}
       
   172 
       
   173 	public double getRecursiveLoad()
       
   174 	{
       
   175 		return this.recursiveLoad;
       
   176 	}
       
   177 
       
   178 	public double getAccumulatedLoad()
       
   179 	{
       
   180 		return this.accumulatedRecursiveLoad;
       
   181 	}
       
   182 
       
   183 	public int countSamplesThatCallThisFunction()
       
   184 	{
       
   185 		int callCount = 0;
       
   186 		Enumeration<GfcFunctionItemData> callerEnum = this.callerData.elements();
       
   187 		while (callerEnum.hasMoreElements())
       
   188 		{
       
   189 			GfcFunctionItemData data = callerEnum.nextElement();
       
   190 			callCount += data.callTimes.size();
       
   191 		}
       
   192 		return callCount;
       
   193 	}
       
   194 
       
   195 	public ArrayList getSamplesThatCallThisFunction()
       
   196 	{
       
   197 		ArrayList callTimes = new ArrayList();
       
   198 		Enumeration<GfcFunctionItemData> callerEnum = this.callerData.elements();
       
   199 		while (callerEnum.hasMoreElements())
       
   200 		{
       
   201 			GfcFunctionItemData data = callerEnum.nextElement();
       
   202 			callTimes.addAll(data.callTimes);
       
   203 		}
       
   204 		return callTimes;
       
   205 	}
       
   206 
       
   207 	public int countSamplesWhereThisFunctionIsTheCaller()
       
   208 	{
       
   209 		int callCount = 0;
       
   210 		Enumeration<GfcFunctionItemData> calleeEnum = this.calleeData.elements();
       
   211 		while (calleeEnum.hasMoreElements())
       
   212 		{
       
   213 			GfcFunctionItemData data = calleeEnum.nextElement();
       
   214 			callCount += data.callTimes.size();
       
   215 		}
       
   216 		return callCount;
       
   217 	}
       
   218 
       
   219 	public ArrayList getSamplesWhereThisFunctionIsTheCaller()
       
   220 	{
       
   221 		ArrayList callTimes = new ArrayList();
       
   222 		Enumeration<GfcFunctionItemData> calleeEnum = this.calleeData.elements();
       
   223 		while (calleeEnum.hasMoreElements())
       
   224 		{
       
   225 			GfcFunctionItemData data = calleeEnum.nextElement();
       
   226 			callTimes.addAll(data.callTimes);
       
   227 		}
       
   228 		return callTimes;
       
   229 	}
       
   230 
       
   231 	public static class GfcFunctionItemData implements Serializable
       
   232 	{
       
   233 		private static final long serialVersionUID = -8082342532211331507L;
       
   234 		public GfcFunctionItem function;
       
   235 		public Double percentage;
       
   236 		// size of the vector indicates the amount
       
   237 		// of times this function has been called
       
   238 		public Vector callTimes = new Vector();
       
   239 	}
       
   240 
       
   241 	// this function has been called but the caller is unknown
       
   242 	public void addUnknownCaller(Integer callTime)
       
   243 	{
       
   244 		// store unknown callers with an address -1
       
   245 		if (!this.callerData.containsKey(new Long(-1)) )
       
   246 		{
       
   247 			GfcFunctionItemData newData = new GfcFunctionItemData();
       
   248 			newData.function = null;
       
   249 			newData.percentage = new Double(0);
       
   250 			newData.callTimes.add(callTime);
       
   251 
       
   252 			// add a new caller
       
   253 			this.callerData.put(new Long(-1),newData);
       
   254 		}
       
   255 		else
       
   256 		{
       
   257 			// just add the call count of an existing caller
       
   258 			GfcFunctionItemData oldData = (GfcFunctionItemData)this.callerData.get(new Long(-1));
       
   259 			this.totalCallerAmount++;
       
   260 			oldData.callTimes.add(callTime);
       
   261 		}
       
   262 
       
   263 		this.percentageUpdated = false;
       
   264 	}
       
   265 
       
   266 	// this function has been called
       
   267 	public void addCaller(GfcFunctionItem caller, Integer callTime)
       
   268 	{
       
   269 		Long address = new Long(caller.address);
       
   270 		if (!this.callerData.containsKey(address))
       
   271 		{
       
   272 			GfcFunctionItemData newData = new GfcFunctionItemData();
       
   273 			newData.function = caller;
       
   274 			newData.percentage = new Double(0);
       
   275 			newData.callTimes.add(callTime);
       
   276 
       
   277 			this.totalCallerAmount++;
       
   278 
       
   279 			// add a new caller
       
   280 			this.callerData.put(address,newData);
       
   281 		}
       
   282 		else
       
   283 		{
       
   284 			// just add the call count of an existing caller
       
   285 			GfcFunctionItemData oldData = (GfcFunctionItemData)this.callerData.get(address);
       
   286 			this.totalCallerAmount++;
       
   287 			oldData.callTimes.add(callTime);
       
   288 		}
       
   289 
       
   290 		this.percentageUpdated = false;
       
   291 	}
       
   292 
       
   293 	// this function has called another function
       
   294 	public void addCallee(GfcFunctionItem callee, Integer callTime)
       
   295 	{
       
   296 		Long address = new Long(callee.address);
       
   297 		if (!this.calleeData.containsKey(address))
       
   298 		{
       
   299 			GfcFunctionItemData newData = new GfcFunctionItemData();
       
   300 			newData.function = callee;
       
   301 			newData.percentage = new Double(0);
       
   302 			newData.callTimes.add(callTime);
       
   303 
       
   304 			this.totalCalleeAmount++;
       
   305 
       
   306 			// add the new callee
       
   307 			this.calleeData.put(address,newData);
       
   308 		}
       
   309 		else
       
   310 		{
       
   311 			// just add the call count of an existing callee
       
   312 			GfcFunctionItemData oldData = (GfcFunctionItemData)this.calleeData.get(address);
       
   313 			this.totalCalleeAmount++;
       
   314 			oldData.callTimes.add(callTime);
       
   315 		}
       
   316 
       
   317 		this.percentageUpdated = false;
       
   318 	}
       
   319 
       
   320 	public GfcFunctionItem[] getCallerList()
       
   321 	{
       
   322 		GfcFunctionItem[] functionList = new GfcFunctionItem[callerData.size()];
       
   323 		Enumeration callEnum = this.callerData.elements();
       
   324 		int i = 0;
       
   325 
       
   326 		while (callEnum.hasMoreElements())
       
   327 		{
       
   328 			GfcFunctionItemData di = (GfcFunctionItemData)callEnum.nextElement();
       
   329 			if (di.function != null)
       
   330 			{
       
   331 				functionList[i] = di.function;
       
   332 			}
       
   333 			i++;
       
   334 		}
       
   335 		return functionList;
       
   336 	}
       
   337 
       
   338 	public Double[] getCallerPercentages()
       
   339 	{
       
   340 		calculatePercentages();
       
   341 
       
   342 		Double[] pList = new Double[callerData.size()];
       
   343 		Enumeration callEnum = this.callerData.elements();
       
   344 		int i = 0;
       
   345 		
       
   346 		while (callEnum.hasMoreElements())
       
   347 		{
       
   348 			GfcFunctionItemData di = (GfcFunctionItemData)callEnum.nextElement();
       
   349 			if (di.function != null)
       
   350 			{
       
   351 				pList[i] = di.percentage;
       
   352 			}
       
   353 			i++;
       
   354 		}
       
   355 		return pList;
       
   356 	}
       
   357 
       
   358 	public GfcFunctionItem[] getCalleeList()
       
   359 	{
       
   360 		GfcFunctionItem[] functionList = new GfcFunctionItem[calleeData.size()];
       
   361 		Enumeration callEnum = this.calleeData.elements();
       
   362 		int i = 0;
       
   363 
       
   364 		while (callEnum.hasMoreElements())
       
   365 		{
       
   366 			GfcFunctionItemData di = (GfcFunctionItemData)callEnum.nextElement();
       
   367 			functionList[i] = di.function;
       
   368 			i++;
       
   369 		}
       
   370 		return functionList;
       
   371 	}
       
   372 
       
   373 	public Double[] getCalleePercentages()
       
   374 	{
       
   375 		calculatePercentages();
       
   376 
       
   377 		Double[] pList = new Double[calleeData.size()];
       
   378 		Enumeration callEnum = this.calleeData.elements();
       
   379 		int i = 0;
       
   380 
       
   381 		while (callEnum.hasMoreElements())
       
   382 		{
       
   383 			GfcFunctionItemData di = (GfcFunctionItemData)callEnum.nextElement();
       
   384 			pList[i] = di.percentage;
       
   385 			i++;
       
   386 		}
       
   387 		return pList;
       
   388 	}
       
   389 
       
   390 	public String toString()
       
   391 	{
       
   392 		this.calculatePercentages();
       
   393 
       
   394 		String result = Messages.getString("GfcFunctionItem.calledBy1") + this.name + Messages.getString("GfcFunctionItem.calledBy2"); //$NON-NLS-1$ //$NON-NLS-2$
       
   395 		Enumeration callers = this.callerData.elements();
       
   396 
       
   397 		while (callers.hasMoreElements())
       
   398 		{
       
   399 			GfcFunctionItemData item = (GfcFunctionItemData)callers.nextElement();
       
   400 			result += Messages.getString("GfcFunctionItem.calledBy3") + item.percentage + Messages.getString("GfcFunctionItem.calledBy4") + item.function.name + Messages.getString("GfcFunctionItem.calledBy5") + item.function.dllName + Messages.getString("GfcFunctionItem.calledBy6"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
       
   401 		}
       
   402 
       
   403 		result += Messages.getString("GfcFunctionItem.calls1") + this.name + Messages.getString("GfcFunctionItem.calls2"); //$NON-NLS-1$ //$NON-NLS-2$
       
   404 
       
   405 		Enumeration callees = this.calleeData.elements();
       
   406 		while (callees.hasMoreElements())
       
   407 		{
       
   408 			GfcFunctionItemData item = (GfcFunctionItemData)callees.nextElement();
       
   409 			result += Messages.getString("GfcFunctionItem.calls3") + item.percentage + Messages.getString("GfcFunctionItem.calls4") + item.function.name + Messages.getString("GfcFunctionItem.calls5") + item.function.dllName + Messages.getString("GfcFunctionItem.calls6"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
       
   410 		}
       
   411 
       
   412 		result += Messages.getString("GfcFunctionItem.calls7"); //$NON-NLS-1$
       
   413 
       
   414 		return result;
       
   415 	}
       
   416 
       
   417 	private void calculatePercentages()
       
   418 	{
       
   419 		if (this.percentageUpdated == false)
       
   420 		{
       
   421 			Enumeration callEnum = this.callerData.elements();
       
   422 			while (callEnum.hasMoreElements())
       
   423 			{
       
   424 				GfcFunctionItemData di = (GfcFunctionItemData)callEnum.nextElement();
       
   425 				if (di.function != null)
       
   426 				{
       
   427 					di.percentage = new Double((double)((double)di.callTimes.size()/(double)this.totalCallerAmount)*100);
       
   428 				}
       
   429 			}
       
   430 
       
   431 			callEnum = this.calleeData.elements();
       
   432 			while (callEnum.hasMoreElements())
       
   433 			{
       
   434 				GfcFunctionItemData di = (GfcFunctionItemData)callEnum.nextElement();
       
   435 				if (di.function != null)
       
   436 				{
       
   437 					di.percentage = new Double((double)((double)di.callTimes.size()/(double)this.totalCalleeAmount)*100);
       
   438 				}
       
   439 			}
       
   440 
       
   441 			this.percentageUpdated = true;
       
   442 		}
       
   443 	}
       
   444 
       
   445 }