Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
165c977c | 2 | * Copyright (c) 2009 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 | ||
62d1696a | 15 | import java.io.FileNotFoundException; |
62d1696a | 16 | import java.util.Collections; |
8c8bf09f ASL |
17 | import java.util.Vector; |
18 | ||
62d1696a FC |
19 | import org.eclipse.core.runtime.IProgressMonitor; |
20 | import org.eclipse.core.runtime.IStatus; | |
21 | import org.eclipse.core.runtime.Status; | |
22 | import org.eclipse.core.runtime.jobs.Job; | |
8c8bf09f ASL |
23 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
24 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
25 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
50adc88e | 26 | import org.eclipse.linuxtools.tmf.signal.TmfSignalManager; |
8c8bf09f ASL |
27 | |
28 | /** | |
146a887c | 29 | * <b><u>TmfTrace</u></b> |
8c8bf09f | 30 | * <p> |
146a887c FC |
31 | * Abstract implementation of ITmfTrace. It should be sufficient to extend this |
32 | * class and provide implementation for <code>getCurrentLocation()</code> and | |
33 | * <code>seekLocation()</code>, as well as a proper parser, to have a working | |
34 | * concrete imlpementation. | |
8c8bf09f | 35 | */ |
62d1696a FC |
36 | public abstract class TmfTrace implements ITmfTrace { |
37 | ||
38 | // ======================================================================== | |
39 | // Constants | |
40 | // ======================================================================== | |
41 | ||
42 | // The default number of events to cache | |
146a887c | 43 | public static final int DEFAULT_PAGE_SIZE = 1000; |
8c8bf09f | 44 | |
165c977c | 45 | // ======================================================================== |
8c8bf09f | 46 | // Attributes |
165c977c | 47 | // ======================================================================== |
8c8bf09f | 48 | |
62d1696a FC |
49 | // The stream name |
50 | private final String fName; | |
51 | ||
146a887c FC |
52 | // The checkpoints page size |
53 | private final int fPageSize; | |
62d1696a | 54 | |
52e28818 FC |
55 | // The checkpoints page size |
56 | private final boolean fIndex; | |
57 | ||
62d1696a | 58 | // The set of event stream checkpoints (for random access) |
146a887c | 59 | protected Vector<TmfTraceCheckpoint> fCheckpoints = new Vector<TmfTraceCheckpoint>(); |
62d1696a FC |
60 | |
61 | // The number of events collected | |
62 | private int fNbEvents = 0; | |
63 | ||
64 | // The time span of the event stream | |
65 | private TmfTimeRange fTimeRange = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigBang); | |
66 | ||
165c977c | 67 | // ======================================================================== |
50adc88e | 68 | // Constructors |
165c977c | 69 | // ======================================================================== |
8c8bf09f | 70 | |
62d1696a | 71 | /** |
146a887c | 72 | * @param name |
52e28818 FC |
73 | * @param pageSize |
74 | * @param index | |
62d1696a FC |
75 | * @throws FileNotFoundException |
76 | */ | |
52e28818 | 77 | protected TmfTrace(String name, int pageSize, boolean index) throws FileNotFoundException { |
146a887c | 78 | fName = name; |
52e28818 FC |
79 | fPageSize = pageSize; |
80 | fIndex = index; | |
81 | } | |
82 | ||
83 | /** | |
84 | * @param name | |
85 | * @param cacheSize | |
86 | * @throws FileNotFoundException | |
87 | */ | |
88 | protected TmfTrace(String name, boolean index) throws FileNotFoundException { | |
89 | this(name, DEFAULT_PAGE_SIZE, index); | |
90 | } | |
91 | ||
92 | /** | |
93 | * @param name | |
94 | * @param cacheSize | |
95 | * @throws FileNotFoundException | |
96 | */ | |
97 | protected TmfTrace(String name, int pageSize) throws FileNotFoundException { | |
98 | this(name, pageSize, false); | |
8c8bf09f ASL |
99 | } |
100 | ||
62d1696a | 101 | /** |
146a887c | 102 | * @param name |
62d1696a FC |
103 | * @throws FileNotFoundException |
104 | */ | |
146a887c | 105 | protected TmfTrace(String name) throws FileNotFoundException { |
52e28818 | 106 | this(name, DEFAULT_PAGE_SIZE, false); |
8c8bf09f ASL |
107 | } |
108 | ||
165c977c | 109 | // ======================================================================== |
8c8bf09f | 110 | // Accessors |
165c977c | 111 | // ======================================================================== |
8c8bf09f | 112 | |
62d1696a | 113 | /** |
146a887c | 114 | * @return the size of the cache |
62d1696a | 115 | */ |
146a887c FC |
116 | public int getPageSize() { |
117 | return fPageSize; | |
8c8bf09f ASL |
118 | } |
119 | ||
62d1696a | 120 | /** |
146a887c | 121 | * @return the trace name |
62d1696a FC |
122 | */ |
123 | public String getName() { | |
124 | return fName; | |
8c8bf09f ASL |
125 | } |
126 | ||
62d1696a FC |
127 | /* (non-Javadoc) |
128 | * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents() | |
129 | */ | |
130 | public int getNbEvents() { | |
131 | return fNbEvents; | |
8c8bf09f ASL |
132 | } |
133 | ||
62d1696a FC |
134 | /* (non-Javadoc) |
135 | * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange() | |
136 | */ | |
8c8bf09f | 137 | public TmfTimeRange getTimeRange() { |
62d1696a | 138 | return fTimeRange; |
8c8bf09f ASL |
139 | } |
140 | ||
146a887c FC |
141 | public TmfTimestamp getStartTime() { |
142 | return fTimeRange.getStartTime(); | |
143 | } | |
144 | ||
145 | public TmfTimestamp getEndTime() { | |
146 | return fTimeRange.getEndTime(); | |
147 | } | |
148 | ||
149 | public void setTimeRange(TmfTimeRange range) { | |
150 | fTimeRange = range; | |
151 | } | |
152 | ||
153 | public void setStartTime(TmfTimestamp startTime) { | |
154 | fTimeRange = new TmfTimeRange(startTime, fTimeRange.getEndTime()); | |
155 | } | |
156 | ||
157 | public void setEndTime(TmfTimestamp endTime) { | |
158 | fTimeRange = new TmfTimeRange(fTimeRange.getStartTime(), endTime); | |
159 | } | |
160 | ||
62d1696a FC |
161 | /* (non-Javadoc) |
162 | * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getIndex(org.eclipse.linuxtools.tmf.event.TmfTimestamp) | |
163 | */ | |
164 | public int getIndex(TmfTimestamp timestamp) { | |
146a887c | 165 | TmfTraceContext context = seekEvent(timestamp); |
62d1696a | 166 | return context.index; |
8c8bf09f ASL |
167 | } |
168 | ||
62d1696a FC |
169 | /* (non-Javadoc) |
170 | * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimestamp(int) | |
171 | */ | |
172 | public TmfTimestamp getTimestamp(int index) { | |
146a887c | 173 | TmfTraceContext context = seekEvent(index); |
62d1696a FC |
174 | TmfEvent event = peekEvent(context); |
175 | return event.getTimestamp(); | |
82b08e62 FC |
176 | } |
177 | ||
165c977c | 178 | // ======================================================================== |
8c8bf09f | 179 | // Operators |
165c977c | 180 | // ======================================================================== |
8c8bf09f | 181 | |
146a887c FC |
182 | /* (non-Javadoc) |
183 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp) | |
184 | */ | |
185 | public TmfTraceContext seekEvent(TmfTimestamp timestamp) { | |
62d1696a FC |
186 | |
187 | // First, find the right checkpoint | |
146a887c | 188 | int index = Collections.binarySearch(fCheckpoints, new TmfTraceCheckpoint(timestamp, 0)); |
62d1696a FC |
189 | |
190 | // In the very likely event that the checkpoint was not found, bsearch | |
191 | // returns its negated would-be location (not an offset...). From that | |
192 | // index, we can then position the stream and get the event. | |
193 | if (index < 0) { | |
194 | index = Math.max(0, -(index + 2)); | |
195 | } | |
196 | ||
197 | // Position the stream at the checkpoint | |
198 | Object location = (index < fCheckpoints.size()) ? fCheckpoints.elementAt(index).getLocation() : null; | |
146a887c | 199 | TmfTraceContext nextEventContext; |
62d1696a FC |
200 | synchronized(this) { |
201 | nextEventContext = seekLocation(location); | |
202 | } | |
146a887c | 203 | TmfTraceContext currentEventContext = new TmfTraceContext(nextEventContext.location, index * fPageSize); |
62d1696a FC |
204 | |
205 | // And get the event | |
206 | TmfEvent event = getNextEvent(nextEventContext); | |
207 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { | |
208 | currentEventContext.location = nextEventContext.location; | |
209 | currentEventContext.index++; | |
210 | event = getNextEvent(nextEventContext); | |
211 | } | |
212 | ||
213 | return currentEventContext; | |
214 | } | |
215 | ||
146a887c FC |
216 | /* (non-Javadoc) |
217 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int) | |
218 | */ | |
219 | public TmfTraceContext seekEvent(int position) { | |
62d1696a FC |
220 | |
221 | // Position the stream at the previous checkpoint | |
146a887c | 222 | int index = position / fPageSize; |
62d1696a | 223 | Object location = (index < fCheckpoints.size()) ? fCheckpoints.elementAt(index).getLocation() : null; |
146a887c | 224 | TmfTraceContext nextEventContext; |
62d1696a FC |
225 | synchronized(this) { |
226 | nextEventContext = seekLocation(location); | |
227 | } | |
146a887c | 228 | TmfTraceContext currentEventContext = new TmfTraceContext(nextEventContext); |
62d1696a FC |
229 | |
230 | // And locate the event (if it exists) | |
146a887c | 231 | int current = index * fPageSize; |
62d1696a FC |
232 | TmfEvent event = getNextEvent(nextEventContext); |
233 | while (event != null && current < position) { | |
234 | currentEventContext.location = nextEventContext.location; | |
235 | event = getNextEvent(nextEventContext); | |
236 | current++; | |
165c977c | 237 | } |
62d1696a FC |
238 | |
239 | return currentEventContext; | |
8c8bf09f ASL |
240 | } |
241 | ||
146a887c FC |
242 | /* (non-Javadoc) |
243 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#peekEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext) | |
244 | */ | |
245 | public TmfEvent peekEvent(TmfTraceContext context) { | |
246 | TmfTraceContext ctx = new TmfTraceContext(context); | |
62d1696a FC |
247 | return getNextEvent(ctx); |
248 | } | |
82b08e62 | 249 | |
146a887c FC |
250 | /* (non-Javadoc) |
251 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext, org.eclipse.linuxtools.tmf.event.TmfTimestamp) | |
252 | */ | |
253 | public TmfEvent getEvent(TmfTraceContext context, TmfTimestamp timestamp) { | |
254 | TmfTraceContext ctx = seekEvent(timestamp); | |
62d1696a | 255 | context.location = ctx.location; |
146a887c | 256 | return getNextEvent(context); |
82b08e62 | 257 | } |
8c8bf09f | 258 | |
146a887c FC |
259 | /* (non-Javadoc) |
260 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext, int) | |
261 | */ | |
262 | public TmfEvent getEvent(TmfTraceContext context, int position) { | |
263 | TmfTraceContext ctx = seekEvent(position); | |
62d1696a | 264 | context.location = ctx.location; |
146a887c | 265 | return getNextEvent(context); |
62d1696a | 266 | } |
165c977c | 267 | |
146a887c FC |
268 | /* (non-Javadoc) |
269 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext) | |
270 | */ | |
271 | public synchronized TmfEvent getNextEvent(TmfTraceContext context) { | |
272 | seekLocation(context.location); | |
273 | TmfEvent event = parseNextEvent(); | |
274 | processEvent(event); | |
275 | context.location = getCurrentLocation(); | |
276 | return event; | |
277 | } | |
8c8bf09f | 278 | |
146a887c FC |
279 | /** |
280 | * Hook for "special" processing by the extending class | |
281 | * @param event | |
282 | */ | |
283 | public void processEvent(TmfEvent event) { | |
284 | // Do nothing by default | |
62d1696a | 285 | } |
146a887c FC |
286 | |
287 | // ======================================================================== | |
288 | // Stream indexing. Essentially, parse the file asynchronously and build | |
289 | // the checkpoints index. This index is used to quickly find an event based | |
290 | // on a timestamp or an index. | |
291 | // ======================================================================== | |
292 | ||
293 | public void indexStream() { | |
62d1696a FC |
294 | IndexingJob job = new IndexingJob(fName); |
295 | job.schedule(); | |
52e28818 FC |
296 | if (fIndex) { |
297 | try { | |
298 | job.join(); | |
299 | } catch (InterruptedException e) { | |
300 | // TODO Auto-generated catch block | |
301 | e.printStackTrace(); | |
302 | } | |
303 | } | |
62d1696a FC |
304 | } |
305 | ||
306 | private class IndexingJob extends Job { | |
307 | ||
308 | public IndexingJob(String name) { | |
309 | super(name); | |
310 | } | |
311 | ||
312 | /* (non-Javadoc) | |
313 | * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) | |
314 | */ | |
315 | @Override | |
316 | protected IStatus run(IProgressMonitor monitor) { | |
317 | ||
318 | int nbEvents = 0; | |
319 | TmfTimestamp startTime = new TmfTimestamp(); | |
320 | TmfTimestamp lastTime = new TmfTimestamp(); | |
321 | TmfTimestamp rangeStartTime = new TmfTimestamp(); | |
322 | ||
323 | monitor.beginTask("Indexing " + fName, IProgressMonitor.UNKNOWN); | |
324 | ||
325 | try { | |
146a887c | 326 | TmfTraceContext nextEventContext; |
62d1696a FC |
327 | synchronized(this) { |
328 | nextEventContext = seekLocation(null); | |
329 | } | |
146a887c | 330 | TmfTraceContext currentEventContext = new TmfTraceContext(nextEventContext); |
62d1696a FC |
331 | TmfEvent event = getNextEvent(nextEventContext); |
332 | if (event != null) { | |
333 | startTime = event.getTimestamp(); | |
334 | lastTime = event.getTimestamp(); | |
165c977c FC |
335 | } |
336 | ||
62d1696a FC |
337 | rangeStartTime = startTime; |
338 | while (event != null) { | |
339 | lastTime = event.getTimestamp(); | |
146a887c FC |
340 | if ((nbEvents++ % fPageSize) == 0) { |
341 | TmfTraceCheckpoint bookmark = new TmfTraceCheckpoint(lastTime, currentEventContext.location); | |
62d1696a FC |
342 | synchronized(this) { |
343 | fCheckpoints.add(bookmark); | |
344 | fNbEvents = nbEvents; | |
345 | fTimeRange = new TmfTimeRange(startTime, lastTime); | |
346 | } | |
347 | notifyListeners(new TmfTimeRange(rangeStartTime, lastTime)); | |
348 | monitor.worked(1); | |
349 | // Check monitor *after* fCheckpoints has been updated | |
350 | if (monitor.isCanceled()) { | |
351 | return Status.CANCEL_STATUS; | |
352 | } | |
165c977c | 353 | } |
165c977c | 354 | |
146a887c FC |
355 | // Do whatever |
356 | processEvent(event); | |
357 | ||
358 | currentEventContext.location = nextEventContext.location; | |
62d1696a FC |
359 | event = getNextEvent(nextEventContext); |
360 | } | |
361 | } | |
362 | finally { | |
363 | synchronized(this) { | |
364 | fNbEvents = nbEvents; | |
365 | fTimeRange = new TmfTimeRange(startTime, lastTime); | |
366 | } | |
367 | notifyListeners(new TmfTimeRange(rangeStartTime, lastTime)); | |
368 | monitor.done(); | |
8c8bf09f | 369 | } |
62d1696a FC |
370 | |
371 | return Status.OK_STATUS; | |
372 | } | |
8c8bf09f ASL |
373 | } |
374 | ||
146a887c FC |
375 | private void notifyListeners(TmfTimeRange range) { |
376 | TmfSignalManager.dispatchSignal(new TmfTraceUpdatedSignal(this, this, range)); | |
377 | } | |
378 | ||
8c8bf09f | 379 | } |