sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.button/src/com/nokia/carbide/cpp/pi/button/BupTraceParser.java
changeset 2 b9ab3b238396
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.button;
       
    19 
       
    20 import java.io.ByteArrayInputStream;
       
    21 import java.io.DataInputStream;
       
    22 import java.io.EOFException;
       
    23 import java.io.File;
       
    24 import java.io.FileInputStream;
       
    25 import java.util.ArrayList;
       
    26 import java.util.Vector;
       
    27 
       
    28 import com.nokia.carbide.cpp.internal.pi.model.GenericTrace;
       
    29 import com.nokia.carbide.cpp.internal.pi.model.ParsedTraceData;
       
    30 import com.nokia.carbide.cpp.internal.pi.model.Parser;
       
    31 import com.nokia.carbide.cpp.pi.importer.SampleImporter;
       
    32 import com.nokia.carbide.cpp.pi.util.GeneralMessages;
       
    33 import com.nokia.carbide.cpp.sdk.core.ISymbianSDK;
       
    34 import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin;
       
    35 
       
    36 
       
    37 public class BupTraceParser extends Parser
       
    38 {
       
    39 	private BupSample[] traceData;	
       
    40 	private boolean debug = false;
       
    41 
       
    42 	public BupTraceParser() throws Exception
       
    43 	{
       
    44 	}
       
    45 		
       
    46 	public ParsedTraceData parse(File file) throws Exception 
       
    47 	{
       
    48 		if (!file.exists() || file.isDirectory())
       
    49 		{
       
    50 			throw new Exception(Messages.getString("BupTraceParser.unableToOpenTrace")); //$NON-NLS-1$
       
    51 		}
       
    52 		
       
    53 		Vector<BupSample> intermediateTraceData = new Vector<BupSample>();
       
    54 		IBupEventMap map = null;
       
    55 		try 
       
    56 		{
       
    57 			FileInputStream fis = new FileInputStream(file);
       
    58 			byte[] data = new byte[(int)file.length()];
       
    59 			fis.read(data);
       
    60 			
       
    61 			this.traceVersion = this.getVersion(data);
       
    62 			System.out.println(Messages.getString("BupTraceParser.versionTitle") + this.traceVersion); //$NON-NLS-1$
       
    63 			if (traceVersion.indexOf("V1.20") == -1) //$NON-NLS-1$
       
    64 			{
       
    65 			    System.out.println(Messages.getString("BupTraceParser.unsupportedVersion") + this.traceVersion); //$NON-NLS-1$
       
    66 			    fis.close();
       
    67 			    return null;
       
    68 			}
       
    69 			
       
    70 			ByteArrayInputStream bais = new ByteArrayInputStream(data);
       
    71 			DataInputStream dis = new DataInputStream(bais);
       
    72 			
       
    73 			// read the length of the header
       
    74 			int length = dis.readByte();
       
    75 			// skip the header
       
    76 			for (int i = 0; i < length; i++)
       
    77 				dis.readByte();
       
    78 			
       
    79 			int keyCode = 0;
       
    80 			long sampleTime = 0;
       
    81 			long sampleCount = 0;
       
    82 			
       
    83 			SampleImporter sampleImporter = SampleImporter.getInstance();
       
    84 			IBupEventMapProfile profileForParsing = null;
       
    85 			String sdkId = sampleImporter.getBupMapSymbianSDKId();
       
    86 			if (sdkId != null && !sdkId.equals("")) { //$NON-NLS-1$
       
    87 				ISymbianSDK sdk = SDKCorePlugin.getSDKManager().getSDK(sdkId, true);
       
    88 				ArrayList<IBupEventMapProfile> profiles = BupEventMapManager.getInstance().getProfilesFromSDK(sdk);
       
    89 				for (IBupEventMapProfile profile : profiles) {
       
    90 					if (profile.getProfileId().equals(sampleImporter.getBupMapProfileId())) {
       
    91 						profileForParsing = profile;
       
    92 					}
       
    93 				}
       
    94 			} else if (sampleImporter.isBupMapIsWorkspace()) {
       
    95 				ArrayList<IBupEventMapProfile> profiles = BupEventMapManager.getInstance().getProfilesFromWorkspacePref();
       
    96 				for (IBupEventMapProfile profile : profiles) {
       
    97 					if (profile.getProfileId().equals(sampleImporter.getBupMapProfileId())) {
       
    98 						profileForParsing = profile;
       
    99 					}
       
   100 				}
       
   101 			} else {
       
   102 				ArrayList<IBupEventMapProfile> profiles = BupEventMapManager.getInstance().getProfilesFromBuiltin();
       
   103 				for (IBupEventMapProfile profile : profiles) {
       
   104 					if (profile.getProfileId().equals(sampleImporter.getBupMapProfileId())) {
       
   105 						profileForParsing = profile;
       
   106 					}
       
   107 				}
       
   108 			}
       
   109 			// fall back
       
   110 			if (profileForParsing == null) {
       
   111 				profileForParsing = BupEventMapManager.getInstance().getDefaultProfile();
       
   112 				GeneralMessages.showErrorMessage(Messages.getString("BupTraceParser.profile.cannot.load")); //$NON-NLS-1$
       
   113 			}
       
   114 			
       
   115 			map = BupEventMapManager.getInstance().captureMap(profileForParsing);
       
   116 			try
       
   117 			{
       
   118 				while(true)
       
   119 				{
       
   120 					keyCode = (int) readTUint(dis);
       
   121 					sampleTime = readTUint(dis);
       
   122 					sampleCount++;
       
   123 					BupSample sample = new BupSample(sampleTime, keyCode, map);
       
   124 
       
   125 					intermediateTraceData.add(sample);
       
   126 //					System.out.println("Keycode: " + keyCode + " sampletime: " + sampleTime);
       
   127 				}
       
   128 			}
       
   129 			catch (EOFException eof)
       
   130 			{
       
   131 				//return;
       
   132 			}
       
   133 			catch (Exception e)
       
   134 			{
       
   135 				GeneralMessages.showErrorMessage(Messages.getString("BupTraceParser.errorReadingTraceFile")); //$NON-NLS-1$
       
   136 				throw e;				
       
   137 			}
       
   138 			BupEventMapManager.getInstance().releaseMap(map);
       
   139 		} catch (Exception e)
       
   140 		{
       
   141 			BupEventMapManager.getInstance().releaseMap(map);
       
   142 			GeneralMessages.showErrorMessage(Messages.getString("BupTraceParser.errorReadingTraceFile")); //$NON-NLS-1$
       
   143 			throw e;				
       
   144 		} finally {
       
   145 			BupEventMapManager.getInstance().releaseMap(map);
       
   146 		}
       
   147 	
       
   148 		if (debug)
       
   149 			System.out.println(Messages.getString("BupTraceParser.traceFileParsed") + intermediateTraceData.size()); //$NON-NLS-1$
       
   150 		
       
   151 		// all samples have been parsed
       
   152 		this.traceData = new BupSample[intermediateTraceData.size()];
       
   153 		
       
   154 		// store the trace data into an array
       
   155 		intermediateTraceData.toArray(this.traceData);
       
   156 		
       
   157 		ParsedTraceData ptd = new ParsedTraceData();
       
   158 		ptd.traceData = this.getTrace();
       
   159 		return ptd;
       
   160 	}
       
   161 	
       
   162 	private String getVersion(byte[] data)
       
   163 	{
       
   164 		int length = data[0];
       
   165 
       
   166 		String ver = Messages.getString("BupTraceParser.unknown"); //$NON-NLS-1$
       
   167 		String verString = new String(data, 1, length);
       
   168 		if (debug) System.out.println(Messages.getString("BupTraceParser.version") + verString); //$NON-NLS-1$
       
   169 		
       
   170 		if (verString.indexOf("Bappea") != -1) //$NON-NLS-1$
       
   171 			if (verString.indexOf("BUP") != -1) //$NON-NLS-1$
       
   172 			{
       
   173 				int index = verString.indexOf("_"); //$NON-NLS-1$
       
   174 				ver = verString.substring(index+1,length);
       
   175 				
       
   176 			}
       
   177 		return ver;		  			
       
   178 	}		
       
   179 	
       
   180 	private long readTUint(DataInputStream dis) throws Exception
       
   181 	{
       
   182 		long result = dis.readUnsignedByte();
       
   183 		result += dis.readUnsignedByte() << 8;
       
   184 		result += dis.readUnsignedByte() << 16;
       
   185 		result += dis.readUnsignedByte() << 24;
       
   186 		return result;
       
   187 	}
       
   188 	
       
   189 	private GenericTrace getTrace()
       
   190 	{
       
   191 		BupTrace trace = new BupTrace();
       
   192 		for (int i = 0; i < traceData.length; i++)
       
   193 		{
       
   194 			trace.addSample(this.traceData[i]);
       
   195 		}
       
   196 		return trace;
       
   197 	}
       
   198 }