sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.power/src/com/nokia/carbide/cpp/pi/power/PwrTrace.java
changeset 2 b9ab3b238396
child 12 ae255c9aa552
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.power;
       
    19 
       
    20 import java.util.ArrayList;
       
    21 
       
    22 import com.nokia.carbide.cpp.internal.pi.analyser.NpiInstanceRepository;
       
    23 import com.nokia.carbide.cpp.internal.pi.model.GenericSampledTrace;
       
    24 import com.nokia.carbide.cpp.internal.pi.visual.GenericTraceGraph;
       
    25 import com.nokia.carbide.cpp.pi.editors.PIPageEditor;
       
    26 
       
    27 
       
    28 public class PwrTrace extends GenericSampledTrace
       
    29 {
       
    30 	private static final long serialVersionUID = -2398791759386296139L;
       
    31 	
       
    32 	transient private PowerTraceGraph[] graphs = null;
       
    33 	transient private boolean complete = false;
       
    34 	transient private ArrayList<PwrSample> powerChangePoints;	// times when power samples change
       
    35 	transient private long maxEndTime;
       
    36 	
       
    37     private float voltage = 3.7f;
       
    38 	
       
    39     // used when manually positioning the trace
       
    40     public int offset = 0; 
       
    41 	
       
    42     public float batterySize = 1500f;
       
    43     
       
    44     private double scale = 0.0;
       
    45 	private double min = 0.0;
       
    46 	private double max = 0.0;
       
    47 	private double synchValue = 0.0;
       
    48 	
       
    49 	transient private int[] sampleTimes;
       
    50     transient private int[] ampValues;
       
    51     transient private int[] voltValues;
       
    52     transient private int[] capaValues;
       
    53     transient private double maxAmps = Double.MIN_VALUE;
       
    54 	transient private double minAmps = Double.MAX_VALUE;
       
    55 	transient private double maxPower = 0.0;
       
    56 
       
    57     public GenericTraceGraph getTraceGraph(int graphIndex, int uid)
       
    58 	{		
       
    59 		return getPowerGraph(graphIndex, uid);
       
    60 	}
       
    61 	
       
    62 	public PowerTraceGraph getPowerGraph(int graphIndex, int uid)
       
    63 	{
       
    64 		if (graphs == null) {
       
    65 			graphs = new PowerTraceGraph[3];
       
    66 			maxEndTime = (long) ((PIPageEditor.currentPageEditor().getMaxEndTime() + .0005) * 1000); 
       
    67 		}
       
    68 
       
    69 		// note that graphIndex needs not match the index sent to GppTraceGraph
       
    70 		if (   (graphIndex == PIPageEditor.THREADS_PAGE)
       
    71 			|| (graphIndex == PIPageEditor.BINARIES_PAGE)
       
    72 			|| (graphIndex == PIPageEditor.FUNCTIONS_PAGE)) {
       
    73 			if (graphs[graphIndex] == null)
       
    74 				graphs[graphIndex] = new PowerTraceGraph(graphIndex, uid, this);
       
    75 			return graphs[graphIndex];
       
    76 		}
       
    77 	
       
    78 		return null;
       
    79 	}
       
    80 
       
    81     public float getBatterySize() 
       
    82 	{
       
    83         return batterySize;
       
    84     }
       
    85 
       
    86     public void setBatterySize(float newVal) 
       
    87 	{
       
    88         batterySize = newVal;
       
    89     }
       
    90 
       
    91     public int getOffset() 
       
    92 	{
       
    93         return offset;
       
    94     }
       
    95 
       
    96     public float getVoltage()
       
    97     {
       
    98     	return voltage;
       
    99     }
       
   100 
       
   101 	public int[] getSampleTimes()
       
   102     {
       
   103     	return sampleTimes;
       
   104     }
       
   105 
       
   106 	public int[] getAmpValues()
       
   107     {
       
   108     	return ampValues;
       
   109     }
       
   110 
       
   111     public int[] getVoltValues()
       
   112     {
       
   113     	return voltValues;
       
   114     }
       
   115 
       
   116     public int[] getCapaValues()
       
   117     {
       
   118     	return capaValues;
       
   119     }
       
   120 
       
   121     public double getMaxAmps()
       
   122     {
       
   123     	return maxAmps;
       
   124     }
       
   125 
       
   126     public double getMinAmps()
       
   127     {
       
   128     	return minAmps;
       
   129     }
       
   130 
       
   131 	public double getMaxPower()
       
   132     {
       
   133     	return maxPower;
       
   134     }
       
   135 
       
   136 	public void setVoltage(float voltage)
       
   137     {
       
   138     	this.voltage = voltage;
       
   139     }
       
   140     
       
   141     public void setOffset( int newOffset ) 
       
   142 	{
       
   143 		int mySize = getSampleAmount();
       
   144 		
       
   145 		if( newOffset > mySize )
       
   146 			offset = mySize;
       
   147 		else if( (newOffset * -1) > mySize )
       
   148 			offset = (-1)*mySize;
       
   149 		else
       
   150 			offset = newOffset;
       
   151     }
       
   152     
       
   153 	public double getScale()
       
   154 	{
       
   155 		return scale;
       
   156 	}
       
   157 	
       
   158 	public double getMin()
       
   159 	{
       
   160 		return min;
       
   161 	}
       
   162 	
       
   163 	public double getMax()
       
   164 	{
       
   165 		return max;
       
   166 	}
       
   167 	
       
   168 	public long getMaxEndTime()
       
   169 	{
       
   170 		return maxEndTime;
       
   171 	}
       
   172 	
       
   173 	public double getSynchValue()
       
   174 	{
       
   175 		return synchValue;
       
   176 	}
       
   177 	
       
   178 	public void setSynchValue(double aSynchValue)
       
   179 	{
       
   180 		synchValue = aSynchValue;
       
   181 	}
       
   182 	
       
   183 	public void addSample(PwrSample sample)
       
   184 	{
       
   185 		this.samples.add(sample);
       
   186 	}
       
   187 
       
   188 	public PwrSample getPwrSample(int number)
       
   189 	{
       
   190 		return (PwrSample)this.samples.elementAt(number);
       
   191 	}
       
   192 
       
   193 	// compensate for different sampling speeds. 
       
   194 	// the software traces are done at 1KHz and the 
       
   195 	// power traces are typically done at 250Khz.
       
   196 	//
       
   197 	public void initData()
       
   198 	{
       
   199 		ArrayList<PwrSample> powerChangePoints = new ArrayList<PwrSample>();
       
   200 		PwrSample sample = (PwrSample) this.samples.get(0);
       
   201 			
       
   202 		long   currentTime     = sample.sampleSynchTime;
       
   203 		double currentCurrent  = sample.current;
       
   204 		double currentVoltage  = sample.voltage;
       
   205 		double currentCapacity = sample.capacity;
       
   206 				
       
   207 		for (int i = 1; i < this.samples.size(); i++) {
       
   208 			sample = (PwrSample) this.samples.get(i);
       
   209 			if (   sample.current  != currentCurrent
       
   210 				|| sample.voltage  != currentVoltage
       
   211 				|| sample.capacity != currentCapacity) {
       
   212 				PwrSample addSample = new PwrSample(currentTime, currentCurrent, currentVoltage, currentCapacity);
       
   213 				powerChangePoints.add(addSample);
       
   214 				currentTime     = sample.sampleSynchTime;
       
   215 				currentCurrent  = sample.current;
       
   216 				currentVoltage  = sample.voltage;
       
   217 				currentCapacity = sample.capacity;
       
   218 			}
       
   219 		}
       
   220 		
       
   221 		sample = powerChangePoints.get(powerChangePoints.size() - 1);
       
   222 		if (   sample.current  != currentCurrent
       
   223 			|| sample.voltage  != currentVoltage
       
   224 			|| sample.capacity != currentCapacity) {
       
   225 			powerChangePoints.add(new PwrSample(currentTime, currentCurrent, currentVoltage, currentCapacity));
       
   226 		}
       
   227 
       
   228 		this.powerChangePoints = powerChangePoints;
       
   229 		
       
   230 		sampleTimes = new int[this.powerChangePoints.size()];
       
   231         ampValues   = new int[this.powerChangePoints.size()];
       
   232         voltValues  = new int[this.powerChangePoints.size()];
       
   233         capaValues  = new int[this.powerChangePoints.size()];
       
   234 
       
   235 		int localMaxAmps = Integer.MIN_VALUE;
       
   236 		int localMinAmps = Integer.MAX_VALUE;
       
   237 
       
   238 		for (int i = 0; i < this.powerChangePoints.size(); i++)
       
   239 	    {
       
   240 			PwrSample tmp = this.powerChangePoints.get(i);
       
   241 			sampleTimes[i] = (int)Math.abs(tmp.sampleSynchTime);		
       
   242 			ampValues[i]   = (int)Math.abs(tmp.current);	// in milliamps
       
   243 			voltValues[i]  = (int)Math.abs(tmp.voltage);	// in millivolts
       
   244 			capaValues[i]  = (int)Math.abs(tmp.capacity);
       
   245 
       
   246 			if (ampValues[i] > localMaxAmps)
       
   247 			{
       
   248 				localMaxAmps = ampValues[i];
       
   249 			}
       
   250 			else if (ampValues[i] < minAmps )
       
   251 			{
       
   252 				localMinAmps = ampValues[i];
       
   253 			}
       
   254 		}
       
   255 	    
       
   256 	    maxAmps = localMaxAmps;
       
   257 	    minAmps = localMinAmps;
       
   258 
       
   259 	    scaleMaxPower();
       
   260 	}
       
   261 	
       
   262 	public ArrayList<PwrSample> getPowerChangePoints()
       
   263 	{
       
   264 		return this.powerChangePoints;
       
   265 	}
       
   266 	
       
   267 	public void scaleMaxPower()
       
   268 	{
       
   269 		maxPower = maxAmps * this.voltage;
       
   270 
       
   271 		if (maxPower < 10)
       
   272 	    	maxPower =     10;
       
   273 		else if (maxPower <     20)
       
   274 	    	maxPower =     20;
       
   275 	    else if (maxPower <     50)
       
   276 	    	maxPower =     50;
       
   277 	    else if (maxPower <    100)
       
   278 	    	maxPower =    100;
       
   279 	    else if (maxPower <    200)
       
   280 	    	maxPower =    200;
       
   281 	    else if (maxPower <    500)
       
   282 	    	maxPower =    500;
       
   283 	    else if (maxPower <   1000)
       
   284 	    	maxPower =   1000;
       
   285 	    else if (maxPower <   2000)
       
   286 	    	maxPower =   2000;
       
   287 	    else if (maxPower <   5000)
       
   288 	    	maxPower =   5000;
       
   289 	    else if (maxPower <  10000)
       
   290 	    	maxPower =  10000;
       
   291 	    else if (maxPower <  20000)
       
   292 	    	maxPower =  20000;
       
   293 	    else if (maxPower <  50000)
       
   294 	    	maxPower =  50000;
       
   295 	    else if (maxPower < 100000)
       
   296 	    	maxPower = 100000;
       
   297 	}
       
   298 
       
   299   	/*
       
   300   	 * Check if the power trace is complete (first sample is at time 1, sample N is at time N)
       
   301   	 */
       
   302   	public void setComplete()
       
   303   	{
       
   304 		int samplingInterval = (Integer) NpiInstanceRepository.getInstance().activeUidGetPersistState("com.nokia.carbide.cpp.pi.address.samplingInterval"); //$NON-NLS-1$
       
   305   		int size;
       
   306   		
       
   307   		this.complete = true;
       
   308   		
       
   309    		size = this.samples.size();
       
   310   		for (int i = 0; i < size; i++) {
       
   311   			PwrSample element = (PwrSample)this.samples.get(i);
       
   312   			if (element.sampleSynchTime != i*samplingInterval) {
       
   313   				this.complete = false;
       
   314   				break;
       
   315   			}
       
   316   		}
       
   317   	}
       
   318   	
       
   319   	public boolean isComplete() {
       
   320   		return this.complete;
       
   321   	}
       
   322 }