sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.graphicsmemory/src/com/nokia/carbide/cpp/pi/graphicsmemory/GraphicsMemoryTraceParser.java
changeset 12 ae255c9aa552
equal deleted inserted replaced
11:5b9d4d8641ce 12:ae255c9aa552
       
     1 /*
       
     2  * Copyright (c) 2010 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.graphicsmemory;
       
    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.io.IOException;
       
    26 import java.text.MessageFormat;
       
    27 import java.util.ArrayList;
       
    28 import java.util.List;
       
    29 
       
    30 import com.nokia.carbide.cpp.internal.pi.model.GenericSampledTrace;
       
    31 import com.nokia.carbide.cpp.internal.pi.model.ParsedTraceData;
       
    32 import com.nokia.carbide.cpp.internal.pi.model.Parser;
       
    33 import com.nokia.carbide.cpp.pi.util.GeneralMessages;
       
    34 
       
    35 public class GraphicsMemoryTraceParser extends Parser {
       
    36 	private boolean debug = false;
       
    37 	private String version;
       
    38 
       
    39 	private GraphicsMemoryTrace memTrace;
       
    40 	private int readCount = 0;
       
    41 
       
    42 	// constants
       
    43 	public final static int SAMPLE_TOTAL_MEMORY_PROCESS_ID = -1;
       
    44 	public final static String SAMPLE_TOTAL_MEMORY_PROCESS_NAME = "TOTAL_MEMORY_USAGE"; //$NON-NLS-1$
       
    45 
       
    46 	public ParsedTraceData parse(File file) throws IOException {
       
    47 		if (!file.exists() || file.isDirectory()) {
       
    48 			throw new IOException(Messages
       
    49 					.getString("GraphicsMemoryTraceParser.cannotOpenTraceFile")); //$NON-NLS-1$
       
    50 		}
       
    51 		if (debug)
       
    52 			System.out
       
    53 					.println(Messages
       
    54 							.getString("GraphicsMemoryTraceParser.traceFileLength") + file.length()); //$NON-NLS-1$
       
    55 
       
    56 		parseMemTrace(file);
       
    57 		int versionNumber = convertVersionStringToInt(version);
       
    58 		memTrace.setVersion(versionNumber);
       
    59 
       
    60 		ParsedTraceData ptd = new ParsedTraceData();
       
    61 		ptd.traceData = this.getTrace();
       
    62 
       
    63 		return ptd;
       
    64 	}
       
    65 
       
    66 	private void parseV100GPUFile(DataInputStream dis) throws IOException {
       
    67 		// read the version again
       
    68 		byte[] version = readElementWithLength(dis);
       
    69 		if (debug)
       
    70 			System.out
       
    71 					.println(Messages
       
    72 							.getString("GraphicsMemoryTraceParser.readVersionDebug") + new String(version)); //$NON-NLS-1$
       
    73 		this.version = new String(version);
       
    74 
       
    75 		this.readV100GPUSample(dis);
       
    76 	}
       
    77 
       
    78 	public String getProfilerVersion() {
       
    79 		return version;
       
    80 	}
       
    81 
       
    82 	private void readV100GPUSample(DataInputStream dis) throws IOException {
       
    83 		memTrace = new GraphicsMemoryTrace();
       
    84 
       
    85 		GraphicsMemoryProcess process;
       
    86 
       
    87 		// read sample header length
       
    88 		dis.readUnsignedByte();
       
    89 		readCount++;
       
    90 
       
    91 		int length;
       
    92 		int mode = -1;
       
    93 		String processName = null;
       
    94 		GraphicsMemorySample gpuSample = null;
       
    95 		int usedMemory = -1;
       
    96 		int totalMemory = -1;
       
    97 		long sample = -1;
       
    98 
       
    99 		List<GraphicsMemoryProcess> processList = new ArrayList<GraphicsMemoryProcess>();
       
   100 		try {
       
   101 
       
   102 			while (true) {
       
   103 				length = dis.readUnsignedByte();
       
   104 				readCount++;
       
   105 				if (length == 1) {
       
   106 					mode = dis.readUnsignedByte();
       
   107 					readCount++;
       
   108 				} else {
       
   109 					throw new IOException(Messages
       
   110 							.getString("GraphicsMemoryTraceParser.wrongLength")); //$NON-NLS-1$
       
   111 				}
       
   112 
       
   113 				if (mode == 0xac) { // time stamp
       
   114 					// first there should be 4 bytes of sample time
       
   115 					sample = this.readTUint(dis);
       
   116 				}
       
   117 
       
   118 				else if (mode == 0xaa) { // process name
       
   119 					length = dis.readUnsignedByte();
       
   120 					readCount++; // process sample length
       
   121 					// reading process name
       
   122 					processName = readProcessName(dis, length);
       
   123 				} else if (mode == 0xab) { // process id
       
   124 					// reading process id
       
   125 					long processId = readTUint(dis);
       
   126 					// create process and sample
       
   127 					process = new GraphicsMemoryProcess(Integer
       
   128 							.valueOf((int) processId), processName);
       
   129 					gpuSample = new GraphicsMemorySample(process, (int) sample);
       
   130 
       
   131 				} else if (mode == 0xdb) { // total GPU data
       
   132 					// read the total GPU data
       
   133 					long total = readTUint(dis);
       
   134 					totalMemory = (int) total;
       
   135 
       
   136 				} else if (mode == 0xdc) { // used GPU data
       
   137 					// read the used GPU data
       
   138 					long used = readTUint(dis);
       
   139 					usedMemory = (int) used;
       
   140 
       
   141 					if (debug)
       
   142 						System.out
       
   143 								.println(MessageFormat
       
   144 										.format(
       
   145 												Messages
       
   146 														.getString("GraphicsMemoryTraceParser.readMemoryUsageDebug"), usedMemory, totalMemory, sample)); //$NON-NLS-1$
       
   147 					GraphicsMemorySample memoryUsageSample = new GraphicsMemorySample(
       
   148 							usedMemory, totalMemory, (int) sample);
       
   149 					memTrace.addSample(memoryUsageSample);
       
   150 					processList.add(memoryUsageSample.process);
       
   151 
       
   152 					length = dis.readUnsignedByte(); // read process sample
       
   153 														// length
       
   154 					readCount++;
       
   155 
       
   156 				} else if (mode == 0xde) { // private data
       
   157 					// read the private data
       
   158 					long privateSize = readTUint(dis);
       
   159 					gpuSample.privateSize = (int) privateSize;
       
   160 				} else if (mode == 0xdf) { // shared data
       
   161 					// read the shared data
       
   162 					long sharedSize = readTUint(dis);
       
   163 					gpuSample.sharedSize = (int) sharedSize;
       
   164 
       
   165 					if (debug)
       
   166 						System.out
       
   167 								.println(MessageFormat
       
   168 										.format(
       
   169 												Messages
       
   170 														.getString("GraphicsMemoryTraceParser.readProcessDebug"), gpuSample.process.processName, gpuSample.sampleSynchTime, Long.toHexString(gpuSample.process.processId), gpuSample.privateSize, gpuSample.sharedSize)); //$NON-NLS-1$
       
   171 
       
   172 					memTrace.addSample(gpuSample);
       
   173 					processList.add(gpuSample.process);
       
   174 
       
   175 					length = dis.readUnsignedByte(); // read process sample
       
   176 														// length
       
   177 					readCount++;
       
   178 				} else {
       
   179 					throw new IOException(
       
   180 							Messages
       
   181 									.getString("GraphicsMemoryTraceParser.wrongMode") + mode); //$NON-NLS-1$
       
   182 				}
       
   183 			}
       
   184 		} catch (EOFException e) {
       
   185 			memTrace.setProcesses(processList
       
   186 					.toArray(new GraphicsMemoryProcess[processList.size()]));
       
   187 			System.out.println(Messages
       
   188 					.getString("GraphicsMemoryTraceParser.finishedReading")); //$NON-NLS-1$
       
   189 		}
       
   190 	}
       
   191 
       
   192 	/*
       
   193 	 * A method for calculating hash value for a string
       
   194 	 */
       
   195 	public static long sum(String arg) {
       
   196 		int total = 0;
       
   197 		for (int i = 0; i < arg.length(); i++) {
       
   198 			total += (long) arg.charAt(i);
       
   199 		}
       
   200 		return total; // returns the sum of the chars after cast
       
   201 	}
       
   202 
       
   203 	private String readProcessName(DataInputStream dis, int length)
       
   204 			throws IOException {
       
   205 		if (length != 0) {
       
   206 			byte[] element = new byte[length];
       
   207 			dis.read(element, 0, length);
       
   208 			readCount += length;
       
   209 			return new String(element);
       
   210 		} else
       
   211 			return null;
       
   212 	}
       
   213 
       
   214 	private byte[] readElementWithLength(DataInputStream dis) throws IOException {
       
   215 		byte length = dis.readByte();
       
   216 		readCount++;
       
   217 		if (length != 0) {
       
   218 			byte[] element = new byte[length];
       
   219 			dis.read(element, 0, length);
       
   220 			readCount += length;
       
   221 			return element;
       
   222 		} else
       
   223 			return null;
       
   224 	}
       
   225 
       
   226 	private long readTUint(DataInputStream dis) throws IOException {
       
   227 		long result = dis.readUnsignedByte();
       
   228 		readCount++;
       
   229 		result += dis.readUnsignedByte() << 8;
       
   230 		readCount++;
       
   231 		result += dis.readUnsignedByte() << 16;
       
   232 		readCount++;
       
   233 		result += dis.readUnsignedByte() << 24;
       
   234 		readCount++;
       
   235 		return result;
       
   236 	}
       
   237 
       
   238 	private void parseMemTrace(File file) throws IOException {
       
   239 		DataInputStream dis = new DataInputStream(new FileInputStream(file));
       
   240 		byte[] traceArray = new byte[(int) file.length()];
       
   241 		dis.readFully(traceArray);
       
   242 
       
   243 		// test the graph mem trace version
       
   244 		String s = new String(traceArray, 1, traceArray[0]);
       
   245 		if (traceArray.length > 257) {
       
   246 			if (s.startsWith("Bappea_GPU_V1.00")) { //$NON-NLS-1$
       
   247 				ByteArrayInputStream bais = new ByteArrayInputStream(traceArray);
       
   248 				dis = new DataInputStream(bais);
       
   249 				this.parseV100GPUFile(dis);
       
   250 				return;
       
   251 			}
       
   252 		}
       
   253 		String version = s.substring(8, 12);
       
   254 		String traceType = s.substring(13, s.length());
       
   255 		System.out
       
   256 				.println(Messages
       
   257 						.getString("GraphicsMemoryTraceParser.foundVersion1") + version + Messages.getString("GraphicsMemoryTraceParser.foundVersion2") + traceType + Messages.getString("GraphicsMemoryTraceParser.foundVersion3")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
   258 		GeneralMessages
       
   259 				.showErrorMessage(Messages
       
   260 						.getString("GraphicsMemoryTraceParser.wrongTraceType1") + traceType + Messages.getString("GraphicsMemoryTraceParser.wrongTraceType2")); //$NON-NLS-1$ //$NON-NLS-2$
       
   261 		throw new IOException(
       
   262 				Messages.getString("GraphicsMemoryTraceParser.wrongTraceType1") + traceType + Messages.getString("GraphicsMemoryTraceParser.wrongTraceType2")); //$NON-NLS-1$ //$NON-NLS-2$
       
   263 
       
   264 	}
       
   265 
       
   266 	private GenericSampledTrace getTrace() {
       
   267 		return (GenericSampledTrace) memTrace;
       
   268 	}
       
   269 
       
   270 	private int convertVersionStringToInt(String version) {
       
   271 		// Coverts version number from string to int
       
   272 		int i = 0;
       
   273 		int versionInt = 0;
       
   274 		String versionString = ""; //$NON-NLS-1$
       
   275 
       
   276 		// goes string thru and copies all digits into another string
       
   277 		while (i < version.length()) {
       
   278 			if (Character.isDigit(version.charAt(i))) {
       
   279 				versionString += version.charAt(i);
       
   280 			}
       
   281 			i++;
       
   282 
       
   283 		}
       
   284 		// convert string to int
       
   285 		try {
       
   286 			versionInt = Integer.parseInt(versionString);
       
   287 		} catch (Exception e) {
       
   288 			e.printStackTrace();
       
   289 		}
       
   290 		return versionInt;
       
   291 	}
       
   292 }