1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*******************************************************************************
* Copyright (c) 2009, 2013 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Francois Chouinard - Initial API and implementation
* Patrick Tasse - Updated for removal of context clone
*******************************************************************************/
package org.eclipse.linuxtools.tmf.tests.stubs.trace;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.linuxtools.internal.tmf.core.Activator;
import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
/**
* <b><u>TmfTraceStub</u></b>
* <p>
* Dummy test trace. Use in conjunction with TmfEventParserStub.
*/
public class TmfTraceStub extends TmfTrace implements ITmfEventParser, ITmfPersistentlyIndexable {
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
// The actual stream
private RandomAccessFile fTrace;
// // The associated event parser
// private ITmfEventParser<TmfEvent> fParser;
// The synchronization lock
private final ReentrantLock fLock = new ReentrantLock();
private ITmfTimestamp fInitialRangeOffset = null;
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
/**
* Default constructor
*/
public TmfTraceStub() {
super();
setParser(new TmfEventParserStub(this));
}
/**
* Constructor with which you can specify a custom streaming interval. The
* parser and indexer won't be specified.
*
* @param path
* The path to the trace file
* @param cacheSize
* The cache size
* @param interval
* The trace streaming interval
* @throws TmfTraceException
* If an error occurred opening the trace
*/
public TmfTraceStub(final String path,
final int cacheSize,
final long interval) throws TmfTraceException {
super(null, ITmfEvent.class, path, cacheSize, interval, null, null);
setupTrace(path);
setParser(new TmfEventParserStub(this));
}
/**
* Constructor to specify the parser and indexer. The streaming interval
* will be 0.
*
* @param path
* The path to the trace file
* @param cacheSize
* The cache size
* @param waitForCompletion
* Do we block the caller until the trace is indexed, or not.
* @param indexer
* The trace indexer to use
* @param parser
* The trace parser. If left 'null', it will use a
* {@link TmfEventParserStub}.
* @throws TmfTraceException
* If an error occurred opening the trace
*/
public TmfTraceStub(final String path,
final int cacheSize,
final boolean waitForCompletion,
final ITmfTraceIndexer indexer,
final ITmfEventParser parser) throws TmfTraceException {
super(null, ITmfEvent.class, path, cacheSize, 0, indexer, null);
setupTrace(path);
setParser((parser != null) ? parser : new TmfEventParserStub(this));
if (waitForCompletion) {
indexTrace(true);
}
}
/**
* Copy constructor
*
* @param trace
* The trace to copy
* @throws TmfTraceException
* If an error occurred opening the trace
*/
public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException {
super(trace);
setupTrace(getPath()); // fPath will be set by the super-constructor
setParser(new TmfEventParserStub(this));
}
private void setupTrace(String path) throws TmfTraceException {
try {
fTrace = new RandomAccessFile(path, "r"); //$NON-NLS-1$
} catch (FileNotFoundException e) {
throw new TmfTraceException(e.getMessage());
}
}
// ------------------------------------------------------------------------
// Initializers
// ------------------------------------------------------------------------
@Override
public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
try {
fTrace = new RandomAccessFile(path, "r"); //$NON-NLS-1$
} catch (FileNotFoundException e) {
throw new TmfTraceException(e.getMessage());
}
setParser(new TmfEventParserStub(this));
super.initTrace(resource, path, type);
}
@Override
public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
super.initialize(resource, path, type);
}
// ------------------------------------------------------------------------
// Accessors
// ------------------------------------------------------------------------
/**
* @return The file stream to the trace
*/
public RandomAccessFile getStream() {
return fTrace;
}
/**
* Set the initial range offset.
*
* @param initOffset
* The new initial range offset
*/
public void setInitialRangeOffset(ITmfTimestamp initOffset) {
fInitialRangeOffset = initOffset;
}
@Override
public ITmfTimestamp getInitialRangeOffset() {
if (fInitialRangeOffset != null) {
return fInitialRangeOffset;
}
return super.getInitialRangeOffset();
}
// ------------------------------------------------------------------------
// Operators
// ------------------------------------------------------------------------
@Override
public TmfContext seekEvent(final ITmfLocation location) {
try {
fLock.lock();
try {
if (fTrace != null) {
// Position the trace at the requested location and
// returns the corresponding context
long loc = 0;
long rank = 0;
if (location != null) {
loc = (Long) location.getLocationInfo();
rank = ITmfContext.UNKNOWN_RANK;
}
if (loc != fTrace.getFilePointer()) {
fTrace.seek(loc);
}
final TmfContext context = new TmfContext(getCurrentLocation(), rank);
return context;
}
} catch (final IOException e) {
e.printStackTrace();
} catch (final NullPointerException e) {
e.printStackTrace();
}
finally{
fLock.unlock();
}
} catch (final NullPointerException e) {
e.printStackTrace();
}
return null;
}
@Override
public TmfContext seekEvent(final double ratio) {
fLock.lock();
try {
if (fTrace != null) {
final ITmfLocation location = new TmfLongLocation(Long.valueOf(Math.round(ratio * fTrace.length())));
final TmfContext context = seekEvent(location);
context.setRank(ITmfContext.UNKNOWN_RANK);
return context;
}
} catch (final IOException e) {
e.printStackTrace();
} finally {
fLock.unlock();
}
return null;
}
@Override
public double getLocationRatio(ITmfLocation location) {
fLock.lock();
try {
if (fTrace != null) {
if (location.getLocationInfo() instanceof Long) {
return (double) ((Long) location.getLocationInfo()) / fTrace.length();
}
}
} catch (final IOException e) {
e.printStackTrace();
} finally {
fLock.unlock();
}
return 0;
}
@Override
public ITmfLocation getCurrentLocation() {
fLock.lock();
try {
if (fTrace != null) {
return new TmfLongLocation(fTrace.getFilePointer());
}
} catch (final IOException e) {
e.printStackTrace();
} finally {
fLock.unlock();
}
return null;
}
@Override
public ITmfEvent parseEvent(final ITmfContext context) {
fLock.lock();
try {
// parseNextEvent will update the context
if (fTrace != null && getParser() != null && context != null) {
final ITmfEvent event = getParser().parseEvent(context);
return event;
}
} finally {
fLock.unlock();
}
return null;
}
@Override
public synchronized void setNbEvents(final long nbEvents) {
super.setNbEvents(nbEvents);
}
@Override
public void setTimeRange(final TmfTimeRange range) {
super.setTimeRange(range);
}
@Override
public void setStartTime(final ITmfTimestamp startTime) {
super.setStartTime(startTime);
}
@Override
public void setEndTime(final ITmfTimestamp endTime) {
super.setEndTime(endTime);
}
@Override
public void setStreamingInterval(final long interval) {
super.setStreamingInterval(interval);
}
@Override
public synchronized void dispose() {
fLock.lock();
try {
if (fTrace != null) {
fTrace.close();
fTrace = null;
}
} catch (final IOException e) {
// Ignore
} finally {
fLock.unlock();
}
super.dispose();
}
@Override
public IStatus validate(IProject project, String path) {
if (fileExists(path)) {
return Status.OK_STATUS;
}
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "File does not exist: " + path);
}
private static int fCheckpointSize = -1;
@Override
public synchronized int getCheckpointSize() {
if (fCheckpointSize == -1) {
TmfCheckpoint c = new TmfCheckpoint(new TmfTimestamp(0L), new TmfLongLocation(0L), 0);
ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
b.clear();
c.serialize(b);
fCheckpointSize = b.position();
}
return fCheckpointSize;
}
@Override
public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
return new TmfLongLocation(bufferIn);
}
}
|