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
.internal
.tmf
.ui
.viewers
.timeAnalysis
.widgets
;
18 import java
.text
.SimpleDateFormat
;
19 import java
.util
.Date
;
20 import java
.util
.Iterator
;
21 import java
.util
.TimeZone
;
23 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.ITimeAnalysisViewer
.TimeFormat
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITimeEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITmfTimeAnalysisEntry
;
26 import org
.eclipse
.swt
.graphics
.Color
;
27 import org
.eclipse
.swt
.graphics
.Device
;
28 import org
.eclipse
.swt
.graphics
.GC
;
29 import org
.eclipse
.swt
.graphics
.Point
;
30 import org
.eclipse
.swt
.graphics
.Rectangle
;
31 import org
.eclipse
.swt
.widgets
.Display
;
35 static public final int IMG_THREAD_RUNNING
= 0;
36 static public final int IMG_THREAD_SUSPENDED
= 1;
37 static public final int IMG_THREAD_STOPPED
= 2;
38 static public final int IMG_METHOD_RUNNING
= 3;
39 static public final int IMG_METHOD
= 4;
40 static public final int IMG_NUM
= 5;
42 static public final Object
[] _empty
= new Object
[0];
44 static enum Resolution
{
45 SECONDS
, MILLISEC
, MICROSEC
, NANOSEC
48 static private final SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
49 static private final SimpleDateFormat sdateformat
= new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
51 stimeformat
.setTimeZone(TimeZone
.getTimeZone("GMT")); //$NON-NLS-1$
52 sdateformat
.setTimeZone(TimeZone
.getTimeZone("GMT")); //$NON-NLS-1$
55 // static private String _externalPlugin[] = { "org.eclipse.debug.ui",
56 // "org.eclipse.debug.ui", "org.eclipse.debug.ui",
57 // "org.eclipse.debug.ui", "org.eclipse.debug.ui", };
59 // static private String _externalPath[] = {
60 // "icons/full/obj16/thread_obj.gif", // running thread
61 // "icons/full/obj16/threads_obj.gif", // suspended
62 // "icons/full/obj16/threadt_obj.gif", // stopped
63 // "icons/full/obj16/stckframe_running_obj.gif", // running stack frame
64 // "icons/full/obj16/stckframe_obj.gif", // stack frame
67 // static public Image getImage(int idx) {
68 // if (idx < 0 || idx >= IMG_NUM)
69 // SWT.error(SWT.ERROR_INVALID_ARGUMENT);
70 // String key = "trace.img." + idx;
71 // Image img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
73 // ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(
74 // _externalPlugin[idx], _externalPath[idx]);
75 // TimeAnalysisPlugin.getDefault().getImageRegistry().put(key, desc);
76 // img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
81 static public void init(Rectangle rect
) {
88 static public void init(Rectangle rect
, int x
, int y
, int width
, int height
) {
95 static public void init(Rectangle rect
, Rectangle source
) {
98 rect
.width
= source
.width
;
99 rect
.height
= source
.height
;
102 static public void deflate(Rectangle rect
, int x
, int y
) {
106 rect
.height
-= y
+ y
;
109 static public void inflate(Rectangle rect
, int x
, int y
) {
113 rect
.height
+= y
+ y
;
116 static void dispose(Color col
) {
121 static public Color
mixColors(Device display
, Color c1
, Color c2
, int w1
,
123 return new Color(display
, (w1
* c1
.getRed() + w2
* c2
.getRed())
124 / (w1
+ w2
), (w1
* c1
.getGreen() + w2
* c2
.getGreen())
125 / (w1
+ w2
), (w1
* c1
.getBlue() + w2
* c2
.getBlue())
129 static public Color
getSysColor(int id
) {
130 Color col
= Display
.getCurrent().getSystemColor(id
);
131 return new Color(col
.getDevice(), col
.getRGB());
134 static public Color
mixColors(Color col1
, Color col2
, int w1
, int w2
) {
135 return mixColors(Display
.getCurrent(), col1
, col2
, w1
, w2
);
138 static public int drawText(GC gc
, String text
, Rectangle rect
,
140 Point size
= gc
.stringExtent(text
);
141 gc
.drawText(text
, rect
.x
, rect
.y
, transp
);
145 static public int drawText(GC gc
, String text
, int x
, int y
, boolean transp
) {
146 Point size
= gc
.stringExtent(text
);
147 gc
.drawText(text
, x
, y
, transp
);
152 * Formats time in format: MM:SS:NNN
155 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
156 * @param resolution the resolution
157 * @return the formatted time
159 static public String
formatTime(long time
, TimeFormat format
, Resolution resolution
) {
160 // if format is absolute (Calendar)
161 if (format
== TimeFormat
.ABSOLUTE
) {
162 return formatTimeAbs(time
, resolution
);
165 StringBuffer str
= new StringBuffer();
166 boolean neg
= time
< 0;
172 long sec
= (long) (time
* 1E-9);
173 // TODO: Expand to make it possible to select the minute, second, nanosecond format
174 //printing minutes is suppressed just sec and ns
175 // if (sec / 60 < 10)
177 // str.append(sec / 60);
183 String ns
= formatNs(time
, resolution
);
184 if (!ns
.equals("")) { //$NON-NLS-1$
189 return str
.toString();
193 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
196 * @return the formatted date
198 public static String
formatDate(long absTime
) {
199 String sdate
= sdateformat
.format(new Date((long) (absTime
* 1E-6)));
204 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
207 * @return the formatted time
209 static public String
formatTimeAbs(long time
, Resolution res
) {
210 StringBuffer str
= new StringBuffer();
212 // format time from nanoseconds to calendar time HH:MM:SS
213 String stime
= stimeformat
.format(new Date((long) (time
* 1E-6)));
214 str
.append(stime
+ " "); //$NON-NLS-1$
215 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
217 str
.append(formatNs(time
, res
));
218 return str
.toString();
222 * Obtains the remainder fraction on unit Seconds of the entered value in
223 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
224 * seconds can be obtained by removing the last 9 digits: 1241207054 the
225 * fractional portion of seconds, expressed in ns is: 171080214
229 * @return the formatted nanosec
231 public static String
formatNs(long time
, Resolution res
) {
232 StringBuffer temp
= new StringBuffer();
233 boolean neg
= time
< 0;
238 // The following approach could be used although performance
239 // decreases in half.
240 // String strVal = String.format("%09d", time);
241 // String tmp = strVal.substring(strVal.length() - 9);
243 // number of segments to be included
262 temp
.append("00000000"); //$NON-NLS-1$
263 } else if (ns
< 100) {
264 temp
.append("0000000"); //$NON-NLS-1$
265 } else if (ns
< 1000) {
266 temp
.append("000000"); //$NON-NLS-1$
267 } else if (ns
< 10000) {
268 temp
.append("00000"); //$NON-NLS-1$
269 } else if (ns
< 100000) {
270 temp
.append("0000"); //$NON-NLS-1$
271 } else if (ns
< 1000000) {
272 temp
.append("000"); //$NON-NLS-1$
273 } else if (ns
< 10000000) {
274 temp
.append("00"); //$NON-NLS-1$
275 } else if (ns
< 100000000) {
276 temp
.append("0"); //$NON-NLS-1$
280 StringBuffer str
= new StringBuffer();
283 str
.append(temp
.substring(0, 3));
287 str
.append("."); //$NON-NLS-1$
288 str
.append(temp
.substring(3, 6));
291 // append Nano seconds
292 str
.append("."); //$NON-NLS-1$
293 str
.append(temp
.substring(6));
296 return str
.toString();
299 static public int loadIntOption(String opt
, int def
, int min
, int max
) {
301 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
311 // static public int loadIntOption(String opt) {
312 // int val = TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
316 static public void saveIntOption(String opt
, int val
) {
317 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
320 static ITimeEvent
getFirstEvent(ITmfTimeAnalysisEntry thread
) {
323 Iterator
<ITimeEvent
> iterator
= thread
.getTraceEventsIterator();
324 if (iterator
.hasNext()) {
325 return iterator
.next();
332 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
333 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
341 static ITimeEvent
findEvent(ITmfTimeAnalysisEntry thread
, long time
, int n
) {
344 Iterator
<ITimeEvent
> iterator
= thread
.getTraceEventsIterator();
345 ITimeEvent nextEvent
= null;
346 ITimeEvent currEvent
= null;
347 ITimeEvent prevEvent
= null;
349 while (iterator
.hasNext()) {
350 nextEvent
= (ITimeEvent
) iterator
.next();
351 long nextStartTime
= nextEvent
.getTime();
353 if (nextStartTime
> time
) {
357 if (currEvent
== null || currEvent
.getTime() != nextStartTime
) {
358 prevEvent
= currEvent
;
359 currEvent
= nextEvent
;
363 if (n
== -1) { //previous
364 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
369 } else if (n
== 0) { //current
370 if (currEvent
!= null && currEvent
.getTime() + currEvent
.getDuration() >= time
) {
375 } else if (n
== 1) { //next
377 } else if (n
== 2) { //current or previous when in empty space
384 // static public TRCPackage getPackage(Object element) {
385 // if (element instanceof TRCPackage)
386 // return (TRCPackage) element;
387 // if (element instanceof TRCClass)
388 // return ((TRCClass) element).getPackage();
392 // static public TRCObjectAllocationAnnotation getAllocationAnnotation(
394 // TRCObjectAllocationAnnotation aa = null;
395 // EList list = cls.getAnnotations();
396 // int len = list.size();
397 // for (int i = 0; i < len; i++) {
398 // TRCAnnotation annotation = (TRCAnnotation) list.get(i);
399 // if (annotation instanceof TRCObjectAllocationAnnotation)
400 // aa = (TRCObjectAllocationAnnotation) annotation;
405 static public String
fixMethodSignature(String sig
) {
406 int pos
= sig
.indexOf('(');
408 String ret
= sig
.substring(0, pos
);
409 sig
= sig
.substring(pos
);
410 sig
= sig
+ " " + ret
; //$NON-NLS-1$
415 static public String
restoreMethodSignature(String sig
) {
416 String ret
= ""; //$NON-NLS-1$
417 int pos
= sig
.indexOf('(');
419 ret
= sig
.substring(0, pos
);
420 sig
= sig
.substring(pos
+ 1);
422 pos
= sig
.indexOf(')');
424 sig
= sig
.substring(0, pos
);
426 String args
[] = sig
.split(","); //$NON-NLS-1$
427 StringBuffer result
= new StringBuffer("("); //$NON-NLS-1$
428 for (int i
= 0; i
< args
.length
; i
++) {
429 String arg
= args
[i
].trim();
430 if (arg
.length() == 0 && args
.length
== 1)
432 result
.append(getTypeSignature(arg
));
434 result
.append(")").append(getTypeSignature(ret
)); //$NON-NLS-1$
435 return result
.toString();
438 static public String
getTypeSignature(String type
) {
440 for (int j
= 0; j
< type
.length(); j
++) {
441 if (type
.charAt(j
) == '[')
444 int pos
= type
.indexOf('[');
446 type
= type
.substring(0, pos
);
447 StringBuffer sig
= new StringBuffer(""); //$NON-NLS-1$
448 for (int j
= 0; j
< dim
; j
++)
449 sig
.append("["); //$NON-NLS-1$
450 if (type
.equals("boolean")) //$NON-NLS-1$
451 sig
.append("Z"); //$NON-NLS-1$
452 else if (type
.equals("byte")) //$NON-NLS-1$
453 sig
.append("B"); //$NON-NLS-1$
454 else if (type
.equals("char")) //$NON-NLS-1$
455 sig
.append("C"); //$NON-NLS-1$
456 else if (type
.equals("short")) //$NON-NLS-1$
457 sig
.append("S"); //$NON-NLS-1$
458 else if (type
.equals("int")) //$NON-NLS-1$
459 sig
.append("I"); //$NON-NLS-1$
460 else if (type
.equals("long")) //$NON-NLS-1$
461 sig
.append("J"); //$NON-NLS-1$
462 else if (type
.equals("float")) //$NON-NLS-1$
463 sig
.append("F"); //$NON-NLS-1$
464 else if (type
.equals("double")) //$NON-NLS-1$
465 sig
.append("D"); //$NON-NLS-1$
466 else if (type
.equals("void")) //$NON-NLS-1$
467 sig
.append("V"); //$NON-NLS-1$
469 sig
.append("L").append(type
.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
470 return sig
.toString();
473 // static public boolean openSource(Object element) {
474 // if (element instanceof String) {
475 // final String pattern = (String) element;
476 // final int javaType = IJavaSearchConstants.METHOD;
477 // BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
478 // public void run() {
479 // if (!OpenJavaSource.openSource(pattern, javaType,
480 // SearchEngine.createWorkspaceScope(), true)) {
481 // MessageDialog.openInformation(UIPlugin.getDefault()
482 // .getWorkbench().getActiveWorkbenchWindow()
483 // .getShell(), TraceMessages.TRC_MSGT, NLS.bind(
484 // TraceUIMessages._68, pattern));
489 // OpenSource.openSource(element);
493 // static public int getObjAge(TRCFullTraceObject obj, EList listGC) {
495 // double t0 = obj.getCreateTime();
496 // double t1 = obj.getCollectTime();
497 // int len = listGC.size();
498 // for (int j = 0; j < len; j++) {
499 // TRCGCEvent gcEvent = (TRCGCEvent) listGC.get(j);
500 // if (gcEvent.getType().equals("finish")) {
501 // double time = gcEvent.getTime();
504 // if (t1 > 0 && time >= t1)
512 static public int compare(double d1
, double d2
) {
520 static public int compare(String s1
, String s2
) {
521 if (s1
!= null && s2
!= null)
522 return s1
.compareToIgnoreCase(s2
);
530 // static public String formatPercent(int val, int max) {
531 // String s = max > 0 && max >= val ? TString
532 // .formatAsPercentage((double) val / (double) max) : "";