From d389ef2c487dbca647d2bd3ccd2a8242da06e01e Mon Sep 17 00:00:00 2001 From: Matthew Khouzam Date: Wed, 4 Dec 2024 23:11:43 -0500 Subject: [PATCH] traceevent: add offsets to json file Store initial entries as properties. Signed-off-by: Matthew Khouzam --- .../core/tests/TraceEventTraceTest.java | 4 +-- .../core/trace/TraceEventSortingJob.java | 6 ++-- .../core/trace/TraceEventTrace.java | 34 ++++++++++++++++--- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core.tests/src/org/eclipse/tracecompass/incubator/traceevent/core/tests/TraceEventTraceTest.java b/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core.tests/src/org/eclipse/tracecompass/incubator/traceevent/core/tests/TraceEventTraceTest.java index 361c47884..c52d3aed7 100644 --- a/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core.tests/src/org/eclipse/tracecompass/incubator/traceevent/core/tests/TraceEventTraceTest.java +++ b/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core.tests/src/org/eclipse/tracecompass/incubator/traceevent/core/tests/TraceEventTraceTest.java @@ -63,7 +63,7 @@ public void testEvent() throws TmfTraceException { ImmutableSet 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"); @@ -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(); diff --git a/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core/src/org/eclipse/tracecompass/incubator/internal/traceevent/core/trace/TraceEventSortingJob.java b/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core/src/org/eclipse/tracecompass/incubator/internal/traceevent/core/trace/TraceEventSortingJob.java index 25b1a6719..c8608fb91 100644 --- a/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core/src/org/eclipse/tracecompass/incubator/internal/traceevent/core/trace/TraceEventSortingJob.java +++ b/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core/src/org/eclipse/tracecompass/incubator/internal/traceevent/core/trace/TraceEventSortingJob.java @@ -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 diff --git a/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core/src/org/eclipse/tracecompass/incubator/internal/traceevent/core/trace/TraceEventTrace.java b/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core/src/org/eclipse/tracecompass/incubator/internal/traceevent/core/trace/TraceEventTrace.java index 9b67c74a3..d2e6471bf 100644 --- a/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core/src/org/eclipse/tracecompass/incubator/internal/traceevent/core/trace/TraceEventTrace.java +++ b/tracetypes/org.eclipse.tracecompass.incubator.traceevent.core/src/org/eclipse/tracecompass/incubator/internal/traceevent/core/trace/TraceEventTrace.java @@ -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; @@ -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; @@ -167,9 +169,30 @@ public void initTrace(IResource resource, String path, Class 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 map = new JSONObject(jsonSoFar).toMap(); + for (Entry 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 { @@ -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 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);