sysperfana/memspyext/com.nokia.s60tools.swmtanalyser/src/com/nokia/s60tools/swmtanalyser/ui/graphs/GenericGraph.java
changeset 7 8e12a575a9b5
equal deleted inserted replaced
6:f65f740e69f9 7:8e12a575a9b5
       
     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 "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 package com.nokia.s60tools.swmtanalyser.ui.graphs;
       
    18 
       
    19 import java.text.DecimalFormat;
       
    20 import java.util.ArrayList;
       
    21 
       
    22 import org.eclipse.draw2d.FigureCanvas;
       
    23 import org.eclipse.draw2d.Graphics;
       
    24 import org.eclipse.draw2d.geometry.Rectangle;
       
    25 import org.eclipse.swt.graphics.Color;
       
    26 import org.eclipse.swt.graphics.GC;
       
    27 import org.eclipse.swt.graphics.Image;
       
    28 import org.eclipse.swt.graphics.RGB;
       
    29 import org.eclipse.swt.widgets.Display;
       
    30 
       
    31 import com.nokia.s60tools.swmtanalyser.data.CycleData;
       
    32 import com.nokia.s60tools.swmtanalyser.data.KernelElements;
       
    33 import com.nokia.s60tools.swmtanalyser.data.ParsedData;
       
    34 import com.nokia.s60tools.swmtanalyser.model.SWMTLogReaderUtils;
       
    35 
       
    36 public abstract class GenericGraph {
       
    37 
       
    38 	//
       
    39 	// Constants
       
    40 	//
       
    41 	
       
    42 	/**
       
    43 	 * Mega Byte format
       
    44 	 */
       
    45 	protected static final DecimalFormat MBformat = new DecimalFormat("#####.0");
       
    46 	/**
       
    47 	 * Byte format
       
    48 	 */
       
    49 	protected static final DecimalFormat Bytes_Format = new DecimalFormat("#####.##");	
       
    50 	/**
       
    51 	 * Label for time in X axis
       
    52 	 */
       
    53 	protected static final String TIME_X_AXIS_LABEL = "Time (h:min:s)";	
       
    54 	
       
    55 	//
       
    56 	// Members
       
    57 	// 
       
    58 	private EventTypes event;
       
    59 	private ArrayList<String> userSelectedItems;
       
    60 	private ArrayList<Color> colors;
       
    61 	private ParsedData parsedData;
       
    62 	private double scale = 1.0;
       
    63 	
       
    64 	
       
    65 	protected int maxBytes = 10;	
       
    66 	protected int[] valuesToBePlotted = null;
       
    67 	protected int lastSampleTime = 0;
       
    68 	protected int visualSizeY = 0;
       
    69 	protected int timeOffset = 0;
       
    70 	
       
    71 	/**
       
    72 	 * Event types to map the event names into corresponding event enumerators.
       
    73 	 * @see com.nokia.s60tools.swmtanalyser.ui.graphs.GraphsUtils#EVENT_NAMES_ARR
       
    74 	 */
       
    75 	public enum EventTypes {GLOBAL_DATA_SIZE, NON_HEAP_CHUNK_SIZE, DISK_USED_SIZE, DISK_TOTAL_SIZE, NO_OF_FILES, MAX_HEAP_SIZE, HEAP_SIZE, HEAP_ALLOC_SPACE, HEAP_FREE_SPACE, HEAP_ALLOC_CELL_COUNT, HEAP_FREE_CELL_COUNT, HEAP_FREE_SLACK, NO_OF_PSHANDLES, RAM_USED, RAM_TOTAL, SYSTEM_DATA};
       
    76 		
       
    77 	private static ArrayList<String> graphableKernels = new ArrayList<String>();
       
    78 	
       
    79 	/**
       
    80 	 * Get graphable kernels
       
    81 	 * @return the graphable kernels
       
    82 	 */
       
    83 	public static ArrayList<String> getGraphableKernels() {
       
    84 		return graphableKernels;
       
    85 	}
       
    86 
       
    87 	static{
       
    88 		graphableKernels.add("Number of Processes");
       
    89 		graphableKernels.add("Number of Threads");
       
    90 		graphableKernels.add("Number of Timers");
       
    91 		graphableKernels.add("Number of Semaphores");
       
    92 		graphableKernels.add("Number of Servers");
       
    93 		graphableKernels.add("Number of Sessions");
       
    94 		graphableKernels.add("Number of Chunks");
       
    95 		graphableKernels.add("Number of Msg. Queues");
       
    96 	}
       
    97 	
       
    98 	/**
       
    99 	 * Get cycles data
       
   100 	 * @return cycle data
       
   101 	 */
       
   102 	public ParsedData getCyclesData() {
       
   103 		return parsedData;
       
   104 	}
       
   105 
       
   106 	/**
       
   107 	 * Set cycles data
       
   108 	 * @param parsedData
       
   109 	 */
       
   110 	public void setCyclesData(ParsedData parsedData) {
       
   111 		this.parsedData = parsedData;
       
   112 	}
       
   113 
       
   114 	/**
       
   115 	 * Get event
       
   116 	 * @return event
       
   117 	 */
       
   118 	public EventTypes getEvent() {
       
   119 		return event;
       
   120 	}
       
   121 
       
   122 	/**
       
   123 	 * Set event
       
   124 	 * @param event
       
   125 	 */
       
   126 	public void setEvent(EventTypes event) {
       
   127 		this.event = event;
       
   128 	}
       
   129 
       
   130 	/**
       
   131 	 * Get items that user has been selected
       
   132 	 * @return selected items
       
   133 	 */
       
   134 	public ArrayList<String> getUserSelectedItems() {
       
   135 		return userSelectedItems;
       
   136 	}
       
   137 
       
   138 	/**
       
   139 	 * Set items that user has been selected
       
   140 	 * @param userSelectedItems
       
   141 	 */
       
   142 	public void setUserSelectedItems(ArrayList<String> userSelectedItems) {
       
   143 		this.userSelectedItems = userSelectedItems;
       
   144 	}
       
   145 
       
   146 	/**
       
   147 	 * Draw contents to drawn area
       
   148 	 * @param graphics
       
   149 	 */
       
   150 	public abstract void paint(Graphics graphics);
       
   151 	/**
       
   152 	 * Prepare data for drawing
       
   153 	 */
       
   154 	public abstract void prepareData();
       
   155 	/**
       
   156 	 * Draw headers to Y axis
       
   157 	 * @param gc
       
   158 	 */
       
   159 	public abstract void paintYAxis(GC gc);
       
   160 	/**
       
   161 	 * Get tool tip text for coordinates
       
   162 	 * @param x
       
   163 	 * @param y
       
   164 	 * @return tooltip text
       
   165 	 */
       
   166 	public abstract String getToolTipText(int x, int y);
       
   167 		
       
   168 	/**
       
   169 	 * Draw lines to background
       
   170 	 * @param canvas
       
   171 	 * @param graphics
       
   172 	 */
       
   173 	public void drawBackGroundLines(FigureCanvas canvas, Graphics graphics)
       
   174 	{
       
   175 		Rectangle canvasRect = graphics.getClip(new org.eclipse.draw2d.geometry.Rectangle());
       
   176 	  	graphics.setForegroundColor(new Color(Display.getDefault(), new RGB(200, 200, 200)));
       
   177 	 	graphics.setBackgroundColor(new Color(Display.getDefault(), new RGB(170,170,170)));
       
   178 		
       
   179 	 	int height = canvas.getClientArea().height;
       
   180 	  	int width = canvas.getClientArea().width;
       
   181 	  	  	
       
   182 	  	graphics.fillRectangle(new Rectangle(canvasRect.x,0,width, height-50));
       
   183 	  	
       
   184 	    double visY = height - CommonGraphConstants.XLEGENDSPACE;
       
   185 	    			
       
   186 		int k = 0;
       
   187 		
       
   188 		for (float y = 0; k <= 10; y += visY * 10000 / 100001, k++)
       
   189 		{
       
   190 			for (int x = canvasRect.x; x <= canvasRect.x + canvasRect.width; x += 5)
       
   191 			{
       
   192 				if ((x / 5) % 2 == 0) graphics.drawLine(x, ((int)y) + 1, x + 5, ((int)y) + 1);
       
   193 			}
       
   194 		}
       
   195 		
       
   196 		graphics.setForegroundColor(new Color(Display.getDefault(), new RGB(100, 100, 100)));
       
   197 		graphics.setBackgroundColor(new Color(Display.getDefault(),new RGB(255, 255, 255)));
       
   198 		
       
   199 		// horizontal lines
       
   200 		if (width > 0)
       
   201 		{
       
   202 			for (int x = 0; x <= canvasRect.x + canvasRect.width; x += 50)
       
   203 			{	
       
   204 				if (x % 100 == 0)
       
   205 					graphics.setForegroundColor(new Color(Display.getDefault(), new RGB(100, 100, 100)));
       
   206 				else
       
   207 					graphics.setForegroundColor(new Color(Display.getDefault(),new RGB(200, 200, 200)));
       
   208 				
       
   209 				for (int y = 0; y < height; y += 5)
       
   210 				{
       
   211 					if ((y / 5) % 2 == 0)
       
   212 						graphics.drawLine(x, y, x, y + 5);
       
   213 				}
       
   214 			}
       
   215 		}
       
   216 		
       
   217 		graphics.setForegroundColor(new Color(Display.getDefault(), new RGB(100, 100, 100)));
       
   218 		graphics.setBackgroundColor(new Color(Display.getDefault(),new RGB(255, 255, 255)));
       
   219 		
       
   220 		for (int x = 0; x <= canvasRect.x + canvasRect.width; x += 50)
       
   221 		{
       
   222 			double time = (double) x;
       
   223 			TimeObject timeObj = new TimeObject(time, scale);
       
   224 			graphics.drawString(timeObj.getHourMinutesAndSeconds(), x + 5, height - 13);
       
   225 			if(timeObj.hasDays()){
       
   226 				graphics.drawString(timeObj.getDays(), x + 5, height - 26);
       
   227 			}
       
   228 		}
       
   229 		Image img = getVerticalLabel(TIME_X_AXIS_LABEL);
       
   230 		graphics.drawImage(img, width/2, height-30);
       
   231 	}
       
   232 
       
   233 	/**
       
   234 	 * Called when horizontal bar is moved in graphs, to set X location of current selection.
       
   235 	 * @param x X-location of {@link org.eclipse.draw2d.geometry.Point}
       
   236 	 */
       
   237 	public void setScrolledXOrigin(int x)
       
   238 	{
       
   239 		this.timeOffset = x;
       
   240 	}
       
   241 
       
   242 	/**
       
   243 	 * Get cycle times from parsed data
       
   244 	 * @return list of times
       
   245 	 */
       
   246 	protected int [] calculateTimeIntervals()
       
   247 	{
       
   248 		int [] time = new int[parsedData.getNumberOfCycles()];
       
   249 		int prevDuration = 0;
       
   250 		time[0] = 0;
       
   251 				
       
   252 		SWMTLogReaderUtils utils = new SWMTLogReaderUtils();
       
   253 		
       
   254 		CycleData [] cycles = parsedData.getLogData();
       
   255 		for(int i=1; i<parsedData.getNumberOfCycles();i++)
       
   256 		{
       
   257 			String currentTime = cycles[i].getTime();
       
   258 			String prevTime = cycles[i-1].getTime();
       
   259 			int timeDiff = (int)utils.getDurationInSeconds(prevTime, currentTime);
       
   260 						
       
   261 			if(timeDiff < 0)
       
   262 			{
       
   263 				//error condition
       
   264 			}
       
   265 			else
       
   266 			{
       
   267 				timeDiff += prevDuration;
       
   268 				prevDuration = timeDiff;
       
   269 			
       
   270 				time[i] = timeDiff;
       
   271 			}
       
   272 			
       
   273 		}
       
   274 		
       
   275 		return time;
       
   276 	}
       
   277 	
       
   278 	/**
       
   279 	 * Get largest value from given items
       
   280 	 * @param values
       
   281 	 * @return largest value found
       
   282 	 */
       
   283 	protected int calculateMaxValue(int [] values)
       
   284 	{
       
   285 		int maxValue = 0;
       
   286 		
       
   287 		for(int i=0; i<values.length; i++)
       
   288 		{
       
   289 			if(values[i] > maxValue)
       
   290 				maxValue = values[i];
       
   291 		}
       
   292 		
       
   293 		return maxValue;
       
   294 	}
       
   295 
       
   296 	/**
       
   297 	 * Get colors
       
   298 	 * @return colors
       
   299 	 */
       
   300 	public ArrayList<Color> getColors() {
       
   301 		return colors;
       
   302 	}
       
   303 
       
   304 	/**
       
   305 	 * Set colors
       
   306 	 * @param colors
       
   307 	 */
       
   308 	public void setColors(ArrayList<Color> colors) {
       
   309 		this.colors = colors;
       
   310 	}
       
   311 		
       
   312 	/**
       
   313 	 * Get label for vertical axis
       
   314 	 * @param name
       
   315 	 * @return label
       
   316 	 */
       
   317 	protected Image getVerticalLabel(String name)
       
   318 	{
       
   319 		return GraphsUtils.getVerticalLabel(name, 110, 15, 9);
       
   320 	}
       
   321 	
       
   322 	/**
       
   323 	 * Get scale
       
   324 	 * @return scale
       
   325 	 */
       
   326 	public double getScale() {
       
   327 		return scale;
       
   328 	}
       
   329 
       
   330 	/**
       
   331 	 * Set scale
       
   332 	 * @param scale
       
   333 	 */
       
   334 	public void setScale(double scale) {
       
   335 		this.scale = scale;
       
   336 	}
       
   337 	
       
   338 	/**
       
   339 	 * Set visual size
       
   340 	 * @param height
       
   341 	 */
       
   342 	public void setVisualSize(int height)
       
   343 	{
       
   344 		this.visualSizeY = height;
       
   345 	}
       
   346 
       
   347 	/**
       
   348 	 * Get count for wanted item
       
   349 	 * @param item
       
   350 	 * @param kernelsList
       
   351 	 * @return count of items wanted
       
   352 	 */
       
   353 	protected int [] getValuesForGivenKerenelElement(String item, ArrayList<KernelElements> kernelsList)
       
   354 	{
       
   355 		int [] values = new int[kernelsList.size()];
       
   356 
       
   357 		int index = graphableKernels.indexOf(item);		
       
   358 		
       
   359 		for(int i=0; i<kernelsList.size(); i++)
       
   360 		{
       
   361 			KernelElements kernels = kernelsList.get(i);
       
   362 			
       
   363 			if(index == 0)
       
   364 				values [i] = kernels.getNumberOfProcesses();
       
   365 			else if(index == 1)
       
   366 				values[i] = kernels.getNumberOfThreads();
       
   367 			else if(index == 2)
       
   368 				values [i] = kernels.getNumberOfTimers();
       
   369 			else if(index == 3)
       
   370 				values [i] = kernels.getNumberOfSemaphores();
       
   371 			else if(index == 4)
       
   372 				values [i] = kernels.getNumberOfServers();
       
   373 			else if(index == 5)
       
   374 				values [i] = kernels.getNumberOfSessions();
       
   375 			else if(index == 6)
       
   376 				values [i] = kernels.getNumberOfChunks();
       
   377 			else if(index == 7)
       
   378 				values [i] = kernels.getNumberOfMsgQueues();
       
   379 	
       
   380 		}
       
   381 			
       
   382 		return values;
       
   383 		
       
   384 	}
       
   385 }