sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.address/src/com/nokia/carbide/cpp/pi/address/GppTraceCsvPrinter.java
changeset 2 b9ab3b238396
child 5 844b047e260d
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.address;
       
    19 
       
    20 import java.io.BufferedReader;
       
    21 import java.io.File;
       
    22 import java.io.FileNotFoundException;
       
    23 import java.io.FileReader;
       
    24 import java.io.IOException;
       
    25 import java.io.StringWriter;
       
    26 import java.util.Enumeration;
       
    27 import java.util.Hashtable;
       
    28 import java.util.Vector;
       
    29 
       
    30 import com.nokia.carbide.cpp.internal.pi.model.ProfiledThread;
       
    31 
       
    32 
       
    33 public class GppTraceCsvPrinter
       
    34 {
       
    35     private StringWriter csvWriter;
       
    36     
       
    37     // directory for the address trace plugin
       
    38     private String directory = System.getProperty("user.dir") + File.separator   //$NON-NLS-1$
       
    39     						+ "plugins" + File.separator  //$NON-NLS-1$
       
    40     						+ AddressPlugin.getDefault().getBundle().getSymbolicName() + "_"  //$NON-NLS-1$
       
    41     						+ AddressPlugin.getDefault().getBundle().getHeaders().get("Bundle-Version");  //$NON-NLS-1$
       
    42     private String eka1 = "fixed_thread_list_eka1_for_csv_print.txt"; //$NON-NLS-1$
       
    43     private String eka2 = "fixed_thread_list_eka2_for_csv_print.txt"; //$NON-NLS-1$
       
    44     private static final int eka1Mode = 1;
       
    45     private static final int eka2Mode = 2;
       
    46     
       
    47     public String getCsvPrint()
       
    48     {
       
    49         if (csvWriter == null)
       
    50         	return null;
       
    51         else
       
    52         	return csvWriter.toString();
       
    53     }
       
    54     
       
    55     public GppTraceCsvPrinter(GppTrace trace)
       
    56     {
       
    57     	String threadListFile;
       
    58     	File f;
       
    59 
       
    60     	int mode = solveMode(trace);
       
    61     	if (mode == eka1Mode)
       
    62     		threadListFile = directory + File.separator + eka1;
       
    63     	else if (mode == eka2Mode)
       
    64     		threadListFile = directory + File.separator + eka2;
       
    65     	else
       
    66     	{
       
    67     		System.out.println(Messages.getString("GppTraceCsvPrinter.1"));  //$NON-NLS-1$
       
    68     		return;
       
    69     	}
       
    70     	
       
    71     	f = new File(threadListFile);
       
    72     	if (!f.exists())
       
    73     	{
       
    74     		System.out.println(Messages.getString("GppTraceCsvPrinter.2") + threadListFile + Messages.getString("GppTraceCsvPrinter.3"));   //$NON-NLS-1$ //$NON-NLS-2$
       
    75     		return;
       
    76     	}
       
    77     	csvWriter = new StringWriter();
       
    78     	
       
    79     	Enumeration pts = trace.getSortedThreadsElements();
       
    80     	if (pts == null)
       
    81     		return;
       
    82     	formCsvPrint(pts, f);
       
    83     }
       
    84     
       
    85     private Vector getFixedThreadTable(File fixedThreadFile)
       
    86     {
       
    87     	try 
       
    88     	{
       
    89     		BufferedReader br = new BufferedReader(new FileReader(fixedThreadFile));
       
    90 			String line;
       
    91 			Vector v = new Vector();
       
    92 			while ((line = br.readLine()) != null)
       
    93 			{
       
    94 				String threadName;
       
    95 				String mappedName = ""; //$NON-NLS-1$
       
    96     			int mappedNameStart = line.indexOf('{');
       
    97 				int mappedNameEnd = line.indexOf('}');
       
    98 				if (mappedNameStart != -1 && mappedNameEnd != -1)
       
    99 				{
       
   100 					mappedName = line.substring(mappedNameStart + 1, mappedNameEnd);
       
   101 					threadName = line.substring(0, mappedNameStart).trim();
       
   102 				}
       
   103 				else
       
   104 					threadName = line.trim();
       
   105 				v.add(new ObjectMap(threadName, mappedName));
       
   106 			}
       
   107 			br.close();
       
   108 			return v;
       
   109 		} catch (FileNotFoundException e) 
       
   110 		{
       
   111 			e.printStackTrace();
       
   112 		} catch(IOException e1)
       
   113 		{
       
   114 			e1.printStackTrace();
       
   115 		}
       
   116 		return null;
       
   117     	
       
   118     }
       
   119     
       
   120     private void formCsvPrint(Enumeration profiledThreads, File threadFile)
       
   121     {
       
   122     	Hashtable<String,int[]> activityByName = new Hashtable<String,int[]>();
       
   123     	Vector fixedThreads = getFixedThreadTable(threadFile);
       
   124     	Vector<ObjectMap> resultVector = new Vector<ObjectMap>();
       
   125     	boolean nullListFormed = false;
       
   126     	int[] nullList = null;
       
   127     	
       
   128     	while (profiledThreads.hasMoreElements())
       
   129     	{
       
   130     		ProfiledThread pt = (ProfiledThread)profiledThreads.nextElement();
       
   131     		if (!nullListFormed)
       
   132     		{
       
   133     			nullList = new int[pt.getActivityList().length];
       
   134     			nullListFormed = true;
       
   135     		}
       
   136     		String name = formatThreadName(pt.getNameString());
       
   137     		if (activityByName.containsKey(name))
       
   138     		{
       
   139     			int[] existingList = activityByName.get(name);
       
   140     			int[] combinedList = combineLists(pt.getActivityList(), existingList);
       
   141     			activityByName.put(name, combinedList);
       
   142     		}
       
   143     		else
       
   144     			activityByName.put(name, pt.getActivityList());
       
   145     	}
       
   146     	
       
   147     	for (Enumeration e = fixedThreads.elements(); e.hasMoreElements();)
       
   148     	{
       
   149     		ObjectMap om = (ObjectMap)e.nextElement();
       
   150     		boolean threadFound = false;
       
   151     		for (Enumeration<String> enu = activityByName.keys(); enu.hasMoreElements();)
       
   152     		{
       
   153     			String threadName = enu.nextElement();
       
   154     			if (threadName.startsWith((String)om.object))
       
   155     			{
       
   156     				threadFound = true;
       
   157     				String mappedName = (String)om.mappedObject;
       
   158     				if (mappedName != "") //$NON-NLS-1$
       
   159     					resultVector.add(new ObjectMap(mappedName, activityByName.get(threadName)));
       
   160     				else
       
   161     					resultVector.add(new ObjectMap((String)om.object, activityByName.get(threadName)));
       
   162     				break;
       
   163     			}
       
   164     		}
       
   165     		if (!threadFound)
       
   166     		{
       
   167     			String mappedName = (String)om.mappedObject;
       
   168 				if (mappedName != "") //$NON-NLS-1$
       
   169 					resultVector.add(new ObjectMap(mappedName, nullList));
       
   170 				else
       
   171 					resultVector.add(new ObjectMap((String)om.object, nullList));
       
   172     		}
       
   173     	}
       
   174     	if (resultVector.size() != 0)
       
   175     		writeCsvPrint(resultVector);
       
   176     }
       
   177     
       
   178     private void writeCsvPrint(Vector<ObjectMap> resultVector)
       
   179     {
       
   180     	csvWriter.write(",Time"); //$NON-NLS-1$
       
   181     	int csvTableHeight = ((int[])(resultVector.elementAt(0)).mappedObject).length;
       
   182     	int csvTableWidth = 0;
       
   183     	for (Enumeration<ObjectMap> e = resultVector.elements(); e.hasMoreElements();)
       
   184     	{
       
   185     		String key = (String)(e.nextElement()).object;
       
   186     		csvWriter.write("," + key); //$NON-NLS-1$
       
   187     	}
       
   188     	csvWriter.write("\r\n"); //$NON-NLS-1$
       
   189     	csvTableWidth = resultVector.size();
       
   190     	
       
   191     	for (int i = 0; i < csvTableHeight; i++)
       
   192     	{
       
   193     		csvWriter.write("R" + (i + 1) + "," + i/10f); //$NON-NLS-1$ //$NON-NLS-2$
       
   194     		for (int j = 0; j < csvTableWidth; j++)
       
   195     		{
       
   196     			ObjectMap om = resultVector.elementAt(j);
       
   197     			int[] tmp = (int[])om.mappedObject;
       
   198     			csvWriter.write("," + tmp[i]/100d); //$NON-NLS-1$
       
   199     		}
       
   200     		csvWriter.write("\r\n"); //$NON-NLS-1$
       
   201     	}
       
   202     }
       
   203     
       
   204     private String formatThreadName(String name)
       
   205     {
       
   206     	int firstPartEnd = name.indexOf('[');
       
   207     	int lastPartStart = name.indexOf(':');
       
   208     	int lastPartEnd = name.lastIndexOf('_');
       
   209     	if (firstPartEnd == -1 || lastPartStart == -1 || lastPartEnd == -1)
       
   210     		return name;
       
   211     	String firstPart = name.substring(0, firstPartEnd);
       
   212     	String lastPart = name.substring(lastPartStart, lastPartEnd);
       
   213     	return firstPart + lastPart;
       
   214     	
       
   215     }
       
   216     
       
   217     private int[] combineLists(int[] p1List, int[] p2List)
       
   218     {
       
   219     	if (p1List.length < p2List.length)
       
   220     		return null;
       
   221     	int[] combinedList = new int[p2List.length];
       
   222     	for (int i = 0; i < p2List.length; i++)
       
   223     	{
       
   224     		combinedList[i] = p2List[i] + p1List[i];
       
   225     	}
       
   226     	return combinedList;
       
   227     }
       
   228     
       
   229     private int solveMode(GppTrace trace)
       
   230     {
       
   231     	String processName = ((GppSample)trace.samples.elementAt(0)).thread.process.name;
       
   232     	if (processName == null)
       
   233     		return -1;
       
   234     	int start = processName.indexOf('[');
       
   235     	int end = processName.indexOf(']');
       
   236     	String processId = processName.substring(start + 1, end);
       
   237     	if (start == -1 || end == -1)
       
   238     		return -1;
       
   239     	if (processId.length() > 3)
       
   240     		return 1;
       
   241     	else
       
   242     		return 2;
       
   243     }
       
   244     
       
   245     static private class ObjectMap
       
   246     {	
       
   247     	public Object object;
       
   248     	public Object mappedObject;
       
   249     	public ObjectMap (Object object, Object mappedObject)
       
   250     	{
       
   251     		this.object = object;
       
   252     		this.mappedObject = mappedObject;
       
   253     	}
       
   254     }  
       
   255 }