Skip to content

Commit

Permalink
traceevent: add offsets to json file
Browse files Browse the repository at this point in the history
Store initial entries as properties.

Signed-off-by: Matthew Khouzam <[email protected]>
  • Loading branch information
MatthewKhouzam committed Dec 5, 2024
1 parent a87455a commit d389ef2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testEvent() throws TmfTraceException {
ImmutableSet<String> aspectNames = ImmutableSet.of("TID", "Args", "Phase", "Category", "PID", "Duration", "ID", "Callsite", "Timestamp", "LogLevel", "Name", "Process Name", "Thread Name");

assertEquals(aspectNames, eventAspects.keySet());
testAspect(eventAspects.get("TID"), event, 0);
testAspect(eventAspects.get("TID"), event, "48");
testAspect(eventAspects.get("Phase"), event, "C");
testAspect(eventAspects.get("Category"), event, null);
testAspect(eventAspects.get("Name"), event, "foo");
Expand Down Expand Up @@ -329,7 +329,7 @@ public void testEmptyEvent() throws TmfTraceException {
assertEquals("X", eventField.getField("ph").getValue());
assertEquals("event1", eventField.getField("name").getValue());
assertEquals("12", eventField.getField("pid").getValue());
assertEquals(12, eventField.getField("tid").getValue());
assertEquals("12", eventField.getField("tid").getValue());
assertEquals("456123453", eventField.getField("ts").getValue()); // it's in microseconds
} finally {
trace.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ public class TraceEventSortingJob extends SortingJob {
* the trace to be sort
* @param path
* the path to the trace file
* @param startOffset
* to seek in bytes
*/
public TraceEventSortingJob(ITmfTrace trace, String path) {
super(trace, path, "\"ts\":", 1); //$NON-NLS-1$
public TraceEventSortingJob(ITmfTrace trace, String path, long startOffset) {
super(trace, path, "\"ts\":", 1, startOffset); //$NON-NLS-1$
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -45,6 +46,7 @@
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
import org.json.JSONObject;

import com.google.common.collect.Lists;

Expand Down Expand Up @@ -167,9 +169,30 @@ public void initTrace(IResource resource, String path, Class<? extends ITmfEvent
super.initTrace(resource, path, type);
fProperties.put("Type", "Trace-Event"); //$NON-NLS-1$ //$NON-NLS-2$
String dir = TmfTraceManager.getSupplementaryFileDir(this);
long startOffset = 0;
try (RandomAccessFile raf = (new RandomAccessFile(new File(path), "r"))) {
goToCorrectStart(raf);
startOffset = raf.getFilePointer();
if (startOffset > 14) {
raf.seek(0);
byte[] data = new byte[(int) startOffset];
raf.read(data);
String jsonSoFar = new String(data);
jsonSoFar = jsonSoFar.substring(0, jsonSoFar.length()-14)+'}';
Map<String, Object> map = new JSONObject(jsonSoFar).toMap();
for (Entry<String, Object> entry : map.entrySet()) {
if (!fProperties.containsKey(entry.getKey())) {
fProperties.put(entry.getKey(), entry.getValue().toString());
}
}
}
} catch (IOException e) {
throw new TmfTraceException(e.getMessage(), e);
}

fFile = new File(dir + new File(path).getName());
if (!fFile.exists()) {
Job sortJob = new TraceEventSortingJob(this, path);
Job sortJob = new TraceEventSortingJob(this, path, startOffset);
sortJob.schedule();
while (sortJob.getResult() == null) {
try {
Expand Down Expand Up @@ -216,26 +239,27 @@ protected static void goToCorrectStart(RandomAccessFile rafile) throws IOExcepti
// skip start if it's {"traceEvents":
String traceEventsKey = "\"traceEvents\""; //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
rafile.seek(0);
int val = rafile.read();
/*
* Skip list contains all the odd control characters
*/
Set<Integer> skipList = new HashSet<>();
skipList.add((int) ':');
skipList.add((int) '\t');
skipList.add((int) '\n');
skipList.add((int) '\r');
skipList.add((int) ' ');
skipList.add((int) '\b');
skipList.add((int) '\f');
int maxLen = 1024;
while (val != -1 && val != ':' && sb.length() < maxLen && !sb.toString().endsWith(traceEventsKey)) {
int maxLen = 4096;
while (val != -1 && sb.length() < maxLen && !sb.toString().endsWith(traceEventsKey)) {
if (!skipList.contains(val)) {
sb.append((char) val);
}
val = rafile.read();
}
if (!(sb.toString().endsWith(traceEventsKey) && rafile.length() > maxLen)) {
String string = sb.toString();
if (!(string.endsWith(traceEventsKey) && string.length() < maxLen)) {
// Trace does not start with {"TraceEvents", maybe it's the events
// directly, go back to start of trace
rafile.seek(0);
Expand Down

0 comments on commit d389ef2

Please sign in to comment.