Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
09e86496 | 2 | * Copyright (c) 2009, 2010, 2012 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: | |
20658947 FC |
10 | * Francois Chouinard - Initial API and implementation |
11 | * Francois Chouinard - Updated as per TMF Trace Model 1.0 | |
8c8bf09f ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.trace; |
8c8bf09f | 15 | |
6f4a1d2b | 16 | import java.io.File; |
62d1696a | 17 | import java.io.FileNotFoundException; |
8c8bf09f | 18 | |
12c155f5 | 19 | import org.eclipse.core.resources.IProject; |
828e5592 | 20 | import org.eclipse.core.resources.IResource; |
c7e2f194 | 21 | import org.eclipse.core.runtime.Path; |
6c13869b | 22 | import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; |
72f1e62a | 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
4df4581d | 24 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b FC |
25 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
26 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
27 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; | |
28 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; | |
8c8bf09f ASL |
29 | |
30 | /** | |
146a887c | 31 | * <b><u>TmfTrace</u></b> |
8c8bf09f | 32 | * <p> |
09e86496 FC |
33 | * Abstract implementation of ITmfTrace. |
34 | * <p> | |
20658947 | 35 | * Document me... |
8c8bf09f | 36 | */ |
3791b5df | 37 | public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T> { |
62d1696a | 38 | |
e31e01e8 | 39 | // ------------------------------------------------------------------------ |
62d1696a | 40 | // Constants |
e31e01e8 | 41 | // ------------------------------------------------------------------------ |
62d1696a | 42 | |
3791b5df | 43 | /** |
20658947 | 44 | * The default trace cache size |
3791b5df | 45 | */ |
20658947 | 46 | public static final int DEFAULT_TRACE_CACHE_SIZE = 1000; |
8c8bf09f | 47 | |
e31e01e8 | 48 | // ------------------------------------------------------------------------ |
8c8bf09f | 49 | // Attributes |
e31e01e8 | 50 | // ------------------------------------------------------------------------ |
8c8bf09f | 51 | |
09e86496 FC |
52 | // The resource used for persistent properties for this trace |
53 | private IResource fResource; | |
54 | ||
b0a282fb | 55 | // The trace path |
12c155f5 | 56 | private String fPath; |
b0a282fb | 57 | |
3791b5df | 58 | /** |
7e6347b0 | 59 | * The cache page size |
3791b5df | 60 | */ |
20658947 | 61 | protected int fCacheSize = DEFAULT_TRACE_CACHE_SIZE; |
62d1696a | 62 | |
7e6347b0 FC |
63 | /** |
64 | * The number of events collected so far | |
65 | */ | |
a3fe52fc | 66 | protected long fNbEvents = 0; |
62d1696a FC |
67 | |
68 | // The time span of the event stream | |
a4115405 FC |
69 | private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH; |
70 | private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG; | |
62d1696a | 71 | |
3791b5df | 72 | /** |
085d898f | 73 | * The trace streaming interval (0 = no streaming) |
3791b5df FC |
74 | */ |
75 | protected long fStreamingInterval = 0; | |
085d898f | 76 | |
20658947 FC |
77 | /** |
78 | * The trace indexer | |
79 | */ | |
80 | protected ITmfTraceIndexer<ITmfTrace<ITmfEvent>> fIndexer; | |
81 | ||
7e6347b0 FC |
82 | /** |
83 | * The trace parser | |
84 | */ | |
85 | protected ITmfEventParser<ITmfEvent> fParser; | |
86 | ||
e31e01e8 | 87 | // ------------------------------------------------------------------------ |
3791b5df | 88 | // Construction |
e31e01e8 | 89 | // ------------------------------------------------------------------------ |
8c8bf09f | 90 | |
62d1696a | 91 | /** |
3791b5df | 92 | * The default, parameterless, constructor |
62d1696a | 93 | */ |
20658947 | 94 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
3791b5df FC |
95 | public TmfTrace() { |
96 | super(); | |
7e6347b0 | 97 | fIndexer = new TmfCheckpointIndexer(this); |
05bd3318 FC |
98 | } |
99 | ||
100 | /** | |
1703b536 | 101 | * The standard constructor (non-live trace) |
3791b5df | 102 | * |
20658947 FC |
103 | * @param resource the resource associated to the trace |
104 | * @param type the trace event type | |
105 | * @param path the trace path | |
106 | * @param cacheSize the trace cache size | |
107 | * @throws FileNotFoundException | |
108 | */ | |
109 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize) throws FileNotFoundException { | |
110 | this(resource, type, path, cacheSize, 0, null); | |
111 | } | |
112 | ||
113 | /** | |
1703b536 | 114 | * The standard constructor (live trace) |
20658947 FC |
115 | * |
116 | * @param resource the resource associated to the trace | |
3791b5df FC |
117 | * @param type the trace event type |
118 | * @param path the trace path | |
20658947 FC |
119 | * @param cacheSize the trace cache size |
120 | * @param interval the trace streaming interval | |
05bd3318 FC |
121 | * @throws FileNotFoundException |
122 | */ | |
20658947 FC |
123 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, final long interval) throws FileNotFoundException { |
124 | this(resource, type, path, cacheSize, interval, null); | |
05bd3318 FC |
125 | } |
126 | ||
127 | /** | |
3791b5df FC |
128 | * The full constructor |
129 | * | |
20658947 | 130 | * @param resource the resource associated to the trace |
3791b5df FC |
131 | * @param type the trace event type |
132 | * @param path the trace path | |
20658947 FC |
133 | * @param cacheSize the trace cache size |
134 | * @param indexer the trace indexer | |
05bd3318 FC |
135 | * @throws FileNotFoundException |
136 | */ | |
20658947 FC |
137 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
138 | protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, | |
139 | final long interval, final ITmfTraceIndexer<?> indexer) throws FileNotFoundException { | |
00641a97 | 140 | super(); |
20658947 | 141 | fCacheSize = (cacheSize > 0) ? cacheSize : DEFAULT_TRACE_CACHE_SIZE; |
3791b5df | 142 | fStreamingInterval = interval; |
7e6347b0 | 143 | fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize); |
09e86496 | 144 | initialize(resource, path, type); |
8c8bf09f ASL |
145 | } |
146 | ||
3791b5df FC |
147 | /** |
148 | * Copy constructor | |
149 | * | |
150 | * @param trace the original trace | |
151 | */ | |
20658947 FC |
152 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
153 | public TmfTrace(final TmfTrace<T> trace) throws FileNotFoundException { | |
3791b5df FC |
154 | super(); |
155 | if (trace == null) | |
156 | throw new IllegalArgumentException(); | |
20658947 FC |
157 | fCacheSize = trace.getCacheSize(); |
158 | fStreamingInterval = trace.getStreamingInterval(); | |
7e6347b0 | 159 | fIndexer = new TmfCheckpointIndexer(this); |
09e86496 | 160 | initialize(trace.getResource(), trace.getPath(), trace.getType()); |
3791b5df FC |
161 | } |
162 | ||
7e6347b0 FC |
163 | // ------------------------------------------------------------------------ |
164 | // ITmfTrace - Initializers | |
165 | // ------------------------------------------------------------------------ | |
166 | ||
167 | /* (non-Javadoc) | |
168 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class) | |
169 | */ | |
170 | @Override | |
171 | public void initTrace(final IResource resource, final String path, final Class<T> type) throws FileNotFoundException { | |
172 | initialize(resource, path, type); | |
173 | fIndexer.buildIndex(false); | |
174 | } | |
175 | ||
09e86496 | 176 | /** |
1703b536 FC |
177 | * Initialize the trace common attributes and the base component. |
178 | * | |
179 | * @param resource the Eclipse resource (trace) | |
180 | * @param path the trace path | |
181 | * @param type the trace event type | |
182 | * | |
09e86496 | 183 | * @throws FileNotFoundException |
3791b5df | 184 | */ |
09e86496 | 185 | protected void initialize(final IResource resource, final String path, final Class<T> type) throws FileNotFoundException { |
1703b536 FC |
186 | if (path == null) |
187 | throw new FileNotFoundException(); | |
3791b5df | 188 | fPath = path; |
1703b536 | 189 | fResource = resource; |
25e48683 | 190 | String traceName = (resource != null) ? resource.getName() : null; |
1703b536 FC |
191 | // If no resource was provided, extract the display name the trace path |
192 | if (traceName == null) { | |
193 | final int sep = path.lastIndexOf(Path.SEPARATOR); | |
194 | traceName = (sep >= 0) ? path.substring(sep + 1) : path; | |
195 | } | |
3791b5df FC |
196 | super.init(traceName, type); |
197 | } | |
198 | ||
09e86496 FC |
199 | /* (non-Javadoc) |
200 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String) | |
20658947 FC |
201 | * |
202 | * Default validation: make sure the trace file exists. | |
3791b5df FC |
203 | */ |
204 | @Override | |
085d898f FC |
205 | public boolean validate(final IProject project, final String path) { |
206 | final File file = new File(path); | |
3791b5df FC |
207 | return file.exists(); |
208 | } | |
209 | ||
3791b5df | 210 | // ------------------------------------------------------------------------ |
09e86496 | 211 | // ITmfTrace - Basic getters |
e31e01e8 | 212 | // ------------------------------------------------------------------------ |
8c8bf09f | 213 | |
09e86496 FC |
214 | /* (non-Javadoc) |
215 | * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getType() | |
25e48683 FC |
216 | */ |
217 | @Override | |
09e86496 | 218 | @SuppressWarnings("unchecked") |
25e48683 | 219 | public Class<T> getType() { |
09e86496 | 220 | return (Class<T>) super.getType(); |
25e48683 FC |
221 | } |
222 | ||
09e86496 FC |
223 | /* (non-Javadoc) |
224 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource() | |
62d1696a | 225 | */ |
d4011df2 | 226 | @Override |
09e86496 FC |
227 | public IResource getResource() { |
228 | return fResource; | |
8c8bf09f ASL |
229 | } |
230 | ||
09e86496 FC |
231 | /* (non-Javadoc) |
232 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath() | |
62d1696a | 233 | */ |
d4011df2 | 234 | @Override |
09e86496 FC |
235 | public String getPath() { |
236 | return fPath; | |
8c8bf09f ASL |
237 | } |
238 | ||
20658947 FC |
239 | /* (non-Javadoc) |
240 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize() | |
241 | */ | |
242 | @Override | |
243 | public int getCacheSize() { | |
244 | return fCacheSize; | |
245 | } | |
246 | ||
247 | /* (non-Javadoc) | |
248 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval() | |
249 | */ | |
250 | @Override | |
251 | public long getStreamingInterval() { | |
252 | return fStreamingInterval; | |
253 | } | |
254 | ||
09e86496 FC |
255 | // ------------------------------------------------------------------------ |
256 | // ITmfTrace - Trace characteristics getters | |
257 | // ------------------------------------------------------------------------ | |
258 | ||
259 | /* (non-Javadoc) | |
260 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents() | |
b0a282fb | 261 | */ |
d4011df2 | 262 | @Override |
09e86496 | 263 | public long getNbEvents() { |
3791b5df | 264 | return fNbEvents; |
b0a282fb FC |
265 | } |
266 | ||
09e86496 FC |
267 | /* (non-Javadoc) |
268 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange() | |
62d1696a | 269 | */ |
d4011df2 | 270 | @Override |
12c155f5 | 271 | public TmfTimeRange getTimeRange() { |
cb866e08 | 272 | return new TmfTimeRange(fStartTime, fEndTime); |
8c8bf09f ASL |
273 | } |
274 | ||
09e86496 FC |
275 | /* (non-Javadoc) |
276 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime() | |
e31e01e8 | 277 | */ |
d4011df2 | 278 | @Override |
4df4581d | 279 | public ITmfTimestamp getStartTime() { |
20658947 | 280 | return fStartTime.clone(); |
146a887c FC |
281 | } |
282 | ||
09e86496 FC |
283 | /* (non-Javadoc) |
284 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime() | |
e31e01e8 | 285 | */ |
d4011df2 | 286 | @Override |
4df4581d | 287 | public ITmfTimestamp getEndTime() { |
20658947 FC |
288 | return fEndTime.clone(); |
289 | } | |
290 | ||
291 | // ------------------------------------------------------------------------ | |
292 | // Convenience setters | |
293 | // ------------------------------------------------------------------------ | |
294 | ||
295 | /** | |
296 | * Update the trace events time range | |
297 | * | |
298 | * @param range the new time range | |
299 | */ | |
300 | protected void setTimeRange(final TmfTimeRange range) { | |
301 | fStartTime = range.getStartTime().clone(); | |
302 | fEndTime = range.getEndTime().clone(); | |
303 | } | |
304 | ||
305 | /** | |
306 | * Update the trace chronologically first event timestamp | |
307 | * | |
308 | * @param startTime the new first event timestamp | |
309 | */ | |
310 | protected void setStartTime(final ITmfTimestamp startTime) { | |
311 | fStartTime = startTime.clone(); | |
312 | } | |
313 | ||
314 | /** | |
315 | * Update the trace chronologically last event timestamp | |
316 | * | |
317 | * @param endTime the new last event timestamp | |
318 | */ | |
319 | protected void setEndTime(final ITmfTimestamp endTime) { | |
320 | fEndTime = endTime.clone(); | |
321 | } | |
322 | ||
323 | /** | |
324 | * Update the trace streaming interval | |
325 | * | |
326 | * @param interval the new trace streaming interval | |
327 | */ | |
328 | protected void setStreamingInterval(final long interval) { | |
1703b536 | 329 | fStreamingInterval = (interval > 0) ? interval : 0; |
146a887c FC |
330 | } |
331 | ||
09e86496 | 332 | // ------------------------------------------------------------------------ |
7e6347b0 | 333 | // ITmfTrace - SeekEvent operations (returning a trace context) |
09e86496 FC |
334 | // ------------------------------------------------------------------------ |
335 | ||
336 | /* (non-Javadoc) | |
7e6347b0 | 337 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long) |
1b70b6dc PT |
338 | */ |
339 | @Override | |
7e6347b0 | 340 | public synchronized ITmfContext seekEvent(final long rank) { |
09e86496 | 341 | |
7e6347b0 FC |
342 | // A rank <= 0 indicates to seek the first event |
343 | if (rank <= 0) | |
344 | return seekEvent((ITmfLocation<?>) null); | |
09e86496 | 345 | |
09e86496 | 346 | // Position the trace at the checkpoint |
7e6347b0 | 347 | final ITmfContext context = fIndexer.seekIndex(rank); |
09e86496 FC |
348 | |
349 | // And locate the requested event context | |
7e6347b0 FC |
350 | long pos = context.getRank(); |
351 | if (pos < rank) { | |
352 | ITmfEvent event = readEvent(context); | |
353 | while (event != null && ++pos < rank) { | |
354 | event = readEvent(context); | |
355 | } | |
09e86496 FC |
356 | } |
357 | return context; | |
1b70b6dc PT |
358 | } |
359 | ||
09e86496 | 360 | /* (non-Javadoc) |
7e6347b0 | 361 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) |
09e86496 FC |
362 | */ |
363 | @Override | |
7e6347b0 | 364 | public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { |
09e86496 | 365 | |
7e6347b0 FC |
366 | // A null timestamp indicates to seek the first event |
367 | if (timestamp == null) | |
368 | return seekEvent(0); | |
09e86496 | 369 | |
1703b536 | 370 | // Position the trace at the checkpoint |
7e6347b0 | 371 | final ITmfContext context = fIndexer.seekIndex(timestamp); |
09e86496 FC |
372 | |
373 | // And locate the requested event context | |
7e6347b0 FC |
374 | final ITmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype... |
375 | ITmfEvent event = readEvent(nextEventContext); | |
376 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { | |
377 | context.setLocation(nextEventContext.getLocation().clone()); | |
378 | context.increaseRank(); | |
379 | event = readEvent(nextEventContext); | |
09e86496 FC |
380 | } |
381 | return context; | |
382 | } | |
383 | ||
09e86496 FC |
384 | // ------------------------------------------------------------------------ |
385 | // ITmfTrace - Read operations (returning an actual event) | |
386 | // ------------------------------------------------------------------------ | |
387 | ||
d337369a | 388 | /* (non-Javadoc) |
7e6347b0 | 389 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext) |
abfad0aa | 390 | */ |
d4011df2 | 391 | @Override |
7e6347b0 | 392 | public synchronized ITmfEvent readEvent(final ITmfContext context) { |
09e86496 | 393 | // parseEvent() does not update the context |
7e6347b0 | 394 | final ITmfEvent event = fParser.parseEvent(context); |
09e86496 | 395 | if (event != null) { |
d337369a | 396 | updateAttributes(context, event.getTimestamp()); |
09e86496 FC |
397 | context.setLocation(getCurrentLocation()); |
398 | context.increaseRank(); | |
399 | processEvent(event); | |
400 | } | |
401 | return event; | |
402 | } | |
403 | ||
404 | /** | |
d337369a | 405 | * Hook for special event processing by the concrete class |
7e6347b0 | 406 | * (called by TmfTrace.getEvent()) |
09e86496 | 407 | * |
d337369a | 408 | * @param event the event |
09e86496 FC |
409 | */ |
410 | protected void processEvent(final ITmfEvent event) { | |
d337369a | 411 | // Do nothing |
09e86496 FC |
412 | } |
413 | ||
d337369a FC |
414 | /** |
415 | * Update the trace attributes | |
416 | * | |
417 | * @param context the current trace context | |
418 | * @param rank | |
419 | * @param timestamp | |
420 | */ | |
421 | protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) { | |
09e86496 FC |
422 | if (fStartTime.compareTo(timestamp, false) > 0) { |
423 | fStartTime = timestamp; | |
424 | } | |
425 | if (fEndTime.compareTo(timestamp, false) < 0) { | |
426 | fEndTime = timestamp; | |
427 | } | |
428 | if (context.hasValidRank()) { | |
d337369a | 429 | long rank = context.getRank(); |
09e86496 FC |
430 | if (fNbEvents <= rank) { |
431 | fNbEvents = rank + 1; | |
432 | } | |
d337369a | 433 | fIndexer.updateIndex(context, timestamp); |
09e86496 | 434 | } |
abfad0aa FC |
435 | } |
436 | ||
3791b5df | 437 | // ------------------------------------------------------------------------ |
d337369a | 438 | // TmfDataProvider |
3791b5df FC |
439 | // ------------------------------------------------------------------------ |
440 | ||
d337369a FC |
441 | /* (non-Javadoc) |
442 | * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest) | |
443 | */ | |
3791b5df | 444 | @Override |
085d898f | 445 | public ITmfContext armRequest(final ITmfDataRequest<T> request) { |
3791b5df | 446 | if (request instanceof ITmfEventRequest<?> |
25e48683 FC |
447 | && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest<T>) request).getRange().getStartTime()) |
448 | && request.getIndex() == 0) { | |
085d898f | 449 | final ITmfContext context = seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime()); |
3791b5df FC |
450 | ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank()); |
451 | return context; | |
452 | ||
453 | } | |
454 | return seekEvent(request.getIndex()); | |
455 | } | |
456 | ||
d337369a FC |
457 | /* (non-Javadoc) |
458 | * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext) | |
3791b5df | 459 | */ |
3791b5df | 460 | @Override |
20658947 | 461 | @SuppressWarnings("unchecked") |
085d898f FC |
462 | public T getNext(final ITmfContext context) { |
463 | if (context instanceof TmfContext) | |
7e6347b0 | 464 | return (T) readEvent(context); |
3791b5df FC |
465 | return null; |
466 | } | |
467 | ||
09e86496 | 468 | |
3791b5df | 469 | // ------------------------------------------------------------------------ |
09e86496 | 470 | // toString |
3791b5df FC |
471 | // ------------------------------------------------------------------------ |
472 | ||
d337369a FC |
473 | /* (non-Javadoc) |
474 | * @see java.lang.Object#toString() | |
475 | */ | |
12c155f5 | 476 | @Override |
09e86496 FC |
477 | @SuppressWarnings("nls") |
478 | public String toString() { | |
20658947 FC |
479 | return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize |
480 | + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime | |
481 | + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]"; | |
12c155f5 FC |
482 | } |
483 | ||
8c8bf09f | 484 | } |