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