ui: remove repeated query for each process

Change-Id: Ie293d3943b6614d06c2bd8e88a2a0ed6a41a090e
diff --git a/ui/src/tracks/process_summary/index.ts b/ui/src/tracks/process_summary/index.ts
index ca6f598..70e22e6 100644
--- a/ui/src/tracks/process_summary/index.ts
+++ b/ui/src/tracks/process_summary/index.ts
@@ -20,7 +20,14 @@
   PluginContextTrace,
   PluginDescriptor,
 } from '../../public';
-import {NUM, NUM_NULL, STR, STR_NULL} from '../../trace_processor/query_result';
+import {
+  LONG_NULL,
+  NUM,
+  NUM_NULL,
+  STR,
+  STR_NULL,
+} from '../../trace_processor/query_result';
+import {assertExists} from '../../base/logging';
 
 import {
   Config as ProcessSchedulingTrackConfig,
@@ -87,10 +94,18 @@
       union
       select upid as upid, 0 as utid from heap_graph_object
     ),
-    schedSum as materialized (
-      select upid, sum(thread_total_dur) as total_dur
+    schedSummary as materialized (
+      select
+        upid,
+        sum(thread_total_dur) as total_dur,
+        max(thread_max_dur) as total_max_dur,
+        sum(thread_event_count) as total_event_count
       from (
-        select utid, sum(dur) as thread_total_dur
+        select
+          utid,
+          sum(dur) as thread_total_dur,
+          max(dur) as thread_max_dur,
+          count() as thread_event_count
         from sched where dur != -1 and utid != 0
         group by utid
       )
@@ -114,6 +129,8 @@
       the_tracks.upid,
       the_tracks.utid,
       total_dur as hasSched,
+      total_max_dur as schedMaxDur,
+      total_event_count as schedEventCount,
       hasHeapProfiles,
       process.pid as pid,
       thread.tid as tid,
@@ -135,7 +152,7 @@
          else 0
       end) as chromeProcessRank
     from candidateThreadsAndProcesses the_tracks
-    left join schedSum using(upid)
+    left join schedSummary using(upid)
     left join (
       select
         distinct(upid) as upid,
@@ -182,6 +199,8 @@
       threadName: STR_NULL,
       processName: STR_NULL,
       hasSched: NUM_NULL,
+      schedMaxDur: LONG_NULL,
+      schedEventCount: NUM_NULL,
       hasHeapProfiles: NUM_NULL,
       isDebuggable: NUM_NULL,
       chromeProcessLabels: STR,
@@ -192,6 +211,8 @@
       const upid = it.upid;
       const pid = it.pid;
       const hasSched = Boolean(it.hasSched);
+      const schedMaxDur = it.schedMaxDur;
+      const schedEventCount = it.schedEventCount;
       const isDebuggable = Boolean(it.isDebuggable);
 
       // Group by upid if present else by utid.
@@ -218,7 +239,13 @@
             tags: {
               isDebuggable,
             },
-            trackFactory: () => new ProcessSchedulingTrack(ctx.engine, config),
+            trackFactory: () =>
+              new ProcessSchedulingTrack(
+                ctx.engine,
+                config,
+                assertExists(schedMaxDur),
+                assertExists(schedEventCount),
+              ),
           });
         } else {
           const config: ProcessSummaryTrackConfig = {
diff --git a/ui/src/tracks/process_summary/process_scheduling_track.ts b/ui/src/tracks/process_summary/process_scheduling_track.ts
index 0edbf58..07400ca 100644
--- a/ui/src/tracks/process_summary/process_scheduling_track.ts
+++ b/ui/src/tracks/process_summary/process_scheduling_track.ts
@@ -59,15 +59,23 @@
   private utidHoveredInThisTrack = -1;
   private fetcher = new TimelineFetcher(this.onBoundsChange.bind(this));
   private maxCpu = 0;
-  private maxDur = 0n;
+  private maxDur;
+  private eventCount;
   private cachedBucketSize = BIMath.INT64_MAX;
   private engine: EngineProxy;
   private uuid = uuidv4();
   private config: Config;
 
-  constructor(engine: EngineProxy, config: Config) {
+  constructor(
+    engine: EngineProxy,
+    config: Config,
+    maxDur: duration,
+    eventCount: number,
+  ) {
     this.engine = engine;
     this.config = config;
+    this.maxDur = maxDur;
+    this.eventCount = eventCount;
   }
 
   // Returns a valid SQL table name with the given prefix that should be unique
@@ -88,17 +96,7 @@
     assertTrue(cpus.length > 0);
     this.maxCpu = Math.max(...cpus) + 1;
 
-    const result = (
-      await this.engine.query(`
-      select ifnull(max(dur), 0) as maxDur, count(1) as count
-      from ${this.tableName('process_sched')}
-    `)
-    ).iter({maxDur: LONG, count: NUM});
-    assertTrue(result.valid());
-    this.maxDur = result.maxDur;
-
-    const rowCount = result.count;
-    const bucketSize = calcCachedBucketSize(rowCount);
+    const bucketSize = calcCachedBucketSize(this.eventCount);
     if (bucketSize === undefined) {
       return;
     }