Migrate v8 tables to new intrinsics / stdlib convention

Bug: b/283794416
Change-Id: Ia5060c5e15a6da4dd740d754e33d4aa4ec0d4b1a
diff --git a/Android.bp b/Android.bp
index 8d476ce..e9d7c45 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12086,6 +12086,7 @@
         "src/trace_processor/perfetto_sql/stdlib/slices/slices.sql",
         "src/trace_processor/perfetto_sql/stdlib/slices/with_context.sql",
         "src/trace_processor/perfetto_sql/stdlib/time/conversion.sql",
+        "src/trace_processor/perfetto_sql/stdlib/v8/jit.sql",
     ],
     cmd: "$(location tools/gen_amalgamated_sql.py) --namespace=stdlib --cpp-out=$(out) $(in)",
     out: [
diff --git a/BUILD b/BUILD
index 1b3d23f..84baca4 100644
--- a/BUILD
+++ b/BUILD
@@ -2541,6 +2541,14 @@
     ],
 )
 
+# GN target: //src/trace_processor/perfetto_sql/stdlib/v8:v8
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_stdlib_v8_v8",
+    srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/v8/jit.sql",
+    ],
+)
+
 # GN target: //src/trace_processor/perfetto_sql/stdlib:stdlib
 perfetto_cc_amalgamated_sql(
     name = "src_trace_processor_perfetto_sql_stdlib_stdlib",
@@ -2562,6 +2570,7 @@
         ":src_trace_processor_perfetto_sql_stdlib_sched_utilization_utilization",
         ":src_trace_processor_perfetto_sql_stdlib_slices_slices",
         ":src_trace_processor_perfetto_sql_stdlib_time_time",
+        ":src_trace_processor_perfetto_sql_stdlib_v8_v8",
     ],
     outs = [
         "src/trace_processor/perfetto_sql/stdlib/stdlib.h",
diff --git a/src/trace_processor/importers/proto/v8_tracker.h b/src/trace_processor/importers/proto/v8_tracker.h
index 6da4d12..3d85377 100644
--- a/src/trace_processor/importers/proto/v8_tracker.h
+++ b/src/trace_processor/importers/proto/v8_tracker.h
@@ -111,7 +111,7 @@
     size_t operator()(const tables::V8JsFunctionTable::Row& v) const {
       return static_cast<size_t>(base::Hasher::Combine(
           v.name.raw_id(), v.v8_js_script_id.value, v.is_toplevel,
-          v.kind.raw_id(), v.line.value_or(0), v.column.value_or(0)));
+          v.kind.raw_id(), v.line.value_or(0), v.col.value_or(0)));
     }
   };
   base::FlatHashMap<tables::V8JsFunctionTable::Row,
diff --git a/src/trace_processor/perfetto_sql/stdlib/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/BUILD.gn
index c277966..69fa506 100644
--- a/src/trace_processor/perfetto_sql/stdlib/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/BUILD.gn
@@ -34,6 +34,7 @@
     "sched",
     "slices",
     "time",
+    "v8",
   ]
   generated_header = "stdlib.h"
   namespace = "stdlib"
diff --git a/src/trace_processor/perfetto_sql/stdlib/v8/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/v8/BUILD.gn
new file mode 100644
index 0000000..8bb2833
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/v8/BUILD.gn
@@ -0,0 +1,19 @@
+# Copyright (C) 2024 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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.
+
+import("../../../../../gn/perfetto_sql.gni")
+
+perfetto_sql_source_set("v8") {
+  sources = [ "jit.sql" ]
+}
diff --git a/src/trace_processor/perfetto_sql/stdlib/v8/jit.sql b/src/trace_processor/perfetto_sql/stdlib/v8/jit.sql
new file mode 100644
index 0000000..6765272
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/v8/jit.sql
@@ -0,0 +1,151 @@
+--
+-- Copyright 2024 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- 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.
+
+-- These are the tables for the V8 jit data source
+-- (protos/perfetto/trace/chrome/v8.proto).
+--
+-- All events are associated to a V8 isolate instance. There can be multiple
+-- instances associated to a given thread, although this is rare.
+--
+-- Generated code in V8 is allocated in the V8 heap (in a special executeable
+-- section), this means that code can be garbage collected (when no longer used)
+-- or can be moved around (e.g. during heap compactation). This means that a
+-- given callsite might correspond to function `A` at one point in time and to
+-- function `B` later on.
+-- In addition V8 code has various levels of optimization, so a function might
+-- have multiple associated code snippets.
+--
+-- V8 does not track code deletion, so we have to indirectly infer it by
+-- detecting code overlaps, if a newer code creation event overlaps with older
+-- code we need to asume that the old code was deleted. Code moves are logged,
+-- and there is an event to track those.
+
+-- A V8 Isolate instance. A V8 Isolate represents an isolated instance of the V8
+-- engine.
+CREATE PERFETTO VIEW v8_isolate(
+  -- Unique V8 isolate id.
+  v8_isolate_id UINT,
+  -- Process the isolate was created in.
+  upid UINT,
+  -- Internal id used by the v8 engine. Unique in a process.
+  internal_isolate_id UINT,
+  -- Absolute start address of the embedded code blob.
+  embedded_blob_code_start_address LONG,
+  -- Size in bytes of the embedded code blob.
+  embedded_blob_code_size LONG,
+  -- Base address of the code range if the isolate defines one.
+  code_range_base_address LONG,
+  -- Size of a code range if the isolate defines one.
+  code_range_size LONG,
+  -- Whether the code range for this Isolate is shared with others in the same
+  -- process. There is at max one such shared code range per process.
+  shared_code_range LONG,
+  -- Used when short builtin calls are enabled, where embedded builtins are
+  -- copied into the CodeRange so calls can be nearer.
+  embedded_blob_code_copy_start_address LONG
+) AS
+SELECT
+  id AS v8_isolate_id,
+  upid,
+  internal_isolate_id,
+  embedded_blob_code_start_address,
+  embedded_blob_code_size,
+  code_range_base_address,
+  code_range_size,
+  shared_code_range,
+  embedded_blob_code_copy_start_address
+FROM
+  __intrinsic_v8_isolate;
+
+
+-- Represents a script that was compiled to generate code. Some V8 code is
+-- generated out of scripts and will reference a V8Script other types of code
+-- will not (e.g. builtins).
+CREATE PERFETTO VIEW v8_js_script (
+  -- Unique V8 JS script id.
+  v8_js_script_id UINT,
+  -- V8 isolate this script belongs to (joinable with v8_isolate.v8_isolate_id).
+  v8_isolate_id UINT,
+  -- Script id used by the V8 engine.
+  internal_script_id UINT,
+  -- Script type.
+  script_type STRING,
+  -- Script name.
+  name STRING,
+  -- Actual contents of the script.
+  source STRING
+) AS
+SELECT
+  id AS v8_js_script_id,
+  v8_isolate_id,
+  internal_script_id,
+  script_type,
+  name,
+  source
+FROM
+  __intrinsic_v8_js_script;
+
+
+-- Represents one WASM script.
+CREATE PERFETTO VIEW v8_wasm_script (
+  -- Unique V8 WASM script id.
+  v8_wasm_script_id UINT,
+  -- V8 Isolate this script belongs to (joinable with v8_isolate.v8_isolate_id).
+  v8_isolate_id UINT,
+  -- Script id used by the V8 engine.
+  internal_script_id UINT,
+  -- URL of the source.
+  url STRING,
+  -- Actual contents of the script.
+  source STRING
+) AS
+SELECT
+  id AS v8_wasm_script_id,
+  v8_isolate_id,
+  internal_script_id,
+  url,
+  source
+FROM
+  __intrinsic_v8_wasm_script;
+
+
+-- Represents a v8 Javascript function.
+CREATE PERFETTO VIEW v8_js_function (
+  -- Unique V8 JS function id.
+  v8_js_function_id UINT,
+  -- Function name.
+  name STRING,
+  -- Script where the function is defined (joinable with
+  -- v8_js_script.v8_js_script_id).
+  v8_js_script_id UINT,
+  -- Whether this function represents the top level script.
+  is_toplevel BOOL,
+  -- Function kind (e.g. regular function or constructor).
+  kind STRING,
+  -- Line in script where function is defined. Starts at 1.
+  line UINT,
+  -- Column in script where function is defined. Starts at 1.
+  col UINT
+) AS
+SELECT
+  id AS v8_js_function_id,
+  name,
+  v8_js_script_id,
+  is_toplevel,
+  kind,
+  line,
+  col
+FROM
+  __intrinsic_v8_js_function;
diff --git a/src/trace_processor/tables/v8_tables.py b/src/trace_processor/tables/v8_tables.py
index 8fb7c01..76a8cda 100644
--- a/src/trace_processor/tables/v8_tables.py
+++ b/src/trace_processor/tables/v8_tables.py
@@ -35,7 +35,7 @@
 V8_ISOLATE = Table(
     python_module=__file__,
     class_name='V8IsolateTable',
-    sql_name='v8_isolate',
+    sql_name='__intrinsic_v8_isolate',
     columns=[
         C('upid', CppUint32()),
         C('internal_isolate_id', CppInt32()),
@@ -45,7 +45,6 @@
         C('code_range_size', CppOptional(CppInt64())),
         C('shared_code_range', CppOptional(CppBool())),
         C('embedded_blob_code_copy_start_address', CppOptional(CppInt64())),
-        C('v8_isolate_id', Alias('id')),
     ],
     tabledoc=TableDoc(
         doc='Represents one Isolate instance',
@@ -72,8 +71,6 @@
                 'Used when short builtin calls are enabled, where embedded'
                 ' builtins are copied into the CodeRange so calls can be'
                 ' nearer.',
-            'v8_isolate_id':
-                'Alias for id. Makes joins easier',
         },
     ),
 )
@@ -81,14 +78,13 @@
 V8_JS_SCRIPT = Table(
     python_module=__file__,
     class_name='V8JsScriptTable',
-    sql_name='v8_js_script',
+    sql_name='__intrinsic_v8_js_script',
     columns=[
         C('v8_isolate_id', CppTableId(V8_ISOLATE)),
         C('internal_script_id', CppInt32()),
         C('script_type', CppString()),
         C('name', CppString()),
         C('source', CppOptional(CppString())),
-        C('v8_js_script_id', Alias('id')),
     ],
     tabledoc=TableDoc(
         doc='Represents one Javascript script',
@@ -99,7 +95,6 @@
             'script_type': '',
             'name': '',
             'source': 'Actual contents of the script.',
-            'v8_js_script_id': 'Alias for id. Makes joins easier',
         },
     ),
 )
@@ -107,13 +102,12 @@
 V8_WASM_SCRIPT = Table(
     python_module=__file__,
     class_name='V8WasmScriptTable',
-    sql_name='v8_wasm_script',
+    sql_name='__intrinsic_v8_wasm_script',
     columns=[
         C('v8_isolate_id', CppTableId(V8_ISOLATE)),
         C('internal_script_id', CppInt32()),
         C('url', CppString()),
         C('source', CppOptional(CppString())),
-        C('v8_wasm_script_id', Alias('id')),
     ],
     tabledoc=TableDoc(
         doc='Represents one WASM script',
@@ -123,7 +117,6 @@
             'internal_script_id': 'Script id used by the V8 engine',
             'url': 'URL of the source',
             'source': 'Actual contents of the script.',
-            'v8_wasm_script_id': 'Alias for id. Makes joins easier',
         },
     ),
 )
@@ -131,15 +124,14 @@
 V8_JS_FUNCTION = Table(
     python_module=__file__,
     class_name='V8JsFunctionTable',
-    sql_name='v8_js_function',
+    sql_name='__intrinsic_v8_js_function',
     columns=[
         C('name', CppString()),
         C('v8_js_script_id', CppTableId(V8_JS_SCRIPT)),
         C('is_toplevel', CppBool()),
         C('kind', CppString()),
         C('line', CppOptional(CppUint32())),
-        C('column', CppOptional(CppUint32())),
-        C('v8_js_function_id', Alias('id')),
+        C('col', CppOptional(CppUint32())),
     ],
     tabledoc=TableDoc(
         doc='Represents a v8 Javascript function',
@@ -158,10 +150,8 @@
                 'Function kind (e.g. regular function or constructor)',
             'line':
                 'Line in script where function is defined. Starts at 1',
-            'column':
+            'col':
                 'Column in script where function is defined. Starts at 1',
-            'v8_js_function_id':
-                'Alias for id. Makes joins easier',
         },
     ),
 )