Merge "ui: Split TraceController in async functions" into main
diff --git a/src/trace_processor/importers/proto/track_event_parser.cc b/src/trace_processor/importers/proto/track_event_parser.cc
index 360126c..67a3dee 100644
--- a/src/trace_processor/importers/proto/track_event_parser.cc
+++ b/src/trace_processor/importers/proto/track_event_parser.cc
@@ -218,13 +218,6 @@
 
     RETURN_IF_ERROR(ParseTrackAssociation());
 
-    // Counter-type events don't support arguments (those are on the
-    // CounterDescriptor instead). All they have is a |{double_,}counter_value|.
-    if (event_.type() == TrackEvent::TYPE_COUNTER) {
-      ParseCounterEvent();
-      return base::OkStatus();
-    }
-
     // If we have legacy thread time / instruction count fields, also parse them
     // into the counters tables.
     ParseLegacyThreadTimeAndInstructionsAsCounters();
@@ -234,6 +227,12 @@
     // these counter values and also parse them as slice attributes / arguments.
     ParseExtraCounterValues();
 
+    // Non-legacy counters are treated differently. Legacy counters do not have
+    // a track_id_ and should instead go through the switch below.
+    if (event_.type() == TrackEvent::TYPE_COUNTER) {
+      return ParseCounterEvent();
+    }
+
     // TODO(eseckler): Replace phase with type and remove handling of
     // legacy_event_.phase() once it is no longer used by producers.
     char phase = static_cast<char>(ParsePhaseOrType());
@@ -561,7 +560,7 @@
     }
   }
 
-  void ParseCounterEvent() {
+  base::Status ParseCounterEvent() {
     // Tokenizer ensures that TYPE_COUNTER events are associated with counter
     // tracks and have values.
     PERFETTO_DCHECK(storage_->counter_track_table().FindById(track_id_));
@@ -569,7 +568,9 @@
                     event_.has_double_counter_value());
 
     context_->event_tracker->PushCounter(
-        ts_, static_cast<double>(event_data_->counter_value), track_id_);
+        ts_, static_cast<double>(event_data_->counter_value), track_id_,
+        [this](BoundInserter* inserter) { ParseTrackEventArgs(inserter); });
+    return base::OkStatus();
   }
 
   void ParseLegacyThreadTimeAndInstructionsAsCounters() {
diff --git a/ui/src/core_plugins/counter/counter_details_panel.ts b/ui/src/core_plugins/counter/counter_details_panel.ts
index 70d96c1..e0ff568 100644
--- a/ui/src/core_plugins/counter/counter_details_panel.ts
+++ b/ui/src/core_plugins/counter/counter_details_panel.ts
@@ -14,6 +14,7 @@
 
 import {Time, duration, time} from '../../base/time';
 import {Engine} from '../../trace_processor/engine';
+import {Trace} from '../../public/trace';
 import {
   LONG,
   LONG_NULL,
@@ -29,6 +30,9 @@
 import {Timestamp} from '../../frontend/widgets/timestamp';
 import {DurationWidget} from '../../frontend/widgets/duration';
 import {TrackEventSelection} from '../../public/selection';
+import {hasArgs, renderArguments} from '../../frontend/slice_args';
+import {asArgSetId} from '../../trace_processor/sql_utils/core_types';
+import {Arg, getArgs} from '../../trace_processor/sql_utils/args';
 
 interface CounterDetails {
   // The "left" timestamp of the counter sample T(N)
@@ -42,9 +46,12 @@
 
   // The delta between this sample's value and the previous one F(N) - F(N-1)
   delta: number;
+
+  args?: Arg[];
 }
 
 export class CounterDetailsPanel implements TrackEventDetailsPanel {
+  private readonly trace: Trace;
   private readonly engine: Engine;
   private readonly trackId: number;
   private readonly rootTable: string;
@@ -52,12 +59,13 @@
   private counterDetails?: CounterDetails;
 
   constructor(
-    engine: Engine,
+    trace: Trace,
     trackId: number,
     trackName: string,
     rootTable = 'counter',
   ) {
-    this.engine = engine;
+    this.trace = trace;
+    this.engine = trace.engine;
     this.trackId = trackId;
     this.trackName = trackName;
     this.rootTable = rootTable;
@@ -75,6 +83,14 @@
   render() {
     const counterInfo = this.counterDetails;
     if (counterInfo) {
+      const args =
+        hasArgs(counterInfo.args) &&
+        m(
+          Section,
+          {title: 'Arguments'},
+          m(Tree, renderArguments(this.trace, counterInfo.args)),
+        );
+
       return m(
         DetailsShell,
         {title: 'Counter', description: `${this.trackName}`},
@@ -104,6 +120,7 @@
               }),
             ),
           ),
+          args,
         ),
       );
     } else {
@@ -129,7 +146,8 @@
         ts as leftTs,
         value,
         LAG(value) OVER (ORDER BY ts) AS prevValue,
-        LEAD(ts) OVER (ORDER BY ts) AS rightTs
+        LEAD(ts) OVER (ORDER BY ts) AS rightTs,
+        arg_set_id AS argSetId
       FROM ${rootTable}
       WHERE track_id = ${trackId}
     )
@@ -142,6 +160,7 @@
     prevValue: NUM_NULL,
     leftTs: LONG,
     rightTs: LONG_NULL,
+    argSetId: NUM_NULL,
   });
   const value = row.value;
   const leftTs = Time.fromRaw(row.leftTs);
@@ -150,5 +169,8 @@
 
   const delta = value - prevValue;
   const duration = rightTs - leftTs;
-  return {ts: leftTs, value, delta, duration};
+  const argSetId = row.argSetId;
+  const args =
+    argSetId == null ? undefined : await getArgs(engine, asArgSetId(argSetId));
+  return {ts: leftTs, value, delta, duration, args};
 }
diff --git a/ui/src/core_plugins/counter/index.ts b/ui/src/core_plugins/counter/index.ts
index 66ff682..8ddb193 100644
--- a/ui/src/core_plugins/counter/index.ts
+++ b/ui/src/core_plugins/counter/index.ts
@@ -172,7 +172,7 @@
             unit,
           },
         }),
-        detailsPanel: () => new CounterDetailsPanel(ctx.engine, trackId, title),
+        detailsPanel: () => new CounterDetailsPanel(ctx, trackId, title),
       });
       const track = new TrackNode({uri, title});
       ctx.workspace.addChildInOrder(track);
@@ -248,7 +248,7 @@
           trackId: trackId,
           options: getDefaultCounterOptions(name),
         }),
-        detailsPanel: () => new CounterDetailsPanel(ctx.engine, trackId, name),
+        detailsPanel: () => new CounterDetailsPanel(ctx, trackId, name),
       });
       const trackNode = new TrackNode({uri, title: name, sortOrder: -20});
       ctx.workspace.addChildInOrder(trackNode);
@@ -315,7 +315,7 @@
           trackId: trackId,
           options: getDefaultCounterOptions(name),
         }),
-        detailsPanel: () => new CounterDetailsPanel(ctx.engine, trackId, name),
+        detailsPanel: () => new CounterDetailsPanel(ctx, trackId, name),
       });
       const group = getOrCreateGroupForThread(ctx.workspace, utid);
       const track = new TrackNode({uri, title: name, sortOrder: 30});
@@ -373,7 +373,7 @@
           trackId: trackId,
           options: getDefaultCounterOptions(name),
         }),
-        detailsPanel: () => new CounterDetailsPanel(ctx.engine, trackId, name),
+        detailsPanel: () => new CounterDetailsPanel(ctx, trackId, name),
       });
       const group = getOrCreateGroupForProcess(ctx.workspace, upid);
       const track = new TrackNode({uri, title: name, sortOrder: 20});
@@ -413,8 +413,7 @@
             trackId: trackId,
             options: getDefaultCounterOptions(name),
           }),
-          detailsPanel: () =>
-            new CounterDetailsPanel(ctx.engine, trackId, name),
+          detailsPanel: () => new CounterDetailsPanel(ctx, trackId, name),
         });
         const track = new TrackNode({uri, title: name, sortOrder: -20});
         ctx.workspace.addChildInOrder(track);