1 /*****************************************************************************
2 * Copyright (c) 2007, 2008 Intel Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Intel Corporation - Initial API and implementation
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation
11 * Alvaro Sanchex-Leon - Udpated for TMF
13 * $Id: Utils.java,v 1.11 2008/06/16 21:04:49 jkubasta Exp $
14 *****************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.widgets
;
18 import java
.text
.SimpleDateFormat
;
19 import java
.util
.Date
;
20 import java
.util
.Iterator
;
21 import java
.util
.List
;
22 import java
.util
.TimeZone
;
23 import java
.util
.Vector
;
25 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.ITimeAnalysisViewer
.TimeFormat
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITimeEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITmfTimeAnalysisEntry
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.TimeEvent
;
29 import org
.eclipse
.swt
.graphics
.Color
;
30 import org
.eclipse
.swt
.graphics
.Device
;
31 import org
.eclipse
.swt
.graphics
.GC
;
32 import org
.eclipse
.swt
.graphics
.Point
;
33 import org
.eclipse
.swt
.graphics
.Rectangle
;
34 import org
.eclipse
.swt
.widgets
.Display
;
38 static public final int IMG_THREAD_RUNNING
= 0;
39 static public final int IMG_THREAD_SUSPENDED
= 1;
40 static public final int IMG_THREAD_STOPPED
= 2;
41 static public final int IMG_METHOD_RUNNING
= 3;
42 static public final int IMG_METHOD
= 4;
43 static public final int IMG_NUM
= 5;
45 static public final Object
[] _empty
= new Object
[0];
47 static enum Resolution
{
48 SECONDS
, MILLISEC
, MICROSEC
, NANOSEC
51 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss");
52 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd");
54 stimeformat
.setTimeZone(TimeZone
.getTimeZone("GMT"));
55 sdateformat
.setTimeZone(TimeZone
.getTimeZone("GMT"));
58 // static private String _externalPlugin[] = { "org.eclipse.debug.ui",
59 // "org.eclipse.debug.ui", "org.eclipse.debug.ui",
60 // "org.eclipse.debug.ui", "org.eclipse.debug.ui", };
62 // static private String _externalPath[] = {
63 // "icons/full/obj16/thread_obj.gif", // running thread
64 // "icons/full/obj16/threads_obj.gif", // suspended
65 // "icons/full/obj16/threadt_obj.gif", // stopped
66 // "icons/full/obj16/stckframe_running_obj.gif", // running stack frame
67 // "icons/full/obj16/stckframe_obj.gif", // stack frame
70 // static public Image getImage(int idx) {
71 // if (idx < 0 || idx >= IMG_NUM)
72 // SWT.error(SWT.ERROR_INVALID_ARGUMENT);
73 // String key = "trace.img." + idx;
74 // Image img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
76 // ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(
77 // _externalPlugin[idx], _externalPath[idx]);
78 // TimeAnalysisPlugin.getDefault().getImageRegistry().put(key, desc);
79 // img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
84 static public void init(Rectangle rect
) {
91 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
98 static public void init(Rectangle rect
, Rectangle source
) {
101 rect
.width
= source
.width
;
102 rect
.height
= source
.height
;
105 static public void deflate(Rectangle rect
, int x
, int y
) {
109 rect
.height
-= y
+ y
;
112 static public void inflate(Rectangle rect
, int x
, int y
) {
116 rect
.height
+= y
+ y
;
119 static void dispose(Color col
) {
124 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
126 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
127 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
128 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
132 static public Color
getSysColor(int id
) {
133 Color col
= Display
.getCurrent().getSystemColor(id
);
134 return new Color(col
.getDevice(), col
.getRGB());
137 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
138 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
141 static public int drawText(GC gc
, String text
, Rectangle rect
,
143 Point size
= gc
.stringExtent(text
);
144 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
148 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
149 Point size
= gc
.stringExtent(text
);
150 gc
.drawText(text
, x
, y
, transp
);
155 * Formats time in format: MM:SS:NNN
159 * 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
162 static public String
formatTime(long v
, TimeFormat format
, Resolution res
) {
163 // if format is absolute (Calendar)
164 if (format
== TimeFormat
.ABSOLUTE
) {
165 return formatTimeAbs(v
, res
);
168 StringBuffer str
= new StringBuffer();
175 long sec
= (long) (v
* 1E-9);
176 // TODO: Expand to make it possible to select the minute, second, nanosecond format
177 //printing minutes is suppressed just sec and ns
178 // if (sec / 60 < 10)
180 // str.append(sec / 60);
186 String ns
= formatNs(v
, res
);
187 if (!ns
.equals("")) {
192 return str
.toString();
196 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
201 public static String
formatDate(long absTime
) {
202 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
207 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
212 static public String
formatTimeAbs(long time
, Resolution res
) {
213 StringBuffer str
= new StringBuffer();
215 // format time from nanoseconds to calendar time HH:MM:SS
216 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
217 str
.append(stime
+ " ");
218 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
220 str
.append(formatNs(time
, res
));
221 return str
.toString();
225 * Obtains the remainder fraction on unit Seconds of the entered value in
226 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
227 * seconds can be obtained by removing the last 9 digits: 1241207054 the
228 * fractional portion of seconds, expressed in ns is: 171080214
234 public static String
formatNs(long time
, Resolution res
) {
235 StringBuffer temp
= new StringBuffer();
236 boolean neg
= time
< 0;
241 // The following approach could be used although performance
242 // decreases in half.
243 // String strVal = String.format("%09d", time);
244 // String tmp = strVal.substring(strVal.length() - 9);
246 // number of segments to be included
265 temp
.append("00000000");
266 } else if (ns
< 100) {
267 temp
.append("0000000");
268 } else if (ns
< 1000) {
269 temp
.append("000000");
270 } else if (ns
< 10000) {
271 temp
.append("00000");
272 } else if (ns
< 100000) {
274 } else if (ns
< 1000000) {
276 } else if (ns
< 10000000) {
278 } else if (ns
< 100000000) {
283 StringBuffer str
= new StringBuffer();
286 str
.append(temp
.substring(0, 3));
291 str
.append(temp
.substring(3, 6));
294 // append Nano seconds
296 str
.append(temp
.substring(6));
299 return str
.toString();
302 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
304 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
314 // static public int loadIntOption(String opt) {
315 // int val = TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
319 static public void saveIntOption(String opt
, int val
) {
320 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
323 static ITimeEvent
getFirstEvent(ITmfTimeAnalysisEntry thread
) {
326 Vector
<TimeEvent
> list
= thread
.getTraceEvents();
327 ITimeEvent event
= null;
329 event
= (ITimeEvent
) list
.get(0);
334 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
335 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
343 static ITimeEvent
findEvent(ITmfTimeAnalysisEntry thread
, long time
, int n
) {
346 List
<TimeEvent
> list
= thread
.getTraceEvents();
347 Iterator
<TimeEvent
> it
= list
.iterator();
348 ITimeEvent nextEvent
= null;
349 ITimeEvent currEvent
= null;
350 ITimeEvent prevEvent
= null;
352 while (it
.hasNext()) {
353 nextEvent
= (ITimeEvent
) it
.next();
354 long nextStartTime
= nextEvent
.getTime();
356 if (nextStartTime
> time
) {
360 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
361 prevEvent
= currEvent
;
362 currEvent
= nextEvent
;
366 if (n
== -1) { //previous
367 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
372 } else if (n
== 0) { //current
373 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
378 } else if (n
== 1) { //next
380 } else if (n
== 2) { //current or previous when in empty space
387 // static public TRCPackage getPackage(Object element) {
388 // if (element instanceof TRCPackage)
389 // return (TRCPackage) element;
390 // if (element instanceof TRCClass)
391 // return ((TRCClass) element).getPackage();
395 // static public TRCObjectAllocationAnnotation getAllocationAnnotation(
397 // TRCObjectAllocationAnnotation aa = null;
398 // EList list = cls.getAnnotations();
399 // int len = list.size();
400 // for (int i = 0; i < len; i++) {
401 // TRCAnnotation annotation = (TRCAnnotation) list.get(i);
402 // if (annotation instanceof TRCObjectAllocationAnnotation)
403 // aa = (TRCObjectAllocationAnnotation) annotation;
408 static public String
fixMethodSignature(String sig
) {
409 int pos
= sig
.indexOf('(');
411 String ret
= sig
.substring(0, pos
);
412 sig
= sig
.substring(pos
);
413 sig
= sig
+ " " + ret
;
418 static public String
restoreMethodSignature(String sig
) {
420 int pos
= sig
.indexOf('(');
422 ret
= sig
.substring(0, pos
);
423 sig
= sig
.substring(pos
+ 1);
425 pos
= sig
.indexOf(')');
427 sig
= sig
.substring(0, pos
);
429 String args
[] = sig
.split(",");
431 for (int i
= 0; i
< args
.length
; i
++) {
432 String arg
= args
[i
].trim();
433 if (arg
.length() == 0 && args
.length
== 1)
435 sig
+= getTypeSignature(arg
);
437 sig
+= ")" + getTypeSignature(ret
);
441 static public String
getTypeSignature(String type
) {
443 for (int j
= 0; j
< type
.length(); j
++) {
444 if (type
.charAt(j
) == '[')
447 int pos
= type
.indexOf('[');
449 type
= type
.substring(0, pos
);
451 for (int j
= 0; j
< dim
; j
++)
453 if (type
.equals("boolean"))
455 else if (type
.equals("byte"))
457 else if (type
.equals("char"))
459 else if (type
.equals("short"))
461 else if (type
.equals("int"))
463 else if (type
.equals("long"))
465 else if (type
.equals("float"))
467 else if (type
.equals("double"))
469 else if (type
.equals("void"))
472 sig
+= "L" + type
.replace('.', '/') + ";";
476 // static public boolean openSource(Object element) {
477 // if (element instanceof String) {
478 // final String pattern = (String) element;
479 // final int javaType = IJavaSearchConstants.METHOD;
480 // BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
481 // public void run() {
482 // if (!OpenJavaSource.openSource(pattern, javaType,
483 // SearchEngine.createWorkspaceScope(), true)) {
484 // MessageDialog.openInformation(UIPlugin.getDefault()
485 // .getWorkbench().getActiveWorkbenchWindow()
486 // .getShell(), TraceMessages.TRC_MSGT, NLS.bind(
487 // TraceUIMessages._68, pattern));
492 // OpenSource.openSource(element);
496 // static public int getObjAge(TRCFullTraceObject obj, EList listGC) {
498 // double t0 = obj.getCreateTime();
499 // double t1 = obj.getCollectTime();
500 // int len = listGC.size();
501 // for (int j = 0; j < len; j++) {
502 // TRCGCEvent gcEvent = (TRCGCEvent) listGC.get(j);
503 // if (gcEvent.getType().equals("finish")) {
504 // double time = gcEvent.getTime();
507 // if (t1 > 0 && time >= t1)
515 static public int compare(double d1
, double d2
) {
523 static public int compare(String s1
, String s2
) {
524 if (s1
!= null && s2
!= null)
525 return s1
.compareToIgnoreCase(s2
);
533 // static public String formatPercent(int val, int max) {
534 // String s = max > 0 && max >= val ? TString
535 // .formatAsPercentage((double) val / (double) max) : "";