Merge "tp: fix various issues around CPU table" into main
diff --git a/src/trace_processor/perfetto_sql/stdlib/prelude/tables_views.sql b/src/trace_processor/perfetto_sql/stdlib/prelude/tables_views.sql
index 74ef1df..402ac52 100644
--- a/src/trace_processor/perfetto_sql/stdlib/prelude/tables_views.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/prelude/tables_views.sql
@@ -12,9 +12,11 @@
 -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
-
 -- Contains information of CPUs seen during the trace.
 CREATE PERFETTO VIEW cpu (
+  -- Unique identifier for this CPU. Identical to |ucpu|, prefer using |ucpu|
+  -- instead.
+  id UINT,
   -- Unique identifier for this CPU. Isn't equal to |cpu| for remote machines
   -- and is equal to |cpu| for the host machine.
   ucpu UINT,
@@ -30,6 +32,7 @@
   machine_id UINT
 ) AS
 SELECT
+  id,
   id AS ucpu,
   cpu,
   type AS type,
@@ -45,26 +48,19 @@
 CREATE PERFETTO VIEW cpu_frequencies (
   -- Unique identifier for this cpu frequency.
   id UINT,
-  -- The name of the "most-specific" child table containing this row.
-  type STRING,
-  -- The CPU identifier.
+  -- The CPU for this frequency, meaningful only in single machine traces.
+  -- For multi-machine, join with the `cpu` table on `ucpu` to get the CPU
+  -- identifier of each machine.
   cpu UINT,
   -- CPU frequency in KHz.
   freq UINT,
-  -- Machine identifier, non-null for CPUs on a remote machine.
-  machine_id UINT,
-  -- The unique CPU identifier that the slice executed on.
+  -- The CPU that the slice executed on (meaningful only in single machine
+  -- traces). For multi-machine, join with the `cpu` table on `ucpu` to get the
+  -- CPU identifier of each machine.
   ucpu UINT
 ) AS
-SELECT
-  __intrinsic_cpu_freq.id,
-  __intrinsic_cpu_freq.type AS type,
-  cpu,
-  freq,
-  machine_id,
-  cpu.ucpu
-FROM
-  __intrinsic_cpu_freq LEFT JOIN cpu USING (ucpu);
+SELECT id, ucpu AS cpu, freq, ucpu
+FROM __intrinsic_cpu_freq;
 
 -- This table holds slices with kernel thread scheduling information. These
 -- slices are collected when the Linux "ftrace" data source is used with the
@@ -289,4 +285,4 @@
   ucpu,
   upid
 FROM
-  __intrinsic_sched_upid;
+  __intrinsic_sched_upid;
\ No newline at end of file
diff --git a/test/trace_processor/diff_tests/parser/parsing/tests.py b/test/trace_processor/diff_tests/parser/parsing/tests.py
index 0112f55..988a430 100644
--- a/test/trace_processor/diff_tests/parser/parsing/tests.py
+++ b/test/trace_processor/diff_tests/parser/parsing/tests.py
@@ -1394,8 +1394,9 @@
         query="""
         SELECT
           freq,
-          GROUP_CONCAT(cpu) AS cpus
+          GROUP_CONCAT(cpu.cpu) AS cpus
         FROM cpu_frequencies
+        JOIN cpu using (ucpu)
         WHERE machine_id is not NULL
         GROUP BY freq
         ORDER BY freq;
diff --git a/test/trace_processor/diff_tests/tables/tests_sched.py b/test/trace_processor/diff_tests/tables/tests_sched.py
index d174e3f..7944b2d 100644
--- a/test/trace_processor/diff_tests/tables/tests_sched.py
+++ b/test/trace_processor/diff_tests/tables/tests_sched.py
@@ -721,16 +721,17 @@
     return DiffTestBlueprint(
         trace=DataPath('sched_switch_original.pb'),
         query="""
-        SELECT * from cpu
+        SELECT cpu, cluster_id
+        FROM cpu
         """,
         out=Csv("""
-        "ucpu","cpu","type","cluster_id","processor","machine_id"
-        0,0,"__intrinsic_cpu",0,"[NULL]","[NULL]"
-        1,1,"__intrinsic_cpu",0,"[NULL]","[NULL]"
-        2,2,"__intrinsic_cpu",0,"[NULL]","[NULL]"
-        3,3,"__intrinsic_cpu",0,"[NULL]","[NULL]"
-        4,4,"__intrinsic_cpu",0,"[NULL]","[NULL]"
-        7,7,"__intrinsic_cpu",0,"[NULL]","[NULL]"
+        "cpu","cluster_id"
+        0,0
+        1,0
+        2,0
+        3,0
+        4,0
+        7,0
         """))
 
   def test_sched_cpu_id_machine_id(self):
@@ -738,14 +739,16 @@
         trace=DataPath('sched_switch_original.pb'),
         trace_modifier=TraceInjector(['ftrace_events'], {'machine_id': 1001}),
         query="""
-        SELECT * from cpu
+        SELECT cpu, cluster_id, machine.raw_id as raw_machine_id
+        FROM cpu
+        JOIN machine ON cpu.machine_id = machine.id
         """,
         out=Csv("""
-        "ucpu","cpu","type","cluster_id","processor","machine_id"
-        4096,0,"__intrinsic_cpu",0,"[NULL]",1
-        4097,1,"__intrinsic_cpu",0,"[NULL]",1
-        4098,2,"__intrinsic_cpu",0,"[NULL]",1
-        4099,3,"__intrinsic_cpu",0,"[NULL]",1
-        4100,4,"__intrinsic_cpu",0,"[NULL]",1
-        4103,7,"__intrinsic_cpu",0,"[NULL]",1
+        "cpu","cluster_id","raw_machine_id"
+        0,0,1001
+        1,0,1001
+        2,0,1001
+        3,0,1001
+        4,0,1001
+        7,0,1001
         """))