Commit | Line | Data |
---|---|---|
ce62370f FC |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010 Ericsson\r | |
013a5f1c | 3 | *\r |
ce62370f FC |
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 | |
013a5f1c | 8 | *\r |
ce62370f FC |
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 | 20 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;\r |
fb5cad3d | 21 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;\r |
ce62370f | 22 | \r |
013a5f1c AM |
23 | /**\r |
24 | * An entry (row) in the time chart analysis view\r | |
25 | *\r | |
26 | * @version 1.0\r | |
27 | * @author Patrick Tasse\r | |
28 | */\r | |
fb5cad3d | 29 | public class TimeChartAnalysisEntry implements ITimeGraphEntry {\r |
ce62370f | 30 | \r |
6256d8ad | 31 | private final ITmfTrace fTrace;\r |
013a5f1c | 32 | private final Vector<TimeChartEvent> fTraceEvents;\r |
ce62370f FC |
33 | private int fPower = 0; // 2^fPower nanoseconds per vector position\r |
34 | private long fReferenceTime = -1; // time corresponding to beginning of index 0\r | |
35 | private long fStartTime = -1; // time of first event\r | |
36 | private long fStopTime = -1; // time of last event\r | |
37 | private long fLastRank = -1; // rank of last processed trace event\r | |
38 | \r | |
6256d8ad | 39 | TimeChartAnalysisEntry(ITmfTrace trace, int modelSize) {\r |
ce62370f FC |
40 | fTrace = trace;\r |
41 | fTraceEvents = new Vector<TimeChartEvent>(modelSize);\r | |
42 | }\r | |
013a5f1c | 43 | \r |
fb5cad3d PT |
44 | @Override\r |
45 | public ITimeGraphEntry[] getChildren() {\r | |
46 | return null;\r | |
ce62370f | 47 | }\r |
fb5cad3d | 48 | \r |
ce62370f | 49 | @Override\r |
fb5cad3d PT |
50 | public ITimeGraphEntry getParent() {\r |
51 | return null;\r | |
ce62370f FC |
52 | }\r |
53 | \r | |
54 | @Override\r | |
fb5cad3d PT |
55 | public boolean hasChildren() {\r |
56 | return false;\r | |
ce62370f FC |
57 | }\r |
58 | \r | |
59 | @Override\r | |
60 | public String getName() {\r | |
61 | return fTrace.getName();\r | |
62 | }\r | |
63 | \r | |
64 | @Override\r | |
65 | public long getStartTime() {\r | |
66 | return fStartTime;\r | |
67 | }\r | |
68 | \r | |
69 | @Override\r | |
b83af2c3 | 70 | public long getEndTime() {\r |
ce62370f FC |
71 | return fStopTime;\r |
72 | }\r | |
73 | \r | |
bc51e30c PT |
74 | @Override\r |
75 | public boolean hasTimeEvents() {\r | |
76 | return true;\r | |
77 | }\r | |
78 | \r | |
ce62370f | 79 | @Override\r |
fb5cad3d | 80 | public Iterator<ITimeEvent> getTimeEventsIterator() {\r |
ce62370f FC |
81 | return new EntryIterator(0, Long.MAX_VALUE, 0);\r |
82 | }\r | |
013a5f1c | 83 | \r |
ce62370f | 84 | @Override\r |
fb5cad3d | 85 | public Iterator<ITimeEvent> getTimeEventsIterator(long startTime, long stopTime, long maxDuration) {\r |
ce62370f FC |
86 | return new EntryIterator(startTime, stopTime, maxDuration);\r |
87 | }\r | |
013a5f1c | 88 | \r |
ce62370f FC |
89 | private class EntryIterator implements Iterator<ITimeEvent> {\r |
90 | private final long fIteratorStartTime;\r | |
91 | private final long fIteratorStopTime;\r | |
92 | private final long fIteratorMaxDuration;\r | |
93 | private long lastTime = -1;\r | |
94 | private TimeChartEvent next = null;\r | |
95 | private Iterator<ITimeEvent> nestedIterator = null;\r | |
013a5f1c | 96 | \r |
ce62370f FC |
97 | public EntryIterator(long startTime, long stopTime, long maxDuration) {\r |
98 | fIteratorStartTime = startTime;\r | |
99 | fIteratorStopTime = stopTime;\r | |
100 | fIteratorMaxDuration = maxDuration;\r | |
101 | }\r | |
013a5f1c | 102 | \r |
ce62370f FC |
103 | @Override\r |
104 | public boolean hasNext() {\r | |
105 | synchronized (fTraceEvents) {\r | |
013a5f1c AM |
106 | if (next != null) {\r |
107 | return true;\r | |
108 | }\r | |
ce62370f FC |
109 | if (nestedIterator != null) {\r |
110 | if (nestedIterator.hasNext()) {\r | |
111 | return true;\r | |
ce62370f | 112 | }\r |
abbdd66a | 113 | nestedIterator = null;\r |
ce62370f FC |
114 | }\r |
115 | long time = (lastTime == -1) ? fStartTime : lastTime;\r | |
116 | int index = (fReferenceTime == -1) ? 0 : (int) ((time - fReferenceTime) >> fPower);\r | |
117 | while (index < fTraceEvents.size()) {\r | |
118 | TimeChartEvent event = fTraceEvents.get(index++);\r | |
119 | if (event != null && (lastTime == -1 || event.getTime() > time)) {\r | |
120 | if (event.getTime() + event.getDuration() >= fIteratorStartTime && event.getTime() <= fIteratorStopTime) {\r | |
121 | if (event.getItemizedEntry() == null || event.getDuration() <= fIteratorMaxDuration) {\r | |
122 | lastTime = event.getTime() + event.getDuration();\r | |
123 | next = event;\r | |
124 | return true;\r | |
ce62370f | 125 | }\r |
abbdd66a AM |
126 | nestedIterator = event.getItemizedEntry().getTimeEventsIterator(fIteratorStartTime, fIteratorStopTime, fIteratorMaxDuration);\r |
127 | return nestedIterator.hasNext();\r | |
ce62370f FC |
128 | }\r |
129 | }\r | |
130 | }\r | |
131 | return false;\r | |
132 | }\r | |
133 | }\r | |
134 | \r | |
135 | @Override\r | |
136 | public TimeChartEvent next() {\r | |
137 | synchronized (fTraceEvents) {\r | |
138 | if (nestedIterator != null) {\r | |
139 | TimeChartEvent event = (TimeChartEvent) nestedIterator.next();\r | |
140 | lastTime = event.getTime() + event.getDuration();\r | |
013a5f1c | 141 | return event;\r |
ce62370f FC |
142 | }\r |
143 | if (hasNext()) {\r | |
144 | TimeChartEvent event = next;\r | |
145 | next = null;\r | |
146 | return event;\r | |
147 | }\r | |
148 | throw new NoSuchElementException();\r | |
149 | }\r | |
150 | }\r | |
151 | \r | |
152 | @Override\r | |
153 | public void remove() {\r | |
154 | throw new UnsupportedOperationException();\r | |
155 | }\r | |
013a5f1c | 156 | \r |
ce62370f FC |
157 | }\r |
158 | \r | |
20ff3b75 AM |
159 | /**\r |
160 | * Add a time event to the time chart entry\r | |
161 | *\r | |
162 | * @param timeEvent\r | |
163 | * The event to add\r | |
164 | */\r | |
ce62370f FC |
165 | public void addTraceEvent(ITimeEvent timeEvent) {\r |
166 | long time = timeEvent.getTime();\r | |
167 | synchronized (fTraceEvents) {\r | |
168 | long index = (fReferenceTime == -1) ? 0 : (time - fReferenceTime) >> fPower;\r | |
169 | if (index < 0) {\r | |
170 | if (fTraceEvents.capacity() - fTraceEvents.size() < -index) {\r | |
171 | int powershift = (-index + fTraceEvents.size() <= 2 * fTraceEvents.capacity()) ? 1 :\r | |
172 | (int) Math.ceil(Math.log((double) (-index + fTraceEvents.size()) / fTraceEvents.capacity()) / Math.log(2));\r | |
173 | merge(powershift);\r | |
174 | index = (int) ((time - fReferenceTime) >> fPower);\r | |
175 | }\r | |
176 | shift((int) -index);\r | |
177 | index = 0;\r | |
178 | fTraceEvents.set(0, (TimeChartEvent) timeEvent);\r | |
179 | } else if (index < fTraceEvents.capacity()) {\r | |
180 | if (index >= fTraceEvents.size()) {\r | |
181 | fTraceEvents.setSize((int) index + 1);\r | |
182 | }\r | |
183 | } else {\r | |
184 | int powershift = (index < 2 * fTraceEvents.capacity()) ? 1 :\r | |
185 | (int) Math.ceil(Math.log((double) (index + 1) / fTraceEvents.capacity()) / Math.log(2));\r | |
186 | merge(powershift);\r | |
187 | index = (int) ((time - fReferenceTime) >> fPower);\r | |
188 | fTraceEvents.setSize((int) index + 1);\r | |
189 | }\r | |
abbdd66a | 190 | TimeChartEvent event = fTraceEvents.get((int) index);\r |
ce62370f FC |
191 | if (event == null) {\r |
192 | fTraceEvents.set((int) index, (TimeChartEvent) timeEvent);\r | |
193 | } else {\r | |
194 | if (event.getItemizedEntry() == null) {\r | |
195 | event.merge((TimeChartEvent) timeEvent);\r | |
196 | } else {\r | |
197 | event.mergeDecorations((TimeChartEvent) timeEvent);\r | |
198 | event.getItemizedEntry().addTraceEvent(timeEvent);\r | |
199 | }\r | |
200 | }\r | |
201 | if (fReferenceTime == -1 || time < fReferenceTime) {\r | |
202 | fReferenceTime = (time >> fPower) << fPower;\r | |
203 | }\r | |
204 | if (fStartTime == -1 || time < fStartTime) {\r | |
205 | fStartTime = time;\r | |
206 | }\r | |
207 | if (fStopTime == -1 || time > fStopTime) {\r | |
208 | fStopTime = time;\r | |
209 | }\r | |
210 | }\r | |
211 | }\r | |
212 | \r | |
213 | private void merge(int powershift) {\r | |
214 | fPower += powershift;\r | |
215 | fReferenceTime = (fReferenceTime >> fPower) << fPower;\r | |
216 | int index = 0;\r | |
217 | for (int i = 0; i < fTraceEvents.size(); i++) {\r | |
218 | TimeChartEvent event = fTraceEvents.get(i);\r | |
219 | if (event != null) {\r | |
220 | index = (int) ((event.getTime() - fReferenceTime) >> fPower);\r | |
abbdd66a | 221 | TimeChartEvent mergedEvent = fTraceEvents.get(index);\r |
ce62370f FC |
222 | if (mergedEvent == null) {\r |
223 | fTraceEvents.set(index, event);\r | |
224 | } else {\r | |
225 | mergedEvent.merge(event);\r | |
226 | }\r | |
227 | if (i != index) {\r | |
228 | fTraceEvents.set(i, null);\r | |
229 | }\r | |
230 | }\r | |
231 | }\r | |
232 | fTraceEvents.setSize(index + 1);\r | |
233 | }\r | |
234 | \r | |
235 | private void shift(int indexshift) {\r | |
236 | int oldSize = fTraceEvents.size();\r | |
237 | fTraceEvents.setSize(oldSize + indexshift);\r | |
238 | for (int i = oldSize - 1; i >= 0; i--) {\r | |
239 | fTraceEvents.set(i + indexshift, fTraceEvents.get(i));\r | |
240 | }\r | |
241 | for (int i = 0; i < indexshift; i++) {\r | |
242 | fTraceEvents.set(i, null);\r | |
243 | }\r | |
244 | }\r | |
013a5f1c | 245 | \r |
20ff3b75 AM |
246 | /**\r |
247 | * Retrieve the trace associated with this entry\r | |
248 | *\r | |
249 | * @return The trace object\r | |
250 | */\r | |
6256d8ad | 251 | public ITmfTrace getTrace() {\r |
ce62370f FC |
252 | return fTrace;\r |
253 | }\r | |
013a5f1c | 254 | \r |
20ff3b75 AM |
255 | /**\r |
256 | * Set the last rank of the entry\r | |
257 | *\r | |
258 | * @param rank\r | |
259 | * The rank to set\r | |
260 | */\r | |
ce62370f FC |
261 | public void setLastRank(long rank) {\r |
262 | fLastRank = rank;\r | |
263 | }\r | |
013a5f1c | 264 | \r |
20ff3b75 AM |
265 | /**\r |
266 | * Retrieve the last rank of the entry\r | |
267 | *\r | |
268 | * @return The last rank\r | |
269 | */\r | |
ce62370f FC |
270 | public long getLastRank() {\r |
271 | return fLastRank;\r | |
272 | }\r | |
273 | }\r |