sysperfana/memspyext/com.nokia.s60tools.swmtanalyser/src/com/nokia/s60tools/swmtanalyser/model/SwmtParser.java
changeset 7 8e12a575a9b5
equal deleted inserted replaced
6:f65f740e69f9 7:8e12a575a9b5
       
     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 "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 package com.nokia.s60tools.swmtanalyser.model;
       
    18 
       
    19 import java.io.BufferedReader;
       
    20 import java.io.FileNotFoundException;
       
    21 import java.io.FileReader;
       
    22 import java.io.IOException;
       
    23 
       
    24 import com.nokia.s60tools.swmtanalyser.data.ChunksData;
       
    25 import com.nokia.s60tools.swmtanalyser.data.CycleData;
       
    26 import com.nokia.s60tools.swmtanalyser.data.DiskData;
       
    27 import com.nokia.s60tools.swmtanalyser.data.FilesData;
       
    28 import com.nokia.s60tools.swmtanalyser.data.GlobalDataChunks;
       
    29 import com.nokia.s60tools.swmtanalyser.data.HeapData;
       
    30 import com.nokia.s60tools.swmtanalyser.data.KernelHandles;
       
    31 import com.nokia.s60tools.swmtanalyser.data.PSHandlesData;
       
    32 import com.nokia.s60tools.swmtanalyser.data.StackData;
       
    33 import com.nokia.s60tools.swmtanalyser.data.SystemData;
       
    34 import com.nokia.s60tools.swmtanalyser.data.WindowGroups;
       
    35 import com.nokia.s60tools.swmtanalyser.exception.SwmtFormatException;
       
    36 
       
    37 /**
       
    38  * Reads the text log files and stores all the data in CycleData objects.
       
    39  *
       
    40  */
       
    41 public class SwmtParser {
       
    42 
       
    43 	private static BufferedReader reader = null;
       
    44 
       
    45 	/**
       
    46 	 * @param path specifes the filesystem path of swmt log to be parsed
       
    47 	 * @param cycleData is the data structure, wherein the parsed data gets stored.
       
    48 	 * @return error messages during parsing. In case of successful parsing, null will be returned
       
    49 	 */
       
    50 	public static String parseSwmtLog(String path, CycleData cycleData) throws SwmtFormatException
       
    51 	{
       
    52 		FileReader inputStr;
       
    53 		int lineNo = 0;
       
    54 		
       
    55 		try 
       
    56 		{
       
    57 			inputStr = new FileReader(path);
       
    58 		
       
    59 			reader = new BufferedReader(inputStr);
       
    60 			String s = null;
       
    61 		    		    
       
    62 			while((s = reader.readLine()) != null)
       
    63 			{
       
    64 				lineNo++;
       
    65 				
       
    66 				if(s.contains("{SYSM}"))
       
    67 				{
       
    68 					SystemData sysData = readSystemMemory(s);
       
    69 					
       
    70 					if(sysData != null)
       
    71 					{
       
    72 						if(s.contains("Free"))
       
    73 							cycleData.setFreeMemory(sysData.getFreeMemory());
       
    74 						else if(s.contains("Total"))
       
    75 							cycleData.setTotalMemory(sysData.getTotalMemory());
       
    76 					}
       
    77 					else
       
    78 					{
       
    79 						SwmtFormatException ex = new SwmtFormatException("Error in SYSM view. ");
       
    80 						throw ex;
       
    81 					}
       
    82 					//String freeRam = s.substring(s.indexOf(FREE_MEM) + (FREE_MEM).length()).trim();
       
    83 					
       
    84 				}
       
    85 				if(s.contains("{DISK}")){
       
    86 					DiskData diskData = readDiskData(s);
       
    87 									
       
    88 					if(diskData != null){
       
    89 						cycleData.addDiskData(diskData);
       
    90 					}
       
    91 					else{
       
    92 						SwmtFormatException ex = new SwmtFormatException("Error in DISK view. ");
       
    93 						throw ex;
       
    94 					}
       
    95 				}
       
    96 				else if(s.contains("{HGEN}"))
       
    97 				{
       
    98 					KernelHandles kernelData = readKernelHandlesData(s);
       
    99 					
       
   100 					if(kernelData != null){
       
   101 						cycleData.addKernelData(kernelData);
       
   102 					}
       
   103 					else{
       
   104 						SwmtFormatException ex = new SwmtFormatException("Error in HGEN view. ");
       
   105 						throw ex;
       
   106 					}
       
   107 				}
       
   108 				else if(s.contains("{FILE}"))
       
   109 				{
       
   110 					//DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Processing File info..");
       
   111 					FilesData fileData = readFileData(s);
       
   112 					
       
   113 					if(fileData != null)
       
   114 						cycleData.addFileData(fileData);
       
   115 					
       
   116 					else{
       
   117 						SwmtFormatException ex = new SwmtFormatException("Error in FILE view. ");
       
   118 						throw ex;
       
   119 					}
       
   120 				}
       
   121 				else if(s.contains("{HEAP}"))
       
   122 				{
       
   123 					//DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Processing Heap info..");
       
   124 					HeapData heapData = readHeapData(s);
       
   125 					
       
   126 					if(heapData != null)
       
   127 						cycleData.addHeapData(heapData);
       
   128 					else{
       
   129 						SwmtFormatException ex = new SwmtFormatException("Error in HEAP view. ");
       
   130 						throw ex;
       
   131 					}
       
   132 				}
       
   133 				else if(s.contains("{STAK}"))
       
   134 				{
       
   135 					//DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Processing Stack info..");
       
   136 					StackData stackData = readStackData(s);
       
   137 					
       
   138 					if(stackData != null)
       
   139 						cycleData.addStackData(stackData);
       
   140 					else{
       
   141 						SwmtFormatException ex = new SwmtFormatException("Error in STAK view. ");
       
   142 						throw ex;
       
   143 					}
       
   144 				}
       
   145 				else if(s.contains("{HPAS}"))
       
   146 				{
       
   147 					PSHandlesData psHandlesData= readHPASHandlesData(s);
       
   148 					
       
   149 					if(psHandlesData != null){
       
   150 						cycleData.addHPASHandlesData(psHandlesData);
       
   151 					}
       
   152 					else{
       
   153 						SwmtFormatException ex = new SwmtFormatException("Error in HPAS view. ");
       
   154 						throw ex;
       
   155 					}
       
   156 				}
       
   157 				else if(s.contains("{CHNK}"))
       
   158 				{
       
   159 					ChunksData chunksData= readChunksData(s);
       
   160 					
       
   161 					if(chunksData != null){
       
   162 						cycleData.addChunksData(chunksData);
       
   163 					}
       
   164 					else{
       
   165 						SwmtFormatException ex = new SwmtFormatException("Error in CHNK view. ");
       
   166 						throw ex;
       
   167 					}
       
   168 				}
       
   169 				else if(s.contains("{GLOD}"))
       
   170 				{
       
   171 					GlobalDataChunks globalChunksData= readGlobalChunksData(s);
       
   172 					
       
   173 					if(globalChunksData != null){
       
   174 						cycleData.addGlobalChunksData(globalChunksData);
       
   175 					}
       
   176 					else{
       
   177 						SwmtFormatException ex = new SwmtFormatException("Error in GLOD view. ");
       
   178 						throw ex;
       
   179 					}
       
   180 				}
       
   181 				else if(s.contains("{WNDG}"))
       
   182 				{
       
   183 					WindowGroups wndgData = readWindowGroupsData(s);
       
   184 					
       
   185 					if(wndgData != null){
       
   186 						cycleData.addWindowGroupsData(wndgData);
       
   187 					}
       
   188 					else
       
   189 					{
       
   190 						SwmtFormatException ex = new SwmtFormatException("Error in WNDG view. ");
       
   191 						throw ex;
       
   192 					}
       
   193 				}
       
   194 			}
       
   195 		
       
   196 		} 
       
   197 		catch (FileNotFoundException e) {
       
   198 			return e.getMessage();
       
   199 		} catch (IOException e) {
       
   200 			return e.getMessage();
       
   201 		}catch(SwmtFormatException e)
       
   202 		{
       
   203 			throw new SwmtFormatException(e.getMessage() + " Line No: " + lineNo); 
       
   204 		}
       
   205 		
       
   206 		finally {
       
   207 			try{
       
   208 				reader.close();
       
   209 			}catch(IOException e){
       
   210 				
       
   211 			}
       
   212 		}
       
   213 		return null;
       
   214 	}
       
   215 	
       
   216 	/**
       
   217 	 * This method reads the DISK data from the given string 
       
   218 	 * and returns a DiskData object. If the string does not contain
       
   219 	 * "MemSpy" and "{DISK}" tags, it returns null.
       
   220 	 */
       
   221 	private static DiskData readDiskData(String str) throws SwmtFormatException
       
   222 	{
       
   223 		//DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Reading Disk Data..");
       
   224 		
       
   225 		if(str.contains("MemSpy") && str.contains("{DISK}"))
       
   226 		{
       
   227 			DiskData data = new DiskData();
       
   228 			String [] subStrings = str.split(",");
       
   229 			
       
   230 			if(subStrings.length !=8)
       
   231 				throw new SwmtFormatException("Invalid number of fields in DISK view.");
       
   232 			
       
   233 			data.setName(subStrings[1].trim());
       
   234 			data.setSize(subStrings[3].trim());
       
   235 			data.setFreeSize(subStrings[4].trim());
       
   236 			data.setStatus(subStrings[7].trim());
       
   237 			
       
   238 			return data;
       
   239 		}
       
   240 		return null;
       
   241 	}
       
   242 	
       
   243 	/**
       
   244 	 * This method reads the information under SYSM bucket to get the
       
   245 	 * total memory and free memory of the system.  
       
   246 	 */
       
   247 	private static SystemData readSystemMemory(String str) throws SwmtFormatException
       
   248 	{
       
   249 		if(str.contains("MemSpy") && str.contains("{SYSM}"))
       
   250 		{
       
   251 			SystemData sysData = new SystemData();
       
   252 			String [] subStrings = str.split(",");
       
   253 			
       
   254 			if(subStrings.length !=4)
       
   255 				throw new SwmtFormatException("Invalid number of fields in SYSM view.");
       
   256 			
       
   257 			if(str.contains("Free"))
       
   258 				sysData.setFreeMemory(Long.parseLong(subStrings[2]));
       
   259 			if(str.contains("Total"))
       
   260 				sysData.setTotalMemory(Long.parseLong(subStrings[2]));
       
   261 			
       
   262 			return sysData;
       
   263 		}
       
   264 		
       
   265 		return null;
       
   266 	}
       
   267 	/**
       
   268 	 * This method reads the Kerenel Handles data from the given string 
       
   269 	 * and returns a KernelHandles object. If the string does not contain
       
   270 	 * "MemSpy" and "{HGEN}" tags, it returns null.
       
   271 	 */
       
   272 	private static KernelHandles readKernelHandlesData(String str) throws SwmtFormatException
       
   273 	{
       
   274 		if(str.contains("MemSpy") && str.contains("{HGEN}"))
       
   275 		{
       
   276 			KernelHandles kernelData = new KernelHandles();
       
   277 			//DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Parsing Kernel Data " + str);
       
   278 			
       
   279 			String [] subStrings = str.split(",");
       
   280 			
       
   281 			if(subStrings.length != 5)
       
   282 				throw new SwmtFormatException("Invalid number of fields in HGEN view.");
       
   283 			
       
   284 			kernelData.setHandleName(subStrings[1].trim());
       
   285 			kernelData.setHandle(subStrings[2].trim());
       
   286 			kernelData.setHandleType(subStrings[3].trim());
       
   287 			kernelData.setStatus(subStrings[4].trim());
       
   288 			
       
   289 			return kernelData;
       
   290 		}
       
   291 		return null;
       
   292 	}
       
   293 	
       
   294 	/**
       
   295 	 * This method reads the FILE data from the given string 
       
   296 	 * and returns a FilesData object. If the string does not contain
       
   297 	 * "MemSpy" and "{FILE}" tags, it returns null.
       
   298 	 * @throws SwmtFormatException if the string is having invalid number formats
       
   299 	 */
       
   300 	private static FilesData readFileData(String str) throws SwmtFormatException
       
   301 	{
       
   302 		if(str.contains("MemSpy") && str.contains("{FILE}"))
       
   303 		{
       
   304 			//DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Parsing File Data " + str);
       
   305 			FilesData fileData = new FilesData();
       
   306 			
       
   307 			String [] subStrings = str.split(",");
       
   308 			
       
   309 			if(subStrings.length != 6)
       
   310 				throw new SwmtFormatException("Invalid number of fields in FILE view");
       
   311 			
       
   312 			fileData.setFileName(subStrings[1].trim());
       
   313 			fileData.setThreadName(subStrings[2].trim());
       
   314 			fileData.setFileSize(subStrings[3].trim());
       
   315 			fileData.setStatus(subStrings[5].trim());
       
   316 			
       
   317 			return fileData;
       
   318 		}
       
   319 		return null;
       
   320 	}
       
   321 	
       
   322 	/**
       
   323 	 * This method reads the Heap data from the given string 
       
   324 	 * and returns a HeapData object. If the string does not contain
       
   325 	 * "MemSpy" and "{HEAP}" tags, it returns null.
       
   326 	 * @throws SwmtFormatException
       
   327 	 */
       
   328 	private static HeapData readHeapData(String str) throws SwmtFormatException
       
   329 	{
       
   330 		if(str.contains("MemSpy") && str.contains("{HEAP}"))
       
   331 		{
       
   332 			//DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Parsing Heap Data " + str);
       
   333 			HeapData heapData = new HeapData();
       
   334 			
       
   335 			String [] subStrings = str.split(",");
       
   336 			
       
   337 			if(subStrings.length != 18)
       
   338 				throw new SwmtFormatException("Invalid number of fields in HEAP view");
       
   339 			
       
   340 			if(!subStrings[1].contains("::"))
       
   341 				throw new SwmtFormatException("Invalid thread name");
       
   342 			
       
   343 			heapData.setThreadAndProcessName(subStrings[1].trim());
       
   344 			heapData.setBaseAddr(subStrings[4].trim());
       
   345 			heapData.setSize(subStrings[5].trim());
       
   346 			heapData.setMaxSize(subStrings[7].trim());
       
   347 			heapData.setAllocatedCells(subStrings[10].trim());
       
   348 			heapData.setAllocSpace(subStrings[11].trim());
       
   349 			heapData.setFreeCells(subStrings[12].trim());
       
   350 			heapData.setFreeSpace(subStrings[13].trim());
       
   351 			heapData.setFreeSlack(subStrings[14].trim());
       
   352 			heapData.setLargestFreeCell(subStrings[15].trim());
       
   353 			heapData.setLargestAllocCell(subStrings[16].trim());
       
   354 			heapData.setStatus(subStrings[17].trim());
       
   355 			
       
   356 			return heapData;
       
   357 		}
       
   358 		return null;
       
   359 	}
       
   360 	
       
   361 	/**
       
   362 	 * This method reads the STACK data from the given string 
       
   363 	 * and returns a StackData object. If the string does not contain
       
   364 	 * "MemSpy" and "{STAK}" tags, it returns null.
       
   365 	 * @throws SwmtFormatException
       
   366 	 */
       
   367 	private static StackData readStackData(String str) throws SwmtFormatException
       
   368 	{
       
   369 		if(str.contains("MemSpy") && str.contains("{STAK}"))
       
   370 		{
       
   371 			//DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Parsing Stack Data " + str);
       
   372 			StackData stackData = new StackData();
       
   373 			
       
   374 			String [] subStrings = str.split(",");
       
   375 			
       
   376 			if(subStrings.length != 6)
       
   377 				throw new SwmtFormatException("Invalid number of fields in STAK view");
       
   378 			
       
   379 			if(!subStrings[1].contains("::"))
       
   380 				throw new SwmtFormatException("Invalid thread name");
       
   381 			
       
   382 			stackData.setThreadName(subStrings[1].trim());
       
   383 			stackData.setChunkName(subStrings[2].trim());
       
   384 			stackData.setSize(subStrings[4].trim());
       
   385 			stackData.setStatus(subStrings[5].trim());
       
   386 			
       
   387 			return stackData;
       
   388 		}
       
   389 		return null;
       
   390 	}
       
   391 	
       
   392 	/**
       
   393 	 * This method reads the HPAS Handles data from the given string 
       
   394 	 * and returns a HPAS Handles object. If the string does not contain
       
   395 	 * "MemSpy" and "{HPAS}" tags, it returns null.
       
   396 	 */
       
   397 	private static PSHandlesData readHPASHandlesData(String str) throws SwmtFormatException
       
   398 	{
       
   399 		if(str.contains("MemSpy") && str.contains("{HPAS}"))
       
   400 		{
       
   401 			PSHandlesData psHandlesData = new PSHandlesData();
       
   402 			
       
   403 			String [] subStrings = str.split(",");
       
   404 			
       
   405 			if(subStrings.length != 11)
       
   406 				throw new SwmtFormatException("Invalid number of fields in HPAS view");			
       
   407 			
       
   408 			psHandlesData.setHandleName(subStrings[1].trim());
       
   409 			psHandlesData.setHandle(subStrings[2].trim());
       
   410 			psHandlesData.setKeyType(Integer.parseInt(subStrings[3].trim()));
       
   411 			psHandlesData.setThreadId(Long.parseLong(subStrings[8].trim()));
       
   412 			psHandlesData.setThreadName(subStrings[9].trim());
       
   413 			psHandlesData.setStatus(subStrings[10].trim());
       
   414 					
       
   415 			return psHandlesData;
       
   416 		}
       
   417 		return null;
       
   418 	}
       
   419 	
       
   420 	/**
       
   421 	 * This method reads the Chunks data from the given string 
       
   422 	 * and returns a Chunk data object. If the string does not contain
       
   423 	 * "MemSpy" and "{CHNK}" tags, it returns null.
       
   424 	 */
       
   425 	private static ChunksData readChunksData(String str) throws SwmtFormatException
       
   426 	{
       
   427 		if(str.contains("MemSpy") && str.contains("{CHNK}"))
       
   428 		{
       
   429 			ChunksData chunkData = new ChunksData();
       
   430 			
       
   431 			String [] subStrings = str.split(",");
       
   432 			if(subStrings.length != 8)
       
   433 				throw new SwmtFormatException("Invalid number of fields in CHUNK view");			
       
   434 			
       
   435 			chunkData.setProcessName(subStrings[1].trim());
       
   436 			chunkData.setChunkName(subStrings[2].trim());
       
   437 			chunkData.setHandle(subStrings[3].trim());
       
   438 			chunkData.setBaseAddr(subStrings[4].trim());
       
   439 			chunkData.setSize(Long.parseLong(subStrings[5].trim()));
       
   440 			if(subStrings[7].equals("[N]+[A]"))
       
   441 				chunkData.setAttrib(CycleData.New);
       
   442 			else if(subStrings[7].equals("[D]"))
       
   443 				chunkData.setAttrib(CycleData.Deleted);
       
   444 			else if(subStrings[7].equals("[A]"))
       
   445 				chunkData.setAttrib(CycleData.Alive);
       
   446 			
       
   447 			return chunkData;
       
   448 		}
       
   449 		return null;
       
   450 	}
       
   451 	
       
   452 	/**
       
   453 	 * This method reads the Global Chunks data from the given string 
       
   454 	 * and returns a GlobalChunk data object. If the string does not contain
       
   455 	 * "MemSpy" and "{GLOD}" tags, it returns null.
       
   456 	 */
       
   457 	private static GlobalDataChunks readGlobalChunksData(String str) throws SwmtFormatException
       
   458 	{
       
   459 		if(str.contains("MemSpy") && str.contains("{GLOD}"))
       
   460 		{
       
   461 			GlobalDataChunks chunkData = new GlobalDataChunks();
       
   462 			
       
   463 			String [] subStrings = str.split(",");
       
   464 			if(subStrings.length != 8)
       
   465 				throw new SwmtFormatException("Invalid number of fields in HPAS view");			
       
   466 			
       
   467 			chunkData.setProcessName(subStrings[1].trim());
       
   468 			chunkData.setChunkName(subStrings[2].trim());
       
   469 			chunkData.setBaseAddr(subStrings[4].trim());
       
   470 			
       
   471 			if(subStrings[5].trim().length()!=0)
       
   472 				chunkData.setSize(Long.parseLong(subStrings[5].trim()));
       
   473 			else
       
   474 				chunkData.setSize(-1);
       
   475 			
       
   476 			if(subStrings[7].trim().equals("[N]+[A]"))
       
   477 				chunkData.setAttrib(CycleData.New);
       
   478 			else if(subStrings[7].trim().equals("[D]"))
       
   479 				chunkData.setAttrib(CycleData.Deleted);
       
   480 			else if(subStrings[7].trim().equals("[A]"))
       
   481 				chunkData.setAttrib(CycleData.Alive);
       
   482 			
       
   483 			return chunkData;
       
   484 		}
       
   485 		return null;
       
   486 	}
       
   487 
       
   488 	/**
       
   489 	 * 
       
   490 	 * This method parses the data related to WNDG events.
       
   491 	 */
       
   492 	private static WindowGroups readWindowGroupsData(String start_line) throws SwmtFormatException
       
   493 	{
       
   494 		if(start_line.contains("MemSpy") && start_line.contains("{WNDG}"))
       
   495 		{
       
   496 			String [] subStrings = start_line.split(",");
       
   497 			
       
   498 			if(subStrings.length != 6)
       
   499 				throw new SwmtFormatException("Invalid number of fields in WNDG view");			
       
   500 			
       
   501 			WindowGroups wndgData = new WindowGroups();
       
   502 			
       
   503 			String id_str = subStrings[1].trim();
       
   504 			int id = Integer.parseInt(id_str);
       
   505 			
       
   506 			int event = Integer.parseInt(subStrings[4].trim());
       
   507 			
       
   508 			wndgData.setId(id);
       
   509 			wndgData.setName(subStrings[2].trim());
       
   510 			wndgData.setEvent(event);
       
   511 			
       
   512 			if(subStrings[5].trim().equals("[N]+[A]"))
       
   513 				wndgData.setStatus(CycleData.New);
       
   514 			else if(subStrings[5].trim().equals("[D]"))
       
   515 				wndgData.setStatus(CycleData.Deleted);
       
   516 			else if(subStrings[5].trim().equals("[A]"))
       
   517 				wndgData.setStatus(CycleData.Alive);
       
   518 			
       
   519 			return wndgData;
       
   520 		}
       
   521 		return null;
       
   522 	}
       
   523 }