[llvm] 747c450 - Fix JSON formatting when converting to trace event format

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 9 18:01:50 PST 2021


Author: Todd Lipcon
Date: 2021-02-10T13:00:28+11:00
New Revision: 747c450e6f1f7238243150a6870daa6385476413

URL: https://github.com/llvm/llvm-project/commit/747c450e6f1f7238243150a6870daa6385476413
DIFF: https://github.com/llvm/llvm-project/commit/747c450e6f1f7238243150a6870daa6385476413.diff

LOG: Fix JSON formatting when converting to trace event format

Reviewed By: dberris

Differential Revision: https://reviews.llvm.org/D96384

Added: 
    llvm/test/tools/llvm-xray/X86/convert-fdr-to-traceevent.txt
    llvm/test/tools/llvm-xray/X86/convert-traceevent-special-events.txt

Modified: 
    llvm/tools/llvm-xray/xray-converter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-xray/X86/convert-fdr-to-traceevent.txt b/llvm/test/tools/llvm-xray/X86/convert-fdr-to-traceevent.txt
new file mode 100644
index 000000000000..2e845ab3aa92
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/convert-fdr-to-traceevent.txt
@@ -0,0 +1,12 @@
+; RUN: llvm-xray convert %S/Inputs/fdr-log-version-1.xray -f=trace_event -o - \
+; RUN:   | %python -c 'import json, sys; json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' \
+; RUN:   | FileCheck %s
+
+; CHECK: "traceEvents": [
+; Check fields for an event are present
+; CHECK: "name":
+; CHECK-NEXT: "ph":
+; CHECK-NEXT: "pid":
+; CHECK-NEXT: "sf":
+; CHECK-NEXT: "tid":
+; CHECK-NEXT: "ts":

diff  --git a/llvm/test/tools/llvm-xray/X86/convert-traceevent-special-events.txt b/llvm/test/tools/llvm-xray/X86/convert-traceevent-special-events.txt
new file mode 100644
index 000000000000..1693a4213a1b
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/convert-traceevent-special-events.txt
@@ -0,0 +1,24 @@
+# RUN: llvm-xray convert %s -f=trace_event -o - \
+# RUN:   | %python -c 'import json, sys; json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' \
+# RUN:   | FileCheck %s
+---
+header:
+  version: 1
+  type: 0
+  constant-tsc: true
+  nonstop-tsc: true
+  cycle-frequency: 2601000000
+records:
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-enter, tsc: 10001 }
+  - { type: 0, func-id: 1, function: 'x', cpu: 1, thread: 111, process: 111, kind: custom-event, tsc: 2000, data: "\x03\0\0\0" }
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-exit, tsc: 10100 }
+...
+
+# CHECK: "traceEvents": [
+# Check fields for an event are present
+# CHECK: "name":
+# CHECK-NEXT: "ph":
+# CHECK-NEXT: "pid":
+# CHECK-NEXT: "sf":
+# CHECK-NEXT: "tid":
+# CHECK-NEXT: "ts":

diff  --git a/llvm/tools/llvm-xray/xray-converter.cpp b/llvm/tools/llvm-xray/xray-converter.cpp
index c1a623f0f858..645e2e3d7199 100644
--- a/llvm/tools/llvm-xray/xray-converter.cpp
+++ b/llvm/tools/llvm-xray/xray-converter.cpp
@@ -269,19 +269,14 @@ void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
   auto CycleFreq = FH.CycleFrequency;
 
   unsigned id_counter = 0;
+  int NumOutputRecords = 0;
 
-  OS << "{\n  \"traceEvents\": [";
+  OS << "{\n  \"traceEvents\": [\n";
   DenseMap<uint32_t, StackTrieNode *> StackCursorByThreadId{};
   DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> StackRootsByThreadId{};
   DenseMap<unsigned, StackTrieNode *> StacksByStackId{};
   std::forward_list<StackTrieNode> NodeStore{};
-  int loop_count = 0;
   for (const auto &R : Records) {
-    if (loop_count++ == 0)
-      OS << "\n";
-    else
-      OS << ",\n";
-
     // Chrome trace event format always wants data in micros.
     // CyclesPerMicro = CycleHertz / 10^6
     // TSC / CyclesPerMicro == TSC * 10^6 / CycleHertz == MicroTimestamp
@@ -306,6 +301,9 @@ void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
       // type of B for begin or E for end, thread id, process id,
       // timestamp in microseconds, and a stack frame id. The ids are logged
       // in an id dictionary after the events.
+      if (NumOutputRecords++ > 0) {
+        OS << ",\n";
+      }
       writeTraceViewerRecord(Version, OS, R.FuncId, R.TId, R.PId, Symbolize,
                              FuncIdHelper, EventTimestampUs, *StackCursor, "B");
       break;
@@ -318,7 +316,7 @@ void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
       // (And/Or in loop termination below)
       StackTrieNode *PreviousCursor = nullptr;
       do {
-        if (PreviousCursor != nullptr) {
+        if (NumOutputRecords++ > 0) {
           OS << ",\n";
         }
         writeTraceViewerRecord(Version, OS, StackCursor->FuncId, R.TId, R.PId,


        


More information about the llvm-commits mailing list