1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.timechart
;
15 import java
.util
.Iterator
;
16 import java
.util
.NoSuchElementException
;
17 import java
.util
.Vector
;
19 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
20 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITimeEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITmfTimeAnalysisEntry
;
23 public class TimeChartAnalysisEntry
implements ITmfTimeAnalysisEntry
{
25 private ITmfTrace fTrace
;
26 private String fGroup
;
27 private Vector
<TimeChartEvent
> fTraceEvents
;
28 private int fPower
= 0; // 2^fPower nanoseconds per vector position
29 private long fReferenceTime
= -1; // time corresponding to beginning of index 0
30 private long fStartTime
= -1; // time of first event
31 private long fStopTime
= -1; // time of last event
32 private long fLastRank
= -1; // rank of last processed trace event
34 TimeChartAnalysisEntry(ITmfTrace trace
, int modelSize
) {
36 fTraceEvents
= new Vector
<TimeChartEvent
>(modelSize
);
39 TimeChartAnalysisEntry(ITmfTrace trace
, String group
, int modelSize
) {
41 fTraceEvents
= new Vector
<TimeChartEvent
>(modelSize
);
46 public String
getGroupName() {
52 // TODO Auto-generated method stub
57 public String
getName() {
58 return fTrace
.getName();
62 public long getStartTime() {
67 public long getStopTime() {
72 @Deprecated public <T
extends ITimeEvent
> Vector
<T
> getTraceEvents() {
77 public Iterator
<ITimeEvent
> getTraceEventsIterator() {
78 return new EntryIterator(0, Long
.MAX_VALUE
, 0);
82 public Iterator
<ITimeEvent
> getTraceEventsIterator(long startTime
, long stopTime
, long maxDuration
) {
83 return new EntryIterator(startTime
, stopTime
, maxDuration
);
86 private class EntryIterator
implements Iterator
<ITimeEvent
> {
87 private final long fIteratorStartTime
;
88 private final long fIteratorStopTime
;
89 private final long fIteratorMaxDuration
;
90 private long lastTime
= -1;
91 private TimeChartEvent next
= null;
92 private Iterator
<ITimeEvent
> nestedIterator
= null;
94 public EntryIterator(long startTime
, long stopTime
, long maxDuration
) {
95 fIteratorStartTime
= startTime
;
96 fIteratorStopTime
= stopTime
;
97 fIteratorMaxDuration
= maxDuration
;
101 public boolean hasNext() {
102 synchronized (fTraceEvents
) {
103 if (next
!= null) return true;
104 if (nestedIterator
!= null) {
105 if (nestedIterator
.hasNext()) {
108 nestedIterator
= null;
111 long time
= (lastTime
== -1) ? fStartTime
: lastTime
;
112 int index
= (fReferenceTime
== -1) ?
0 : (int) ((time
- fReferenceTime
) >> fPower
);
113 while (index
< fTraceEvents
.size()) {
114 TimeChartEvent event
= fTraceEvents
.get(index
++);
115 if (event
!= null && (lastTime
== -1 || event
.getTime() > time
)) {
116 if (event
.getTime() + event
.getDuration() >= fIteratorStartTime
&& event
.getTime() <= fIteratorStopTime
) {
117 if (event
.getItemizedEntry() == null || event
.getDuration() <= fIteratorMaxDuration
) {
118 lastTime
= event
.getTime() + event
.getDuration();
122 nestedIterator
= event
.getItemizedEntry().getTraceEventsIterator(fIteratorStartTime
, fIteratorStopTime
, fIteratorMaxDuration
);
123 return nestedIterator
.hasNext();
133 public TimeChartEvent
next() {
134 synchronized (fTraceEvents
) {
135 if (nestedIterator
!= null) {
136 TimeChartEvent event
= (TimeChartEvent
) nestedIterator
.next();
137 lastTime
= event
.getTime() + event
.getDuration();
141 TimeChartEvent event
= next
;
145 throw new NoSuchElementException();
150 public void remove() {
151 throw new UnsupportedOperationException();
157 public void addTraceEvent(ITimeEvent timeEvent
) {
158 long time
= timeEvent
.getTime();
159 synchronized (fTraceEvents
) {
160 long index
= (fReferenceTime
== -1) ?
0 : (time
- fReferenceTime
) >> fPower
;
162 if (fTraceEvents
.capacity() - fTraceEvents
.size() < -index
) {
163 int powershift
= (-index
+ fTraceEvents
.size() <= 2 * fTraceEvents
.capacity()) ?
1 :
164 (int) Math
.ceil(Math
.log((double) (-index
+ fTraceEvents
.size()) / fTraceEvents
.capacity()) / Math
.log(2));
166 index
= (int) ((time
- fReferenceTime
) >> fPower
);
170 fTraceEvents
.set(0, (TimeChartEvent
) timeEvent
);
171 } else if (index
< fTraceEvents
.capacity()) {
172 if (index
>= fTraceEvents
.size()) {
173 fTraceEvents
.setSize((int) index
+ 1);
176 int powershift
= (index
< 2 * fTraceEvents
.capacity()) ?
1 :
177 (int) Math
.ceil(Math
.log((double) (index
+ 1) / fTraceEvents
.capacity()) / Math
.log(2));
179 index
= (int) ((time
- fReferenceTime
) >> fPower
);
180 fTraceEvents
.setSize((int) index
+ 1);
182 TimeChartEvent event
= (TimeChartEvent
) fTraceEvents
.get((int) index
);
184 fTraceEvents
.set((int) index
, (TimeChartEvent
) timeEvent
);
186 if (event
.getItemizedEntry() == null) {
187 event
.merge((TimeChartEvent
) timeEvent
);
189 event
.mergeDecorations((TimeChartEvent
) timeEvent
);
190 event
.getItemizedEntry().addTraceEvent(timeEvent
);
193 if (fReferenceTime
== -1 || time
< fReferenceTime
) {
194 fReferenceTime
= (time
>> fPower
) << fPower
;
196 if (fStartTime
== -1 || time
< fStartTime
) {
199 if (fStopTime
== -1 || time
> fStopTime
) {
205 private void merge(int powershift
) {
206 fPower
+= powershift
;
207 fReferenceTime
= (fReferenceTime
>> fPower
) << fPower
;
209 for (int i
= 0; i
< fTraceEvents
.size(); i
++) {
210 TimeChartEvent event
= fTraceEvents
.get(i
);
212 index
= (int) ((event
.getTime() - fReferenceTime
) >> fPower
);
213 TimeChartEvent mergedEvent
= (TimeChartEvent
) fTraceEvents
.get(index
);
214 if (mergedEvent
== null) {
215 fTraceEvents
.set(index
, event
);
217 mergedEvent
.merge(event
);
220 fTraceEvents
.set(i
, null);
224 fTraceEvents
.setSize(index
+ 1);
227 private void shift(int indexshift
) {
228 int oldSize
= fTraceEvents
.size();
229 fTraceEvents
.setSize(oldSize
+ indexshift
);
230 for (int i
= oldSize
- 1; i
>= 0; i
--) {
231 fTraceEvents
.set(i
+ indexshift
, fTraceEvents
.get(i
));
233 for (int i
= 0; i
< indexshift
; i
++) {
234 fTraceEvents
.set(i
, null);
238 public ITmfTrace
getTrace() {
242 public void setLastRank(long rank
) {
246 public long getLastRank() {