Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
e31e01e8 | 2 | * Copyright (c) 2009, 2010 Ericsson |
8c8bf09f ASL |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.trace; | |
14 | ||
b0a282fb | 15 | import java.io.File; |
62d1696a | 16 | import java.io.FileNotFoundException; |
62d1696a | 17 | import java.util.Collections; |
8c8bf09f ASL |
18 | import java.util.Vector; |
19 | ||
fc6ccf6f | 20 | import org.eclipse.linuxtools.tmf.component.TmfEventProvider; |
8c8bf09f ASL |
21 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
22 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
23 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
2fb2eb37 FC |
24 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; |
25 | import org.eclipse.linuxtools.tmf.request.ITmfEventRequest; | |
8c8bf09f ASL |
26 | |
27 | /** | |
146a887c | 28 | * <b><u>TmfTrace</u></b> |
8c8bf09f | 29 | * <p> |
146a887c FC |
30 | * Abstract implementation of ITmfTrace. It should be sufficient to extend this |
31 | * class and provide implementation for <code>getCurrentLocation()</code> and | |
32 | * <code>seekLocation()</code>, as well as a proper parser, to have a working | |
4e3aa37d | 33 | * concrete implementation. |
ff4ed569 | 34 | * <p> |
54d55ced | 35 | * Note: The notion of event rank is still under heavy discussion. Although |
ff4ed569 | 36 | * used by the Events View and probably useful in the general case, there |
54d55ced | 37 | * is no easy way to implement it for LTTng (actually a strong case is being |
ff4ed569 FC |
38 | * made that this is useless). |
39 | * <p> | |
40 | * That it is not supported by LTTng does by no mean indicate that it is not | |
41 | * useful for (just about) every other tracing tool. Therefore, this class | |
42 | * provides a minimal (and partial) implementation of rank. However, the current | |
43 | * implementation should not be relied on in the general case. | |
54d55ced | 44 | * |
4e3aa37d | 45 | * TODO: Add support for live streaming (notifications, incremental indexing, ...) |
8c8bf09f | 46 | */ |
ff4ed569 | 47 | public abstract class TmfTrace<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace, Cloneable { |
62d1696a | 48 | |
e31e01e8 | 49 | // ------------------------------------------------------------------------ |
62d1696a | 50 | // Constants |
e31e01e8 | 51 | // ------------------------------------------------------------------------ |
62d1696a FC |
52 | |
53 | // The default number of events to cache | |
e31e01e8 | 54 | // TODO: Make the DEFAULT_CACHE_SIZE a preference |
8d2e2848 | 55 | public static final int DEFAULT_CACHE_SIZE = 1000; |
8c8bf09f | 56 | |
e31e01e8 | 57 | // ------------------------------------------------------------------------ |
8c8bf09f | 58 | // Attributes |
e31e01e8 | 59 | // ------------------------------------------------------------------------ |
8c8bf09f | 60 | |
b0a282fb FC |
61 | // The trace path |
62 | private final String fPath; | |
63 | ||
8d2e2848 | 64 | // The cache page size AND checkpoints interval |
9f584e4c | 65 | protected int fIndexPageSize; |
62d1696a FC |
66 | |
67 | // The set of event stream checkpoints (for random access) | |
9f584e4c | 68 | protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>(); |
62d1696a FC |
69 | |
70 | // The number of events collected | |
a3fe52fc | 71 | protected long fNbEvents = 0; |
62d1696a FC |
72 | |
73 | // The time span of the event stream | |
cb866e08 FC |
74 | private TmfTimestamp fStartTime = TmfTimestamp.BigCrunch; |
75 | private TmfTimestamp fEndTime = TmfTimestamp.BigBang; | |
62d1696a | 76 | |
e31e01e8 | 77 | // ------------------------------------------------------------------------ |
50adc88e | 78 | // Constructors |
e31e01e8 | 79 | // ------------------------------------------------------------------------ |
8c8bf09f | 80 | |
ff4ed569 FC |
81 | /** |
82 | * @param path | |
83 | * @throws FileNotFoundException | |
84 | */ | |
ce785d7d FC |
85 | protected TmfTrace(String name, Class<T> type, String path) throws FileNotFoundException { |
86 | this(name, type, path, DEFAULT_CACHE_SIZE); | |
ff4ed569 FC |
87 | } |
88 | ||
62d1696a | 89 | /** |
e31e01e8 FC |
90 | * @param path |
91 | * @param cacheSize | |
62d1696a FC |
92 | * @throws FileNotFoundException |
93 | */ | |
ce785d7d FC |
94 | protected TmfTrace(String name, Class<T> type, String path, int cacheSize) throws FileNotFoundException { |
95 | super(name, type); | |
b0a282fb | 96 | int sep = path.lastIndexOf(File.separator); |
ce785d7d FC |
97 | String simpleName = (sep >= 0) ? path.substring(sep + 1) : path; |
98 | setName(simpleName); | |
b0a282fb | 99 | fPath = path; |
9f584e4c | 100 | fIndexPageSize = (cacheSize > 0) ? cacheSize : DEFAULT_CACHE_SIZE; |
8c8bf09f ASL |
101 | } |
102 | ||
ff4ed569 FC |
103 | /* (non-Javadoc) |
104 | * @see java.lang.Object#clone() | |
62d1696a | 105 | */ |
ff4ed569 FC |
106 | @SuppressWarnings("unchecked") |
107 | @Override | |
108 | public TmfTrace<T> clone() throws CloneNotSupportedException { | |
109 | TmfTrace<T> clone = (TmfTrace<T>) super.clone(); | |
cb866e08 FC |
110 | clone.fCheckpoints = (Vector<TmfCheckpoint>) fCheckpoints; |
111 | clone.fStartTime = new TmfTimestamp(fStartTime); | |
112 | clone.fEndTime = new TmfTimestamp(fEndTime); | |
ff4ed569 | 113 | return clone; |
8c8bf09f ASL |
114 | } |
115 | ||
e31e01e8 | 116 | // ------------------------------------------------------------------------ |
8c8bf09f | 117 | // Accessors |
e31e01e8 | 118 | // ------------------------------------------------------------------------ |
8c8bf09f | 119 | |
62d1696a | 120 | /** |
b0a282fb | 121 | * @return the trace path |
62d1696a | 122 | */ |
b0a282fb FC |
123 | public String getPath() { |
124 | return fPath; | |
8c8bf09f ASL |
125 | } |
126 | ||
62d1696a FC |
127 | /* (non-Javadoc) |
128 | * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents() | |
129 | */ | |
4e3aa37d | 130 | public long getNbEvents() { |
62d1696a | 131 | return fNbEvents; |
8c8bf09f ASL |
132 | } |
133 | ||
b0a282fb FC |
134 | /** |
135 | * @return the size of the cache | |
136 | */ | |
8d2e2848 | 137 | public int getCacheSize() { |
9f584e4c | 138 | return fIndexPageSize; |
b0a282fb FC |
139 | } |
140 | ||
62d1696a FC |
141 | /* (non-Javadoc) |
142 | * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange() | |
143 | */ | |
8c8bf09f | 144 | public TmfTimeRange getTimeRange() { |
cb866e08 | 145 | return new TmfTimeRange(fStartTime, fEndTime); |
8c8bf09f ASL |
146 | } |
147 | ||
e31e01e8 FC |
148 | /* (non-Javadoc) |
149 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime() | |
150 | */ | |
146a887c | 151 | public TmfTimestamp getStartTime() { |
cb866e08 | 152 | return fStartTime; |
146a887c FC |
153 | } |
154 | ||
e31e01e8 FC |
155 | /* (non-Javadoc) |
156 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime() | |
157 | */ | |
146a887c | 158 | public TmfTimestamp getEndTime() { |
cb866e08 | 159 | return fEndTime; |
146a887c FC |
160 | } |
161 | ||
ff4ed569 FC |
162 | @SuppressWarnings("unchecked") |
163 | public Vector<TmfCheckpoint> getCheckpoints() { | |
164 | return (Vector<TmfCheckpoint>) fCheckpoints.clone(); | |
54d55ced FC |
165 | } |
166 | ||
e31e01e8 | 167 | // ------------------------------------------------------------------------ |
8c8bf09f | 168 | // Operators |
e31e01e8 | 169 | // ------------------------------------------------------------------------ |
8c8bf09f | 170 | |
4e3aa37d | 171 | protected void setTimeRange(TmfTimeRange range) { |
cb866e08 FC |
172 | fStartTime = range.getStartTime(); |
173 | fEndTime = range.getEndTime(); | |
4e3aa37d FC |
174 | } |
175 | ||
176 | protected void setStartTime(TmfTimestamp startTime) { | |
cb866e08 | 177 | fStartTime = startTime; |
4e3aa37d FC |
178 | } |
179 | ||
180 | protected void setEndTime(TmfTimestamp endTime) { | |
cb866e08 | 181 | fEndTime = endTime; |
4e3aa37d FC |
182 | } |
183 | ||
e31e01e8 FC |
184 | // ------------------------------------------------------------------------ |
185 | // TmfProvider | |
186 | // ------------------------------------------------------------------------ | |
187 | ||
188 | @Override | |
2fb2eb37 FC |
189 | public ITmfContext armRequest(ITmfDataRequest<T> request) { |
190 | if (request instanceof ITmfEventRequest<?>) { | |
191 | return seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime()); | |
e31e01e8 | 192 | } |
ff4ed569 | 193 | return seekEvent(request.getIndex()); |
e31e01e8 FC |
194 | } |
195 | ||
196 | /** | |
197 | * Return the next piece of data based on the context supplied. The context | |
198 | * would typically be updated for the subsequent read. | |
199 | * | |
200 | * @param context | |
201 | * @return | |
202 | */ | |
203 | @SuppressWarnings("unchecked") | |
204 | @Override | |
205 | public T getNext(ITmfContext context) { | |
9f584e4c FC |
206 | if (context instanceof TmfContext) { |
207 | return (T) getNextEvent((TmfContext) context); | |
e31e01e8 FC |
208 | } |
209 | return null; | |
210 | } | |
211 | ||
e31e01e8 FC |
212 | // ------------------------------------------------------------------------ |
213 | // ITmfTrace | |
214 | // ------------------------------------------------------------------------ | |
215 | ||
146a887c FC |
216 | /* (non-Javadoc) |
217 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp) | |
218 | */ | |
9f584e4c | 219 | public TmfContext seekEvent(TmfTimestamp timestamp) { |
62d1696a | 220 | |
4e3aa37d FC |
221 | if (timestamp == null) { |
222 | timestamp = TmfTimestamp.BigBang; | |
223 | } | |
224 | ||
225 | // First, find the right checkpoint | |
9f584e4c | 226 | int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null)); |
62d1696a | 227 | |
8d2e2848 | 228 | // In the very likely case that the checkpoint was not found, bsearch |
62d1696a FC |
229 | // returns its negated would-be location (not an offset...). From that |
230 | // index, we can then position the stream and get the event. | |
231 | if (index < 0) { | |
232 | index = Math.max(0, -(index + 2)); | |
233 | } | |
234 | ||
235 | // Position the stream at the checkpoint | |
452ad365 | 236 | ITmfLocation<?> location; |
e31e01e8 FC |
237 | synchronized (fCheckpoints) { |
238 | if (fCheckpoints.size() > 0) { | |
239 | if (index >= fCheckpoints.size()) { | |
240 | index = fCheckpoints.size() - 1; | |
241 | } | |
242 | location = fCheckpoints.elementAt(index).getLocation(); | |
243 | } | |
244 | else { | |
245 | location = null; | |
246 | } | |
8d2e2848 | 247 | } |
54d55ced FC |
248 | TmfContext context = seekLocation(location); |
249 | context.setRank(index * fIndexPageSize); | |
62d1696a | 250 | |
54d55ced | 251 | // And locate the event |
ff4ed569 | 252 | TmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype... |
62d1696a FC |
253 | TmfEvent event = getNextEvent(nextEventContext); |
254 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { | |
54d55ced FC |
255 | context.setLocation(nextEventContext.getLocation().clone()); |
256 | context.updateRank(1); | |
62d1696a FC |
257 | event = getNextEvent(nextEventContext); |
258 | } | |
259 | ||
54d55ced | 260 | return context; |
62d1696a FC |
261 | } |
262 | ||
146a887c FC |
263 | /* (non-Javadoc) |
264 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int) | |
265 | */ | |
9f584e4c | 266 | public TmfContext seekEvent(long rank) { |
62d1696a FC |
267 | |
268 | // Position the stream at the previous checkpoint | |
9f584e4c | 269 | int index = (int) rank / fIndexPageSize; |
452ad365 | 270 | ITmfLocation<?> location; |
e31e01e8 | 271 | synchronized (fCheckpoints) { |
54d55ced FC |
272 | if (fCheckpoints.size() == 0) { |
273 | location = null; | |
274 | } | |
275 | else { | |
e31e01e8 | 276 | if (index >= fCheckpoints.size()) { |
54d55ced | 277 | index = fCheckpoints.size() - 1; |
e31e01e8 FC |
278 | } |
279 | location = fCheckpoints.elementAt(index).getLocation(); | |
280 | } | |
8d2e2848 | 281 | } |
54d55ced | 282 | |
9f584e4c FC |
283 | TmfContext context = seekLocation(location); |
284 | long pos = index * fIndexPageSize; | |
285 | context.setRank(pos); | |
e31e01e8 | 286 | |
9f584e4c | 287 | if (pos < rank) { |
e31e01e8 | 288 | TmfEvent event = getNextEvent(context); |
9f584e4c | 289 | while (event != null && ++pos < rank) { |
e31e01e8 FC |
290 | event = getNextEvent(context); |
291 | } | |
165c977c | 292 | } |
62d1696a | 293 | |
8f50c396 | 294 | return context; |
8c8bf09f ASL |
295 | } |
296 | ||
146a887c FC |
297 | /* (non-Javadoc) |
298 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext) | |
299 | */ | |
9f584e4c | 300 | public synchronized TmfEvent getNextEvent(TmfContext context) { |
e31e01e8 | 301 | // parseEvent() does not update the context |
cc6eec3e | 302 | TmfEvent event = parseEvent(context); |
4e3aa37d | 303 | if (event != null) { |
550d787e | 304 | updateIndex(context, context.getRank(), event.getTimestamp()); |
cb866e08 | 305 | context.setLocation(getCurrentLocation()); |
54d55ced | 306 | context.updateRank(1); |
4e3aa37d FC |
307 | processEvent(event); |
308 | } | |
146a887c FC |
309 | return event; |
310 | } | |
8c8bf09f | 311 | |
cb866e08 FC |
312 | protected synchronized void updateIndex(ITmfContext context, long rank, TmfTimestamp timestamp) { |
313 | if (fStartTime.compareTo(timestamp, false) > 0) fStartTime = timestamp; | |
314 | if (fEndTime.compareTo(timestamp, false) < 0) fEndTime = timestamp; | |
315 | if (context.isValidRank()) { | |
316 | if (fNbEvents <= rank) | |
317 | fNbEvents = rank + 1; | |
318 | // Build the index as we go along | |
319 | if ((rank % fIndexPageSize) == 0) { | |
320 | // Determine the table position | |
321 | long position = rank / fIndexPageSize; | |
322 | // Add new entry at proper location (if empty) | |
323 | if (fCheckpoints.size() == position) { | |
324 | ITmfLocation<?> location = context.getLocation().clone(); | |
325 | fCheckpoints.add(new TmfCheckpoint(timestamp, location)); | |
326 | // System.out.println(getName() + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString()); | |
327 | } | |
550d787e FC |
328 | } |
329 | } | |
330 | } | |
331 | ||
4e3aa37d | 332 | /** |
e31e01e8 FC |
333 | * Hook for "special" processing by the concrete class |
334 | * (called by getNextEvent()) | |
335 | * | |
146a887c FC |
336 | * @param event |
337 | */ | |
ff4ed569 | 338 | protected void processEvent(TmfEvent event) { |
146a887c | 339 | // Do nothing by default |
62d1696a | 340 | } |
4e3aa37d | 341 | |
e31e01e8 FC |
342 | /** |
343 | * To be implemented by the concrete class | |
4e3aa37d | 344 | */ |
452ad365 FC |
345 | public abstract TmfContext seekLocation(ITmfLocation<?> location); |
346 | public abstract ITmfLocation<?> getCurrentLocation(); | |
9f584e4c | 347 | public abstract TmfEvent parseEvent(TmfContext context); |
4e3aa37d | 348 | |
e31e01e8 FC |
349 | // ------------------------------------------------------------------------ |
350 | // toString | |
351 | // ------------------------------------------------------------------------ | |
8d2e2848 FC |
352 | |
353 | /* (non-Javadoc) | |
354 | * @see java.lang.Object#toString() | |
355 | */ | |
356 | @Override | |
357 | public String toString() { | |
ce785d7d | 358 | return "[TmfTrace (" + getName() + ")]"; |
8d2e2848 | 359 | } |
146a887c | 360 | |
8c8bf09f | 361 | } |