| 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; |
| 28 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; |
| 29 | import org.eclipse.linuxtools.tmf.request.TmfDataRequest; |
| 30 | import org.eclipse.linuxtools.tmf.trace.ITmfContext; |
| 31 | import org.eclipse.linuxtools.tmf.trace.ITmfLocation; |
| 32 | import org.eclipse.linuxtools.tmf.trace.ITmfTrace; |
| 33 | import org.eclipse.linuxtools.tmf.trace.TmfCheckpoint; |
| 34 | import org.eclipse.linuxtools.tmf.trace.TmfContext; |
| 35 | import org.eclipse.linuxtools.tmf.trace.TmfLocation; |
| 36 | import org.eclipse.linuxtools.tmf.trace.TmfTrace; |
| 37 | |
| 38 | public class LTTngTextTrace extends TmfTrace<LttngEvent> implements ITmfTrace { |
| 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 | |
| 56 | private boolean showDebug = false; |
| 57 | |
| 58 | public LTTngTextTrace(String path) throws Exception { |
| 59 | this(path, false); |
| 60 | } |
| 61 | |
| 62 | public LTTngTextTrace(String path, boolean skipIndexing) throws Exception { |
| 63 | super(LttngEvent.class, path, 1); |
| 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 | |
| 74 | currentLttngEvent = new TextLttngEvent(eventTimestamp, eventSource, eventType, eventContent, eventReference); |
| 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 { |
| 81 | fIndexPageSize = 1000; |
| 82 | |
| 83 | // Skip indexing if asked |
| 84 | if ( skipIndexing == true ) { |
| 85 | fCheckpoints.add(new TmfCheckpoint(new LttngTimestamp(0L), new TmfLocation<Long>(0L))); |
| 86 | } |
| 87 | else { |
| 88 | indexTrace(true); |
| 89 | } |
| 90 | |
| 91 | Long endTime = currentLttngEvent.getTimestamp().getValue(); |
| 92 | positionToFirstEvent(); |
| 93 | |
| 94 | getNextEvent(new TmfContext(null, 0)); |
| 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 | |
| 105 | private LTTngTextTrace(LTTngTrace oldStream) throws Exception { |
| 106 | super(LttngEvent.class, null); |
| 107 | throw new Exception("Copy constructor should never be use with a LTTngTrace!"); |
| 108 | } |
| 109 | |
| 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 | |
| 137 | private void skipToPosition(TmfLocation<Long> skip) { |
| 138 | try { |
| 139 | long skipPosition = skip.getValue(); |
| 140 | if ( skipPosition < 0 ) { |
| 141 | skipPosition = 0L; |
| 142 | } |
| 143 | |
| 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(); |
| 150 | br.skip(skipPosition); |
| 151 | |
| 152 | nbCharRead = skipPosition; |
| 153 | } |
| 154 | catch (Exception e) { |
| 155 | e.printStackTrace(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @SuppressWarnings("unchecked") |
| 160 | public TmfContext seekLocation(ITmfLocation location) { |
| 161 | |
| 162 | if (location == null) { |
| 163 | location = new TmfLocation<Long>(0L); |
| 164 | } |
| 165 | |
| 166 | if (!((TmfLocation<Long>) location).getValue().equals(nbCharRead)) { |
| 167 | skipToPosition((TmfLocation<Long>) location); |
| 168 | } |
| 169 | |
| 170 | TmfContext tmpTraceContext = new TmfContext(location, 0L); |
| 171 | |
| 172 | return tmpTraceContext; |
| 173 | } |
| 174 | |
| 175 | private LttngEvent parseMyNextEvent(TmfContext context) { |
| 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 "." |
| 220 | // We should check in a better way (string comparison and such) but it make the whole process to weight a lot more |
| 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 | } |
| 227 | return null; |
| 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 | // |
| 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. |
| 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 | } |
| 356 | } |
| 357 | else { |
| 358 | fieldsMap = new HashMap<String, LttngEventField>(); |
| 359 | |
| 360 | markerName = ""; |
| 361 | payload = ""; |
| 362 | |
| 363 | LttngEventField tmpField = new LttngEventField(eventContent, markerName, payload); |
| 364 | fieldsMap.put(markerName, tmpField); |
| 365 | } |
| 366 | |
| 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 | |
| 378 | returnedEvent = currentLttngEvent; |
| 379 | } |
| 380 | else if ( showDebug == true ) { |
| 381 | System.out.println("NULL READING"); |
| 382 | System.out.println(); |
| 383 | returnedEvent = null; |
| 384 | } |
| 385 | } |
| 386 | catch (Exception e) { |
| 387 | System.out.println("Pos is :" + nbCharRead); |
| 388 | if ( tmpContent != null ) { |
| 389 | System.out.println("Erroneous content is :" + tmpContent); |
| 390 | } |
| 391 | |
| 392 | tmpContent = null; |
| 393 | e.printStackTrace(); |
| 394 | returnedEvent = null; |
| 395 | } |
| 396 | |
| 397 | return returnedEvent; |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public ITmfLocation getCurrentLocation() { |
| 402 | return new TmfLocation<Long>(nbCharRead); |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public LttngEvent parseEvent(TmfContext context) { |
| 407 | context = seekLocation(context.getLocation()); |
| 408 | return parseMyNextEvent(context); |
| 409 | |
| 410 | } |
| 411 | |
| 412 | public int getCpuNumber() { |
| 413 | return cpuNumber; |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public ITmfContext setContext(TmfDataRequest<LttngEvent> request) { |
| 418 | // TODO Auto-generated method stub |
| 419 | return null; |
| 420 | } |
| 421 | |
| 422 | } |
| 423 | |
| 424 | |
| 425 | // Redefine event to override method we know won't work with a Text tracefile |
| 426 | class TextLttngEvent extends LttngEvent { |
| 427 | |
| 428 | public TextLttngEvent( LttngTimestamp timestamp, |
| 429 | LttngEventSource source, |
| 430 | LttngEventType type, |
| 431 | LttngEventContent content, |
| 432 | LttngEventReference reference) |
| 433 | { |
| 434 | super(timestamp, source, type, content, reference, null); |
| 435 | } |
| 436 | |
| 437 | public TextLttngEvent(TextLttngEvent oldEvent) { |
| 438 | this( |
| 439 | (LttngTimestamp)oldEvent.getTimestamp(), |
| 440 | (LttngEventSource)oldEvent.getSource(), |
| 441 | (LttngEventType)oldEvent.getType(), |
| 442 | (LttngEventContent)oldEvent.getContent(), |
| 443 | (LttngEventReference)oldEvent.getReference() |
| 444 | ); |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public JniEvent convertEventTmfToJni() { |
| 449 | System.out.println("WARNING : Cannot use convertEventTmfToJni() on a trace in text format."); |
| 450 | return null; |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public void updateJniEventReference(JniEvent newJniEventReference) { |
| 455 | System.out.println("WARNING : Cannot use updateJniEventReference on a trace in text format. Using null."); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | |
| 460 | class TextLttngEventContent extends LttngEventContent { |
| 461 | |
| 462 | public TextLttngEventContent() { |
| 463 | super(); |
| 464 | } |
| 465 | |
| 466 | public TextLttngEventContent(TextLttngEvent thisParent) { |
| 467 | super(thisParent, null); |
| 468 | } |
| 469 | |
| 470 | public TextLttngEventContent(TextLttngEvent thisParent, HashMap<String, LttngEventField> thisContent) { |
| 471 | super(thisParent, thisContent); |
| 472 | } |
| 473 | |
| 474 | public TextLttngEventContent(TextLttngEventContent oldContent) { |
| 475 | this( (TextLttngEvent)oldContent.fParentEvent, oldContent.getRawContent()); |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public LttngEventField[] getFields() { |
| 480 | return getRawContent().values().toArray(new LttngEventField[getRawContent().size()]); |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public LttngEventField getField(String name) { |
| 485 | LttngEventField returnedField = getRawContent().get(name); |
| 486 | |
| 487 | return returnedField; |
| 488 | } |
| 489 | |
| 490 | @Override |
| 491 | public LttngEventField getField(int position) { |
| 492 | LttngEventField returnedField = null; |
| 493 | String label = fParentEvent.getType().getLabel(position); |
| 494 | |
| 495 | if ( label != null ) { |
| 496 | returnedField = this.getField(label); |
| 497 | } |
| 498 | |
| 499 | return returnedField; |
| 500 | } |
| 501 | } |