ui: remove last few uses of ref/ref_type for counters and slices

This is in preparation for the removal of ref/ref_type from these
tables.

Context: go/perfetto-tp-refactor
Bug: 135177627
Change-Id: I3e157cc66fa2962a1b957b3e4936685aa476d708
diff --git a/ui/src/common/engine.ts b/ui/src/common/engine.ts
index d9f6760..b7734e3 100644
--- a/ui/src/common/engine.ts
+++ b/ui/src/common/engine.ts
@@ -101,8 +101,11 @@
 
   async getNumberOfGpus(): Promise<number> {
     if (!this._numGpus) {
-      const result = await this.query(
-          'select count(distinct(arg_set_id)) as gpuCount from counters where name = "gpufreq";');
+      const result = await this.query(`
+        select count(distinct(gpu_id)) as gpuCount
+        from gpu_counter_track
+        where name = 'gpufreq';
+      `);
       this._numGpus = +result.columns[0].longValues![0];
     }
     return this._numGpus;
diff --git a/ui/src/common/search_data.ts b/ui/src/common/search_data.ts
index 79b7bbf..0969c1d 100644
--- a/ui/src/common/search_data.ts
+++ b/ui/src/common/search_data.ts
@@ -23,6 +23,6 @@
   tsStarts: Float64Array;
   utids: Float64Array;
   trackIds: string[];
-  refTypes: string[];
+  sources: string[];
   totalResults: number;
 }
diff --git a/ui/src/controller/search_controller.ts b/ui/src/controller/search_controller.ts
index 0edaa53..5e29627 100644
--- a/ui/src/controller/search_controller.ts
+++ b/ui/src/controller/search_controller.ts
@@ -55,7 +55,7 @@
     await this.query(`create virtual table search_summary_sched_span using
       span_join(sched PARTITIONED cpu, search_summary_window);`);
     await this.query(`create virtual table search_summary_slice_span using
-      span_join(slice PARTITIONED ref_type  ref, search_summary_window);`);
+      span_join(slice PARTITIONED track_id, search_summary_window);`);
   }
 
   run() {
@@ -92,7 +92,7 @@
         sliceIds: new Float64Array(0),
         tsStarts: new Float64Array(0),
         utids: new Float64Array(0),
-        refTypes: [],
+        sources: [],
         trackIds: [],
         totalResults: 0,
       });
@@ -208,7 +208,7 @@
       row_id as slice_id,
       ts,
       'cpu' as source,
-      cpu as ref,
+      cpu as source_id,
       utid
     from sched where utid in (${utids.join(',')})
     union
@@ -216,7 +216,7 @@
       slice_id,
       ts,
       'track' as source,
-      track_id as ref,
+      track_id as source_id,
       0 as utid
       from slice
       inner join track on slice.track_id = track.id
@@ -230,19 +230,19 @@
       tsStarts: new Float64Array(numRows),
       utids: new Float64Array(numRows),
       trackIds: [],
-      refTypes: [],
+      sources: [],
       totalResults: +numRows,
     };
 
     const columns = rawResult.columns;
     for (let row = 0; row < numRows; row++) {
       const source = columns[2].stringValues![row];
-      const ref = +columns[3].longValues![row];
+      const sourceId = +columns[3].longValues![row];
       let trackId = undefined;
       if (source === 'cpu') {
-        trackId = cpuToTrackId.get(ref);
+        trackId = cpuToTrackId.get(sourceId);
       } else if (source === 'track') {
-        trackId = engineTrackIdToTrackId.get(ref);
+        trackId = engineTrackIdToTrackId.get(sourceId);
       }
 
       if (trackId === undefined) {
@@ -251,7 +251,7 @@
       }
 
       searchResults.trackIds.push(trackId);
-      searchResults.refTypes.push(source);
+      searchResults.sources.push(source);
       searchResults.sliceIds[row] = +columns[0].longValues![row];
       searchResults.tsStarts[row] = +columns[1].longValues![row];
       searchResults.utids[row] = +columns[4].longValues![row];
diff --git a/ui/src/controller/trace_controller.ts b/ui/src/controller/trace_controller.ts
index 8125125..9bb47d5 100644
--- a/ui/src/controller/trace_controller.ts
+++ b/ui/src/controller/trace_controller.ts
@@ -307,10 +307,10 @@
     //  }));
     //}
     const maxCpuFreq = await engine.query(`
-     select max(value)
-     from counter c
-     inner join cpu_counter_track t on c.track_id = t.id
-     where name = 'cpufreq';
+      select max(value)
+      from counter c
+      inner join cpu_counter_track t on c.track_id = t.id
+      where name = 'cpufreq';
     `);
 
     const cpus = await engine.getCpus();
@@ -410,18 +410,19 @@
     }
 
     const maxGpuFreq = await engine.query(`
-     select max(value)
-     from counters
-     where name = 'gpufreq';
+      select max(value)
+      from counter c
+      inner join gpu_counter_track t on c.track_id = t.id
+      where name = 'gpufreq';
     `);
 
     for (let gpu = 0; gpu < numGpus; gpu++) {
       // Only add a gpu freq track if we have
       // gpu freq data.
       const freqExists = await engine.query(`
-        select value
-        from counters
-        where name = 'gpufreq' and ref = ${gpu}
+        select id
+        from gpu_counter_track
+        where name = 'gpufreq' and gpu_id = ${gpu}
         limit 1;
       `);
       if (freqExists.numRecords > 0) {
@@ -432,6 +433,7 @@
           trackGroup: SCROLLING_TRACK_GROUP,
           config: {
             gpu,
+            trackId: +freqExists.columns[0].longValues![0],
             maximumValue: +maxGpuFreq.columns[0].doubleValues![0],
           }
         });
diff --git a/ui/src/frontend/globals.ts b/ui/src/frontend/globals.ts
index 57389ad..c31ca65 100644
--- a/ui/src/frontend/globals.ts
+++ b/ui/src/frontend/globals.ts
@@ -101,7 +101,7 @@
     tsStarts: new Float64Array(0),
     utids: new Float64Array(0),
     trackIds: [],
-    refTypes: [],
+    sources: [],
     totalResults: 0,
   };
   searchSummary: SearchSummary = {
@@ -257,7 +257,7 @@
       tsStarts: new Float64Array(0),
       utids: new Float64Array(0),
       trackIds: [],
-      refTypes: [],
+      sources: [],
       totalResults: 0,
     };
   }
diff --git a/ui/src/frontend/search_handler.ts b/ui/src/frontend/search_handler.ts
index 0e726a8..4db56c6 100644
--- a/ui/src/frontend/search_handler.ts
+++ b/ui/src/frontend/search_handler.ts
@@ -70,13 +70,13 @@
 function selectCurrentSearchResult() {
   const state = globals.frontendLocalState;
   const searchIndex = state.searchIndex;
-  const refType = globals.currentSearchResults.refTypes[searchIndex];
+  const source = globals.currentSearchResults.sources[searchIndex];
   const currentId = globals.currentSearchResults.sliceIds[searchIndex];
   const trackId = globals.currentSearchResults.trackIds[searchIndex];
 
   if (currentId === undefined) return;
 
-  if (refType === 'cpu') {
+  if (source === 'cpu') {
     globals.dispatch(Actions.selectSlice({id: currentId, trackId}));
   } else {
     globals.dispatch(Actions.selectChromeSlice({id: currentId, trackId}));
diff --git a/ui/src/frontend/sidebar.ts b/ui/src/frontend/sidebar.ts
index 420ac57..4e7c3aa 100644
--- a/ui/src/frontend/sidebar.ts
+++ b/ui/src/frontend/sidebar.ts
@@ -54,10 +54,11 @@
   sum(dur * freq)/1e6 as mcycles
 from (
   select
-    ref as cpu,
+    cpu,
     value as freq,
-    lead(ts) over (partition by ref order by ts) - ts as dur
-  from counters
+    lead(ts) over (partition by cpu order by ts) - ts as dur
+  from counter
+  inner join cpu_counter_track on counter.track_id = cpu_counter_track.id
   where name = 'cpufreq'
 ) group by cpu, freq
 order by mcycles desc limit 32;`;
diff --git a/ui/src/tracks/gpu_freq/common.ts b/ui/src/tracks/gpu_freq/common.ts
index dd8665d..f860234 100644
--- a/ui/src/tracks/gpu_freq/common.ts
+++ b/ui/src/tracks/gpu_freq/common.ts
@@ -27,5 +27,7 @@
 
 export interface Config {
   gpu: number;
+  trackId: number;
   maximumValue?: number;
-  minimumValue?: number;}
+  minimumValue?: number;
+}
diff --git a/ui/src/tracks/gpu_freq/controller.ts b/ui/src/tracks/gpu_freq/controller.ts
index 6fa2e39..76a35fd 100644
--- a/ui/src/tracks/gpu_freq/controller.ts
+++ b/ui/src/tracks/gpu_freq/controller.ts
@@ -37,9 +37,9 @@
 
     if (!this.setup) {
       const result = await this.query(`
-      select max(value) from
-        counters where name = 'gpufreq'
-        and ref = ${this.config.gpu}`);
+        select max(value)
+        from counter
+        where track_id = ${this.config.trackId}`);
       this.maximumValueSeen = +result.columns[0].doubleValues![0];
 
       await this.query(
@@ -49,25 +49,22 @@
           as select
             ts,
             lead(ts) over (order by ts) - ts as dur,
-            ref as gpu,
             name as freq_name,
             value as freq_value
-          from counters
-          where name = 'gpufreq'
-            and ref = ${this.config.gpu}
-            and ref_type = 'gpu';
+          from counter
+          where track_id = ${this.config.trackId};
       `);
 
       await this.query(`create virtual table ${this.tableName('span_activity')}
-      using span_join(${this.tableName('freq')} PARTITIONED gpu,
-                      ${this.tableName('window')});`);
+        using span_join(${this.tableName('freq')}, ${
+          this.tableName('window')});`);
 
-      await this.query(`create view ${this.tableName('activity')}
-      as select
-        ts,
-        dur,
-        quantum_ts,
-        freq_value as freq
+      await this.query(`create view ${this.tableName('activity')} as
+        select
+          ts,
+          dur,
+          quantum_ts,
+          freq_value as freq
         from ${this.tableName('span_activity')};
       `);
 
@@ -75,9 +72,9 @@
     }
 
     this.query(`update ${this.tableName('window')} set
-    window_start = ${startNs},
-    window_dur = ${Math.max(1, endNs - startNs)},
-    quantum = 0`);
+      window_start = ${startNs},
+      window_dur = ${Math.max(1, endNs - startNs)},
+      quantum = 0`);
 
     const result = await this.engine.queryOneRow(`select count(*)
       from ${this.tableName('activity')}`);