Refactor TmfTrace and dependencies - remove getTrace()
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / internal / lttng / core / trace / LTTngTextTrace.java
CommitLineData
5d10d135
ASL
1/*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12
5945cec9 13package org.eclipse.linuxtools.internal.lttng.core.trace;
5d10d135
ASL
14
15import java.io.BufferedReader;
16import java.io.FileReader;
17import java.io.IOException;
18import java.util.HashMap;
19
25e48683 20import org.eclipse.core.resources.IResource;
5945cec9
FC
21import org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent;
22import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventContent;
23import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventField;
24import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventType;
25import org.eclipse.linuxtools.internal.lttng.core.event.LttngTimestamp;
5d10d135 26import org.eclipse.linuxtools.lttng.jni.JniEvent;
6c13869b 27import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
a1440d1f 28import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
6c13869b
FC
29import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
30import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint;
31import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
32import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
33import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
5d10d135 34
12c155f5 35public class LTTngTextTrace extends TmfTrace<LttngEvent> {
25e48683 36 private LttngTimestamp eventTimestamp = null;
99005796 37 private String eventSource = null;
5d10d135
ASL
38 private LttngEventType eventType = null;
39 private TextLttngEventContent eventContent = null;
4641c2f7 40 private String eventReference = null;
5d10d135 41 // The actual event
25e48683
FC
42 private TextLttngEvent currentLttngEvent = null;
43
5d10d135 44 private HashMap<String, LttngEventType> traceTypes = null;
25e48683 45
9c4eb5f7 46 private String tracepath = ""; //$NON-NLS-1$
5d10d135
ASL
47 private FileReader fr = null;
48 private BufferedReader br = null;
49 private Long nbCharRead = 0L;
25e48683 50
5d10d135 51 private int cpuNumber = -1;
25e48683
FC
52
53 private final boolean showDebug = false;
54
55 public LTTngTextTrace(final IResource resource, final String path) throws Exception {
56 this(resource, path, true); // false);
5d10d135 57 }
25e48683
FC
58
59 public LTTngTextTrace(final IResource resource, final String path, final boolean skipIndexing) throws Exception {
60 super(null, LttngEvent.class, path, 1, !skipIndexing);
61
5d10d135
ASL
62 tracepath = path;
63 traceTypes = new HashMap<String, LttngEventType>();
25e48683 64
5d10d135 65 eventTimestamp = new LttngTimestamp();
21412413 66 eventSource = "Kernel Core"; //$NON-NLS-1$
5d10d135
ASL
67 eventType = new LttngEventType();
68 eventContent = new TextLttngEventContent(currentLttngEvent);
4641c2f7 69 eventReference = getName();
25e48683 70
b7f0ec69 71 currentLttngEvent = new TextLttngEvent(this, eventTimestamp, eventSource, eventType, eventContent, eventReference);
5d10d135 72 eventContent.setEvent(currentLttngEvent);
25e48683
FC
73
74 if ( positionToFirstEvent() == false )
75 throw new IOException("Fail to position to the beginning of the trace"); //$NON-NLS-1$
5d10d135 76 else {
25e48683
FC
77 fIndexPageSize = 1000;
78
79 // Skip indexing if asked
80 // if ( skipIndexing == true ) {
81 fCheckpoints.add(new TmfCheckpoint(new LttngTimestamp(0L), new TmfLocation<Long>(0L)));
82 // }
83 // else {
84 // indexTrace(true);
85 // }
86
87 final Long endTime = currentLttngEvent.getTimestamp().getValue();
88 positionToFirstEvent();
89
90 getNextEvent(new TmfContext(null, 0));
91 final Long starTime = currentLttngEvent.getTimestamp().getValue();
92 positionToFirstEvent();
93
94 setTimeRange( new TmfTimeRange( new LttngTimestamp(starTime),
95 new LttngTimestamp(endTime)
96 ) );
5d10d135
ASL
97 }
98 }
25e48683
FC
99
100
101 public LTTngTextTrace(final LTTngTextTrace oldTrace) throws Exception {
102 this(oldTrace.getResource(), oldTrace.getPath(), true);
103
104 // *** VERIFY ***
105 // Is this safe?
106 fCheckpoints = oldTrace.fCheckpoints;
5d10d135 107 }
25e48683 108
5d10d135 109 private boolean positionToFirstEvent() {
25e48683
FC
110
111 boolean isSuccessful = true;
112
113 try {
114 if ( br != null ) {
115 br.close();
116 fr.close();
117 }
118
119 fr = new FileReader(tracepath);
120 br = new BufferedReader(fr);
121
122 // Skip the 2 lines header
123 br.readLine();
124 br.readLine();
125
126 // Make sure the event time is consistent
127 eventTimestamp.setValue(0L);
128 }
129 catch (final IOException e) {
130 isSuccessful = false;
131 }
132
133 return isSuccessful;
5d10d135 134 }
25e48683
FC
135
136 private void skipToPosition(final TmfLocation<Long> skip) {
137 try {
138 long skipPosition = skip.getLocation();
139 if ( skipPosition < 0 )
140 skipPosition = 0L;
141
142 if ( showDebug == true ) {
143 System.out.println("skipToPosition(Long skipPosition)"); //$NON-NLS-1$
144 System.out.println("\tSkipping to : " + skipPosition); //$NON-NLS-1$
145 System.out.println();
146 }
147 positionToFirstEvent();
148 final long nbSkipped = br.skip(skipPosition);
149 if ( nbSkipped != skipPosition)
150 throw new IOException("Too few characters skipped, positionning failed! (skipToPosition)"); //$NON-NLS-1$
151
152 nbCharRead = skipPosition;
153 }
154 catch (final Exception e) {
155 e.printStackTrace();
156 }
5d10d135 157 }
25e48683 158
452ad365 159 @Override
25e48683
FC
160 @SuppressWarnings("unchecked")
161 public TmfContext seekLocation(ITmfLocation<?> location) {
162 if (location == null)
163 location = new TmfLocation<Long>(0L);
164
165 if (!((TmfLocation<Long>) location).getLocation().equals(nbCharRead))
166 skipToPosition((TmfLocation<Long>) location);
167
168 final TmfContext tmpTraceContext = new TmfContext(location, 0L);
169
170 return tmpTraceContext;
88144d4a 171 }
25e48683 172
90e2b925 173 @Override
25e48683 174 public TmfContext seekLocation(final double ratio) {
90e2b925
FC
175 // TODO Auto-generated method stub
176 return null;
177 }
178
179 @Override
25e48683 180 public double getLocationRatio(final ITmfLocation<?> location) {
90e2b925
FC
181 // TODO Auto-generated method stub
182 return 0;
183 }
184
25e48683
FC
185 private LttngEvent parseMyNextEvent(final ITmfContext context) {
186
187 // All parsing variables declared here so to be able to print them into the catch if needed
188 String tmpContent = null;
189 int tmpCurIndex = 0;
190 int tmpPrevIndex = 0;
191
192 String tracefile = ""; //$NON-NLS-1$
193 long tmpCpu = 0;
194 String marker = ""; //$NON-NLS-1$
195
196 long tmpSecond = 0;
197 long tmpNanosecond = 0;
198
199 String parsedPayload = ""; //$NON-NLS-1$
200 String markerName = ""; //$NON-NLS-1$
201 Object payload = ""; //$NON-NLS-1$
202
203 HashMap<String, LttngEventField> fieldsMap = null;
204
205 LttngEvent returnedEvent = null;
206
207 try {
208 tmpContent = br.readLine();
209
210 if (tmpContent != null) {
211 // *** NOTE :
212 // -1 is the skip the end of line (\n)
213 nbCharRead += (tmpContent.length()+1);
214
215 if ( (currentLttngEvent != null) && (currentLttngEvent.getContent().getMapContent() != null) )
216 currentLttngEvent.getContent().emptyContent();
217
218 // Tracefile and marker are first in the file
219 // Sound like :
220 // kernel.syscall_entry:
221 tmpCurIndex = tmpContent.indexOf(".", tmpPrevIndex); //$NON-NLS-1$
222
223 // *** HACK ***
224 // Evil exit case here : the two last line of the text dump does not contain "."
225 // We should check in a better way (string comparison and such) but it make the whole process to weight a lot more
226 // Conclusion : this is ugly but fast.
227 if ( tmpCurIndex < 0 ) {
228 if ( showDebug == true ) {
229 System.out.println("END OF FILE."); //$NON-NLS-1$
230 System.out.println();
231 }
232 return null;
233 }
234
235 tracefile = tmpContent.substring(tmpPrevIndex, tmpCurIndex ).trim();
236 /*System.out.println(tracefile);*/
237
238 tmpPrevIndex = tmpCurIndex;
239 tmpCurIndex = tmpContent.indexOf(":", tmpPrevIndex); //$NON-NLS-1$
240 marker = tmpContent.substring(tmpPrevIndex+1, tmpCurIndex ).trim();
241 /*System.out.println(marker);*/
242
243 // Timestamp is next but is presented in second.milisecond format, we have to split them
244 // Sound like :
245 // 952.162637168
246 tmpPrevIndex = tmpCurIndex+1;
247 tmpCurIndex = tmpContent.indexOf(".", tmpPrevIndex); //$NON-NLS-1$
248 tmpSecond = Long.parseLong( tmpContent.substring(tmpPrevIndex, tmpCurIndex ).trim() );
249 /*System.out.println(tmpSecond);*/
250
251 tmpPrevIndex = tmpCurIndex+1;
252 tmpCurIndex = tmpContent.indexOf(" ", tmpPrevIndex); //$NON-NLS-1$
253 tmpNanosecond = Long.parseLong( tmpContent.substring(tmpPrevIndex, tmpCurIndex ).trim() );
254 /*System.out.println(tmpNanosecond);*/
255
256 // We have enough information here to set the timestamp
257 eventTimestamp.setValue( (tmpSecond * 1000000000) + tmpNanosecond );
258 /*System.out.println(eventTimestamp.toString());*/
259
260 // Next field is the reference
261 // A long string enclosed by parenthesis and ending with a comma
262 // (/home/william/workspace/org.eclipse.linuxtools.lttng.tests/traceset/trace-618339events-1293lost-1cpu/kernel_0),
263 tmpPrevIndex = tmpCurIndex+1;
264 tmpCurIndex = tmpContent.indexOf("(", tmpPrevIndex); //$NON-NLS-1$
265 tmpPrevIndex = tmpCurIndex+1;
266 tmpCurIndex = tmpContent.indexOf("),", tmpPrevIndex); //$NON-NLS-1$
267 final String fullTracePath = tmpContent.substring(tmpPrevIndex, tmpCurIndex ).trim();
268 /*System.out.println(fullTracePath);*/
269
270 final String traceName = fullTracePath.substring(fullTracePath.lastIndexOf("/")+1).trim(); //$NON-NLS-1$
271 /*System.out.println(traceName);*/
272 eventReference = traceName;
273 currentLttngEvent.setReference(traceName);
274
275
276 // The next few fields are relatives to the state system (pid, ppid, etc...) we need to skip them.
277 // They should be like the following :
278 // 4175, 4175, hal-acl-tool, , 4168,
279
280 // 1st comma
281 tmpPrevIndex = tmpCurIndex+1;
282 tmpCurIndex = tmpContent.indexOf(",", tmpPrevIndex); //$NON-NLS-1$
283 // 2nd comma
284 tmpPrevIndex = tmpCurIndex+1;
285 tmpCurIndex = tmpContent.indexOf(",", tmpPrevIndex); //$NON-NLS-1$
286 // 3rd comma
287 tmpPrevIndex = tmpCurIndex+1;
288 tmpCurIndex = tmpContent.indexOf(",", tmpPrevIndex); //$NON-NLS-1$
289 // 4th comma
290 tmpPrevIndex = tmpCurIndex+1;
291 tmpCurIndex = tmpContent.indexOf(",", tmpPrevIndex); //$NON-NLS-1$
292 // 5th comma
293 tmpPrevIndex = tmpCurIndex+1;
294 tmpCurIndex = tmpContent.indexOf(",", tmpPrevIndex); //$NON-NLS-1$
295 // 6th comma
296 tmpPrevIndex = tmpCurIndex+1;
297 tmpCurIndex = tmpContent.indexOf(",", tmpPrevIndex); //$NON-NLS-1$
298
299 // The next field is the CPU, in hexadecimal format
300 // Should be like :
301 // 0x0,
302 tmpPrevIndex = tmpCurIndex+1;
303 tmpCurIndex = tmpContent.indexOf("0x", tmpPrevIndex); //$NON-NLS-1$
304 tmpPrevIndex = tmpCurIndex+2;
305
306 tmpCurIndex = tmpContent.indexOf(",", tmpPrevIndex); //$NON-NLS-1$
307 tmpCpu = Long.parseLong( tmpContent.substring(tmpPrevIndex, tmpCurIndex ).trim() );
308
309 // Set the cpu number of trace if we found a "new" cpu
310 if ( cpuNumber < (tmpCpu + 1) )
311 cpuNumber = (int)(tmpCpu+1);
312
313
314 // The last field is the parsed content
315 // It is enclosed by { }
316 // Look like :
317 // SYSCALL { ip = 0xb7f05422, syscall_id = 221 [sys_fcntl64+0x0/0x79] }
318 //
319 // NOTE : it seems some state system events do not respect this format as they have no payload.
320 // We will create empty payload then.
321 final int tmpIndex = tmpContent.indexOf("{", tmpPrevIndex); //$NON-NLS-1$
322 if ( tmpIndex != -1 ) {
323 tmpPrevIndex = tmpCurIndex+1;
324 tmpCurIndex = tmpIndex;
325 tmpPrevIndex = tmpCurIndex+1;
326 tmpCurIndex = tmpContent.indexOf("}", tmpPrevIndex); //$NON-NLS-1$
327 parsedPayload = tmpContent.substring(tmpPrevIndex, tmpCurIndex ).trim();
328
329 // Now add each LttngField
330 boolean isDone = false;
331 int tmpIndexBegin = 0;
332 int tmpIndexEqual = 0;
333 int tmpIndexEnd = 0;
334
335 fieldsMap = new HashMap<String, LttngEventField>();
336
337 while ( isDone == false ) {
338 tmpIndexEqual = parsedPayload.indexOf("=", tmpIndexBegin); //$NON-NLS-1$
339 tmpIndexEnd = parsedPayload.indexOf(", ", tmpIndexEqual); //$NON-NLS-1$
340 if ( tmpIndexEnd == -1 ) {
341 tmpIndexEnd = parsedPayload.length();
342 isDone = true;
343 }
344
345 markerName = parsedPayload.substring(tmpIndexBegin, tmpIndexEqual-1 ).trim();
346 payload = parsedPayload.substring(tmpIndexEqual+1, tmpIndexEnd ).replace("\"", " ").trim(); //$NON-NLS-1$ //$NON-NLS-2$
347
348 // Try to cast the payload into the correct type
349 try {
350 payload = Long.parseLong((String)payload);
351 }
352 catch (final NumberFormatException e) { }
353
354 final LttngEventField tmpField = new LttngEventField(markerName, payload);
355 fieldsMap.put(markerName, tmpField);
356
357 tmpIndexBegin = tmpIndexEnd+1;
358 }
359 }
360 else {
361 fieldsMap = new HashMap<String, LttngEventField>();
362
363 markerName = ""; //$NON-NLS-1$
364 payload = ""; //$NON-NLS-1$
365
366 final LttngEventField tmpField = new LttngEventField(markerName, payload);
367 fieldsMap.put(markerName, tmpField);
368 }
369
370 eventContent = new TextLttngEventContent(currentLttngEvent, fieldsMap);
371
372 // We now have what we need for the type
373 final String tmpTypeKey = tracefile + "/" + tmpCpu + "/" + marker; //$NON-NLS-1$ //$NON-NLS-2$
374 if ( traceTypes.get(tmpTypeKey) == null )
375 traceTypes.put(tmpTypeKey, new LttngEventType(tracefile, tmpCpu, marker, 0, fieldsMap.keySet().toArray(new String[fieldsMap.size()] )) );
376
377 currentLttngEvent.setContent(eventContent);
378 currentLttngEvent.setType(traceTypes.get(tmpTypeKey));
379
380 returnedEvent = currentLttngEvent;
381 }
382 else if ( showDebug == true ) {
383 System.out.println("NULL READING"); //$NON-NLS-1$
384 System.out.println();
385 returnedEvent = null;
386 }
387 }
388 catch (final Exception e) {
389 System.out.println("Pos is :" + nbCharRead); //$NON-NLS-1$
390 if ( tmpContent != null )
391 System.out.println("Erroneous content is :" + tmpContent); //$NON-NLS-1$
392
393 tmpContent = null;
394 e.printStackTrace();
395 returnedEvent = null;
396 }
397
398 return returnedEvent;
5d10d135 399 }
25e48683 400
5d10d135 401 @Override
452ad365 402 public ITmfLocation<?> getCurrentLocation() {
25e48683 403 return new TmfLocation<Long>(nbCharRead);
5d10d135 404 }
25e48683
FC
405
406 @Override
407 public LttngEvent parseEvent(ITmfContext context) {
408 context = seekLocation(context.getLocation());
409 return parseMyNextEvent(context);
410
5d10d135 411 }
25e48683
FC
412
413 public int getCpuNumber() {
414 return cpuNumber;
5d10d135 415 }
cb866e08 416
5d10d135
ASL
417}
418
419
25e48683 420// Redefine event to override method we know won't work with a Text tracefile
5d10d135 421class TextLttngEvent extends LttngEvent {
25e48683
FC
422
423 public TextLttngEvent( final TmfTrace<LttngEvent> parent,
424 final LttngTimestamp timestamp,
425 final String source,
426 final LttngEventType type,
427 final LttngEventContent content,
428 final String reference)
429 {
430 super(parent, timestamp, source, type, content, reference, null);
431 }
432
433 @SuppressWarnings("unchecked")
434 public TextLttngEvent(final TextLttngEvent oldEvent) {
435 this(
436 (TmfTrace<LttngEvent>) oldEvent.getTrace(),
437 (LttngTimestamp)oldEvent.getTimestamp(),
438 oldEvent.getSource(),
439 oldEvent.getType(),
440 oldEvent.getContent(),
441 oldEvent.getReference()
442 );
443 }
444
445 @Override
446 public JniEvent convertEventTmfToJni() {
447 // System.out.println("WARNING : Cannot use convertEventTmfToJni() on a trace in text format."); //$NON-NLS-1$
448 return null;
449 }
450
451 @Override
452 public void updateJniEventReference(final JniEvent newJniEventReference) {
453 // System.out.println("WARNING : Cannot use updateJniEventReference on a trace in text format. Using null."); //$NON-NLS-1$
5d10d135
ASL
454 }
455}
456
457
458class TextLttngEventContent extends LttngEventContent {
25e48683
FC
459
460 public TextLttngEventContent() {
5d10d135
ASL
461 super();
462 }
25e48683
FC
463
464 public TextLttngEventContent(final TextLttngEvent thisParent) {
5d10d135
ASL
465 super(thisParent, null);
466 }
25e48683
FC
467
468 public TextLttngEventContent(final TextLttngEvent thisParent, final HashMap<String, LttngEventField> thisContent) {
5d10d135
ASL
469 super(thisParent, thisContent);
470 }
25e48683
FC
471
472 public TextLttngEventContent(final TextLttngEventContent oldContent) {
473 this(((TextLttngEvent) oldContent.getEvent()), oldContent.getMapContent());
5d10d135 474 }
25e48683 475
5d10d135
ASL
476 @Override
477 public LttngEventField[] getFields() {
25e48683 478 return getMapContent().values().toArray(new LttngEventField[getMapContent().size()]);
5d10d135 479 }
25e48683 480
5d10d135 481 @Override
25e48683
FC
482 public LttngEventField getField(final String name) {
483 final LttngEventField returnedField = getMapContent().get(name);
484
5d10d135
ASL
485 return returnedField;
486 }
25e48683 487
5d10d135 488 @Override
25e48683
FC
489 public LttngEventField getField(final int position) {
490 LttngEventField returnedField = null;
491 String label = null;
492 // try {
493 label = getEvent().getType().getFieldName(position);
494 returnedField = this.getField(label);
495 // }
496 // catch (TmfNoSuchFieldException e) {
497 // System.out.println("Invalid field position requested : " + position + ", ignoring (getField)."); //$NON-NLS-1$ //$NON-NLS-2$
498 // }
499
5d10d135
ASL
500 return returnedField;
501 }
12c155f5 502}
This page took 0.061721 seconds and 5 git commands to generate.