sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/engine/ParseAnalyzeData.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 ParseAnalyzeData
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.s60tools.analyzetool.engine;
       
    19 
       
    20 import java.io.File;
       
    21 import java.io.FileNotFoundException;
       
    22 import java.io.FileOutputStream;
       
    23 import java.io.IOException;
       
    24 import java.util.AbstractList;
       
    25 import java.util.ArrayList;
       
    26 import java.util.Hashtable;
       
    27 import java.util.Iterator;
       
    28 
       
    29 import org.eclipse.core.runtime.IStatus;
       
    30 
       
    31 import com.nokia.s60tools.analyzetool.Activator;
       
    32 import com.nokia.s60tools.analyzetool.engine.statistic.AllocCallstack;
       
    33 import com.nokia.s60tools.analyzetool.engine.statistic.AllocInfo;
       
    34 import com.nokia.s60tools.analyzetool.engine.statistic.DllLoad;
       
    35 import com.nokia.s60tools.analyzetool.engine.statistic.FreeInfo;
       
    36 import com.nokia.s60tools.analyzetool.engine.statistic.ProcessInfo;
       
    37 import com.nokia.s60tools.analyzetool.global.Constants;
       
    38 
       
    39 /**
       
    40  * Parses trace messages which comes thru tracing utility.
       
    41  * If one message contains PCSS prefix the message will saved to the data file Data file will be saved
       
    42  * to the project [bld.inf location]\atool_temp folder.
       
    43  *
       
    44  * @author kihe
       
    45  *
       
    46  */
       
    47 public class ParseAnalyzeData {
       
    48 
       
    49 	private static final String KEYWORD_ABNORMAL = "ABNORMAL";//$NON-NLS-1$
       
    50 	/** FileOutputStream . */
       
    51 	private FileOutputStream fis;
       
    52 
       
    53 	/** File. */
       
    54 	private File file;
       
    55 
       
    56 	/** Contains information which processes are started. */
       
    57 	private final Hashtable<String, Integer> processStart;
       
    58 
       
    59 	/** Contains information which processes are ended. */
       
    60 	private final AbstractList<Integer> processEnd;
       
    61 
       
    62 
       
    63 	/** Contains information of where to save trace data. */
       
    64 	private String usedFilePath;
       
    65 
       
    66 	/** Contains active process information. */
       
    67 	public Hashtable<Integer, ProcessInfo> processes;
       
    68 
       
    69 	/**
       
    70 	 * Contains list of executed process. One process will begin in the
       
    71 	 * PROCESS_START and ends with the PROCESS_END tag
       
    72 	 */
       
    73 	private final AbstractList<ProcessInfo> processList;
       
    74 
       
    75 	/**
       
    76 	 * Allocation cache
       
    77 	 * Used when one free info is separated to multiple lines.
       
    78 	 */
       
    79 	private final Hashtable<Long, AllocInfo> allocCache;
       
    80 
       
    81 
       
    82 	/**
       
    83 	 * Deallocation cache.
       
    84 	 * Used when one free info is separated to multiple lines.
       
    85 	 */
       
    86 	private final Hashtable<Long, FreeInfo> freeCache;
       
    87 
       
    88 
       
    89 	/**
       
    90 	 * Cache for dll loads.
       
    91 	 * We must find dll load item for every allocation callstack item.
       
    92 	 * This is heavy process and usually we must find more than thousand times.
       
    93 	 * So when using cache for found items => it makes finding process more rapid than without it.
       
    94 	 */
       
    95 	private final Hashtable<Long, DllLoad> dllLoadCache;
       
    96 
       
    97 
       
    98 	/**
       
    99 	 * Flag to determinate need to save parsed data to file.
       
   100 	 * If this flag is set to "true" all the lines which contains PCSS are saved to the file.
       
   101 	 * Otherwise just parse the file/trace content.
       
   102 	 */
       
   103 	boolean saveDataToFile;
       
   104 
       
   105 	boolean createGraphModel;
       
   106 	/**
       
   107 	 * Constructor.
       
   108 	 * @param saveData Need to save data to file.
       
   109 	 */
       
   110 	public ParseAnalyzeData(boolean saveData, boolean createModel) {
       
   111 
       
   112 		processStart = new Hashtable<String, Integer>();
       
   113 		processEnd = new ArrayList<Integer>();
       
   114 		processes = new Hashtable<Integer, ProcessInfo>();
       
   115 		processList = new ArrayList<ProcessInfo>();
       
   116 		allocCache = new Hashtable<Long, AllocInfo>();
       
   117 		saveDataToFile = saveData;
       
   118 		dllLoadCache = new Hashtable<Long, DllLoad>();
       
   119 		freeCache = new Hashtable<Long, FreeInfo>();
       
   120 		createGraphModel = createModel;
       
   121 	}
       
   122 
       
   123 	/**
       
   124 	 * Add one dllLoad object to process related list
       
   125 	 *
       
   126 	 * @param dllLoad
       
   127 	 *            One DllLoad
       
   128 	 */
       
   129 	private void addDllLoad(DllLoad dllLoad) {
       
   130 		// if one of the started process contains same process id what dll load
       
   131 		// has add dll load to list
       
   132 		if (processes.containsKey(dllLoad.getProcessID())) {
       
   133 			ProcessInfo tempProcessInfo = processes.get(dllLoad.getProcessID());
       
   134 			tempProcessInfo.addOneDllLoad(dllLoad);
       
   135 		}
       
   136 	}
       
   137 
       
   138 	/**
       
   139 	 * Add one memory allocation info to process related list
       
   140 	 *
       
   141 	 * @param info
       
   142 	 *            One memory allocation info
       
   143 	 */
       
   144 	private void addMemAddress(AllocInfo info) {
       
   145 		// if one of the started process contains same process id what memory
       
   146 		// allocation has add memory allocation info to list
       
   147 		if (processes.containsKey(info.getProcessID())) {
       
   148 			ProcessInfo tempProcessInfo = processes.get(info.getProcessID());
       
   149 			tempProcessInfo.addOneAlloc(info);
       
   150 		}
       
   151 	}
       
   152 
       
   153 
       
   154 	/**
       
   155 	 * Removes memory allocation from process related
       
   156 	 * memory allocations list.
       
   157 	 *
       
   158 	 * @param info Deallocation info
       
   159 	 */
       
   160 	private void removeMemAddress(FreeInfo info) {
       
   161 		if (processes.containsKey(info.getProcessID())) {
       
   162 			ProcessInfo tempProcessInfo = processes.get(info.getProcessID());
       
   163 			tempProcessInfo.free(info);
       
   164 		}
       
   165 
       
   166 	}
       
   167 	/**
       
   168 	 * Closes the input stream if it is open.
       
   169 	 */
       
   170 	private final void closeStreams() {
       
   171 		try {
       
   172 
       
   173 			// if fis exists => close fis
       
   174 			if (fis != null) {
       
   175 				fis.close();
       
   176 
       
   177 				// clear file and fis
       
   178 				file = null;
       
   179 				fis = null;
       
   180 			}
       
   181 		} catch (IOException ioe) {
       
   182 			ioe.printStackTrace();
       
   183 		}
       
   184 	}
       
   185 
       
   186 	/**
       
   187 	 * Finish the writing.
       
   188 	 *
       
   189 	 */
       
   190 	public final void finish() {
       
   191 
       
   192 		try {
       
   193 			// if some data is not write to file yet => do it now
       
   194 			if (fis != null) {
       
   195 				fis.flush();
       
   196 			}
       
   197 		} catch (IOException ioe) {
       
   198 			return;
       
   199 		} finally {
       
   200 
       
   201 			// close needed streams
       
   202 			closeStreams();
       
   203 		}
       
   204 
       
   205 		// clear stored data
       
   206 		processList.clear();
       
   207 		processes.clear();
       
   208 		processStart.clear();
       
   209 		processEnd.clear();
       
   210 		allocCache.clear();
       
   211 		freeCache.clear();
       
   212 		dllLoadCache.clear();
       
   213 	}
       
   214 
       
   215 	/**
       
   216 	 * Return memory allocation size for all started processes.
       
   217 	 *
       
   218 	 * @return Memory allocation size for all started processes
       
   219 	 */
       
   220 	public final int getAllocationsSize() {
       
   221 		int allocCnt = 0;
       
   222 		// get memory allocations count from ongoing processes
       
   223 		for (ProcessInfo p : processes.values()) {
       
   224 			//the current number of memory allocations is traced as potential memory leaks
       
   225 			allocCnt += p.getMemLeaksNumber();
       
   226 		}
       
   227 		
       
   228 		//get memory allocations count from already ended processes
       
   229 		Iterator<ProcessInfo> iterPro = processList.iterator();
       
   230 		while( iterPro.hasNext() ) {
       
   231 			ProcessInfo oneInfo = iterPro.next();
       
   232 			allocCnt += oneInfo.getMemLeaksNumber();
       
   233 		}
       
   234 		return allocCnt;
       
   235 	}
       
   236 
       
   237 	/**
       
   238 	 * Gets used data file name.
       
   239 	 *
       
   240 	 * @return Data file name
       
   241 	 */
       
   242 	public final String getDataFileName() {
       
   243 		return usedFilePath;
       
   244 	}
       
   245 
       
   246 	/**
       
   247 	 * Checks given path if it is null set used path to point java temp
       
   248 	 * directory.
       
   249 	 *
       
   250 	 * @param path
       
   251 	 *            Given path
       
   252 	 * @return Data file location
       
   253 	 */
       
   254 	private final String getFileName(final String path) {
       
   255 		if (path == null || ("").equals(path)) {
       
   256 			// null path given => use java temp dir
       
   257 			usedFilePath = System.getProperty("java.io.tmpdir")
       
   258 					+ Constants.FILENAME;
       
   259 		} else if (!path.contains(Constants.FILENAME)) {
       
   260 			usedFilePath = path + Constants.FILENAME;
       
   261 		}
       
   262 		return usedFilePath;
       
   263 	}
       
   264 
       
   265 	/**
       
   266 	 * Gets started process info.
       
   267 	 *
       
   268 	 * @return Hashtable<String, String> Started processes info
       
   269 	 */
       
   270 	public final Hashtable<String, Integer> getStartedProcesses() {
       
   271 		if (processStart.isEmpty()) {
       
   272 			return null;
       
   273 		}
       
   274 		return processStart;
       
   275 	}
       
   276 
       
   277 	/**
       
   278 	 * Returns stored statistic
       
   279 	 *
       
   280 	 * @return Stored statistic
       
   281 	 */
       
   282 	public AbstractList<ProcessInfo> getStatistic() {
       
   283 		return processList;
       
   284 	}
       
   285 
       
   286 	/**
       
   287 	 * Opens the needed streams.
       
   288 	 *
       
   289 	 * @param filePath
       
   290 	 *            File name and path where to save trace data
       
   291 	 *
       
   292 	 * @return True if stream could be opened otherwise false
       
   293 	 */
       
   294 	public final boolean openStreams(final String filePath) {
       
   295 
       
   296 		boolean returnValue = false;
       
   297 		try {
       
   298 			// store used data file name
       
   299 			usedFilePath = getFileName(filePath);
       
   300 
       
   301 			// if file did not exists => create it
       
   302 			if (file == null) {
       
   303 				file = new File(usedFilePath);
       
   304 			}
       
   305 
       
   306 			// if fis did not exists => open it
       
   307 			if (fis == null) {
       
   308 				fis = new FileOutputStream(file, false);
       
   309 			}
       
   310 			returnValue = true;
       
   311 		} catch (FileNotFoundException fno) {
       
   312 
       
   313 			// if error occurs => close open streams
       
   314 			closeStreams();
       
   315 			returnValue = false;
       
   316 		}
       
   317 		return returnValue;
       
   318 	}
       
   319 
       
   320 	/**
       
   321 	 * Executes parser.
       
   322 	 *
       
   323 	 * @param data
       
   324 	 *            File name to be used
       
   325 	 */
       
   326 	public final boolean parse(final String data) {
       
   327 
       
   328 		try{
       
   329 			// if no data => leave
       
   330 			if (data == null) {
       
   331 				//although the data is null
       
   332 				//return true because false is returned only when
       
   333 				//some unexpected error occurs
       
   334 				return true;
       
   335 			}
       
   336 
       
   337 			// if data must be saved to project group\atool_temp folder
       
   338 			if(saveDataToFile) {
       
   339 				// open needed streams
       
   340 				openStreams(usedFilePath);
       
   341 			}
       
   342 
       
   343 			// if data contains desired prefix => write data to file
       
   344 			boolean contains = data.contains(Constants.PREFIX);
       
   345 
       
   346 			if (contains) {
       
   347 				writeDataToFile(data);
       
   348 			}
       
   349 			return true;
       
   350 		}catch(OutOfMemoryError oome) {
       
   351 			return false;
       
   352 		}catch(Exception e) {
       
   353 			Activator.getDefault().log(IStatus.ERROR, "AnalyzeTool - parsing trace data", e);
       
   354 			return false;
       
   355 		}
       
   356 	}
       
   357 
       
   358 	/**
       
   359 	 * Fills dll load item information. Check what data file version is used,
       
   360 	 * because data file format is changed.
       
   361 	 * 
       
   362 	 * @param dllLoad
       
   363 	 *            Dll load item
       
   364 	 * @param splittedText
       
   365 	 *            One line of trace data file.
       
   366 	 */
       
   367 	private void fillDllLoadInfo(DllLoad dllLoad, String[] splittedText) {
       
   368 		int processID = dllLoad.getProcessID();
       
   369 		if(processes.containsKey(processID)) {
       
   370 			ProcessInfo processInfo = processes.get(processID);
       
   371 
       
   372 			//if data file contains also time stamp for dll load
       
   373 			//this information is added later than other information
       
   374 			//thats why we must check which version is used.
       
   375 			if( processInfo.getTraceDataVersion() > 1 && splittedText.length > 6 ) {
       
   376 				dllLoad.setLoadTime(splittedText[4]);
       
   377 				dllLoad.setStartAddress(splittedText[5]);
       
   378 				dllLoad.setEndAddress(splittedText[6]);
       
   379 			}
       
   380 			else {
       
   381 				dllLoad.setStartAddress(splittedText[4]);
       
   382 				dllLoad.setEndAddress(splittedText[5]);
       
   383 			}
       
   384 		}
       
   385 	}
       
   386 
       
   387 	/**
       
   388 	 * Parses "FREE" tag information from the trace data file.
       
   389 	 *
       
   390 	 * Note! This tag is not used in current version,
       
   391 	 * this is support for older version of trace data files.
       
   392 	 * @param splitted One line content of trace file.
       
   393 	 */
       
   394 	private void parseFree(String[] splitted) {
       
   395 		
       
   396 		// check that there is enough data
       
   397 		// at least we need know to memory address 
       
   398 		// which is forth item of trace data
       
   399 		if( splitted.length > 3 ) {
       
   400 			String processID = splitted[1];
       
   401 			FreeInfo freeInfo = new FreeInfo();
       
   402 			freeInfo.setProcessID(processID);
       
   403 			freeInfo.setMemoryAddress(splitted[3]);
       
   404 			removeMemAddress(freeInfo);
       
   405 		}
       
   406 	}
       
   407 
       
   408 
       
   409 	/**
       
   410 	 * Parse dealloction header from the line
       
   411 	 * @param line Allocation header line
       
   412 	 */
       
   413 	private void parseFreeHeader(String[] splitted) {
       
   414 		//get free line info
       
   415 		String processID = splitted[1];
       
   416 		FreeInfo freeInfo = new FreeInfo();
       
   417 		freeInfo.setProcessID(processID);
       
   418 
       
   419 		freeInfo.setMemoryAddress(splitted[3]);
       
   420 		
       
   421 		if( createGraphModel ) {
       
   422 		
       
   423 			int traceFileVersion = 1;
       
   424 			//get trace file version
       
   425 			if(processes.containsKey(freeInfo.getProcessID())) {
       
   426 				ProcessInfo processInfo = processes.get(freeInfo.getProcessID());
       
   427 				traceFileVersion = processInfo.getTraceDataVersion();
       
   428 			}
       
   429 			// how many callstakc items
       
   430 			int callstackCount = 0;
       
   431 
       
   432 			// index where the callstack addresses begins
       
   433 			int startIndex = 5;
       
   434 
       
   435 			//if using the new trace file format
       
   436 			if( traceFileVersion > 1 ) {
       
   437 				freeInfo.setTime(splitted[4]);
       
   438 				callstackCount = Integer.parseInt(splitted[5], 16);
       
   439 				startIndex = 6;
       
   440 			}
       
   441 			else {
       
   442 				callstackCount = Integer.parseInt(splitted[5], 16);
       
   443 			}
       
   444 
       
   445 			AbstractList<AllocCallstack> callstack = new ArrayList<AllocCallstack>();
       
   446 			createCallstack(splitted, freeInfo.getProcessID(), callstack, startIndex);
       
   447 			freeInfo.addCallstack(callstack);
       
   448 			
       
   449 			//if this free item contains fragments
       
   450 			//so we must store this info to cache
       
   451 			//and rest of the callstack items later
       
   452 			if( callstackCount > (splitted.length-startIndex) ) {
       
   453 				freeCache.put(freeInfo.getMemoryAddress(), freeInfo);
       
   454 			}
       
   455 		}
       
   456 		
       
   457 		removeMemAddress(freeInfo);
       
   458 
       
   459 		
       
   460 	}
       
   461 
       
   462 
       
   463 	/**
       
   464 	 * Parse dealloction fragment from the line
       
   465 	 * @param line Allocation fragment line
       
   466 	 */
       
   467 	private void parseFreeFragment(String[] splitted)
       
   468 	{
       
   469 		if( createGraphModel ) {
       
   470 			String procId = splitted[1];
       
   471 			int processId = Integer.parseInt(procId,16);
       
   472 			if (processes.containsKey(processId)) {
       
   473 				String memAddr = splitted[3];
       
   474 				Long memoryAddress =Long.parseLong(memAddr, 16);
       
   475 				Long time = Long.parseLong(splitted[4],16);
       
   476 				String packetNumber = splitted[5];
       
   477 
       
   478 				//if cache contains corresponding free info
       
   479 				if (freeCache.containsKey(memoryAddress)) {
       
   480 					FreeInfo info = freeCache.get(memoryAddress);
       
   481 					if (info.getMemoryAddress() == memoryAddress && info.getTime() == time ) {
       
   482 						AbstractList<AllocCallstack> callstack = new ArrayList<AllocCallstack>();
       
   483 						createCallstack(splitted, processId, callstack, 6);
       
   484 						info.updateFragment(callstack, packetNumber);
       
   485 					}
       
   486 				}
       
   487 			}
       
   488 		}
       
   489 	}
       
   490 	
       
   491 	/**
       
   492 	 * Remove dll load from the list.
       
   493 	 * This provides functionality for dynamically loaded dll loads
       
   494 	 * @param dllLoad DllLoad item to unload
       
   495 	 */
       
   496 	private void unloadDll( DllLoad dllLoad )
       
   497 	{
       
   498 		if (processes.containsKey(dllLoad.getProcessID())) {
       
   499 			ProcessInfo tempProcessInfo = processes.get(dllLoad.getProcessID());
       
   500 			tempProcessInfo.unloadOneDll(dllLoad);
       
   501 
       
   502 			//remove found dll load item from cache
       
   503 			for( java.util.Enumeration<Long> e = dllLoadCache.keys(); e.hasMoreElements();)
       
   504 			{
       
   505 				Long key = e.nextElement();
       
   506 				DllLoad tempDllLoad = dllLoadCache.get(key);
       
   507 
       
   508 				// if values equals remove it from the list
       
   509 				if (tempDllLoad.getProcessID() == dllLoad.getProcessID() && tempDllLoad.getStartAddress() == dllLoad.getStartAddress()
       
   510 						&& tempDllLoad.getEndAddress() == dllLoad.getEndAddress() && tempDllLoad.getName() == tempDllLoad.getName()) {
       
   511 					dllLoadCache.remove(key);
       
   512 				}
       
   513 			}
       
   514 
       
   515 		}
       
   516 	}
       
   517 
       
   518 	/**
       
   519 	 * Parse allocation header info from the line
       
   520 	 * @param line Allocation line
       
   521 	 */
       
   522 	private void parseAllocHeader(String[] splitted)
       
   523 	{
       
   524 		try {
       
   525 			String procID = splitted[1];
       
   526 			int processID = Integer.parseInt(procID, 16);
       
   527 			if (processes.containsKey(processID)) {
       
   528 				AllocInfo oneAlloc = new AllocInfo();
       
   529 				oneAlloc.setProcessID(procID);
       
   530 				oneAlloc.setMemoryAddress(splitted[3]);
       
   531 				if( createGraphModel ) {
       
   532 					oneAlloc.setTime(splitted[4]);
       
   533 					
       
   534 					oneAlloc.setSizeInt(Integer.parseInt(splitted[5], 16));
       
   535 					// if one trace message contains callstack
       
   536 					if (splitted.length > 6) {
       
   537 
       
   538 						AbstractList<AllocCallstack> callstack = new ArrayList<AllocCallstack>();
       
   539 						createCallstack(splitted, processID, callstack, 7);
       
   540 						oneAlloc.addCallstack(callstack);
       
   541 						allocCache.put(oneAlloc.getMemoryAddress(), oneAlloc);
       
   542 					}
       
   543 				}
       
   544 				addMemAddress(oneAlloc);
       
   545 			}
       
   546 		} catch (NumberFormatException nfe) {
       
   547 			nfe.printStackTrace();
       
   548 		}
       
   549 
       
   550 	}
       
   551 
       
   552 	/**
       
   553 	 * Parse allocation fragment from the line
       
   554 	 * 
       
   555 	 * @param line
       
   556 	 *            Allocation fragment line
       
   557 	 */
       
   558 	private void parseAllocFragment(String[] splitted) {
       
   559 		if( createGraphModel ) {
       
   560 			String procId = splitted[1];
       
   561 			int processId = Integer.parseInt(procId, 16);
       
   562 			if (processes.containsKey(processId)) {
       
   563 				String memAddr = splitted[3];
       
   564 				Long memoryAddress = Long.parseLong(memAddr,16);
       
   565 				Long time = Long.parseLong(splitted[4],16);
       
   566 				String packetNumber = splitted[5];
       
   567 
       
   568 				if (allocCache.containsKey(memoryAddress)) {
       
   569 					AllocInfo info = allocCache.get(memoryAddress);
       
   570 					if (info.getMemoryAddress() == memoryAddress && info.getTime() == time) {
       
   571 						AbstractList<AllocCallstack> callstack = new ArrayList<AllocCallstack>();
       
   572 						createCallstack(splitted, processId, callstack, 6);
       
   573 						info.updateFragment(callstack, packetNumber);
       
   574 					}
       
   575 				}
       
   576 			}
       
   577 		}
       
   578 	}
       
   579 
       
   580 	/**
       
   581 	 * Creates callstack values
       
   582 	 * 
       
   583 	 * @param splitted
       
   584 	 *            Callstack values
       
   585 	 * @param processId
       
   586 	 *            Process id
       
   587 	 * @param callstack
       
   588 	 *            Callstack reference where the callstack values are added
       
   589 	 * @param startIndex
       
   590 	 *            Index where to start parse callstack values
       
   591 	 */
       
   592 	private void createCallstack(String[] splitted, int processId, AbstractList<AllocCallstack> callstack, int startIndex) {
       
   593 		// append whole callstack as a one memory address
       
   594 		for (int i = startIndex; i < splitted.length; i++) {
       
   595 			try{
       
   596 				AllocCallstack allocCAll = new AllocCallstack();
       
   597 				allocCAll.setMemoryAddress(splitted[i]);
       
   598 
       
   599 				// define dll load for current alloc
       
   600 				DllLoad dllLoad = getDllLoadName(processId, Long.parseLong(splitted[i],16));
       
   601 				if( dllLoad != null ) {
       
   602 					allocCAll.setDllLoad(dllLoad);
       
   603 					callstack.add(allocCAll);	
       
   604 				}
       
   605 			} catch(NumberFormatException nfe) {
       
   606 				//no nothing by design
       
   607 				
       
   608 			} catch( Exception e ) {
       
   609 				//no nothing by design
       
   610 			}
       
   611 			
       
   612 		}
       
   613 	}
       
   614 
       
   615 	/**
       
   616 	 * Parses memory address from the one trace message line. Parses one trace
       
   617 	 * message whole callstack to one memory address. This memory address is
       
   618 	 * used to calculate most used memory allocation location.
       
   619 	 * 
       
   620 	 * @param line
       
   621 	 *            One trace message
       
   622 	 */
       
   623 	private void parseMemAddressesFromLine(String[] splitted) {
       
   624 
       
   625 		//TODO
       
   626 		String processID = splitted[1];
       
   627 		if (processes.containsKey(processID)) {
       
   628 			AllocInfo oneAlloc = new AllocInfo();
       
   629 			oneAlloc.setProcessID(processID);
       
   630 			oneAlloc.setMemoryAddress(splitted[3]);
       
   631 			oneAlloc.setTime(splitted[4]);
       
   632 			oneAlloc.setSizeInt(Integer.parseInt(splitted[5], 16));
       
   633 
       
   634 			// if one trace message contains callstack
       
   635 			if (splitted.length > 5) {
       
   636 				AbstractList<AllocCallstack> callstack = new ArrayList<AllocCallstack>();
       
   637 				// append whole callstack as a one memory address
       
   638 				for (int i = 6; i < splitted.length; i++) {
       
   639 					AllocCallstack allocCAll = new AllocCallstack();
       
   640 					allocCAll.setMemoryAddress(splitted[i]);
       
   641 
       
   642 					// define dll load for current alloc
       
   643 					DllLoad dllLoad = getDllLoadName(oneAlloc.getProcessID(), allocCAll.getMemoryAddress());
       
   644 					if (dllLoad != null) {
       
   645 						allocCAll.setDllLoad(dllLoad);
       
   646 					}
       
   647 
       
   648 					callstack.add(allocCAll);
       
   649 				}
       
   650 				oneAlloc.addCallstack(callstack);
       
   651 				addMemAddress(oneAlloc);
       
   652 			}
       
   653 		}
       
   654 	}
       
   655 
       
   656 	/**
       
   657 	 * Returns dll load item for the memory address. Checks that entered memory
       
   658 	 * address is dll load memory area
       
   659 	 * 
       
   660 	 * @param processId
       
   661 	 *            Process id
       
   662 	 * @param memoryAddress
       
   663 	 *            Memory address
       
   664 	 * @return DllLoad item if found otherwise null
       
   665 	 */
       
   666 	private DllLoad getDllLoadName(int processId, Long memoryAddress) {
       
   667 		if (processes.containsKey(processId)) {
       
   668 			ProcessInfo tempProcessInfo = processes.get(processId);
       
   669 			Hashtable<String, DllLoad> loads = tempProcessInfo.getDllLoads();
       
   670 
       
   671 			// check does cache contains already corresponding item
       
   672 			if (dllLoadCache.containsKey(memoryAddress)) {
       
   673 				return dllLoadCache.get(memoryAddress);
       
   674 			}
       
   675 
       
   676 			// no item found in the cache loop thru the loaded dlls
       
   677 			for (java.util.Enumeration<String> e = loads.keys(); e.hasMoreElements();) {
       
   678 				try {
       
   679 					String key = e.nextElement();
       
   680 					DllLoad oneLoad = loads.get(key);
       
   681 					Long start = oneLoad.getStartAddress();
       
   682 					Long end =  oneLoad.getEndAddress();
       
   683 					Long actual = memoryAddress;
       
   684 					if (actual >= start && actual <= end) {
       
   685 						// dll load found => save it to cache and return it
       
   686 						dllLoadCache.put(memoryAddress, oneLoad);
       
   687 						return oneLoad;
       
   688 					}
       
   689 				} catch (java.lang.NumberFormatException nfe) {
       
   690 					// trace message corrupt?
       
   691 				}
       
   692 			}
       
   693 		}
       
   694 		return null;
       
   695 	}
       
   696 
       
   697 	/**
       
   698 	 * process the line: switch on the event type and fill the model
       
   699 	 * 
       
   700 	 * @param aLine
       
   701 	 *            One debug print
       
   702 	 */
       
   703 	private final void parseLine(final String aLine) {
       
   704 
       
   705 		try {
       
   706 			// parse switch
       
   707 			int index = aLine.indexOf(Constants.PREFIX); // lines should be preceded
       
   708 			// with PCSS
       
   709 			String usedString = null;
       
   710 
       
   711 			if (index == -1 || index == 0) {
       
   712 				usedString = aLine;
       
   713 			} else {
       
   714 				usedString = aLine.substring(index, aLine.length());
       
   715 			}
       
   716 
       
   717 			String[] lineFragments = usedString.split(" ");
       
   718 
       
   719 			// determine the memory operation/event
       
   720 			String event = null;
       
   721 			if (lineFragments.length >= 3) {
       
   722 				event = lineFragments[2];
       
   723 			}
       
   724 
       
   725 			DllLoad dllLoad = null;
       
   726 
       
   727 			switch (Constants.Operation.toOperation(event)) {
       
   728 			case PROCESS_START:
       
   729 				// process PROCESS_START
       
   730 				parseProcessStarted(lineFragments);
       
   731 				break;
       
   732 			case PROCESS_END:
       
   733 				// process PROCESS_END
       
   734 				parseProcessEnded(lineFragments);
       
   735 				break;
       
   736 			case ALLOC:
       
   737 				// process ALLOC
       
   738 				parseMemAddressesFromLine(lineFragments);
       
   739 				break;
       
   740 			case FREE:
       
   741 				parseFree(lineFragments);
       
   742 				break;
       
   743 			case ALLOCH:
       
   744 				parseAllocHeader(lineFragments);
       
   745 				break;
       
   746 			case FREEH:
       
   747 				parseFreeHeader(lineFragments);
       
   748 				break;
       
   749 			case ALLOCF:
       
   750 				parseAllocFragment(lineFragments);
       
   751 				break;
       
   752 			case FREEF:
       
   753 				parseFreeFragment(lineFragments);
       
   754 				break;
       
   755 			case DLL_LOAD:
       
   756 				if(createGraphModel) {
       
   757 					dllLoad = new DllLoad();
       
   758 					dllLoad.setProcessID(lineFragments[1]);
       
   759 					dllLoad.setName(lineFragments[3]);
       
   760 					fillDllLoadInfo(dllLoad, lineFragments);
       
   761 					addDllLoad(dllLoad);
       
   762 				}
       
   763 				break;
       
   764 			case DLL_UNLOAD:
       
   765 				if(createGraphModel) {
       
   766 					dllLoad = new DllLoad();
       
   767 					dllLoad.setProcessID(lineFragments[1]);
       
   768 					dllLoad.setName(lineFragments[3]);
       
   769 					fillDllLoadInfo(dllLoad, lineFragments);
       
   770 					unloadDll(dllLoad);
       
   771 				}
       
   772 				break;
       
   773 			default:
       
   774 				// ignore this line
       
   775 				break;
       
   776 			}
       
   777 		}catch(Exception e) {
       
   778 			Activator.getDefault().log(IStatus.ERROR, "Error while parsing data", e);
       
   779 		}
       
   780 		
       
   781 	}
       
   782 
       
   783 	/**
       
   784 	 * Processing PROCESS_END tag
       
   785 	 * @param lineFragments String[] containing split PROCESS_END tag line
       
   786 	 */
       
   787 	private void parseProcessEnded(String[] lineFragments) {
       
   788 
       
   789 		// process id
       
   790 		String procId = null;
       
   791 
       
   792 		// if data contains all the needed information
       
   793 		if (lineFragments.length >= 2) {
       
   794 			procId = lineFragments[1];
       
   795 		}
       
   796 		
       
   797 		int processId = Integer.parseInt(procId, 16);
       
   798 		if (processes.containsKey(processId)) {
       
   799 			if( lineFragments.length > 4 ){
       
   800 				//check is ABNORMAL process end and that there are enough data to parse
       
   801 				if( lineFragments[4].equals(KEYWORD_ABNORMAL) && lineFragments.length > 5 ) {
       
   802 					processes.get(processId).setEndTime(lineFragments[5]);	
       
   803 				}
       
   804 				else {
       
   805 					processes.get(processId).setEndTime(lineFragments[4]);
       
   806 				}	
       
   807 			}
       
   808 			
       
   809 
       
   810 			// store process id and process name
       
   811 			this.processEnd.add(processId);
       
   812 
       
   813 			if (processStart.containsValue(processId)) {
       
   814 				for (java.util.Enumeration<String> e = processStart.keys(); e.hasMoreElements();) {
       
   815 					Object key = e.nextElement();
       
   816 					Object value = processStart.get(key);
       
   817 					if (value.equals(processId)) {
       
   818 						processStart.remove(key);
       
   819 					}
       
   820 
       
   821 				}
       
   822 			}
       
   823 
       
   824 			// PROCESS_END tag reached
       
   825 			// check that started processes contains this process id.
       
   826 			ProcessInfo info = processes.get(processId);
       
   827 
       
   828 			// process found from the started processes list
       
   829 			// add it to list
       
   830 			if (info != null) {
       
   831 				addProcessInfoToList(processes.get(processId));
       
   832 			}
       
   833 
       
   834 			// remove process id from processes list
       
   835 			processes.remove(processId);
       
   836 
       
   837 			// clear the founded dll load items list
       
   838 			// this prevent that results between runs do not mixed up
       
   839 			dllLoadCache.clear();
       
   840 		}
       
   841 	}
       
   842 
       
   843 	/**
       
   844 	 * Processing PROCESS_START tag
       
   845 	 * @param lineFragments String[] containing split PROCESS_START tag line
       
   846 	 */
       
   847 	private void parseProcessStarted(String[] lineFragments) {
       
   848 
       
   849 		String procId = lineFragments[1];
       
   850 		int processId = Integer.parseInt(procId, 16);
       
   851 
       
   852 		ProcessInfo processInfo = new ProcessInfo();
       
   853 		processInfo.setProcessID(processId);
       
   854 
       
   855 		if (lineFragments.length >= 4) {
       
   856 			processInfo.setProcessName(lineFragments[3]);
       
   857 		}
       
   858 
       
   859 		if (lineFragments.length > 5) {
       
   860 			processInfo.setStartTime(lineFragments[5]);
       
   861 		}
       
   862 
       
   863 		// set trace data version number
       
   864 		// this information is used later when parsing e.g. one allocation
       
   865 		// information
       
   866 		if (lineFragments.length > 7) {
       
   867 			processInfo.setTraceDataVersion(lineFragments[7]);
       
   868 		}
       
   869 
       
   870 		// store process id and process name
       
   871 		if( lineFragments.length > 3 ) {
       
   872 			this.processStart.put(lineFragments[3], processId);
       
   873 			processes.put(processId, processInfo);
       
   874 		}
       
   875 	}
       
   876 
       
   877 	/**
       
   878 	 * Adds process info to the list
       
   879 	 * 
       
   880 	 * @param info
       
   881 	 *            ProcessInfo reference
       
   882 	 */
       
   883 	private void addProcessInfoToList(ProcessInfo info) {
       
   884 		if (processList.isEmpty()) {
       
   885 			processList.add(info);
       
   886 			return;
       
   887 		}
       
   888 		for (int i = 0; i < processList.size(); i++) {
       
   889 			ProcessInfo tempInfo = processList.get(i);
       
   890 			if( info.getStartTime() != null && info.getStartTime() < tempInfo.getStartTime() ){
       
   891 				processList.add(i, info);
       
   892 				return;
       
   893 			}
       
   894 		}
       
   895 		processList.add(info);
       
   896 	}
       
   897 
       
   898 	/**
       
   899 	 * Writes data to file if line contains wanted data.
       
   900 	 * 
       
   901 	 * @param data
       
   902 	 *            Data to write
       
   903 	 */
       
   904 	private void writeDataToFile(String data) {
       
   905 		try {
       
   906 
       
   907 			// check if data contains PROCESS_START or PROCESS_END tag
       
   908 			parseLine(data);
       
   909 
       
   910 			// if saveDataToFile flag is true => save data
       
   911 			if (saveDataToFile) {
       
   912 
       
   913 				if (fis == null) {
       
   914 
       
   915 					// streams not open => try to open
       
   916 					boolean returnValue = this.openStreams(usedFilePath);
       
   917 					if (!returnValue) {
       
   918 						return;
       
   919 					}
       
   920 				}
       
   921 
       
   922 				// append line feed and write given data to file
       
   923 				String dataAndLineFeed = data + "\n";
       
   924 
       
   925 				// write data
       
   926 				fis.write(dataAndLineFeed.getBytes("UTF-8"));
       
   927 			}
       
   928 
       
   929 		} catch (IOException ioe) {
       
   930 			return;
       
   931 		}
       
   932 	}
       
   933 }