[Tmf][Ctf] Add descriptive fail to import messages.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
CommitLineData
b1baa808 1/*******************************************************************************
ea271da6 2 * Copyright (c) 2012, 2013 Ericsson
b1baa808
MK
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 *
ea271da6
PT
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
b1baa808
MK
12 *******************************************************************************/
13
a3fc8213
AM
14package org.eclipse.linuxtools.tmf.core.ctfadaptor;
15
a3fc8213
AM
16import org.eclipse.core.resources.IProject;
17import org.eclipse.core.resources.IResource;
a94410d9
MK
18import org.eclipse.core.runtime.IStatus;
19import org.eclipse.core.runtime.Status;
a3fc8213
AM
20import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
a94410d9 22import org.eclipse.linuxtools.internal.tmf.core.Activator;
6256d8ad 23import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
b4f71e4a 24import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
3bd46eef
AM
25import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
26import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
a3fc8213 27import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
4b7c469f 28import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
a3fc8213 29import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
4b7c469f 30import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
a3fc8213 31
9ac2eb62 32/**
d09f973b
FC
33 * The CTf trace handler
34 *
35 * @version 1.0
36 * @author Matthew khouzam
9ac2eb62 37 */
1e1bef82 38public class CtfTmfTrace extends TmfTrace implements ITmfEventParser {
a3fc8213 39
a94410d9
MK
40 // -------------------------------------------
41 // Constants
42 // -------------------------------------------
324a6a4a
BH
43 /**
44 * Default cache size for CTF traces
45 */
46 protected static final int DEFAULT_CACHE_SIZE = 50000;
64c2cb4c 47
a94410d9
MK
48 // -------------------------------------------
49 // Fields
50 // -------------------------------------------
a3fc8213 51
4b7c469f
MK
52 /* Reference to the CTF Trace */
53 private CTFTrace fTrace;
a3fc8213 54
a94410d9
MK
55 // -------------------------------------------
56 // TmfTrace Overrides
57 // -------------------------------------------
b1baa808
MK
58 /**
59 * Method initTrace.
063f0d27
AM
60 *
61 * @param resource
62 * The resource associated with this trace
63 * @param path
64 * The path to the trace file
65 * @param eventType
66 * The type of events that will be read from this trace
b1baa808 67 * @throws TmfTraceException
063f0d27 68 * If something when wrong while reading the trace
b1baa808 69 */
a3fc8213 70 @Override
6256d8ad 71 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
b4f71e4a 72 throws TmfTraceException {
4a110860
AM
73 /*
74 * Set the cache size. This has to be done before the call to super()
75 * because the super needs to know the cache size.
76 */
77 setCacheSize();
324a6a4a 78
32c16b50
GB
79 super.initTrace(resource, path, eventType);
80
e30ce12e
AM
81 @SuppressWarnings("unused")
82 CtfTmfEventType type;
83
a3fc8213
AM
84 try {
85 this.fTrace = new CTFTrace(path);
53b235e1 86 CtfIteratorManager.addTrace(this);
81a2d02e 87 CtfTmfContext ctx;
99b483fe 88 /* Set the start and (current) end times for this trace */
81a2d02e 89 ctx = (CtfTmfContext) seekEvent(0L);
132a02b0 90 CtfTmfEvent event = getNext(ctx);
a94410d9 91 if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
99b483fe
AM
92 /* Handle the case where the trace is empty */
93 this.setStartTime(TmfTimestamp.BIG_BANG);
94 } else {
132a02b0 95 final ITmfTimestamp curTime = event.getTimestamp();
21fb02fa
MK
96 this.setStartTime(curTime);
97 this.setEndTime(curTime);
99b483fe
AM
98 }
99
25e48683 100 } catch (final CTFReaderException e) {
a3fc8213
AM
101 /*
102 * If it failed at the init(), we can assume it's because the file
103 * was not found or was not recognized as a CTF trace. Throw into
104 * the new type of exception expected by the rest of TMF.
105 */
9fa32496 106 throw new TmfTraceException(e.getMessage(), e);
a3fc8213 107 }
a3fc8213
AM
108 }
109
a94410d9
MK
110 /*
111 * (non-Javadoc)
112 *
53b235e1
MK
113 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
114 */
115 @Override
116 public synchronized void dispose() {
117 CtfIteratorManager.removeTrace(this);
5d1c6919
PT
118 if (fTrace != null) {
119 fTrace.dispose();
120 fTrace = null;
121 }
53b235e1
MK
122 super.dispose();
123 }
124
b1baa808
MK
125 /**
126 * Method validate.
a94410d9
MK
127 *
128 * @param project
129 * IProject
130 * @param path
131 * String
132 * @return IStatus IStatus.error or Status.OK_STATUS
b1baa808 133 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
a94410d9 134 * @since 2.0
b1baa808 135 */
a3fc8213 136 @Override
a94410d9
MK
137 public IStatus validate(final IProject project, final String path) {
138 IStatus validTrace = Status.OK_STATUS;
a3fc8213
AM
139 try {
140 final CTFTrace temp = new CTFTrace(path);
81fe3479
PT
141 boolean valid = temp.majortIsSet(); // random test
142 temp.dispose();
a94410d9
MK
143 if (!valid) {
144 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
145 }
25e48683 146 } catch (final CTFReaderException e) {
a94410d9 147 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError +": " + e.toString()); //$NON-NLS-1$
a3fc8213 148 }
a94410d9 149 return validTrace;
a3fc8213
AM
150 }
151
b1baa808 152 /**
f474d36b 153 * Method getCurrentLocation. This is not applicable in CTF
a94410d9 154 *
f474d36b 155 * @return null, since the trace has no knowledge of the current location
b1baa808
MK
156 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
157 */
a3fc8213 158 @Override
1e1bef82 159 public ITmfLocation getCurrentLocation() {
f474d36b 160 return null;
a3fc8213
AM
161 }
162
a3fc8213 163 @Override
1e1bef82 164 public double getLocationRatio(ITmfLocation location) {
4b7c469f 165 final CtfLocation curLocation = (CtfLocation) location;
81a2d02e 166 final CtfTmfContext context = new CtfTmfContext(this);
53b235e1 167 context.setLocation(curLocation);
5976d44a 168 context.seek(curLocation.getLocationInfo());
a94410d9 169 final CtfLocationInfo currentTime = ((CtfLocationInfo) context.getLocation().getLocationInfo());
53b235e1
MK
170 final long startTime = getIterator(this, context).getStartTime();
171 final long endTime = getIterator(this, context).getEndTime();
132a02b0 172 return ((double) currentTime.getTimestamp() - startTime)
53b235e1 173 / (endTime - startTime);
a3fc8213
AM
174 }
175
b1baa808
MK
176 /**
177 * Method seekEvent.
a94410d9
MK
178 *
179 * @param location
180 * ITmfLocation<?>
b1baa808 181 * @return ITmfContext
b1baa808 182 */
a3fc8213 183 @Override
76643eb7 184 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
ce2388e0 185 CtfLocation currentLocation = (CtfLocation) location;
81a2d02e 186 CtfTmfContext context = new CtfTmfContext(this);
76643eb7
BH
187 if (fTrace == null) {
188 context.setLocation(null);
189 context.setRank(ITmfContext.UNKNOWN_RANK);
190 return context;
191 }
4a110860
AM
192 /*
193 * The rank is set to 0 if the iterator seeks the beginning. If not, it
194 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
195 * by rank for now.
196 */
11d6f468 197 if (currentLocation == null) {
f5df94f8 198 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
4a110860 199 context.setRank(0);
11d6f468 200 }
5976d44a 201 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
d62bb185 202 currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L);
1191a574 203 }
f474d36b 204 context.setLocation(currentLocation);
7f0bab07
PT
205 if (location == null) {
206 CtfTmfEvent event = getIterator(this, context).getCurrentEvent();
207 if (event != null) {
d62bb185 208 currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0);
7f0bab07
PT
209 }
210 }
a94410d9 211 if (context.getRank() != 0) {
3bd44ac8 212 context.setRank(ITmfContext.UNKNOWN_RANK);
64c2cb4c 213 }
f474d36b 214 return context;
a3fc8213
AM
215 }
216
a3fc8213 217 @Override
76643eb7 218 public synchronized ITmfContext seekEvent(double ratio) {
81a2d02e 219 CtfTmfContext context = new CtfTmfContext(this);
76643eb7
BH
220 if (fTrace == null) {
221 context.setLocation(null);
222 context.setRank(ITmfContext.UNKNOWN_RANK);
223 return context;
224 }
b2dc9e02
MK
225 final long end = this.getEndTime().getValue();
226 final long start = this.getStartTime().getValue();
227 final long diff = end - start;
15e89960 228 final long ratioTs = Math.round(diff * ratio) + start;
b2dc9e02 229 context.seek(ratioTs);
f474d36b
PT
230 context.setRank(ITmfContext.UNKNOWN_RANK);
231 return context;
a3fc8213
AM
232 }
233
b1baa808
MK
234 /**
235 * Method readNextEvent.
a94410d9
MK
236 *
237 * @param context
238 * ITmfContext
b1baa808 239 * @return CtfTmfEvent
c32744d6 240 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
b1baa808 241 */
a3fc8213 242 @Override
4b7c469f 243 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
faa38350
PT
244 if (fTrace == null) {
245 return null;
246 }
f474d36b 247 CtfTmfEvent event = null;
81a2d02e 248 if (context instanceof CtfTmfContext) {
575beffc 249 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
ae09313d
PT
250 return null;
251 }
81a2d02e 252 CtfTmfContext ctfContext = (CtfTmfContext) context;
788ddcbc 253 event = ctfContext.getCurrentEvent();
4a110860 254
324a6a4a
BH
255 if (event != null) {
256 updateAttributes(context, event.getTimestamp());
788ddcbc
MK
257 ctfContext.advance();
258 ctfContext.increaseRank();
324a6a4a 259 }
f474d36b 260 }
4a110860 261
aa572e22 262 return event;
a3fc8213
AM
263 }
264
4b7c469f
MK
265 /**
266 * gets the CTFtrace that this is wrapping
a94410d9 267 *
4b7c469f
MK
268 * @return the CTF trace
269 */
270 public CTFTrace getCTFTrace() {
a3fc8213
AM
271 return fTrace;
272 }
a1a24d68 273
a94410d9
MK
274 // -------------------------------------------
275 // Environment Parameters
276 // -------------------------------------------
d26f90fd 277 /**
4b7c469f
MK
278 * Method getNbEnvVars.
279 *
280 * @return int
d26f90fd 281 */
4b7c469f
MK
282 public int getNbEnvVars() {
283 return this.fTrace.getEnvironment().size();
284 }
285
286 /**
287 * Method getEnvNames.
288 *
289 * @return String[]
290 */
291 public String[] getEnvNames() {
292 final String[] s = new String[getNbEnvVars()];
293 return this.fTrace.getEnvironment().keySet().toArray(s);
294 }
295
296 /**
297 * Method getEnvValue.
298 *
299 * @param key
300 * String
301 * @return String
302 */
303 public String getEnvValue(final String key) {
304 return this.fTrace.getEnvironment().get(key);
305 }
306
a94410d9
MK
307 // -------------------------------------------
308 // Clocks
309 // -------------------------------------------
bfe038ff 310
9ac2eb62
MK
311 /**
312 * gets the clock offset
a94410d9 313 *
9ac2eb62
MK
314 * @return the clock offset in ns
315 */
a94410d9
MK
316 public long getOffset() {
317 if (fTrace != null) {
bfe038ff
MK
318 return fTrace.getOffset();
319 }
320 return 0;
321 }
322
a94410d9
MK
323 // -------------------------------------------
324 // Parser
325 // -------------------------------------------
4b7c469f
MK
326
327 @Override
bfe038ff 328 public CtfTmfEvent parseEvent(ITmfContext context) {
4b7c469f 329 CtfTmfEvent event = null;
ea271da6
PT
330 if (context instanceof CtfTmfContext) {
331 final ITmfContext tmpContext = seekEvent(context.getLocation());
332 event = getNext(tmpContext);
4b7c469f
MK
333 }
334 return event;
11d6f468 335 }
64c2cb4c 336
324a6a4a 337 /**
64c2cb4c 338 * Sets the cache size for a CtfTmfTrace.
324a6a4a
BH
339 */
340 protected void setCacheSize() {
341 setCacheSize(DEFAULT_CACHE_SIZE);
342 }
ce2388e0 343
a94410d9
MK
344 // -------------------------------------------
345 // Helpers
346 // -------------------------------------------
53b235e1 347
a94410d9 348 private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfContext context) {
53b235e1
MK
349 return CtfIteratorManager.getIterator(trace, context);
350 }
36dd544c
MK
351
352 /**
353 * Get an iterator to the trace
354 *
355 * @return an iterator to the trace
ed59ab27 356 * @since 2.0
36dd544c 357 */
a94410d9 358 public CtfIterator createIterator() {
36dd544c
MK
359 return new CtfIterator(this);
360 }
a3fc8213 361}
This page took 0.05729 seconds and 5 git commands to generate.