Merge "tp: remove machine_id from the counter table" into main
diff --git a/src/trace_processor/importers/common/event_tracker.cc b/src/trace_processor/importers/common/event_tracker.cc
index 43cf710..a38d776 100644
--- a/src/trace_processor/importers/common/event_tracker.cc
+++ b/src/trace_processor/importers/common/event_tracker.cc
@@ -16,20 +16,18 @@
 
 #include "src/trace_processor/importers/common/event_tracker.h"
 
-#include <math.h>
+#include <cinttypes>
+#include <cstdint>
 #include <optional>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/ext/base/utils.h"
 #include "src/trace_processor/importers/common/args_tracker.h"
-#include "src/trace_processor/importers/common/process_tracker.h"
 #include "src/trace_processor/importers/common/track_tracker.h"
 #include "src/trace_processor/storage/stats.h"
+#include "src/trace_processor/storage/trace_storage.h"
 #include "src/trace_processor/types/trace_processor_context.h"
-#include "src/trace_processor/types/variadic.h"
 
-namespace perfetto {
-namespace trace_processor {
+namespace perfetto::trace_processor {
 
 EventTracker::EventTracker(TraceProcessorContext* context)
     : context_(context) {}
@@ -65,9 +63,7 @@
   max_timestamp_ = timestamp;
 
   auto* counter_values = context_->storage->mutable_counter_table();
-  return counter_values
-      ->Insert({timestamp, track_id, value, {}, context_->machine_id()})
-      .id;
+  return counter_values->Insert({timestamp, track_id, value, {}}).id;
 }
 
 std::optional<CounterId> EventTracker::PushCounter(
@@ -106,5 +102,4 @@
   pending_upid_resolution_counter_.clear();
 }
 
-}  // namespace trace_processor
-}  // namespace perfetto
+}  // namespace perfetto::trace_processor
diff --git a/src/trace_processor/tables/counter_tables.py b/src/trace_processor/tables/counter_tables.py
index 3a9e062..6892aec 100644
--- a/src/trace_processor/tables/counter_tables.py
+++ b/src/trace_processor/tables/counter_tables.py
@@ -35,25 +35,15 @@
         C('track_id', CppTableId(COUNTER_TRACK_TABLE)),
         C('value', CppDouble()),
         C('arg_set_id', CppOptional(CppUint32())),
-        C('machine_id', CppOptional(CppTableId(MACHINE_TABLE))),
     ],
     tabledoc=TableDoc(
         doc='''''',
         group='Events',
         columns={
-            'ts':
-                '''''',
-            'track_id':
-                '''''',
-            'value':
-                '''''',
-            'arg_set_id':
-                '''''',
-            'machine_id':
-                '''
-                  Machine identifier, non-null for counters from a remote
-                  machine.
-                ''',
+            'ts': '''''',
+            'track_id': '''''',
+            'value': '''''',
+            'arg_set_id': '''''',
         }))
 
 # Keep this list sorted.
diff --git a/src/trace_processor/trace_processor_impl.cc b/src/trace_processor/trace_processor_impl.cc
index fe25faf..337512d 100644
--- a/src/trace_processor/trace_processor_impl.cc
+++ b/src/trace_processor/trace_processor_impl.cc
@@ -808,6 +808,7 @@
   // Note: if adding a table here which might potentially contain many rows
   // (O(rows in sched/slice/counter)), then consider calling ShrinkToFit on
   // that table in TraceStorage::ShrinkToFitTables.
+  RegisterStaticTable(storage->machine_table());
   RegisterStaticTable(storage->arg_table());
   RegisterStaticTable(storage->raw_table());
   RegisterStaticTable(storage->ftrace_event_table());
diff --git a/test/trace_processor/diff_tests/parser/sched/tests.py b/test/trace_processor/diff_tests/parser/sched/tests.py
index 5568147..bc63ff9 100644
--- a/test/trace_processor/diff_tests/parser/sched/tests.py
+++ b/test/trace_processor/diff_tests/parser/sched/tests.py
@@ -83,28 +83,29 @@
           t.name,
           c.ts,
           c.value,
-          c.machine_id
+          m.raw_id as raw_machine_id
         FROM
           counter AS c
-        LEFT JOIN
+        JOIN
           counter_track AS t
           ON c.track_id = t.id
+        JOIN machine as m on t.machine_id = m.id
         WHERE
           name GLOB "Cpu ? Cap" OR name GLOB "Cpu ? Util" OR name GLOB "Cpu ? Nr Running"
         ORDER BY ts;
         """,
         out=Csv("""
-        "name","ts","value","machine_id"
-        "Cpu 6 Util",10000,1.000000,1
-        "Cpu 6 Cap",10000,1004.000000,1
-        "Cpu 6 Nr Running",10000,0.000000,1
-        "Cpu 7 Util",11000,1.000000,1
-        "Cpu 7 Cap",11000,1007.000000,1
-        "Cpu 7 Nr Running",11000,0.000000,1
-        "Cpu 4 Util",12000,43.000000,1
-        "Cpu 4 Cap",12000,760.000000,1
-        "Cpu 4 Nr Running",12000,0.000000,1
-        "Cpu 5 Util",13000,125.000000,1
-        "Cpu 5 Cap",13000,757.000000,1
-        "Cpu 5 Nr Running",13000,1.000000,1
+        "name","ts","value","raw_machine_id"
+        "Cpu 6 Util",10000,1.000000,1001
+        "Cpu 6 Cap",10000,1004.000000,1001
+        "Cpu 6 Nr Running",10000,0.000000,1001
+        "Cpu 7 Util",11000,1.000000,1001
+        "Cpu 7 Cap",11000,1007.000000,1001
+        "Cpu 7 Nr Running",11000,0.000000,1001
+        "Cpu 4 Util",12000,43.000000,1001
+        "Cpu 4 Cap",12000,760.000000,1001
+        "Cpu 4 Nr Running",12000,0.000000,1001
+        "Cpu 5 Util",13000,125.000000,1001
+        "Cpu 5 Cap",13000,757.000000,1001
+        "Cpu 5 Nr Running",13000,1.000000,1001
         """))
diff --git a/test/trace_processor/diff_tests/parser/simpleperf/perf_test.sql b/test/trace_processor/diff_tests/parser/simpleperf/perf_test.sql
index 9f93369..fad6c5c 100644
--- a/test/trace_processor/diff_tests/parser/simpleperf/perf_test.sql
+++ b/test/trace_processor/diff_tests/parser/simpleperf/perf_test.sql
@@ -12,8 +12,7 @@
       ts,
       track_id,
       IIF(lag_value IS NULL, value, value - lag_value) AS delta,
-      arg_set_id,
-      machine_id
+      arg_set_id
     FROM counter_delta_base
   )
 SELECT
diff --git a/test/trace_processor/diff_tests/parser/simpleperf/perf_with_add_counter_test.sql b/test/trace_processor/diff_tests/parser/simpleperf/perf_with_add_counter_test.sql
index b81beac..17ff666 100644
--- a/test/trace_processor/diff_tests/parser/simpleperf/perf_with_add_counter_test.sql
+++ b/test/trace_processor/diff_tests/parser/simpleperf/perf_with_add_counter_test.sql
@@ -12,8 +12,7 @@
       ts,
       track_id,
       IIF(lag_value IS NULL, value, value - lag_value) AS delta,
-      arg_set_id,
-      machine_id
+      arg_set_id
     FROM counter_delta_base
   ),
   named_counter AS (
diff --git a/test/trace_processor/diff_tests/parser/simpleperf/tests.py b/test/trace_processor/diff_tests/parser/simpleperf/tests.py
index 4f5a22b..73d756d 100644
--- a/test/trace_processor/diff_tests/parser/simpleperf/tests.py
+++ b/test/trace_processor/diff_tests/parser/simpleperf/tests.py
@@ -64,18 +64,17 @@
           name,
           unit,
           description,
-          perf_session_id,
           cpu,
           is_timebase
         FROM perf_counter_track
         ORDER BY perf_session_id, name, cpu;
         ''',
         out=Csv('''
-        "name","unit","description","perf_session_id","cpu","is_timebase"
-        "","","",0,2,1
-        "","","",0,6,1
-        "","","",0,7,1
-        "","","",0,16,1
+        "name","unit","description","cpu","is_timebase"
+        "","","",2,1
+        "","","",6,1
+        "","","",7,1
+        "","","",16,1
         '''))
 
   def test_perf_with_add_counter_tracks(self):
@@ -84,21 +83,17 @@
         query='''
         SELECT
           name,
-          parent_id,
-          source_arg_set_id,
-          machine_id,
           unit,
           description,
-          perf_session_id,
           cpu,
           is_timebase
         FROM perf_counter_track
         ORDER BY perf_session_id, name, cpu;
         ''',
         out=Csv('''
-        "name","parent_id","source_arg_set_id","machine_id","unit","description","perf_session_id","cpu","is_timebase"
-        "cpu-cycles","[NULL]","[NULL]","[NULL]","","",0,40,1
-        "instructions","[NULL]","[NULL]","[NULL]","","",0,40,0
+        "name","unit","description","cpu","is_timebase"
+        "cpu-cycles","","",40,1
+        "instructions","","",40,0
         '''))
 
   # simpleperf report -i perf.data --print-event-count --csv
diff --git a/test/trace_processor/diff_tests/syntax/table_tests.py b/test/trace_processor/diff_tests/syntax/table_tests.py
index 5785b6f..ca6a5d3 100644
--- a/test/trace_processor/diff_tests/syntax/table_tests.py
+++ b/test/trace_processor/diff_tests/syntax/table_tests.py
@@ -78,7 +78,6 @@
         3,"perfetto_table_info","track_id","uint32",0,0
         4,"perfetto_table_info","value","double",0,0
         5,"perfetto_table_info","arg_set_id","uint32",1,0
-        6,"perfetto_table_info","machine_id","uint32",1,0
         """))
 
   def test_perfetto_table_info_runtime_table(self):
diff --git a/test/trace_processor/diff_tests/tables/tests_counters.py b/test/trace_processor/diff_tests/tables/tests_counters.py
index 4cc79a0..4a543be 100644
--- a/test/trace_processor/diff_tests/tables/tests_counters.py
+++ b/test/trace_processor/diff_tests/tables/tests_counters.py
@@ -133,23 +133,25 @@
             ['ftrace_events', 'sys_stats', 'process_stats', 'process_tree'],
             {'machine_id': 1001}),
         query="""
-        SELECT ts, dur, machine_id
-        FROM experimental_counter_dur
+        SELECT ts, dur, m.raw_id as raw_machine_id
+        FROM experimental_counter_dur c
+        JOIN counter_track t on c.track_id = t.id
+        JOIN machine m on t.machine_id = m.id
         WHERE track_id IN (1, 2, 3)
         ORDER BY dur LIMIT 10;
         """,
         out=Csv("""
-        "ts","dur","machine_id"
-        100351738640,-1,1
-        100351738640,-1,1
-        100351738640,-1,1
-        70731059648,19510835,1
-        70731059648,19510835,1
-        70731059648,19510835,1
-        73727335051,23522762,1
-        73727335051,23522762,1
-        73727335051,23522762,1
-        86726132752,24487554,1
+        "ts","dur","raw_machine_id"
+        100351738640,-1,1001
+        100351738640,-1,1001
+        100351738640,-1,1001
+        70731059648,19510835,1001
+        70731059648,19510835,1001
+        70731059648,19510835,1001
+        73727335051,23522762,1001
+        73727335051,23522762,1001
+        73727335051,23522762,1001
+        86726132752,24487554,1001
         """))
 
   # Tests counter.machine_id and process_counter_track.machine.
@@ -174,7 +176,6 @@
               AND t.machine_id is not NULL
           )
           AND value != 17952.000000
-          AND counter.machine_id is not NULL
         LIMIT 20;
         """,
         out=Path('filter_row_vector_example_android_trace_30s.out'))
@@ -186,16 +187,16 @@
         query="""
         SELECT
           ts,
-          lead(ts, 1, ts) OVER (PARTITION BY name ORDER BY ts) - ts AS dur,
-          value, c.machine_id
+          lead(ts, 1, ts) OVER (PARTITION BY track_id ORDER BY ts) - ts AS dur,
+          value
         FROM counter c
-        JOIN cpu_counter_track t ON t.id = c.track_id
+        JOIN cpu_counter_track t on c.track_id = t.id
         WHERE cpu = 1;
         """,
         out=Csv("""
-        "ts","dur","value","machine_id"
-        1000,1,3000.000000,1
-        1001,0,4000.000000,1
+        "ts","dur","value"
+        1000,1,3000.000000
+        1001,0,4000.000000
         """))
 
   def test_synth_1_filter_counter_machine_id(self):
@@ -205,14 +206,14 @@
             ['ftrace_events', 'process_stats', 'process_tree'],
             {'machine_id': 1001}),
         query="""
-        SELECT COUNT(*), machine_id
+        SELECT COUNT(*)
         FROM counter
         WHERE
           track_id = 0;
         """,
         out=Csv("""
-        "COUNT(*)","machine_id"
-        2,1
+        "COUNT(*)"
+        2
         """))
 
   def test_memory_counters_machine_id(self):
@@ -222,9 +223,11 @@
             ['ftrace_events', 'sys_stats', 'process_stats', 'process_tree'],
             {'machine_id': 1001}),
         query="""
-        SELECT count(*), machine_id FROM counters WHERE -1 < ts group by machine_id;
+        SELECT count(*)
+        FROM counters
+        WHERE -1 < ts group by machine_id;
         """,
         out=Csv("""
-        "count(*)","machine_id"
-        98688,1
+        "count(*)"
+        98688
         """))