[PATCH] D96384: Fix JSON formatting when converting to trace event format

Todd Lipcon via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 9 16:34:04 PST 2021


toddlipcon created this revision.
toddlipcon added a reviewer: dberris.
toddlipcon requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96384

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


Index: llvm/tools/llvm-xray/xray-converter.cpp
===================================================================
--- llvm/tools/llvm-xray/xray-converter.cpp
+++ llvm/tools/llvm-xray/xray-converter.cpp
@@ -269,19 +269,14 @@
   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 @@
       // 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 @@
       // (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,
Index: llvm/test/tools/llvm-xray/X86/convert-traceevent-special-events.txt
===================================================================
--- /dev/null
+++ 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":
Index: llvm/test/tools/llvm-xray/X86/convert-fdr-to-traceevent.txt
===================================================================
--- /dev/null
+++ 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":


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96384.322537.patch
Type: text/x-patch
Size: 3461 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210210/07278771/attachment.bin>


More information about the llvm-commits mailing list