sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/engine/statistic/ProcessInfo.java
changeset 1 1050670c6980
child 6 f65f740e69f9
equal deleted inserted replaced
0:5ad7ad99af01 1:1050670c6980
       
     1 /*
       
     2  * Copyright (c) 2008-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 "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:  Definitions for the class ProcessInfo
       
    15  *
       
    16  */
       
    17 package com.nokia.s60tools.analyzetool.engine.statistic;
       
    18 
       
    19 import java.util.AbstractList;
       
    20 import java.util.ArrayList;
       
    21 import java.util.HashMap;
       
    22 import java.util.Hashtable;
       
    23 import java.util.List;
       
    24 import java.util.Map;
       
    25 
       
    26 import org.eclipse.core.runtime.IStatus;
       
    27 import com.nokia.s60tools.analyzetool.Activator;
       
    28 
       
    29 /**
       
    30  * Contains information of process
       
    31  * 
       
    32  * @author kihe
       
    33  * 
       
    34  */
       
    35 public class ProcessInfo {
       
    36 
       
    37 	/** List of allocations and frees sorted as they arrive in time */
       
    38 	AbstractList<BaseInfo> allocsFrees;
       
    39 
       
    40 	/** List of dll loads */
       
    41 	Hashtable<String, DllLoad> dllLoads;
       
    42 
       
    43 	/** Process id */
       
    44 	int processID;
       
    45 
       
    46 	/** Process Name */
       
    47 	String processName;
       
    48 
       
    49 	/** Process start time */
       
    50 	Long startTime;
       
    51 
       
    52 	/** Process start time */
       
    53 	Long endTime;
       
    54 
       
    55 	/** Trace data format version number */
       
    56 	int traceDataVersion = 1;
       
    57 
       
    58 	/** active allocations account */
       
    59 	private int allocCount = 0;
       
    60 	/** total memory consumed by this process at each event */
       
    61 	private int totalMemory = 0;
       
    62 	/** highest memory consumed by this process */
       
    63 	private int highestMemory =0;
       
    64 
       
    65 	/** Map of potential leaks; to track allocations not yet freed */
       
    66 	private Map<Long, List<AllocInfo>> potentialLeaksMap;
       
    67 
       
    68 	/**
       
    69 	 * Constructor
       
    70 	 */
       
    71 	public ProcessInfo() {
       
    72 		dllLoads = new Hashtable<String, DllLoad>();
       
    73 		allocsFrees = new ArrayList<BaseInfo>();
       
    74 		allocCount = 0;
       
    75 		potentialLeaksMap = new HashMap<Long, List<AllocInfo>>(); 
       
    76 	}
       
    77 
       
    78 	/**
       
    79 	 * Adds one memory allocation to the list.
       
    80 	 * 
       
    81 	 * @param oneInfo
       
    82 	 *            One memory allocation
       
    83 	 */
       
    84 	public void addOneAlloc(AllocInfo oneInfo) {
       
    85 		allocCount++;
       
    86 		totalMemory = totalMemory + oneInfo.getSizeInt();
       
    87 		oneInfo.setTotalMem(totalMemory);
       
    88 		if (totalMemory > highestMemory) {
       
    89 			highestMemory = totalMemory;
       
    90 		}
       
    91 		allocsFrees.add(oneInfo);
       
    92 		//add this alloc to the potential leaks map
       
    93 		Long addr = oneInfo.getMemoryAddress();
       
    94 		List<AllocInfo> allocsSameAddr = potentialLeaksMap.get(addr);
       
    95 		if (allocsSameAddr == null){
       
    96 			allocsSameAddr = new ArrayList<AllocInfo>();
       
    97 			potentialLeaksMap.put(addr, allocsSameAddr);
       
    98 		}
       
    99 		allocsSameAddr.add(oneInfo);
       
   100 	}
       
   101 
       
   102 	/**
       
   103 	 * Adds one dll load to the list.
       
   104 	 * 
       
   105 	 * @param dllLoad
       
   106 	 *            One dll load
       
   107 	 */
       
   108 	public void addOneDllLoad(DllLoad dllLoad) {
       
   109 		dllLoads.put(dllLoad.getName(), dllLoad);
       
   110 	}
       
   111 
       
   112 	/**
       
   113 	 * Updates memory leaks list by given free information
       
   114 	 * 
       
   115 	 * @param info
       
   116 	 *            Which memory allocation to be deallocated.
       
   117 	 */
       
   118 	public void free(FreeInfo info) {
       
   119 
       
   120 		int freeSize = 0;
       
   121 		
       
   122 		//remove allocs with the same address from the potential leaks map
       
   123 		Long freeAddr = info.getMemoryAddress();
       
   124 		List<AllocInfo> allocsSameAddr = potentialLeaksMap.remove(freeAddr);
       
   125 		if (allocsSameAddr != null && allocsSameAddr.size()>0){
       
   126 			for(AllocInfo allocInfo : allocsSameAddr){
       
   127 				allocInfo.setFreedBy(info);
       
   128 				info.addFreedAlloc(allocInfo);
       
   129 				allocCount--;
       
   130 				int thisFreedSize = allocInfo.getSizeInt();
       
   131 				freeSize = freeSize + thisFreedSize;
       
   132 				totalMemory = totalMemory - thisFreedSize;
       
   133 			}			
       
   134 		}
       
   135 
       
   136 		info.setSizeInt(freeSize);
       
   137 		info.setTotalMem(totalMemory);
       
   138 		
       
   139 		if (info.getTime() == null || info.getTime() == 0 ) {
       
   140 			// support old format
       
   141 			//set time as last operation time or start time.
       
   142 			Long time = getPreviousTime();
       
   143 			if (time == null || time == -1L) {
       
   144 				Activator.getDefault().log(IStatus.WARNING, String.format("AnalyzeTool encountered a process = %s, which starts with FREE.", processID), null);
       
   145 				time = startTime;
       
   146 			}
       
   147 			info.setTime(time);
       
   148 		}
       
   149 		
       
   150 		allocsFrees.add(info);
       
   151 	}
       
   152 
       
   153 	/**
       
   154 	 * Returns the timestamp of the last memory operation
       
   155 	 * @return the timestamp of the last memory operation, or -1 if there
       
   156 	 * are no memory operations
       
   157 	 */
       
   158 	private Long getPreviousTime() {
       
   159 		Long time = -1L;
       
   160 		if (!allocsFrees.isEmpty()) {
       
   161 			time = (allocsFrees.get(allocsFrees.size() -1)).getTime();
       
   162 		}
       
   163 		return time;
       
   164 	}
       
   165 
       
   166 	/**
       
   167 	 * Returns list of allocations
       
   168 	 * 
       
   169 	 * @return List of allocations
       
   170 	 */
       
   171 	public AbstractList<AllocInfo> getAllocs() {
       
   172 		AbstractList<AllocInfo> allocs = new ArrayList<AllocInfo>();
       
   173 		for (BaseInfo alloc : allocsFrees) {
       
   174 			if (alloc instanceof AllocInfo) {
       
   175 				allocs.add((AllocInfo) alloc);
       
   176 			}
       
   177 		}
       
   178 		return allocs;
       
   179 	}
       
   180 
       
   181 	/**
       
   182 	 * Returns list of dll loads
       
   183 	 * 
       
   184 	 * @return List of dll loads
       
   185 	 */
       
   186 	public Hashtable<String, DllLoad> getDllLoads() {
       
   187 		return dllLoads;
       
   188 	}
       
   189 
       
   190 	/**
       
   191 	 * Returns list of memory leak from this process.
       
   192 	 * 
       
   193 	 * @return List of memory leaks.
       
   194 	 */
       
   195 	public AbstractList<AllocInfo> getMemLeaks() {
       
   196 		AbstractList<AllocInfo> leaks = new ArrayList<AllocInfo>();
       
   197 		for (List<AllocInfo> potLeaks : potentialLeaksMap.values()) {
       
   198 			leaks.addAll(potLeaks);
       
   199 		}
       
   200 		return leaks;
       
   201 	}
       
   202 
       
   203 	/**
       
   204 	 * Returns the number of memory leaks of this process.
       
   205 	 * 
       
   206 	 * @return number of memory leaks.
       
   207 	 */
       
   208 	public int getMemLeaksNumber() {
       
   209 		int count = 0;
       
   210 		for (List<AllocInfo> potLeaks : potentialLeaksMap.values()) {
       
   211 			count += potLeaks.size();
       
   212 		}
       
   213 		return count;
       
   214 	}
       
   215 
       
   216 	/** 
       
   217 	 * get list of frees
       
   218 	 * @return List of FreeInfo
       
   219 	 */
       
   220 	public AbstractList<FreeInfo> getFrees() {
       
   221 		AbstractList<FreeInfo> frees = new ArrayList<FreeInfo>();
       
   222 		for (BaseInfo alloc : allocsFrees) {
       
   223 			if (alloc instanceof FreeInfo) {
       
   224 				frees.add((FreeInfo) alloc);
       
   225 			}
       
   226 		}
       
   227 		return frees;
       
   228 	}
       
   229 
       
   230 	/**
       
   231 	 * Returns process id
       
   232 	 * 
       
   233 	 * @return Process id
       
   234 	 */
       
   235 	public int getProcessID() {
       
   236 		return processID;
       
   237 	}
       
   238 
       
   239 	/**
       
   240 	 * Returns process start time
       
   241 	 * 
       
   242 	 * @return Process start time
       
   243 	 */
       
   244 	public Long getStartTime() {
       
   245 		return startTime;
       
   246 	}
       
   247 
       
   248 	/**
       
   249 	 * Returns trace data version number
       
   250 	 * 
       
   251 	 * @return Trace data version number
       
   252 	 */
       
   253 	public int getTraceDataVersion() {
       
   254 		return traceDataVersion;
       
   255 	}
       
   256 
       
   257 	/**
       
   258 	 * Sets process id
       
   259 	 * 
       
   260 	 * @param newProcessID
       
   261 	 *            Process id
       
   262 	 */
       
   263 	public void setProcessID(int newProcessID) {
       
   264 		this.processID = newProcessID;
       
   265 	}
       
   266 
       
   267 	/**
       
   268 	 * Sets process start time
       
   269 	 * 
       
   270 	 * @param newTime
       
   271 	 *            Process start time
       
   272 	 */
       
   273 	public void setStartTime(String newTime) {
       
   274 		Long lValue = Long.parseLong(newTime, 16);
       
   275 		this.startTime = lValue;
       
   276 	}
       
   277 
       
   278 	/**
       
   279 	 * Updates trace version number
       
   280 	 * 
       
   281 	 * @param newTraceDataVersion
       
   282 	 *            New trace data version number
       
   283 	 */
       
   284 	public void setTraceDataVersion(String newTraceDataVersion) {
       
   285 
       
   286 		try {
       
   287 			traceDataVersion = Integer.parseInt(newTraceDataVersion, 16);
       
   288 		} catch (NumberFormatException nfe) {
       
   289 			// exception while trying to set new version number
       
   290 			// use the oldest version number, this version number is safest
       
   291 			// but when use this version some information could be lost
       
   292 			traceDataVersion = 1;
       
   293 		}
       
   294 	}
       
   295 
       
   296 	/**
       
   297 	 * Unloads one dll load from the stored items
       
   298 	 * 
       
   299 	 * @param dllLoad
       
   300 	 *            Dll unload item
       
   301 	 */
       
   302 	public void unloadOneDll(DllLoad dllLoad) {
       
   303 		if (dllLoads.containsKey(dllLoad.getName())) {
       
   304 			dllLoads.remove(dllLoad.getName());
       
   305 		}
       
   306 	}
       
   307 
       
   308 	/**
       
   309 	 * set end time of the process
       
   310 	 * @param aTime
       
   311 	 */
       
   312 	public void setEndTime(String aTime) {
       
   313 		Long lValue = Long.parseLong(aTime, 16);
       
   314 		endTime = lValue;
       
   315 	}
       
   316 
       
   317 	/**
       
   318 	 * set process name
       
   319 	 * @param aProcessName
       
   320 	 */
       
   321 	public void setProcessName(String aProcessName) {
       
   322 		processName = aProcessName;
       
   323 	}
       
   324 
       
   325 	/**
       
   326 	 * @return the process name
       
   327 	 */
       
   328 	public String getProcessName() {
       
   329 		return processName;
       
   330 	}
       
   331 
       
   332 	/**
       
   333 	 * get all events
       
   334 	 * @return list of BaseInfo
       
   335 	 */
       
   336 	public AbstractList<BaseInfo> getAllocsFrees() {
       
   337 		return allocsFrees;
       
   338 	}
       
   339 
       
   340 	/**
       
   341 	 * get end time of the process
       
   342 	 * @return
       
   343 	 */
       
   344 	public Long getEndTime() {
       
   345 		return endTime;
       
   346 	}
       
   347 
       
   348 	/**
       
   349 	 * get list of allocations freed by a FreeInfo
       
   350 	 * 
       
   351 	 * @param free
       
   352 	 * @return AbstractList<AllocInfo> list of allocations freed by the provided
       
   353 	 *         FreeInfo
       
   354 	 */
       
   355 	public AbstractList<AllocInfo> getAllocsFreedBy(FreeInfo free) {
       
   356 		AbstractList<AllocInfo> allocs = new ArrayList<AllocInfo>();
       
   357 		for (BaseInfo alloc : allocsFrees) {
       
   358 			if (alloc instanceof AllocInfo && ((AllocInfo) alloc).getFreedBy() == free) {
       
   359 				allocs.add((AllocInfo) alloc);
       
   360 			}
       
   361 		}
       
   362 		return allocs;
       
   363 	}
       
   364 	/**
       
   365 	 * Getter for highest memory allocation for the current process
       
   366 	 * @return int containing highest memory usage
       
   367 	 */
       
   368 	public int getHighestCumulatedMemoryAlloc() {
       
   369 		return highestMemory;
       
   370 	}
       
   371 
       
   372 }