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
.Vector
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.ITimeAnalysisViewer
.TimeFormat
;
25 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.TimeEvent
;
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
.swt
.graphics
.Color
;
29 import org
.eclipse
.swt
.graphics
.Device
;
30 import org
.eclipse
.swt
.graphics
.GC
;
31 import org
.eclipse
.swt
.graphics
.Point
;
32 import org
.eclipse
.swt
.graphics
.Rectangle
;
33 import org
.eclipse
.swt
.widgets
.Display
;
37 static public final int IMG_THREAD_RUNNING
= 0;
38 static public final int IMG_THREAD_SUSPENDED
= 1;
39 static public final int IMG_THREAD_STOPPED
= 2;
40 static public final int IMG_METHOD_RUNNING
= 3;
41 static public final int IMG_METHOD
= 4;
42 static public final int IMG_NUM
= 5;
44 static public final Object
[] _empty
= new Object
[0];
46 static enum Resolution
{
47 SECONDS
, MILLISEC
, MICROSEC
, NANOSEC
50 static private SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss");
51 static private SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy.MM.dd");
53 // static private String _externalPlugin[] = { "org.eclipse.debug.ui",
54 // "org.eclipse.debug.ui", "org.eclipse.debug.ui",
55 // "org.eclipse.debug.ui", "org.eclipse.debug.ui", };
57 // static private String _externalPath[] = {
58 // "icons/full/obj16/thread_obj.gif", // running thread
59 // "icons/full/obj16/threads_obj.gif", // suspended
60 // "icons/full/obj16/threadt_obj.gif", // stopped
61 // "icons/full/obj16/stckframe_running_obj.gif", // running stack frame
62 // "icons/full/obj16/stckframe_obj.gif", // stack frame
65 // static public Image getImage(int idx) {
66 // if (idx < 0 || idx >= IMG_NUM)
67 // SWT.error(SWT.ERROR_INVALID_ARGUMENT);
68 // String key = "trace.img." + idx;
69 // Image img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
71 // ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(
72 // _externalPlugin[idx], _externalPath[idx]);
73 // TimeAnalysisPlugin.getDefault().getImageRegistry().put(key, desc);
74 // img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
79 static public void init(Rectangle rect
) {
86 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
93 static public void init(Rectangle rect
, Rectangle source
) {
96 rect
.width
= source
.width
;
97 rect
.height
= source
.height
;
100 static public void deflate(Rectangle rect
, int x
, int y
) {
104 rect
.height
-= y
+ y
;
107 static public void inflate(Rectangle rect
, int x
, int y
) {
111 rect
.height
+= y
+ y
;
114 static void dispose(Color col
) {
119 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
121 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
122 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
123 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
127 static public Color
getSysColor(int id
) {
128 Color col
= Display
.getCurrent().getSystemColor(id
);
129 return new Color(col
.getDevice(), col
.getRGB());
132 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
133 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
136 static public int drawText(GC gc
, String text
, Rectangle rect
,
138 Point size
= gc
.stringExtent(text
);
139 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
143 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
144 Point size
= gc
.stringExtent(text
);
145 gc
.drawText(text
, x
, y
, transp
);
150 * Formats time in format: MM:SS:NNN
154 * 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
157 static public String
formatTime(long v
, TimeFormat format
, Resolution res
) {
158 // if format is absolute (Calendar)
159 if (format
== TimeFormat
.ABSOLUTE
) {
160 return formatTimeAbs(v
, res
);
163 StringBuffer str
= new StringBuffer();
170 long sec
= (long) (v
* 1E-9);
171 // TODO: Expand to make it possible to select the minute, second, nanosecond format
172 //printing minutes is suppressed just sec and ns
173 // if (sec / 60 < 10)
175 // str.append(sec / 60);
181 String ns
= formatNs(v
, res
);
182 if (!ns
.equals("")) {
187 return str
.toString();
191 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
196 public static String
formatDate(long absTime
) {
197 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
202 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
207 static public String
formatTimeAbs(long time
, Resolution res
) {
208 StringBuffer str
= new StringBuffer();
210 // format time from nanoseconds to calendar time HH:MM:SS
211 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
212 str
.append(stime
+ " ");
213 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
215 str
.append(formatNs(time
, res
));
216 return str
.toString();
220 * Obtains the remainder fraction on unit Seconds of the entered value in
221 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
222 * seconds can be obtained by removing the last 9 digits: 1241207054 the
223 * fractional portion of seconds, expressed in ns is: 171080214
229 public static String
formatNs(long time
, Resolution res
) {
230 StringBuffer temp
= new StringBuffer();
231 boolean neg
= time
< 0;
236 // The following approach could be used although performance
237 // decreases in half.
238 // String strVal = String.format("%09d", time);
239 // String tmp = strVal.substring(strVal.length() - 9);
241 // number of segments to be included
260 temp
.append("00000000");
261 } else if (ns
< 100) {
262 temp
.append("0000000");
263 } else if (ns
< 1000) {
264 temp
.append("000000");
265 } else if (ns
< 10000) {
266 temp
.append("00000");
267 } else if (ns
< 100000) {
269 } else if (ns
< 1000000) {
271 } else if (ns
< 10000000) {
273 } else if (ns
< 100000000) {
278 StringBuffer str
= new StringBuffer();
281 str
.append(temp
.substring(0, 3));
286 str
.append(temp
.substring(3, 6));
289 // append Nano seconds
291 str
.append(temp
.substring(6));
294 return str
.toString();
297 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
299 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
309 // static public int loadIntOption(String opt) {
310 // int val = TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
314 static public void saveIntOption(String opt
, int val
) {
315 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
318 static ITimeEvent
getFirstEvent(ITmfTimeAnalysisEntry thread
) {
321 Vector
<TimeEvent
> list
= thread
.getTraceEvents();
322 ITimeEvent event
= null;
324 event
= (ITimeEvent
) list
.get(0);
329 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
330 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
338 static ITimeEvent
findEvent(ITmfTimeAnalysisEntry thread
, long time
, int n
) {
341 List
<TimeEvent
> list
= thread
.getTraceEvents();
342 Iterator
<TimeEvent
> it
= list
.iterator();
343 ITimeEvent event
= null;
344 ITimeEvent prevEvent
= null;
345 ITimeEvent nextEvent
= null;
347 event
= (ITimeEvent
) it
.next();
348 long currStartTime
= event
.getTime();
349 long currEndTime
= currStartTime
+ event
.getDuration();
351 if (time
< currStartTime
) {
356 while (it
.hasNext()) {
357 currStartTime
= event
.getTime();
358 currEndTime
= currStartTime
+ event
.getDuration();
359 nextEvent
= (ITimeEvent
) it
.next();
361 long nextEventTime
= nextEvent
.getTime();
362 currEndTime
= currEndTime
>= nextEventTime ?
(nextEventTime
- 1)
365 // Trace.debug("currStartTime: " + currStartTime
366 // + " currEndTime: " + currEndTime + " nextEventTime: "
367 // + nextEventTime + " time: " + time);
368 if (currStartTime
<= time
&& time
<= currEndTime
) {
376 if (time
> currEndTime
&& time
< nextEventTime
) {
377 //Located in a non Event area
378 if (2 == n
|| -1 == n
) {
397 // static public TRCPackage getPackage(Object element) {
398 // if (element instanceof TRCPackage)
399 // return (TRCPackage) element;
400 // if (element instanceof TRCClass)
401 // return ((TRCClass) element).getPackage();
405 // static public TRCObjectAllocationAnnotation getAllocationAnnotation(
407 // TRCObjectAllocationAnnotation aa = null;
408 // EList list = cls.getAnnotations();
409 // int len = list.size();
410 // for (int i = 0; i < len; i++) {
411 // TRCAnnotation annotation = (TRCAnnotation) list.get(i);
412 // if (annotation instanceof TRCObjectAllocationAnnotation)
413 // aa = (TRCObjectAllocationAnnotation) annotation;
418 static public String
fixMethodSignature(String sig
) {
419 int pos
= sig
.indexOf('(');
421 String ret
= sig
.substring(0, pos
);
422 sig
= sig
.substring(pos
);
423 sig
= sig
+ " " + ret
;
428 static public String
restoreMethodSignature(String sig
) {
430 int pos
= sig
.indexOf('(');
432 ret
= sig
.substring(0, pos
);
433 sig
= sig
.substring(pos
+ 1);
435 pos
= sig
.indexOf(')');
437 sig
= sig
.substring(0, pos
);
439 String args
[] = sig
.split(",");
441 for (int i
= 0; i
< args
.length
; i
++) {
442 String arg
= args
[i
].trim();
443 if (arg
.length() == 0 && args
.length
== 1)
445 sig
+= getTypeSignature(arg
);
447 sig
+= ")" + getTypeSignature(ret
);
451 static public String
getTypeSignature(String type
) {
453 for (int j
= 0; j
< type
.length(); j
++) {
454 if (type
.charAt(j
) == '[')
457 int pos
= type
.indexOf('[');
459 type
= type
.substring(0, pos
);
461 for (int j
= 0; j
< dim
; j
++)
463 if (type
.equals("boolean"))
465 else if (type
.equals("byte"))
467 else if (type
.equals("char"))
469 else if (type
.equals("short"))
471 else if (type
.equals("int"))
473 else if (type
.equals("long"))
475 else if (type
.equals("float"))
477 else if (type
.equals("double"))
479 else if (type
.equals("void"))
482 sig
+= "L" + type
.replace('.', '/') + ";";
486 // static public boolean openSource(Object element) {
487 // if (element instanceof String) {
488 // final String pattern = (String) element;
489 // final int javaType = IJavaSearchConstants.METHOD;
490 // BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
491 // public void run() {
492 // if (!OpenJavaSource.openSource(pattern, javaType,
493 // SearchEngine.createWorkspaceScope(), true)) {
494 // MessageDialog.openInformation(UIPlugin.getDefault()
495 // .getWorkbench().getActiveWorkbenchWindow()
496 // .getShell(), TraceMessages.TRC_MSGT, NLS.bind(
497 // TraceUIMessages._68, pattern));
502 // OpenSource.openSource(element);
506 // static public int getObjAge(TRCFullTraceObject obj, EList listGC) {
508 // double t0 = obj.getCreateTime();
509 // double t1 = obj.getCollectTime();
510 // int len = listGC.size();
511 // for (int j = 0; j < len; j++) {
512 // TRCGCEvent gcEvent = (TRCGCEvent) listGC.get(j);
513 // if (gcEvent.getType().equals("finish")) {
514 // double time = gcEvent.getTime();
517 // if (t1 > 0 && time >= t1)
525 static public int compare(double d1
, double d2
) {
533 static public int compare(String s1
, String s2
) {
534 if (s1
!= null && s2
!= null)
535 return s1
.compareToIgnoreCase(s2
);
543 // static public String formatPercent(int val, int max) {
544 // String s = max > 0 && max >= val ? TString
545 // .formatAsPercentage((double) val / (double) max) : "";