sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.peccommon/src/com/nokia/carbide/cpp/pi/peccommon/PecCommonTrace.java
changeset 5 844b047e260d
child 12 ae255c9aa552
equal deleted inserted replaced
4:615035072f7e 5:844b047e260d
       
     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.peccommon;
       
    19 
       
    20 import java.util.Arrays;
       
    21 import java.util.Vector;
       
    22 
       
    23 import com.nokia.carbide.cpp.internal.pi.model.GenericSample;
       
    24 import com.nokia.carbide.cpp.internal.pi.model.GenericSampledTrace;
       
    25 
       
    26 /**
       
    27  * The model class for Performance Counter traces. This manages the data
       
    28  * for the IPC trace graphs, and is responsible for creating the graphs. 
       
    29  */
       
    30 public class PecCommonTrace extends GenericSampledTrace {
       
    31 	private static final long serialVersionUID = 4425739452429422333L;
       
    32 
       
    33 	private int samplingInterval;
       
    34 	
       
    35 	/**
       
    36 	 * hold the event types which have been traced
       
    37 	 */
       
    38 	private String[] valueTypes;
       
    39 
       
    40 	// the following is needed for calculations for the legend (depend on
       
    41 	// selected area). We keep it here since the selected area is the same for all graphs.
       
    42 	// If this assumption ever changes, the calculations need to be moved into
       
    43 	// the LegendContentProvider
       
    44 	private double selectionStart;
       
    45 	private double selectionEnd;
       
    46 	private transient boolean needsRecalc;
       
    47 
       
    48 	private transient PecCommonLegendElement[] legendElements;
       
    49 	
       
    50 	/**
       
    51 	 * Constructor
       
    52 	 */
       
    53 	public PecCommonTrace() {
       
    54 		super();
       
    55 		selectionStart = -1;
       
    56 		selectionEnd = -1;
       
    57 		needsRecalc = true;
       
    58 	}
       
    59 	
       
    60 	
       
    61 
       
    62 	/**
       
    63 	 * Set the event types present in the interconnect performance counter trace.
       
    64 	 * @param valueTypes the event types to set
       
    65 	 */
       
    66 	public void setValueTypes(String[] valueTypes) {
       
    67 		this.valueTypes = valueTypes;
       
    68 	}
       
    69 
       
    70 	/**
       
    71 	 * Get the event types present in the interconnect performance counter trace.
       
    72 	 * @return the event types present in the trace
       
    73 	 */
       
    74 	public String[] getValueTypes() {
       
    75 		return valueTypes;
       
    76 	}
       
    77 
       
    78 	/**
       
    79 	 * @return the sampling interval in milliseconds
       
    80 	 */
       
    81 	public int getSamplingInterval() {
       
    82 		return samplingInterval;
       
    83 	}
       
    84 
       
    85 	/**
       
    86 	 * Setter for the sampling interval in milliseconds
       
    87 	 * @param samplingInterval the sampling interval to set
       
    88 	 */
       
    89 	public void setSamplingInterval(int samplingInterval) {
       
    90 		this.samplingInterval = samplingInterval;
       
    91 	}
       
    92 	
       
    93 	/**
       
    94 	 * Callback for PIEvent.SELECTION_AREA_CHANGED
       
    95 	 * @param newStart new selection start
       
    96 	 * @param newEnd new selection end
       
    97 	 */
       
    98 	public void selectionAreaChanged(double newStart, double newEnd) {
       
    99 		if (newStart != selectionStart || newEnd != selectionEnd){
       
   100 			selectionStart = newStart;
       
   101 			selectionEnd = newEnd;
       
   102 			needsRecalc = true;
       
   103 		}
       
   104 	}    
       
   105 	
       
   106     /**
       
   107 	 * @return the legendElements
       
   108 	 */
       
   109 	public PecCommonLegendElement[] getLegendElements() {
       
   110 		if (needsRecalc || legendElements == null){
       
   111 			needsRecalc = false;
       
   112 			calculateValues();
       
   113 		}
       
   114 		return legendElements;
       
   115 	}
       
   116 	
       
   117 	/**
       
   118 	 * calculates values for the legend depending on the selected time frame
       
   119 	 */
       
   120 	private void calculateValues() {
       
   121     	int c = this.valueTypes.length;
       
   122 		int[] cnts = new int[c];
       
   123 		long[] sums = new long[c];
       
   124 		int[] mins = new int[c];
       
   125 		int[] maxs = new int[c];
       
   126 
       
   127 		if (selectionStart >= 0 && selectionEnd >= 0){
       
   128 			
       
   129 			Arrays.fill(mins, Integer.MAX_VALUE);
       
   130 			Arrays.fill(maxs, Integer.MIN_VALUE);
       
   131 			
       
   132 			int start = (int) ((selectionStart + 0.5f));
       
   133 			int end = (int) ((selectionEnd + 0.5f));
       
   134 			
       
   135 			Vector<GenericSample> selectedSamples = this.getSamplesInsideTimePeriod(start, end);
       
   136 			
       
   137 			for (GenericSample genericSample : selectedSamples) {
       
   138 				PecCommonSample sample = (PecCommonSample)genericSample;
       
   139 				
       
   140 				//average, sum, min, max
       
   141 				for (int i = 0; i < sample.values.length; i++) {
       
   142 					int value = sample.values[i];
       
   143 					
       
   144 					cnts[i] ++;
       
   145 					sums[i] += value;
       
   146 					if (value < mins[i]){
       
   147 						mins[i] = value;					
       
   148 					} 
       
   149 					if (value > maxs[i]){
       
   150 						maxs[i] = value;					
       
   151 					}
       
   152 				}
       
   153 			}
       
   154 		}
       
   155 		
       
   156 		legendElements = createLegendElements(legendElements, this.valueTypes, cnts, sums, mins, maxs);
       
   157 	}
       
   158 	
       
   159 	/**
       
   160 	 * Creates or updates all legend elements
       
   161 	 * @param existingElements Existing elements, if they need updating. If null, new elements are to be created.
       
   162 	 * @param typeStrings Array of all trace event strings present in the trace 
       
   163 	 * @param cnts Array of all count values (one per graph)
       
   164 	 * @param sums Array of all sum values (one per graph)
       
   165 	 * @param mins Array of all minimum values (one per graph)
       
   166 	 * @param maxs Array of all maximum values (one per graph)
       
   167 	 * @return Array of legend elements created or updated
       
   168 	 */
       
   169 	protected PecCommonLegendElement[] createLegendElements(PecCommonLegendElement[] existingElements, String[] typeStrings, int[] cnts,
       
   170 			long[] sums, int[] mins, int[] maxs) {
       
   171 		
       
   172     	int c = typeStrings.length;
       
   173     	boolean create = existingElements == null;    	
       
   174     	PecCommonLegendElement[] les = create ? new PecCommonLegendElement[c] : existingElements;
       
   175 		char shortTitle = 'A';
       
   176 		
       
   177 		for (int i = 0; i < c; i++) {
       
   178 			
       
   179 			PecCommonLegendElement legendElement = create ? new PecCommonLegendElement(i, typeStrings[i], shortTitle, false) : existingElements[i];
       
   180 			legendElement.setCnt(cnts[i]);
       
   181 			legendElement.setSum(sums[i]);
       
   182 			legendElement.setMax(maxs[i]);
       
   183 			legendElement.setMin(mins[i]);
       
   184 			les[i] = legendElement;
       
   185 			shortTitle ++;
       
   186 		}
       
   187 		return les;
       
   188 	}
       
   189 }