Commit | Line | Data |
---|---|---|
ce62370f FC |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010 Ericsson\r | |
3 | * \r | |
4 | * All rights reserved. This program and the accompanying materials are\r | |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
8 | * \r | |
9 | * Contributors:\r | |
10 | * Patrick Tasse - Initial API and implementation\r | |
11 | *******************************************************************************/\r | |
12 | \r | |
13 | package org.eclipse.linuxtools.tmf.ui.views.timechart;\r | |
14 | \r | |
15 | import java.util.Iterator;\r | |
16 | import java.util.NoSuchElementException;\r | |
17 | import java.util.Vector;\r | |
18 | \r | |
6c13869b | 19 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;\r |
2fa130b8 PT |
20 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;\r |
21 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITmfTimeAnalysisEntry;\r | |
ce62370f FC |
22 | \r |
23 | public class TimeChartAnalysisEntry implements ITmfTimeAnalysisEntry {\r | |
24 | \r | |
828e5592 | 25 | private ITmfTrace<?> fTrace;\r |
ce62370f FC |
26 | private String fGroup;\r |
27 | private Vector<TimeChartEvent> fTraceEvents;\r | |
28 | private int fPower = 0; // 2^fPower nanoseconds per vector position\r | |
29 | private long fReferenceTime = -1; // time corresponding to beginning of index 0\r | |
30 | private long fStartTime = -1; // time of first event\r | |
31 | private long fStopTime = -1; // time of last event\r | |
32 | private long fLastRank = -1; // rank of last processed trace event\r | |
33 | \r | |
828e5592 | 34 | TimeChartAnalysisEntry(ITmfTrace<?> trace, int modelSize) {\r |
ce62370f FC |
35 | fTrace = trace;\r |
36 | fTraceEvents = new Vector<TimeChartEvent>(modelSize);\r | |
37 | }\r | |
38 | \r | |
828e5592 | 39 | TimeChartAnalysisEntry(ITmfTrace<?> trace, String group, int modelSize) {\r |
ce62370f FC |
40 | fTrace = trace;\r |
41 | fTraceEvents = new Vector<TimeChartEvent>(modelSize);\r | |
42 | fGroup = group;\r | |
43 | }\r | |
44 | \r | |
45 | @Override\r | |
46 | public String getGroupName() {\r | |
47 | return fGroup;\r | |
48 | }\r | |
49 | \r | |
50 | @Override\r | |
51 | public int getId() {\r | |
52 | // TODO Auto-generated method stub\r | |
53 | return 0;\r | |
54 | }\r | |
55 | \r | |
56 | @Override\r | |
57 | public String getName() {\r | |
58 | return fTrace.getName();\r | |
59 | }\r | |
60 | \r | |
61 | @Override\r | |
62 | public long getStartTime() {\r | |
63 | return fStartTime;\r | |
64 | }\r | |
65 | \r | |
66 | @Override\r | |
67 | public long getStopTime() {\r | |
68 | return fStopTime;\r | |
69 | }\r | |
70 | \r | |
71 | @Override\r | |
72 | @Deprecated public <T extends ITimeEvent> Vector<T> getTraceEvents() {\r | |
73 | return null;\r | |
74 | }\r | |
75 | \r | |
76 | @Override\r | |
77 | public Iterator<ITimeEvent> getTraceEventsIterator() {\r | |
78 | return new EntryIterator(0, Long.MAX_VALUE, 0);\r | |
79 | }\r | |
80 | \r | |
81 | @Override\r | |
82 | public Iterator<ITimeEvent> getTraceEventsIterator(long startTime, long stopTime, long maxDuration) {\r | |
83 | return new EntryIterator(startTime, stopTime, maxDuration);\r | |
84 | }\r | |
85 | \r | |
86 | private class EntryIterator implements Iterator<ITimeEvent> {\r | |
87 | private final long fIteratorStartTime;\r | |
88 | private final long fIteratorStopTime;\r | |
89 | private final long fIteratorMaxDuration;\r | |
90 | private long lastTime = -1;\r | |
91 | private TimeChartEvent next = null;\r | |
92 | private Iterator<ITimeEvent> nestedIterator = null;\r | |
93 | \r | |
94 | public EntryIterator(long startTime, long stopTime, long maxDuration) {\r | |
95 | fIteratorStartTime = startTime;\r | |
96 | fIteratorStopTime = stopTime;\r | |
97 | fIteratorMaxDuration = maxDuration;\r | |
98 | }\r | |
99 | \r | |
100 | @Override\r | |
101 | public boolean hasNext() {\r | |
102 | synchronized (fTraceEvents) {\r | |
103 | if (next != null) return true;\r | |
104 | if (nestedIterator != null) {\r | |
105 | if (nestedIterator.hasNext()) {\r | |
106 | return true;\r | |
107 | } else {\r | |
108 | nestedIterator = null;\r | |
109 | }\r | |
110 | }\r | |
111 | long time = (lastTime == -1) ? fStartTime : lastTime;\r | |
112 | int index = (fReferenceTime == -1) ? 0 : (int) ((time - fReferenceTime) >> fPower);\r | |
113 | while (index < fTraceEvents.size()) {\r | |
114 | TimeChartEvent event = fTraceEvents.get(index++);\r | |
115 | if (event != null && (lastTime == -1 || event.getTime() > time)) {\r | |
116 | if (event.getTime() + event.getDuration() >= fIteratorStartTime && event.getTime() <= fIteratorStopTime) {\r | |
117 | if (event.getItemizedEntry() == null || event.getDuration() <= fIteratorMaxDuration) {\r | |
118 | lastTime = event.getTime() + event.getDuration();\r | |
119 | next = event;\r | |
120 | return true;\r | |
121 | } else {\r | |
122 | nestedIterator = event.getItemizedEntry().getTraceEventsIterator(fIteratorStartTime, fIteratorStopTime, fIteratorMaxDuration);\r | |
123 | return nestedIterator.hasNext();\r | |
124 | }\r | |
125 | }\r | |
126 | }\r | |
127 | }\r | |
128 | return false;\r | |
129 | }\r | |
130 | }\r | |
131 | \r | |
132 | @Override\r | |
133 | public TimeChartEvent next() {\r | |
134 | synchronized (fTraceEvents) {\r | |
135 | if (nestedIterator != null) {\r | |
136 | TimeChartEvent event = (TimeChartEvent) nestedIterator.next();\r | |
137 | lastTime = event.getTime() + event.getDuration();\r | |
138 | return event; \r | |
139 | }\r | |
140 | if (hasNext()) {\r | |
141 | TimeChartEvent event = next;\r | |
142 | next = null;\r | |
143 | return event;\r | |
144 | }\r | |
145 | throw new NoSuchElementException();\r | |
146 | }\r | |
147 | }\r | |
148 | \r | |
149 | @Override\r | |
150 | public void remove() {\r | |
151 | throw new UnsupportedOperationException();\r | |
152 | }\r | |
153 | \r | |
154 | }\r | |
155 | \r | |
156 | @Override\r | |
157 | public void addTraceEvent(ITimeEvent timeEvent) {\r | |
158 | long time = timeEvent.getTime();\r | |
159 | synchronized (fTraceEvents) {\r | |
160 | long index = (fReferenceTime == -1) ? 0 : (time - fReferenceTime) >> fPower;\r | |
161 | if (index < 0) {\r | |
162 | if (fTraceEvents.capacity() - fTraceEvents.size() < -index) {\r | |
163 | int powershift = (-index + fTraceEvents.size() <= 2 * fTraceEvents.capacity()) ? 1 :\r | |
164 | (int) Math.ceil(Math.log((double) (-index + fTraceEvents.size()) / fTraceEvents.capacity()) / Math.log(2));\r | |
165 | merge(powershift);\r | |
166 | index = (int) ((time - fReferenceTime) >> fPower);\r | |
167 | }\r | |
168 | shift((int) -index);\r | |
169 | index = 0;\r | |
170 | fTraceEvents.set(0, (TimeChartEvent) timeEvent);\r | |
171 | } else if (index < fTraceEvents.capacity()) {\r | |
172 | if (index >= fTraceEvents.size()) {\r | |
173 | fTraceEvents.setSize((int) index + 1);\r | |
174 | }\r | |
175 | } else {\r | |
176 | int powershift = (index < 2 * fTraceEvents.capacity()) ? 1 :\r | |
177 | (int) Math.ceil(Math.log((double) (index + 1) / fTraceEvents.capacity()) / Math.log(2));\r | |
178 | merge(powershift);\r | |
179 | index = (int) ((time - fReferenceTime) >> fPower);\r | |
180 | fTraceEvents.setSize((int) index + 1);\r | |
181 | }\r | |
182 | TimeChartEvent event = (TimeChartEvent) fTraceEvents.get((int) index);\r | |
183 | if (event == null) {\r | |
184 | fTraceEvents.set((int) index, (TimeChartEvent) timeEvent);\r | |
185 | } else {\r | |
186 | if (event.getItemizedEntry() == null) {\r | |
187 | event.merge((TimeChartEvent) timeEvent);\r | |
188 | } else {\r | |
189 | event.mergeDecorations((TimeChartEvent) timeEvent);\r | |
190 | event.getItemizedEntry().addTraceEvent(timeEvent);\r | |
191 | }\r | |
192 | }\r | |
193 | if (fReferenceTime == -1 || time < fReferenceTime) {\r | |
194 | fReferenceTime = (time >> fPower) << fPower;\r | |
195 | }\r | |
196 | if (fStartTime == -1 || time < fStartTime) {\r | |
197 | fStartTime = time;\r | |
198 | }\r | |
199 | if (fStopTime == -1 || time > fStopTime) {\r | |
200 | fStopTime = time;\r | |
201 | }\r | |
202 | }\r | |
203 | }\r | |
204 | \r | |
205 | private void merge(int powershift) {\r | |
206 | fPower += powershift;\r | |
207 | fReferenceTime = (fReferenceTime >> fPower) << fPower;\r | |
208 | int index = 0;\r | |
209 | for (int i = 0; i < fTraceEvents.size(); i++) {\r | |
210 | TimeChartEvent event = fTraceEvents.get(i);\r | |
211 | if (event != null) {\r | |
212 | index = (int) ((event.getTime() - fReferenceTime) >> fPower);\r | |
213 | TimeChartEvent mergedEvent = (TimeChartEvent) fTraceEvents.get(index);\r | |
214 | if (mergedEvent == null) {\r | |
215 | fTraceEvents.set(index, event);\r | |
216 | } else {\r | |
217 | mergedEvent.merge(event);\r | |
218 | }\r | |
219 | if (i != index) {\r | |
220 | fTraceEvents.set(i, null);\r | |
221 | }\r | |
222 | }\r | |
223 | }\r | |
224 | fTraceEvents.setSize(index + 1);\r | |
225 | }\r | |
226 | \r | |
227 | private void shift(int indexshift) {\r | |
228 | int oldSize = fTraceEvents.size();\r | |
229 | fTraceEvents.setSize(oldSize + indexshift);\r | |
230 | for (int i = oldSize - 1; i >= 0; i--) {\r | |
231 | fTraceEvents.set(i + indexshift, fTraceEvents.get(i));\r | |
232 | }\r | |
233 | for (int i = 0; i < indexshift; i++) {\r | |
234 | fTraceEvents.set(i, null);\r | |
235 | }\r | |
236 | }\r | |
237 | \r | |
828e5592 | 238 | public ITmfTrace<?> getTrace() {\r |
ce62370f FC |
239 | return fTrace;\r |
240 | }\r | |
241 | \r | |
242 | public void setLastRank(long rank) {\r | |
243 | fLastRank = rank;\r | |
244 | }\r | |
245 | \r | |
246 | public long getLastRank() {\r | |
247 | return fLastRank;\r | |
248 | }\r | |
249 | }\r |