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.lttng.ui.viewers.timeAnalysis.widgets;
18
import java.text.SimpleDateFormat;
19
import java.util.Date;
20
import java.util.Iterator;
22
import org.eclipse.linuxtools.internal.lttng.ui.viewers.timeAnalysis.ITimeAnalysisViewer.TimeFormat;
23
import org.eclipse.linuxtools.internal.lttng.ui.viewers.timeAnalysis.model.ITimeEvent;
24
import org.eclipse.linuxtools.internal.lttng.ui.viewers.timeAnalysis.model.ITmfTimeAnalysisEntry;
25
import org.eclipse.swt.graphics.Color;
26
import org.eclipse.swt.graphics.Device;
27
import org.eclipse.swt.graphics.GC;
28
import org.eclipse.swt.graphics.Point;
29
import org.eclipse.swt.graphics.Rectangle;
30
import org.eclipse.swt.widgets.Display;
34
static public final int IMG_THREAD_RUNNING = 0;
35
static public final int IMG_THREAD_SUSPENDED = 1;
36
static public final int IMG_THREAD_STOPPED = 2;
37
static public final int IMG_METHOD_RUNNING = 3;
38
static public final int IMG_METHOD = 4;
39
static public final int IMG_NUM = 5;
41
static public final Object[] _empty = new Object[0];
43
static enum Resolution {
44
SECONDS, MILLISEC, MICROSEC, NANOSEC
47
static private final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
48
static private final SimpleDateFormat sdateformat = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
50
// static private String _externalPlugin[] = { "org.eclipse.debug.ui",
51
// "org.eclipse.debug.ui", "org.eclipse.debug.ui",
52
// "org.eclipse.debug.ui", "org.eclipse.debug.ui", };
54
// static private String _externalPath[] = {
55
// "icons/full/obj16/thread_obj.gif", // running thread
56
// "icons/full/obj16/threads_obj.gif", // suspended
57
// "icons/full/obj16/threadt_obj.gif", // stopped
58
// "icons/full/obj16/stckframe_running_obj.gif", // running stack frame
59
// "icons/full/obj16/stckframe_obj.gif", // stack frame
62
// static public Image getImage(int idx) {
63
// if (idx < 0 || idx >= IMG_NUM)
64
// SWT.error(SWT.ERROR_INVALID_ARGUMENT);
65
// String key = "trace.img." + idx;
66
// Image img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
68
// ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(
69
// _externalPlugin[idx], _externalPath[idx]);
70
// TimeAnalysisPlugin.getDefault().getImageRegistry().put(key, desc);
71
// img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
76
static public void init(Rectangle rect) {
83
static public void init(Rectangle rect, int x, int y, int width, int height) {
90
static public void init(Rectangle rect, Rectangle source) {
93
rect.width = source.width;
94
rect.height = source.height;
97
static public void deflate(Rectangle rect, int x, int y) {
101
rect.height -= y + y;
104
static public void inflate(Rectangle rect, int x, int y) {
108
rect.height += y + y;
111
static void dispose(Color col) {
116
static public Color mixColors(Device display, Color c1, Color c2, int w1,
118
return new Color(display, (w1 * c1.getRed() + w2 * c2.getRed())
119
/ (w1 + w2), (w1 * c1.getGreen() + w2 * c2.getGreen())
120
/ (w1 + w2), (w1 * c1.getBlue() + w2 * c2.getBlue())
124
static public Color getSysColor(int id) {
125
Color col = Display.getCurrent().getSystemColor(id);
126
return new Color(col.getDevice(), col.getRGB());
129
static public Color mixColors(Color col1, Color col2, int w1, int w2) {
130
return mixColors(Display.getCurrent(), col1, col2, w1, w2);
133
static public int drawText(GC gc, String text, Rectangle rect,
135
Point size = gc.stringExtent(text);
136
gc.drawText(text, rect.x, rect.y, transp);
140
static public int drawText(GC gc, String text, int x, int y, boolean transp) {
141
Point size = gc.stringExtent(text);
142
gc.drawText(text, x, y, transp);
147
* Formats time in format: MM:SS:NNN
150
* @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
151
* @param resolution the resolution
152
* @return the formatted time
154
static public String formatTime(long time, TimeFormat format, Resolution resolution) {
155
// if format is absolute (Calendar)
156
if (format == TimeFormat.ABSOLUTE) {
157
return formatTimeAbs(time, resolution);
160
StringBuffer str = new StringBuffer();
161
boolean neg = time < 0;
167
long sec = (long) (time * 1E-9);
168
// TODO: Expand to make it possible to select the minute, second, nanosecond format
169
//printing minutes is suppressed just sec and ns
170
// if (sec / 60 < 10)
172
// str.append(sec / 60);
178
String ns = formatNs(time, resolution);
179
if (!ns.equals("")) { //$NON-NLS-1$
184
return str.toString();
188
* From input time in nanoseconds, convert to Date format YYYY-MM-dd
191
* @return the formatted date
193
public static String formatDate(long absTime) {
194
String sdate = sdateformat.format(new Date((long) (absTime * 1E-6)));
199
* Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
202
* @return the formatted time
204
static public String formatTimeAbs(long time, Resolution res) {
205
StringBuffer str = new StringBuffer();
207
// format time from nanoseconds to calendar time HH:MM:SS
208
String stime = stimeformat.format(new Date((long) (time * 1E-6)));
209
str.append(stime + " "); //$NON-NLS-1$
210
// append the Milliseconds, MicroSeconds and NanoSeconds as specified in
212
str.append(formatNs(time, res));
213
return str.toString();
217
* Obtains the remainder fraction on unit Seconds of the entered value in
218
* nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
219
* seconds can be obtained by removing the last 9 digits: 1241207054 the
220
* fractional portion of seconds, expressed in ns is: 171080214
224
* @return the formatted nanosec
226
public static String formatNs(long time, Resolution res) {
227
StringBuffer temp = new StringBuffer();
228
boolean neg = time < 0;
233
// The following approach could be used although performance
234
// decreases in half.
235
// String strVal = String.format("%09d", time);
236
// String tmp = strVal.substring(strVal.length() - 9);
238
// number of segments to be included
257
temp.append("00000000"); //$NON-NLS-1$
258
} else if (ns < 100) {
259
temp.append("0000000"); //$NON-NLS-1$
260
} else if (ns < 1000) {
261
temp.append("000000"); //$NON-NLS-1$
262
} else if (ns < 10000) {
263
temp.append("00000"); //$NON-NLS-1$
264
} else if (ns < 100000) {
265
temp.append("0000"); //$NON-NLS-1$
266
} else if (ns < 1000000) {
267
temp.append("000"); //$NON-NLS-1$
268
} else if (ns < 10000000) {
269
temp.append("00"); //$NON-NLS-1$
270
} else if (ns < 100000000) {
271
temp.append("0"); //$NON-NLS-1$
275
StringBuffer str = new StringBuffer();
278
str.append(temp.substring(0, 3));
282
str.append("."); //$NON-NLS-1$
283
str.append(temp.substring(3, 6));
286
// append Nano seconds
287
str.append("."); //$NON-NLS-1$
288
str.append(temp.substring(6));
291
return str.toString();
294
static public int loadIntOption(String opt, int def, int min, int max) {
296
// TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
306
// static public int loadIntOption(String opt) {
307
// int val = TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
311
static public void saveIntOption(String opt, int val) {
312
// TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
315
static ITimeEvent getFirstEvent(ITmfTimeAnalysisEntry thread) {
318
Iterator<ITimeEvent> iterator = thread.getTraceEventsIterator();
319
if (iterator.hasNext()) {
320
return iterator.next();
327
* N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
328
* 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
336
static ITimeEvent findEvent(ITmfTimeAnalysisEntry thread, long time, int n) {
339
Iterator<ITimeEvent> iterator = thread.getTraceEventsIterator();
340
ITimeEvent nextEvent = null;
341
ITimeEvent currEvent = null;
342
ITimeEvent prevEvent = null;
344
while (iterator.hasNext()) {
345
nextEvent = (ITimeEvent) iterator.next();
346
long nextStartTime = nextEvent.getTime();
348
if (nextStartTime > time) {
352
if (currEvent == null || currEvent.getTime() != nextStartTime) {
353
prevEvent = currEvent;
354
currEvent = nextEvent;
358
if (n == -1) { //previous
359
if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
364
} else if (n == 0) { //current
365
if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
370
} else if (n == 1) { //next
372
} else if (n == 2) { //current or previous when in empty space
379
// static public TRCPackage getPackage(Object element) {
380
// if (element instanceof TRCPackage)
381
// return (TRCPackage) element;
382
// if (element instanceof TRCClass)
383
// return ((TRCClass) element).getPackage();
387
// static public TRCObjectAllocationAnnotation getAllocationAnnotation(
389
// TRCObjectAllocationAnnotation aa = null;
390
// EList list = cls.getAnnotations();
391
// int len = list.size();
392
// for (int i = 0; i < len; i++) {
393
// TRCAnnotation annotation = (TRCAnnotation) list.get(i);
394
// if (annotation instanceof TRCObjectAllocationAnnotation)
395
// aa = (TRCObjectAllocationAnnotation) annotation;
400
static public String fixMethodSignature(String sig) {
401
int pos = sig.indexOf('(');
403
String ret = sig.substring(0, pos);
404
sig = sig.substring(pos);
405
sig = sig + " " + ret; //$NON-NLS-1$
410
static public String restoreMethodSignature(String sig) {
411
String ret = ""; //$NON-NLS-1$
412
int pos = sig.indexOf('(');
414
ret = sig.substring(0, pos);
415
sig = sig.substring(pos + 1);
417
pos = sig.indexOf(')');
419
sig = sig.substring(0, pos);
421
String args[] = sig.split(","); //$NON-NLS-1$
422
StringBuffer result = new StringBuffer("("); //$NON-NLS-1$
423
for (int i = 0; i < args.length; i++) {
424
String arg = args[i].trim();
425
if (arg.length() == 0 && args.length == 1)
427
result.append(getTypeSignature(arg));
429
result.append(")").append(getTypeSignature(ret)); //$NON-NLS-1$
430
return result.toString();
433
static public String getTypeSignature(String type) {
435
for (int j = 0; j < type.length(); j++) {
436
if (type.charAt(j) == '[')
439
int pos = type.indexOf('[');
441
type = type.substring(0, pos);
442
StringBuffer sig = new StringBuffer(""); //$NON-NLS-1$
443
for (int j = 0; j < dim; j++)
444
sig.append("["); //$NON-NLS-1$
445
if (type.equals("boolean")) //$NON-NLS-1$
446
sig.append("Z"); //$NON-NLS-1$
447
else if (type.equals("byte")) //$NON-NLS-1$
448
sig.append("B"); //$NON-NLS-1$
449
else if (type.equals("char")) //$NON-NLS-1$
450
sig.append("C"); //$NON-NLS-1$
451
else if (type.equals("short")) //$NON-NLS-1$
452
sig.append("S"); //$NON-NLS-1$
453
else if (type.equals("int")) //$NON-NLS-1$
454
sig.append("I"); //$NON-NLS-1$
455
else if (type.equals("long")) //$NON-NLS-1$
456
sig.append("J"); //$NON-NLS-1$
457
else if (type.equals("float")) //$NON-NLS-1$
458
sig.append("F"); //$NON-NLS-1$
459
else if (type.equals("double")) //$NON-NLS-1$
460
sig.append("D"); //$NON-NLS-1$
461
else if (type.equals("void")) //$NON-NLS-1$
462
sig.append("V"); //$NON-NLS-1$
464
sig.append("L").append(type.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
465
return sig.toString();
468
// static public boolean openSource(Object element) {
469
// if (element instanceof String) {
470
// final String pattern = (String) element;
471
// final int javaType = IJavaSearchConstants.METHOD;
472
// BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
473
// public void run() {
474
// if (!OpenJavaSource.openSource(pattern, javaType,
475
// SearchEngine.createWorkspaceScope(), true)) {
476
// MessageDialog.openInformation(UIPlugin.getDefault()
477
// .getWorkbench().getActiveWorkbenchWindow()
478
// .getShell(), TraceMessages.TRC_MSGT, NLS.bind(
479
// TraceUIMessages._68, pattern));
484
// OpenSource.openSource(element);
488
// static public int getObjAge(TRCFullTraceObject obj, EList listGC) {
490
// double t0 = obj.getCreateTime();
491
// double t1 = obj.getCollectTime();
492
// int len = listGC.size();
493
// for (int j = 0; j < len; j++) {
494
// TRCGCEvent gcEvent = (TRCGCEvent) listGC.get(j);
495
// if (gcEvent.getType().equals("finish")) {
496
// double time = gcEvent.getTime();
499
// if (t1 > 0 && time >= t1)
507
static public int compare(double d1, double d2) {
515
static public int compare(String s1, String s2) {
516
if (s1 != null && s2 != null)
517
return s1.compareToIgnoreCase(s2);
525
// static public String formatPercent(int val, int max) {
526
// String s = max > 0 && max >= val ? TString
527
// .formatAsPercentage((double) val / (double) max) : "";