Merge "Use IDLE bit instead of referenced bit."
diff --git a/Android.bp b/Android.bp
index 7268285..320e7af 100644
--- a/Android.bp
+++ b/Android.bp
@@ -21,6 +21,7 @@
     "src/trace_processor/metrics/android/android_mem_lmk.sql",
     "src/trace_processor/metrics/android/android_mem_proc_counters.sql",
     "src/trace_processor/metrics/android/android_startup.sql",
+    "src/trace_processor/metrics/android/android_startup_cpu.sql",
     "src/trace_processor/metrics/android/android_startup_launches.sql",
     "src/trace_processor/metrics/android/android_task_state.sql",
     "src/trace_processor/metrics/android/heap_profile.sql",
@@ -99,7 +100,6 @@
     "src/protozero/scattered_heap_buffer.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
-    "src/public/trace_writer_base.cc",
     "src/tracing/core/chrome_config.cc",
     "src/tracing/core/commit_data_request.cc",
     "src/tracing/core/data_source_config.cc",
@@ -121,6 +121,7 @@
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/core/tracing_service_impl.cc",
     "src/tracing/core/virtual_destructors.cc",
+    "src/tracing/trace_writer_base.cc",
   ],
   shared_libs: [
     "libbase",
@@ -130,7 +131,6 @@
     "libunwindstack",
   ],
   static_libs: [
-    "libgtest_prod",
     "perfetto_src_tracing_ipc",
   ],
   init_rc: [
@@ -201,7 +201,6 @@
   ],
   static_libs: [
     "libasync_safe",
-    "libgtest_prod",
   ],
   defaults: [
     "perfetto_defaults",
@@ -280,7 +279,6 @@
     "src/protozero/scattered_heap_buffer.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
-    "src/public/trace_writer_base.cc",
     "src/traced/probes/android_log/android_log_data_source.cc",
     "src/traced/probes/filesystem/file_scanner.cc",
     "src/traced/probes/filesystem/fs_mount.cc",
@@ -336,13 +334,13 @@
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/core/tracing_service_impl.cc",
     "src/tracing/core/virtual_destructors.cc",
+    "src/tracing/trace_writer_base.cc",
   ],
   shared_libs: [
     "liblog",
     "libprotobuf-cpp-lite",
   ],
   static_libs: [
-    "libgtest_prod",
     "perfetto_src_tracing_ipc",
   ],
   generated_headers: [
@@ -465,7 +463,6 @@
     "src/protozero/scattered_heap_buffer.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
-    "src/public/trace_writer_base.cc",
     "src/tracing/core/chrome_config.cc",
     "src/tracing/core/commit_data_request.cc",
     "src/tracing/core/data_source_config.cc",
@@ -487,6 +484,7 @@
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/core/tracing_service_impl.cc",
     "src/tracing/core/virtual_destructors.cc",
+    "src/tracing/trace_writer_base.cc",
   ],
   shared_libs: [
     "libandroid",
@@ -498,7 +496,6 @@
     "libz",
   ],
   static_libs: [
-    "libgtest_prod",
     "perfetto_src_tracing_ipc",
   ],
   generated_headers: [
@@ -638,16 +635,6 @@
     "src/protozero/scattered_heap_buffer.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
-    "src/public/api_integrationtest.cc",
-    "src/public/data_source.cc",
-    "src/public/internal/in_process_tracing_backend.cc",
-    "src/public/internal/system_tracing_backend.cc",
-    "src/public/internal/tracing_muxer_impl.cc",
-    "src/public/platform.cc",
-    "src/public/platform_posix.cc",
-    "src/public/trace_writer_base.cc",
-    "src/public/tracing.cc",
-    "src/public/virtual_destructors.cc",
     "src/traced/probes/android_log/android_log_data_source.cc",
     "src/traced/probes/filesystem/file_scanner.cc",
     "src/traced/probes/filesystem/fs_mount.cc",
@@ -680,6 +667,7 @@
     "src/traced/probes/probes_producer.cc",
     "src/traced/probes/ps/process_stats_data_source.cc",
     "src/traced/probes/sys_stats/sys_stats_data_source.cc",
+    "src/tracing/api_integrationtest.cc",
     "src/tracing/core/chrome_config.cc",
     "src/tracing/core/commit_data_request.cc",
     "src/tracing/core/data_source_config.cc",
@@ -701,6 +689,15 @@
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/core/tracing_service_impl.cc",
     "src/tracing/core/virtual_destructors.cc",
+    "src/tracing/data_source.cc",
+    "src/tracing/internal/in_process_tracing_backend.cc",
+    "src/tracing/internal/system_tracing_backend.cc",
+    "src/tracing/internal/tracing_muxer_impl.cc",
+    "src/tracing/platform.cc",
+    "src/tracing/platform_posix.cc",
+    "src/tracing/trace_writer_base.cc",
+    "src/tracing/tracing.cc",
+    "src/tracing/virtual_destructors.cc",
     "test/end_to_end_integrationtest.cc",
     "test/fake_producer.cc",
     "test/task_runner_thread.cc",
@@ -716,7 +713,6 @@
   ],
   static_libs: [
     "libgmock",
-    "libgtest_prod",
     "perfetto_src_tracing_ipc",
   ],
   generated_headers: [
@@ -2747,7 +2743,6 @@
     "src/protozero/scattered_heap_buffer.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
-    "src/public/trace_writer_base.cc",
     "src/tracing/core/chrome_config.cc",
     "src/tracing/core/commit_data_request.cc",
     "src/tracing/core/data_source_config.cc",
@@ -2776,13 +2771,11 @@
     "src/tracing/ipc/service/consumer_ipc_service.cc",
     "src/tracing/ipc/service/producer_ipc_service.cc",
     "src/tracing/ipc/service/service_ipc_host_impl.cc",
+    "src/tracing/trace_writer_base.cc",
   ],
   shared_libs: [
     "libprotobuf-cpp-lite",
   ],
-  static_libs: [
-    "libgtest_prod",
-  ],
   export_include_dirs: [
     "include",
   ],
@@ -3039,7 +3032,6 @@
     "src/protozero/scattered_stream_writer_unittest.cc",
     "src/protozero/test/fake_scattered_buffer.cc",
     "src/protozero/test/protozero_conformance_unittest.cc",
-    "src/public/trace_writer_base.cc",
     "src/traced/probes/android_log/android_log_data_source.cc",
     "src/traced/probes/android_log/android_log_data_source_unittest.cc",
     "src/traced/probes/filesystem/file_scanner.cc",
@@ -3135,6 +3127,7 @@
     "src/tracing/test/mock_producer.cc",
     "src/tracing/test/test_shared_memory.cc",
     "src/tracing/test/tracing_integration_test.cc",
+    "src/tracing/trace_writer_base.cc",
     "tools/ftrace_proto_gen/ftrace_descriptor_gen.cc",
     "tools/ftrace_proto_gen/ftrace_proto_gen.cc",
     "tools/ftrace_proto_gen/ftrace_proto_gen_unittest.cc",
@@ -3156,7 +3149,6 @@
   ],
   static_libs: [
     "libgmock",
-    "libgtest_prod",
     "perfetto_src_tracing_ipc",
   ],
   generated_headers: [
@@ -3311,6 +3303,7 @@
     "src/trace_processor/string_pool.cc",
     "src/trace_processor/string_table.cc",
     "src/trace_processor/syscall_tracker.cc",
+    "src/trace_processor/systrace_trace_parser.cc",
     "src/trace_processor/table.cc",
     "src/trace_processor/thread_table.cc",
     "src/trace_processor/trace_processor.cc",
@@ -3333,7 +3326,6 @@
     "libprotobuf-cpp-lite",
   ],
   static_libs: [
-    "libgtest_prod",
     "libsqlite",
   ],
   generated_headers: [
@@ -3478,7 +3470,6 @@
     "src/protozero/scattered_heap_buffer.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
-    "src/public/trace_writer_base.cc",
     "src/tracing/core/chrome_config.cc",
     "src/tracing/core/commit_data_request.cc",
     "src/tracing/core/data_source_config.cc",
@@ -3500,6 +3491,7 @@
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/core/tracing_service_impl.cc",
     "src/tracing/core/virtual_destructors.cc",
+    "src/tracing/trace_writer_base.cc",
   ],
   shared_libs: [
     "liblog",
@@ -3507,7 +3499,6 @@
     "libz",
   ],
   static_libs: [
-    "libgtest_prod",
     "perfetto_src_tracing_ipc",
   ],
   generated_headers: [
diff --git a/BUILD b/BUILD
index 37588d9..b93362b 100644
--- a/BUILD
+++ b/BUILD
@@ -28,6 +28,7 @@
         "src/trace_processor/metrics/android/android_mem_lmk.sql",
         "src/trace_processor/metrics/android/android_mem_proc_counters.sql",
         "src/trace_processor/metrics/android/android_startup.sql",
+        "src/trace_processor/metrics/android/android_startup_cpu.sql",
         "src/trace_processor/metrics/android/android_startup_launches.sql",
         "src/trace_processor/metrics/android/android_task_state.sql",
         "src/trace_processor/metrics/android/heap_profile.sql",
@@ -46,21 +47,6 @@
 cc_library(
     name = "libprotozero",
     srcs = [
-        "src/base/event.cc",
-        "src/base/file_utils.cc",
-        "src/base/metatrace.cc",
-        "src/base/paged_memory.cc",
-        "src/base/pipe.cc",
-        "src/base/string_splitter.cc",
-        "src/base/string_utils.cc",
-        "src/base/string_view.cc",
-        "src/base/temp_file.cc",
-        "src/base/thread_checker.cc",
-        "src/base/thread_task_runner.cc",
-        "src/base/time.cc",
-        "src/base/unix_task_runner.cc",
-        "src/base/virtual_destructors.cc",
-        "src/base/watchdog_posix.cc",
         "src/protozero/message.cc",
         "src/protozero/message_handle.cc",
         "src/protozero/proto_decoder.cc",
@@ -70,39 +56,39 @@
     ],
     hdrs = [
         "include/perfetto/base/build_config.h",
-        "include/perfetto/base/circular_queue.h",
-        "include/perfetto/base/container_annotations.h",
-        "include/perfetto/base/event.h",
+        "include/perfetto/base/compiler.h",
         "include/perfetto/base/export.h",
-        "include/perfetto/base/file_utils.h",
-        "include/perfetto/base/gtest_prod_util.h",
-        "include/perfetto/base/hash.h",
         "include/perfetto/base/logging.h",
-        "include/perfetto/base/metatrace.h",
-        "include/perfetto/base/no_destructor.h",
-        "include/perfetto/base/optional.h",
-        "include/perfetto/base/paged_memory.h",
-        "include/perfetto/base/pipe.h",
-        "include/perfetto/base/scoped_file.h",
-        "include/perfetto/base/small_set.h",
-        "include/perfetto/base/string_splitter.h",
-        "include/perfetto/base/string_utils.h",
-        "include/perfetto/base/string_view.h",
-        "include/perfetto/base/string_writer.h",
         "include/perfetto/base/task_runner.h",
-        "include/perfetto/base/temp_file.h",
-        "include/perfetto/base/thread_annotations.h",
-        "include/perfetto/base/thread_checker.h",
-        "include/perfetto/base/thread_task_runner.h",
-        "include/perfetto/base/thread_utils.h",
-        "include/perfetto/base/time.h",
-        "include/perfetto/base/unix_socket.h",
-        "include/perfetto/base/unix_task_runner.h",
-        "include/perfetto/base/utils.h",
-        "include/perfetto/base/watchdog.h",
-        "include/perfetto/base/watchdog_noop.h",
-        "include/perfetto/base/watchdog_posix.h",
-        "include/perfetto/base/weak_ptr.h",
+        "include/perfetto/ext/base/circular_queue.h",
+        "include/perfetto/ext/base/container_annotations.h",
+        "include/perfetto/ext/base/event.h",
+        "include/perfetto/ext/base/file_utils.h",
+        "include/perfetto/ext/base/hash.h",
+        "include/perfetto/ext/base/metatrace.h",
+        "include/perfetto/ext/base/no_destructor.h",
+        "include/perfetto/ext/base/optional.h",
+        "include/perfetto/ext/base/paged_memory.h",
+        "include/perfetto/ext/base/pipe.h",
+        "include/perfetto/ext/base/scoped_file.h",
+        "include/perfetto/ext/base/small_set.h",
+        "include/perfetto/ext/base/string_splitter.h",
+        "include/perfetto/ext/base/string_utils.h",
+        "include/perfetto/ext/base/string_view.h",
+        "include/perfetto/ext/base/string_writer.h",
+        "include/perfetto/ext/base/temp_file.h",
+        "include/perfetto/ext/base/thread_annotations.h",
+        "include/perfetto/ext/base/thread_checker.h",
+        "include/perfetto/ext/base/thread_task_runner.h",
+        "include/perfetto/ext/base/thread_utils.h",
+        "include/perfetto/ext/base/time.h",
+        "include/perfetto/ext/base/unix_socket.h",
+        "include/perfetto/ext/base/unix_task_runner.h",
+        "include/perfetto/ext/base/utils.h",
+        "include/perfetto/ext/base/watchdog.h",
+        "include/perfetto/ext/base/watchdog_noop.h",
+        "include/perfetto/ext/base/watchdog_posix.h",
+        "include/perfetto/ext/base/weak_ptr.h",
         "include/perfetto/protozero/contiguous_memory_range.h",
         "include/perfetto/protozero/field.h",
         "include/perfetto/protozero/message.h",
@@ -113,9 +99,6 @@
         "include/perfetto/protozero/scattered_stream_null_delegate.h",
         "include/perfetto/protozero/scattered_stream_writer.h",
     ],
-    deps = [
-        "//third_party/perfetto/google:gtest_prod",
-    ],
 )
 
 # GN target: //src/protozero/protoc_plugin:protoc_plugin
@@ -242,6 +225,7 @@
         "src/trace_processor/span_join_operator_table.h",
         "src/trace_processor/sql_stats_table.cc",
         "src/trace_processor/sql_stats_table.h",
+        "src/trace_processor/sqlite.h",
         "src/trace_processor/sqlite3_str_split.cc",
         "src/trace_processor/sqlite3_str_split.h",
         "src/trace_processor/sqlite_utils.h",
@@ -264,6 +248,9 @@
         "src/trace_processor/syscalls_aarch64.h",
         "src/trace_processor/syscalls_armeabi.h",
         "src/trace_processor/syscalls_x86_64.h",
+        "src/trace_processor/systrace_trace_parser.cc",
+        "src/trace_processor/systrace_trace_parser.h",
+        "src/trace_processor/systrace_utils.h",
         "src/trace_processor/table.cc",
         "src/trace_processor/table.h",
         "src/trace_processor/thread_table.cc",
@@ -286,39 +273,40 @@
     ],
     hdrs = [
         "include/perfetto/base/build_config.h",
-        "include/perfetto/base/circular_queue.h",
-        "include/perfetto/base/container_annotations.h",
-        "include/perfetto/base/event.h",
+        "include/perfetto/base/compiler.h",
         "include/perfetto/base/export.h",
-        "include/perfetto/base/file_utils.h",
-        "include/perfetto/base/gtest_prod_util.h",
-        "include/perfetto/base/hash.h",
         "include/perfetto/base/logging.h",
-        "include/perfetto/base/metatrace.h",
-        "include/perfetto/base/no_destructor.h",
-        "include/perfetto/base/optional.h",
-        "include/perfetto/base/paged_memory.h",
-        "include/perfetto/base/pipe.h",
-        "include/perfetto/base/scoped_file.h",
-        "include/perfetto/base/small_set.h",
-        "include/perfetto/base/string_splitter.h",
-        "include/perfetto/base/string_utils.h",
-        "include/perfetto/base/string_view.h",
-        "include/perfetto/base/string_writer.h",
         "include/perfetto/base/task_runner.h",
-        "include/perfetto/base/temp_file.h",
-        "include/perfetto/base/thread_annotations.h",
-        "include/perfetto/base/thread_checker.h",
-        "include/perfetto/base/thread_task_runner.h",
-        "include/perfetto/base/thread_utils.h",
-        "include/perfetto/base/time.h",
-        "include/perfetto/base/unix_socket.h",
-        "include/perfetto/base/unix_task_runner.h",
-        "include/perfetto/base/utils.h",
-        "include/perfetto/base/watchdog.h",
-        "include/perfetto/base/watchdog_noop.h",
-        "include/perfetto/base/watchdog_posix.h",
-        "include/perfetto/base/weak_ptr.h",
+        "include/perfetto/ext/base/circular_queue.h",
+        "include/perfetto/ext/base/container_annotations.h",
+        "include/perfetto/ext/base/event.h",
+        "include/perfetto/ext/base/file_utils.h",
+        "include/perfetto/ext/base/hash.h",
+        "include/perfetto/ext/base/metatrace.h",
+        "include/perfetto/ext/base/no_destructor.h",
+        "include/perfetto/ext/base/optional.h",
+        "include/perfetto/ext/base/paged_memory.h",
+        "include/perfetto/ext/base/pipe.h",
+        "include/perfetto/ext/base/scoped_file.h",
+        "include/perfetto/ext/base/small_set.h",
+        "include/perfetto/ext/base/string_splitter.h",
+        "include/perfetto/ext/base/string_utils.h",
+        "include/perfetto/ext/base/string_view.h",
+        "include/perfetto/ext/base/string_writer.h",
+        "include/perfetto/ext/base/temp_file.h",
+        "include/perfetto/ext/base/thread_annotations.h",
+        "include/perfetto/ext/base/thread_checker.h",
+        "include/perfetto/ext/base/thread_task_runner.h",
+        "include/perfetto/ext/base/thread_utils.h",
+        "include/perfetto/ext/base/time.h",
+        "include/perfetto/ext/base/unix_socket.h",
+        "include/perfetto/ext/base/unix_task_runner.h",
+        "include/perfetto/ext/base/utils.h",
+        "include/perfetto/ext/base/watchdog.h",
+        "include/perfetto/ext/base/watchdog_noop.h",
+        "include/perfetto/ext/base/watchdog_posix.h",
+        "include/perfetto/ext/base/weak_ptr.h",
+        "include/perfetto/ext/traced/sys_stats_counters.h",
         "include/perfetto/protozero/contiguous_memory_range.h",
         "include/perfetto/protozero/field.h",
         "include/perfetto/protozero/message.h",
@@ -329,12 +317,11 @@
         "include/perfetto/protozero/scattered_stream_null_delegate.h",
         "include/perfetto/protozero/scattered_stream_writer.h",
         "include/perfetto/trace_processor/basic_types.h",
+        "include/perfetto/trace_processor/status.h",
         "include/perfetto/trace_processor/trace_processor.h",
-        "include/perfetto/traced/sys_stats_counters.h",
     ],
     deps = [
         "//third_party/perfetto:gen_merged_sql_metrics",
-        "//third_party/perfetto/google:gtest_prod",
         "//third_party/perfetto/google:jsoncpp",
         "//third_party/perfetto/protos:common_zero_cc_proto",
         "//third_party/perfetto/protos:config_zero_cc_proto",
@@ -362,39 +349,40 @@
     name = "trace_processor_shell",
     srcs = [
         "include/perfetto/base/build_config.h",
-        "include/perfetto/base/circular_queue.h",
-        "include/perfetto/base/container_annotations.h",
-        "include/perfetto/base/event.h",
+        "include/perfetto/base/compiler.h",
         "include/perfetto/base/export.h",
-        "include/perfetto/base/file_utils.h",
-        "include/perfetto/base/gtest_prod_util.h",
-        "include/perfetto/base/hash.h",
         "include/perfetto/base/logging.h",
-        "include/perfetto/base/metatrace.h",
-        "include/perfetto/base/no_destructor.h",
-        "include/perfetto/base/optional.h",
-        "include/perfetto/base/paged_memory.h",
-        "include/perfetto/base/pipe.h",
-        "include/perfetto/base/scoped_file.h",
-        "include/perfetto/base/small_set.h",
-        "include/perfetto/base/string_splitter.h",
-        "include/perfetto/base/string_utils.h",
-        "include/perfetto/base/string_view.h",
-        "include/perfetto/base/string_writer.h",
         "include/perfetto/base/task_runner.h",
-        "include/perfetto/base/temp_file.h",
-        "include/perfetto/base/thread_annotations.h",
-        "include/perfetto/base/thread_checker.h",
-        "include/perfetto/base/thread_task_runner.h",
-        "include/perfetto/base/thread_utils.h",
-        "include/perfetto/base/time.h",
-        "include/perfetto/base/unix_socket.h",
-        "include/perfetto/base/unix_task_runner.h",
-        "include/perfetto/base/utils.h",
-        "include/perfetto/base/watchdog.h",
-        "include/perfetto/base/watchdog_noop.h",
-        "include/perfetto/base/watchdog_posix.h",
-        "include/perfetto/base/weak_ptr.h",
+        "include/perfetto/ext/base/circular_queue.h",
+        "include/perfetto/ext/base/container_annotations.h",
+        "include/perfetto/ext/base/event.h",
+        "include/perfetto/ext/base/file_utils.h",
+        "include/perfetto/ext/base/hash.h",
+        "include/perfetto/ext/base/metatrace.h",
+        "include/perfetto/ext/base/no_destructor.h",
+        "include/perfetto/ext/base/optional.h",
+        "include/perfetto/ext/base/paged_memory.h",
+        "include/perfetto/ext/base/pipe.h",
+        "include/perfetto/ext/base/scoped_file.h",
+        "include/perfetto/ext/base/small_set.h",
+        "include/perfetto/ext/base/string_splitter.h",
+        "include/perfetto/ext/base/string_utils.h",
+        "include/perfetto/ext/base/string_view.h",
+        "include/perfetto/ext/base/string_writer.h",
+        "include/perfetto/ext/base/temp_file.h",
+        "include/perfetto/ext/base/thread_annotations.h",
+        "include/perfetto/ext/base/thread_checker.h",
+        "include/perfetto/ext/base/thread_task_runner.h",
+        "include/perfetto/ext/base/thread_utils.h",
+        "include/perfetto/ext/base/time.h",
+        "include/perfetto/ext/base/unix_socket.h",
+        "include/perfetto/ext/base/unix_task_runner.h",
+        "include/perfetto/ext/base/utils.h",
+        "include/perfetto/ext/base/watchdog.h",
+        "include/perfetto/ext/base/watchdog_noop.h",
+        "include/perfetto/ext/base/watchdog_posix.h",
+        "include/perfetto/ext/base/weak_ptr.h",
+        "include/perfetto/ext/traced/sys_stats_counters.h",
         "include/perfetto/protozero/contiguous_memory_range.h",
         "include/perfetto/protozero/field.h",
         "include/perfetto/protozero/message.h",
@@ -405,8 +393,8 @@
         "include/perfetto/protozero/scattered_stream_null_delegate.h",
         "include/perfetto/protozero/scattered_stream_writer.h",
         "include/perfetto/trace_processor/basic_types.h",
+        "include/perfetto/trace_processor/status.h",
         "include/perfetto/trace_processor/trace_processor.h",
-        "include/perfetto/traced/sys_stats_counters.h",
         "src/base/event.cc",
         "src/base/file_utils.cc",
         "src/base/metatrace.cc",
@@ -513,6 +501,7 @@
         "src/trace_processor/span_join_operator_table.h",
         "src/trace_processor/sql_stats_table.cc",
         "src/trace_processor/sql_stats_table.h",
+        "src/trace_processor/sqlite.h",
         "src/trace_processor/sqlite3_str_split.cc",
         "src/trace_processor/sqlite3_str_split.h",
         "src/trace_processor/sqlite_utils.h",
@@ -535,6 +524,9 @@
         "src/trace_processor/syscalls_aarch64.h",
         "src/trace_processor/syscalls_armeabi.h",
         "src/trace_processor/syscalls_x86_64.h",
+        "src/trace_processor/systrace_trace_parser.cc",
+        "src/trace_processor/systrace_trace_parser.h",
+        "src/trace_processor/systrace_utils.h",
         "src/trace_processor/table.cc",
         "src/trace_processor/table.h",
         "src/trace_processor/thread_table.cc",
@@ -558,7 +550,6 @@
     ],
     deps = [
         "//third_party/perfetto:gen_merged_sql_metrics",
-        "//third_party/perfetto/google:gtest_prod",
         "//third_party/perfetto/google:jsoncpp",
         "//third_party/perfetto/google:linenoise",
         "//third_party/perfetto/google:perfetto_version",
@@ -590,39 +581,40 @@
     name = "trace_to_text",
     srcs = [
         "include/perfetto/base/build_config.h",
-        "include/perfetto/base/circular_queue.h",
-        "include/perfetto/base/container_annotations.h",
-        "include/perfetto/base/event.h",
+        "include/perfetto/base/compiler.h",
         "include/perfetto/base/export.h",
-        "include/perfetto/base/file_utils.h",
-        "include/perfetto/base/gtest_prod_util.h",
-        "include/perfetto/base/hash.h",
         "include/perfetto/base/logging.h",
-        "include/perfetto/base/metatrace.h",
-        "include/perfetto/base/no_destructor.h",
-        "include/perfetto/base/optional.h",
-        "include/perfetto/base/paged_memory.h",
-        "include/perfetto/base/pipe.h",
-        "include/perfetto/base/scoped_file.h",
-        "include/perfetto/base/small_set.h",
-        "include/perfetto/base/string_splitter.h",
-        "include/perfetto/base/string_utils.h",
-        "include/perfetto/base/string_view.h",
-        "include/perfetto/base/string_writer.h",
         "include/perfetto/base/task_runner.h",
-        "include/perfetto/base/temp_file.h",
-        "include/perfetto/base/thread_annotations.h",
-        "include/perfetto/base/thread_checker.h",
-        "include/perfetto/base/thread_task_runner.h",
-        "include/perfetto/base/thread_utils.h",
-        "include/perfetto/base/time.h",
-        "include/perfetto/base/unix_socket.h",
-        "include/perfetto/base/unix_task_runner.h",
-        "include/perfetto/base/utils.h",
-        "include/perfetto/base/watchdog.h",
-        "include/perfetto/base/watchdog_noop.h",
-        "include/perfetto/base/watchdog_posix.h",
-        "include/perfetto/base/weak_ptr.h",
+        "include/perfetto/ext/base/circular_queue.h",
+        "include/perfetto/ext/base/container_annotations.h",
+        "include/perfetto/ext/base/event.h",
+        "include/perfetto/ext/base/file_utils.h",
+        "include/perfetto/ext/base/hash.h",
+        "include/perfetto/ext/base/metatrace.h",
+        "include/perfetto/ext/base/no_destructor.h",
+        "include/perfetto/ext/base/optional.h",
+        "include/perfetto/ext/base/paged_memory.h",
+        "include/perfetto/ext/base/pipe.h",
+        "include/perfetto/ext/base/scoped_file.h",
+        "include/perfetto/ext/base/small_set.h",
+        "include/perfetto/ext/base/string_splitter.h",
+        "include/perfetto/ext/base/string_utils.h",
+        "include/perfetto/ext/base/string_view.h",
+        "include/perfetto/ext/base/string_writer.h",
+        "include/perfetto/ext/base/temp_file.h",
+        "include/perfetto/ext/base/thread_annotations.h",
+        "include/perfetto/ext/base/thread_checker.h",
+        "include/perfetto/ext/base/thread_task_runner.h",
+        "include/perfetto/ext/base/thread_utils.h",
+        "include/perfetto/ext/base/time.h",
+        "include/perfetto/ext/base/unix_socket.h",
+        "include/perfetto/ext/base/unix_task_runner.h",
+        "include/perfetto/ext/base/utils.h",
+        "include/perfetto/ext/base/watchdog.h",
+        "include/perfetto/ext/base/watchdog_noop.h",
+        "include/perfetto/ext/base/watchdog_posix.h",
+        "include/perfetto/ext/base/weak_ptr.h",
+        "include/perfetto/ext/traced/sys_stats_counters.h",
         "include/perfetto/protozero/contiguous_memory_range.h",
         "include/perfetto/protozero/field.h",
         "include/perfetto/protozero/message.h",
@@ -633,8 +625,8 @@
         "include/perfetto/protozero/scattered_stream_null_delegate.h",
         "include/perfetto/protozero/scattered_stream_writer.h",
         "include/perfetto/trace_processor/basic_types.h",
+        "include/perfetto/trace_processor/status.h",
         "include/perfetto/trace_processor/trace_processor.h",
-        "include/perfetto/traced/sys_stats_counters.h",
         "src/base/event.cc",
         "src/base/file_utils.cc",
         "src/base/metatrace.cc",
@@ -741,6 +733,7 @@
         "src/trace_processor/span_join_operator_table.h",
         "src/trace_processor/sql_stats_table.cc",
         "src/trace_processor/sql_stats_table.h",
+        "src/trace_processor/sqlite.h",
         "src/trace_processor/sqlite3_str_split.cc",
         "src/trace_processor/sqlite3_str_split.h",
         "src/trace_processor/sqlite_utils.h",
@@ -763,6 +756,9 @@
         "src/trace_processor/syscalls_aarch64.h",
         "src/trace_processor/syscalls_armeabi.h",
         "src/trace_processor/syscalls_x86_64.h",
+        "src/trace_processor/systrace_trace_parser.cc",
+        "src/trace_processor/systrace_trace_parser.h",
+        "src/trace_processor/systrace_utils.h",
         "src/trace_processor/table.cc",
         "src/trace_processor/table.h",
         "src/trace_processor/thread_table.cc",
@@ -796,7 +792,6 @@
     ],
     deps = [
         "//third_party/perfetto:gen_merged_sql_metrics",
-        "//third_party/perfetto/google:gtest_prod",
         "//third_party/perfetto/google:jsoncpp",
         "//third_party/perfetto/google:perfetto_version",
         "//third_party/perfetto/protos:common_cc_proto",
diff --git a/BUILD.gn b/BUILD.gn
index 1173b01..54b73b6 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -58,7 +58,7 @@
       "protos/perfetto/config:merged_config",  # For syntax-checking the proto.
       "protos/perfetto/trace:merged_trace",  # For syntax-checking the proto.
       "src/ipc/protoc_plugin:ipc_plugin($host_toolchain)",
-      "src/public",
+      "src/tracing:client_api",
       "tools:protoc_helper",
     ]
     if (perfetto_build_standalone) {
@@ -72,6 +72,10 @@
         "tools/proto_to_cpp",
         "tools/trace_to_text",
         "tools/trace_to_text:trace_to_text_lite_host($host_toolchain)",
+
+        # The diff testing framework depends on these descriptors.
+        "protos/perfetto/metrics:descriptor",
+        "protos/perfetto/trace:descriptor",
       ]
       if (is_linux || is_android) {
         deps += [ "tools/skippy" ]
@@ -83,6 +87,13 @@
       }
     }
   }
+
+  # TODO(khokhlov): Remove this once the chrome dep is added. This is a
+  # temporary target to make sure that we don't regress trace processor support
+  # for the Windows build.
+  if (build_with_chromium) {
+    deps += [ "src/trace_processor:lib" ]
+  }
 }
 
 # TODO(primiano): temporary workaround to:
@@ -152,8 +163,8 @@
     deps = [
       "gn:default_deps",
       "gn:gtest_main",
-      "src/public:integrationtests",
       "src/traced/probes/ftrace:integrationtests",
+      "src/tracing:client_api_integrationtests",
       "test:end_to_end_integrationtests",
     ]
     if (perfetto_build_standalone && !is_android) {
@@ -177,7 +188,7 @@
       "gn:default_deps",
       "src/traced/probes",
       "src/traced/service",
-      "src/tracing:api",
+      "src/tracing:consumer_api_deprecated",
     ]
   }
 
@@ -188,7 +199,7 @@
     deps = [
       ":libperfetto",
       "gn:default_deps",
-      "include/perfetto/traced",
+      "include/perfetto/ext/traced",
     ]
     sources = [
       "src/traced/service/main.cc",
@@ -201,7 +212,7 @@
     deps = [
       ":libperfetto",
       "gn:default_deps",
-      "include/perfetto/traced",
+      "include/perfetto/ext/traced",
     ]
     sources = [
       "src/traced/probes/main.cc",
@@ -277,7 +288,7 @@
     configs -= [ "//build/config/compiler:chromium_code" ]
     configs += [ "//build/config/compiler:no_chromium_code" ]
     public_deps = [
-      "include/perfetto/tracing/core",
+      "include/perfetto/ext/tracing/core",
       "protos/perfetto/trace:zero",
       "protos/perfetto/trace/chrome:zero",
       "protos/perfetto/trace/interned_data:zero",
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index fa136fc..3c30708 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -15,134 +15,154 @@
 import itertools
 import subprocess
 
+
 def CheckChange(input, output):
-    # There apparently is no way to wrap strings in blueprints, so ignore long
-    # lines in them.
-    long_line_sources = lambda x: input.FilterSourceFile(
-            x, white_list=".*",
-            black_list=['Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$',
-                        'test/trace_processor/index$', 'BUILD$', 'protos/BUILD$'])
-    results = []
-    results += input.canned_checks.CheckDoNotSubmit(input, output)
-    results += input.canned_checks.CheckChangeHasNoTabs(input, output)
-    results += input.canned_checks.CheckLongLines(
-            input, output, 80, source_file_filter=long_line_sources)
-    results += input.canned_checks.CheckPatchFormatted(
-            input, output, check_js=True)
-    results += input.canned_checks.CheckGNFormatted(input, output)
-    results += CheckIncludeGuards(input, output)
-    results += CheckBuild(input, output)
-    results += CheckAndroidBlueprint(input, output)
-    results += CheckBinaryDescriptors(input, output)
-    results += CheckMergedTraceConfigProto(input, output)
-    results += CheckWhitelist(input, output)
-    return results
+  # There apparently is no way to wrap strings in blueprints, so ignore long
+  # lines in them.
+  def long_line_sources(x): return input.FilterSourceFile(
+      x, white_list=".*",
+      black_list=['Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$',
+                  'test/trace_processor/index$', 'BUILD$', 'protos/BUILD$'])
+  results = []
+  results += input.canned_checks.CheckDoNotSubmit(input, output)
+  results += input.canned_checks.CheckChangeHasNoTabs(input, output)
+  results += input.canned_checks.CheckLongLines(
+      input, output, 80, source_file_filter=long_line_sources)
+  results += input.canned_checks.CheckPatchFormatted(
+      input, output, check_js=True)
+  results += input.canned_checks.CheckGNFormatted(input, output)
+  results += CheckIncludeGuards(input, output)
+  results += CheckIncludeViolations(input, output)
+  results += CheckBuild(input, output)
+  results += CheckAndroidBlueprint(input, output)
+  results += CheckBinaryDescriptors(input, output)
+  results += CheckMergedTraceConfigProto(input, output)
+  results += CheckWhitelist(input, output)
+  return results
 
 
 def CheckChangeOnUpload(input_api, output_api):
-    return CheckChange(input_api, output_api)
+  return CheckChange(input_api, output_api)
 
 
 def CheckChangeOnCommit(input_api, output_api):
-    return CheckChange(input_api, output_api)
+  return CheckChange(input_api, output_api)
 
 
 def CheckBuild(input_api, output_api):
-    # If no GN files were modified, bail out.
-    def build_file_filter(x): return input_api.FilterSourceFile(
-        x,
-        white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_bazel'))
-    if not input_api.AffectedSourceFiles(build_file_filter):
-        return []
-
-    with open('BUILD') as f:
-        current_build = f.read()
-
-    new_build = subprocess.check_output(
-        ['tools/gen_bazel', '--output', '/dev/stdout', '--output-proto', '/dev/null'])
-
-    with open('protos/BUILD') as f:
-        current_proto_build = f.read()
-
-    new_proto_build = subprocess.check_output(
-        ['tools/gen_bazel', '--output', '/dev/null', '--output-proto', '/dev/stdout'])
-
-    if current_build != new_build or current_proto_build != new_proto_build:
-        return [
-            output_api.PresubmitError(
-                'BUILD and/or protos/BUILD is out of date. Please run tools/gen_bazel '
-                'to update it.')
-        ]
+  # If no GN files were modified, bail out.
+  def build_file_filter(x): return input_api.FilterSourceFile(
+      x,
+      white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_bazel'))
+  if not input_api.AffectedSourceFiles(build_file_filter):
     return []
 
+  with open('BUILD') as f:
+    current_build = f.read()
+
+  new_build = subprocess.check_output(
+      ['tools/gen_bazel', '--output', '/dev/stdout', '--output-proto',
+       '/dev/null'])
+
+  with open('protos/BUILD') as f:
+    current_proto_build = f.read()
+
+  new_proto_build = subprocess.check_output(
+      ['tools/gen_bazel', '--output', '/dev/null', '--output-proto',
+       '/dev/stdout'])
+
+  if current_build != new_build or current_proto_build != new_proto_build:
+    return [
+        output_api.PresubmitError(
+            'BUILD and/or protos/BUILD is out of date. ' +
+            'Please run tools/gen_bazel to update it.')
+    ]
+  return []
+
 
 def CheckAndroidBlueprint(input_api, output_api):
-    # If no GN files were modified, bail out.
-    build_file_filter = lambda x: input_api.FilterSourceFile(
-          x,
-          white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp'))
-    if not input_api.AffectedSourceFiles(build_file_filter):
-        return []
-
-    with open('Android.bp') as f:
-        current_blueprint = f.read()
-
-    new_blueprint = subprocess.check_output(
-        ['tools/gen_android_bp', '--output', '/dev/stdout'])
-
-    if current_blueprint != new_blueprint:
-        return [
-            output_api.PresubmitError(
-                'Android.bp is out of date. Please run tools/gen_android_bp '
-                'to update it.')
-        ]
+  # If no GN files were modified, bail out.
+  def build_file_filter(x): return input_api.FilterSourceFile(
+      x,
+      white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp'))
+  if not input_api.AffectedSourceFiles(build_file_filter):
     return []
 
+  with open('Android.bp') as f:
+    current_blueprint = f.read()
+
+  new_blueprint = subprocess.check_output(
+      ['tools/gen_android_bp', '--output', '/dev/stdout'])
+
+  if current_blueprint != new_blueprint:
+    return [
+        output_api.PresubmitError(
+            'Android.bp is out of date. Please run tools/gen_android_bp '
+            'to update it.')
+    ]
+  return []
+
 
 def CheckIncludeGuards(input_api, output_api):
-    tool = 'tools/fix_include_guards'
-    file_filter = lambda x: input_api.FilterSourceFile(
-          x,
-          white_list=('.*[.]cc$', '.*[.]h$', tool))
-    if not input_api.AffectedSourceFiles(file_filter):
-        return []
-    if subprocess.call([tool, '--check-only']):
-        return [
-            output_api.PresubmitError(
-                'Please run ' + tool + ' to fix include guards.')
-        ]
+  tool = 'tools/fix_include_guards'
+
+  def file_filter(x): return input_api.FilterSourceFile(
+      x,
+      white_list=['.*[.]cc$', '.*[.]h$', tool])
+  if not input_api.AffectedSourceFiles(file_filter):
     return []
+  if subprocess.call([tool, '--check-only']):
+    return [
+        output_api.PresubmitError(
+            'Please run ' + tool + ' to fix include guards.')
+    ]
+  return []
+
+
+def CheckIncludeViolations(input_api, output_api):
+  tool = 'tools/check_include_violations'
+
+  def file_filter(x): return input_api.FilterSourceFile(
+      x,
+      white_list=['include/.*[.]h$', tool])
+  if not input_api.AffectedSourceFiles(file_filter):
+    return []
+  if subprocess.call([tool]):
+    return [output_api.PresubmitError(tool + ' failed.')]
+  return []
 
 
 def CheckBinaryDescriptors(input_api, output_api):
-    tool = 'tools/gen_binary_descriptors'
-    file_filter = lambda x: input_api.FilterSourceFile(
-          x,
-          white_list=('protos/perfetto/.*[.]proto$', '.*[.]h', tool))
-    if not input_api.AffectedSourceFiles(file_filter):
-        return []
-    if subprocess.call([tool, '--check-only']):
-        return [
-            output_api.PresubmitError(
-                'Please run ' + tool + ' to update binary descriptors.')
-        ]
+  tool = 'tools/gen_binary_descriptors'
+
+  def file_filter(x): return input_api.FilterSourceFile(
+      x,
+      white_list=['protos/perfetto/.*[.]proto$', '.*[.]h', tool])
+  if not input_api.AffectedSourceFiles(file_filter):
     return []
+  if subprocess.call([tool, '--check-only']):
+    return [
+        output_api.PresubmitError(
+            'Please run ' + tool + ' to update binary descriptors.')
+    ]
+  return []
 
 
 def CheckMergedTraceConfigProto(input_api, output_api):
-    tool = 'tools/gen_merged_protos'
-    build_file_filter = lambda x: input_api.FilterSourceFile(
-          x,
-          white_list=('protos/perfetto/.*[.]proto$', tool))
-    if not input_api.AffectedSourceFiles(build_file_filter):
-        return []
-    if subprocess.call([tool, '--check-only']):
-        return [
-            output_api.PresubmitError(
-                'perfetto_config.proto or perfetto_trace.proto is out of ' +
-                'date. Please run ' + tool + ' to update it.')
-        ]
+  tool = 'tools/gen_merged_protos'
+
+  def build_file_filter(x): return input_api.FilterSourceFile(
+      x,
+      white_list=['protos/perfetto/.*[.]proto$', tool])
+  if not input_api.AffectedSourceFiles(build_file_filter):
     return []
+  if subprocess.call([tool, '--check-only']):
+    return [
+        output_api.PresubmitError(
+            'perfetto_config.proto or perfetto_trace.proto is out of ' +
+            'date. Please run ' + tool + ' to update it.')
+    ]
+  return []
 
 
 # Prevent removing or changing lines in event_whitelist.
@@ -154,9 +174,9 @@
             and new_line != old_line for old_line, new_line
            in itertools.izip(f.OldContents(), f.NewContents())):
       return [
-        output_api.PresubmitError(
-            'event_whitelist only has two supported changes: '
-            'appending a new line, and replacing a line with removed.'
-        )
+          output_api.PresubmitError(
+              'event_whitelist only has two supported changes: '
+              'appending a new line, and replacing a line with removed.'
+          )
       ]
   return []
diff --git a/docs/index.html b/docs/index.html
index 05c1ffd..4db856e 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -50,6 +50,8 @@
   <script src="/static/docsify-themeable.min.js"></script>
   <script src="/static/docsify-copy-code.min.js"></script>
   <script src="/static/prism-bash.min.js"></script>
+  <script src="/static/prism-protobuf.min.js"></script>
+  <script src="/static/prism-sql.min.js"></script>
 </body>
 
-</html>
\ No newline at end of file
+</html>
diff --git a/docs/metrics.md b/docs/metrics.md
new file mode 100644
index 0000000..6ff137b
--- /dev/null
+++ b/docs/metrics.md
@@ -0,0 +1,356 @@
+Writing Perfetto-based metrics
+=============
+
+Contents
+---------
+1. Background
+2. The Perfetto Metrics Platform
+3. Writing your first metric - step by step
+4. Breaking down and composing metrics (TBD)
+5. Adding a new metric or editing an existing metric (TBD)
+6. Running a metric over a set of traces (TBD)
+7. Metrics platform as an API (TBD)
+
+Background
+---------
+Using traces allows computation of reproducible metrics in a wide range
+of situations; examples include benchmarks, lab tests and on
+large corpuses of traces. In these cases, these metrics allow for direct
+root-causing when a regression is detected.
+
+The Perfetto Metrics Platform
+----------
+The metrics platform (powered by the
+[trace processor](trace-processor.md)) allows metrics authors to write
+SQL queries to generate metrics in the form of protobuf messages or proto text.
+
+We strongly encourage all metrics derived on Perfetto traces to be added to the
+Perfetto repo unless there is a clear usecase (e.g. confidentiality) why these
+metrics should not be publicly available.
+
+In return for upstreaming metrics, authors will have first class support for
+running metrics locally and the confidence that their metrics will remain stable
+as trace processor is developed.
+
+For example, generating the full (human readable) set of Android memory
+metrics on a trace is as simple as:
+```shell
+trace_processor_shell --run-metrics android_mem <trace>
+```
+
+As well as scaling upwards while developing from running on a single trace
+locally to running on a large set of traces, the reverse is also very useful.
+When an anomaly is observed in the metrics of a lab benchmark, you can simply
+download a representative trace and run the same metric locally in shell.
+
+Since the same code is running locally and remotely, you can be confident in
+reproducing the issue and use the power of trace processor and/or the Perfetto
+UI to identify the problem!
+
+Writing your first metric: A Step by Step Guide
+----------
+To begin, all you need is some familiarity with SQL and you're ready to start!
+
+Suppose that want a write a metric which computes the CPU time for every process
+in the trace and lists the names of the top 5 processes (by CPU time)
+and the number of threads which were associated with those processes over its
+lifetime.
+
+*Note:*
+* If you want to jump straight to the code, at the end of this guide, your
+workspace should look something like this [GitHub gist](https://gist.github.com/tilal6991/c221cf0cae17e298dfa82b118edf9080). See Step 0 and 4
+below as to where to get trace processor and how to run it to output the
+metrics.
+
+### Step 0
+As a setup step, you'll want to create a folder to act as a scratch workspace;
+this folder will be referred to using the env variable `$WORKSPACE` in Step 4.
+
+The other thing you'll need is trace processor shell. You can download this
+[here](https://get.perfetto.dev/trace_processor) or you can build from source
+using the instructions [here](trace-processor.md). Whichever method is
+chosen, $TRACE_PROCESSOR env variable will be used to refer to the location of
+the binary in Step 4.
+
+### Step 1
+As all metrics in the metrics platform are defined using protos, the metric
+needs to be strctured as a proto. For this metric, there needs to be some notion
+of a process name along with its CPU time and number of threads.
+
+Starting off, in a file named `top_five_processes.proto` in our workspace,
+let's create a basic proto message called ProcessInfo with those three fields:
+```protobuf
+message ProcessInfo {
+  optional string process_name = 1;
+  optional uint64 cpu_time_ms = 2;
+  optional uint32 num_threads = 3;
+}
+```
+
+Next up is a wrapping message which will hold the repeated field containing
+the top 5 processes.
+```protobuf
+message TopProcesses {
+  repeated ProcessInfo process_info = 1;
+}
+```
+
+Finally, let's define an extension to the root proto for all metrics -
+the
+[TraceMetrics](https://android.googlesource.com/platform/external/perfetto/+/HEAD/protos/perfetto/metrics/metrics.proto#39)
+proto).
+```protobuf
+extend TraceMetrics {
+  optional TopProcesses top_processes = 450;
+}
+```
+Adding this extension field allows trace processor to link the newly defined
+metric to the `TraceMetrics` proto.
+
+*Notes:*
+* The field ids 450-500 are reserved for local development so you can use
+any of them as the field id for the extension field.
+* The choice of field name here is important as the SQL file and the final
+table generated in SQL will be based on this name.
+
+Putting everything together, along with some boilerplate header information
+gives:
+```protobuf
+syntax = "proto2";
+option optimize_for = LITE_RUNTIME;
+
+package perfetto.protos;
+
+import "perfetto/metrics/metrics.proto";
+
+message ProcessInfo {
+  optional string process_name = 1;
+  optional int64 cpu_time_ms = 2;
+  optional uint32 num_threads = 3;
+}
+
+message TopProcesses {
+  repeated ProcessInfo process_info = 1;
+}
+
+extend TraceMetrics {
+  optional TopProcesses top_processes = 450;
+}
+```
+
+### Step 2
+Let's write the SQL to generate the table of the top 5 processes ordered
+by the sum of the CPU time they ran for and the number of threads which were
+associated with the process. The following SQL should be to a file called
+`top_five_processes.sql` in your workspace:
+```sql
+CREATE VIEW top_five_processes_by_cpu
+SELECT
+  process.name as process_name,
+  CAST(SUM(sched.dur) / 1e6 as INT64) as cpu_time_ms,
+  COUNT(DISTINCT utid) as num_threads
+FROM sched
+INNER JOIN thread USING(utid)
+INNER JOIN process USING(upid)
+GROUP BY process.name
+ORDER BY cpu_time_ms DESC
+LIMIT 5;
+```
+Let's break this query down:
+1. The first table used is the `sched` table. This contains all the
+   scheduling data available in the trace. Each scheduling "slice" is associated
+   with a thread which is uniquely identified in Perfetto traces using its
+   `utid`. The two pieces of information which needed from the sched table
+   is the `dur` - short for duration, this is the amount of time the slice
+   lasted - and the `utid` which will be use to join with the thread table.
+2. The next table is the thread table. This gives us a lot of information which
+   are not particularily interested (including its thread name) but it does give
+   us the `upid`. Similar to `utid`, `upid` is the unique identifier for a
+   process in a Perfetto trace. In this case, `upid` will refer to the process
+   which hosts the thread given by `utid`.
+3. The final table is the process table. This gives the name of the
+   process associated with the orginal sched slice.
+4. With the process, thread and duration for each sched slice, all the slices
+   for a single processes are collected and their durations summed to get the
+   CPU time (dividing by 1e6 as sched's duration is in nanoseconds) and count
+   the number of distinct threads.
+5. Finally, we order by the cpu time and take limit to the top 5.
+
+### Step 3
+Now that the result of the metric has been expressed as an SQL table, it needs
+to be converted a proto. The metrics platform has built-in support for emitting
+protos using SQL functions; something which is used extensively in this step.
+
+Let's look at how it works for our table above.
+```sql
+CREATE VIEW top_processes_output AS
+SELECT TopProcesses(
+  'process_info', (
+    SELECT RepeatedField(
+      ProcessInfo(
+        'process_name', process_name,
+        'cpu_time_ms', cpu_time_ms,
+        'num_threads', num_threads
+      )
+    )
+    FROM top_five_processes_by_cpu
+  )
+);
+```
+Let's break this down again:
+1. Starting from the inner-most SELECT statement, there is
+   what looks like a function call to the ProcessInfo function; in face this is
+   no conincidence. For each proto that the metrics platform knows about,
+   it generates a SQL function with the same name as the proto. This function
+   takes key value pairs with the key as the name of the proto field to fill
+   and the value being the data to store in the field. The output is the proto
+   created by writing the fields described in the function! (*)
+
+   In this case, this function is called once for each row in
+   the `top_five_processes_by_cpu` table. The output of will be the fully filled
+   ProcessInfo proto.
+
+   The call to the `RepeatedField` function is the most interesting part and
+   also the most important. In technical terms, `RepeatedField` is an aggregate
+   function; practically, this means that it takes a full table of values and
+   generates a single array which contains all the values passed to it.
+
+   Therefore, the output of this whole SELECT statement is an array of
+   5 ProcessInfo protos.
+2. Next is creation of the `TopProcesses` proto. By now, the syntax should
+   already feel somewhat familiar; the proto builder funciton is called
+   to fill in the `process_info` field with the array of protos from the
+   inner funciton.
+
+   The output of this SELECT is a single `TopProcesses` proto containing
+   the ProcessInfos as a repeated field.
+3. Finally, the view is created. This view is specially named to allow the
+   metrics platform to query it to obtain the root proto for each metric (in
+   this case `TopProcesses`). See the note below as to the pattern behind
+   this view's name.
+
+(*) - side note: this is not strictly true. To type-check the protos, we
+also return some metadata about the type of the proto but this is unimportant
+for metric authors
+
+*Note:*
+* It is important that the views be named
+  {name of TraceMetrics extension field}_output. This is the pattern used
+  and expected by the metrics platform for all metrics.
+
+And that's all the SQL we need to write! Our final file should look like so:
+```sql
+CREATE VIEW top_five_processes_by_cpu AS
+SELECT
+  process.name as process_name,
+  CAST(SUM(sched.dur) / 1e6 as INT64) as cpu_time_ms,
+  COUNT(DISTINCT utid) as num_threads
+FROM sched
+INNER JOIN thread USING(utid)
+INNER JOIN process USING(upid)
+GROUP BY process.name
+ORDER BY cpu_time_ms DESC
+LIMIT 5;
+
+CREATE top_processes_output AS
+SELECT TopProcesses(
+  'process_info', (
+    SELECT RepeatedField(
+      ProcessInfo(
+        'process_name', process_name,
+        'cpu_time_ms', cpu_time_ms,
+        'num_threads', num_threads
+      )
+    )
+    FROM top_five_processes_by_cpu
+  )
+);
+```
+
+*Notes:*
+* The name of the SQL file should be the same as the name of TraceMetrics
+  extension field. This is to allow the metrics platform to associated the
+  proto extension field with the SQL which needs to be run to generate it.
+
+### Step 4
+This is the last step and where we get to see the results of our work!
+
+For this step, all we need is a one-liner, invoking trace processor
+shell (see Step 0 for downloading it):
+```shell
+$TRACE_PROCESSOR --run-metrics $WORKSPACE/top_five_processes.sql $TRACE 2> /dev/null
+```
+(If you want a example trace to test this on, see the Notes section below.)
+
+By passing the SQL file for the metric we want to compute, trace processor uses
+the name of this file to both find the proto and also to figure out the name
+of the output table for the proto and the name of the extension field for
+`TraceMetrics`; this is why it was important to choose the names of these other
+objects carefully.
+
+*Notes:*
+* If something doesn't work as intended, check that your workspace looks the
+  same as the contents of this [GitHub gist](https://gist.github.com/tilal6991/c221cf0cae17e298dfa82b118edf9080).
+* A good example trace for this metric is the Android example trace used by
+  the Perfetto UI found [here](https://storage.googleapis.com/perfetto-misc/example_android_trace_30s_1)
+* We're redirecting stderror to remove any noise from parsing the trace that
+  trace processor generates.
+
+If everything went successfully, you should see something like the following
+(this is specifically the output for the Android example trace linked above):
+```
+[perfetto.protos.top_five_processes] {
+  process_info {
+    process_name: "com.google.android.GoogleCamera"
+    cpu_time_ms: 15154
+    num_threads: 125
+  }
+  process_info {
+    process_name: "sugov:4"
+    cpu_time_ms: 6846
+    num_threads: 1
+  }
+  process_info {
+    process_name: "system_server"
+    cpu_time_ms: 6809
+    num_threads: 66
+  }
+  process_info {
+    process_name: "cds_ol_rx_threa"
+    cpu_time_ms: 6684
+    num_threads: 1
+  }
+  process_info {
+    process_name: "com.android.chrome"
+    cpu_time_ms: 5125
+    num_threads: 49
+  }
+}
+```
+
+### Conclusion
+That finishes the introductory guide to writing an metric using the Perfetto
+metrics platform! For more information about where to go next, the following
+links may be useful:
+* To understand what data is available to you and how the SQL tables are
+  structured see the [trace processor](trace-processor.md) docs.
+* To see how you can use the RUN_METRIC function to extract common snippets of
+  SQL and reuse them for writing bigger metrics, continue reading!
+* To see how you can add your own metrics to the platform or edit an existing
+  metric, continue reading!
+
+Breaking down and composing metrics
+----------
+Coming soon!
+
+Adding a new metric or editing an existing metric
+----------
+Coming soon!
+
+Running a metric over a set of traces
+----------
+Coming soon!
+
+Metrics platform as an API
+----------
+Coming soon!
diff --git a/docs/toc.md b/docs/toc.md
index 7fb68f2..ccb96b2 100644
--- a/docs/toc.md
+++ b/docs/toc.md
@@ -10,6 +10,7 @@
   * [Heap Profiling](heapprofd.md)
 * Trace analysis
   * [Trace processor](trace-processor.md)
+  * [Trace-based metrics](metrics.md)
   * [Trace conversion](traceconv.md)
 * Architectural docs
   * [Key concepts](architecture.md)
diff --git a/gn/BUILD.gn b/gn/BUILD.gn
index 7aa6d02..beb1ebb 100644
--- a/gn/BUILD.gn
+++ b/gn/BUILD.gn
@@ -26,17 +26,6 @@
   }
 }
 
-# Used by base/gtest_prod_util.h for the FRIEND_TEST_* macros. Note that other
-# production targets (i.e. testonly == false) should use base/gtest_prod_util.h
-# instead.
-group("gtest_prod_config") {
-  if (perfetto_build_standalone || perfetto_build_with_android) {
-    public_configs = [ "//buildtools:googletest_prod_config" ]
-  } else {
-    public_configs = [ ":gtest_config" ]
-  }
-}
-
 config("gtest_config") {
   if (perfetto_build_with_embedder) {
     include_dirs = [ "//testing/gtest/include" ]
@@ -88,6 +77,18 @@
   }
 }
 
+group("sqlite") {
+  if (perfetto_build_standalone || perfetto_build_with_android) {
+    public_deps = [
+      "//buildtools:sqlite",
+    ]
+  } else {
+    public_deps = [
+      "//third_party/sqlite:sqlite",
+    ]
+  }
+}
+
 # protoc compiler library, for building protoc plugins on the host.
 if (current_toolchain == host_toolchain) {
   group("protoc_lib_deps") {
diff --git a/gn/standalone/proto_library.gni b/gn/standalone/proto_library.gni
index d2ffe54..e496fd0 100644
--- a/gn/standalone/proto_library.gni
+++ b/gn/standalone/proto_library.gni
@@ -105,8 +105,12 @@
   }
 
   config_name = "${target_name}_config"
-  action_name = "${target_name}_gen"
-  source_set_name = target_name
+  if (generate_descriptor == "") {
+    action_name = "${target_name}_gen"
+    source_set_name = target_name
+  } else {
+    action_name = target_name
+  }
 
   config(config_name) {
     include_dirs = [ cc_out_dir ]
@@ -114,7 +118,9 @@
 
   # The XXX_gen action that generates the .pb.{cc,h} files.
   action(action_name) {
-    visibility = [ ":$source_set_name" ]
+    if (generate_descriptor == "") {
+      visibility = [ ":$source_set_name" ]
+    }
     script = "//gn/standalone/build_tool_wrapper.py"
     sources = proto_sources
     outputs = get_path_info(protogens, "abspath")
@@ -184,50 +190,52 @@
     if (defined(invoker.deps)) {
       deps += invoker.deps
     }
-  }  # action "${target_name}_gen"
+  }  # action(action_name)
 
   # The source_set that builds the generated .pb.cc files.
-  source_set(target_name) {
-    forward_variables_from(invoker,
-                           [
-                             "defines",
-                             "include_dirs",
-                             "public_configs",
-                             "testonly",
-                             "visibility",
-                           ])
+  if (generate_descriptor == "") {
+    source_set(source_set_name) {
+      forward_variables_from(invoker,
+                             [
+                               "defines",
+                               "include_dirs",
+                               "public_configs",
+                               "testonly",
+                               "visibility",
+                             ])
 
-    sources = get_target_outputs(":$action_name")
+      sources = get_target_outputs(":$action_name")
 
-    configs -= [ "//gn/standalone:extra_warnings" ]
-    if (defined(invoker.extra_configs)) {
-      configs += invoker.extra_configs
-    }
+      configs -= [ "//gn/standalone:extra_warnings" ]
+      if (defined(invoker.extra_configs)) {
+        configs += invoker.extra_configs
+      }
 
-    if (!defined(invoker.public_configs)) {
-      public_configs = []
-    }
+      if (!defined(invoker.public_configs)) {
+        public_configs = []
+      }
 
-    public_configs += [
-      "//buildtools:protobuf_gen_config",
-      ":$config_name",
-    ]
-
-    # Use protobuf_full only for tests.
-    if (defined(invoker.use_protobuf_full) &&
-        invoker.use_protobuf_full == true) {
-      deps = [
-        "//buildtools:protobuf_full",
+      public_configs += [
+        "//buildtools:protobuf_gen_config",
+        ":$config_name",
       ]
-    } else {
-      deps = [
-        "//buildtools:protobuf_lite",
-      ]
-    }
 
-    deps += [ ":$action_name" ]
-    if (defined(invoker.deps)) {
-      deps += invoker.deps
-    }
-  }  # source_set(target_name)
+      # Use protobuf_full only for tests.
+      if (defined(invoker.use_protobuf_full) &&
+          invoker.use_protobuf_full == true) {
+        deps = [
+          "//buildtools:protobuf_full",
+        ]
+      } else {
+        deps = [
+          "//buildtools:protobuf_lite",
+        ]
+      }
+
+      deps += [ ":$action_name" ]
+      if (defined(invoker.deps)) {
+        deps += invoker.deps
+      }
+    }  # source_set(source_set_name)
+  }
 }  # template
diff --git a/gn/standalone/toolchain/BUILD.gn b/gn/standalone/toolchain/BUILD.gn
index 1ffb94f..7b46500 100644
--- a/gn/standalone/toolchain/BUILD.gn
+++ b/gn/standalone/toolchain/BUILD.gn
@@ -98,10 +98,11 @@
     }
 
     tool("alink") {
+      rspfile = "{{output}}.rsp"
       if (is_mac && ar != "suppress_unused_ar_variable_warning") {
-        command = "rm -f {{output}} && libtool -static {{arflags}} -o {{output}} {{inputs}}"
+        rspfile_content = "{{inputs_newline}}"
+        command = "rm -f {{output}} && libtool -static {{arflags}} -o {{output}} -filelist $rspfile"
       } else {
-        rspfile = "{{output}}.rsp"
         rspfile_content = "{{inputs}}"
         command = "$ar rcsD {{output}} @$rspfile"
       }
diff --git a/include/perfetto/README.md b/include/perfetto/README.md
new file mode 100644
index 0000000..eace7f7
--- /dev/null
+++ b/include/perfetto/README.md
@@ -0,0 +1,111 @@
+# Perfetto public API surface
+
+**This API surface is not stable yet, don't depend on it**
+
+This folder contains the public perfetto API headers. This allows an app to
+inject trace events into perfetto with ~10 lines of code (see
+api_usage_example.cc).
+
+The ext/ subdirectory expose the (unstable) classes and types that are
+exposed to emvbedders that have exceptional requirements in terms of interposing
+their own custom IPC layer. To the day the only case is chromium. Nothing else
+should depend on ext/. Contact perfetto-dev@ if you think you need to 
+depend on an ext/ header.
+
+Headers in this folder must be hermetic. No ext/ perfetto header must be
+leaked from the includes.
+
+What is a client supposed to do to use tracing? See example below in this page.
+
+
+Source code layout: what goes where?
+------------------------------------
+
+**include/perfetto (this folder):**
+Embedders are allowed to access and depend on any folder of this but ext/.
+This contains classes to: (i) use tracing; (ii) extend the tracing internals
+(i.e. implement the Platform).
+
+Rules:
+- This directory should contain only .h files and no .cc files.
+- Corresponding .cc files go into `src/`.
+- .h files in here can depend only on `include/perfetto/` but not on 
+  `include/perfetto/ext/`,
+
+**include/perfetto/tracing/internal:**
+This directory contains headers that are required to implement the public-facing
+tracing API efficiently but that are not part of the API surface.
+In an ideal world there would be no need of these headers and everything would
+be handle via forward-declarations and PIMPL patterns. Unfortunately, however,
+PIMPL cannot be used for inline functions, where the implementation needs to be
+exposed in the public headers, which in turn need to depend on the memory layout
+of structs/classes.
+
+Rules:
+- All classes / types declared in this folder must be wrapped in the
+  ::perfetto::internal namespace.
+- Both public and internal .h headers must not pull other perfetto headers
+  from ext/.
+- .cc files instead can depend on other perfetto classes, as well as .h headers
+  located in src/.
+- Embedders must not depend on the perfetto::internal namespace.
+- Internal types cannot be used as input, output or return arguments of public
+  API functions.
+- Internal types cannot be directly exposed to virtual methods that are
+  intended to be called or overridden by the embedder (e.g. TracingBackend's
+  methods). For those the solution is to create a matching non-internal base
+  class with a static factory method.
+- We don't guarantee binary compatibility between versions (i.e. this client
+  library can only be statically linked) but we guarantee source-level
+  compatibility and ABI of the UNIX socket and shared memory buffers.
+
+
+Usage example
+-------------
+1. Call `perfetto::Tracing::Initialize(...)` once, when starting the app.
+  While doing so the app can chose the tracing model:
+  - Fully in-process: the service runs in a thread within the same process.
+  - System: connects to the traced system daemon via a UNIX socket. This allows
+    the app to join system-wide tracing sessions. This is available only on
+    Linux/Android/MacOS for now.
+  - Private dedicated process: similar to the in-process case, but the service
+    runs in a dedicated process rather than a thread. This is for performance,
+    stability and security isolation. Also, this is not implemented yet.
+  - Custom backend: this is for peculiar cases (mainly chromium) where the
+    embedder is multi-process but wants to use a different IPC mechanism. The
+    embedder needs to deal with the larger and clunkier set of perfetto APIs.
+    Reach out to the team before using this mode. It's very unlikely you need
+    this unless you are a project rolled into chromium.
+
+2. Define and register one or more data sources, like this:
+```cpp
+  #include "perfetto/tracing.h"
+
+  class MyDataSource : public perfetto::DataSource<MyDataSource> {
+    void OnSetup(SetupArgs) override {}
+    void OnStart(StartArgs) override {}
+    void OnStop(StopArgs) override {}
+  };
+  ...
+  PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource);
+  ...
+  perfetto::DataSourceDescriptor dsd;
+  dsd.set_name("my_data_source");
+  MyDataSource::Register(dsd);
+```
+
+3. Optionally define a new proto schema in `trace_packet.proto`
+
+4. Emit trace events
+```cpp
+  MyDataSource::Trace([](TraceContext ctx) {
+      auto trace_packet = ctx.NewTracePacket();
+      ctx.set_timestamp(...);
+      ctx.set_my_custom_proto(...);
+  });
+```
+
+The passed labmda will be called only if tracing is enabled and the data source
+was enabled in the trace config. It might be called multiple times, one for each
+active tracing session, in case of concurrent tracing sessions (or even within a
+single tracing session, if the data source is listed twice in the trace config).
diff --git a/include/perfetto/base/BUILD.gn b/include/perfetto/base/BUILD.gn
index 0540e0f..3a3e426 100644
--- a/include/perfetto/base/BUILD.gn
+++ b/include/perfetto/base/BUILD.gn
@@ -17,47 +17,9 @@
 source_set("base") {
   sources = [
     "build_config.h",
-    "circular_queue.h",
-    "container_annotations.h",
-    "event.h",
+    "compiler.h",
     "export.h",
-    "file_utils.h",
-    "gtest_prod_util.h",
-    "hash.h",
     "logging.h",
-    "metatrace.h",
-    "no_destructor.h",
-    "optional.h",
-    "paged_memory.h",
-    "pipe.h",
-    "scoped_file.h",
-    "small_set.h",
-    "string_splitter.h",
-    "string_utils.h",
-    "string_view.h",
-    "string_writer.h",
     "task_runner.h",
-    "temp_file.h",
-    "thread_annotations.h",
-    "thread_checker.h",
-    "thread_task_runner.h",
-    "thread_utils.h",
-    "time.h",
-    "unix_task_runner.h",
-    "utils.h",
-    "watchdog.h",
-    "watchdog_noop.h",
-    "watchdog_posix.h",
-    "weak_ptr.h",
   ]
-  if (is_android) {
-    sources += [ "android_task_runner.h" ]
-  }
-  if (perfetto_build_with_ipc_layer) {
-    sources += [ "unix_socket.h" ]
-  }
-  public_deps = [
-    "../../../gn:gtest_prod_config",
-  ]
-  public_configs = [ "../../../gn:asan_instrumentation" ]
 }
diff --git a/include/perfetto/base/compiler.h b/include/perfetto/base/compiler.h
new file mode 100644
index 0000000..a4cb6ec
--- /dev/null
+++ b/include/perfetto/base/compiler.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#ifndef INCLUDE_PERFETTO_BASE_COMPILER_H_
+#define INCLUDE_PERFETTO_BASE_COMPILER_H_
+
+#include <type_traits>
+
+#define PERFETTO_LIKELY(_x) __builtin_expect(!!(_x), 1)
+#define PERFETTO_UNLIKELY(_x) __builtin_expect(!!(_x), 0)
+
+#if defined(__GNUC__) || defined(__clang__)
+#define PERFETTO_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+#else
+#define PERFETTO_WARN_UNUSED_RESULT
+#endif
+
+#if defined(__clang__)
+#define PERFETTO_ALWAYS_INLINE __attribute__((__always_inline__))
+#else
+// GCC is too pedantic and often fails with the error:
+// "always_inline function might not be inlinable"
+#define PERFETTO_ALWAYS_INLINE
+#endif
+
+// TODO(lalitm): is_trivially_constructible is currently not available
+// in some environments we build in. Reenable when that environment supports
+// this.
+#if defined(__GLIBCXX__)
+#define PERFETTO_IS_TRIVIALLY_CONSTRUCTIBLE(T) true
+#else
+#define PERFETTO_IS_TRIVIALLY_CONSTRUCTIBLE(T) \
+  std::is_trivially_constructible<T>::value
+#endif
+
+// TODO(lalitm): is_trivially_copyable is currently not available
+// in some environments we build in. Reenable when that environment supports
+// this.
+#if defined(__GLIBCXX__)
+#define PERFETTO_IS_TRIVIALLY_COPYABLE(T) true
+#else
+#define PERFETTO_IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
+#endif
+
+namespace perfetto {
+namespace base {
+
+template <typename... T>
+inline void ignore_result(const T&...) {}
+
+}  // namespace base
+}  // namespace perfetto
+
+#endif  // INCLUDE_PERFETTO_BASE_COMPILER_H_
diff --git a/include/perfetto/base/gtest_prod_util.h b/include/perfetto/base/gtest_prod_util.h
deleted file mode 100644
index 09b5f82..0000000
--- a/include/perfetto/base/gtest_prod_util.h
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * Copyright (C) 2018 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.
- */
-
-#ifndef INCLUDE_PERFETTO_BASE_GTEST_PROD_UTIL_H_
-#define INCLUDE_PERFETTO_BASE_GTEST_PROD_UTIL_H_
-
-#include "gtest/gtest_prod.h"  // nogncheck
-
-#endif  // INCLUDE_PERFETTO_BASE_GTEST_PROD_UTIL_H_
diff --git a/include/perfetto/base/logging.h b/include/perfetto/base/logging.h
index 131f589..09512d4 100644
--- a/include/perfetto/base/logging.h
+++ b/include/perfetto/base/logging.h
@@ -22,6 +22,9 @@
 #include <stdlib.h>
 #include <string.h>  // For strerror.
 
+#include "perfetto/base/build_config.h"
+#include "perfetto/base/compiler.h"
+
 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
 #define PERFETTO_DCHECK_IS_ON() 0
 #else
@@ -34,9 +37,6 @@
 #define PERFETTO_DLOG_IS_ON() PERFETTO_FORCE_DLOG
 #endif
 
-#include "perfetto/base/build_config.h"
-#include "perfetto/base/utils.h"
-
 #if defined(PERFETTO_ANDROID_ASYNC_SAFE_LOG)
 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
     !PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
diff --git a/include/perfetto/base/task_runner.h b/include/perfetto/base/task_runner.h
index 3b80bbb..cf60401 100644
--- a/include/perfetto/base/task_runner.h
+++ b/include/perfetto/base/task_runner.h
@@ -19,18 +19,11 @@
 
 #include <functional>
 
-#include "perfetto/base/build_config.h"
 #include "perfetto/base/export.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/base/watchdog.h"
 
 namespace perfetto {
 namespace base {
 
-// Maximum time a single task can take in a TaskRunner before the
-// program suicides.
-constexpr int64_t kWatchdogMillis = 30000;  // 30s
-
 // A generic interface to allow the library clients to interleave the execution
 // of the tracing internals in their runtime environment.
 // The expectation is that all tasks, which are queued either via PostTask() or
@@ -73,13 +66,6 @@
   // thread/sequence. This can allow some callers to skip PostTask and instead
   // directly execute the code. Can be called from any thread.
   virtual bool RunsTasksOnCurrentThread() const = 0;
-
- protected:
-  static void RunTask(const std::function<void()>& task) {
-    Watchdog::Timer handle =
-        base::Watchdog::GetInstance()->CreateFatalTimer(kWatchdogMillis);
-    task();
-  }
 };
 
 }  // namespace base
diff --git a/include/perfetto/base/watchdog.h b/include/perfetto/base/watchdog.h
deleted file mode 100644
index 77fde51..0000000
--- a/include/perfetto/base/watchdog.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (C) 2018 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.
- */
-
-#ifndef INCLUDE_PERFETTO_BASE_WATCHDOG_H_
-#define INCLUDE_PERFETTO_BASE_WATCHDOG_H_
-
-#include "perfetto/base/build_config.h"
-
-#if (PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||    \
-     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)) && \
-    !PERFETTO_BUILDFLAG(PERFETTO_EMBEDDER_BUILD)
-#include "perfetto/base/watchdog_posix.h"
-#else
-#include "perfetto/base/watchdog_noop.h"
-#endif
-
-#endif  // INCLUDE_PERFETTO_BASE_WATCHDOG_H_
diff --git a/include/perfetto/ext/base/BUILD.gn b/include/perfetto/ext/base/BUILD.gn
new file mode 100644
index 0000000..64d50ae
--- /dev/null
+++ b/include/perfetto/ext/base/BUILD.gn
@@ -0,0 +1,58 @@
+# Copyright (C) 2019 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.gni")
+
+source_set("base") {
+  sources = [
+    "circular_queue.h",
+    "container_annotations.h",
+    "event.h",
+    "file_utils.h",
+    "hash.h",
+    "metatrace.h",
+    "no_destructor.h",
+    "optional.h",
+    "paged_memory.h",
+    "pipe.h",
+    "scoped_file.h",
+    "small_set.h",
+    "string_splitter.h",
+    "string_utils.h",
+    "string_view.h",
+    "string_writer.h",
+    "temp_file.h",
+    "thread_annotations.h",
+    "thread_checker.h",
+    "thread_task_runner.h",
+    "thread_utils.h",
+    "time.h",
+    "unix_task_runner.h",
+    "utils.h",
+    "watchdog.h",
+    "watchdog_noop.h",
+    "watchdog_posix.h",
+    "weak_ptr.h",
+  ]
+  if (is_android) {
+    sources += [ "android_task_runner.h" ]
+  }
+  if (perfetto_build_with_ipc_layer) {
+    sources += [ "unix_socket.h" ]
+  }
+  public_configs = [ "../../../../gn:asan_instrumentation" ]
+  public_deps = [
+    "../../base",
+  ]
+}
diff --git a/include/perfetto/base/android_task_runner.h b/include/perfetto/ext/base/android_task_runner.h
similarity index 89%
rename from include/perfetto/base/android_task_runner.h
rename to include/perfetto/ext/base/android_task_runner.h
index 5986997..85a5e40 100644
--- a/include/perfetto/base/android_task_runner.h
+++ b/include/perfetto/ext/base/android_task_runner.h
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_
-#define INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_ANDROID_TASK_RUNNER_H_
+#define INCLUDE_PERFETTO_EXT_BASE_ANDROID_TASK_RUNNER_H_
 
-#include "perfetto/base/event.h"
-#include "perfetto/base/scoped_file.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/event.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/base/time.h"
 
 #include <poll.h>
 #include <chrono>
@@ -90,4 +90,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_ANDROID_TASK_RUNNER_H_
diff --git a/include/perfetto/base/circular_queue.h b/include/perfetto/ext/base/circular_queue.h
similarity index 97%
rename from include/perfetto/base/circular_queue.h
rename to include/perfetto/ext/base/circular_queue.h
index 6e8d917..72f14d1 100644
--- a/include/perfetto/base/circular_queue.h
+++ b/include/perfetto/ext/base/circular_queue.h
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_CIRCULAR_QUEUE_H_
-#define INCLUDE_PERFETTO_BASE_CIRCULAR_QUEUE_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_CIRCULAR_QUEUE_H_
+#define INCLUDE_PERFETTO_EXT_BASE_CIRCULAR_QUEUE_H_
 
 #include <stdint.h>
 #include <iterator>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace base {
@@ -298,4 +298,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_CIRCULAR_QUEUE_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_CIRCULAR_QUEUE_H_
diff --git a/include/perfetto/base/container_annotations.h b/include/perfetto/ext/base/container_annotations.h
similarity index 93%
rename from include/perfetto/base/container_annotations.h
rename to include/perfetto/ext/base/container_annotations.h
index 615a0b3..183e6e0 100644
--- a/include/perfetto/base/container_annotations.h
+++ b/include/perfetto/ext/base/container_annotations.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_CONTAINER_ANNOTATIONS_H_
-#define INCLUDE_PERFETTO_BASE_CONTAINER_ANNOTATIONS_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_CONTAINER_ANNOTATIONS_H_
+#define INCLUDE_PERFETTO_EXT_BASE_CONTAINER_ANNOTATIONS_H_
 
 #include "perfetto/base/build_config.h"
 
@@ -54,4 +54,4 @@
                                  new_capacity)
 #endif  // defined(ADDRESS_SANITIZER)
 
-#endif  // INCLUDE_PERFETTO_BASE_CONTAINER_ANNOTATIONS_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_CONTAINER_ANNOTATIONS_H_
diff --git a/include/perfetto/base/event.h b/include/perfetto/ext/base/event.h
similarity index 90%
rename from include/perfetto/base/event.h
rename to include/perfetto/ext/base/event.h
index bca6c00..4cfdb7a 100644
--- a/include/perfetto/base/event.h
+++ b/include/perfetto/ext/base/event.h
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_EVENT_H_
-#define INCLUDE_PERFETTO_BASE_EVENT_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_EVENT_H_
+#define INCLUDE_PERFETTO_EXT_BASE_EVENT_H_
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
@@ -64,4 +64,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_EVENT_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_EVENT_H_
diff --git a/include/perfetto/base/file_utils.h b/include/perfetto/ext/base/file_utils.h
similarity index 86%
rename from include/perfetto/base/file_utils.h
rename to include/perfetto/ext/base/file_utils.h
index 0d5b825..e297812 100644
--- a/include/perfetto/base/file_utils.h
+++ b/include/perfetto/ext/base/file_utils.h
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_FILE_UTILS_H_
-#define INCLUDE_PERFETTO_BASE_FILE_UTILS_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_
+#define INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_
 
 #include <stddef.h>
 
 #include <string>
 
-#include "perfetto/base/utils.h"  // For ssize_t on Windows.
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace base {
@@ -43,4 +43,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_FILE_UTILS_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_
diff --git a/include/perfetto/base/hash.h b/include/perfetto/ext/base/hash.h
similarity index 92%
rename from include/perfetto/base/hash.h
rename to include/perfetto/ext/base/hash.h
index e64778e..87b1a54 100644
--- a/include/perfetto/base/hash.h
+++ b/include/perfetto/ext/base/hash.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_HASH_H_
-#define INCLUDE_PERFETTO_BASE_HASH_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_HASH_H_
+#define INCLUDE_PERFETTO_EXT_BASE_HASH_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -60,4 +60,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_HASH_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_HASH_H_
diff --git a/include/perfetto/base/lookup_set.h b/include/perfetto/ext/base/lookup_set.h
similarity index 91%
rename from include/perfetto/base/lookup_set.h
rename to include/perfetto/ext/base/lookup_set.h
index a730dbe..11bff8e 100644
--- a/include/perfetto/base/lookup_set.h
+++ b/include/perfetto/ext/base/lookup_set.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_LOOKUP_SET_H_
-#define INCLUDE_PERFETTO_BASE_LOOKUP_SET_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_LOOKUP_SET_H_
+#define INCLUDE_PERFETTO_EXT_BASE_LOOKUP_SET_H_
 
 #include <set>
 
@@ -63,4 +63,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_LOOKUP_SET_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_LOOKUP_SET_H_
diff --git a/include/perfetto/base/metatrace.h b/include/perfetto/ext/base/metatrace.h
similarity index 90%
rename from include/perfetto/base/metatrace.h
rename to include/perfetto/ext/base/metatrace.h
index 116a160..c41e0ee 100644
--- a/include/perfetto/base/metatrace.h
+++ b/include/perfetto/ext/base/metatrace.h
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_METATRACE_H_
-#define INCLUDE_PERFETTO_BASE_METATRACE_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_METATRACE_H_
+#define INCLUDE_PERFETTO_EXT_BASE_METATRACE_H_
 
 #include <string.h>
 
 #include <string>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace base {
@@ -66,4 +66,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_METATRACE_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_METATRACE_H_
diff --git a/include/perfetto/base/no_destructor.h b/include/perfetto/ext/base/no_destructor.h
similarity index 92%
rename from include/perfetto/base/no_destructor.h
rename to include/perfetto/ext/base/no_destructor.h
index 8009fc5..3ecea99 100644
--- a/include/perfetto/base/no_destructor.h
+++ b/include/perfetto/ext/base/no_destructor.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_NO_DESTRUCTOR_H_
-#define INCLUDE_PERFETTO_BASE_NO_DESTRUCTOR_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_NO_DESTRUCTOR_H_
+#define INCLUDE_PERFETTO_EXT_BASE_NO_DESTRUCTOR_H_
 
 #include <new>
 #include <utility>
@@ -64,4 +64,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_NO_DESTRUCTOR_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_NO_DESTRUCTOR_H_
diff --git a/include/perfetto/base/optional.h b/include/perfetto/ext/base/optional.h
similarity index 98%
rename from include/perfetto/base/optional.h
rename to include/perfetto/ext/base/optional.h
index c46d6ca..2a09bf2 100644
--- a/include/perfetto/base/optional.h
+++ b/include/perfetto/ext/base/optional.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_OPTIONAL_H_
-#define INCLUDE_PERFETTO_BASE_OPTIONAL_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_OPTIONAL_H_
+#define INCLUDE_PERFETTO_EXT_BASE_OPTIONAL_H_
 
 #include <functional>
 #include <type_traits>
@@ -569,7 +569,7 @@
     return &storage_.value_;
   }
 
-  const T& operator*() const & {
+  const T& operator*() const& {
     PERFETTO_DCHECK(storage_.is_populated_);
     return storage_.value_;
   }
@@ -579,7 +579,7 @@
     return storage_.value_;
   }
 
-  const T&& operator*() const && {
+  const T&& operator*() const&& {
     PERFETTO_DCHECK(storage_.is_populated_);
     return std::move(storage_.value_);
   }
@@ -598,7 +598,7 @@
     return storage_.value_;
   }
 
-  const T& value() const & {
+  const T& value() const& {
     PERFETTO_CHECK(storage_.is_populated_);
     return storage_.value_;
   }
@@ -608,13 +608,13 @@
     return std::move(storage_.value_);
   }
 
-  const T&& value() const && {
+  const T&& value() const&& {
     PERFETTO_CHECK(storage_.is_populated_);
     return std::move(storage_.value_);
   }
 
   template <class U>
-  constexpr T value_or(U&& default_value) const & {
+  constexpr T value_or(U&& default_value) const& {
     static_assert(std::is_convertible<U, T>::value,
                   "U must be convertible to T");
     return storage_.is_populated_
@@ -899,4 +899,4 @@
 
 }  // namespace std
 
-#endif  // INCLUDE_PERFETTO_BASE_OPTIONAL_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_OPTIONAL_H_
diff --git a/include/perfetto/base/paged_memory.h b/include/perfetto/ext/base/paged_memory.h
similarity index 93%
rename from include/perfetto/base/paged_memory.h
rename to include/perfetto/ext/base/paged_memory.h
index f994ea5..f0ed568 100644
--- a/include/perfetto/base/paged_memory.h
+++ b/include/perfetto/ext/base/paged_memory.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_PAGED_MEMORY_H_
-#define INCLUDE_PERFETTO_BASE_PAGED_MEMORY_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_PAGED_MEMORY_H_
+#define INCLUDE_PERFETTO_EXT_BASE_PAGED_MEMORY_H_
 
 #include <memory>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/container_annotations.h"
+#include "perfetto/ext/base/container_annotations.h"
 
 // We need to track the committed size on windows and when ASAN is enabled.
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) || defined(ADDRESS_SANITIZER)
@@ -72,7 +72,7 @@
   // chunks above |committed_size|. Crashes if the memory couldn't be committed.
 #if TRACK_COMMITTED_SIZE()
   void EnsureCommitted(size_t committed_size);
-#else  // TRACK_COMMITTED_SIZE()
+#else   // TRACK_COMMITTED_SIZE()
   void EnsureCommitted(size_t /*committed_size*/) {}
 #endif  // TRACK_COMMITTED_SIZE()
 
@@ -98,4 +98,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_PAGED_MEMORY_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_PAGED_MEMORY_H_
diff --git a/include/perfetto/base/pipe.h b/include/perfetto/ext/base/pipe.h
similarity index 84%
rename from include/perfetto/base/pipe.h
rename to include/perfetto/ext/base/pipe.h
index a1fde3c..ba22729 100644
--- a/include/perfetto/base/pipe.h
+++ b/include/perfetto/ext/base/pipe.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_PIPE_H_
-#define INCLUDE_PERFETTO_BASE_PIPE_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_PIPE_H_
+#define INCLUDE_PERFETTO_EXT_BASE_PIPE_H_
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 namespace perfetto {
 namespace base {
@@ -44,4 +44,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_PIPE_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_PIPE_H_
diff --git a/include/perfetto/base/scoped_file.h b/include/perfetto/ext/base/scoped_file.h
similarity index 94%
rename from include/perfetto/base/scoped_file.h
rename to include/perfetto/ext/base/scoped_file.h
index 9981001..2fee6a1 100644
--- a/include/perfetto/base/scoped_file.h
+++ b/include/perfetto/ext/base/scoped_file.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_SCOPED_FILE_H_
-#define INCLUDE_PERFETTO_BASE_SCOPED_FILE_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_SCOPED_FILE_H_
+#define INCLUDE_PERFETTO_EXT_BASE_SCOPED_FILE_H_
 
 #include "perfetto/base/build_config.h"
 
@@ -104,4 +104,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_SCOPED_FILE_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_SCOPED_FILE_H_
diff --git a/include/perfetto/base/small_set.h b/include/perfetto/ext/base/small_set.h
similarity index 91%
rename from include/perfetto/base/small_set.h
rename to include/perfetto/ext/base/small_set.h
index 94f2324..db057a0 100644
--- a/include/perfetto/base/small_set.h
+++ b/include/perfetto/ext/base/small_set.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_SMALL_SET_H_
-#define INCLUDE_PERFETTO_BASE_SMALL_SET_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_SMALL_SET_H_
+#define INCLUDE_PERFETTO_EXT_BASE_SMALL_SET_H_
 
 #include <array>
 
@@ -59,4 +59,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_SMALL_SET_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_SMALL_SET_H_
diff --git a/include/perfetto/base/string_splitter.h b/include/perfetto/ext/base/string_splitter.h
similarity index 93%
rename from include/perfetto/base/string_splitter.h
rename to include/perfetto/ext/base/string_splitter.h
index cee1a27..c68506c 100644
--- a/include/perfetto/base/string_splitter.h
+++ b/include/perfetto/ext/base/string_splitter.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_STRING_SPLITTER_H_
-#define INCLUDE_PERFETTO_BASE_STRING_SPLITTER_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_STRING_SPLITTER_H_
+#define INCLUDE_PERFETTO_EXT_BASE_STRING_SPLITTER_H_
 
 #include <string>
 
@@ -71,4 +71,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_STRING_SPLITTER_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_STRING_SPLITTER_H_
diff --git a/include/perfetto/base/string_utils.h b/include/perfetto/ext/base/string_utils.h
similarity index 83%
rename from include/perfetto/base/string_utils.h
rename to include/perfetto/ext/base/string_utils.h
index d2f9a36..3220a69 100644
--- a/include/perfetto/base/string_utils.h
+++ b/include/perfetto/ext/base/string_utils.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_STRING_UTILS_H_
-#define INCLUDE_PERFETTO_BASE_STRING_UTILS_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_STRING_UTILS_H_
+#define INCLUDE_PERFETTO_EXT_BASE_STRING_UTILS_H_
 
 #include <string>
 #include <vector>
@@ -26,6 +26,7 @@
 bool StartsWith(const std::string& str, const std::string& prefix);
 bool EndsWith(const std::string& str, const std::string& suffix);
 bool Contains(const std::string& haystack, const std::string& needle);
+bool CaseInsensitiveEqual(const std::string& first, const std::string& second);
 std::string Join(const std::vector<std::string>& parts,
                  const std::string& delim);
 std::vector<std::string> SplitString(const std::string& text,
@@ -34,4 +35,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_STRING_UTILS_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_STRING_UTILS_H_
diff --git a/include/perfetto/base/string_view.h b/include/perfetto/ext/base/string_view.h
similarity index 95%
rename from include/perfetto/base/string_view.h
rename to include/perfetto/ext/base/string_view.h
index b0b0f79..dba22e7 100644
--- a/include/perfetto/base/string_view.h
+++ b/include/perfetto/ext/base/string_view.h
@@ -14,16 +14,16 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_STRING_VIEW_H_
-#define INCLUDE_PERFETTO_BASE_STRING_VIEW_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_STRING_VIEW_H_
+#define INCLUDE_PERFETTO_EXT_BASE_STRING_VIEW_H_
 
 #include <string.h>
 
 #include <algorithm>
 #include <string>
 
-#include "perfetto/base/hash.h"
 #include "perfetto/base/logging.h"
+#include "perfetto/ext/base/hash.h"
 
 namespace perfetto {
 namespace base {
@@ -154,4 +154,4 @@
 
 }  // namespace std
 
-#endif  // INCLUDE_PERFETTO_BASE_STRING_VIEW_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_STRING_VIEW_H_
diff --git a/include/perfetto/base/string_writer.h b/include/perfetto/ext/base/string_writer.h
similarity index 95%
rename from include/perfetto/base/string_writer.h
rename to include/perfetto/ext/base/string_writer.h
index 5bc81bb..10e92a0 100644
--- a/include/perfetto/base/string_writer.h
+++ b/include/perfetto/ext/base/string_writer.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_STRING_WRITER_H_
-#define INCLUDE_PERFETTO_BASE_STRING_WRITER_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_STRING_WRITER_H_
+#define INCLUDE_PERFETTO_EXT_BASE_STRING_WRITER_H_
 
 #include <math.h>
 #include <stdlib.h>
@@ -23,8 +23,7 @@
 #include <limits>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 
 namespace perfetto {
 namespace base {
@@ -146,4 +145,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_STRING_WRITER_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_STRING_WRITER_H_
diff --git a/include/perfetto/base/temp_file.h b/include/perfetto/ext/base/temp_file.h
similarity index 90%
rename from include/perfetto/base/temp_file.h
rename to include/perfetto/ext/base/temp_file.h
index e862971..da7e1bf 100644
--- a/include/perfetto/base/temp_file.h
+++ b/include/perfetto/ext/base/temp_file.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_TEMP_FILE_H_
-#define INCLUDE_PERFETTO_BASE_TEMP_FILE_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_TEMP_FILE_H_
+#define INCLUDE_PERFETTO_EXT_BASE_TEMP_FILE_H_
 
 #include <string>
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 namespace perfetto {
 namespace base {
@@ -75,4 +75,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_TEMP_FILE_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_TEMP_FILE_H_
diff --git a/include/perfetto/base/thread_annotations.h b/include/perfetto/ext/base/thread_annotations.h
similarity index 89%
rename from include/perfetto/base/thread_annotations.h
rename to include/perfetto/ext/base/thread_annotations.h
index b5d3671..beb8c45 100644
--- a/include/perfetto/base/thread_annotations.h
+++ b/include/perfetto/ext/base/thread_annotations.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_THREAD_ANNOTATIONS_H_
-#define INCLUDE_PERFETTO_BASE_THREAD_ANNOTATIONS_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_THREAD_ANNOTATIONS_H_
+#define INCLUDE_PERFETTO_EXT_BASE_THREAD_ANNOTATIONS_H_
 
 #include "perfetto/base/build_config.h"
 
@@ -37,4 +37,4 @@
 #define PERFETTO_ANNOTATE_BENIGN_RACE_SIZED(pointer, size, description)
 #endif  // defined(ADDRESS_SANITIZER)
 
-#endif  // INCLUDE_PERFETTO_BASE_THREAD_ANNOTATIONS_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_THREAD_ANNOTATIONS_H_
diff --git a/include/perfetto/base/thread_checker.h b/include/perfetto/ext/base/thread_checker.h
similarity index 90%
rename from include/perfetto/base/thread_checker.h
rename to include/perfetto/ext/base/thread_checker.h
index 9b99e88..643aba1 100644
--- a/include/perfetto/base/thread_checker.h
+++ b/include/perfetto/ext/base/thread_checker.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_THREAD_CHECKER_H_
-#define INCLUDE_PERFETTO_BASE_THREAD_CHECKER_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_THREAD_CHECKER_H_
+#define INCLUDE_PERFETTO_EXT_BASE_THREAD_CHECKER_H_
 
 #include "perfetto/base/build_config.h"
 
@@ -26,7 +26,7 @@
 
 #include "perfetto/base/export.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace base {
@@ -65,4 +65,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_THREAD_CHECKER_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_THREAD_CHECKER_H_
diff --git a/include/perfetto/base/thread_task_runner.h b/include/perfetto/ext/base/thread_task_runner.h
similarity index 90%
rename from include/perfetto/base/thread_task_runner.h
rename to include/perfetto/ext/base/thread_task_runner.h
index db49a61..fac4553 100644
--- a/include/perfetto/base/thread_task_runner.h
+++ b/include/perfetto/ext/base/thread_task_runner.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_THREAD_TASK_RUNNER_H_
-#define INCLUDE_PERFETTO_BASE_THREAD_TASK_RUNNER_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_THREAD_TASK_RUNNER_H_
+#define INCLUDE_PERFETTO_EXT_BASE_THREAD_TASK_RUNNER_H_
 
 #include <functional>
 #include <thread>
 
-#include "perfetto/base/unix_task_runner.h"
+#include "perfetto/ext/base/unix_task_runner.h"
 
 namespace perfetto {
 namespace base {
@@ -63,4 +63,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_THREAD_TASK_RUNNER_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_THREAD_TASK_RUNNER_H_
diff --git a/include/perfetto/base/thread_utils.h b/include/perfetto/ext/base/thread_utils.h
similarity index 92%
rename from include/perfetto/base/thread_utils.h
rename to include/perfetto/ext/base/thread_utils.h
index 3d7eac0..2914025 100644
--- a/include/perfetto/base/thread_utils.h
+++ b/include/perfetto/ext/base/thread_utils.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_THREAD_UTILS_H_
-#define INCLUDE_PERFETTO_BASE_THREAD_UTILS_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_THREAD_UTILS_H_
+#define INCLUDE_PERFETTO_EXT_BASE_THREAD_UTILS_H_
 
 #include <stdint.h>
 
@@ -73,4 +73,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_THREAD_UTILS_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_THREAD_UTILS_H_
diff --git a/include/perfetto/base/time.h b/include/perfetto/ext/base/time.h
similarity index 97%
rename from include/perfetto/base/time.h
rename to include/perfetto/ext/base/time.h
index b58c0ed..a3b4a78 100644
--- a/include/perfetto/base/time.h
+++ b/include/perfetto/ext/base/time.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_TIME_H_
-#define INCLUDE_PERFETTO_BASE_TIME_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_TIME_H_
+#define INCLUDE_PERFETTO_EXT_BASE_TIME_H_
 
 #include <time.h>
 
@@ -161,4 +161,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_TIME_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_TIME_H_
diff --git a/include/perfetto/base/unix_socket.h b/include/perfetto/ext/base/unix_socket.h
similarity index 97%
rename from include/perfetto/base/unix_socket.h
rename to include/perfetto/ext/base/unix_socket.h
index f2b8003..b4e8e04 100644
--- a/include/perfetto/base/unix_socket.h
+++ b/include/perfetto/ext/base/unix_socket.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_UNIX_SOCKET_H_
-#define INCLUDE_PERFETTO_BASE_UNIX_SOCKET_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_UNIX_SOCKET_H_
+#define INCLUDE_PERFETTO_EXT_BASE_UNIX_SOCKET_H_
 
 #include <stdint.h>
 #include <sys/types.h>
@@ -24,9 +24,9 @@
 #include <string>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/base/weak_ptr.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/base/weak_ptr.h"
 
 struct msghdr;
 
@@ -329,4 +329,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_UNIX_SOCKET_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_UNIX_SOCKET_H_
diff --git a/include/perfetto/base/unix_task_runner.h b/include/perfetto/ext/base/unix_task_runner.h
similarity index 90%
rename from include/perfetto/base/unix_task_runner.h
rename to include/perfetto/ext/base/unix_task_runner.h
index 98c9c7e..d25bc52 100644
--- a/include/perfetto/base/unix_task_runner.h
+++ b/include/perfetto/ext/base/unix_task_runner.h
@@ -14,16 +14,16 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_UNIX_TASK_RUNNER_H_
-#define INCLUDE_PERFETTO_BASE_UNIX_TASK_RUNNER_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_UNIX_TASK_RUNNER_H_
+#define INCLUDE_PERFETTO_EXT_BASE_UNIX_TASK_RUNNER_H_
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/event.h"
-#include "perfetto/base/scoped_file.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/base/thread_utils.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/event.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/base/thread_utils.h"
+#include "perfetto/ext/base/time.h"
 
 #include <poll.h>
 #include <chrono>
@@ -117,4 +117,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_UNIX_TASK_RUNNER_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_UNIX_TASK_RUNNER_H_
diff --git a/include/perfetto/base/utils.h b/include/perfetto/ext/base/utils.h
similarity index 66%
rename from include/perfetto/base/utils.h
rename to include/perfetto/ext/base/utils.h
index 3669cb1..884ee5e 100644
--- a/include/perfetto/base/utils.h
+++ b/include/perfetto/ext/base/utils.h
@@ -14,10 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_UTILS_H_
-#define INCLUDE_PERFETTO_BASE_UTILS_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_UTILS_H_
+#define INCLUDE_PERFETTO_EXT_BASE_UTILS_H_
 
 #include "perfetto/base/build_config.h"
+#include "perfetto/base/compiler.h"
 
 #include <errno.h>
 #include <stddef.h>
@@ -35,9 +36,6 @@
     eintr_wrapper_result;                                   \
   })
 
-#define PERFETTO_LIKELY(_x) __builtin_expect(!!(_x), 1)
-#define PERFETTO_UNLIKELY(_x) __builtin_expect(!!(_x), 0)
-
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 // TODO(brucedawson) - create a ::perfetto::base::IOSize to replace this.
 #if defined(_WIN64)
@@ -47,39 +45,6 @@
 #endif
 #endif
 
-#if defined(__GNUC__) || defined(__clang__)
-#define PERFETTO_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
-#else
-#define PERFETTO_WARN_UNUSED_RESULT
-#endif
-
-#if defined(__clang__)
-#define PERFETTO_ALWAYS_INLINE __attribute__((__always_inline__))
-#else
-// GCC is too pedantic and often fails with the error:
-// "always_inline function might not be inlinable"
-#define PERFETTO_ALWAYS_INLINE
-#endif
-
-// TODO(lalitm): is_trivially_constructible is currently not available
-// in some environments we build in. Reenable when that environment supports
-// this.
-#if defined(__GLIBCXX__)
-#define PERFETTO_IS_TRIVIALLY_CONSTRUCTIBLE(T) true
-#else
-#define PERFETTO_IS_TRIVIALLY_CONSTRUCTIBLE(T) \
-  std::is_trivially_constructible<T>::value
-#endif
-
-// TODO(lalitm): is_trivially_copyable is currently not available
-// in some environments we build in. Reenable when that environment supports
-// this.
-#if defined(__GLIBCXX__)
-#define PERFETTO_IS_TRIVIALLY_COPYABLE(T) true
-#else
-#define PERFETTO_IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
-#endif
-
 namespace perfetto {
 namespace base {
 
@@ -96,9 +61,6 @@
   return sizeof(array) / sizeof(array[0]);
 }
 
-template <typename... T>
-inline void ignore_result(const T&...) {}
-
 // Function object which invokes 'free' on its parameter, which must be
 // a pointer. Can be used to store malloc-allocated pointers in std::unique_ptr:
 //
@@ -129,4 +91,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_UTILS_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_UTILS_H_
diff --git a/include/perfetto/ext/base/watchdog.h b/include/perfetto/ext/base/watchdog.h
new file mode 100644
index 0000000..02f1918
--- /dev/null
+++ b/include/perfetto/ext/base/watchdog.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_H_
+#define INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_H_
+
+#include <functional>
+
+#include "perfetto/base/build_config.h"
+
+#if (PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||    \
+     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)) && \
+    !PERFETTO_BUILDFLAG(PERFETTO_EMBEDDER_BUILD)
+#include "perfetto/ext/base/watchdog_posix.h"
+#else
+#include "perfetto/ext/base/watchdog_noop.h"
+#endif
+
+namespace perfetto {
+namespace base {
+
+inline void RunTaskWithWatchdogGuard(const std::function<void()>& task) {
+  // Maximum time a single task can take in a TaskRunner before the
+  // program suicides.
+  constexpr int64_t kWatchdogMillis = 30000;  // 30s
+
+  Watchdog::Timer handle =
+      base::Watchdog::GetInstance()->CreateFatalTimer(kWatchdogMillis);
+  task();
+}
+
+}  // namespace base
+}  // namespace perfetto
+
+#endif  // INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_H_
diff --git a/include/perfetto/base/watchdog_noop.h b/include/perfetto/ext/base/watchdog_noop.h
similarity index 88%
rename from include/perfetto/base/watchdog_noop.h
rename to include/perfetto/ext/base/watchdog_noop.h
index e542aa6..d678063 100644
--- a/include/perfetto/base/watchdog_noop.h
+++ b/include/perfetto/ext/base/watchdog_noop.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_WATCHDOG_NOOP_H_
-#define INCLUDE_PERFETTO_BASE_WATCHDOG_NOOP_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_NOOP_H_
+#define INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_NOOP_H_
 
 #include <stdint.h>
 
@@ -44,4 +44,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_WATCHDOG_NOOP_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_NOOP_H_
diff --git a/include/perfetto/base/watchdog_posix.h b/include/perfetto/ext/base/watchdog_posix.h
similarity index 95%
rename from include/perfetto/base/watchdog_posix.h
rename to include/perfetto/ext/base/watchdog_posix.h
index a7080df..40e3289 100644
--- a/include/perfetto/base/watchdog_posix.h
+++ b/include/perfetto/ext/base/watchdog_posix.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_WATCHDOG_POSIX_H_
-#define INCLUDE_PERFETTO_BASE_WATCHDOG_POSIX_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_POSIX_H_
+#define INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_POSIX_H_
 
-#include "perfetto/base/thread_checker.h"
+#include "perfetto/ext/base/thread_checker.h"
 
 #include <atomic>
 #include <condition_variable>
@@ -151,4 +151,4 @@
 
 }  // namespace base
 }  // namespace perfetto
-#endif  // INCLUDE_PERFETTO_BASE_WATCHDOG_POSIX_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_POSIX_H_
diff --git a/include/perfetto/base/weak_ptr.h b/include/perfetto/ext/base/weak_ptr.h
similarity index 93%
rename from include/perfetto/base/weak_ptr.h
rename to include/perfetto/ext/base/weak_ptr.h
index 369b70e..7ba4ca3 100644
--- a/include/perfetto/base/weak_ptr.h
+++ b/include/perfetto/ext/base/weak_ptr.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_BASE_WEAK_PTR_H_
-#define INCLUDE_PERFETTO_BASE_WEAK_PTR_H_
+#ifndef INCLUDE_PERFETTO_EXT_BASE_WEAK_PTR_H_
+#define INCLUDE_PERFETTO_EXT_BASE_WEAK_PTR_H_
 
-#include "perfetto/base/thread_checker.h"
+#include "perfetto/ext/base/thread_checker.h"
 
 #include <memory>
 
@@ -104,4 +104,4 @@
 }  // namespace base
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_BASE_WEAK_PTR_H_
+#endif  // INCLUDE_PERFETTO_EXT_BASE_WEAK_PTR_H_
diff --git a/include/perfetto/ipc/BUILD.gn b/include/perfetto/ext/ipc/BUILD.gn
similarity index 100%
rename from include/perfetto/ipc/BUILD.gn
rename to include/perfetto/ext/ipc/BUILD.gn
diff --git a/include/perfetto/ipc/async_result.h b/include/perfetto/ext/ipc/async_result.h
similarity index 91%
rename from include/perfetto/ipc/async_result.h
rename to include/perfetto/ext/ipc/async_result.h
index e3178c3..cb4d849 100644
--- a/include/perfetto/ipc/async_result.h
+++ b/include/perfetto/ext/ipc/async_result.h
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_ASYNC_RESULT_H_
-#define INCLUDE_PERFETTO_IPC_ASYNC_RESULT_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_ASYNC_RESULT_H_
+#define INCLUDE_PERFETTO_EXT_IPC_ASYNC_RESULT_H_
 
 #include <memory>
 #include <type_traits>
 #include <utility>
 
-#include "perfetto/ipc/basic_types.h"
+#include "perfetto/ext/ipc/basic_types.h"
 
 namespace perfetto {
 namespace ipc {
@@ -71,4 +71,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_ASYNC_RESULT_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_ASYNC_RESULT_H_
diff --git a/include/perfetto/ipc/basic_types.h b/include/perfetto/ext/ipc/basic_types.h
similarity index 89%
rename from include/perfetto/ipc/basic_types.h
rename to include/perfetto/ext/ipc/basic_types.h
index 2d14085..793cc19 100644
--- a/include/perfetto/ipc/basic_types.h
+++ b/include/perfetto/ext/ipc/basic_types.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_BASIC_TYPES_H_
-#define INCLUDE_PERFETTO_IPC_BASIC_TYPES_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_BASIC_TYPES_H_
+#define INCLUDE_PERFETTO_EXT_IPC_BASIC_TYPES_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -46,4 +46,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_BASIC_TYPES_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_BASIC_TYPES_H_
diff --git a/include/perfetto/ipc/client.h b/include/perfetto/ext/ipc/client.h
similarity index 89%
rename from include/perfetto/ipc/client.h
rename to include/perfetto/ext/ipc/client.h
index cd802d1..72c3e73 100644
--- a/include/perfetto/ipc/client.h
+++ b/include/perfetto/ext/ipc/client.h
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_CLIENT_H_
-#define INCLUDE_PERFETTO_IPC_CLIENT_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_CLIENT_H_
+#define INCLUDE_PERFETTO_EXT_IPC_CLIENT_H_
 
 #include <functional>
 #include <memory>
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/ipc/basic_types.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/ipc/basic_types.h"
 
 namespace perfetto {
 
@@ -66,4 +66,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_CLIENT_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_CLIENT_H_
diff --git a/include/perfetto/ipc/client_info.h b/include/perfetto/ext/ipc/client_info.h
similarity index 89%
rename from include/perfetto/ipc/client_info.h
rename to include/perfetto/ext/ipc/client_info.h
index 5c4fe5b..0b43f9a 100644
--- a/include/perfetto/ipc/client_info.h
+++ b/include/perfetto/ext/ipc/client_info.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_CLIENT_INFO_H_
-#define INCLUDE_PERFETTO_IPC_CLIENT_INFO_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_CLIENT_INFO_H_
+#define INCLUDE_PERFETTO_EXT_IPC_CLIENT_INFO_H_
 
 #include <unistd.h>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/ipc/basic_types.h"
+#include "perfetto/ext/ipc/basic_types.h"
 
 namespace perfetto {
 namespace ipc {
@@ -59,4 +59,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_CLIENT_INFO_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_CLIENT_INFO_H_
diff --git a/include/perfetto/ipc/codegen_helpers.h b/include/perfetto/ext/ipc/codegen_helpers.h
similarity index 91%
rename from include/perfetto/ipc/codegen_helpers.h
rename to include/perfetto/ext/ipc/codegen_helpers.h
index e4cec82..90ca49d 100644
--- a/include/perfetto/ipc/codegen_helpers.h
+++ b/include/perfetto/ext/ipc/codegen_helpers.h
@@ -16,8 +16,8 @@
 
 // This file is only meant to be included in autogenerated .cc files.
 
-#ifndef INCLUDE_PERFETTO_IPC_CODEGEN_HELPERS_H_
-#define INCLUDE_PERFETTO_IPC_CODEGEN_HELPERS_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_CODEGEN_HELPERS_H_
+#define INCLUDE_PERFETTO_EXT_IPC_CODEGEN_HELPERS_H_
 
 // A templated protobuf message decoder. Returns nullptr in case of failure.
 template <typename T>
@@ -45,4 +45,4 @@
       ::perfetto::ipc::Deferred<TReply>(::std::move(reply)));
 }
 
-#endif  // INCLUDE_PERFETTO_IPC_CODEGEN_HELPERS_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_CODEGEN_HELPERS_H_
diff --git a/include/perfetto/ipc/deferred.h b/include/perfetto/ext/ipc/deferred.h
similarity index 95%
rename from include/perfetto/ipc/deferred.h
rename to include/perfetto/ext/ipc/deferred.h
index b1740b0..8b58920 100644
--- a/include/perfetto/ipc/deferred.h
+++ b/include/perfetto/ext/ipc/deferred.h
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_DEFERRED_H_
-#define INCLUDE_PERFETTO_IPC_DEFERRED_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_DEFERRED_H_
+#define INCLUDE_PERFETTO_EXT_IPC_DEFERRED_H_
 
 #include <functional>
 #include <memory>
 #include <utility>
 
-#include "perfetto/ipc/async_result.h"
-#include "perfetto/ipc/basic_types.h"
+#include "perfetto/ext/ipc/async_result.h"
+#include "perfetto/ext/ipc/basic_types.h"
 
 namespace perfetto {
 namespace ipc {
@@ -137,4 +137,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_DEFERRED_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_DEFERRED_H_
diff --git a/include/perfetto/ipc/host.h b/include/perfetto/ext/ipc/host.h
similarity index 90%
rename from include/perfetto/ipc/host.h
rename to include/perfetto/ext/ipc/host.h
index a3abb99..2c02c29 100644
--- a/include/perfetto/ipc/host.h
+++ b/include/perfetto/ext/ipc/host.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_HOST_H_
-#define INCLUDE_PERFETTO_IPC_HOST_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_HOST_H_
+#define INCLUDE_PERFETTO_EXT_IPC_HOST_H_
 
 #include <memory>
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/ipc/basic_types.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/ipc/basic_types.h"
 
 namespace perfetto {
 
@@ -62,4 +62,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_HOST_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_HOST_H_
diff --git a/include/perfetto/ipc/service.h b/include/perfetto/ext/ipc/service.h
similarity index 90%
rename from include/perfetto/ipc/service.h
rename to include/perfetto/ext/ipc/service.h
index 18dc1be..ea49834 100644
--- a/include/perfetto/ipc/service.h
+++ b/include/perfetto/ext/ipc/service.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_SERVICE_H_
-#define INCLUDE_PERFETTO_IPC_SERVICE_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_SERVICE_H_
+#define INCLUDE_PERFETTO_EXT_IPC_SERVICE_H_
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/ipc/client_info.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/ipc/client_info.h"
 
 namespace perfetto {
 namespace ipc {
@@ -71,4 +71,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_SERVICE_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_SERVICE_H_
diff --git a/include/perfetto/ipc/service_descriptor.h b/include/perfetto/ext/ipc/service_descriptor.h
similarity index 90%
rename from include/perfetto/ipc/service_descriptor.h
rename to include/perfetto/ext/ipc/service_descriptor.h
index c4aca7e..97eb9bf 100644
--- a/include/perfetto/ipc/service_descriptor.h
+++ b/include/perfetto/ext/ipc/service_descriptor.h
@@ -14,16 +14,16 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_SERVICE_DESCRIPTOR_H_
-#define INCLUDE_PERFETTO_IPC_SERVICE_DESCRIPTOR_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_SERVICE_DESCRIPTOR_H_
+#define INCLUDE_PERFETTO_EXT_IPC_SERVICE_DESCRIPTOR_H_
 
 #include <functional>
 #include <string>
 #include <utility>
 #include <vector>
 
-#include "perfetto/ipc/basic_types.h"
-#include "perfetto/ipc/deferred.h"
+#include "perfetto/ext/ipc/basic_types.h"
+#include "perfetto/ext/ipc/deferred.h"
 
 namespace perfetto {
 namespace ipc {
@@ -69,4 +69,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_SERVICE_DESCRIPTOR_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_SERVICE_DESCRIPTOR_H_
diff --git a/include/perfetto/ipc/service_proxy.h b/include/perfetto/ext/ipc/service_proxy.h
similarity index 91%
rename from include/perfetto/ipc/service_proxy.h
rename to include/perfetto/ext/ipc/service_proxy.h
index a0107da..98f111c 100644
--- a/include/perfetto/ipc/service_proxy.h
+++ b/include/perfetto/ext/ipc/service_proxy.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_IPC_SERVICE_PROXY_H_
-#define INCLUDE_PERFETTO_IPC_SERVICE_PROXY_H_
+#ifndef INCLUDE_PERFETTO_EXT_IPC_SERVICE_PROXY_H_
+#define INCLUDE_PERFETTO_EXT_IPC_SERVICE_PROXY_H_
 
-#include "perfetto/ipc/basic_types.h"
+#include "perfetto/ext/ipc/basic_types.h"
 
 #include <assert.h>
 
@@ -26,8 +26,8 @@
 #include <memory>
 #include <string>
 
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/ipc/deferred.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/ipc/deferred.h"
 
 namespace perfetto {
 namespace ipc {
@@ -98,4 +98,4 @@
 }  // namespace ipc
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_IPC_SERVICE_PROXY_H_
+#endif  // INCLUDE_PERFETTO_EXT_IPC_SERVICE_PROXY_H_
diff --git a/include/perfetto/traced/BUILD.gn b/include/perfetto/ext/traced/BUILD.gn
similarity index 90%
rename from include/perfetto/traced/BUILD.gn
rename to include/perfetto/ext/traced/BUILD.gn
index c0c9316..3e7df3a 100644
--- a/include/perfetto/traced/BUILD.gn
+++ b/include/perfetto/ext/traced/BUILD.gn
@@ -21,8 +21,8 @@
 
 source_set("sys_stats_counters") {
   deps = [
-    "../../../gn:default_deps",
-    "../../../protos/perfetto/common:zero",
+    "../../../../gn:default_deps",
+    "../../../../protos/perfetto/common:zero",
     "../base",
   ]
   sources = [
diff --git a/include/perfetto/traced/data_source_types.h b/include/perfetto/ext/traced/data_source_types.h
similarity index 91%
rename from include/perfetto/traced/data_source_types.h
rename to include/perfetto/ext/traced/data_source_types.h
index 6a76e50..4ed362b 100644
--- a/include/perfetto/traced/data_source_types.h
+++ b/include/perfetto/ext/traced/data_source_types.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACED_DATA_SOURCE_TYPES_H_
-#define INCLUDE_PERFETTO_TRACED_DATA_SOURCE_TYPES_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACED_DATA_SOURCE_TYPES_H_
+#define INCLUDE_PERFETTO_EXT_TRACED_DATA_SOURCE_TYPES_H_
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -60,4 +60,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACED_DATA_SOURCE_TYPES_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACED_DATA_SOURCE_TYPES_H_
diff --git a/include/perfetto/traced/sys_stats_counters.h b/include/perfetto/ext/traced/sys_stats_counters.h
similarity index 97%
rename from include/perfetto/traced/sys_stats_counters.h
rename to include/perfetto/ext/traced/sys_stats_counters.h
index 98d25af..48f2fcb 100644
--- a/include/perfetto/traced/sys_stats_counters.h
+++ b/include/perfetto/ext/traced/sys_stats_counters.h
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACED_SYS_STATS_COUNTERS_H_
-#define INCLUDE_PERFETTO_TRACED_SYS_STATS_COUNTERS_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACED_SYS_STATS_COUNTERS_H_
+#define INCLUDE_PERFETTO_EXT_TRACED_SYS_STATS_COUNTERS_H_
 
-#include "perfetto/base/utils.h"
 #include "perfetto/common/sys_stats_counters.pbzero.h"
+#include "perfetto/ext/base/utils.h"
 
 #include <vector>
 
@@ -232,4 +232,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACED_SYS_STATS_COUNTERS_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACED_SYS_STATS_COUNTERS_H_
diff --git a/include/perfetto/traced/traced.h b/include/perfetto/ext/traced/traced.h
similarity index 83%
rename from include/perfetto/traced/traced.h
rename to include/perfetto/ext/traced/traced.h
index dd58d63..042f9ec 100644
--- a/include/perfetto/traced/traced.h
+++ b/include/perfetto/ext/traced/traced.h
@@ -14,10 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACED_TRACED_H_
-#define INCLUDE_PERFETTO_TRACED_TRACED_H_
-
-#include "perfetto/base/build_config.h"
+#ifndef INCLUDE_PERFETTO_EXT_TRACED_TRACED_H_
+#define INCLUDE_PERFETTO_EXT_TRACED_TRACED_H_
 
 namespace perfetto {
 
@@ -28,4 +26,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACED_TRACED_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACED_TRACED_H_
diff --git a/include/perfetto/tracing/core/BUILD.gn b/include/perfetto/ext/tracing/core/BUILD.gn
similarity index 100%
rename from include/perfetto/tracing/core/BUILD.gn
rename to include/perfetto/ext/tracing/core/BUILD.gn
diff --git a/include/perfetto/tracing/core/basic_types.h b/include/perfetto/ext/tracing/core/basic_types.h
similarity index 93%
rename from include/perfetto/tracing/core/basic_types.h
rename to include/perfetto/ext/tracing/core/basic_types.h
index a7bbdf2..cf05dd4 100644
--- a/include/perfetto/tracing/core/basic_types.h
+++ b/include/perfetto/ext/tracing/core/basic_types.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_BASIC_TYPES_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_BASIC_TYPES_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_BASIC_TYPES_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_BASIC_TYPES_H_
 
 #include "perfetto/base/build_config.h"
 
@@ -80,4 +80,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_BASIC_TYPES_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_BASIC_TYPES_H_
diff --git a/include/perfetto/tracing/core/chrome_config.h b/include/perfetto/ext/tracing/core/chrome_config.h
similarity index 93%
rename from include/perfetto/tracing/core/chrome_config.h
rename to include/perfetto/ext/tracing/core/chrome_config.h
index 8dc87a6..f6a729e 100644
--- a/include/perfetto/tracing/core/chrome_config.h
+++ b/include/perfetto/ext/tracing/core/chrome_config.h
@@ -25,8 +25,8 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_CHROME_CONFIG_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_CHROME_CONFIG_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_CHROME_CONFIG_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_CHROME_CONFIG_H_
 
 #include <stdint.h>
 #include <string>
@@ -80,4 +80,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_CHROME_CONFIG_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_CHROME_CONFIG_H_
diff --git a/include/perfetto/tracing/core/commit_data_request.h b/include/perfetto/ext/tracing/core/commit_data_request.h
similarity index 97%
rename from include/perfetto/tracing/core/commit_data_request.h
rename to include/perfetto/ext/tracing/core/commit_data_request.h
index b93576d..ca68a9e 100644
--- a/include/perfetto/tracing/core/commit_data_request.h
+++ b/include/perfetto/ext/tracing/core/commit_data_request.h
@@ -25,8 +25,8 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_COMMIT_DATA_REQUEST_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_COMMIT_DATA_REQUEST_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_COMMIT_DATA_REQUEST_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_COMMIT_DATA_REQUEST_H_
 
 #include <stdint.h>
 #include <string>
@@ -238,4 +238,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_COMMIT_DATA_REQUEST_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_COMMIT_DATA_REQUEST_H_
diff --git a/include/perfetto/tracing/core/consumer.h b/include/perfetto/ext/tracing/core/consumer.h
similarity index 91%
rename from include/perfetto/tracing/core/consumer.h
rename to include/perfetto/ext/tracing/core/consumer.h
index cf92253..b8d722a 100644
--- a/include/perfetto/tracing/core/consumer.h
+++ b/include/perfetto/ext/tracing/core/consumer.h
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_CONSUMER_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_CONSUMER_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_CONSUMER_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_CONSUMER_H_
 
 #include <vector>
 
 #include "perfetto/base/export.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/observable_events.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/observable_events.h"
 
 namespace perfetto {
 
@@ -82,4 +82,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_CONSUMER_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_CONSUMER_H_
diff --git a/include/perfetto/tracing/core/data_source_config.h b/include/perfetto/ext/tracing/core/data_source_config.h
similarity index 94%
rename from include/perfetto/tracing/core/data_source_config.h
rename to include/perfetto/ext/tracing/core/data_source_config.h
index e0db801..c841101 100644
--- a/include/perfetto/tracing/core/data_source_config.h
+++ b/include/perfetto/ext/tracing/core/data_source_config.h
@@ -25,8 +25,8 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_DATA_SOURCE_CONFIG_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_DATA_SOURCE_CONFIG_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_DATA_SOURCE_CONFIG_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_DATA_SOURCE_CONFIG_H_
 
 #include <stdint.h>
 #include <string>
@@ -35,8 +35,8 @@
 
 #include "perfetto/base/export.h"
 
-#include "perfetto/tracing/core/chrome_config.h"
-#include "perfetto/tracing/core/test_config.h"
+#include "perfetto/ext/tracing/core/chrome_config.h"
+#include "perfetto/ext/tracing/core/test_config.h"
 
 // Forward declarations for protobuf types.
 namespace perfetto {
@@ -160,4 +160,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_DATA_SOURCE_CONFIG_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_DATA_SOURCE_CONFIG_H_
diff --git a/include/perfetto/tracing/core/data_source_descriptor.h b/include/perfetto/ext/tracing/core/data_source_descriptor.h
similarity index 93%
rename from include/perfetto/tracing/core/data_source_descriptor.h
rename to include/perfetto/ext/tracing/core/data_source_descriptor.h
index 2653c8f..652261d 100644
--- a/include/perfetto/tracing/core/data_source_descriptor.h
+++ b/include/perfetto/ext/tracing/core/data_source_descriptor.h
@@ -25,8 +25,8 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_DATA_SOURCE_DESCRIPTOR_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_DATA_SOURCE_DESCRIPTOR_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_DATA_SOURCE_DESCRIPTOR_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_DATA_SOURCE_DESCRIPTOR_H_
 
 #include <stdint.h>
 #include <string>
@@ -92,4 +92,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_DATA_SOURCE_DESCRIPTOR_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_DATA_SOURCE_DESCRIPTOR_H_
diff --git a/include/perfetto/tracing/core/observable_events.h b/include/perfetto/ext/tracing/core/observable_events.h
similarity index 95%
rename from include/perfetto/tracing/core/observable_events.h
rename to include/perfetto/ext/tracing/core/observable_events.h
index 8ce8378..2ba9da7 100644
--- a/include/perfetto/tracing/core/observable_events.h
+++ b/include/perfetto/ext/tracing/core/observable_events.h
@@ -25,8 +25,8 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_OBSERVABLE_EVENTS_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_OBSERVABLE_EVENTS_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_OBSERVABLE_EVENTS_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_OBSERVABLE_EVENTS_H_
 
 #include <stdint.h>
 #include <string>
@@ -138,4 +138,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_OBSERVABLE_EVENTS_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_OBSERVABLE_EVENTS_H_
diff --git a/include/perfetto/tracing/core/producer.h b/include/perfetto/ext/tracing/core/producer.h
similarity index 96%
rename from include/perfetto/tracing/core/producer.h
rename to include/perfetto/ext/tracing/core/producer.h
index f383b7e..30775f7 100644
--- a/include/perfetto/tracing/core/producer.h
+++ b/include/perfetto/ext/tracing/core/producer.h
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_PRODUCER_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_PRODUCER_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_PRODUCER_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_PRODUCER_H_
 
 #include "perfetto/base/export.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 
 namespace perfetto {
 
@@ -125,4 +125,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_PRODUCER_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_PRODUCER_H_
diff --git a/include/perfetto/tracing/core/shared_memory.h b/include/perfetto/ext/tracing/core/shared_memory.h
similarity index 90%
rename from include/perfetto/tracing/core/shared_memory.h
rename to include/perfetto/ext/tracing/core/shared_memory.h
index b2ca791..3c77e07 100644
--- a/include/perfetto/tracing/core/shared_memory.h
+++ b/include/perfetto/ext/tracing/core/shared_memory.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_H_
 
 #include <stddef.h>
 
@@ -50,4 +50,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_H_
diff --git a/include/perfetto/tracing/core/shared_memory_abi.h b/include/perfetto/ext/tracing/core/shared_memory_abi.h
similarity index 98%
rename from include/perfetto/tracing/core/shared_memory_abi.h
rename to include/perfetto/ext/tracing/core/shared_memory_abi.h
index 7a5a14c..b92da2e 100644
--- a/include/perfetto/tracing/core/shared_memory_abi.h
+++ b/include/perfetto/ext/tracing/core/shared_memory_abi.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ABI_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ABI_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_ABI_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_ABI_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -578,4 +578,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ABI_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_ABI_H_
diff --git a/include/perfetto/tracing/core/shared_memory_arbiter.h b/include/perfetto/ext/tracing/core/shared_memory_arbiter.h
similarity index 90%
rename from include/perfetto/tracing/core/shared_memory_arbiter.h
rename to include/perfetto/ext/tracing/core/shared_memory_arbiter.h
index 660014c..9c34e50 100644
--- a/include/perfetto/tracing/core/shared_memory_arbiter.h
+++ b/include/perfetto/ext/tracing/core/shared_memory_arbiter.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ARBITER_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ARBITER_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_ARBITER_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_ARBITER_H_
 
 #include <stddef.h>
 
@@ -24,8 +24,8 @@
 #include <vector>
 
 #include "perfetto/base/export.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 namespace perfetto {
 
@@ -85,4 +85,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ARBITER_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_ARBITER_H_
diff --git a/include/perfetto/tracing/core/slice.h b/include/perfetto/ext/tracing/core/slice.h
similarity index 92%
rename from include/perfetto/tracing/core/slice.h
rename to include/perfetto/ext/tracing/core/slice.h
index 954e58d..b2db3d0 100644
--- a/include/perfetto/tracing/core/slice.h
+++ b/include/perfetto/ext/tracing/core/slice.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_SLICE_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_SLICE_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_SLICE_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_SLICE_H_
 
 #include <stddef.h>
 #include <string.h>
@@ -72,4 +72,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_SLICE_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_SLICE_H_
diff --git a/include/perfetto/tracing/core/startup_trace_writer.h b/include/perfetto/ext/tracing/core/startup_trace_writer.h
similarity index 94%
rename from include/perfetto/tracing/core/startup_trace_writer.h
rename to include/perfetto/ext/tracing/core/startup_trace_writer.h
index 9e3642a..700af74 100644
--- a/include/perfetto/tracing/core/startup_trace_writer.h
+++ b/include/perfetto/ext/tracing/core/startup_trace_writer.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_STARTUP_TRACE_WRITER_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_STARTUP_TRACE_WRITER_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_STARTUP_TRACE_WRITER_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_STARTUP_TRACE_WRITER_H_
 
 #include <memory>
 #include <mutex>
@@ -23,13 +23,13 @@
 #include <vector>
 
 #include "perfetto/base/export.h"
-#include "perfetto/base/optional.h"
-#include "perfetto/base/thread_checker.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "perfetto/protozero/message_handle.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "perfetto/protozero/scattered_stream_writer.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/trace_writer.h"
 
 namespace perfetto {
 
@@ -180,4 +180,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_STARTUP_TRACE_WRITER_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_STARTUP_TRACE_WRITER_H_
diff --git a/include/perfetto/tracing/core/startup_trace_writer_registry.h b/include/perfetto/ext/tracing/core/startup_trace_writer_registry.h
similarity index 93%
rename from include/perfetto/tracing/core/startup_trace_writer_registry.h
rename to include/perfetto/ext/tracing/core/startup_trace_writer_registry.h
index 0cd035d..cdd7931 100644
--- a/include/perfetto/tracing/core/startup_trace_writer_registry.h
+++ b/include/perfetto/ext/tracing/core/startup_trace_writer_registry.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_STARTUP_TRACE_WRITER_REGISTRY_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_STARTUP_TRACE_WRITER_REGISTRY_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_STARTUP_TRACE_WRITER_REGISTRY_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_STARTUP_TRACE_WRITER_REGISTRY_H_
 
 #include <functional>
 #include <memory>
@@ -24,8 +24,8 @@
 #include <vector>
 
 #include "perfetto/base/export.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 
 namespace perfetto {
 
@@ -140,4 +140,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_STARTUP_TRACE_WRITER_REGISTRY_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_STARTUP_TRACE_WRITER_REGISTRY_H_
diff --git a/include/perfetto/tracing/core/test_config.h b/include/perfetto/ext/tracing/core/test_config.h
similarity index 97%
rename from include/perfetto/tracing/core/test_config.h
rename to include/perfetto/ext/tracing/core/test_config.h
index 17caa70..1033f18 100644
--- a/include/perfetto/tracing/core/test_config.h
+++ b/include/perfetto/ext/tracing/core/test_config.h
@@ -25,8 +25,8 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_TEST_CONFIG_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_TEST_CONFIG_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_TEST_CONFIG_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_TEST_CONFIG_H_
 
 #include <stdint.h>
 #include <string>
@@ -184,4 +184,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_TEST_CONFIG_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_TEST_CONFIG_H_
diff --git a/include/perfetto/tracing/core/trace_config.h b/include/perfetto/ext/tracing/core/trace_config.h
similarity index 98%
rename from include/perfetto/tracing/core/trace_config.h
rename to include/perfetto/ext/tracing/core/trace_config.h
index 284a84b..7aaa95b 100644
--- a/include/perfetto/tracing/core/trace_config.h
+++ b/include/perfetto/ext/tracing/core/trace_config.h
@@ -25,8 +25,8 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_TRACE_CONFIG_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_TRACE_CONFIG_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_CONFIG_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_CONFIG_H_
 
 #include <stdint.h>
 #include <string>
@@ -35,7 +35,7 @@
 
 #include "perfetto/base/export.h"
 
-#include "perfetto/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
 
 // Forward declarations for protobuf types.
 namespace perfetto {
@@ -601,4 +601,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_TRACE_CONFIG_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_CONFIG_H_
diff --git a/include/perfetto/tracing/core/trace_packet.h b/include/perfetto/ext/tracing/core/trace_packet.h
similarity index 94%
rename from include/perfetto/tracing/core/trace_packet.h
rename to include/perfetto/ext/tracing/core/trace_packet.h
index cdf61aa..308a2ef 100644
--- a/include/perfetto/tracing/core/trace_packet.h
+++ b/include/perfetto/ext/tracing/core/trace_packet.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_TRACE_PACKET_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_TRACE_PACKET_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_PACKET_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_PACKET_H_
 
 #include <stddef.h>
 #include <memory>
@@ -24,7 +24,7 @@
 #include <google/protobuf/io/zero_copy_stream.h>
 #include "perfetto/base/export.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/tracing/core/slice.h"
+#include "perfetto/ext/tracing/core/slice.h"
 
 namespace perfetto {
 
@@ -103,4 +103,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_TRACE_PACKET_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_PACKET_H_
diff --git a/include/perfetto/tracing/core/trace_stats.h b/include/perfetto/ext/tracing/core/trace_stats.h
similarity index 97%
rename from include/perfetto/tracing/core/trace_stats.h
rename to include/perfetto/ext/tracing/core/trace_stats.h
index 9eaa86d..184227a 100644
--- a/include/perfetto/tracing/core/trace_stats.h
+++ b/include/perfetto/ext/tracing/core/trace_stats.h
@@ -25,8 +25,8 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_TRACE_STATS_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_TRACE_STATS_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_STATS_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_STATS_H_
 
 #include <stdint.h>
 #include <string>
@@ -225,4 +225,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_TRACE_STATS_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_STATS_H_
diff --git a/include/perfetto/tracing/core/trace_writer.h b/include/perfetto/ext/tracing/core/trace_writer.h
similarity index 93%
rename from include/perfetto/tracing/core/trace_writer.h
rename to include/perfetto/ext/tracing/core/trace_writer.h
index 8df1567..7af1bbf 100644
--- a/include/perfetto/tracing/core/trace_writer.h
+++ b/include/perfetto/ext/tracing/core/trace_writer.h
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_TRACE_WRITER_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_TRACE_WRITER_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_WRITER_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_WRITER_H_
 
 #include <functional>
 
 #include "perfetto/base/export.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 #include "perfetto/protozero/message_handle.h"
-#include "perfetto/public/trace_writer_base.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/tracing/trace_writer_base.h"
 
 namespace perfetto {
 
@@ -95,4 +95,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_TRACE_WRITER_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACE_WRITER_H_
diff --git a/include/perfetto/tracing/core/tracing_service.h b/include/perfetto/ext/tracing/core/tracing_service.h
similarity index 97%
rename from include/perfetto/tracing/core/tracing_service.h
rename to include/perfetto/ext/tracing/core/tracing_service.h
index fd10957..c24e629 100644
--- a/include/perfetto/tracing/core/tracing_service.h
+++ b/include/perfetto/ext/tracing/core/tracing_service.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_TRACING_SERVICE_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_TRACING_SERVICE_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACING_SERVICE_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACING_SERVICE_H_
 
 #include <stdint.h>
 
@@ -24,9 +24,9 @@
 #include <vector>
 
 #include "perfetto/base/export.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/shared_memory.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
 
 namespace perfetto {
 
@@ -292,4 +292,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_TRACING_SERVICE_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_CORE_TRACING_SERVICE_H_
diff --git a/include/perfetto/tracing/ipc/BUILD.gn b/include/perfetto/ext/tracing/ipc/BUILD.gn
similarity index 100%
rename from include/perfetto/tracing/ipc/BUILD.gn
rename to include/perfetto/ext/tracing/ipc/BUILD.gn
diff --git a/include/perfetto/tracing/ipc/consumer_ipc_client.h b/include/perfetto/ext/tracing/ipc/consumer_ipc_client.h
similarity index 86%
rename from include/perfetto/tracing/ipc/consumer_ipc_client.h
rename to include/perfetto/ext/tracing/ipc/consumer_ipc_client.h
index 85ca354..b39d17c 100644
--- a/include/perfetto/tracing/ipc/consumer_ipc_client.h
+++ b/include/perfetto/ext/tracing/ipc/consumer_ipc_client.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_IPC_CONSUMER_IPC_CLIENT_H_
-#define INCLUDE_PERFETTO_TRACING_IPC_CONSUMER_IPC_CLIENT_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_IPC_CONSUMER_IPC_CLIENT_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_IPC_CONSUMER_IPC_CLIENT_H_
 
 #include <memory>
 #include <string>
 
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 namespace perfetto {
 
@@ -50,4 +50,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_IPC_CONSUMER_IPC_CLIENT_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_IPC_CONSUMER_IPC_CLIENT_H_
diff --git a/include/perfetto/tracing/ipc/producer_ipc_client.h b/include/perfetto/ext/tracing/ipc/producer_ipc_client.h
similarity index 88%
rename from include/perfetto/tracing/ipc/producer_ipc_client.h
rename to include/perfetto/ext/tracing/ipc/producer_ipc_client.h
index 533c97f..6f6a63e 100644
--- a/include/perfetto/tracing/ipc/producer_ipc_client.h
+++ b/include/perfetto/ext/tracing/ipc/producer_ipc_client.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_IPC_PRODUCER_IPC_CLIENT_H_
-#define INCLUDE_PERFETTO_TRACING_IPC_PRODUCER_IPC_CLIENT_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_IPC_PRODUCER_IPC_CLIENT_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_IPC_PRODUCER_IPC_CLIENT_H_
 
 #include <memory>
 #include <string>
 
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 namespace perfetto {
 
@@ -55,4 +55,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_IPC_PRODUCER_IPC_CLIENT_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_IPC_PRODUCER_IPC_CLIENT_H_
diff --git a/include/perfetto/tracing/ipc/service_ipc_host.h b/include/perfetto/ext/tracing/ipc/service_ipc_host.h
similarity index 87%
rename from include/perfetto/tracing/ipc/service_ipc_host.h
rename to include/perfetto/ext/tracing/ipc/service_ipc_host.h
index eeec568..86915af 100644
--- a/include/perfetto/tracing/ipc/service_ipc_host.h
+++ b/include/perfetto/ext/tracing/ipc/service_ipc_host.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_IPC_SERVICE_IPC_HOST_H_
-#define INCLUDE_PERFETTO_TRACING_IPC_SERVICE_IPC_HOST_H_
+#ifndef INCLUDE_PERFETTO_EXT_TRACING_IPC_SERVICE_IPC_HOST_H_
+#define INCLUDE_PERFETTO_EXT_TRACING_IPC_SERVICE_IPC_HOST_H_
 
 #include <memory>
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 
 namespace perfetto {
 namespace base {
@@ -62,4 +62,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_IPC_SERVICE_IPC_HOST_H_
+#endif  // INCLUDE_PERFETTO_EXT_TRACING_IPC_SERVICE_IPC_HOST_H_
diff --git a/include/perfetto/protozero/proto_decoder.h b/include/perfetto/protozero/proto_decoder.h
index 8089acf..fb65261 100644
--- a/include/perfetto/protozero/proto_decoder.h
+++ b/include/perfetto/protozero/proto_decoder.h
@@ -22,9 +22,8 @@
 #include <memory>
 #include <vector>
 
+#include "perfetto/base/compiler.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/utils.h"
 #include "perfetto/protozero/field.h"
 #include "perfetto/protozero/proto_utils.h"
 
diff --git a/include/perfetto/protozero/proto_utils.h b/include/perfetto/protozero/proto_utils.h
index 9dd5278..0ea5d5e 100644
--- a/include/perfetto/protozero/proto_utils.h
+++ b/include/perfetto/protozero/proto_utils.h
@@ -23,7 +23,6 @@
 #include <type_traits>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
 
 namespace protozero {
 namespace proto_utils {
diff --git a/include/perfetto/protozero/scattered_stream_writer.h b/include/perfetto/protozero/scattered_stream_writer.h
index 175a0aa..c508ad9 100644
--- a/include/perfetto/protozero/scattered_stream_writer.h
+++ b/include/perfetto/protozero/scattered_stream_writer.h
@@ -22,8 +22,8 @@
 #include <stdint.h>
 #include <string.h>
 
+#include "perfetto/base/compiler.h"
 #include "perfetto/base/export.h"
-#include "perfetto/base/utils.h"
 #include "perfetto/protozero/contiguous_memory_range.h"
 
 namespace protozero {
diff --git a/include/perfetto/public/BUILD.gn b/include/perfetto/public/BUILD.gn
index 31c0175..0ec88f1 100644
--- a/include/perfetto/public/BUILD.gn
+++ b/include/perfetto/public/BUILD.gn
@@ -13,24 +13,7 @@
 # limitations under the License.
 
 source_set("public") {
-  deps = [
-    "../../../gn:default_deps",
-  ]
-  public_deps = [
-    "../../../protos/perfetto/trace:zero",
-    "../protozero",
-  ]
   sources = [
-    "compiler.h",
-    "data_source.h",
-    "internal/basic_types.h",
-    "internal/data_source_internal.h",
-    "internal/tracing_muxer.h",
-    "internal/tracing_tls.h",
-    "locked_handle.h",
-    "platform.h",
-    "trace_writer_base.h",
-    "tracing.h",
-    "tracing_backend.h",
+    "consumer_api.h",
   ]
 }
diff --git a/include/perfetto/public/README.md b/include/perfetto/public/README.md
index 48d485a..e26e9c3 100644
--- a/include/perfetto/public/README.md
+++ b/include/perfetto/public/README.md
@@ -1,120 +1,3 @@
-# Perfetto public API surface
-
-**This API surface is not stable yet, don't depend on it**
-
-This folder contains the public perfetto API headers. This allows an app to
-inject trace events into perfetto with ~10 lines of code (see
-api_usage_example.cc).
-
-Headers in this folder must be hermetic. No other perfetto header must be leaked
-from the includes. This applies also to the public/internal directory.
-
-What is a client supposed to do to use tracing? See example below in this page.
-
-
-Source code layout: what goes where?
-------------------------------------
-
-There are four "public" directories in the perfetto codebase:
-
-**include/perfetto/public (this folder):**
-This is the only directory that embedders are allowed to access and depend on.
-This contains classes to: (i) use tracing; (ii) extend the tracing internals
-(i.e. implement the Platform).
-
-Rules:
-- This directory should contain only .h files and no .cc files.
-- Corresponding .cc files go into `src/public`.
-- .h files in here can depend only on:
-  (i) `include/perfetto/public/`,
-  (ii) `include/perfetto/public/internal/`.
-  (iii) `include/perfetto/protozero/`.
-
-**src/public:**
-Contains the .cc files that implement matching headers in
-`include/perfetto/public`. They can freely include other parts of the perfetto
-codebase outside of /public/ because /src/ is not exposed to clients.
-
-**include/perfetto/public/internal:**
-This directory contains headers that are required to implement the public-facing
-tracing API efficiently but that are not part of the API surface.
-In an ideal world there would be no need of these headers and everything would
-be handle via forward-declarations and PIMPL patterns. Unfortunately, however,
-PIMPL cannot be used for inline functions, where the implementation needs to be
-exposed in the public headers, which in turn need to depend on the memory layout
-of structs/classes.
-
-Rules:
-- All classes / types declared in this folder must be wrapped in the
-  ::perfetto::internal namespace.
-- Both public and internal .h headers must not pull other perfetto headers
-  (even base/) outside of /public/ (with the exclusion of protozero, which
-  should be moved to public as well soon).
-- .cc files instead can depend on other perfetto classes, as well as .h headers
-  located in src/public (as opposite to include/public).
-- Embedders must not depend or rely on the declarations of internal types.
-- Internal types cannot be used as input, output or return arguments of public
-  API functions.
-- Internal types cannot be directly exposed to virtual methods that are
-  intended to be called or overridden by the embedder (e.g. TracingBackend's
-  methods). For those the solution is to create a matching non-internal base
-  class with a static factory method.
-- We don't guarantee binary compatibility between versions (i.e. this client
-  library can only be statically linked) but we guarantee source-level
-  compatibility and ABI of the UNIX socket and shared memory buffers.
-
-**src/public/internal:**
-This directory contains .cc files that implement classes defined in
-`include/perfetto/public/internal` headers.
-
-
-Usage example
--------------
-1. Call `perfetto::Tracing::Initialize(...)` once, when starting the app.
-  While doing so the app can chose the tracing model:
-  - Fully in-process: the service runs in a thread within the same process.
-  - System: connects to the traced system daemon via a UNIX socket. This allows
-    the app to join system-wide tracing sessions. This is available only on
-    Linux/Android/MacOS for now.
-  - Private dedicated process: similar to the in-process case, but the service
-    runs in a dedicated process rather than a thread. This is for performance,
-    stability and security isolation. Also, this is not implemented yet.
-  - Custom backend: this is for peculiar cases (mainly chromium) where the
-    embedder is multi-process but wants to use a different IPC mechanism. The
-    embedder needs to deal with the larger and clunkier set of perfetto APIs.
-    Reach out to the team before using this mode. It's very unlikely you need
-    this unless you are a project rolled into chromium.
-
-2. Define and register one or more data sources, like this:
-```cpp
-  #include "perfetto/public/tracing.h"
-
-  class MyDataSource : public perfetto::DataSource<MyDataSource> {
-    void OnSetup(SetupArgs) override {}
-    void OnStart(StartArgs) override {}
-    void OnStop(StopArgs) override {}
-  };
-  ...
-  PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource);
-  ...
-  perfetto::DataSourceDescriptor dsd;
-  dsd.set_name("my_data_source");
-  MyDataSource::Register(dsd);
-```
-
-3. Optionally define a new proto schema in `trace_packet.proto`
-
-4. Emit trace events
-```cpp
-  MyDataSource::Trace([](TraceContext ctx) {
-      auto trace_packet = ctx.NewTracePacket();
-      ctx.set_timestamp(...);
-      ctx.set_my_custom_proto(...);
-  });
-```
-
-The passed labmda will be called only if tracing is enabled and the data source
-was enabled in the trace config. It might be called multiple times, one for each
-active tracing session, in case of concurrent tracing sessions (or even within a
-single tracing session, if the data source is listed twice in the trace config).
-
+This folder contains the deprecated consumer API. The only client of this
+is iorap in the Android tree. For any new use-case use the new Perfetto Client
+API under include/perfetto/tracing. 
\ No newline at end of file
diff --git a/include/perfetto/public/compiler.h b/include/perfetto/public/compiler.h
deleted file mode 100644
index d659df3..0000000
--- a/include/perfetto/public/compiler.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 2019 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.
- */
-
-// TODO(primiano): move bunch of include/base/ classes into public once we reach
-// a quieter period with release branches.
-
-#ifndef INCLUDE_PERFETTO_PUBLIC_COMPILER_H_
-#define INCLUDE_PERFETTO_PUBLIC_COMPILER_H_
-
-#ifndef PERFETTO_LIKELY
-#define PERFETTO_LIKELY(_x) __builtin_expect(!!(_x), 1)
-#endif
-
-#ifndef PERFETTO_UNLIKELY
-#define PERFETTO_UNLIKELY(_x) __builtin_expect(!!(_x), 0)
-#endif
-
-#endif  // INCLUDE_PERFETTO_PUBLIC_COMPILER_H_
diff --git a/include/perfetto/trace_processor/BUILD.gn b/include/perfetto/trace_processor/BUILD.gn
index e887327..90200fa 100644
--- a/include/perfetto/trace_processor/BUILD.gn
+++ b/include/perfetto/trace_processor/BUILD.gn
@@ -15,6 +15,7 @@
 source_set("trace_processor") {
   sources = [
     "basic_types.h",
+    "status.h",
     "trace_processor.h",
   ]
 }
diff --git a/include/perfetto/trace_processor/basic_types.h b/include/perfetto/trace_processor/basic_types.h
index 82cd65b..76d14d6 100644
--- a/include/perfetto/trace_processor/basic_types.h
+++ b/include/perfetto/trace_processor/basic_types.h
@@ -17,13 +17,11 @@
 #ifndef INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_
 #define INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_
 
+#include <assert.h>
 #include <stdarg.h>
 #include <stdint.h>
 #include <string>
 
-#include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
-
 namespace perfetto {
 namespace trace_processor {
 
@@ -42,7 +40,7 @@
   };
 
   double AsDouble() {
-    PERFETTO_CHECK(type == kDouble);
+    assert(type == kDouble);
     return double_value;
   }
 
@@ -60,61 +58,6 @@
   Type type = kNull;
 };
 
-// Status and related methods are inside util for consistency with embedders of
-// trace processor.
-namespace util {
-
-// Represents either the success or the failure message of a function.
-// This can used as the return type of functions which would usually return an
-// bool for success or int for errno but also wants to add some string context
-// (ususally for logging).
-class Status {
- public:
-  Status() = default;
-  explicit Status(std::string error) : message_(std::move(error)) {}
-
-  // Copy operations.
-  Status(const Status&) = default;
-  Status& operator=(const Status&) = default;
-
-  // Move operations. The moved-from state is valid but unspecified.
-  Status(Status&&) noexcept = default;
-  Status& operator=(Status&&) = default;
-
-  bool ok() const { return !message_.has_value(); }
-
-  // Only valid to call when this message has an Err status (i.e. ok() returned
-  // false or operator bool() returned true).
-  const std::string& message() const { return message_.value(); }
-
-  // Only valid to call when this message has an Err status (i.e. ok() returned
-  // false or operator bool() returned true).
-  const char* c_message() const { return message_.value().c_str(); }
-
- private:
-  base::Optional<std::string> message_;
-};
-
-// Returns a status object which represents the Ok status.
-inline Status OkStatus() {
-  return Status();
-}
-
-// Returns a status object which represents an error with the given message
-// formatted using printf.
-__attribute__((__format__(__printf__, 1, 2))) inline Status ErrStatus(
-    const char* format,
-    ...) {
-  va_list ap;
-  va_start(ap, format);
-
-  char buffer[1024];
-  vsnprintf(buffer, sizeof(buffer), format, ap);
-  return Status(std::string(buffer));
-}
-
-}  // namespace util
-
 }  // namespace trace_processor
 }  // namespace perfetto
 
diff --git a/include/perfetto/trace_processor/status.h b/include/perfetto/trace_processor/status.h
new file mode 100644
index 0000000..934c01b
--- /dev/null
+++ b/include/perfetto/trace_processor/status.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#ifndef INCLUDE_PERFETTO_TRACE_PROCESSOR_STATUS_H_
+#define INCLUDE_PERFETTO_TRACE_PROCESSOR_STATUS_H_
+
+#include <stdarg.h>
+#include <string>
+
+namespace perfetto {
+namespace trace_processor {
+
+// Status and related methods are inside util for consistency with embedders of
+// trace processor.
+namespace util {
+
+// Represents either the success or the failure message of a function.
+// This can used as the return type of functions which would usually return an
+// bool for success or int for errno but also wants to add some string context
+// (ususally for logging).
+class Status {
+ public:
+  Status() : ok_(true) {}
+  explicit Status(std::string error) : ok_(false), message_(std::move(error)) {}
+
+  // Copy operations.
+  Status(const Status&) = default;
+  Status& operator=(const Status&) = default;
+
+  // Move operations. The moved-from state is valid but unspecified.
+  Status(Status&&) noexcept = default;
+  Status& operator=(Status&&) = default;
+
+  bool ok() const { return ok_; }
+
+  // Only valid to call when this message has an Err status (i.e. ok() returned
+  // false or operator bool() returned true).
+  const std::string& message() const { return message_; }
+
+  // Only valid to call when this message has an Err status (i.e. ok() returned
+  // false or operator bool() returned true).
+  const char* c_message() const { return message_.c_str(); }
+
+ private:
+  bool ok_ = false;
+  std::string message_;
+};
+
+// Returns a status object which represents the Ok status.
+inline Status OkStatus() {
+  return Status();
+}
+
+// Returns a status object which represents an error with the given message
+// formatted using printf.
+__attribute__((__format__(__printf__, 1, 2))) inline Status ErrStatus(
+    const char* format,
+    ...) {
+  va_list ap;
+  va_start(ap, format);
+
+  char buffer[1024];
+  vsnprintf(buffer, sizeof(buffer), format, ap);
+  return Status(std::string(buffer));
+}
+
+}  // namespace util
+}  // namespace trace_processor
+}  // namespace perfetto
+
+#endif  // INCLUDE_PERFETTO_TRACE_PROCESSOR_STATUS_H_
diff --git a/include/perfetto/trace_processor/trace_processor.h b/include/perfetto/trace_processor/trace_processor.h
index 2c8e4a3..2ab067f 100644
--- a/include/perfetto/trace_processor/trace_processor.h
+++ b/include/perfetto/trace_processor/trace_processor.h
@@ -17,13 +17,11 @@
 #ifndef INCLUDE_PERFETTO_TRACE_PROCESSOR_TRACE_PROCESSOR_H_
 #define INCLUDE_PERFETTO_TRACE_PROCESSOR_TRACE_PROCESSOR_H_
 
-#include <functional>
 #include <memory>
-#include <unordered_map>
 #include <vector>
 
-#include "perfetto/base/optional.h"
 #include "perfetto/trace_processor/basic_types.h"
+#include "perfetto/trace_processor/status.h"
 
 namespace perfetto {
 
diff --git a/include/perfetto/tracing.h b/include/perfetto/tracing.h
new file mode 100644
index 0000000..6e0abef
--- /dev/null
+++ b/include/perfetto/tracing.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#ifndef INCLUDE_PERFETTO_TRACING_H_
+#define INCLUDE_PERFETTO_TRACING_H_
+
+// This headers wraps all the headers necessary to use the public Perfetto
+// Tracing API. Embedders should preferrably use this one header to avoid having
+// to figure out the various set of header required for each class.
+// The only exception to this should be large projects where build time is a
+// concern (e.g. chromium), which migh prefer sticking to strict IWYU.
+
+#include "perfetto/tracing/data_source.h"
+#include "perfetto/tracing/platform.h"
+#include "perfetto/tracing/tracing.h"
+#include "perfetto/tracing/tracing_backend.h"
+
+#endif  // INCLUDE_PERFETTO_TRACING_H_
diff --git a/include/perfetto/traced/BUILD.gn b/include/perfetto/tracing/BUILD.gn
similarity index 62%
copy from include/perfetto/traced/BUILD.gn
copy to include/perfetto/tracing/BUILD.gn
index c0c9316..5f8ff06 100644
--- a/include/perfetto/traced/BUILD.gn
+++ b/include/perfetto/tracing/BUILD.gn
@@ -1,4 +1,4 @@
-# Copyright (C) 2017 The Android Open Source Project
+# Copyright (C) 2019 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.
@@ -12,20 +12,17 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-source_set("traced") {
+source_set("tracing") {
   sources = [
-    "data_source_types.h",
-    "traced.h",
-  ]
-}
-
-source_set("sys_stats_counters") {
-  deps = [
-    "../../../gn:default_deps",
-    "../../../protos/perfetto/common:zero",
-    "../base",
-  ]
-  sources = [
-    "sys_stats_counters.h",
+    "data_source.h",
+    "internal/basic_types.h",
+    "internal/data_source_internal.h",
+    "internal/tracing_muxer.h",
+    "internal/tracing_tls.h",
+    "locked_handle.h",
+    "platform.h",
+    "trace_writer_base.h",
+    "tracing.h",
+    "tracing_backend.h",
   ]
 }
diff --git a/include/perfetto/public/data_source.h b/include/perfetto/tracing/data_source.h
similarity index 96%
rename from include/perfetto/public/data_source.h
rename to include/perfetto/tracing/data_source.h
index 563bd49..6412820 100644
--- a/include/perfetto/public/data_source.h
+++ b/include/perfetto/tracing/data_source.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_DATA_SOURCE_H_
-#define INCLUDE_PERFETTO_PUBLIC_DATA_SOURCE_H_
+#ifndef INCLUDE_PERFETTO_TRACING_DATA_SOURCE_H_
+#define INCLUDE_PERFETTO_TRACING_DATA_SOURCE_H_
 
 // This header contains the key class (DataSource) that a producer app should
 // override in order to create a custom data source that gets tracing Start/Stop
@@ -31,15 +31,15 @@
 #include <memory>
 #include <mutex>
 
+#include "perfetto/base/compiler.h"
 #include "perfetto/protozero/message.h"
 #include "perfetto/protozero/message_handle.h"
-#include "perfetto/public/compiler.h"
-#include "perfetto/public/internal/basic_types.h"
-#include "perfetto/public/internal/data_source_internal.h"
-#include "perfetto/public/internal/tracing_muxer.h"
-#include "perfetto/public/locked_handle.h"
-#include "perfetto/public/trace_writer_base.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
+#include "perfetto/tracing/internal/basic_types.h"
+#include "perfetto/tracing/internal/data_source_internal.h"
+#include "perfetto/tracing/internal/tracing_muxer.h"
+#include "perfetto/tracing/locked_handle.h"
+#include "perfetto/tracing/trace_writer_base.h"
 
 namespace perfetto {
 
@@ -280,4 +280,4 @@
   thread_local perfetto::internal::DataSourceThreadLocalState* \
       perfetto::DataSource<X>::tls_state_ = nullptr
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_DATA_SOURCE_H_
+#endif  // INCLUDE_PERFETTO_TRACING_DATA_SOURCE_H_
diff --git a/include/perfetto/public/internal/basic_types.h b/include/perfetto/tracing/internal/basic_types.h
similarity index 89%
rename from include/perfetto/public/internal/basic_types.h
rename to include/perfetto/tracing/internal/basic_types.h
index 14d3a74..81f5353 100644
--- a/include/perfetto/public/internal/basic_types.h
+++ b/include/perfetto/tracing/internal/basic_types.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_INTERNAL_BASIC_TYPES_H_
-#define INCLUDE_PERFETTO_PUBLIC_INTERNAL_BASIC_TYPES_H_
+#ifndef INCLUDE_PERFETTO_TRACING_INTERNAL_BASIC_TYPES_H_
+#define INCLUDE_PERFETTO_TRACING_INTERNAL_BASIC_TYPES_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -43,4 +43,4 @@
 }  // namespace internal
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_INTERNAL_BASIC_TYPES_H_
+#endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_BASIC_TYPES_H_
diff --git a/include/perfetto/public/internal/data_source_internal.h b/include/perfetto/tracing/internal/data_source_internal.h
similarity index 91%
rename from include/perfetto/public/internal/data_source_internal.h
rename to include/perfetto/tracing/internal/data_source_internal.h
index 4db439c..9ce027a 100644
--- a/include/perfetto/public/internal/data_source_internal.h
+++ b/include/perfetto/tracing/internal/data_source_internal.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_INTERNAL_DATA_SOURCE_INTERNAL_H_
-#define INCLUDE_PERFETTO_PUBLIC_INTERNAL_DATA_SOURCE_INTERNAL_H_
+#ifndef INCLUDE_PERFETTO_TRACING_INTERNAL_DATA_SOURCE_INTERNAL_H_
+#define INCLUDE_PERFETTO_TRACING_INTERNAL_DATA_SOURCE_INTERNAL_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -26,9 +26,9 @@
 #include <memory>
 #include <mutex>
 
-// No perfetto headers (other than /public/ and protozero) should be here.
-#include "perfetto/public/internal/basic_types.h"
-#include "perfetto/public/trace_writer_base.h"
+// No perfetto headers (other than tracing/api and protozero) should be here.
+#include "perfetto/tracing/internal/basic_types.h"
+#include "perfetto/tracing/trace_writer_base.h"
 
 namespace perfetto {
 
@@ -135,4 +135,4 @@
 }  // namespace internal
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_INTERNAL_DATA_SOURCE_INTERNAL_H_
+#endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_DATA_SOURCE_INTERNAL_H_
diff --git a/include/perfetto/public/internal/tracing_muxer.h b/include/perfetto/tracing/internal/tracing_muxer.h
similarity index 90%
rename from include/perfetto/public/internal/tracing_muxer.h
rename to include/perfetto/tracing/internal/tracing_muxer.h
index e92c7e1..f13dfb2 100644
--- a/include/perfetto/public/internal/tracing_muxer.h
+++ b/include/perfetto/tracing/internal/tracing_muxer.h
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_INTERNAL_TRACING_MUXER_H_
-#define INCLUDE_PERFETTO_PUBLIC_INTERNAL_TRACING_MUXER_H_
+#ifndef INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_MUXER_H_
+#define INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_MUXER_H_
 
 #include <atomic>
 #include <memory>
 
-#include "perfetto/public/internal/basic_types.h"
-#include "perfetto/public/internal/tracing_tls.h"
-#include "perfetto/public/platform.h"
+#include "perfetto/tracing/internal/basic_types.h"
+#include "perfetto/tracing/internal/tracing_tls.h"
+#include "perfetto/tracing/platform.h"
 
 namespace perfetto {
 
@@ -44,7 +44,7 @@
 // See tracing_muxer_impl.h for the full picture. This class contains only the
 // fewer fields and methods that need to be exposed to public/ headers. Fields
 // and methods that are required to implement them should go into
-// src/public/internal/tracing_muxer_impl.h instead: that one can pull in
+// src/tracing/internal/tracing_muxer_impl.h instead: that one can pull in
 // perfetto headers outside of public, this one cannot.
 class TracingMuxer {
  public:
@@ -104,4 +104,4 @@
 }  // namespace internal
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_INTERNAL_TRACING_MUXER_H_
+#endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_MUXER_H_
diff --git a/include/perfetto/public/internal/tracing_tls.h b/include/perfetto/tracing/internal/tracing_tls.h
similarity index 90%
rename from include/perfetto/public/internal/tracing_tls.h
rename to include/perfetto/tracing/internal/tracing_tls.h
index 6b2f9fc..8c39dfa 100644
--- a/include/perfetto/public/internal/tracing_tls.h
+++ b/include/perfetto/tracing/internal/tracing_tls.h
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_INTERNAL_TRACING_TLS_H_
-#define INCLUDE_PERFETTO_PUBLIC_INTERNAL_TRACING_TLS_H_
+#ifndef INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_TLS_H_
+#define INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_TLS_H_
 
 #include <array>
 #include <memory>
 
-#include "perfetto/public/internal/basic_types.h"
-#include "perfetto/public/internal/data_source_internal.h"
-#include "perfetto/public/platform.h"
+#include "perfetto/tracing/internal/basic_types.h"
+#include "perfetto/tracing/internal/data_source_internal.h"
+#include "perfetto/tracing/platform.h"
 
 namespace perfetto {
 
@@ -80,4 +80,4 @@
 }  // namespace internal
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_INTERNAL_TRACING_TLS_H_
+#endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_TLS_H_
diff --git a/include/perfetto/public/locked_handle.h b/include/perfetto/tracing/locked_handle.h
similarity index 89%
rename from include/perfetto/public/locked_handle.h
rename to include/perfetto/tracing/locked_handle.h
index b3242d5..f9ae8e5 100644
--- a/include/perfetto/public/locked_handle.h
+++ b/include/perfetto/tracing/locked_handle.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_LOCKED_HANDLE_H_
-#define INCLUDE_PERFETTO_PUBLIC_LOCKED_HANDLE_H_
+#ifndef INCLUDE_PERFETTO_TRACING_LOCKED_HANDLE_H_
+#define INCLUDE_PERFETTO_TRACING_LOCKED_HANDLE_H_
 
 #include <mutex>
 
@@ -49,4 +49,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_LOCKED_HANDLE_H_
+#endif  // INCLUDE_PERFETTO_TRACING_LOCKED_HANDLE_H_
diff --git a/include/perfetto/public/platform.h b/include/perfetto/tracing/platform.h
similarity index 95%
rename from include/perfetto/public/platform.h
rename to include/perfetto/tracing/platform.h
index 54f07b1..6e5c95d 100644
--- a/include/perfetto/public/platform.h
+++ b/include/perfetto/tracing/platform.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_PLATFORM_H_
-#define INCLUDE_PERFETTO_PUBLIC_PLATFORM_H_
+#ifndef INCLUDE_PERFETTO_TRACING_PLATFORM_H_
+#define INCLUDE_PERFETTO_TRACING_PLATFORM_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -83,4 +83,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_PLATFORM_H_
+#endif  // INCLUDE_PERFETTO_TRACING_PLATFORM_H_
diff --git a/include/perfetto/public/trace_writer_base.h b/include/perfetto/tracing/trace_writer_base.h
similarity index 86%
rename from include/perfetto/public/trace_writer_base.h
rename to include/perfetto/tracing/trace_writer_base.h
index 52cdde6..96e308c 100644
--- a/include/perfetto/public/trace_writer_base.h
+++ b/include/perfetto/tracing/trace_writer_base.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_TRACE_WRITER_BASE_H_
-#define INCLUDE_PERFETTO_PUBLIC_TRACE_WRITER_BASE_H_
+#ifndef INCLUDE_PERFETTO_TRACING_TRACE_WRITER_BASE_H_
+#define INCLUDE_PERFETTO_TRACING_TRACE_WRITER_BASE_H_
 
 #include "perfetto/protozero/message_handle.h"
 
@@ -38,4 +38,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_TRACE_WRITER_BASE_H_
+#endif  // INCLUDE_PERFETTO_TRACING_TRACE_WRITER_BASE_H_
diff --git a/include/perfetto/public/tracing.h b/include/perfetto/tracing/tracing.h
similarity index 93%
rename from include/perfetto/public/tracing.h
rename to include/perfetto/tracing/tracing.h
index e65bfce..db6021a 100644
--- a/include/perfetto/public/tracing.h
+++ b/include/perfetto/tracing/tracing.h
@@ -14,10 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_TRACING_H_
-#define INCLUDE_PERFETTO_PUBLIC_TRACING_H_
-
-// The only header that embedders should include to get everything.
+#ifndef INCLUDE_PERFETTO_TRACING_TRACING_H_
+#define INCLUDE_PERFETTO_TRACING_TRACING_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -25,10 +23,7 @@
 #include <functional>
 #include <memory>
 #include <string>
-
-#include "perfetto/public/data_source.h"
-#include "perfetto/public/platform.h"
-#include "perfetto/public/tracing_backend.h"
+#include <vector>
 
 namespace perfetto {
 
@@ -134,4 +129,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_TRACING_H_
+#endif  // INCLUDE_PERFETTO_TRACING_TRACING_H_
diff --git a/include/perfetto/public/tracing_backend.h b/include/perfetto/tracing/tracing_backend.h
similarity index 94%
rename from include/perfetto/public/tracing_backend.h
rename to include/perfetto/tracing/tracing_backend.h
index 461a776..f35a4ae 100644
--- a/include/perfetto/public/tracing_backend.h
+++ b/include/perfetto/tracing/tracing_backend.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_PUBLIC_TRACING_BACKEND_H_
-#define INCLUDE_PERFETTO_PUBLIC_TRACING_BACKEND_H_
+#ifndef INCLUDE_PERFETTO_TRACING_TRACING_BACKEND_H_
+#define INCLUDE_PERFETTO_TRACING_TRACING_BACKEND_H_
 
 #include <memory>
 #include <string>
@@ -80,4 +80,4 @@
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_PUBLIC_TRACING_BACKEND_H_
+#endif  // INCLUDE_PERFETTO_TRACING_TRACING_BACKEND_H_
diff --git a/infra/perfetto-site.appspot.com/Makefile b/infra/perfetto-site.appspot.com/Makefile
index a1586e4..0811e7f 100644
--- a/infra/perfetto-site.appspot.com/Makefile
+++ b/infra/perfetto-site.appspot.com/Makefile
@@ -23,6 +23,8 @@
 	cp node_modules/docsify-themeable/dist/css/theme-simple.css static/
 	cp node_modules/docsify-copy-code/dist/docsify-copy-code.min.js static/
 	cp node_modules/prismjs/components/prism-bash.min.js static/
+	cp node_modules/prismjs/components/prism-protobuf.min.js static/
+	cp node_modules/prismjs/components/prism-sql.min.js static/
 
 deploy: static
 	gcloud app deploy app.yaml --project perfetto-site
diff --git a/protos/BUILD b/protos/BUILD
index b75db01..4c280e9 100644
--- a/protos/BUILD
+++ b/protos/BUILD
@@ -54,6 +54,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/common:lite_gen
+java_proto_library(
+    name = "common_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:common",
+    ],
+)
+
 # GN target: //protos/perfetto/common:zero_gen
 proto_library(
     name = "common_zero",
@@ -73,7 +84,6 @@
     src_proto_library = "//third_party/perfetto/protos:common_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -116,6 +126,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/config:lite_gen
+java_proto_library(
+    name = "config_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:config",
+    ],
+)
+
 # GN target: //protos/perfetto/config:merged_config_gen
 proto_library(
     name = "config_merged_config_gen",
@@ -141,6 +162,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/config:merged_config_gen
+java_proto_library(
+    name = "config_merged_config_gen_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:config_merged_config_gen",
+    ],
+)
+
 # GN target: //protos/perfetto/config:zero_gen
 proto_library(
     name = "config_zero",
@@ -169,7 +201,6 @@
     src_proto_library = "//third_party/perfetto/protos:config_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -217,6 +248,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/metrics/android:lite_gen
+java_proto_library(
+    name = "metrics_android_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:metrics_android",
+    ],
+)
+
 # GN target: //protos/perfetto/metrics/android:zero_gen
 proto_library(
     name = "metrics_android_zero",
@@ -233,7 +275,6 @@
     src_proto_library = "//third_party/perfetto/protos:metrics_android_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -248,6 +289,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/metrics:lite_gen
+java_proto_library(
+    name = "metrics_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:metrics",
+    ],
+)
+
 # GN target: //protos/perfetto/metrics:zero_gen
 proto_library(
     name = "metrics_zero",
@@ -265,7 +317,6 @@
     src_proto_library = "//third_party/perfetto/protos:metrics_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -294,6 +345,17 @@
     ],
 )
 
+# GN target: //protos/third_party/pprof:lite_gen
+java_proto_library(
+    name = "protos_third_party_pprof_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:protos_third_party_pprof",
+    ],
+)
+
 # GN target: //protos/perfetto/trace:lite_gen
 proto_library(
     name = "trace",
@@ -354,6 +416,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/android:lite_gen
+java_proto_library(
+    name = "trace_android_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_android",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/android:zero_gen
 proto_library(
     name = "trace_android_zero",
@@ -372,7 +445,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_android_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -413,6 +485,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/chrome:lite_gen
+java_proto_library(
+    name = "trace_chrome_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_chrome",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/chrome:zero_gen
 proto_library(
     name = "trace_chrome_zero",
@@ -432,7 +515,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_chrome_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -461,6 +543,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/filesystem:lite_gen
+java_proto_library(
+    name = "trace_filesystem_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_filesystem",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/filesystem:zero_gen
 proto_library(
     name = "trace_filesystem_zero",
@@ -475,7 +568,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_filesystem_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -535,6 +627,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/ftrace:lite_gen
+java_proto_library(
+    name = "trace_ftrace_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_ftrace",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/ftrace:zero_gen
 proto_library(
     name = "trace_ftrace_zero",
@@ -580,7 +683,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_ftrace_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -612,6 +714,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/interned_data:lite_gen
+java_proto_library(
+    name = "trace_interned_data_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_interned_data",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/interned_data:zero_gen
 proto_library(
     name = "trace_interned_data_zero",
@@ -629,7 +742,17 @@
     src_proto_library = "//third_party/perfetto/protos:trace_interned_data_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
+    ],
+)
+
+# GN target: //protos/perfetto/trace:lite_gen
+java_proto_library(
+    name = "trace_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace",
     ],
 )
 
@@ -658,6 +781,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace:merged_trace_gen
+java_proto_library(
+    name = "trace_merged_trace_gen_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_merged_trace_gen",
+    ],
+)
+
 # GN target: //protos/perfetto/trace:minimal_lite_gen
 proto_library(
     name = "trace_minimal",
@@ -689,6 +823,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace:minimal_lite_gen
+java_proto_library(
+    name = "trace_minimal_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_minimal",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/power:lite_gen
 proto_library(
     name = "trace_power",
@@ -715,6 +860,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/power:lite_gen
+java_proto_library(
+    name = "trace_power_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_power",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/power:zero_gen
 proto_library(
     name = "trace_power_zero",
@@ -730,7 +886,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_power_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -761,6 +916,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace_processor:lite_gen
+java_proto_library(
+    name = "trace_processor_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_processor",
+    ],
+)
+
 # GN target: //protos/perfetto/trace_processor:metrics_impl_zero_gen
 proto_library(
     name = "trace_processor_metrics_impl_zero",
@@ -775,7 +941,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_processor_metrics_impl_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -804,6 +969,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/profiling:lite_gen
+java_proto_library(
+    name = "trace_profiling_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_profiling",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/profiling:zero_gen
 proto_library(
     name = "trace_profiling_zero",
@@ -818,7 +994,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_profiling_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -848,6 +1023,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/ps:lite_gen
+java_proto_library(
+    name = "trace_ps_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_ps",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/ps:zero_gen
 proto_library(
     name = "trace_ps_zero",
@@ -863,7 +1049,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_ps_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -895,6 +1080,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/sys_stats:lite_gen
+java_proto_library(
+    name = "trace_sys_stats_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_sys_stats",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/sys_stats:zero_gen
 proto_library(
     name = "trace_sys_stats_zero",
@@ -912,7 +1108,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_sys_stats_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -945,6 +1140,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/trace/track_event:lite_gen
+java_proto_library(
+    name = "trace_track_event_java_proto",
+    visibility = [
+        "//visibility:public",
+    ],
+    deps = [
+        "//third_party/perfetto/protos:trace_track_event",
+    ],
+)
+
 # GN target: //protos/perfetto/trace/track_event:zero_gen
 proto_library(
     name = "trace_track_event_zero",
@@ -963,7 +1169,6 @@
     src_proto_library = "//third_party/perfetto/protos:trace_track_event_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
 
@@ -1000,6 +1205,5 @@
     src_proto_library = "//third_party/perfetto/protos:trace_zero",
     deps = [
         "//third_party/perfetto:libprotozero",
-        "//third_party/perfetto/google:gtest_prod",
     ],
 )
diff --git a/protos/perfetto/metrics/android/OWNERS b/protos/perfetto/metrics/android/OWNERS
new file mode 100644
index 0000000..0bcdb4d
--- /dev/null
+++ b/protos/perfetto/metrics/android/OWNERS
@@ -0,0 +1 @@
+ilkos@google.com
diff --git a/protos/perfetto/metrics/android/startup_metric.proto b/protos/perfetto/metrics/android/startup_metric.proto
index 9414502..0d2e14c 100644
--- a/protos/perfetto/metrics/android/startup_metric.proto
+++ b/protos/perfetto/metrics/android/startup_metric.proto
@@ -30,6 +30,8 @@
 
 // Android app startup metrics.
 message AndroidStartupMetric {
+  message Slice { optional int64 dur_ns = 1; }
+
   // Timing information spanning the intent received by the
   // activity manager to the first frame drawn.
   // All times and durations in nanoseconds (ns).
@@ -40,6 +42,28 @@
     // In this timespan, how many processes (apart from the main activity) were
     // spawned.
     optional uint32 other_processes_spawned_count = 3;
+
+    // Total time spent in activity manager between the initial intent
+    // and the end of the activity starter.
+    optional Slice time_activity_manager = 4;
+
+    // The following slices follow the typical steps post-fork.
+    optional Slice time_activity_thread_main = 5;
+    optional Slice time_bind_application = 6;
+    optional Slice time_activity_start = 7;
+    optional Slice time_activity_resume = 8;
+    optional Slice time_choreographer = 9;
+
+    // If we are starting a new process, record the duration from the
+    // intent being received to the time we call the zygote.
+    optional Slice time_before_start_process = 10;
+
+    // The actual duration of the process start (based on the zygote slice).
+    optional Slice time_during_start_process = 11;
+
+    // The ratio between the cpu time of the activity process
+    // to all other processes in the system.
+    optional double other_process_to_activity_cpu_ratio = 12;
   }
 
   // Next id: 7
@@ -58,7 +82,8 @@
 
     // Number of processes hosting the activity involved in the launch.
     // This will usually be 1. If it is 0, it is indicative of a data / process
-    // error. If > 1, the process died during startup and the system respawned.
+    // error. If > 1, the process died during startup and the system respawned
+    // it.
     optional uint32 activity_hosting_process_count = 6;
 
     optional ToFirstFrame to_first_frame = 5;
diff --git a/protos/perfetto/trace/perfetto_trace.proto b/protos/perfetto/trace/perfetto_trace.proto
index 319953a..fb15d07 100644
--- a/protos/perfetto/trace/perfetto_trace.proto
+++ b/protos/perfetto/trace/perfetto_trace.proto
@@ -2749,6 +2749,15 @@
     optional int64 oom_score_adj = 10;
   }
   repeated Process processes = 1;
+
+  // The time at which we finish collecting this batch of samples;
+  // the top-level packet timestamp is the time at which
+  // we begin collection.
+  // TODO(dancol): analysis might be improved by
+  // time-bracketing each sample as well as the whole
+  // ProcessStats, but doing that is probably gated on
+  // a vdso for CLOCK_BOOTTIME.
+  optional uint64 collection_end_timestamp = 2;
 }
 
 // End of protos/perfetto/trace/ps/process_stats.proto
@@ -2795,6 +2804,11 @@
   // #2 { processes: [{pid: 20, ...}], threads: [{pid: 13, tgid: 10}] }
   repeated Process processes = 1;
   repeated Thread threads = 2;
+
+  // The time at which we finish collecting this process tree;
+  // the top-level packet timestamp is the time at which
+  // we begin collection.
+  optional uint64 collection_end_timestamp = 3;
 }
 
 // End of protos/perfetto/trace/ps/process_tree.proto
@@ -2850,6 +2864,11 @@
   // Populated only if SOFTIRQ_COUNTS in config.stat_counters.
   optional uint64 num_softirq_total = 7;    // Total num of softirqs since boot.
   repeated InterruptCount num_softirq = 8;  // Per-softirq count.
+
+  // The time at which we finish collecting this set of samples;
+  // the top-level packet timestamp is the time at which
+  // we begin collection.
+  optional uint64 collection_end_timestamp = 9;
 }
 
 // End of protos/perfetto/trace/sys_stats/sys_stats.proto
@@ -3104,7 +3123,7 @@
 // thread, i.e. all events in the same packet sequence. Valid for all subsequent
 // events in the same sequence.
 //
-// Next id: 8.
+// Next id: 9.
 message ThreadDescriptor {
   optional int32 pid = 1;
   optional int32 tid = 2;
@@ -3113,8 +3132,29 @@
   optional int32 legacy_sort_index = 3;
 
   enum ChromeThreadType {
-    THREAD_UNSPECIFIED = 0;
-    // TODO(eseckler): Add thread types.
+    THREAD_TYPE_UNSPECIFIED = 0;
+
+    THREAD_TYPE_MAIN = 1;
+    THREAD_TYPE_IO = 2;
+
+    // Scheduler:
+    THREAD_TYPE_THREAD_POOL_BG_WORKER = 3;
+    THREAD_TYPE_THREAD_POOL_FG_WORKER = 4;
+    THREAD_TYPE_THREAD_POOL_FB_BLOCKING = 5;
+    THREAD_TYPE_THREAD_POOL_BG_BLOCKING = 6;
+    THREAD_TYPE_THREAD_POOL_SERVICE = 7;
+
+    // Compositor:
+    THREAD_TYPE_COMPOSITOR = 8;
+    THREAD_TYPE_VIZ_COMPOSITOR = 9;
+    THREAD_TYPE_COMPOSITOR_WORKER = 10;
+
+    // Renderer:
+    THREAD_TYPE_SERVICE_WORKER = 11;
+
+    // Tracing related threads:
+    THREAD_TYPE_MEMORY_INFRA = 50;
+    THREAD_TYPE_SAMPLING_PROFILER = 51;
   };
   optional ChromeThreadType chrome_thread_type = 4;
 
@@ -3126,6 +3166,7 @@
   // encoding size.
   optional int64 reference_timestamp_us = 6;
   optional int64 reference_thread_time_us = 7;
+  optional int64 reference_thread_instruction_count = 8;
 }
 
 // End of protos/perfetto/trace/track_event/thread_descriptor.proto
@@ -3149,7 +3190,7 @@
 // association) are emitted as part of ProcessDescriptor and ThreadDescriptor
 // messages in separate TracePackets instead.
 //
-// Next reserved id: 7 (up to 15).
+// Next reserved id: 8 (up to 15).
 // Next id: 21.
 message TrackEvent {
   // Timestamp in microseconds (usually CLOCK_MONOTONIC).
@@ -3177,8 +3218,16 @@
     int64 thread_time_absolute_us = 17;
   }
 
-  // Elapsed retired instruction count during the event.
-  optional int64 thread_instruction_delta = 20;
+  // Value of the instruction counter for the current thread.
+  oneof thread_instruction_count {
+    // Same encoding as |timestamp| fields above.
+    int64 thread_instruction_count_delta = 8;
+    // TODO(eseckler): Consider removing absolute thread instruction count
+    // support. It's currently required to support writing PHASE_COMPLETE events
+    // out-of-order, but shouldn't be required anymore when we split them into
+    // begin/end.
+    int64 thread_instruction_count_absolute = 20;
+  }
 
   // We intend to add a binary symbol version of this in the future.
   repeated uint32 category_iids = 3;  // interned EventCategoryName.
@@ -3199,7 +3248,7 @@
   // features in TrackEvent (e.g. async + flow events), or deprecate them
   // without replacement where transition is unsuitable.
   //
-  // Next reserved id: 15 (up to 16).
+  // Next reserved id: 16 (up to 16).
   // Next id: 20.
   message LegacyEvent {
     optional uint32 name_iid = 1;  // interned LegacyEventName.
@@ -3207,6 +3256,9 @@
     optional int64 duration_us = 3;
     optional int64 thread_duration_us = 4;
 
+    // Elapsed retired instruction count during the event.
+    optional int64 thread_instruction_delta = 15;
+
     reserved 5;  // used to be |flags|.
 
     oneof id {
diff --git a/protos/perfetto/trace/ps/process_stats.proto b/protos/perfetto/trace/ps/process_stats.proto
index 49e8237..5db028f 100644
--- a/protos/perfetto/trace/ps/process_stats.proto
+++ b/protos/perfetto/trace/ps/process_stats.proto
@@ -43,4 +43,13 @@
     optional int64 oom_score_adj = 10;
   }
   repeated Process processes = 1;
+
+  // The time at which we finish collecting this batch of samples;
+  // the top-level packet timestamp is the time at which
+  // we begin collection.
+  // TODO(dancol): analysis might be improved by
+  // time-bracketing each sample as well as the whole
+  // ProcessStats, but doing that is probably gated on
+  // a vdso for CLOCK_BOOTTIME.
+  optional uint64 collection_end_timestamp = 2;
 }
diff --git a/protos/perfetto/trace/ps/process_tree.proto b/protos/perfetto/trace/ps/process_tree.proto
index 50579e5..cf83d12 100644
--- a/protos/perfetto/trace/ps/process_tree.proto
+++ b/protos/perfetto/trace/ps/process_tree.proto
@@ -58,4 +58,9 @@
   // #2 { processes: [{pid: 20, ...}], threads: [{pid: 13, tgid: 10}] }
   repeated Process processes = 1;
   repeated Thread threads = 2;
+
+  // The time at which we finish collecting this process tree;
+  // the top-level packet timestamp is the time at which
+  // we begin collection.
+  optional uint64 collection_end_timestamp = 3;
 }
diff --git a/protos/perfetto/trace/sys_stats/sys_stats.proto b/protos/perfetto/trace/sys_stats/sys_stats.proto
index 7b298bf..4b10ecf 100644
--- a/protos/perfetto/trace/sys_stats/sys_stats.proto
+++ b/protos/perfetto/trace/sys_stats/sys_stats.proto
@@ -69,4 +69,9 @@
   // Populated only if SOFTIRQ_COUNTS in config.stat_counters.
   optional uint64 num_softirq_total = 7;    // Total num of softirqs since boot.
   repeated InterruptCount num_softirq = 8;  // Per-softirq count.
+
+  // The time at which we finish collecting this set of samples;
+  // the top-level packet timestamp is the time at which
+  // we begin collection.
+  optional uint64 collection_end_timestamp = 9;
 }
diff --git a/protos/perfetto/trace/track_event/thread_descriptor.proto b/protos/perfetto/trace/track_event/thread_descriptor.proto
index d8648ff..85a5824 100644
--- a/protos/perfetto/trace/track_event/thread_descriptor.proto
+++ b/protos/perfetto/trace/track_event/thread_descriptor.proto
@@ -23,7 +23,7 @@
 // thread, i.e. all events in the same packet sequence. Valid for all subsequent
 // events in the same sequence.
 //
-// Next id: 8.
+// Next id: 9.
 message ThreadDescriptor {
   optional int32 pid = 1;
   optional int32 tid = 2;
@@ -32,8 +32,29 @@
   optional int32 legacy_sort_index = 3;
 
   enum ChromeThreadType {
-    THREAD_UNSPECIFIED = 0;
-    // TODO(eseckler): Add thread types.
+    THREAD_TYPE_UNSPECIFIED = 0;
+
+    THREAD_TYPE_MAIN = 1;
+    THREAD_TYPE_IO = 2;
+
+    // Scheduler:
+    THREAD_TYPE_THREAD_POOL_BG_WORKER = 3;
+    THREAD_TYPE_THREAD_POOL_FG_WORKER = 4;
+    THREAD_TYPE_THREAD_POOL_FB_BLOCKING = 5;
+    THREAD_TYPE_THREAD_POOL_BG_BLOCKING = 6;
+    THREAD_TYPE_THREAD_POOL_SERVICE = 7;
+
+    // Compositor:
+    THREAD_TYPE_COMPOSITOR = 8;
+    THREAD_TYPE_VIZ_COMPOSITOR = 9;
+    THREAD_TYPE_COMPOSITOR_WORKER = 10;
+
+    // Renderer:
+    THREAD_TYPE_SERVICE_WORKER = 11;
+
+    // Tracing related threads:
+    THREAD_TYPE_MEMORY_INFRA = 50;
+    THREAD_TYPE_SAMPLING_PROFILER = 51;
   };
   optional ChromeThreadType chrome_thread_type = 4;
 
@@ -45,4 +66,5 @@
   // encoding size.
   optional int64 reference_timestamp_us = 6;
   optional int64 reference_thread_time_us = 7;
+  optional int64 reference_thread_instruction_count = 8;
 }
diff --git a/protos/perfetto/trace/track_event/track_event.proto b/protos/perfetto/trace/track_event/track_event.proto
index 92a05fc..3a71df4 100644
--- a/protos/perfetto/trace/track_event/track_event.proto
+++ b/protos/perfetto/trace/track_event/track_event.proto
@@ -39,7 +39,7 @@
 // association) are emitted as part of ProcessDescriptor and ThreadDescriptor
 // messages in separate TracePackets instead.
 //
-// Next reserved id: 7 (up to 15).
+// Next reserved id: 8 (up to 15).
 // Next id: 21.
 message TrackEvent {
   // Timestamp in microseconds (usually CLOCK_MONOTONIC).
@@ -67,8 +67,16 @@
     int64 thread_time_absolute_us = 17;
   }
 
-  // Elapsed retired instruction count during the event.
-  optional int64 thread_instruction_delta = 20;
+  // Value of the instruction counter for the current thread.
+  oneof thread_instruction_count {
+    // Same encoding as |timestamp| fields above.
+    int64 thread_instruction_count_delta = 8;
+    // TODO(eseckler): Consider removing absolute thread instruction count
+    // support. It's currently required to support writing PHASE_COMPLETE events
+    // out-of-order, but shouldn't be required anymore when we split them into
+    // begin/end.
+    int64 thread_instruction_count_absolute = 20;
+  }
 
   // We intend to add a binary symbol version of this in the future.
   repeated uint32 category_iids = 3;  // interned EventCategoryName.
@@ -89,7 +97,7 @@
   // features in TrackEvent (e.g. async + flow events), or deprecate them
   // without replacement where transition is unsuitable.
   //
-  // Next reserved id: 15 (up to 16).
+  // Next reserved id: 16 (up to 16).
   // Next id: 20.
   message LegacyEvent {
     optional uint32 name_iid = 1;  // interned LegacyEventName.
@@ -97,6 +105,9 @@
     optional int64 duration_us = 3;
     optional int64 thread_duration_us = 4;
 
+    // Elapsed retired instruction count during the event.
+    optional int64 thread_instruction_delta = 15;
+
     reserved 5;  // used to be |flags|.
 
     oneof id {
diff --git a/src/base/BUILD.gn b/src/base/BUILD.gn
index 97abdcf..7359374 100644
--- a/src/base/BUILD.gn
+++ b/src/base/BUILD.gn
@@ -22,6 +22,7 @@
   ]
   public_deps = [
     "../../include/perfetto/base",
+    "../../include/perfetto/ext/base",
   ]
   sources = [
     "file_utils.cc",
@@ -82,7 +83,8 @@
     ]
     deps = [
       "../../gn:default_deps",
-      "../../include/perfetto/base",
+      "../../include/perfetto/ext/base",
+      "../../include/perfetto/ext/base",
     ]
     if (is_linux || is_android) {
       deps += [ "../../buildtools:libbacktrace" ]
@@ -96,7 +98,8 @@
   source_set("unix_socket") {
     deps = [
       "../../gn:default_deps",
-      "../../include/perfetto/base",
+      "../../include/perfetto/ext/base",
+      "../../include/perfetto/ext/base",
     ]
     sources = [
       "unix_socket.cc",
@@ -136,7 +139,8 @@
     "../../gn:gtest_deps",
   ]
 
-  if (is_android && (perfetto_build_standalone || perfetto_build_with_android)) {
+  if (is_android &&
+      (perfetto_build_standalone || perfetto_build_with_android)) {
     deps += [ ":android_task_runner" ]
   }
   sources = [
diff --git a/src/base/android_task_runner.cc b/src/base/android_task_runner.cc
index aab61d1..e6979e4 100644
--- a/src/base/android_task_runner.cc
+++ b/src/base/android_task_runner.cc
@@ -14,11 +14,13 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/android_task_runner.h"
+#include "perfetto/ext/base/android_task_runner.h"
 
 #include <errno.h>
 #include <sys/timerfd.h>
 
+#include "perfetto/ext/base/watchdog.h"
+
 namespace perfetto {
 namespace base {
 
@@ -96,7 +98,7 @@
   if (has_next)
     ScheduleImmediateWakeUp();
   errno = 0;
-  RunTask(immediate_task);
+  RunTaskWithWatchdogGuard(immediate_task);
 }
 
 void AndroidTaskRunner::RunDelayedTask() {
@@ -122,7 +124,7 @@
   if (next_wake_up.count())
     ScheduleDelayedWakeUp(next_wake_up);
   errno = 0;
-  RunTask(delayed_task);
+  RunTaskWithWatchdogGuard(delayed_task);
 }
 
 void AndroidTaskRunner::ScheduleImmediateWakeUp() {
@@ -200,7 +202,7 @@
     task = it->second;
   }
   errno = 0;
-  RunTask(task);
+  RunTaskWithWatchdogGuard(task);
   return true;
 }
 
diff --git a/src/base/circular_queue_unittest.cc b/src/base/circular_queue_unittest.cc
index 6fefe0c..ededf2f 100644
--- a/src/base/circular_queue_unittest.cc
+++ b/src/base/circular_queue_unittest.cc
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/circular_queue.h"
+#include "perfetto/ext/base/circular_queue.h"
 
 #include <random>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/debug_crash_stack_trace.cc b/src/base/debug_crash_stack_trace.cc
index 31fe32d..ae857a4 100644
--- a/src/base/debug_crash_stack_trace.cc
+++ b/src/base/debug_crash_stack_trace.cc
@@ -28,7 +28,7 @@
 #include <unwind.h>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
+#include "perfetto/ext/base/file_utils.h"
 
 // Some glibc headers hit this when using signals.
 #pragma GCC diagnostic push
diff --git a/src/base/event.cc b/src/base/event.cc
index 4fa6ec0..14d5a4a 100644
--- a/src/base/event.cc
+++ b/src/base/event.cc
@@ -17,9 +17,10 @@
 #include <stdint.h>
 #include <unistd.h>
 
-#include "perfetto/base/event.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/pipe.h"
+#include "perfetto/ext/base/event.h"
+#include "perfetto/ext/base/pipe.h"
+#include "perfetto/ext/base/utils.h"
 
 #if PERFETTO_USE_EVENTFD()
 #include <sys/eventfd.h>
diff --git a/src/base/file_utils.cc b/src/base/file_utils.cc
index 7dc003c..8dc0b0b 100644
--- a/src/base/file_utils.cc
+++ b/src/base/file_utils.cc
@@ -17,9 +17,10 @@
 #include <sys/stat.h>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/utils.h"
 
 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 #include <unistd.h>
diff --git a/src/base/metatrace.cc b/src/base/metatrace.cc
index 99b787f..66954bf 100644
--- a/src/base/metatrace.cc
+++ b/src/base/metatrace.cc
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/metatrace.h"
+#include "perfetto/ext/base/metatrace.h"
 
 #include <fcntl.h>
 #include <stdlib.h>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/time.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 #include <corecrt_io.h>
diff --git a/src/base/no_destructor_unittest.cc b/src/base/no_destructor_unittest.cc
index 1f99db7..9c3460d 100644
--- a/src/base/no_destructor_unittest.cc
+++ b/src/base/no_destructor_unittest.cc
@@ -17,9 +17,9 @@
 
 #include <vector>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/no_destructor.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/no_destructor.h"
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/optional_unittest.cc b/src/base/optional_unittest.cc
index f2a26f8..7d7c3cc 100644
--- a/src/base/optional_unittest.cc
+++ b/src/base/optional_unittest.cc
@@ -26,7 +26,8 @@
 #include <string>
 #include <vector>
 
-#include "perfetto/base/optional.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/utils.h"
 
 using ::testing::ElementsAre;
 
diff --git a/src/base/paged_memory.cc b/src/base/paged_memory.cc
index 95eebc0..2339309 100644
--- a/src/base/paged_memory.cc
+++ b/src/base/paged_memory.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/paged_memory.h"
+#include "perfetto/ext/base/paged_memory.h"
 
 #include <algorithm>
 #include <cmath>
@@ -26,7 +26,8 @@
 #endif  // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/container_annotations.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace base {
@@ -37,7 +38,7 @@
 
 #if TRACK_COMMITTED_SIZE()
 constexpr size_t kCommitChunkSize = kPageSize * 1024;  // 4mB
-#endif  // TRACK_COMMITTED_SIZE()
+#endif                                                 // TRACK_COMMITTED_SIZE()
 
 }  // namespace
 
@@ -75,11 +76,11 @@
 
 PagedMemory::PagedMemory() {}
 
-PagedMemory::PagedMemory(char* p, size_t size) : p_(p), size_(size) {
-  ANNOTATE_NEW_BUFFER(p_, size_, committed_size_);
-}
+PagedMemory::PagedMemory(char* p, size_t size)
+    : p_(p),
+      size_(size){ANNOTATE_NEW_BUFFER(p_, size_, committed_size_)}
 
-PagedMemory::PagedMemory(PagedMemory&& other) noexcept {
+      PagedMemory::PagedMemory(PagedMemory && other) noexcept {
   *this = other;
   other.p_ = nullptr;
 }
@@ -103,7 +104,7 @@
   int res = munmap(start, outer_size);
   PERFETTO_CHECK(res == 0);
 #endif  // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
-  ANNOTATE_DELETE_BUFFER(p_, size_, committed_size_);
+  ANNOTATE_DELETE_BUFFER(p_, size_, committed_size_)
 }
 
 bool PagedMemory::AdviseDontNeed(void* p, size_t size) {
@@ -141,12 +142,12 @@
                            PAGE_READWRITE);
   PERFETTO_CHECK(res);
   ANNOTATE_CHANGE_SIZE(p_, size_, committed_size_,
-                       committed_size_ + commit_size);
+                       committed_size_ + commit_size)
   committed_size_ += commit_size;
 #else   // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
   // mmap commits automatically as needed, so we only track here for ASAN.
   committed_size = std::max(committed_size_, committed_size);
-  ANNOTATE_CHANGE_SIZE(p_, size_, committed_size_, committed_size);
+  ANNOTATE_CHANGE_SIZE(p_, size_, committed_size_, committed_size)
   committed_size_ = committed_size;
 #endif  // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 }
diff --git a/src/base/paged_memory_unittest.cc b/src/base/paged_memory_unittest.cc
index 103cf1e..1ccea60 100644
--- a/src/base/paged_memory_unittest.cc
+++ b/src/base/paged_memory_unittest.cc
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/paged_memory.h"
+#include "perfetto/ext/base/paged_memory.h"
 
 #include <stdint.h>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
 #include "src/base/test/vm_test_utils.h"
 
diff --git a/src/base/pipe.cc b/src/base/pipe.cc
index db7fd3a..5c9c3d7 100644
--- a/src/base/pipe.cc
+++ b/src/base/pipe.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/pipe.h"
+#include "perfetto/ext/base/pipe.h"
 
 #include <sys/types.h>
 #include <unistd.h>
diff --git a/src/base/scoped_file_unittest.cc b/src/base/scoped_file_unittest.cc
index 257b5ca..7c77641 100644
--- a/src/base/scoped_file_unittest.cc
+++ b/src/base/scoped_file_unittest.cc
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 #include "perfetto/base/build_config.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
@@ -29,7 +29,7 @@
 #define TEST_INVALID_CLOSE
 #endif
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/string_splitter.cc b/src/base/string_splitter.cc
index aab47b1..adf9fc8 100644
--- a/src/base/string_splitter.cc
+++ b/src/base/string_splitter.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/string_splitter.h"
+#include "perfetto/ext/base/string_splitter.h"
 
 #include <utility>
 
diff --git a/src/base/string_splitter_unittest.cc b/src/base/string_splitter_unittest.cc
index c3a3796..fa98434 100644
--- a/src/base/string_splitter_unittest.cc
+++ b/src/base/string_splitter_unittest.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/string_splitter.h"
+#include "perfetto/ext/base/string_splitter.h"
 
 #include <vector>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/string_utils.cc b/src/base/string_utils.cc
index 41df6b6..e5b7c4e 100644
--- a/src/base/string_utils.cc
+++ b/src/base/string_utils.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/string_utils.h"
+#include "perfetto/ext/base/string_utils.h"
 
 #include <algorithm>
 
@@ -37,6 +37,12 @@
   return haystack.find(needle) != std::string::npos;
 }
 
+bool CaseInsensitiveEqual(const std::string& first, const std::string& second) {
+  return first.size() == second.size() &&
+         std::equal(first.begin(), first.end(), second.begin(),
+                    [](char a, char b) { return tolower(a) == tolower(b); });
+}
+
 std::string Join(const std::vector<std::string>& parts,
                  const std::string& delim) {
   std::string acc;
diff --git a/src/base/string_utils_unittest.cc b/src/base/string_utils_unittest.cc
index bed511f..72471e7 100644
--- a/src/base/string_utils_unittest.cc
+++ b/src/base/string_utils_unittest.cc
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/string_utils.h"
+#include "perfetto/ext/base/string_utils.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
@@ -47,6 +47,15 @@
   EXPECT_FALSE(EndsWith("", "c"));
 }
 
+TEST(StringUtilsTest, CaseInsensitiveEqual) {
+  EXPECT_TRUE(CaseInsensitiveEqual("", ""));
+  EXPECT_TRUE(CaseInsensitiveEqual("abc", "abc"));
+  EXPECT_TRUE(CaseInsensitiveEqual("ABC", "abc"));
+  EXPECT_TRUE(CaseInsensitiveEqual("abc", "ABC"));
+  EXPECT_FALSE(CaseInsensitiveEqual("abc", "AB"));
+  EXPECT_FALSE(CaseInsensitiveEqual("ab", "ABC"));
+}
+
 TEST(StringUtilsTest, SplitString) {
   EXPECT_THAT(SplitString("", ":"), ElementsAre(""));
   EXPECT_THAT(SplitString("a:b:c", ":"), ElementsAre("a", "b", "c"));
diff --git a/src/base/string_view.cc b/src/base/string_view.cc
index b10239d..7292546 100644
--- a/src/base/string_view.cc
+++ b/src/base/string_view.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/string_view_unittest.cc b/src/base/string_view_unittest.cc
index 61dd2a6..4e1ef06 100644
--- a/src/base/string_view_unittest.cc
+++ b/src/base/string_view_unittest.cc
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 
 #include <forward_list>
 #include <unordered_map>
 #include <unordered_set>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/string_writer_unittest.cc b/src/base/string_writer_unittest.cc
index f251ba6..8d83dcc 100644
--- a/src/base/string_writer_unittest.cc
+++ b/src/base/string_writer_unittest.cc
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/string_writer.h"
+#include "perfetto/ext/base/string_writer.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/task_runner_unittest.cc b/src/base/task_runner_unittest.cc
index 46620de..611cff3 100644
--- a/src/base/task_runner_unittest.cc
+++ b/src/base/task_runner_unittest.cc
@@ -14,21 +14,22 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/unix_task_runner.h"
+#include "perfetto/ext/base/unix_task_runner.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
     !PERFETTO_BUILDFLAG(PERFETTO_EMBEDDER_BUILD)
-#include "perfetto/base/android_task_runner.h"
+#include "perfetto/ext/base/android_task_runner.h"
 #endif
 
 #include <thread>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/pipe.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/pipe.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/base/test/gtest_test_suite.h"
 
 namespace perfetto {
diff --git a/src/base/temp_file.cc b/src/base/temp_file.cc
index e648108..d57d383 100644
--- a/src/base/temp_file.cc
+++ b/src/base/temp_file.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/temp_file.h"
 
 #include <stdlib.h>
 #include <unistd.h>
diff --git a/src/base/temp_file_unittest.cc b/src/base/temp_file_unittest.cc
index d167b66..8f5dc22 100644
--- a/src/base/temp_file_unittest.cc
+++ b/src/base/temp_file_unittest.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/temp_file.h"
 
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/test/benchmark_main.cc b/src/base/test/benchmark_main.cc
index f904cfc..e410e52 100644
--- a/src/base/test/benchmark_main.cc
+++ b/src/base/test/benchmark_main.cc
@@ -12,6 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "benchmark/benchmark.h"
+#include <benchmark/benchmark.h>
 
 BENCHMARK_MAIN();
diff --git a/src/base/test/gtest_test_suite.h b/src/base/test/gtest_test_suite.h
index 6bf7758..b8b7561 100644
--- a/src/base/test/gtest_test_suite.h
+++ b/src/base/test/gtest_test_suite.h
@@ -17,7 +17,7 @@
 #ifndef SRC_BASE_TEST_GTEST_TEST_SUITE_H_
 #define SRC_BASE_TEST_GTEST_TEST_SUITE_H_
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 // Define newer TEST_SUITE googletest APIs as aliases of the older APIs where
 // necessary. This makes it possible to migrate Perfetto to the newer APIs and
diff --git a/src/base/test/test_task_runner.h b/src/base/test/test_task_runner.h
index 28cb92a..a1a9a35 100644
--- a/src/base/test/test_task_runner.h
+++ b/src/base/test/test_task_runner.h
@@ -25,12 +25,12 @@
 #include <string>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/base/unix_task_runner.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/base/unix_task_runner.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
     !PERFETTO_BUILDFLAG(PERFETTO_EMBEDDER_BUILD)
-#include "perfetto/base/android_task_runner.h"
+#include "perfetto/ext/base/android_task_runner.h"
 #endif
 
 namespace perfetto {
diff --git a/src/base/test/utils.h b/src/base/test/utils.h
index b3677b9..2884b57 100644
--- a/src/base/test/utils.h
+++ b/src/base/test/utils.h
@@ -19,7 +19,7 @@
 
 #include <string>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
 
 #if defined(GTEST_HAS_DEATH_TEST)
diff --git a/src/base/test/vm_test_utils.cc b/src/base/test/vm_test_utils.cc
index 619fcbf..58a81a6 100644
--- a/src/base/test/vm_test_utils.cc
+++ b/src/base/test/vm_test_utils.cc
@@ -17,7 +17,7 @@
 #include "src/base/test/vm_test_utils.h"
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 #include <memory>
 
@@ -32,7 +32,7 @@
 #include <sys/stat.h>
 #endif
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
 
 namespace perfetto {
diff --git a/src/base/thread_checker.cc b/src/base/thread_checker.cc
index c723dbe..501abbb 100644
--- a/src/base/thread_checker.cc
+++ b/src/base/thread_checker.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/thread_checker.h"
+#include "perfetto/ext/base/thread_checker.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 #include <Windows.h>
diff --git a/src/base/thread_checker_unittest.cc b/src/base/thread_checker_unittest.cc
index b1bdc3d..46ffcd3 100644
--- a/src/base/thread_checker_unittest.cc
+++ b/src/base/thread_checker_unittest.cc
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/thread_checker.h"
+#include "perfetto/ext/base/thread_checker.h"
 
 #include <pthread.h>
 
 #include <functional>
 #include <memory>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/thread_task_runner.cc b/src/base/thread_task_runner.cc
index a545e6b..d0a1036 100644
--- a/src/base/thread_task_runner.cc
+++ b/src/base/thread_task_runner.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/thread_task_runner.h"
+#include "perfetto/ext/base/thread_task_runner.h"
 
 #include <condition_variable>
 #include <functional>
@@ -22,7 +22,7 @@
 #include <thread>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/unix_task_runner.h"
+#include "perfetto/ext/base/unix_task_runner.h"
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/thread_task_runner_unittest.cc b/src/base/thread_task_runner_unittest.cc
index 823967b..c4356fe 100644
--- a/src/base/thread_task_runner_unittest.cc
+++ b/src/base/thread_task_runner_unittest.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/thread_task_runner.h"
+#include "perfetto/ext/base/thread_task_runner.h"
 
 #include <thread>
 
-#include "gtest/gtest.h"
-#include "perfetto/base/thread_checker.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/thread_checker.h"
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/time.cc b/src/base/time.cc
index 8e22d59..f3f809a 100644
--- a/src/base/time.cc
+++ b/src/base/time.cc
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
+#include "perfetto/ext/base/time.h"
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/time.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 #include <Windows.h>
diff --git a/src/base/time_unittest.cc b/src/base/time_unittest.cc
index ee1aa9d..cac79bd 100644
--- a/src/base/time_unittest.cc
+++ b/src/base/time_unittest.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/time.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/unix_socket.cc b/src/base/unix_socket.cc
index 1a8364f..cbf3301 100644
--- a/src/base/unix_socket.cc
+++ b/src/base/unix_socket.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/unix_socket.h"
+#include "perfetto/ext/base/unix_socket.h"
 
 #include <errno.h>
 #include <fcntl.h>
@@ -32,7 +32,7 @@
 #include "perfetto/base/build_config.h"
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX)
 #include <sys/ucred.h>
diff --git a/src/base/unix_socket_unittest.cc b/src/base/unix_socket_unittest.cc
index 5ea646e..e7b3d1a 100644
--- a/src/base/unix_socket_unittest.cc
+++ b/src/base/unix_socket_unittest.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/unix_socket.h"
+#include "perfetto/ext/base/unix_socket.h"
 
 #include <signal.h>
 #include <sys/mman.h>
@@ -24,14 +24,14 @@
 #include <list>
 #include <thread>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/pipe.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/pipe.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/ipc/test/test_socket.h"
 
diff --git a/src/base/unix_task_runner.cc b/src/base/unix_task_runner.cc
index 0b651ae..6ef71aa 100644
--- a/src/base/unix_task_runner.cc
+++ b/src/base/unix_task_runner.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/unix_task_runner.h"
+#include "perfetto/ext/base/unix_task_runner.h"
 
 #include "perfetto/base/build_config.h"
 
@@ -24,6 +24,8 @@
 
 #include <limits>
 
+#include "perfetto/ext/base/watchdog.h"
+
 namespace perfetto {
 namespace base {
 
@@ -114,10 +116,10 @@
 
   errno = 0;
   if (immediate_task)
-    RunTask(immediate_task);
+    RunTaskWithWatchdogGuard(immediate_task);
   errno = 0;
   if (delayed_task)
-    RunTask(delayed_task);
+    RunTaskWithWatchdogGuard(delayed_task);
 }
 
 void UnixTaskRunner::PostFileDescriptorWatches() {
@@ -163,7 +165,7 @@
     task = it->second.callback;
   }
   errno = 0;
-  RunTask(task);
+  RunTaskWithWatchdogGuard(task);
 }
 
 int UnixTaskRunner::GetDelayMsToNextTaskLocked() const {
diff --git a/src/base/utils_unittest.cc b/src/base/utils_unittest.cc
index 1d0f834..a1c04af 100644
--- a/src/base/utils_unittest.cc
+++ b/src/base/utils_unittest.cc
@@ -14,17 +14,17 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 #include <fcntl.h>
 #include <signal.h>
 #include <stdint.h>
 #include <unistd.h>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/pipe.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/pipe.h"
 
 namespace perfetto {
 namespace base {
diff --git a/src/base/watchdog_posix.cc b/src/base/watchdog_posix.cc
index dd68455..9a8965e 100644
--- a/src/base/watchdog_posix.cc
+++ b/src/base/watchdog_posix.cc
@@ -23,7 +23,7 @@
 // one-off case.
 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX)
 
-#include "perfetto/base/watchdog_posix.h"
+#include "perfetto/ext/base/watchdog_posix.h"
 
 #include <fcntl.h>
 #include <inttypes.h>
@@ -35,8 +35,8 @@
 
 #include "perfetto/base/build_config.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/thread_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/thread_utils.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_EMBEDDER_BUILD)
 #error perfetto::base::Watchdog should not be used in Chromium or embedders
diff --git a/src/base/watchdog_unittest.cc b/src/base/watchdog_unittest.cc
index ec55101..e8acaf7 100644
--- a/src/base/watchdog_unittest.cc
+++ b/src/base/watchdog_unittest.cc
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/watchdog.h"
+#include "perfetto/ext/base/watchdog.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/thread_utils.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/thread_utils.h"
 
 #include <signal.h>
 #include <time.h>
diff --git a/src/base/weak_ptr_unittest.cc b/src/base/weak_ptr_unittest.cc
index eebd36d..4b63d4e 100644
--- a/src/base/weak_ptr_unittest.cc
+++ b/src/base/weak_ptr_unittest.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/weak_ptr.h"
+#include "perfetto/ext/base/weak_ptr.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace base {
diff --git a/src/ipc/BUILD.gn b/src/ipc/BUILD.gn
index 290da3c..e14d147 100644
--- a/src/ipc/BUILD.gn
+++ b/src/ipc/BUILD.gn
@@ -12,9 +12,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../gn/perfetto.gni")
 import("../../gn/fuzzer.gni")
 import("../../gn/ipc_library.gni")
+import("../../gn/perfetto.gni")
 import("../../gn/proto_library.gni")
 
 # For use_libfuzzer.
@@ -31,7 +31,7 @@
 source_set("ipc") {
   public_configs = [ "../../gn:default_config" ]
   public_deps = [
-    "../../include/perfetto/ipc",
+    "../../include/perfetto/ext/ipc",
     "../base:unix_socket",
   ]
   deps = [
diff --git a/src/ipc/buffered_frame_deserializer.cc b/src/ipc/buffered_frame_deserializer.cc
index 620fbae..f065443 100644
--- a/src/ipc/buffered_frame_deserializer.cc
+++ b/src/ipc/buffered_frame_deserializer.cc
@@ -24,7 +24,7 @@
 
 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 #include "src/ipc/wire_protocol.pb.h"
 
diff --git a/src/ipc/buffered_frame_deserializer.h b/src/ipc/buffered_frame_deserializer.h
index a8c3f74..3aeee1f 100644
--- a/src/ipc/buffered_frame_deserializer.h
+++ b/src/ipc/buffered_frame_deserializer.h
@@ -24,9 +24,9 @@
 
 #include <sys/mman.h>
 
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/ipc/basic_types.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/ipc/basic_types.h"
 
 namespace perfetto {
 namespace ipc {
diff --git a/src/ipc/buffered_frame_deserializer_fuzzer.cc b/src/ipc/buffered_frame_deserializer_fuzzer.cc
index ae6525b..c3b22bb 100644
--- a/src/ipc/buffered_frame_deserializer_fuzzer.cc
+++ b/src/ipc/buffered_frame_deserializer_fuzzer.cc
@@ -17,7 +17,7 @@
 #include <stddef.h>
 #include <stdint.h>
 
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/ipc/buffered_frame_deserializer.h"
 #include "src/ipc/wire_protocol.pb.h"
 
diff --git a/src/ipc/buffered_frame_deserializer_unittest.cc b/src/ipc/buffered_frame_deserializer_unittest.cc
index e38aa7d..c9a74e2 100644
--- a/src/ipc/buffered_frame_deserializer_unittest.cc
+++ b/src/ipc/buffered_frame_deserializer_unittest.cc
@@ -19,9 +19,9 @@
 #include <algorithm>
 #include <string>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 #include "src/ipc/wire_protocol.pb.h"
 
diff --git a/src/ipc/client_impl.cc b/src/ipc/client_impl.cc
index d648df5..ff0dca2 100644
--- a/src/ipc/client_impl.cc
+++ b/src/ipc/client_impl.cc
@@ -23,9 +23,9 @@
 #include <utility>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/ipc/service_descriptor.h"
-#include "perfetto/ipc/service_proxy.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/ipc/service_descriptor.h"
+#include "perfetto/ext/ipc/service_proxy.h"
 
 // TODO(primiano): Add ThreadChecker everywhere.
 
diff --git a/src/ipc/client_impl.h b/src/ipc/client_impl.h
index d071053..db65f02 100644
--- a/src/ipc/client_impl.h
+++ b/src/ipc/client_impl.h
@@ -17,10 +17,10 @@
 #ifndef SRC_IPC_CLIENT_IMPL_H_
 #define SRC_IPC_CLIENT_IMPL_H_
 
-#include "perfetto/base/scoped_file.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/ipc/client.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/ipc/client.h"
 #include "src/ipc/buffered_frame_deserializer.h"
 
 #include "src/ipc/wire_protocol.pb.h"
diff --git a/src/ipc/client_impl_unittest.cc b/src/ipc/client_impl_unittest.cc
index 139702e..7fee3f0 100644
--- a/src/ipc/client_impl_unittest.cc
+++ b/src/ipc/client_impl_unittest.cc
@@ -21,14 +21,14 @@
 
 #include <string>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/ipc/service_descriptor.h"
-#include "perfetto/ipc/service_proxy.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/ipc/service_descriptor.h"
+#include "perfetto/ext/ipc/service_proxy.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/ipc/buffered_frame_deserializer.h"
 #include "src/ipc/test/test_socket.h"
diff --git a/src/ipc/deferred.cc b/src/ipc/deferred.cc
index d923ffc..2bc12ec 100644
--- a/src/ipc/deferred.cc
+++ b/src/ipc/deferred.cc
@@ -14,10 +14,12 @@
  * limitations under the License.
  */
 
-#include "perfetto/ipc/deferred.h"
+#include "perfetto/ext/ipc/deferred.h"
 
 #include <google/protobuf/message_lite.h>
+
 #include "perfetto/base/logging.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace ipc {
diff --git a/src/ipc/deferred_unittest.cc b/src/ipc/deferred_unittest.cc
index 4362c07..b927e14 100644
--- a/src/ipc/deferred_unittest.cc
+++ b/src/ipc/deferred_unittest.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "perfetto/ipc/deferred.h"
+#include "perfetto/ext/ipc/deferred.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
 
 #include "src/ipc/test/deferred_unittest_messages.pb.h"
@@ -26,7 +26,7 @@
 namespace {
 
 #if PERFETTO_DCHECK_IS_ON()
-#define EXPECT_DCHECK(x) EXPECT_DEATH_IF_SUPPORTED((x), ".*");
+#define EXPECT_DCHECK(x) EXPECT_DEATH_IF_SUPPORTED((x), ".*")
 #else
 #define EXPECT_DCHECK(x) x
 #endif
diff --git a/src/ipc/host_impl.cc b/src/ipc/host_impl.cc
index 0ccdde3..6895424 100644
--- a/src/ipc/host_impl.cc
+++ b/src/ipc/host_impl.cc
@@ -22,9 +22,9 @@
 #include <utility>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/ipc/service.h"
-#include "perfetto/ipc/service_descriptor.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/ipc/service.h"
+#include "perfetto/ext/ipc/service_descriptor.h"
 
 #include "src/ipc/wire_protocol.pb.h"
 
diff --git a/src/ipc/host_impl.h b/src/ipc/host_impl.h
index 262da9a..97b5a8b 100644
--- a/src/ipc/host_impl.h
+++ b/src/ipc/host_impl.h
@@ -23,10 +23,10 @@
 #include <vector>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/ipc/deferred.h"
-#include "perfetto/ipc/host.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/ipc/deferred.h"
+#include "perfetto/ext/ipc/host.h"
 #include "src/ipc/buffered_frame_deserializer.h"
 
 namespace perfetto {
diff --git a/src/ipc/host_impl_unittest.cc b/src/ipc/host_impl_unittest.cc
index 121f57b..57fe441 100644
--- a/src/ipc/host_impl_unittest.cc
+++ b/src/ipc/host_impl_unittest.cc
@@ -18,15 +18,15 @@
 
 #include <memory>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/ipc/service.h"
-#include "perfetto/ipc/service_descriptor.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/ipc/service.h"
+#include "perfetto/ext/ipc/service_descriptor.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/ipc/buffered_frame_deserializer.h"
 #include "src/ipc/test/test_socket.h"
diff --git a/src/ipc/protoc_plugin/ipc_generator.cc b/src/ipc/protoc_plugin/ipc_generator.cc
index 3c3befb..c955cb1 100644
--- a/src/ipc/protoc_plugin/ipc_generator.cc
+++ b/src/ipc/protoc_plugin/ipc_generator.cc
@@ -250,14 +250,14 @@
   h_printer.Print(kBanner);
   h_printer.Print("#ifndef $guard$\n#define $guard$\n\n", "guard", guard);
   h_printer.Print("#include \"$h$\"\n", "h", StripName(*file) + ".pb.h");
-  h_printer.Print("#include \"perfetto/ipc/deferred.h\"\n");
-  h_printer.Print("#include \"perfetto/ipc/service.h\"\n");
-  h_printer.Print("#include \"perfetto/ipc/service_descriptor.h\"\n");
-  h_printer.Print("#include \"perfetto/ipc/service_proxy.h\"\n\n");
+  h_printer.Print("#include \"perfetto/ext/ipc/deferred.h\"\n");
+  h_printer.Print("#include \"perfetto/ext/ipc/service.h\"\n");
+  h_printer.Print("#include \"perfetto/ext/ipc/service_descriptor.h\"\n");
+  h_printer.Print("#include \"perfetto/ext/ipc/service_proxy.h\"\n\n");
 
   cc_printer.Print(kBanner);
   cc_printer.Print("#include \"$h$\"\n", "h", GetStubName(*file) + ".h");
-  cc_printer.Print("#include \"perfetto/ipc/codegen_helpers.h\"\n\n");
+  cc_printer.Print("#include \"perfetto/ext/ipc/codegen_helpers.h\"\n\n");
   cc_printer.Print("#include <memory>\n");
 
   for (int i = 0; i < file->service_count(); i++) {
diff --git a/src/ipc/service_proxy.cc b/src/ipc/service_proxy.cc
index dabde38..5edb857 100644
--- a/src/ipc/service_proxy.cc
+++ b/src/ipc/service_proxy.cc
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#include "perfetto/ipc/service_proxy.h"
+#include "perfetto/ext/ipc/service_proxy.h"
 
 #include <utility>
 
 #include <google/protobuf/message_lite.h>
 #include "perfetto/base/logging.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/ipc/service_descriptor.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/ipc/service_descriptor.h"
 #include "src/ipc/client_impl.h"
 
 namespace perfetto {
diff --git a/src/ipc/test/ipc_integrationtest.cc b/src/ipc/test/ipc_integrationtest.cc
index 9e21128..cfe5202 100644
--- a/src/ipc/test/ipc_integrationtest.cc
+++ b/src/ipc/test/ipc_integrationtest.cc
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/ipc/client.h"
-#include "perfetto/ipc/host.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/ipc/client.h"
+#include "perfetto/ext/ipc/host.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/ipc/test/test_socket.h"
 
diff --git a/src/ipc/virtual_destructors.cc b/src/ipc/virtual_destructors.cc
index 4a4cb04..d16a770 100644
--- a/src/ipc/virtual_destructors.cc
+++ b/src/ipc/virtual_destructors.cc
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include "perfetto/ipc/client.h"
-#include "perfetto/ipc/host.h"
-#include "perfetto/ipc/service.h"
-#include "perfetto/ipc/service_proxy.h"
+#include "perfetto/ext/ipc/client.h"
+#include "perfetto/ext/ipc/host.h"
+#include "perfetto/ext/ipc/service.h"
+#include "perfetto/ext/ipc/service_proxy.h"
 
 // This translation unit contains the definitions for the destructor of pure
 // virtual interfaces for the current build target. The alternative would be
diff --git a/src/perfetto_cmd/BUILD.gn b/src/perfetto_cmd/BUILD.gn
index 2a741a3..0050bea 100644
--- a/src/perfetto_cmd/BUILD.gn
+++ b/src/perfetto_cmd/BUILD.gn
@@ -18,7 +18,7 @@
 source_set("perfetto_cmd") {
   public_deps = [
     ":protos",
-    "../../include/perfetto/traced",
+    "../../include/perfetto/ext/traced",
   ]
   deps = [
     ":trigger_producer",
@@ -54,7 +54,7 @@
 source_set("trigger_perfetto_cmd") {
   public_deps = [
     ":protos",
-    "../../include/perfetto/traced",
+    "../../include/perfetto/ext/traced",
   ]
   deps = [
     ":trigger_producer",
@@ -99,7 +99,7 @@
     ":perfetto_cmd",
     "../../gn:default_deps",
     "../../gn:gtest_deps",
-    "../../include/perfetto/base",
+    "../../include/perfetto/ext/base",
     "../../protos/perfetto/config:lite",
   ]
   sources = [
diff --git a/src/perfetto_cmd/config_unittest.cc b/src/perfetto_cmd/config_unittest.cc
index 024c42f..41ccbb8 100644
--- a/src/perfetto_cmd/config_unittest.cc
+++ b/src/perfetto_cmd/config_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/perfetto_cmd/config.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include "perfetto/config/trace_config.pb.h"
 
diff --git a/src/perfetto_cmd/main.cc b/src/perfetto_cmd/main.cc
index dc71930..0fbdfc4 100644
--- a/src/perfetto_cmd/main.cc
+++ b/src/perfetto_cmd/main.cc
@@ -15,7 +15,7 @@
  */
 
 #include <stdio.h>
-#include "perfetto/traced/traced.h"
+#include "perfetto/ext/traced/traced.h"
 
 int main(int argc, char** argv) {
   return perfetto::PerfettoCmdMain(argc, argv);
diff --git a/src/perfetto_cmd/pbtxt_to_pb.cc b/src/perfetto_cmd/pbtxt_to_pb.cc
index fe50fc0..35cda58 100644
--- a/src/perfetto_cmd/pbtxt_to_pb.cc
+++ b/src/perfetto_cmd/pbtxt_to_pb.cc
@@ -23,11 +23,11 @@
 
 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/utils.h"
 #include "perfetto/common/descriptor.pb.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/message.h"
 #include "perfetto/protozero/message_handle.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
diff --git a/src/perfetto_cmd/pbtxt_to_pb_unittest.cc b/src/perfetto_cmd/pbtxt_to_pb_unittest.cc
index f7a7320..3fbeb87 100644
--- a/src/perfetto_cmd/pbtxt_to_pb_unittest.cc
+++ b/src/perfetto_cmd/pbtxt_to_pb_unittest.cc
@@ -19,8 +19,8 @@
 #include <memory>
 #include <string>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
 #include "perfetto/config/trace_config.pb.h"
diff --git a/src/perfetto_cmd/perfetto_cmd.cc b/src/perfetto_cmd/perfetto_cmd.cc
index 1c3ac22..823fbdd 100644
--- a/src/perfetto_cmd/perfetto_cmd.cc
+++ b/src/perfetto_cmd/perfetto_cmd.cc
@@ -21,6 +21,7 @@
 #include <signal.h>
 #include <stdio.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
 
@@ -29,18 +30,18 @@
 #include <iterator>
 #include <sstream>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/time.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/traced/traced.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 #include "perfetto/protozero/proto_utils.h"
-#include "perfetto/traced/traced.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
 #include "src/perfetto_cmd/config.h"
 #include "src/perfetto_cmd/pbtxt_to_pb.h"
 #include "src/perfetto_cmd/trigger_producer.h"
@@ -120,6 +121,14 @@
   return true;
 }
 
+void ClearUmask() {
+#if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
+    PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
+    PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX)
+  umask(0000);
+#endif
+}
+
 }  // namespace
 
 // Temporary directory for DropBox traces. Note that this is automatically
@@ -167,6 +176,8 @@
 }
 
 int PerfettoCmd::Main(int argc, char** argv) {
+  ClearUmask();  // make sure that file creation is not affected by umask
+
   enum LongOption {
     OPT_ALERT_ID = 1000,
     OPT_CONFIG_ID,
@@ -518,9 +529,10 @@
       trace_config_->guardrail_overrides().max_upload_per_day_bytes();
 #endif
 
-  if ((trace_config_->duration_ms() == 0) && args.is_dropbox &&
-      !args.ignore_guardrails) {
-    PERFETTO_ELOG("Can't trace indefinitely when uploading via Dropbox.");
+  if (args.is_dropbox && !args.ignore_guardrails &&
+      (trace_config_->duration_ms() == 0 &&
+       trace_config_->trigger_config().trigger_timeout_ms() == 0)) {
+    PERFETTO_ELOG("Can't trace indefinitely when tracing to Dropbox.");
     return 1;
   }
 
diff --git a/src/perfetto_cmd/perfetto_cmd.h b/src/perfetto_cmd/perfetto_cmd.h
index 122f042..0461ab8 100644
--- a/src/perfetto_cmd/perfetto_cmd.h
+++ b/src/perfetto_cmd/perfetto_cmd.h
@@ -24,17 +24,17 @@
 #include <time.h>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/event.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/unix_task_runner.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/ipc/consumer_ipc_client.h"
+#include "perfetto/ext/base/event.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/unix_task_runner.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
 #include "src/perfetto_cmd/rate_limiter.h"
 
 #include "src/perfetto_cmd/perfetto_cmd_state.pb.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
-#include "perfetto/base/android_task_runner.h"
+#include "perfetto/ext/base/android_task_runner.h"
 #endif  // PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
 
 namespace perfetto {
diff --git a/src/perfetto_cmd/rate_limiter.cc b/src/perfetto_cmd/rate_limiter.cc
index 3b018bc..973a751 100644
--- a/src/perfetto_cmd/rate_limiter.cc
+++ b/src/perfetto_cmd/rate_limiter.cc
@@ -20,10 +20,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/perfetto_cmd/perfetto_cmd.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
diff --git a/src/perfetto_cmd/rate_limiter.h b/src/perfetto_cmd/rate_limiter.h
index beb61c8..54411c7 100644
--- a/src/perfetto_cmd/rate_limiter.h
+++ b/src/perfetto_cmd/rate_limiter.h
@@ -17,7 +17,7 @@
 #ifndef SRC_PERFETTO_CMD_RATE_LIMITER_H_
 #define SRC_PERFETTO_CMD_RATE_LIMITER_H_
 
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/time.h"
 #include "src/perfetto_cmd/perfetto_cmd_state.pb.h"
 
 namespace perfetto {
diff --git a/src/perfetto_cmd/rate_limiter_unittest.cc b/src/perfetto_cmd/rate_limiter_unittest.cc
index 81521cf..839c879 100644
--- a/src/perfetto_cmd/rate_limiter_unittest.cc
+++ b/src/perfetto_cmd/rate_limiter_unittest.cc
@@ -18,13 +18,13 @@
 
 #include <stdio.h>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/utils.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 using testing::_;
 using testing::NiceMock;
diff --git a/src/perfetto_cmd/trigger_perfetto.cc b/src/perfetto_cmd/trigger_perfetto.cc
index c6e3a14..cd22ea2 100644
--- a/src/perfetto_cmd/trigger_perfetto.cc
+++ b/src/perfetto_cmd/trigger_perfetto.cc
@@ -20,8 +20,8 @@
 #include <vector>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/unix_task_runner.h"
-#include "perfetto/traced/traced.h"
+#include "perfetto/ext/base/unix_task_runner.h"
+#include "perfetto/ext/traced/traced.h"
 #include "src/perfetto_cmd/trigger_producer.h"
 
 namespace perfetto {
diff --git a/src/perfetto_cmd/trigger_perfetto_main.cc b/src/perfetto_cmd/trigger_perfetto_main.cc
index 3ba5581..01173bf 100644
--- a/src/perfetto_cmd/trigger_perfetto_main.cc
+++ b/src/perfetto_cmd/trigger_perfetto_main.cc
@@ -15,7 +15,7 @@
  */
 
 #include <stdio.h>
-#include "perfetto/traced/traced.h"
+#include "perfetto/ext/traced/traced.h"
 
 int main(int argc, char** argv) {
   return perfetto::TriggerPerfettoMain(argc, argv);
diff --git a/src/perfetto_cmd/trigger_producer.cc b/src/perfetto_cmd/trigger_producer.cc
index c84716f..04f12ad 100644
--- a/src/perfetto_cmd/trigger_producer.cc
+++ b/src/perfetto_cmd/trigger_producer.cc
@@ -19,8 +19,8 @@
 #include <memory>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/ipc/producer_ipc_client.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
 #include "src/tracing/ipc/default_socket.h"
 
 namespace perfetto {
diff --git a/src/perfetto_cmd/trigger_producer.h b/src/perfetto_cmd/trigger_producer.h
index 6abab0e..d12c178 100644
--- a/src/perfetto_cmd/trigger_producer.h
+++ b/src/perfetto_cmd/trigger_producer.h
@@ -21,9 +21,9 @@
 #include <vector>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 namespace perfetto {
 
diff --git a/src/profiling/memory/bookkeeping.cc b/src/profiling/memory/bookkeeping.cc
index 0d1b533..39943a2 100644
--- a/src/profiling/memory/bookkeeping.cc
+++ b/src/profiling/memory/bookkeeping.cc
@@ -21,21 +21,13 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 namespace perfetto {
 namespace profiling {
 
-GlobalCallstackTrie::Node* GlobalCallstackTrie::Node::GetOrCreateChild(
-    const Interned<Frame>& loc) {
-  Node* child = children_.Get(loc);
-  if (!child)
-    child = children_.Emplace(loc, this);
-  return child;
-}
-
 void HeapTracker::RecordMalloc(const std::vector<FrameData>& callstack,
                                uint64_t address,
                                uint64_t size,
@@ -136,6 +128,14 @@
   return alloc.allocated - alloc.freed;
 }
 
+GlobalCallstackTrie::Node* GlobalCallstackTrie::Node::GetOrCreateChild(
+    const Interned<Frame>& loc) {
+  Node* child = children_.Get(loc);
+  if (!child)
+    child = children_.Emplace(loc, this);
+  return child;
+}
+
 std::vector<Interned<Frame>> GlobalCallstackTrie::BuildCallstack(
     const Node* node) const {
   std::vector<Interned<Frame>> res;
diff --git a/src/profiling/memory/bookkeeping.h b/src/profiling/memory/bookkeeping.h
index 892523d..b09eb22 100644
--- a/src/profiling/memory/bookkeeping.h
+++ b/src/profiling/memory/bookkeeping.h
@@ -21,9 +21,9 @@
 #include <string>
 #include <vector>
 
-#include "perfetto/base/lookup_set.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/lookup_set.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/time.h"
 #include "src/profiling/memory/interner.h"
 #include "src/profiling/memory/unwound_messages.h"
 
diff --git a/src/profiling/memory/bookkeeping_dump.h b/src/profiling/memory/bookkeeping_dump.h
index 2362b8d..9b45560 100644
--- a/src/profiling/memory/bookkeeping_dump.h
+++ b/src/profiling/memory/bookkeeping_dump.h
@@ -25,7 +25,7 @@
 #include "perfetto/trace/profiling/profile_packet.pbzero.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
 
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 
 #include "src/profiling/memory/bookkeeping.h"
 #include "src/profiling/memory/interner.h"
diff --git a/src/profiling/memory/bookkeeping_unittest.cc b/src/profiling/memory/bookkeeping_unittest.cc
index a990f65..be64c99 100644
--- a/src/profiling/memory/bookkeeping_unittest.cc
+++ b/src/profiling/memory/bookkeeping_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/profiling/memory/bookkeeping.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/client.cc b/src/profiling/memory/client.cc
index dc6952d..86550b2 100644
--- a/src/profiling/memory/client.cc
+++ b/src/profiling/memory/client.cc
@@ -34,11 +34,11 @@
 #include <new>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/thread_utils.h"
-#include "perfetto/base/time.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/thread_utils.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/profiling/memory/sampler.h"
 #include "src/profiling/memory/scoped_spinlock.h"
 #include "src/profiling/memory/wire_protocol.h"
diff --git a/src/profiling/memory/client.h b/src/profiling/memory/client.h
index 72b9c6c..5b4a95e 100644
--- a/src/profiling/memory/client.h
+++ b/src/profiling/memory/client.h
@@ -25,7 +25,7 @@
 #include <mutex>
 #include <vector>
 
-#include "perfetto/base/unix_socket.h"
+#include "perfetto/ext/base/unix_socket.h"
 #include "src/profiling/memory/sampler.h"
 #include "src/profiling/memory/shared_ring_buffer.h"
 #include "src/profiling/memory/unhooked_allocator.h"
diff --git a/src/profiling/memory/client_unittest.cc b/src/profiling/memory/client_unittest.cc
index e5ed201..fb790ac 100644
--- a/src/profiling/memory/client_unittest.cc
+++ b/src/profiling/memory/client_unittest.cc
@@ -16,9 +16,9 @@
 
 #include "src/profiling/memory/client.h"
 
-#include "gtest/gtest.h"
-#include "perfetto/base/thread_utils.h"
-#include "perfetto/base/unix_socket.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/thread_utils.h"
+#include "perfetto/ext/base/unix_socket.h"
 
 #include <thread>
 
diff --git a/src/profiling/memory/heapprofd_end_to_end_test.cc b/src/profiling/memory/heapprofd_end_to_end_test.cc
index 07bd493..e5191b1 100644
--- a/src/profiling/memory/heapprofd_end_to_end_test.cc
+++ b/src/profiling/memory/heapprofd_end_to_end_test.cc
@@ -18,10 +18,10 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/pipe.h"
+#include "perfetto/ext/base/pipe.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/profiling/memory/heapprofd_producer.h"
diff --git a/src/profiling/memory/heapprofd_producer.cc b/src/profiling/memory/heapprofd_producer.cc
index 2c32f07..f55306d 100644
--- a/src/profiling/memory/heapprofd_producer.cc
+++ b/src/profiling/memory/heapprofd_producer.cc
@@ -22,13 +22,13 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/string_utils.h"
-#include "perfetto/base/thread_task_runner.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/trace_writer.h"
-#include "perfetto/tracing/ipc/producer_ipc_client.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/string_utils.h"
+#include "perfetto/ext/base/thread_task_runner.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
 
 #include "src/profiling/memory/bookkeeping_dump.h"
 
diff --git a/src/profiling/memory/heapprofd_producer.h b/src/profiling/memory/heapprofd_producer.h
index ec088cf..f1fee65 100644
--- a/src/profiling/memory/heapprofd_producer.h
+++ b/src/profiling/memory/heapprofd_producer.h
@@ -22,16 +22,16 @@
 #include <map>
 #include <vector>
 
-#include "perfetto/base/optional.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/unix_task_runner.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/unix_task_runner.h"
 
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/trace_writer.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 #include "src/profiling/memory/bookkeeping.h"
 #include "src/profiling/memory/heapprofd_config.h"
diff --git a/src/profiling/memory/heapprofd_producer_unittest.cc b/src/profiling/memory/heapprofd_producer_unittest.cc
index e3cb229..6e936bd 100644
--- a/src/profiling/memory/heapprofd_producer_unittest.cc
+++ b/src/profiling/memory/heapprofd_producer_unittest.cc
@@ -16,12 +16,12 @@
 
 #include "src/profiling/memory/heapprofd_producer.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/commit_data_request.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
 
 #include "src/base/test/test_task_runner.h"
 
diff --git a/src/profiling/memory/interner_unittest.cc b/src/profiling/memory/interner_unittest.cc
index 6b239fd..b7ffb5f 100644
--- a/src/profiling/memory/interner_unittest.cc
+++ b/src/profiling/memory/interner_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/profiling/memory/interner.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/main.cc b/src/profiling/memory/main.cc
index a134930..a66c6aa 100644
--- a/src/profiling/memory/main.cc
+++ b/src/profiling/memory/main.cc
@@ -23,15 +23,15 @@
 #include <getopt.h>
 #include <signal.h>
 
-#include "perfetto/base/event.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/watchdog.h"
+#include "perfetto/ext/base/event.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/watchdog.h"
 #include "src/profiling/memory/heapprofd_producer.h"
 #include "src/profiling/memory/wire_protocol.h"
 #include "src/tracing/ipc/default_socket.h"
 
-#include "perfetto/base/unix_task_runner.h"
+#include "perfetto/ext/base/unix_task_runner.h"
 
 // TODO(rsavitski): the task runner watchdog spawns a thread (normally for
 // tracking cpu/mem usage) that we don't strictly need.
diff --git a/src/profiling/memory/malloc_hooks.cc b/src/profiling/memory/malloc_hooks.cc
index 6651216..a50b662 100644
--- a/src/profiling/memory/malloc_hooks.cc
+++ b/src/profiling/memory/malloc_hooks.cc
@@ -31,9 +31,9 @@
 
 #include "perfetto/base/build_config.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/no_destructor.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/no_destructor.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/profiling/memory/client.h"
 #include "src/profiling/memory/proc_utils.h"
 #include "src/profiling/memory/scoped_spinlock.h"
diff --git a/src/profiling/memory/page_idle_checker.cc b/src/profiling/memory/page_idle_checker.cc
index 62e19f6..10cd1fe 100644
--- a/src/profiling/memory/page_idle_checker.cc
+++ b/src/profiling/memory/page_idle_checker.cc
@@ -15,6 +15,7 @@
  */
 
 #include "src/profiling/memory/page_idle_checker.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/profiling/memory/utils.h"
 
 #include <inttypes.h>
diff --git a/src/profiling/memory/page_idle_checker.h b/src/profiling/memory/page_idle_checker.h
index 5a3dcae..529c097 100644
--- a/src/profiling/memory/page_idle_checker.h
+++ b/src/profiling/memory/page_idle_checker.h
@@ -22,7 +22,7 @@
 #include <stddef.h>
 #include <stdint.h>
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/page_idle_checker_unittest.cc b/src/profiling/memory/page_idle_checker_unittest.cc
index 2f7ada9..1de479c 100644
--- a/src/profiling/memory/page_idle_checker_unittest.cc
+++ b/src/profiling/memory/page_idle_checker_unittest.cc
@@ -16,7 +16,9 @@
 
 #include "src/profiling/memory/page_idle_checker.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
+
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/proc_utils.cc b/src/profiling/memory/proc_utils.cc
index baef7ad..ba5ee79 100644
--- a/src/profiling/memory/proc_utils.cc
+++ b/src/profiling/memory/proc_utils.cc
@@ -20,7 +20,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "perfetto/base/file_utils.h"
+#include "perfetto/ext/base/file_utils.h"
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/proc_utils.h b/src/profiling/memory/proc_utils.h
index 0cd16d8..d20d8ba 100644
--- a/src/profiling/memory/proc_utils.h
+++ b/src/profiling/memory/proc_utils.h
@@ -21,7 +21,7 @@
 #include <set>
 #include <vector>
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/proc_utils_unittest.cc b/src/profiling/memory/proc_utils_unittest.cc
index 9421d9c..cc89dd6 100644
--- a/src/profiling/memory/proc_utils_unittest.cc
+++ b/src/profiling/memory/proc_utils_unittest.cc
@@ -16,8 +16,10 @@
 
 #include "src/profiling/memory/proc_utils.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/sampler.h b/src/profiling/memory/sampler.h
index 0108634..b46d4bf 100644
--- a/src/profiling/memory/sampler.h
+++ b/src/profiling/memory/sampler.h
@@ -22,7 +22,7 @@
 #include <atomic>
 #include <random>
 
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/sampler_unittest.cc b/src/profiling/memory/sampler_unittest.cc
index 4e53f24..0bd45fb 100644
--- a/src/profiling/memory/sampler_unittest.cc
+++ b/src/profiling/memory/sampler_unittest.cc
@@ -16,7 +16,7 @@
 
 #include "src/profiling/memory/sampler.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 #include <thread>
 
diff --git a/src/profiling/memory/scoped_spinlock.cc b/src/profiling/memory/scoped_spinlock.cc
index de9ef59..c306d79 100644
--- a/src/profiling/memory/scoped_spinlock.cc
+++ b/src/profiling/memory/scoped_spinlock.cc
@@ -20,7 +20,7 @@
 
 #include <atomic>
 
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace {
 // Wait for ~1s before timing out (+- spurious wakeups from the sleeps).
diff --git a/src/profiling/memory/scoped_spinlock.h b/src/profiling/memory/scoped_spinlock.h
index 612c151..67d8bd1 100644
--- a/src/profiling/memory/scoped_spinlock.h
+++ b/src/profiling/memory/scoped_spinlock.h
@@ -18,7 +18,7 @@
 #define SRC_PROFILING_MEMORY_SCOPED_SPINLOCK_H_
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 #include <atomic>
 #include <new>
diff --git a/src/profiling/memory/shared_ring_buffer.cc b/src/profiling/memory/shared_ring_buffer.cc
index d913488..99a9c89 100644
--- a/src/profiling/memory/shared_ring_buffer.cc
+++ b/src/profiling/memory/shared_ring_buffer.cc
@@ -26,8 +26,8 @@
 #include <unistd.h>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/temp_file.h"
 #include "src/profiling/memory/scoped_spinlock.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
diff --git a/src/profiling/memory/shared_ring_buffer.h b/src/profiling/memory/shared_ring_buffer.h
index b1d4d89..0c5492f 100644
--- a/src/profiling/memory/shared_ring_buffer.h
+++ b/src/profiling/memory/shared_ring_buffer.h
@@ -17,9 +17,9 @@
 #ifndef SRC_PROFILING_MEMORY_SHARED_RING_BUFFER_H_
 #define SRC_PROFILING_MEMORY_SHARED_RING_BUFFER_H_
 
-#include "perfetto/base/optional.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/profiling/memory/scoped_spinlock.h"
 
 #include <atomic>
diff --git a/src/profiling/memory/shared_ring_buffer_fuzzer.cc b/src/profiling/memory/shared_ring_buffer_fuzzer.cc
index a874a0f..0fd03ce 100644
--- a/src/profiling/memory/shared_ring_buffer_fuzzer.cc
+++ b/src/profiling/memory/shared_ring_buffer_fuzzer.cc
@@ -17,8 +17,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/temp_file.h"
 #include "src/profiling/memory/shared_ring_buffer.h"
 
 namespace perfetto {
diff --git a/src/profiling/memory/shared_ring_buffer_unittest.cc b/src/profiling/memory/shared_ring_buffer_unittest.cc
index 46ee3c7..e11ae67 100644
--- a/src/profiling/memory/shared_ring_buffer_unittest.cc
+++ b/src/profiling/memory/shared_ring_buffer_unittest.cc
@@ -22,8 +22,8 @@
 #include <thread>
 #include <unordered_map>
 
-#include "gtest/gtest.h"
-#include "perfetto/base/optional.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/optional.h"
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/shared_ring_buffer_write_fuzzer.cc b/src/profiling/memory/shared_ring_buffer_write_fuzzer.cc
index 1bdd925..be5bbb9 100644
--- a/src/profiling/memory/shared_ring_buffer_write_fuzzer.cc
+++ b/src/profiling/memory/shared_ring_buffer_write_fuzzer.cc
@@ -17,8 +17,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/temp_file.h"
 #include "src/profiling/memory/shared_ring_buffer.h"
 
 namespace perfetto {
diff --git a/src/profiling/memory/system_property.cc b/src/profiling/memory/system_property.cc
index f73c3dd..a2fda64 100644
--- a/src/profiling/memory/system_property.cc
+++ b/src/profiling/memory/system_property.cc
@@ -17,6 +17,7 @@
 #include "src/profiling/memory/system_property.h"
 
 #include "perfetto/base/logging.h"
+#include "perfetto/ext/base/utils.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
 #include <sys/system_properties.h>
diff --git a/src/profiling/memory/system_property_unittest.cc b/src/profiling/memory/system_property_unittest.cc
index 2bf16ab..8ddf601 100644
--- a/src/profiling/memory/system_property_unittest.cc
+++ b/src/profiling/memory/system_property_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/profiling/memory/system_property.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/profiling/memory/unwinding.cc b/src/profiling/memory/unwinding.cc
index 9db1a84..dcf41e2 100644
--- a/src/profiling/memory/unwinding.cc
+++ b/src/profiling/memory/unwinding.cc
@@ -44,12 +44,12 @@
 
 #include <procinfo/process_map.h>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/string_utils.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/thread_task_runner.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/string_utils.h"
+#include "perfetto/ext/base/thread_task_runner.h"
 
 #include "src/profiling/memory/utils.h"
 #include "src/profiling/memory/wire_protocol.h"
diff --git a/src/profiling/memory/unwinding.h b/src/profiling/memory/unwinding.h
index 2d235b1..9fab671 100644
--- a/src/profiling/memory/unwinding.h
+++ b/src/profiling/memory/unwinding.h
@@ -27,9 +27,9 @@
 #include <unwindstack/JitDebug.h>
 #endif
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/thread_task_runner.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/thread_task_runner.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 #include "src/profiling/memory/bookkeeping.h"
 #include "src/profiling/memory/unwound_messages.h"
 #include "src/profiling/memory/wire_protocol.h"
diff --git a/src/profiling/memory/unwinding_fuzzer.cc b/src/profiling/memory/unwinding_fuzzer.cc
index a80e039..c2046b0 100644
--- a/src/profiling/memory/unwinding_fuzzer.cc
+++ b/src/profiling/memory/unwinding_fuzzer.cc
@@ -17,8 +17,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
-#include "perfetto/base/utils.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 #include "src/profiling/memory/shared_ring_buffer.h"
 #include "src/profiling/memory/unwinding.h"
 #include "src/profiling/memory/unwound_messages.h"
diff --git a/src/profiling/memory/unwinding_unittest.cc b/src/profiling/memory/unwinding_unittest.cc
index 6696410..881d809 100644
--- a/src/profiling/memory/unwinding_unittest.cc
+++ b/src/profiling/memory/unwinding_unittest.cc
@@ -15,12 +15,12 @@
  */
 
 #include "src/profiling/memory/unwinding.h"
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 #include "src/profiling/memory/client.h"
 #include "src/profiling/memory/wire_protocol.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include <cxxabi.h>
 #include <fcntl.h>
diff --git a/src/profiling/memory/wire_protocol.cc b/src/profiling/memory/wire_protocol.cc
index 52af41b..6209fc8 100644
--- a/src/profiling/memory/wire_protocol.cc
+++ b/src/profiling/memory/wire_protocol.cc
@@ -17,8 +17,8 @@
 #include "src/profiling/memory/wire_protocol.h"
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/utils.h"
 
 #include <sys/socket.h>
 #include <sys/types.h>
diff --git a/src/profiling/memory/wire_protocol_unittest.cc b/src/profiling/memory/wire_protocol_unittest.cc
index 189cba0..ed9cc74 100644
--- a/src/profiling/memory/wire_protocol_unittest.cc
+++ b/src/profiling/memory/wire_protocol_unittest.cc
@@ -16,14 +16,14 @@
 
 #include "src/profiling/memory/wire_protocol.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/unix_socket.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/unix_socket.h"
 
 #include <sys/socket.h>
 #include <sys/types.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace profiling {
diff --git a/src/protozero/BUILD.gn b/src/protozero/BUILD.gn
index 1197437..7d1526c 100644
--- a/src/protozero/BUILD.gn
+++ b/src/protozero/BUILD.gn
@@ -24,8 +24,8 @@
   ]
   deps = [
     "../../gn:default_deps",
-    "../../gn:gtest_prod_config",
-    "../base",
+    "../../include/perfetto/base",
+    "../../include/perfetto/ext/base",  # TODO(primiano): remove this
   ]
   sources = [
     "message.cc",
@@ -98,5 +98,6 @@
   deps = [
     ":protozero",
     "../../gn:default_deps",
+    "../base",
   ]
 }
diff --git a/src/protozero/message_handle_unittest.cc b/src/protozero/message_handle_unittest.cc
index 1ca4c28..f6cbdb8 100644
--- a/src/protozero/message_handle_unittest.cc
+++ b/src/protozero/message_handle_unittest.cc
@@ -16,7 +16,7 @@
 
 #include "perfetto/protozero/message_handle.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/protozero/message.h"
 
 namespace protozero {
diff --git a/src/protozero/message_unittest.cc b/src/protozero/message_unittest.cc
index c61bc56..8bffabc 100644
--- a/src/protozero/message_unittest.cc
+++ b/src/protozero/message_unittest.cc
@@ -22,7 +22,7 @@
 #include <utility>
 #include <vector>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
 #include "src/base/test/utils.h"
 #include "src/protozero/test/fake_scattered_buffer.h"
diff --git a/src/protozero/proto_decoder.cc b/src/protozero/proto_decoder.cc
index 4eddec7..073fece 100644
--- a/src/protozero/proto_decoder.cc
+++ b/src/protozero/proto_decoder.cc
@@ -17,9 +17,10 @@
 #include "perfetto/protozero/proto_decoder.h"
 
 #include <string.h>
+#include <limits>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/proto_utils.h"
 
 namespace protozero {
diff --git a/src/protozero/proto_decoder_unittest.cc b/src/protozero/proto_decoder_unittest.cc
index 2f5ab53..cc98efa 100644
--- a/src/protozero/proto_decoder_unittest.cc
+++ b/src/protozero/proto_decoder_unittest.cc
@@ -16,9 +16,9 @@
 
 #include "perfetto/protozero/proto_decoder.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/utils.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/message.h"
 #include "perfetto/protozero/proto_utils.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
diff --git a/src/protozero/proto_utils_unittest.cc b/src/protozero/proto_utils_unittest.cc
index ad63258..bb1be9e 100644
--- a/src/protozero/proto_utils_unittest.cc
+++ b/src/protozero/proto_utils_unittest.cc
@@ -18,9 +18,9 @@
 
 #include <limits>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace protozero {
 namespace proto_utils {
diff --git a/src/protozero/scattered_stream_writer_unittest.cc b/src/protozero/scattered_stream_writer_unittest.cc
index 6a61d33..dca3c0e 100644
--- a/src/protozero/scattered_stream_writer_unittest.cc
+++ b/src/protozero/scattered_stream_writer_unittest.cc
@@ -20,7 +20,7 @@
 
 #include <memory>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
 #include "src/protozero/test/fake_scattered_buffer.h"
 
diff --git a/src/protozero/test/fake_scattered_buffer.cc b/src/protozero/test/fake_scattered_buffer.cc
index 3c48f48..626ab28 100644
--- a/src/protozero/test/fake_scattered_buffer.cc
+++ b/src/protozero/test/fake_scattered_buffer.cc
@@ -19,7 +19,7 @@
 #include <sstream>
 #include <utility>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace protozero {
 
diff --git a/src/protozero/test/protozero_conformance_unittest.cc b/src/protozero/test/protozero_conformance_unittest.cc
index 034487f..b92a875 100644
--- a/src/protozero/test/protozero_conformance_unittest.cc
+++ b/src/protozero/test/protozero_conformance_unittest.cc
@@ -18,7 +18,7 @@
 #include <memory>
 #include <vector>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/protozero/message_handle.h"
 #include "src/protozero/test/fake_scattered_buffer.h"
 
diff --git a/src/public/BUILD.gn b/src/public/BUILD.gn
deleted file mode 100644
index 012d447..0000000
--- a/src/public/BUILD.gn
+++ /dev/null
@@ -1,93 +0,0 @@
-# Copyright (C) 2019 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.gni")
-
-if (perfetto_build_with_ipc_layer) {
-  source_set("public") {
-    deps = [
-      ":common",
-      "../../gn:default_deps",
-      "../base",
-      "../tracing",
-    ]
-    public_deps = [
-      "../../include/perfetto/public",
-    ]
-    sources = [
-      "data_source.cc",
-      "internal/in_process_tracing_backend.cc",
-      "internal/in_process_tracing_backend.h",
-      "internal/tracing_muxer_impl.cc",
-      "platform.cc",
-      "tracing.cc",
-      "virtual_destructors.cc",
-    ]
-
-    # TODO there should be an option to compile this out. This pulls IPC
-    # dependencies and bloats binary size.
-    deps += [ "../tracing:ipc" ]
-    sources += [
-      "internal/system_tracing_backend.cc",
-      "internal/system_tracing_backend.h",
-    ]
-  }
-}
-
-# Separate target because the embedder might not want this (e.g. on Windows).
-if (is_linux || is_mac || is_android) {
-  source_set("platform_posix") {
-    deps = [
-      "../../gn:default_deps",
-      "../../include/perfetto/public",
-      "../base",
-    ]
-    sources = [
-      "platform_posix.cc",
-    ]
-  }
-}
-
-# Stuff that both ":public" and other perfetto code (e.g. src/tracing/core) need
-# to depend on. It cannot be in the public target otherwise there would be a
-# cyclic dependency because public itself needs to depend on tracing.
-source_set("common") {
-  deps = [
-    "../../gn:default_deps",
-    "../../include/perfetto/public",
-  ]
-  sources = [
-    "trace_writer_base.cc",
-  ]
-}
-
-if (!perfetto_build_with_embedder) {
-  source_set("integrationtests") {
-    testonly = true
-    deps = [
-      ":platform_posix",
-      ":public",
-      "../../gn:default_deps",
-      "../../gn:gtest_deps",
-      "../../protos/perfetto/trace:lite",
-      "../base",
-
-      # TODO(primiano): move DataSourceConfig to /public/ and remove this.
-      "../../include/perfetto/tracing/core",
-    ]
-    sources = [
-      "api_integrationtest.cc",
-    ]
-  }
-}
diff --git a/src/trace_processor/BUILD.gn b/src/trace_processor/BUILD.gn
index 6ef7285..e5d33e2 100644
--- a/src/trace_processor/BUILD.gn
+++ b/src/trace_processor/BUILD.gn
@@ -16,26 +16,29 @@
 import("../../gn/perfetto.gni")
 import("../../gn/wasm.gni")
 
-wasm_lib("trace_processor_wasm") {
-  name = "trace_processor"
-  sources = [
-    "wasm_bridge.cc",
-  ]
+if (perfetto_build_standalone) {
+  wasm_lib("trace_processor_wasm") {
+    name = "trace_processor"
+    sources = [
+      "wasm_bridge.cc",
+    ]
 
-  # All the deps below are implicitly xxx($wasm_toolchain) because of the
-  # outer "is_wasm" block.
-  deps = [
-    ":lib",
-    "../../buildtools:sqlite",
-    "../../gn:default_deps",
-    "../../include/perfetto/base",
-    "../../protos/perfetto/trace_processor:lite",
-  ]
+    # All the deps below are implicitly xxx($wasm_toolchain) because of the
+    # outer "is_wasm" block.
+    deps = [
+      ":lib",
+      "../../buildtools:sqlite",
+      "../../gn:default_deps",
+      "../../include/perfetto/ext/base",
+      "../../protos/perfetto/trace_processor:lite",
+    ]
+  }
 }
 
 # The library which eases processing of Perfetto traces by exposing reading
 # friendly APIs.
 static_library("trace_processor") {
+  complete_static_lib = true
   deps = [
     ":lib",
   ]
@@ -136,6 +139,9 @@
     "syscalls_aarch64.h",
     "syscalls_armeabi.h",
     "syscalls_x86_64.h",
+    "systrace_trace_parser.cc",
+    "systrace_trace_parser.h",
+    "systrace_utils.h",
     "table.cc",
     "table.h",
     "thread_table.cc",
@@ -158,9 +164,9 @@
   ]
 
   deps = [
-    "../../buildtools:sqlite",
+    ":sqlite",
     "../../gn:default_deps",
-    "../../include/perfetto/traced:sys_stats_counters",
+    "../../include/perfetto/ext/traced:sys_stats_counters",
     "../../protos/perfetto/common:zero",
     "../../protos/perfetto/config:zero",
     "../../protos/perfetto/metrics:zero",
@@ -197,7 +203,7 @@
   }
 }
 
-if (current_toolchain == host_toolchain) {
+if (!perfetto_build_with_embedder && current_toolchain == host_toolchain) {
   executable("trace_processor_shell_host") {
     testonly = true  # We need this for proto full.
     deps = [
@@ -212,12 +218,6 @@
         "../../buildtools:linenoise",
         "../../gn/standalone:gen_git_revision",
       ]
-
-      # Build the descriptors as the diff testing framework depends on this.
-      deps += [
-        "../../protos/perfetto/metrics:descriptor",
-        "../../protos/perfetto/trace:descriptor",
-      ]
     }
 
     sources = [
@@ -226,19 +226,22 @@
   }
 }
 
-copy("trace_processor_shell") {
-  testonly = true  # We need this for proto full.
-  host_out_dir_ = get_label_info(":trace_processor_shell_host($host_toolchain)",
-                                 "root_out_dir")
-  deps = [
-    ":trace_processor_shell_host($host_toolchain)",
-  ]
-  sources = [
-    "${host_out_dir_}/trace_processor_shell_host",
-  ]
-  outputs = [
-    "${root_out_dir}/trace_processor_shell",
-  ]
+if (!perfetto_build_with_embedder) {
+  copy("trace_processor_shell") {
+    testonly = true  # We need this for proto full.
+    host_out_dir_ =
+        get_label_info(":trace_processor_shell_host($host_toolchain)",
+                       "root_out_dir")
+    deps = [
+      ":trace_processor_shell_host($host_toolchain)",
+    ]
+    sources = [
+      "${host_out_dir_}/trace_processor_shell_host",
+    ]
+    outputs = [
+      "${root_out_dir}/trace_processor_shell",
+    ]
+  }
 }
 
 source_set("unittests") {
@@ -262,13 +265,14 @@
     "sqlite3_str_split_unittest.cc",
     "string_pool_unittest.cc",
     "syscall_tracker_unittest.cc",
+    "systrace_utils_unittest.cc",
     "thread_table_unittest.cc",
     "trace_processor_impl_unittest.cc",
     "trace_sorter_unittest.cc",
   ]
   deps = [
     ":lib",
-    "../../buildtools:sqlite",
+    ":sqlite",
     "../../gn:default_deps",
     "../../gn:gtest_deps",
     "../../protos/perfetto/common:zero",
@@ -292,6 +296,16 @@
   }
 }
 
+# Indirection target to allow modifying the path to the SQLite header.
+source_set("sqlite") {
+  sources = [
+    "sqlite.h",
+  ]
+  public_deps = [
+    "../../gn:sqlite",
+  ]
+}
+
 source_set("integrationtests") {
   testonly = true
   sources = [
@@ -299,7 +313,7 @@
   ]
   deps = [
     ":lib",
-    "../../buildtools:sqlite",
+    ":sqlite",
     "../../gn:default_deps",
     "../../gn:gtest_deps",
     "../../protos/perfetto/trace:lite",
@@ -319,9 +333,9 @@
   ]
   deps = [
     ":lib",
-    "../../../../gn:default_deps",
     "../../buildtools:sqlite",
     "../../gn:default_deps",
+    "../../gn:default_deps",
     "../../gn:gtest_deps",
     "../../protos/perfetto/trace:lite",
     "../../protos/perfetto/trace_processor:lite",
diff --git a/src/trace_processor/args_table_unittest.cc b/src/trace_processor/args_table_unittest.cc
index 2ec7ef9..9b46424 100644
--- a/src/trace_processor/args_table_unittest.cc
+++ b/src/trace_processor/args_table_unittest.cc
@@ -19,8 +19,8 @@
 #include "src/trace_processor/trace_processor_context.h"
 #include "src/trace_processor/trace_storage.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/chunked_trace_reader.h b/src/trace_processor/chunked_trace_reader.h
index bbf93a2..f09967d 100644
--- a/src/trace_processor/chunked_trace_reader.h
+++ b/src/trace_processor/chunked_trace_reader.h
@@ -23,6 +23,7 @@
 #include <memory>
 
 #include "perfetto/trace_processor/basic_types.h"
+#include "perfetto/trace_processor/status.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/clock_tracker.h b/src/trace_processor/clock_tracker.h
index 98104de..4867ea1 100644
--- a/src/trace_processor/clock_tracker.h
+++ b/src/trace_processor/clock_tracker.h
@@ -23,7 +23,7 @@
 #include <vector>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
+#include "perfetto/ext/base/optional.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/clock_tracker_unittest.cc b/src/trace_processor/clock_tracker_unittest.cc
index 848c0e2..a96d4e3 100644
--- a/src/trace_processor/clock_tracker_unittest.cc
+++ b/src/trace_processor/clock_tracker_unittest.cc
@@ -15,12 +15,12 @@
  */
 
 #include "src/trace_processor/clock_tracker.h"
-#include "perfetto/base/optional.h"
+#include "perfetto/ext/base/optional.h"
 #include "src/trace_processor/trace_processor_context.h"
 #include "src/trace_processor/trace_storage.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/event_tracker.cc b/src/trace_processor/event_tracker.cc
index 36faa5d..beedc5b 100644
--- a/src/trace_processor/event_tracker.cc
+++ b/src/trace_processor/event_tracker.cc
@@ -18,7 +18,7 @@
 
 #include <math.h>
 
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/trace_processor/args_tracker.h"
 #include "src/trace_processor/ftrace_descriptors.h"
 #include "src/trace_processor/ftrace_utils.h"
diff --git a/src/trace_processor/event_tracker.h b/src/trace_processor/event_tracker.h
index d8a765a..60fabcf 100644
--- a/src/trace_processor/event_tracker.h
+++ b/src/trace_processor/event_tracker.h
@@ -20,8 +20,8 @@
 #include <array>
 #include <limits>
 
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/trace_processor/trace_storage.h"
 
 namespace perfetto {
diff --git a/src/trace_processor/event_tracker_unittest.cc b/src/trace_processor/event_tracker_unittest.cc
index aa27a45..1773cdb 100644
--- a/src/trace_processor/event_tracker_unittest.cc
+++ b/src/trace_processor/event_tracker_unittest.cc
@@ -19,8 +19,8 @@
 #include "src/trace_processor/args_tracker.h"
 #include "src/trace_processor/process_tracker.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
@@ -57,7 +57,7 @@
   context.event_tracker->PushSchedSwitch(cpu, timestamp, pid_1, kCommProc2,
                                          prio, prev_state, pid_2, kCommProc1,
                                          prio);
-  ASSERT_EQ(timestamps.size(), 1);
+  ASSERT_EQ(timestamps.size(), 1u);
 
   context.event_tracker->PushSchedSwitch(cpu, timestamp + 1, pid_2, kCommProc1,
                                          prio, prev_state, pid_1, kCommProc2,
@@ -69,7 +69,7 @@
   ASSERT_STREQ(
       context.storage->GetString(context.storage->GetThread(1).name_id).c_str(),
       kCommProc1);
-  ASSERT_EQ(context.storage->slices().utids().front(), 1);
+  ASSERT_EQ(context.storage->slices().utids().front(), 1u);
   ASSERT_EQ(context.storage->slices().durations().front(), 1);
 }
 
@@ -85,7 +85,7 @@
   context.event_tracker->PushSchedSwitch(cpu, timestamp, /*tid=*/4, kCommProc2,
                                          prio, prev_state,
                                          /*tid=*/2, kCommProc1, prio);
-  ASSERT_EQ(timestamps.size(), 1);
+  ASSERT_EQ(timestamps.size(), 1u);
 
   context.event_tracker->PushSchedSwitch(cpu, timestamp + 1, /*tid=*/2,
                                          kCommProc1, prio, prev_state,
diff --git a/src/trace_processor/export_json_unittest.cc b/src/trace_processor/export_json_unittest.cc
index 2f6dde5..d2180c8 100644
--- a/src/trace_processor/export_json_unittest.cc
+++ b/src/trace_processor/export_json_unittest.cc
@@ -16,10 +16,10 @@
 
 #include "src/trace_processor/export_json.h"
 
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/temp_file.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include <json/reader.h>
 #include <json/value.h>
diff --git a/src/trace_processor/filtered_row_index.cc b/src/trace_processor/filtered_row_index.cc
index f0dbbdf..48c5d3c 100644
--- a/src/trace_processor/filtered_row_index.cc
+++ b/src/trace_processor/filtered_row_index.cc
@@ -16,6 +16,7 @@
 
 #include "src/trace_processor/filtered_row_index.h"
 
+#include <stddef.h>
 #include <numeric>
 
 namespace perfetto {
diff --git a/src/trace_processor/filtered_row_index_unittest.cc b/src/trace_processor/filtered_row_index_unittest.cc
index 2a32a3f..280b633 100644
--- a/src/trace_processor/filtered_row_index_unittest.cc
+++ b/src/trace_processor/filtered_row_index_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/trace_processor/filtered_row_index.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/ftrace_utils.cc b/src/trace_processor/ftrace_utils.cc
index d1c0575..c035b0f 100644
--- a/src/trace_processor/ftrace_utils.cc
+++ b/src/trace_processor/ftrace_utils.cc
@@ -20,7 +20,7 @@
 #include <algorithm>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_writer.h"
+#include "perfetto/ext/base/string_writer.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/ftrace_utils.h b/src/trace_processor/ftrace_utils.h
index 434c233..bb6ba4c 100644
--- a/src/trace_processor/ftrace_utils.h
+++ b/src/trace_processor/ftrace_utils.h
@@ -21,8 +21,8 @@
 #include <array>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/string_writer.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/string_writer.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/ftrace_utils_unittest.cc b/src/trace_processor/ftrace_utils_unittest.cc
index 5f03a65..c54d8bd 100644
--- a/src/trace_processor/ftrace_utils_unittest.cc
+++ b/src/trace_processor/ftrace_utils_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/trace_processor/ftrace_utils.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/fuchsia_trace_parser.cc b/src/trace_processor/fuchsia_trace_parser.cc
index 6599a73..e2f6be9 100644
--- a/src/trace_processor/fuchsia_trace_parser.cc
+++ b/src/trace_processor/fuchsia_trace_parser.cc
@@ -115,7 +115,10 @@
           UniqueTid utid =
               procs->UpdateThread(static_cast<uint32_t>(tinfo.tid),
                                   static_cast<uint32_t>(tinfo.pid));
-          slices->End(ts, utid, cat, name);
+          // TODO(b/131181693): |cat| and |name| are not passed here so that
+          // if two slices end at the same timestep, the slices get closed in
+          // the correct order regardless of which end event is processed first.
+          slices->End(ts, utid);
           break;
         }
         case kDurationComplete: {
diff --git a/src/trace_processor/fuchsia_trace_tokenizer.cc b/src/trace_processor/fuchsia_trace_tokenizer.cc
index db90da2..95c7b37 100644
--- a/src/trace_processor/fuchsia_trace_tokenizer.cc
+++ b/src/trace_processor/fuchsia_trace_tokenizer.cc
@@ -20,7 +20,7 @@
 #include <unordered_map>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 #include "src/trace_processor/ftrace_utils.h"
 #include "src/trace_processor/fuchsia_provider_view.h"
 #include "src/trace_processor/process_tracker.h"
diff --git a/src/trace_processor/fuchsia_trace_utils.h b/src/trace_processor/fuchsia_trace_utils.h
index 21d3a70..c1fb779 100644
--- a/src/trace_processor/fuchsia_trace_utils.h
+++ b/src/trace_processor/fuchsia_trace_utils.h
@@ -21,7 +21,7 @@
 #include <stdint.h>
 #include <functional>
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/heap_profile_tracker_unittest.cc b/src/trace_processor/heap_profile_tracker_unittest.cc
index 9a0b0b8..7f88359 100644
--- a/src/trace_processor/heap_profile_tracker_unittest.cc
+++ b/src/trace_processor/heap_profile_tracker_unittest.cc
@@ -18,8 +18,8 @@
 
 #include "src/trace_processor/trace_processor_context.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/json_trace_parser.cc b/src/trace_processor/json_trace_parser.cc
index 75533f7..cb4e2d5 100644
--- a/src/trace_processor/json_trace_parser.cc
+++ b/src/trace_processor/json_trace_parser.cc
@@ -25,8 +25,8 @@
 
 #include "perfetto/base/build_config.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/trace_processor/json_trace_utils.h"
 #include "src/trace_processor/process_tracker.h"
 #include "src/trace_processor/slice_tracker.h"
@@ -104,13 +104,15 @@
       break;
     }
     case 'M': {  // Metadata events (process and thread names).
-      if (strcmp(value["name"].asCString(), "thread_name") == 0) {
+      if (strcmp(value["name"].asCString(), "thread_name") == 0 &&
+          !value["args"]["name"].empty()) {
         const char* thread_name = value["args"]["name"].asCString();
         auto thread_name_id = context_->storage->InternString(thread_name);
         procs->UpdateThreadName(tid, thread_name_id);
         break;
       }
-      if (strcmp(value["name"].asCString(), "process_name") == 0) {
+      if (strcmp(value["name"].asCString(), "process_name") == 0 &&
+          !value["args"]["name"].empty()) {
         const char* proc_name = value["args"]["name"].asCString();
         procs->UpdateProcess(pid, base::nullopt, proc_name);
         break;
diff --git a/src/trace_processor/json_trace_tokenizer_unittest.cc b/src/trace_processor/json_trace_tokenizer_unittest.cc
index 9bfb87e..3a266ee 100644
--- a/src/trace_processor/json_trace_tokenizer_unittest.cc
+++ b/src/trace_processor/json_trace_tokenizer_unittest.cc
@@ -18,8 +18,8 @@
 
 #include <json/value.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/json_trace_utils.h b/src/trace_processor/json_trace_utils.h
index eef12f4..8055b02 100644
--- a/src/trace_processor/json_trace_utils.h
+++ b/src/trace_processor/json_trace_utils.h
@@ -19,7 +19,7 @@
 
 #include <stdint.h>
 
-#include "perfetto/base/optional.h"
+#include "perfetto/ext/base/optional.h"
 
 namespace Json {
 class Value;
diff --git a/src/trace_processor/json_trace_utils_unittest.cc b/src/trace_processor/json_trace_utils_unittest.cc
index 531d397..50b13b5 100644
--- a/src/trace_processor/json_trace_utils_unittest.cc
+++ b/src/trace_processor/json_trace_utils_unittest.cc
@@ -18,8 +18,8 @@
 
 #include <json/value.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/metadata_table_unittest.cc b/src/trace_processor/metadata_table_unittest.cc
index 0f629d4..49cb7e9 100644
--- a/src/trace_processor/metadata_table_unittest.cc
+++ b/src/trace_processor/metadata_table_unittest.cc
@@ -19,8 +19,8 @@
 #include "src/trace_processor/trace_processor_context.h"
 #include "src/trace_processor/trace_storage.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/metrics/BUILD.gn b/src/trace_processor/metrics/BUILD.gn
index 2961de9..9afeba9 100644
--- a/src/trace_processor/metrics/BUILD.gn
+++ b/src/trace_processor/metrics/BUILD.gn
@@ -22,6 +22,7 @@
   "android/android_startup_launches.sql",
   "android/android_task_state.sql",
   "android/android_startup.sql",
+  "android/android_startup_cpu.sql",
   "android/heap_profile.sql",
 ]
 
@@ -52,13 +53,14 @@
     "metrics.h",
   ]
   deps = [
-    "../../../buildtools:sqlite",
+    "../:sqlite",
     "../../../gn:default_deps",
     "../../../include/perfetto/trace_processor",
     "../../../protos/perfetto/common:zero",
     "../../../protos/perfetto/metrics:zero",
     "../../../protos/perfetto/metrics/android:zero",
     "../../../protos/perfetto/trace_processor:metrics_impl_zero",
+    "../../base",
     "../../protozero:protozero",
   ]
   public_deps = [
@@ -73,9 +75,9 @@
   ]
   deps = [
     ":lib",
-    "../../../buildtools:sqlite",
     "../../../gn:default_deps",
     "../../../gn:gtest_deps",
+    "../../../gn:sqlite",
     "../../../protos/perfetto/common:zero",
   ]
 }
diff --git a/src/trace_processor/metrics/android/android_startup.sql b/src/trace_processor/metrics/android/android_startup.sql
index cbb9609..1d4c915 100644
--- a/src/trace_processor/metrics/android/android_startup.sql
+++ b/src/trace_processor/metrics/android/android_startup.sql
@@ -17,6 +17,7 @@
 -- Create the base tables and views containing the launch spans.
 SELECT RUN_METRIC('android/android_startup_launches.sql');
 SELECT RUN_METRIC('android/android_task_state.sql');
+SELECT RUN_METRIC('android/android_startup_cpu.sql');
 
 -- Slices for forked processes. Never present in hot starts.
 -- Prefer this over process start_ts, since the process might have
@@ -59,6 +60,27 @@
 FROM main_thread_state
 GROUP BY 1, 2;
 
+-- Tracks all slices for the main process threads
+CREATE TABLE main_process_slices AS
+SELECT
+  launches.id AS launch_id,
+  slices.name AS name,
+  AndroidStartupMetric_Slice('dur_ns', SUM(slices.dur)) AS slice_proto
+FROM launches
+JOIN launch_processes ON (launches.id = launch_processes.launch_id)
+JOIN thread ON (launch_processes.upid = thread.upid)
+JOIN slices ON (
+  slices.utid = thread.utid
+  AND slices.ts BETWEEN launches.ts AND launches.ts + launches.dur)
+WHERE slices.name IN (
+  'ActivityThreadMain',
+  'bindApplication',
+  'activityStart',
+  'activityResume',
+  'Choreographer#doFrame',
+  'inflate')
+GROUP BY 1, 2;
+
 CREATE VIEW startup_view AS
 SELECT
   AndroidStartupMetric_Startup(
@@ -103,6 +125,44 @@
         SELECT COUNT(1) FROM process
         WHERE (process.name IS NULL OR process.name != launches.package)
         AND process.start_ts BETWEEN launches.ts AND launches.ts + launches.dur
+      ),
+      'time_activity_manager', AndroidStartupMetric_Slice(
+        'dur_ns', (
+          SELECT launching_events.ts - launches.ts FROM launching_events
+          WHERE launching_events.type = 'S'
+          AND launching_events.ts BETWEEN launches.ts AND launches.ts + launches.dur
+        )
+      ),
+      'time_activity_thread_main', (
+        SELECT slice_proto FROM main_process_slices
+        WHERE launch_id = launches.id AND name = 'ActivityThreadMain'
+      ),
+      'time_bind_application', (
+        SELECT slice_proto FROM main_process_slices
+        WHERE launch_id = launches.id AND name = 'bindApplication'
+      ),
+      'time_activity_start', (
+        SELECT slice_proto FROM main_process_slices
+        WHERE launch_id = launches.id AND name = 'activityStart'
+      ),
+      'time_activity_resume', (
+        SELECT slice_proto FROM main_process_slices
+        WHERE launch_id = launches.id AND name = 'activityResume'
+      ),
+      'time_choreographer', (
+        SELECT slice_proto FROM main_process_slices
+        WHERE launch_id = launches.id AND name = 'Choreographer#doFrame'
+      ),
+      'time_before_start_process', (
+        SELECT AndroidStartupMetric_Slice('dur_ns', ts - launches.ts)
+        FROM zygote_forks_by_id WHERE id = launches.id
+      ),
+      'time_during_start_process', (
+        SELECT AndroidStartupMetric_Slice('dur_ns', dur)
+        FROM zygote_forks_by_id WHERE id = launches.id
+      ),
+      'other_process_to_activity_cpu_ratio', (
+        SELECT cpu_ratio FROM launch_cpu WHERE launch_id = launches.id
       )
     )
   ) as startup
diff --git a/src/trace_processor/metrics/android/android_startup_cpu.sql b/src/trace_processor/metrics/android/android_startup_cpu.sql
new file mode 100644
index 0000000..f71282e
--- /dev/null
+++ b/src/trace_processor/metrics/android/android_startup_cpu.sql
@@ -0,0 +1,51 @@
+--
+-- Copyright 2019 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.
+--
+
+-- Sched view per process
+CREATE VIEW per_process_cpu AS
+SELECT process.upid AS upid, ts, dur
+FROM sched
+JOIN thread USING(utid)
+JOIN process USING(upid);
+
+-- CPU usage during the activity launch.
+CREATE TABLE launch_cpu_per_process_type AS
+SELECT
+  id AS launch_id,
+  per_process_cpu.upid IN (
+    SELECT upid FROM launch_processes AS lp WHERE lp.launch_id = launches.id
+  ) AS is_launch_process,
+  SUM(per_process_cpu.dur) AS dur
+FROM launches
+JOIN per_process_cpu ON (
+  per_process_cpu.ts BETWEEN launches.ts AND launches.ts + launches.dur)
+GROUP BY 1, 2;
+
+CREATE VIEW launch_cpu AS
+SELECT
+  launch_id,
+  other_process.dur AS other_process_dur,
+  launch_process.dur AS launch_process_dur,
+  1.0 * IFNULL(other_process.dur, 0) / launch_process.dur AS cpu_ratio
+FROM (
+  SELECT * FROM launch_cpu_per_process_type
+  WHERE is_launch_process = 1
+) AS launch_process
+LEFT JOIN (
+  SELECT * FROM launch_cpu_per_process_type
+  WHERE is_launch_process = 0
+) AS other_process
+USING (launch_id);
diff --git a/src/trace_processor/metrics/android/android_startup_launches.sql b/src/trace_processor/metrics/android/android_startup_launches.sql
index 4fd64cd..b3b84c5 100644
--- a/src/trace_processor/metrics/android/android_startup_launches.sql
+++ b/src/trace_processor/metrics/android/android_startup_launches.sql
@@ -34,10 +34,12 @@
   package_name,
   type
 FROM raw
-JOIN launching_events_helper USING(arg_set_id)
+CROSS JOIN launching_events_helper
 JOIN thread USING(utid)
 JOIN process USING(upid)
-WHERE raw.name = 'print' AND process.name = 'system_server';
+WHERE raw.arg_set_id = launching_events_helper.arg_set_id
+AND raw.name = 'print'
+AND process.name = 'system_server';
 
 -- Marks the beginning of the trace and is equivalent to when the statsd launch
 -- logging begins.
diff --git a/src/trace_processor/metrics/descriptors.cc b/src/trace_processor/metrics/descriptors.cc
index 6b1d393..0d0e816 100644
--- a/src/trace_processor/metrics/descriptors.cc
+++ b/src/trace_processor/metrics/descriptors.cc
@@ -15,6 +15,7 @@
  */
 
 #include "src/trace_processor/metrics/descriptors.h"
+#include "perfetto/ext/base/string_view.h"
 
 #include "perfetto/common/descriptor.pbzero.h"
 
@@ -32,8 +33,10 @@
           ? base::StringView(f_decoder.type_name()).ToStdString()
           : "";
   // TODO(lalitm): add support for enums here.
-  uint32_t type = f_decoder.has_type() ? static_cast<uint32_t>(f_decoder.type())
-                                       : FieldDescriptorProto::TYPE_MESSAGE;
+  uint32_t type =
+      f_decoder.has_type()
+          ? static_cast<uint32_t>(f_decoder.type())
+          : static_cast<uint32_t>(FieldDescriptorProto::TYPE_MESSAGE);
   return FieldDescriptor(
       base::StringView(f_decoder.name()).ToStdString(),
       static_cast<uint32_t>(f_decoder.number()), type, std::move(type_name),
diff --git a/src/trace_processor/metrics/descriptors.h b/src/trace_processor/metrics/descriptors.h
index 37ef287..d05cd59 100644
--- a/src/trace_processor/metrics/descriptors.h
+++ b/src/trace_processor/metrics/descriptors.h
@@ -21,8 +21,9 @@
 #include <string>
 #include <vector>
 
-#include "perfetto/base/optional.h"
+#include "perfetto/ext/base/optional.h"
 #include "perfetto/trace_processor/basic_types.h"
+#include "perfetto/trace_processor/status.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/metrics/metrics.cc b/src/trace_processor/metrics/metrics.cc
index 25f5683..e8b9927 100644
--- a/src/trace_processor/metrics/metrics.cc
+++ b/src/trace_processor/metrics/metrics.cc
@@ -20,7 +20,8 @@
 #include <unordered_map>
 #include <vector>
 
-#include "perfetto/base/string_utils.h"
+#include "perfetto/ext/base/string_utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "src/trace_processor/metrics/sql_metrics.h"
 
diff --git a/src/trace_processor/metrics/metrics.descriptor.h b/src/trace_processor/metrics/metrics.descriptor.h
index 3d3b904..34f0e3b 100644
--- a/src/trace_processor/metrics/metrics.descriptor.h
+++ b/src/trace_processor/metrics/metrics.descriptor.h
@@ -19,7 +19,7 @@
 
 namespace perfetto {
 
-constexpr std::array<uint8_t, 4134> kMetricsDescriptor{
+constexpr std::array<uint8_t, 5029> kMetricsDescriptor{
     {0x0a, 0xc6, 0x0f, 0x0a, 0x29, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74,
      0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x61, 0x6e,
      0x64, 0x72, 0x6f, 0x69, 0x64, 0x2f, 0x6d, 0x65, 0x6d, 0x5f, 0x6d, 0x65,
@@ -186,7 +186,7 @@
      0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a,
      0x06, 0x67, 0x72, 0x6f, 0x77, 0x74, 0x68, 0x18, 0x04, 0x20, 0x01, 0x28,
      0x01, 0x52, 0x06, 0x67, 0x72, 0x6f, 0x77, 0x74, 0x68, 0x42, 0x02, 0x48,
-     0x03, 0x0a, 0x91, 0x07, 0x0a, 0x2d, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74,
+     0x03, 0x0a, 0x90, 0x0e, 0x0a, 0x2d, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74,
      0x74, 0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x61,
      0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x2f, 0x73, 0x74, 0x61, 0x72, 0x74,
      0x75, 0x70, 0x5f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x70, 0x72,
@@ -210,7 +210,7 @@
      0x73, 0x6c, 0x65, 0x65, 0x70, 0x5f, 0x64, 0x75, 0x72, 0x5f, 0x6e, 0x73,
      0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x17, 0x69, 0x6e, 0x74, 0x65,
      0x72, 0x72, 0x75, 0x70, 0x74, 0x69, 0x62, 0x6c, 0x65, 0x53, 0x6c, 0x65,
-     0x65, 0x70, 0x44, 0x75, 0x72, 0x4e, 0x73, 0x22, 0xe7, 0x04, 0x0a, 0x14,
+     0x65, 0x70, 0x44, 0x75, 0x72, 0x4e, 0x73, 0x22, 0xe6, 0x0b, 0x0a, 0x14,
      0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74,
      0x75, 0x70, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x12, 0x47, 0x0a, 0x07,
      0x73, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x18, 0x01, 0x20, 0x03, 0x28,
@@ -218,153 +218,228 @@
      0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e, 0x64, 0x72,
      0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x4d, 0x65,
      0x74, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70,
-     0x52, 0x07, 0x73, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x1a, 0xc7, 0x01,
-     0x0a, 0x0c, 0x54, 0x6f, 0x46, 0x69, 0x72, 0x73, 0x74, 0x46, 0x72, 0x61,
-     0x6d, 0x65, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x75, 0x72, 0x5f, 0x6e, 0x73,
-     0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x64, 0x75, 0x72, 0x4e,
-     0x73, 0x12, 0x5d, 0x0a, 0x19, 0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x74, 0x68,
-     0x72, 0x65, 0x61, 0x64, 0x5f, 0x62, 0x79, 0x5f, 0x74, 0x61, 0x73, 0x6b,
-     0x5f, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b,
-     0x32, 0x23, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e,
-     0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x53,
-     0x74, 0x61, 0x74, 0x65, 0x42, 0x72, 0x65, 0x61, 0x6b, 0x64, 0x6f, 0x77,
-     0x6e, 0x52, 0x15, 0x6d, 0x61, 0x69, 0x6e, 0x54, 0x68, 0x72, 0x65, 0x61,
-     0x64, 0x42, 0x79, 0x54, 0x61, 0x73, 0x6b, 0x53, 0x74, 0x61, 0x74, 0x65,
-     0x12, 0x41, 0x0a, 0x1d, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x5f, 0x70, 0x72,
-     0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x73, 0x5f, 0x73, 0x70, 0x61, 0x77,
-     0x6e, 0x65, 0x64, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20,
-     0x01, 0x28, 0x0d, 0x52, 0x1a, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x50, 0x72,
-     0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x73, 0x53, 0x70, 0x61, 0x77, 0x6e,
-     0x65, 0x64, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x1a, 0xbb, 0x02, 0x0a, 0x07,
-     0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x12, 0x1d, 0x0a, 0x0a, 0x73,
-     0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20,
-     0x01, 0x28, 0x0d, 0x52, 0x09, 0x73, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70,
-     0x49, 0x64, 0x12, 0x21, 0x0a, 0x0c, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67,
-     0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
-     0x52, 0x0b, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x4e, 0x61, 0x6d,
-     0x65, 0x12, 0x21, 0x0a, 0x0c, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73,
-     0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52,
-     0x0b, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x4e, 0x61, 0x6d, 0x65,
-     0x12, 0x2c, 0x0a, 0x12, 0x7a, 0x79, 0x67, 0x6f, 0x74, 0x65, 0x5f, 0x6e,
-     0x65, 0x77, 0x5f, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x18, 0x04,
-     0x20, 0x01, 0x28, 0x08, 0x52, 0x10, 0x7a, 0x79, 0x67, 0x6f, 0x74, 0x65,
-     0x4e, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x12, 0x43,
-     0x0a, 0x1e, 0x61, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x5f, 0x68,
-     0x6f, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x5f, 0x70, 0x72, 0x6f, 0x63, 0x65,
-     0x73, 0x73, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x06, 0x20, 0x01,
-     0x28, 0x0d, 0x52, 0x1b, 0x61, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79,
-     0x48, 0x6f, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x50, 0x72, 0x6f, 0x63, 0x65,
-     0x73, 0x73, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x58, 0x0a, 0x0e, 0x74,
-     0x6f, 0x5f, 0x66, 0x69, 0x72, 0x73, 0x74, 0x5f, 0x66, 0x72, 0x61, 0x6d,
-     0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x32, 0x2e, 0x70, 0x65,
+     0x52, 0x07, 0x73, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x1a, 0x1e, 0x0a,
+     0x05, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x75,
+     0x72, 0x5f, 0x6e, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05,
+     0x64, 0x75, 0x72, 0x4e, 0x73, 0x1a, 0xa6, 0x08, 0x0a, 0x0c, 0x54, 0x6f,
+     0x46, 0x69, 0x72, 0x73, 0x74, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x12, 0x15,
+     0x0a, 0x06, 0x64, 0x75, 0x72, 0x5f, 0x6e, 0x73, 0x18, 0x01, 0x20, 0x01,
+     0x28, 0x03, 0x52, 0x05, 0x64, 0x75, 0x72, 0x4e, 0x73, 0x12, 0x5d, 0x0a,
+     0x19, 0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64,
+     0x5f, 0x62, 0x79, 0x5f, 0x74, 0x61, 0x73, 0x6b, 0x5f, 0x73, 0x74, 0x61,
+     0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x23, 0x2e, 0x70,
+     0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74,
+     0x6f, 0x73, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x53, 0x74, 0x61, 0x74, 0x65,
+     0x42, 0x72, 0x65, 0x61, 0x6b, 0x64, 0x6f, 0x77, 0x6e, 0x52, 0x15, 0x6d,
+     0x61, 0x69, 0x6e, 0x54, 0x68, 0x72, 0x65, 0x61, 0x64, 0x42, 0x79, 0x54,
+     0x61, 0x73, 0x6b, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x41, 0x0a, 0x1d,
+     0x6f, 0x74, 0x68, 0x65, 0x72, 0x5f, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73,
+     0x73, 0x65, 0x73, 0x5f, 0x73, 0x70, 0x61, 0x77, 0x6e, 0x65, 0x64, 0x5f,
+     0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52,
+     0x1a, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73,
+     0x73, 0x65, 0x73, 0x53, 0x70, 0x61, 0x77, 0x6e, 0x65, 0x64, 0x43, 0x6f,
+     0x75, 0x6e, 0x74, 0x12, 0x5f, 0x0a, 0x15, 0x74, 0x69, 0x6d, 0x65, 0x5f,
+     0x61, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x5f, 0x6d, 0x61, 0x6e,
+     0x61, 0x67, 0x65, 0x72, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x2b,
+     0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72,
+     0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64,
+     0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x4d, 0x65, 0x74, 0x72, 0x69,
+     0x63, 0x2e, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x52, 0x13, 0x74, 0x69, 0x6d,
+     0x65, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x4d, 0x61, 0x6e,
+     0x61, 0x67, 0x65, 0x72, 0x12, 0x66, 0x0a, 0x19, 0x74, 0x69, 0x6d, 0x65,
+     0x5f, 0x61, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x5f, 0x74, 0x68,
+     0x72, 0x65, 0x61, 0x64, 0x5f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x05, 0x20,
+     0x01, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74,
+     0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e,
+     0x64, 0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70,
+     0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x6c, 0x69, 0x63, 0x65,
+     0x52, 0x16, 0x74, 0x69, 0x6d, 0x65, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69,
+     0x74, 0x79, 0x54, 0x68, 0x72, 0x65, 0x61, 0x64, 0x4d, 0x61, 0x69, 0x6e,
+     0x12, 0x5f, 0x0a, 0x15, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x62, 0x69, 0x6e,
+     0x64, 0x5f, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
+     0x6e, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x70, 0x65,
      0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
      0x73, 0x2e, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61,
-     0x72, 0x74, 0x75, 0x70, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x54,
-     0x6f, 0x46, 0x69, 0x72, 0x73, 0x74, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x52,
-     0x0c, 0x74, 0x6f, 0x46, 0x69, 0x72, 0x73, 0x74, 0x46, 0x72, 0x61, 0x6d,
-     0x65, 0x42, 0x02, 0x48, 0x03, 0x0a, 0xaa, 0x04, 0x0a, 0x2b, 0x70, 0x65,
-     0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69,
-     0x63, 0x73, 0x2f, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x2f, 0x68,
-     0x65, 0x61, 0x70, 0x5f, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e,
-     0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0f, 0x70, 0x65, 0x72, 0x66, 0x65,
-     0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x22, 0xe5,
-     0x03, 0x0a, 0x0b, 0x48, 0x65, 0x61, 0x70, 0x50, 0x72, 0x6f, 0x66, 0x69,
-     0x6c, 0x65, 0x12, 0x41, 0x0a, 0x07, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c,
-     0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x27, 0x2e, 0x70, 0x65,
-     0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
-     0x73, 0x2e, 0x48, 0x65, 0x61, 0x70, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c,
-     0x65, 0x2e, 0x50, 0x65, 0x72, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73,
-     0x52, 0x07, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x1a, 0x1b, 0x0a,
-     0x05, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61,
-     0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61,
-     0x6d, 0x65, 0x1a, 0x44, 0x0a, 0x08, 0x43, 0x61, 0x6c, 0x6c, 0x73, 0x69,
-     0x74, 0x65, 0x12, 0x38, 0x0a, 0x05, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x18,
-     0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x70, 0x65, 0x72, 0x66,
-     0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e,
-     0x48, 0x65, 0x61, 0x70, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e,
-     0x46, 0x72, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x66, 0x72, 0x61, 0x6d, 0x65,
-     0x1a, 0xaa, 0x01, 0x0a, 0x0d, 0x43, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74,
-     0x65, 0x53, 0x74, 0x61, 0x74, 0x73, 0x12, 0x41, 0x0a, 0x08, 0x63, 0x61,
-     0x6c, 0x6c, 0x73, 0x69, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b,
-     0x32, 0x25, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e,
-     0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x48, 0x65, 0x61, 0x70, 0x50,
-     0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x43, 0x61, 0x6c, 0x6c, 0x73,
-     0x69, 0x74, 0x65, 0x52, 0x08, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74,
-     0x65, 0x12, 0x2a, 0x0a, 0x11, 0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x73, 0x5f,
-     0x73, 0x65, 0x6c, 0x66, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02,
-     0x20, 0x01, 0x28, 0x03, 0x52, 0x0f, 0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x73,
-     0x53, 0x65, 0x6c, 0x66, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x2a, 0x0a,
-     0x11, 0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x73, 0x5f, 0x73, 0x65, 0x6c, 0x66,
-     0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03,
-     0x52, 0x0f, 0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x73, 0x53, 0x65, 0x6c, 0x66,
-     0x42, 0x79, 0x74, 0x65, 0x73, 0x1a, 0x82, 0x01, 0x0a, 0x0a, 0x50, 0x65,
-     0x72, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x12, 0x21, 0x0a, 0x0c,
+     0x72, 0x74, 0x75, 0x70, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x53,
+     0x6c, 0x69, 0x63, 0x65, 0x52, 0x13, 0x74, 0x69, 0x6d, 0x65, 0x42, 0x69,
+     0x6e, 0x64, 0x41, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
+     0x6e, 0x12, 0x5b, 0x0a, 0x13, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x61, 0x63,
+     0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74,
+     0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x70, 0x65, 0x72,
+     0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73,
+     0x2e, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72,
+     0x74, 0x75, 0x70, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x6c,
+     0x69, 0x63, 0x65, 0x52, 0x11, 0x74, 0x69, 0x6d, 0x65, 0x41, 0x63, 0x74,
+     0x69, 0x76, 0x69, 0x74, 0x79, 0x53, 0x74, 0x61, 0x72, 0x74, 0x12, 0x5d,
+     0x0a, 0x14, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x61, 0x63, 0x74, 0x69, 0x76,
+     0x69, 0x74, 0x79, 0x5f, 0x72, 0x65, 0x73, 0x75, 0x6d, 0x65, 0x18, 0x08,
+     0x20, 0x01, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65,
+     0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41,
+     0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75,
+     0x70, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x6c, 0x69, 0x63,
+     0x65, 0x52, 0x12, 0x74, 0x69, 0x6d, 0x65, 0x41, 0x63, 0x74, 0x69, 0x76,
+     0x69, 0x74, 0x79, 0x52, 0x65, 0x73, 0x75, 0x6d, 0x65, 0x12, 0x5a, 0x0a,
+     0x12, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x63, 0x68, 0x6f, 0x72, 0x65, 0x6f,
+     0x67, 0x72, 0x61, 0x70, 0x68, 0x65, 0x72, 0x18, 0x09, 0x20, 0x01, 0x28,
+     0x0b, 0x32, 0x2b, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f,
+     0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e, 0x64, 0x72,
+     0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x4d, 0x65,
+     0x74, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x52, 0x11,
+     0x74, 0x69, 0x6d, 0x65, 0x43, 0x68, 0x6f, 0x72, 0x65, 0x6f, 0x67, 0x72,
+     0x61, 0x70, 0x68, 0x65, 0x72, 0x12, 0x66, 0x0a, 0x19, 0x74, 0x69, 0x6d,
+     0x65, 0x5f, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x5f, 0x73, 0x74, 0x61,
+     0x72, 0x74, 0x5f, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x18, 0x0a,
+     0x20, 0x01, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65,
+     0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41,
+     0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75,
+     0x70, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x6c, 0x69, 0x63,
+     0x65, 0x52, 0x16, 0x74, 0x69, 0x6d, 0x65, 0x42, 0x65, 0x66, 0x6f, 0x72,
+     0x65, 0x53, 0x74, 0x61, 0x72, 0x74, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73,
+     0x73, 0x12, 0x66, 0x0a, 0x19, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x64, 0x75,
+     0x72, 0x69, 0x6e, 0x67, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x70,
+     0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x0b,
+     0x32, 0x2b, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e,
+     0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e, 0x64, 0x72, 0x6f,
+     0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x4d, 0x65, 0x74,
+     0x72, 0x69, 0x63, 0x2e, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x52, 0x16, 0x74,
+     0x69, 0x6d, 0x65, 0x44, 0x75, 0x72, 0x69, 0x6e, 0x67, 0x53, 0x74, 0x61,
+     0x72, 0x74, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x12, 0x4b, 0x0a,
+     0x23, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x5f, 0x70, 0x72, 0x6f, 0x63, 0x65,
+     0x73, 0x73, 0x5f, 0x74, 0x6f, 0x5f, 0x61, 0x63, 0x74, 0x69, 0x76, 0x69,
+     0x74, 0x79, 0x5f, 0x63, 0x70, 0x75, 0x5f, 0x72, 0x61, 0x74, 0x69, 0x6f,
+     0x18, 0x0c, 0x20, 0x01, 0x28, 0x01, 0x52, 0x1e, 0x6f, 0x74, 0x68, 0x65,
+     0x72, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x41, 0x63,
+     0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x43, 0x70, 0x75, 0x52, 0x61, 0x74,
+     0x69, 0x6f, 0x1a, 0xbb, 0x02, 0x0a, 0x07, 0x53, 0x74, 0x61, 0x72, 0x74,
+     0x75, 0x70, 0x12, 0x1d, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x72, 0x74, 0x75,
+     0x70, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09,
+     0x73, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x49, 0x64, 0x12, 0x21, 0x0a,
+     0x0c, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x5f, 0x6e, 0x61, 0x6d,
+     0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x70, 0x61, 0x63,
+     0x6b, 0x61, 0x67, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x21, 0x0a, 0x0c,
      0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x5f, 0x6e, 0x61, 0x6d, 0x65,
-     0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x70, 0x72, 0x6f, 0x63,
-     0x65, 0x73, 0x73, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x51, 0x0a, 0x0e, 0x63,
-     0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74, 0x65, 0x5f, 0x73, 0x74, 0x61, 0x74,
-     0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2a, 0x2e, 0x70, 0x65,
+     0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x70, 0x72, 0x6f, 0x63,
+     0x65, 0x73, 0x73, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x2c, 0x0a, 0x12, 0x7a,
+     0x79, 0x67, 0x6f, 0x74, 0x65, 0x5f, 0x6e, 0x65, 0x77, 0x5f, 0x70, 0x72,
+     0x6f, 0x63, 0x65, 0x73, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52,
+     0x10, 0x7a, 0x79, 0x67, 0x6f, 0x74, 0x65, 0x4e, 0x65, 0x77, 0x50, 0x72,
+     0x6f, 0x63, 0x65, 0x73, 0x73, 0x12, 0x43, 0x0a, 0x1e, 0x61, 0x63, 0x74,
+     0x69, 0x76, 0x69, 0x74, 0x79, 0x5f, 0x68, 0x6f, 0x73, 0x74, 0x69, 0x6e,
+     0x67, 0x5f, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x5f, 0x63, 0x6f,
+     0x75, 0x6e, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x1b, 0x61,
+     0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x48, 0x6f, 0x73, 0x74, 0x69,
+     0x6e, 0x67, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x43, 0x6f, 0x75,
+     0x6e, 0x74, 0x12, 0x58, 0x0a, 0x0e, 0x74, 0x6f, 0x5f, 0x66, 0x69, 0x72,
+     0x73, 0x74, 0x5f, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01,
+     0x28, 0x0b, 0x32, 0x32, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74,
+     0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e, 0x64,
+     0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x4d,
+     0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x54, 0x6f, 0x46, 0x69, 0x72, 0x73,
+     0x74, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x52, 0x0c, 0x74, 0x6f, 0x46, 0x69,
+     0x72, 0x73, 0x74, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x42, 0x02, 0x48, 0x03,
+     0x0a, 0xaa, 0x04, 0x0a, 0x2b, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74,
+     0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x61, 0x6e,
+     0x64, 0x72, 0x6f, 0x69, 0x64, 0x2f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x70,
+     0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
+     0x12, 0x0f, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70,
+     0x72, 0x6f, 0x74, 0x6f, 0x73, 0x22, 0xe5, 0x03, 0x0a, 0x0b, 0x48, 0x65,
+     0x61, 0x70, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x12, 0x41, 0x0a,
+     0x07, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x03,
+     0x28, 0x0b, 0x32, 0x27, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74,
+     0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x48, 0x65, 0x61,
+     0x70, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x50, 0x65, 0x72,
+     0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x52, 0x07, 0x70, 0x72, 0x6f,
+     0x66, 0x69, 0x6c, 0x65, 0x1a, 0x1b, 0x0a, 0x05, 0x46, 0x72, 0x61, 0x6d,
+     0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20,
+     0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x1a, 0x44, 0x0a,
+     0x08, 0x43, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74, 0x65, 0x12, 0x38, 0x0a,
+     0x05, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b,
+     0x32, 0x22, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e,
+     0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x48, 0x65, 0x61, 0x70, 0x50,
+     0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x46, 0x72, 0x61, 0x6d, 0x65,
+     0x52, 0x05, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x1a, 0xaa, 0x01, 0x0a, 0x0d,
+     0x43, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74, 0x65, 0x53, 0x74, 0x61, 0x74,
+     0x73, 0x12, 0x41, 0x0a, 0x08, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74,
+     0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x70, 0x65,
      0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
      0x73, 0x2e, 0x48, 0x65, 0x61, 0x70, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c,
-     0x65, 0x2e, 0x43, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74, 0x65, 0x53, 0x74,
-     0x61, 0x74, 0x73, 0x52, 0x0d, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74,
-     0x65, 0x53, 0x74, 0x61, 0x74, 0x73, 0x42, 0x02, 0x48, 0x03, 0x0a, 0x99,
-     0x05, 0x0a, 0x1e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2f,
-     0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x6d, 0x65, 0x74, 0x72,
-     0x69, 0x63, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0f, 0x70,
-     0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74,
-     0x6f, 0x73, 0x1a, 0x29, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f,
-     0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x61, 0x6e, 0x64,
-     0x72, 0x6f, 0x69, 0x64, 0x2f, 0x6d, 0x65, 0x6d, 0x5f, 0x6d, 0x65, 0x74,
-     0x72, 0x69, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x2d, 0x70,
+     0x65, 0x2e, 0x43, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74, 0x65, 0x52, 0x08,
+     0x63, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74, 0x65, 0x12, 0x2a, 0x0a, 0x11,
+     0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x73, 0x5f, 0x73, 0x65, 0x6c, 0x66, 0x5f,
+     0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52,
+     0x0f, 0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x73, 0x53, 0x65, 0x6c, 0x66, 0x43,
+     0x6f, 0x75, 0x6e, 0x74, 0x12, 0x2a, 0x0a, 0x11, 0x61, 0x6c, 0x6c, 0x6f,
+     0x63, 0x73, 0x5f, 0x73, 0x65, 0x6c, 0x66, 0x5f, 0x62, 0x79, 0x74, 0x65,
+     0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0f, 0x61, 0x6c, 0x6c,
+     0x6f, 0x63, 0x73, 0x53, 0x65, 0x6c, 0x66, 0x42, 0x79, 0x74, 0x65, 0x73,
+     0x1a, 0x82, 0x01, 0x0a, 0x0a, 0x50, 0x65, 0x72, 0x50, 0x72, 0x6f, 0x63,
+     0x65, 0x73, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x70, 0x72, 0x6f, 0x63, 0x65,
+     0x73, 0x73, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
+     0x09, 0x52, 0x0b, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x4e, 0x61,
+     0x6d, 0x65, 0x12, 0x51, 0x0a, 0x0e, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x69,
+     0x74, 0x65, 0x5f, 0x73, 0x74, 0x61, 0x74, 0x73, 0x18, 0x02, 0x20, 0x03,
+     0x28, 0x0b, 0x32, 0x2a, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74,
+     0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x48, 0x65, 0x61,
+     0x70, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x43, 0x61, 0x6c,
+     0x6c, 0x73, 0x69, 0x74, 0x65, 0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x0d,
+     0x63, 0x61, 0x6c, 0x6c, 0x73, 0x69, 0x74, 0x65, 0x53, 0x74, 0x61, 0x74,
+     0x73, 0x42, 0x02, 0x48, 0x03, 0x0a, 0x99, 0x05, 0x0a, 0x1e, 0x70, 0x65,
+     0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69,
+     0x63, 0x73, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2e, 0x70,
+     0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0f, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74,
+     0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x1a, 0x29, 0x70,
      0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72,
      0x69, 0x63, 0x73, 0x2f, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x2f,
-     0x73, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x5f, 0x6d, 0x65, 0x74, 0x72,
-     0x69, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x2b, 0x70, 0x65,
-     0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69,
-     0x63, 0x73, 0x2f, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x2f, 0x68,
-     0x65, 0x61, 0x70, 0x5f, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e,
-     0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x94, 0x01, 0x0a, 0x0f, 0x54, 0x72,
-     0x61, 0x63, 0x65, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
-     0x73, 0x12, 0x3c, 0x0a, 0x05, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x18, 0x01,
-     0x20, 0x03, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65,
-     0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x54,
-     0x72, 0x61, 0x63, 0x65, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69,
-     0x63, 0x73, 0x2e, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x05, 0x65, 0x6e,
-     0x74, 0x72, 0x79, 0x1a, 0x43, 0x0a, 0x05, 0x45, 0x6e, 0x74, 0x72, 0x79,
-     0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01,
-     0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03,
-     0x69, 0x64, 0x78, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x69,
-     0x64, 0x78, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18,
-     0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65,
-     0x22, 0xc3, 0x02, 0x0a, 0x0c, 0x54, 0x72, 0x61, 0x63, 0x65, 0x4d, 0x65,
-     0x74, 0x72, 0x69, 0x63, 0x73, 0x12, 0x45, 0x0a, 0x0b, 0x61, 0x6e, 0x64,
-     0x72, 0x6f, 0x69, 0x64, 0x5f, 0x6d, 0x65, 0x6d, 0x18, 0x01, 0x20, 0x01,
-     0x28, 0x0b, 0x32, 0x24, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74,
-     0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e, 0x64,
-     0x72, 0x6f, 0x69, 0x64, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x4d, 0x65,
-     0x74, 0x72, 0x69, 0x63, 0x52, 0x0a, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69,
-     0x64, 0x4d, 0x65, 0x6d, 0x12, 0x4e, 0x0a, 0x0f, 0x61, 0x6e, 0x64, 0x72,
-     0x6f, 0x69, 0x64, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x18,
-     0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x70, 0x65, 0x72, 0x66,
-     0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e,
-     0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74,
-     0x75, 0x70, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x52, 0x0e, 0x61, 0x6e,
-     0x64, 0x72, 0x6f, 0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70,
-     0x12, 0x4b, 0x0a, 0x10, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x5f, 0x73, 0x74,
-     0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, 0x18, 0x03, 0x20, 0x01,
-     0x28, 0x0b, 0x32, 0x20, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74,
-     0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x54, 0x72, 0x61,
-     0x63, 0x65, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
-     0x52, 0x0f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x53, 0x74, 0x61, 0x74, 0x69,
-     0x73, 0x74, 0x69, 0x63, 0x73, 0x12, 0x3f, 0x0a, 0x0c, 0x68, 0x65, 0x61,
-     0x70, 0x5f, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x18, 0x04, 0x20,
-     0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74,
-     0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x48, 0x65,
-     0x61, 0x70, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x52, 0x0b, 0x68,
-     0x65, 0x61, 0x70, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2a, 0x06,
-     0x08, 0xc2, 0x03, 0x10, 0xf4, 0x03, 0x2a, 0x06, 0x08, 0xf4, 0x03, 0x10,
-     0xe9, 0x07, 0x42, 0x02, 0x48, 0x03}};
+     0x6d, 0x65, 0x6d, 0x5f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x70,
+     0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x2d, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74,
+     0x74, 0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x61,
+     0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x2f, 0x73, 0x74, 0x61, 0x72, 0x74,
+     0x75, 0x70, 0x5f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x2e, 0x70, 0x72,
+     0x6f, 0x74, 0x6f, 0x1a, 0x2b, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74,
+     0x6f, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x61, 0x6e,
+     0x64, 0x72, 0x6f, 0x69, 0x64, 0x2f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x70,
+     0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
+     0x22, 0x94, 0x01, 0x0a, 0x0f, 0x54, 0x72, 0x61, 0x63, 0x65, 0x53, 0x74,
+     0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, 0x12, 0x3c, 0x0a, 0x05,
+     0x65, 0x6e, 0x74, 0x72, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32,
+     0x26, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70,
+     0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x54, 0x72, 0x61, 0x63, 0x65, 0x53,
+     0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, 0x2e, 0x45, 0x6e,
+     0x74, 0x72, 0x79, 0x52, 0x05, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x1a, 0x43,
+     0x0a, 0x05, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x6e,
+     0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e,
+     0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x69, 0x64, 0x78, 0x18, 0x02,
+     0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x69, 0x64, 0x78, 0x12, 0x14, 0x0a,
+     0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03,
+     0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0xc3, 0x02, 0x0a, 0x0c,
+     0x54, 0x72, 0x61, 0x63, 0x65, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73,
+     0x12, 0x45, 0x0a, 0x0b, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x5f,
+     0x6d, 0x65, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x24, 0x2e,
+     0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f,
+     0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x4d,
+     0x65, 0x6d, 0x6f, 0x72, 0x79, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x52,
+     0x0a, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x4d, 0x65, 0x6d, 0x12,
+     0x4e, 0x0a, 0x0f, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x5f, 0x73,
+     0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b,
+     0x32, 0x25, 0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e,
+     0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x41, 0x6e, 0x64, 0x72, 0x6f,
+     0x69, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x4d, 0x65, 0x74,
+     0x72, 0x69, 0x63, 0x52, 0x0e, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64,
+     0x53, 0x74, 0x61, 0x72, 0x74, 0x75, 0x70, 0x12, 0x4b, 0x0a, 0x10, 0x65,
+     0x72, 0x72, 0x6f, 0x72, 0x5f, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74,
+     0x69, 0x63, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x20, 0x2e,
+     0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f,
+     0x74, 0x6f, 0x73, 0x2e, 0x54, 0x72, 0x61, 0x63, 0x65, 0x53, 0x74, 0x61,
+     0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, 0x52, 0x0f, 0x65, 0x72, 0x72,
+     0x6f, 0x72, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
+     0x12, 0x3f, 0x0a, 0x0c, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x70, 0x72, 0x6f,
+     0x66, 0x69, 0x6c, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c,
+     0x2e, 0x70, 0x65, 0x72, 0x66, 0x65, 0x74, 0x74, 0x6f, 0x2e, 0x70, 0x72,
+     0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x48, 0x65, 0x61, 0x70, 0x50, 0x72, 0x6f,
+     0x66, 0x69, 0x6c, 0x65, 0x52, 0x0b, 0x68, 0x65, 0x61, 0x70, 0x50, 0x72,
+     0x6f, 0x66, 0x69, 0x6c, 0x65, 0x2a, 0x06, 0x08, 0xc2, 0x03, 0x10, 0xf4,
+     0x03, 0x2a, 0x06, 0x08, 0xf4, 0x03, 0x10, 0xe9, 0x07, 0x42, 0x02, 0x48,
+     0x03}};
 
 }  // namespace perfetto
 
diff --git a/src/trace_processor/metrics/metrics.h b/src/trace_processor/metrics/metrics.h
index cf41db9..de9de6e 100644
--- a/src/trace_processor/metrics/metrics.h
+++ b/src/trace_processor/metrics/metrics.h
@@ -17,17 +17,17 @@
 #ifndef SRC_TRACE_PROCESSOR_METRICS_METRICS_H_
 #define SRC_TRACE_PROCESSOR_METRICS_METRICS_H_
 
-#include <sqlite3.h>
 #include <unordered_map>
 #include <vector>
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 #include "perfetto/protozero/field.h"
 #include "perfetto/protozero/message.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "perfetto/trace_processor/trace_processor.h"
 
 #include "src/trace_processor/metrics/descriptors.h"
+#include "src/trace_processor/sqlite.h"
 
 #include "perfetto/trace_processor/metrics_impl.pbzero.h"
 
diff --git a/src/trace_processor/metrics/metrics_unittest.cc b/src/trace_processor/metrics/metrics_unittest.cc
index 46898c36..5c766a5 100644
--- a/src/trace_processor/metrics/metrics_unittest.cc
+++ b/src/trace_processor/metrics/metrics_unittest.cc
@@ -18,7 +18,7 @@
 
 #include <vector>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 #include "perfetto/common/descriptor.pbzero.h"
 
diff --git a/src/trace_processor/null_term_string_view.h b/src/trace_processor/null_term_string_view.h
index 60a27f9..f576006 100644
--- a/src/trace_processor/null_term_string_view.h
+++ b/src/trace_processor/null_term_string_view.h
@@ -17,7 +17,7 @@
 #ifndef SRC_TRACE_PROCESSOR_NULL_TERM_STRING_VIEW_H_
 #define SRC_TRACE_PROCESSOR_NULL_TERM_STRING_VIEW_H_
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/null_term_string_view_unittest.cc b/src/trace_processor/null_term_string_view_unittest.cc
index 75977b0..7ea23d4 100644
--- a/src/trace_processor/null_term_string_view_unittest.cc
+++ b/src/trace_processor/null_term_string_view_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/trace_processor/null_term_string_view.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/process_table_unittest.cc b/src/trace_processor/process_table_unittest.cc
index 745bbd5..1144e30 100644
--- a/src/trace_processor/process_table_unittest.cc
+++ b/src/trace_processor/process_table_unittest.cc
@@ -20,8 +20,8 @@
 #include "src/trace_processor/scoped_db.h"
 #include "src/trace_processor/trace_processor_context.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/process_tracker.h b/src/trace_processor/process_tracker.h
index ee064c9..6834008 100644
--- a/src/trace_processor/process_tracker.h
+++ b/src/trace_processor/process_tracker.h
@@ -19,7 +19,7 @@
 
 #include <tuple>
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 #include "src/trace_processor/trace_processor_context.h"
 #include "src/trace_processor/trace_storage.h"
 
diff --git a/src/trace_processor/process_tracker_unittest.cc b/src/trace_processor/process_tracker_unittest.cc
index 0402b21..abd85f8 100644
--- a/src/trace_processor/process_tracker_unittest.cc
+++ b/src/trace_processor/process_tracker_unittest.cc
@@ -19,8 +19,8 @@
 #include "src/trace_processor/args_tracker.h"
 #include "src/trace_processor/event_tracker.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
@@ -47,7 +47,7 @@
   TraceStorage storage;
   context.process_tracker->UpdateProcess(1, base::nullopt, "test");
   auto pair_it = context.process_tracker->UpidsForPid(1);
-  ASSERT_EQ(pair_it.first->second, 1);
+  ASSERT_EQ(pair_it.first->second, 1u);
 }
 
 TEST_F(ProcessTrackerTest, GetOrCreateNewProcess) {
@@ -67,7 +67,7 @@
   context.process_tracker->UpdateProcess(1, base::nullopt, "test");
   context.process_tracker->UpdateProcess(1, base::nullopt, "test");
   auto pair_it = context.process_tracker->UpidsForPid(1);
-  ASSERT_EQ(pair_it.first->second, 1);
+  ASSERT_EQ(pair_it.first->second, 1u);
   ASSERT_EQ(++pair_it.first, pair_it.second);
 }
 
@@ -75,9 +75,9 @@
   context.process_tracker->UpdateProcess(1, base::nullopt, "test");
   context.process_tracker->UpdateProcess(3, base::nullopt, "test");
   auto pair_it = context.process_tracker->UpidsForPid(1);
-  ASSERT_EQ(pair_it.first->second, 1);
+  ASSERT_EQ(pair_it.first->second, 1u);
   auto second_pair_it = context.process_tracker->UpidsForPid(3);
-  ASSERT_EQ(second_pair_it.first->second, 2);
+  ASSERT_EQ(second_pair_it.first->second, 2u);
 }
 
 TEST_F(ProcessTrackerTest, AddProcessEntry_CorrectName) {
@@ -107,9 +107,9 @@
   TraceStorage::Thread thread = context.storage->GetThread(/*utid=*/1);
   TraceStorage::Process process = context.storage->GetProcess(/*utid=*/1);
 
-  ASSERT_EQ(thread.tid, 4);
-  ASSERT_EQ(thread.upid.value(), 1);
-  ASSERT_EQ(process.pid, 2);
+  ASSERT_EQ(thread.tid, 4u);
+  ASSERT_EQ(thread.upid.value(), 1u);
+  ASSERT_EQ(process.pid, 2u);
   ASSERT_EQ(process.start_ns, 0);
 }
 
@@ -119,14 +119,14 @@
   TraceStorage::Thread thread = context.storage->GetThread(1);
 
   // We expect 3 threads: Invalid thread, main thread for pid, tid 12.
-  ASSERT_EQ(context.storage->thread_count(), 3);
+  ASSERT_EQ(context.storage->thread_count(), 3u);
 
   auto tid_it = context.process_tracker->UtidsForTid(12);
   ASSERT_NE(tid_it.first, tid_it.second);
-  ASSERT_EQ(thread.upid.value(), 1);
+  ASSERT_EQ(thread.upid.value(), 1u);
   auto pid_it = context.process_tracker->UpidsForPid(2);
   ASSERT_NE(pid_it.first, pid_it.second);
-  ASSERT_EQ(context.storage->process_count(), 2);
+  ASSERT_EQ(context.storage->process_count(), 2u);
 }
 
 }  // namespace
diff --git a/src/trace_processor/proto_incremental_state.h b/src/trace_processor/proto_incremental_state.h
index e407985..38f4abd 100644
--- a/src/trace_processor/proto_incremental_state.h
+++ b/src/trace_processor/proto_incremental_state.h
@@ -22,7 +22,7 @@
 #include <map>
 #include <unordered_map>
 
-#include "perfetto/base/optional.h"
+#include "perfetto/ext/base/optional.h"
 #include "perfetto/protozero/proto_decoder.h"
 #include "src/trace_processor/trace_blob_view.h"
 #include "src/trace_processor/trace_storage.h"
diff --git a/src/trace_processor/proto_trace_parser.cc b/src/trace_processor/proto_trace_parser.cc
index c3f39d4..ae94745 100644
--- a/src/trace_processor/proto_trace_parser.cc
+++ b/src/trace_processor/proto_trace_parser.cc
@@ -22,11 +22,11 @@
 #include <string>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/traced/sys_stats_counters.h"
 #include "perfetto/protozero/proto_decoder.h"
-#include "perfetto/traced/sys_stats_counters.h"
 #include "src/trace_processor/args_tracker.h"
 #include "src/trace_processor/clock_tracker.h"
 #include "src/trace_processor/event_tracker.h"
@@ -36,6 +36,7 @@
 #include "src/trace_processor/process_tracker.h"
 #include "src/trace_processor/slice_tracker.h"
 #include "src/trace_processor/syscall_tracker.h"
+#include "src/trace_processor/systrace_utils.h"
 #include "src/trace_processor/trace_processor_context.h"
 #include "src/trace_processor/variadic.h"
 
@@ -80,83 +81,6 @@
 
 }  // namespace
 
-// We have to handle trace_marker events of a few different types:
-// 1. some random text
-// 2. B|1636|pokeUserActivity
-// 3. E|1636
-// 4. C|1636|wq:monitor|0
-SystraceParseResult ParseSystraceTracePoint(base::StringView str,
-                                            SystraceTracePoint* out) {
-  // THIS char* IS NOT NULL TERMINATED.
-  const char* s = str.data();
-  size_t len = str.size();
-
-  if (len < 2)
-    return SystraceParseResult::kFailure;
-
-  // If str matches '[BEC]\|[0-9]+[\|\n]' set tgid_length to the length of
-  // the number. Otherwise return kFailure.
-  if (s[1] != '|' && s[1] != '\n')
-    return SystraceParseResult::kFailure;
-  if (s[0] != 'B' && s[0] != 'E' && s[0] != 'C') {
-    // TODO: support android async slices
-    return s[0] == 'S' || s[0] == 'F' ? SystraceParseResult::kUnsupported
-                                      : SystraceParseResult::kFailure;
-  }
-  size_t tgid_length = 0;
-  for (size_t i = 2; i < len; i++) {
-    if (s[i] == '|' || s[i] == '\n') {
-      tgid_length = i - 2;
-      break;
-    }
-    if (s[i] < '0' || s[i] > '9')
-      return SystraceParseResult::kFailure;
-  }
-
-  if (tgid_length == 0) {
-    out->tgid = 0;
-  } else {
-    std::string tgid_str(s + 2, tgid_length);
-    out->tgid = static_cast<uint32_t>(std::stoi(tgid_str.c_str()));
-  }
-
-  out->phase = s[0];
-  switch (s[0]) {
-    case 'B': {
-      size_t name_index = 2 + tgid_length + 1;
-      out->name = base::StringView(
-          s + name_index, len - name_index - (s[len - 1] == '\n' ? 1 : 0));
-      return SystraceParseResult::kSuccess;
-    }
-    case 'E': {
-      return SystraceParseResult::kSuccess;
-    }
-    case 'C': {
-      size_t name_index = 2 + tgid_length + 1;
-      base::Optional<size_t> name_length;
-      for (size_t i = name_index; i < len; i++) {
-        if (s[i] == '|') {
-          name_length = i - name_index;
-          break;
-        }
-      }
-      if (!name_length.has_value())
-        return SystraceParseResult::kFailure;
-      out->name = base::StringView(s + name_index, name_length.value());
-
-      size_t value_index = name_index + name_length.value() + 1;
-      size_t value_len = len - value_index;
-      if (value_len == 0)
-        return SystraceParseResult::kFailure;
-      std::string value_str(s + value_index, value_len);
-      out->value = std::stod(value_str.c_str());
-      return SystraceParseResult::kSuccess;
-    }
-    default:
-      return SystraceParseResult::kFailure;
-  }
-}
-
 ProtoTraceParser::ProtoTraceParser(TraceProcessorContext* context)
     : context_(context),
       utid_name_id_(context->storage->InternString("utid")),
@@ -799,10 +723,10 @@
                                   uint32_t pid,
                                   ConstBytes blob) {
   protos::pbzero::PrintFtraceEvent::Decoder evt(blob.data, blob.size);
-  SystraceTracePoint point{};
+  systrace_utils::SystraceTracePoint point{};
   auto r = ParseSystraceTracePoint(evt.buf(), &point);
-  if (r != SystraceParseResult::kSuccess) {
-    if (r == SystraceParseResult::kFailure) {
+  if (r != systrace_utils::SystraceParseResult::kSuccess) {
+    if (r == systrace_utils::SystraceParseResult::kFailure) {
       context_->storage->IncrementStats(stats::systrace_parse_failure);
     }
     return;
diff --git a/src/trace_processor/proto_trace_parser.h b/src/trace_processor/proto_trace_parser.h
index 3d5251f..a66573d 100644
--- a/src/trace_processor/proto_trace_parser.h
+++ b/src/trace_processor/proto_trace_parser.h
@@ -22,7 +22,7 @@
 #include <array>
 #include <memory>
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 #include "perfetto/protozero/field.h"
 #include "src/trace_processor/ftrace_descriptors.h"
 #include "src/trace_processor/proto_incremental_state.h"
@@ -35,28 +35,6 @@
 
 class TraceProcessorContext;
 
-struct SystraceTracePoint {
-  char phase;
-  uint32_t tgid;
-
-  // For phase = 'B' and phase = 'C' only.
-  base::StringView name;
-
-  // For phase = 'C' only.
-  double value;
-};
-
-inline bool operator==(const SystraceTracePoint& x,
-                       const SystraceTracePoint& y) {
-  return std::tie(x.phase, x.tgid, x.name, x.value) ==
-         std::tie(y.phase, y.tgid, y.name, y.value);
-}
-
-enum class SystraceParseResult { kFailure = 0, kUnsupported, kSuccess };
-
-SystraceParseResult ParseSystraceTracePoint(base::StringView,
-                                            SystraceTracePoint* out);
-
 class ProtoTraceParser : public TraceParser {
  public:
   using ConstBytes = protozero::ConstBytes;
diff --git a/src/trace_processor/proto_trace_parser_unittest.cc b/src/trace_processor/proto_trace_parser_unittest.cc
index 61785e7..6107f4f 100644
--- a/src/trace_processor/proto_trace_parser_unittest.cc
+++ b/src/trace_processor/proto_trace_parser_unittest.cc
@@ -16,9 +16,9 @@
 
 #include "src/trace_processor/proto_trace_tokenizer.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/string_view.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/string_view.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "src/trace_processor/args_tracker.h"
 #include "src/trace_processor/event_tracker.h"
@@ -257,15 +257,15 @@
 
   Tokenize();
   const auto& raw = context_.storage->raw_events();
-  ASSERT_EQ(raw.raw_event_count(), 2);
+  ASSERT_EQ(raw.raw_event_count(), 2u);
   const auto& args = context_.storage->args();
-  ASSERT_EQ(args.args_count(), 6);
+  ASSERT_EQ(args.args_count(), 6u);
   ASSERT_EQ(args.arg_values()[0].int_value, 123);
-  ASSERT_EQ(args.arg_values()[1].string_value, 0);
+  ASSERT_EQ(args.arg_values()[1].string_value, 0u);
   ASSERT_EQ(args.arg_values()[2].int_value, 12);
   ASSERT_EQ(args.arg_values()[3].int_value, 15);
   ASSERT_EQ(args.arg_values()[4].int_value, 20);
-  ASSERT_EQ(args.arg_values()[5].string_value, 0);
+  ASSERT_EQ(args.arg_values()[5].string_value, 0u);
 
   // TODO(taylori): Add test ftrace event with all field types
   // and test here.
@@ -308,9 +308,9 @@
 
   const auto& raw = storage_->raw_events();
 
-  ASSERT_EQ(raw.raw_event_count(), 1);
+  ASSERT_EQ(raw.raw_event_count(), 1u);
   ASSERT_EQ(raw.timestamps().back(), 100);
-  ASSERT_EQ(storage_->GetThread(raw.utids().back()).tid, 10);
+  ASSERT_EQ(storage_->GetThread(raw.utids().back()).tid, 10u);
 
   auto set_id = raw.arg_set_ids().back();
 
@@ -976,30 +976,6 @@
   context_.sorter->ExtractEventsForced();
 }
 
-TEST(SystraceParserTest, SystraceEvent) {
-  SystraceTracePoint result{};
-
-  ASSERT_EQ(ParseSystraceTracePoint(base::StringView(""), &result),
-            SystraceParseResult::kFailure);
-
-  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("B|1|foo"), &result),
-            SystraceParseResult::kSuccess);
-  EXPECT_EQ(result, (SystraceTracePoint{'B', 1, base::StringView("foo"), 0}));
-
-  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("B|42|Bar"), &result),
-            SystraceParseResult::kSuccess);
-  EXPECT_EQ(result, (SystraceTracePoint{'B', 42, base::StringView("Bar"), 0}));
-
-  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("C|543|foo|"), &result),
-            SystraceParseResult::kFailure);
-  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("C|543|foo|8"), &result),
-            SystraceParseResult::kSuccess);
-  EXPECT_EQ(result, (SystraceTracePoint{'C', 543, base::StringView("foo"), 8}));
-
-  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("S|"), &result),
-            SystraceParseResult::kUnsupported);
-}
-
 TEST_F(ProtoTraceParserTest, LoadChromeBenchmarkMetadata) {
   static const char kName[] = "name";
   static const char kTag2[] = "tag1";
diff --git a/src/trace_processor/proto_trace_tokenizer.cc b/src/trace_processor/proto_trace_tokenizer.cc
index 144fa87..d62993c 100644
--- a/src/trace_processor/proto_trace_tokenizer.cc
+++ b/src/trace_processor/proto_trace_tokenizer.cc
@@ -19,7 +19,7 @@
 #include <string>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/proto_decoder.h"
 #include "perfetto/protozero/proto_utils.h"
 #include "src/trace_processor/event_tracker.h"
diff --git a/src/trace_processor/query_constraints.cc b/src/trace_processor/query_constraints.cc
index 9092cb7..b7e4d4d 100644
--- a/src/trace_processor/query_constraints.cc
+++ b/src/trace_processor/query_constraints.cc
@@ -16,10 +16,10 @@
 
 #include "src/trace_processor/query_constraints.h"
 
-#include <sqlite3.h>
 #include <string>
 
-#include "perfetto/base/string_splitter.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "src/trace_processor/sqlite.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/query_constraints.h b/src/trace_processor/query_constraints.h
index e9dc48b..fae2210 100644
--- a/src/trace_processor/query_constraints.h
+++ b/src/trace_processor/query_constraints.h
@@ -17,11 +17,10 @@
 #ifndef SRC_TRACE_PROCESSOR_QUERY_CONSTRAINTS_H_
 #define SRC_TRACE_PROCESSOR_QUERY_CONSTRAINTS_H_
 
-#include <sqlite3.h>
-
 #include <vector>
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "src/trace_processor/sqlite.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/query_constraints_unittest.cc b/src/trace_processor/query_constraints_unittest.cc
index 0435087..a2f3678 100644
--- a/src/trace_processor/query_constraints_unittest.cc
+++ b/src/trace_processor/query_constraints_unittest.cc
@@ -15,8 +15,8 @@
  */
 
 #include "src/trace_processor/query_constraints.h"
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
 
 using testing::ElementsAreArray;
@@ -58,8 +58,8 @@
 
   QueryConstraints qc_result =
       QueryConstraints::FromString(string_result.get());
-  ASSERT_EQ(qc_result.constraints().size(), 0);
-  ASSERT_EQ(qc_result.order_by().size(), 0);
+  ASSERT_EQ(qc_result.constraints().size(), 0u);
+  ASSERT_EQ(qc_result.order_by().size(), 0u);
 }
 
 TEST(QueryConstraintsTest, OnlyOrderBy) {
diff --git a/src/trace_processor/sched_slice_table_unittest.cc b/src/trace_processor/sched_slice_table_unittest.cc
index 438e869..df9a12b 100644
--- a/src/trace_processor/sched_slice_table_unittest.cc
+++ b/src/trace_processor/sched_slice_table_unittest.cc
@@ -22,8 +22,8 @@
 #include "src/trace_processor/scoped_db.h"
 #include "src/trace_processor/trace_processor_context.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/scoped_db.h b/src/trace_processor/scoped_db.h
index 7c1e44f..8d92756 100644
--- a/src/trace_processor/scoped_db.h
+++ b/src/trace_processor/scoped_db.h
@@ -17,13 +17,14 @@
 #ifndef SRC_TRACE_PROCESSOR_SCOPED_DB_H_
 #define SRC_TRACE_PROCESSOR_SCOPED_DB_H_
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "src/trace_processor/sqlite.h"
 
 extern "C" {
 struct sqlite3;
 struct sqlite3_stmt;
-extern int sqlite3_close(sqlite3*);
-extern int sqlite3_finalize(sqlite3_stmt* pStmt);
+SQLITE_API extern int sqlite3_close(sqlite3*);
+SQLITE_API extern int sqlite3_finalize(sqlite3_stmt* pStmt);
 }
 
 namespace perfetto {
diff --git a/src/trace_processor/slice_tracker.cc b/src/trace_processor/slice_tracker.cc
index 56c5a78..281b279 100644
--- a/src/trace_processor/slice_tracker.cc
+++ b/src/trace_processor/slice_tracker.cc
@@ -30,7 +30,7 @@
 // Slices which have been opened but haven't been closed yet will be marked
 // with this duration placeholder.
 constexpr int64_t kPendingDuration = -1;
-};  // namespace
+}  // namespace
 
 SliceTracker::SliceTracker(TraceProcessorContext* context)
     : context_(context) {}
diff --git a/src/trace_processor/slice_tracker_unittest.cc b/src/trace_processor/slice_tracker_unittest.cc
index 0a8eee2..88b1e1d 100644
--- a/src/trace_processor/slice_tracker_unittest.cc
+++ b/src/trace_processor/slice_tracker_unittest.cc
@@ -21,8 +21,8 @@
 #include "src/trace_processor/trace_processor_context.h"
 #include "src/trace_processor/trace_storage.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
@@ -61,11 +61,11 @@
   tracker.End(10 /*ts*/, 42 /*tid*/, 0 /*cat*/, 1 /*name*/);
 
   auto slices = context.storage->nestable_slices();
-  EXPECT_EQ(slices.slice_count(), 1);
+  EXPECT_EQ(slices.slice_count(), 1u);
   EXPECT_EQ(slices.start_ns()[0], 2);
   EXPECT_EQ(slices.durations()[0], 8);
-  EXPECT_EQ(slices.cats()[0], 0);
-  EXPECT_EQ(slices.names()[0], 1);
+  EXPECT_EQ(slices.cats()[0], 0u);
+  EXPECT_EQ(slices.names()[0], 1u);
   EXPECT_EQ(slices.refs()[0], 42);
   EXPECT_EQ(slices.types()[0], kRefUtid);
   EXPECT_EQ(slices.depths()[0], 0);
@@ -89,11 +89,11 @@
               });
 
   auto slices = context.storage->nestable_slices();
-  EXPECT_EQ(slices.slice_count(), 1);
+  EXPECT_EQ(slices.slice_count(), 1u);
   EXPECT_EQ(slices.start_ns()[0], 2);
   EXPECT_EQ(slices.durations()[0], 8);
-  EXPECT_EQ(slices.cats()[0], 0);
-  EXPECT_EQ(slices.names()[0], 1);
+  EXPECT_EQ(slices.cats()[0], 0u);
+  EXPECT_EQ(slices.names()[0], 1u);
   EXPECT_EQ(slices.refs()[0], 42);
   EXPECT_EQ(slices.types()[0], kRefUtid);
   EXPECT_EQ(slices.depths()[0], 0);
@@ -101,12 +101,12 @@
 
   auto args = context.storage->args();
   EXPECT_EQ(args.set_ids()[0], set_id);
-  EXPECT_EQ(args.flat_keys()[0], 1);
-  EXPECT_EQ(args.keys()[0], 2);
+  EXPECT_EQ(args.flat_keys()[0], 1u);
+  EXPECT_EQ(args.keys()[0], 2u);
   EXPECT_EQ(args.arg_values()[0], Variadic::Integer(10));
   EXPECT_EQ(args.set_ids()[1], set_id);
-  EXPECT_EQ(args.flat_keys()[1], 3);
-  EXPECT_EQ(args.keys()[1], 4);
+  EXPECT_EQ(args.flat_keys()[1], 3u);
+  EXPECT_EQ(args.keys()[1], 4u);
   EXPECT_EQ(args.arg_values()[1], Variadic::Integer(20));
 }
 
@@ -122,21 +122,21 @@
 
   auto slices = context.storage->nestable_slices();
 
-  EXPECT_EQ(slices.slice_count(), 2);
+  EXPECT_EQ(slices.slice_count(), 2u);
 
   size_t idx = 0;
   EXPECT_EQ(slices.start_ns()[idx], 2);
   EXPECT_EQ(slices.durations()[idx], 8);
-  EXPECT_EQ(slices.cats()[idx], 0);
-  EXPECT_EQ(slices.names()[idx], 1);
+  EXPECT_EQ(slices.cats()[idx], 0u);
+  EXPECT_EQ(slices.names()[idx], 1u);
   EXPECT_EQ(slices.refs()[idx], 42);
   EXPECT_EQ(slices.types()[idx], kRefUtid);
   EXPECT_EQ(slices.depths()[idx++], 0);
 
   EXPECT_EQ(slices.start_ns()[idx], 3);
   EXPECT_EQ(slices.durations()[idx], 2);
-  EXPECT_EQ(slices.cats()[idx], 0);
-  EXPECT_EQ(slices.names()[idx], 2);
+  EXPECT_EQ(slices.cats()[idx], 0u);
+  EXPECT_EQ(slices.names()[idx], 2u);
   EXPECT_EQ(slices.refs()[idx], 42);
   EXPECT_EQ(slices.types()[idx], kRefUtid);
   EXPECT_EQ(slices.depths()[idx], 1);
diff --git a/src/trace_processor/span_join_operator_table.cc b/src/trace_processor/span_join_operator_table.cc
index c5a7973..a1475f1 100644
--- a/src/trace_processor/span_join_operator_table.cc
+++ b/src/trace_processor/span_join_operator_table.cc
@@ -16,16 +16,16 @@
 
 #include "src/trace_processor/span_join_operator_table.h"
 
-#include <sqlite3.h>
 #include <string.h>
 #include <algorithm>
 #include <set>
 #include <utility>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/string_utils.h"
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/string_utils.h"
+#include "perfetto/ext/base/string_view.h"
+#include "src/trace_processor/sqlite.h"
 #include "src/trace_processor/sqlite_utils.h"
 
 namespace perfetto {
@@ -710,7 +710,7 @@
   if (!splitter.Next())
     return util::OkStatus();
 
-  if (strcasecmp(splitter.cur_token(), "PARTITIONED") != 0)
+  if (!base::CaseInsensitiveEqual(splitter.cur_token(), "PARTITIONED"))
     return util::ErrStatus("SPAN_JOIN: Invalid token");
 
   if (!splitter.Next())
diff --git a/src/trace_processor/span_join_operator_table.h b/src/trace_processor/span_join_operator_table.h
index 27cc5db..6228ad3 100644
--- a/src/trace_processor/span_join_operator_table.h
+++ b/src/trace_processor/span_join_operator_table.h
@@ -17,7 +17,6 @@
 #ifndef SRC_TRACE_PROCESSOR_SPAN_JOIN_OPERATOR_TABLE_H_
 #define SRC_TRACE_PROCESSOR_SPAN_JOIN_OPERATOR_TABLE_H_
 
-#include <sqlite3.h>
 #include <array>
 #include <deque>
 #include <limits>
@@ -28,7 +27,9 @@
 #include <vector>
 
 #include "perfetto/trace_processor/basic_types.h"
+#include "perfetto/trace_processor/status.h"
 #include "src/trace_processor/scoped_db.h"
+#include "src/trace_processor/sqlite.h"
 #include "src/trace_processor/table.h"
 
 namespace perfetto {
diff --git a/src/trace_processor/span_join_operator_table_unittest.cc b/src/trace_processor/span_join_operator_table_unittest.cc
index c67488d..6734c6b 100644
--- a/src/trace_processor/span_join_operator_table_unittest.cc
+++ b/src/trace_processor/span_join_operator_table_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/trace_processor/span_join_operator_table.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "src/trace_processor/trace_processor_context.h"
 #include "src/trace_processor/trace_storage.h"
 
diff --git a/src/trace_processor/sql_stats_table.cc b/src/trace_processor/sql_stats_table.cc
index f81ac66..fdd2467 100644
--- a/src/trace_processor/sql_stats_table.cc
+++ b/src/trace_processor/sql_stats_table.cc
@@ -16,12 +16,11 @@
 
 #include "src/trace_processor/sql_stats_table.h"
 
-#include <sqlite3.h>
-
 #include <algorithm>
 #include <bitset>
 #include <numeric>
 
+#include "src/trace_processor/sqlite.h"
 #include "src/trace_processor/sqlite_utils.h"
 #include "src/trace_processor/trace_storage.h"
 
diff --git a/src/public/data_source.cc b/src/trace_processor/sqlite.h
similarity index 64%
copy from src/public/data_source.cc
copy to src/trace_processor/sqlite.h
index 19eb17d..953a400 100644
--- a/src/public/data_source.cc
+++ b/src/trace_processor/sqlite.h
@@ -14,13 +14,16 @@
  * limitations under the License.
  */
 
-#include "perfetto/public/data_source.h"
+#ifndef SRC_TRACE_PROCESSOR_SQLITE_H_
+#define SRC_TRACE_PROCESSOR_SQLITE_H_
 
-namespace perfetto {
+#include "perfetto/base/build_config.h"
 
-DataSourceBase::~DataSourceBase() = default;
-void DataSourceBase::OnSetup(const SetupArgs&) {}
-void DataSourceBase::OnStart(const StartArgs&) {}
-void DataSourceBase::OnStop(const StopArgs&) {}
+#if PERFETTO_BUILDFLAG(PERFETTO_STANDALONE_BUILD) || \
+    PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
+#include <sqlite3.h>
+#else
+#include "third_party/sqlite/sqlite3.h"
+#endif
 
-}  // namespace perfetto
+#endif  // SRC_TRACE_PROCESSOR_SQLITE_H_
diff --git a/src/trace_processor/sqlite3_str_split_unittest.cc b/src/trace_processor/sqlite3_str_split_unittest.cc
index 4896f27..71a16b0 100644
--- a/src/trace_processor/sqlite3_str_split_unittest.cc
+++ b/src/trace_processor/sqlite3_str_split_unittest.cc
@@ -16,12 +16,12 @@
 
 #include "src/trace_processor/sqlite3_str_split.h"
 
-#include <sqlite3.h>
 #include <string>
 #include "perfetto/base/logging.h"
 #include "src/trace_processor/scoped_db.h"
+#include "src/trace_processor/sqlite.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/sqlite_utils.h b/src/trace_processor/sqlite_utils.h
index 5bd1e5f..60d095f 100644
--- a/src/trace_processor/sqlite_utils.h
+++ b/src/trace_processor/sqlite_utils.h
@@ -18,15 +18,15 @@
 #define SRC_TRACE_PROCESSOR_SQLITE_UTILS_H_
 
 #include <math.h>
-#include <sqlite3.h>
 
 #include <functional>
 #include <limits>
 #include <string>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
+#include "perfetto/ext/base/optional.h"
 #include "src/trace_processor/scoped_db.h"
+#include "src/trace_processor/sqlite.h"
 #include "src/trace_processor/table.h"
 
 namespace perfetto {
diff --git a/src/trace_processor/string_pool.h b/src/trace_processor/string_pool.h
index f4a1523..f5acdc7 100644
--- a/src/trace_processor/string_pool.h
+++ b/src/trace_processor/string_pool.h
@@ -17,7 +17,7 @@
 #ifndef SRC_TRACE_PROCESSOR_STRING_POOL_H_
 #define SRC_TRACE_PROCESSOR_STRING_POOL_H_
 
-#include "perfetto/base/paged_memory.h"
+#include "perfetto/ext/base/paged_memory.h"
 #include "src/trace_processor/null_term_string_view.h"
 
 #include <unordered_map>
diff --git a/src/trace_processor/string_pool_unittest.cc b/src/trace_processor/string_pool_unittest.cc
index 6160be4..4772475 100644
--- a/src/trace_processor/string_pool_unittest.cc
+++ b/src/trace_processor/string_pool_unittest.cc
@@ -18,7 +18,7 @@
 
 #include <random>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
@@ -49,7 +49,7 @@
   StringPool pool;
 
   auto id = pool.InternString(NullTermStringView());
-  ASSERT_EQ(id, 0);
+  ASSERT_EQ(id, 0u);
   ASSERT_EQ(pool.Get(id).c_str(), nullptr);
 }
 
@@ -119,7 +119,7 @@
     }
     string_map.erase(it_pair.first, it_pair.second);
   }
-  ASSERT_EQ(string_map.size(), 0);
+  ASSERT_EQ(string_map.size(), 0u);
 }
 
 }  // namespace
diff --git a/src/trace_processor/string_table.cc b/src/trace_processor/string_table.cc
index 0007296..407b838 100644
--- a/src/trace_processor/string_table.cc
+++ b/src/trace_processor/string_table.cc
@@ -16,13 +16,13 @@
 
 #include "src/trace_processor/string_table.h"
 
-#include <sqlite3.h>
 #include <string.h>
 
 #include <algorithm>
 #include <bitset>
 #include <numeric>
 
+#include "src/trace_processor/sqlite.h"
 #include "src/trace_processor/sqlite_utils.h"
 #include "src/trace_processor/trace_storage.h"
 
diff --git a/src/trace_processor/syscall_tracker.h b/src/trace_processor/syscall_tracker.h
index eba0fea..d2c910f 100644
--- a/src/trace_processor/syscall_tracker.h
+++ b/src/trace_processor/syscall_tracker.h
@@ -20,7 +20,7 @@
 #include <limits>
 #include <tuple>
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 #include "src/trace_processor/slice_tracker.h"
 #include "src/trace_processor/trace_processor_context.h"
 #include "src/trace_processor/trace_storage.h"
diff --git a/src/trace_processor/syscall_tracker_unittest.cc b/src/trace_processor/syscall_tracker_unittest.cc
index 0b8cef7..716889a 100644
--- a/src/trace_processor/syscall_tracker_unittest.cc
+++ b/src/trace_processor/syscall_tracker_unittest.cc
@@ -18,8 +18,8 @@
 
 #include "src/trace_processor/slice_tracker.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/systrace_trace_parser.cc b/src/trace_processor/systrace_trace_parser.cc
new file mode 100644
index 0000000..0dd146c
--- /dev/null
+++ b/src/trace_processor/systrace_trace_parser.cc
@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include "src/trace_processor/systrace_trace_parser.h"
+
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/string_utils.h"
+#include "src/trace_processor/args_tracker.h"
+#include "src/trace_processor/event_tracker.h"
+#include "src/trace_processor/process_tracker.h"
+#include "src/trace_processor/slice_tracker.h"
+#include "src/trace_processor/systrace_utils.h"
+
+#include <inttypes.h>
+#include <string>
+#include <unordered_map>
+
+namespace perfetto {
+namespace trace_processor {
+
+namespace {
+
+std::string SubstrTrim(const std::string& input, size_t start, size_t end) {
+  auto s = input.substr(start, end - start);
+  s.erase(s.begin(), std::find_if(s.begin(), s.end(),
+                                  [](int ch) { return !std::isspace(ch); }));
+  s.erase(std::find_if(s.rbegin(), s.rend(),
+                       [](int ch) { return !std::isspace(ch); })
+              .base(),
+          s.end());
+  return s;
+}
+
+}  // namespace
+
+SystraceTraceParser::SystraceTraceParser(TraceProcessorContext* ctx)
+    : context_(ctx),
+      sched_wakeup_name_id_(ctx->storage->InternString("sched_wakeup")),
+      cpu_idle_name_id_(ctx->storage->InternString("cpuidle")) {}
+SystraceTraceParser::~SystraceTraceParser() = default;
+
+util::Status SystraceTraceParser::Parse(std::unique_ptr<uint8_t[]> owned_buf,
+                                        size_t size) {
+  if (state_ == ParseState::kEndOfSystrace)
+    return util::OkStatus();
+  partial_buf_.insert(partial_buf_.end(), &owned_buf[0], &owned_buf[size]);
+
+  if (state_ == ParseState::kBeforeParse) {
+    state_ = partial_buf_[0] == '<' ? ParseState::kHtmlBeforeSystrace
+                                    : ParseState::kSystrace;
+  }
+
+  const char kSystraceStart[] =
+      R"(<script class="trace-data" type="application/text">)";
+  auto start_it = partial_buf_.begin();
+  for (;;) {
+    auto line_it = std::find(start_it, partial_buf_.end(), '\n');
+    if (line_it == partial_buf_.end())
+      break;
+
+    std::string buffer(start_it, line_it);
+    if (state_ == ParseState::kHtmlBeforeSystrace) {
+      if (base::Contains(buffer, kSystraceStart)) {
+        state_ = ParseState::kSystrace;
+      }
+    } else if (state_ == ParseState::kSystrace) {
+      if (base::Contains(buffer, R"(</script>)")) {
+        state_ = kEndOfSystrace;
+        break;
+      } else if (!base::StartsWith(buffer, "#")) {
+        ParseSingleSystraceEvent(buffer);
+      }
+    }
+    start_it = line_it + 1;
+  }
+  if (state_ == ParseState::kEndOfSystrace) {
+    partial_buf_.clear();
+  } else {
+    partial_buf_.erase(partial_buf_.begin(), start_it);
+  }
+  return util::OkStatus();
+}
+
+util::Status SystraceTraceParser::ParseSingleSystraceEvent(
+    const std::string& buffer) {
+  // An example line from buffer looks something like the following:
+  // <idle>-0     (-----) [000] d..1 16500.715638: cpu_idle: state=0 cpu_id=0
+
+  auto task_idx = 16u;
+  std::string task = SubstrTrim(buffer, 0, task_idx);
+
+  auto tgid_idx = buffer.find('(', task_idx + 1);
+  std::string pid_str = SubstrTrim(buffer, task_idx + 1, tgid_idx);
+  auto pid = static_cast<uint32_t>(std::stoi(pid_str));
+  context_->process_tracker->GetOrCreateThread(pid);
+
+  auto tgid_end = buffer.find(')', tgid_idx + 1);
+  std::string tgid_str = SubstrTrim(buffer, tgid_idx + 1, tgid_end);
+  auto tgid = tgid_str == "-----"
+                  ? base::nullopt
+                  : base::Optional<uint32_t>(
+                        static_cast<uint32_t>(std::stoi(tgid_str)));
+  if (tgid.has_value()) {
+    context_->process_tracker->UpdateThread(pid, tgid.value());
+  }
+
+  auto cpu_idx = buffer.find('[', tgid_end + 1);
+  auto cpu_end = buffer.find(']', cpu_idx + 1);
+  std::string cpu_str = SubstrTrim(buffer, cpu_idx + 1, cpu_end);
+  auto cpu = static_cast<uint32_t>(std::stoi(cpu_str));
+
+  auto ts_idx = buffer.find(' ', cpu_end + 2);
+  auto ts_end = buffer.find(':', ts_idx + 1);
+  std::string ts_str = SubstrTrim(buffer, ts_idx + 1, ts_end);
+  auto ts_float = std::stod(ts_str) * 1e9;
+  auto ts = static_cast<int64_t>(ts_float);
+
+  auto fn_idx = buffer.find(':', ts_end + 2);
+  std::string fn = SubstrTrim(buffer, ts_end + 2, fn_idx);
+
+  std::string args_str = SubstrTrim(buffer, fn_idx + 2, buffer.size());
+
+  std::unordered_map<std::string, std::string> args;
+  for (base::StringSplitter ss(args_str.c_str(), ' '); ss.Next();) {
+    std::string key;
+    std::string value;
+    for (base::StringSplitter inner(ss.cur_token(), '='); inner.Next();) {
+      if (key.empty()) {
+        key = inner.cur_token();
+      } else {
+        value = inner.cur_token();
+      }
+    }
+    args.emplace(std::move(key), std::move(value));
+  }
+  if (fn == "sched_switch") {
+    auto prev_state_str = args["prev_state"];
+    int64_t prev_state =
+        ftrace_utils::TaskState(prev_state_str.c_str()).raw_state();
+
+    auto prev_pid = std::stoi(args["prev_pid"]);
+    auto prev_comm = base::StringView(args["prev_comm"]);
+    auto prev_prio = std::stoi(args["prev_prio"]);
+    auto next_pid = std::stoi(args["next_pid"]);
+    auto next_comm = base::StringView(args["next_comm"]);
+    auto next_prio = std::stoi(args["next_prio"]);
+
+    context_->event_tracker->PushSchedSwitch(
+        static_cast<uint32_t>(cpu), ts, static_cast<uint32_t>(prev_pid),
+        prev_comm, prev_prio, prev_state, static_cast<uint32_t>(next_pid),
+        next_comm, next_prio);
+  } else if (fn == "tracing_mark_write") {
+    systrace_utils::SystraceTracePoint point;
+    auto result = ParseSystraceTracePoint(args_str.c_str(), &point);
+    if (result == systrace_utils::SystraceParseResult::kSuccess) {
+      switch (point.phase) {
+        case 'B': {
+          StringId name_id = context_->storage->InternString(point.name);
+          context_->slice_tracker->BeginAndroid(ts, pid, point.tgid,
+                                                0 /*cat_id*/, name_id);
+          break;
+        }
+        case 'E': {
+          context_->slice_tracker->EndAndroid(ts, pid, point.tgid);
+          break;
+        }
+        case 'C': {
+          // This is per upid on purpose. Some counters are pushed from
+          // arbitrary threads but are really per process.
+          UniquePid upid =
+              context_->process_tracker->GetOrCreateProcess(point.tgid);
+          StringId name_id = context_->storage->InternString(point.name);
+          context_->event_tracker->PushCounter(ts, point.value, name_id, upid,
+                                               RefType::kRefUpid);
+        }
+      }
+    }
+  } else if (fn == "sched_wakeup") {
+    auto comm = args["comm"];
+    uint32_t wakee_pid = static_cast<uint32_t>(std::stoi(args["pid"]));
+
+    StringId name_id = context_->storage->InternString(base::StringView(comm));
+    auto wakee_utid =
+        context_->process_tracker->UpdateThreadName(wakee_pid, name_id);
+    context_->event_tracker->PushInstant(ts, sched_wakeup_name_id_,
+                                         0 /* value */, wakee_utid,
+                                         RefType::kRefUtid);
+  } else if (fn == "cpu_idle") {
+    auto new_state = static_cast<double>(std::stol(args["state"]));
+    uint32_t event_cpu = static_cast<uint32_t>(std::stoi(args["cpu_id"]));
+    context_->event_tracker->PushCounter(ts, new_state, cpu_idle_name_id_,
+                                         event_cpu, RefType::kRefCpuId);
+  }
+
+  return util::OkStatus();
+}
+
+}  // namespace trace_processor
+}  // namespace perfetto
diff --git a/src/trace_processor/systrace_trace_parser.h b/src/trace_processor/systrace_trace_parser.h
new file mode 100644
index 0000000..de58581
--- /dev/null
+++ b/src/trace_processor/systrace_trace_parser.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef SRC_TRACE_PROCESSOR_SYSTRACE_TRACE_PARSER_H_
+#define SRC_TRACE_PROCESSOR_SYSTRACE_TRACE_PARSER_H_
+
+#include <deque>
+#include <regex>
+
+#include "src/trace_processor/chunked_trace_reader.h"
+#include "src/trace_processor/trace_processor_context.h"
+#include "src/trace_processor/trace_storage.h"
+
+namespace perfetto {
+namespace trace_processor {
+
+class SystraceTraceParser : public ChunkedTraceReader {
+ public:
+  explicit SystraceTraceParser(TraceProcessorContext*);
+  ~SystraceTraceParser() override;
+
+  // ChunkedTraceReader implementation.
+  util::Status Parse(std::unique_ptr<uint8_t[]>, size_t size) override;
+
+ private:
+  enum ParseState {
+    kBeforeParse,
+    kHtmlBeforeSystrace,
+    kSystrace,
+    kEndOfSystrace,
+  };
+
+  util::Status ParseSingleSystraceEvent(const std::string& buffer);
+
+  TraceProcessorContext* const context_;
+  const StringId sched_wakeup_name_id_ = 0;
+  const StringId cpu_idle_name_id_ = 0;
+
+  ParseState state_ = ParseState::kBeforeParse;
+
+  // Used to glue together trace packets that span across two (or more)
+  // Parse() boundaries.
+  std::deque<uint8_t> partial_buf_;
+};
+
+}  // namespace trace_processor
+}  // namespace perfetto
+
+#endif  // SRC_TRACE_PROCESSOR_SYSTRACE_TRACE_PARSER_H_
diff --git a/src/trace_processor/systrace_utils.h b/src/trace_processor/systrace_utils.h
new file mode 100644
index 0000000..a23e436
--- /dev/null
+++ b/src/trace_processor/systrace_utils.h
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#ifndef SRC_TRACE_PROCESSOR_SYSTRACE_UTILS_H_
+#define SRC_TRACE_PROCESSOR_SYSTRACE_UTILS_H_
+
+#include <string>
+
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/string_view.h"
+
+namespace perfetto {
+namespace trace_processor {
+namespace systrace_utils {
+
+struct SystraceTracePoint {
+  SystraceTracePoint() {}
+
+  SystraceTracePoint(char p, uint32_t tg, base::StringView n, double v)
+      : phase(p), tgid(tg), name(std::move(n)), value(v) {}
+
+  // Phase can be one of B, E or C.
+  char phase = '\0';
+
+  uint32_t tgid = 0;
+
+  // For phase = 'B' and phase = 'C' only.
+  base::StringView name;
+
+  // For phase = 'C' only.
+  double value = 0;
+};
+
+inline bool operator==(const SystraceTracePoint& x,
+                       const SystraceTracePoint& y) {
+  return std::tie(x.phase, x.tgid, x.name, x.value) ==
+         std::tie(y.phase, y.tgid, y.name, y.value);
+}
+
+enum class SystraceParseResult { kFailure = 0, kUnsupported, kSuccess };
+
+// We have to handle trace_marker events of a few different types:
+// 1. some random text
+// 2. B|1636|pokeUserActivity
+// 3. E|1636
+// 4. C|1636|wq:monitor|0
+inline SystraceParseResult ParseSystraceTracePoint(base::StringView str,
+                                                   SystraceTracePoint* out) {
+  const char* s = str.data();
+  size_t len = str.size();
+
+  if (len < 2)
+    return SystraceParseResult::kFailure;
+
+  // If str matches '[BEC]\|[0-9]+[\|\n]' set tgid_length to the length of
+  // the number. Otherwise return kFailure.
+  if (s[1] != '|' && s[1] != '\n')
+    return SystraceParseResult::kFailure;
+  if (s[0] != 'B' && s[0] != 'E' && s[0] != 'C') {
+    // TODO: support android async slices
+    return s[0] == 'S' || s[0] == 'F' ? SystraceParseResult::kUnsupported
+                                      : SystraceParseResult::kFailure;
+  }
+  size_t tgid_length = 0;
+  for (size_t i = 2; i < len; i++) {
+    if (s[i] == '|' || s[i] == '\n') {
+      tgid_length = i - 2;
+      break;
+    }
+    if (s[i] < '0' || s[i] > '9')
+      return SystraceParseResult::kFailure;
+  }
+
+  if (tgid_length == 0) {
+    out->tgid = 0;
+  } else {
+    std::string tgid_str(s + 2, tgid_length);
+    out->tgid = static_cast<uint32_t>(std::stoi(tgid_str.c_str()));
+  }
+
+  out->phase = s[0];
+  switch (s[0]) {
+    case 'B': {
+      size_t name_index = 2 + tgid_length + 1;
+      out->name = base::StringView(
+          s + name_index, len - name_index - (s[len - 1] == '\n' ? 1 : 0));
+      return SystraceParseResult::kSuccess;
+    }
+    case 'E': {
+      return SystraceParseResult::kSuccess;
+    }
+    case 'C': {
+      size_t name_index = 2 + tgid_length + 1;
+      base::Optional<size_t> name_length;
+      for (size_t i = name_index; i < len; i++) {
+        if (s[i] == '|') {
+          name_length = i - name_index;
+          break;
+        }
+      }
+      if (!name_length.has_value())
+        return SystraceParseResult::kFailure;
+      out->name = base::StringView(s + name_index, name_length.value());
+
+      size_t value_index = name_index + name_length.value() + 1;
+      size_t value_len = len - value_index;
+      if (value_len == 0)
+        return SystraceParseResult::kFailure;
+      std::string value_str(s + value_index, value_len);
+      out->value = std::stod(value_str.c_str());
+      return SystraceParseResult::kSuccess;
+    }
+    default:
+      return SystraceParseResult::kFailure;
+  }
+}
+
+}  // namespace systrace_utils
+}  // namespace trace_processor
+}  // namespace perfetto
+
+#endif  // SRC_TRACE_PROCESSOR_SYSTRACE_UTILS_H_
diff --git a/src/trace_processor/systrace_utils_unittest.cc b/src/trace_processor/systrace_utils_unittest.cc
new file mode 100644
index 0000000..c8c42d0
--- /dev/null
+++ b/src/trace_processor/systrace_utils_unittest.cc
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include "src/trace_processor/systrace_utils.h"
+
+#include <gtest/gtest.h>
+
+namespace perfetto {
+namespace trace_processor {
+namespace systrace_utils {
+
+TEST(SystraceParserTest, SystraceEvent) {
+  SystraceTracePoint result{};
+
+  ASSERT_EQ(ParseSystraceTracePoint(base::StringView(""), &result),
+            SystraceParseResult::kFailure);
+
+  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("B|1|foo"), &result),
+            SystraceParseResult::kSuccess);
+  EXPECT_EQ(result, (SystraceTracePoint('B', 1, base::StringView("foo"), 0)));
+
+  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("B|42|Bar"), &result),
+            SystraceParseResult::kSuccess);
+  EXPECT_EQ(result, (SystraceTracePoint('B', 42, base::StringView("Bar"), 0)));
+
+  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("C|543|foo|"), &result),
+            SystraceParseResult::kFailure);
+  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("C|543|foo|8"), &result),
+            SystraceParseResult::kSuccess);
+  EXPECT_EQ(result, (SystraceTracePoint('C', 543, base::StringView("foo"), 8)));
+
+  ASSERT_EQ(ParseSystraceTracePoint(base::StringView("S|"), &result),
+            SystraceParseResult::kUnsupported);
+}
+}  // namespace systrace_utils
+}  // namespace trace_processor
+}  // namespace perfetto
diff --git a/src/trace_processor/table.h b/src/trace_processor/table.h
index 0405eb9..0c8e638 100644
--- a/src/trace_processor/table.h
+++ b/src/trace_processor/table.h
@@ -17,16 +17,17 @@
 #ifndef SRC_TRACE_PROCESSOR_TABLE_H_
 #define SRC_TRACE_PROCESSOR_TABLE_H_
 
-#include <sqlite3.h>
-
 #include <functional>
 #include <memory>
 #include <string>
 #include <vector>
 
-#include "perfetto/base/optional.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/trace_processor/basic_types.h"
+#include "perfetto/trace_processor/status.h"
 #include "src/trace_processor/query_constraints.h"
+#include "src/trace_processor/sqlite.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/thread_table_unittest.cc b/src/trace_processor/thread_table_unittest.cc
index cfe4b3d..1a91f6d 100644
--- a/src/trace_processor/thread_table_unittest.cc
+++ b/src/trace_processor/thread_table_unittest.cc
@@ -23,8 +23,8 @@
 #include "src/trace_processor/scoped_db.h"
 #include "src/trace_processor/trace_processor_context.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/trace_database_integrationtest.cc b/src/trace_processor/trace_database_integrationtest.cc
index d6db8bf..9a60472 100644
--- a/src/trace_processor/trace_database_integrationtest.cc
+++ b/src/trace_processor/trace_database_integrationtest.cc
@@ -18,9 +18,9 @@
 #include <random>
 #include <string>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 #include "perfetto/trace_processor/trace_processor.h"
 #include "src/base/test/utils.h"
 #include "src/trace_processor/json_trace_parser.h"
diff --git a/src/trace_processor/trace_processor_impl.cc b/src/trace_processor/trace_processor_impl.cc
index 2b9022e..9e34dd3 100644
--- a/src/trace_processor/trace_processor_impl.cc
+++ b/src/trace_processor/trace_processor_impl.cc
@@ -22,9 +22,9 @@
 #include <functional>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/string_utils.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/string_utils.h"
+#include "perfetto/ext/base/time.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "src/trace_processor/android_logs_table.h"
 #include "src/trace_processor/args_table.h"
@@ -60,6 +60,7 @@
 #include "src/trace_processor/stats_table.h"
 #include "src/trace_processor/string_table.h"
 #include "src/trace_processor/syscall_tracker.h"
+#include "src/trace_processor/systrace_trace_parser.h"
 #include "src/trace_processor/table.h"
 #include "src/trace_processor/thread_table.h"
 #include "src/trace_processor/trace_blob_view.h"
@@ -279,11 +280,26 @@
     if (first_word == kFuchsiaMagicNumber)
       return kFuchsiaTraceType;
   }
+
+  // Systrace with header but no leading HTML.
+  if (base::StartsWith(start, "# tracer"))
+    return kSystraceTraceType;
+
+  // Systrace with leading HTML.
+  if (base::StartsWith(start, "<!DOCTYPE html>") ||
+      base::StartsWith(start, "<html>"))
+    return kSystraceTraceType;
+
+  // Systrace with no header or leading HTML.
+  if (base::StartsWith(start, " "))
+    return kSystraceTraceType;
+
   return kProtoTraceType;
 }
 
 TraceProcessorImpl::TraceProcessorImpl(const Config& cfg) {
   sqlite3* db = nullptr;
+  PERFETTO_CHECK(sqlite3_initialize() == SQLITE_OK);
   PERFETTO_CHECK(sqlite3_open(":memory:", &db) == SQLITE_OK);
   InitializeSqlite(db);
   CreateBuiltinTables(db);
@@ -381,6 +397,9 @@
         context_.parser.reset(new FuchsiaTraceParser(&context_));
         break;
       }
+      case kSystraceTraceType:
+        context_.chunk_reader.reset(new SystraceTraceParser(&context_));
+        break;
       case kUnknownTraceType:
         return util::ErrStatus("Unknown trace type provided");
     }
@@ -397,7 +416,8 @@
   if (unrecoverable_parse_error_ || !context_.chunk_reader)
     return;
 
-  context_.sorter->ExtractEventsForced();
+  if (context_.sorter)
+    context_.sorter->ExtractEventsForced();
   context_.event_tracker->FlushPendingEvents();
   BuildBoundsTable(*db_, context_.storage->GetTraceTimestampBoundsNs());
 }
diff --git a/src/trace_processor/trace_processor_impl.h b/src/trace_processor/trace_processor_impl.h
index ee2c055..8673928 100644
--- a/src/trace_processor/trace_processor_impl.h
+++ b/src/trace_processor/trace_processor_impl.h
@@ -17,18 +17,19 @@
 #ifndef SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_
 #define SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_
 
-#include <sqlite3.h>
 #include <atomic>
 #include <functional>
 #include <memory>
 #include <vector>
 
-#include "perfetto/base/string_view.h"
+#include "perfetto/ext/base/string_view.h"
 #include "perfetto/trace_processor/basic_types.h"
+#include "perfetto/trace_processor/status.h"
 #include "perfetto/trace_processor/trace_processor.h"
 #include "src/trace_processor/metrics/descriptors.h"
 #include "src/trace_processor/metrics/metrics.h"
 #include "src/trace_processor/scoped_db.h"
+#include "src/trace_processor/sqlite.h"
 #include "src/trace_processor/trace_processor_context.h"
 
 namespace perfetto {
@@ -40,6 +41,7 @@
   kProtoTraceType,
   kJsonTraceType,
   kFuchsiaTraceType,
+  kSystraceTraceType,
 };
 
 TraceType GuessTraceType(const uint8_t* data, size_t size);
diff --git a/src/trace_processor/trace_processor_impl_unittest.cc b/src/trace_processor/trace_processor_impl_unittest.cc
index 629314f..24611d0 100644
--- a/src/trace_processor/trace_processor_impl_unittest.cc
+++ b/src/trace_processor/trace_processor_impl_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/trace_processor/trace_processor_impl.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/trace_processor_shell.cc b/src/trace_processor/trace_processor_shell.cc
index 800e2cb..96b15f5 100644
--- a/src/trace_processor/trace_processor_shell.cc
+++ b/src/trace_processor/trace_processor_shell.cc
@@ -32,11 +32,11 @@
 #include <google/protobuf/text_format.h>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/time.h"
 #include "perfetto/trace_processor/trace_processor.h"
 #include "src/trace_processor/metrics/metrics.descriptor.h"
 
@@ -599,22 +599,19 @@
       " -e|--export FILE                   Export the trace into a SQLite "
       "database.\n"
       " --run-metrics x,y,z                Runs a comma separated list of "
-      "metrics and prints the result as a TraceMetrics proto to stdout.\n"
-      " --register-metric FILE             Registers the given SQL file with "
-      "trace processor to allow this file to be run as a metric.\n"
-      " --extend-metrics-proto FILE        Extends the TraceMetrics proto "
-      "using the extensions in the given proto file.\n"
-      " --metric-textproto                 Ouputs the result of --run-metrics "
-      "as a textual proto rather than as serialized bytes.",
+      "metrics and prints the result as a TraceMetrics proto to stdout. The "
+      "metrics specified can either be in-built metrics or SQL/proto files of "
+      "extension metrics.\n"
+      " --metrics-output=[binary|text]      Allows the output of --run-metrics "
+      "to be specified in either proto binary or proto text format (default: "
+      "text)",
       argv[0]);
 }
 
 int TraceProcessorMain(int argc, char** argv) {
   enum LongOption {
     OPT_RUN_METRICS = 1000,
-    OPT_REGISTER_METRIC,
-    OPT_EXTEND_METRICS_PROTO,
-    OPT_METRICS_TEXTPROTO,
+    OPT_METRICS_OUTPUT,
   };
 
   static const struct option long_options[] = {
@@ -626,20 +623,15 @@
       {"query-file", required_argument, nullptr, 'q'},
       {"export", required_argument, nullptr, 'e'},
       {"run-metrics", required_argument, nullptr, OPT_RUN_METRICS},
-      {"register-metric", required_argument, nullptr, OPT_REGISTER_METRIC},
-      {"extend-metrics-proto", required_argument, nullptr,
-       OPT_EXTEND_METRICS_PROTO},
-      {"metric-textproto", no_argument, nullptr, OPT_METRICS_TEXTPROTO},
+      {"metrics-output", required_argument, nullptr, OPT_METRICS_OUTPUT},
       {nullptr, 0, nullptr, 0}};
 
   std::string perf_file_path;
   std::string query_file_path;
   std::string sqlite_file_path;
   std::string metric_names;
-  std::string register_metric;
-  std::string extend_metrics_proto;
+  std::string metric_output;
   bool explicit_interactive = false;
-  bool metrics_textproto = false;
   int option_index = 0;
   for (;;) {
     int option =
@@ -683,18 +675,8 @@
       continue;
     }
 
-    if (option == OPT_REGISTER_METRIC) {
-      register_metric = optarg;
-      continue;
-    }
-
-    if (option == OPT_EXTEND_METRICS_PROTO) {
-      extend_metrics_proto = optarg;
-      continue;
-    }
-
-    if (option == OPT_METRICS_TEXTPROTO) {
-      metrics_textproto = true;
+    if (option == OPT_METRICS_OUTPUT) {
+      metric_output = optarg;
       continue;
     }
 
@@ -815,27 +797,45 @@
     pool.BuildFile(desc);
   }
 
-  if (!extend_metrics_proto.empty()) {
-    util::Status status = ExtendMetricsProto(extend_metrics_proto, &pool);
-    if (!status.ok()) {
-      PERFETTO_ELOG("Error when extending proto: %s", status.c_message());
-      return 1;
-    }
-  }
-
-  if (!register_metric.empty()) {
-    util::Status status = RegisterMetric(register_metric);
-    if (!status.ok()) {
-      PERFETTO_ELOG("Error when registering metric: %s", status.c_message());
-      return 1;
-    }
-  }
-
   if (!metric_names.empty()) {
     std::vector<std::string> metrics;
     for (base::StringSplitter ss(metric_names, ','); ss.Next();) {
       metrics.emplace_back(ss.cur_token());
     }
+
+    // For all metrics which are files, register them and extend the metrics
+    // proto.
+    for (size_t i = 0; i < metrics.size(); ++i) {
+      const std::string& metric_or_path = metrics[i];
+
+      // If there is no extension, we assume it is a builtin metric.
+      auto ext_idx = metric_or_path.rfind(".");
+      if (ext_idx == std::string::npos)
+        continue;
+
+      std::string no_ext_name = metric_or_path.substr(0, ext_idx);
+      util::Status status = RegisterMetric(no_ext_name + ".sql");
+      if (!status.ok()) {
+        PERFETTO_ELOG("Unable to register metric %s: %s",
+                      metric_or_path.c_str(), status.c_message());
+        return 1;
+      }
+
+      status = ExtendMetricsProto(no_ext_name + ".proto", &pool);
+      if (!status.ok()) {
+        PERFETTO_ELOG("Unable to extend metrics proto %s: %s",
+                      metric_or_path.c_str(), status.c_message());
+        return 1;
+      }
+
+      auto slash_idx = no_ext_name.rfind('/');
+      std::string basename = slash_idx == std::string::npos
+                                 ? no_ext_name
+                                 : no_ext_name.substr(slash_idx + 1);
+      metrics[i] = basename;
+    }
+
+    bool metrics_textproto = metric_output != "binary";
     int ret = RunMetrics(std::move(metrics), metrics_textproto, pool);
     if (!ret) {
       auto t_query = base::GetWallTimeNs() - t_run_start;
diff --git a/src/trace_processor/trace_sorter.cc b/src/trace_processor/trace_sorter.cc
index 315460c..c08ada8 100644
--- a/src/trace_processor/trace_sorter.cc
+++ b/src/trace_processor/trace_sorter.cc
@@ -17,6 +17,7 @@
 #include <algorithm>
 #include <utility>
 
+#include "perfetto/ext/base/utils.h"
 #include "src/trace_processor/proto_trace_parser.h"
 #include "src/trace_processor/trace_sorter.h"
 
diff --git a/src/trace_processor/trace_sorter.h b/src/trace_processor/trace_sorter.h
index 839453b..6c0d778 100644
--- a/src/trace_processor/trace_sorter.h
+++ b/src/trace_processor/trace_sorter.h
@@ -19,7 +19,7 @@
 
 #include <vector>
 
-#include "perfetto/base/circular_queue.h"
+#include "perfetto/ext/base/circular_queue.h"
 #include "perfetto/trace_processor/basic_types.h"
 #include "src/trace_processor/fuchsia_provider_view.h"
 #include "src/trace_processor/proto_incremental_state.h"
diff --git a/src/trace_processor/trace_sorter_unittest.cc b/src/trace_processor/trace_sorter_unittest.cc
index e4b39c7..3e3927e 100644
--- a/src/trace_processor/trace_sorter_unittest.cc
+++ b/src/trace_processor/trace_sorter_unittest.cc
@@ -19,8 +19,8 @@
 #include <random>
 #include <vector>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include "perfetto/trace_processor/basic_types.h"
 #include "src/trace_processor/trace_processor_context.h"
diff --git a/src/trace_processor/trace_storage.cc b/src/trace_processor/trace_storage.cc
index 2731638..58a2d01 100644
--- a/src/trace_processor/trace_storage.cc
+++ b/src/trace_processor/trace_storage.cc
@@ -20,7 +20,7 @@
 #include <algorithm>
 #include <limits>
 
-#include "perfetto/base/no_destructor.h"
+#include "perfetto/ext/base/no_destructor.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/trace_storage.h b/src/trace_processor/trace_storage.h
index 5318da9..45ffed3 100644
--- a/src/trace_processor/trace_storage.h
+++ b/src/trace_processor/trace_storage.h
@@ -25,12 +25,12 @@
 #include <utility>
 #include <vector>
 
-#include "perfetto/base/hash.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/time.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/hash.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/trace_processor/ftrace_utils.h"
 #include "src/trace_processor/metadata.h"
 #include "src/trace_processor/stats.h"
diff --git a/src/traced/probes/BUILD.gn b/src/traced/probes/BUILD.gn
index adc0124..537ad24 100644
--- a/src/traced/probes/BUILD.gn
+++ b/src/traced/probes/BUILD.gn
@@ -14,7 +14,7 @@
 
 source_set("probes") {
   public_deps = [
-    "../../../include/perfetto/traced",
+    "../../../include/perfetto/ext/traced",
   ]
   deps = [
     ":probes_src",
@@ -33,7 +33,7 @@
   deps = [
     ":data_source",
     "../../../gn:default_deps",
-    "../../../include/perfetto/traced",
+    "../../../include/perfetto/ext/traced",
     "../../../protos/perfetto/trace/ps:zero",
     "../../base",
     "../../tracing:ipc",
diff --git a/src/traced/probes/android_log/BUILD.gn b/src/traced/probes/android_log/BUILD.gn
index be122a4..4ca377a 100644
--- a/src/traced/probes/android_log/BUILD.gn
+++ b/src/traced/probes/android_log/BUILD.gn
@@ -19,7 +19,7 @@
   deps = [
     "..:data_source",
     "../../../../gn:default_deps",
-    "../../../../include/perfetto/traced",
+    "../../../../include/perfetto/ext/traced",
     "../../../../protos/perfetto/common:zero",
     "../../../../protos/perfetto/config:zero",
     "../../../../protos/perfetto/trace/android:zero",
diff --git a/src/traced/probes/android_log/android_log_data_source.cc b/src/traced/probes/android_log/android_log_data_source.cc
index 63392a5..4556033 100644
--- a/src/traced/probes/android_log/android_log_data_source.cc
+++ b/src/traced/probes/android_log/android_log_data_source.cc
@@ -16,18 +16,18 @@
 
 #include "src/traced/probes/android_log/android_log_data_source.h"
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/string_view.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/time.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 
 #include "perfetto/common/android_log_constants.pbzero.h"
 #include "perfetto/config/android/android_log_config.pbzero.h"
diff --git a/src/traced/probes/android_log/android_log_data_source.h b/src/traced/probes/android_log/android_log_data_source.h
index 3a19e19..171e543 100644
--- a/src/traced/probes/android_log/android_log_data_source.h
+++ b/src/traced/probes/android_log/android_log_data_source.h
@@ -21,11 +21,11 @@
 #include <unordered_set>
 #include <vector>
 
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/string_view.h"
-#include "perfetto/base/unix_socket.h"
-#include "perfetto/base/weak_ptr.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/string_view.h"
+#include "perfetto/ext/base/unix_socket.h"
+#include "perfetto/ext/base/weak_ptr.h"
 #include "src/traced/probes/probes_data_source.h"
 
 namespace perfetto {
diff --git a/src/traced/probes/android_log/android_log_data_source_unittest.cc b/src/traced/probes/android_log/android_log_data_source_unittest.cc
index 601a779..defd4b6 100644
--- a/src/traced/probes/android_log/android_log_data_source_unittest.cc
+++ b/src/traced/probes/android_log/android_log_data_source_unittest.cc
@@ -16,13 +16,13 @@
 
 #include "src/traced/probes/android_log/android_log_data_source.h"
 
+#include "perfetto/ext/tracing/core/data_source_config.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
-#include "perfetto/tracing/core/data_source_config.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/tracing/core/trace_writer_for_testing.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include "perfetto/common/android_log_constants.pbzero.h"
 #include "perfetto/config/android/android_log_config.pbzero.h"
diff --git a/src/traced/probes/filesystem/BUILD.gn b/src/traced/probes/filesystem/BUILD.gn
index 37bbdca..edf2b80 100644
--- a/src/traced/probes/filesystem/BUILD.gn
+++ b/src/traced/probes/filesystem/BUILD.gn
@@ -20,7 +20,7 @@
   deps = [
     "..:data_source",
     "../../../../gn:default_deps",
-    "../../../../include/perfetto/traced",
+    "../../../../include/perfetto/ext/traced",
     "../../../../protos/perfetto/config:zero",
     "../../../base",
   ]
diff --git a/src/traced/probes/filesystem/file_scanner.h b/src/traced/probes/filesystem/file_scanner.h
index 7e8f909..5fac41d 100644
--- a/src/traced/probes/filesystem/file_scanner.h
+++ b/src/traced/probes/filesystem/file_scanner.h
@@ -20,10 +20,10 @@
 #include <string>
 #include <vector>
 
-#include "perfetto/base/scoped_file.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/traced/data_source_types.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/traced/data_source_types.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/filesystem/file_scanner_unittest.cc b/src/traced/probes/filesystem/file_scanner_unittest.cc
index 29776b0..ef21e0e 100644
--- a/src/traced/probes/filesystem/file_scanner_unittest.cc
+++ b/src/traced/probes/filesystem/file_scanner_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/traced/probes/filesystem/file_scanner.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include <sys/stat.h>
 #include <memory>
diff --git a/src/traced/probes/filesystem/fs_mount.cc b/src/traced/probes/filesystem/fs_mount.cc
index da2030e..7564490 100644
--- a/src/traced/probes/filesystem/fs_mount.cc
+++ b/src/traced/probes/filesystem/fs_mount.cc
@@ -21,9 +21,9 @@
 #include <fstream>
 #include <sstream>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_splitter.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/string_splitter.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/filesystem/fs_mount.h b/src/traced/probes/filesystem/fs_mount.h
index 2086a80..b66a467 100644
--- a/src/traced/probes/filesystem/fs_mount.h
+++ b/src/traced/probes/filesystem/fs_mount.h
@@ -21,7 +21,7 @@
 #include <map>
 #include <string>
 #include <vector>
-#include "perfetto/traced/data_source_types.h"
+#include "perfetto/ext/traced/data_source_types.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/filesystem/fs_mount_unittest.cc b/src/traced/probes/filesystem/fs_mount_unittest.cc
index 6c6b489..d2462d3 100644
--- a/src/traced/probes/filesystem/fs_mount_unittest.cc
+++ b/src/traced/probes/filesystem/fs_mount_unittest.cc
@@ -21,13 +21,13 @@
 #include <stdlib.h>
 #include <sys/stat.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/filesystem/inode_file_data_source.cc b/src/traced/probes/filesystem/inode_file_data_source.cc
index f3ae903..cf49f46 100644
--- a/src/traced/probes/filesystem/inode_file_data_source.cc
+++ b/src/traced/probes/filesystem/inode_file_data_source.cc
@@ -24,9 +24,9 @@
 #include <unordered_map>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 
 #include "perfetto/config/inode_file/inode_file_config.pbzero.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
diff --git a/src/traced/probes/filesystem/inode_file_data_source.h b/src/traced/probes/filesystem/inode_file_data_source.h
index 6ecd8e5..7acda83 100644
--- a/src/traced/probes/filesystem/inode_file_data_source.h
+++ b/src/traced/probes/filesystem/inode_file_data_source.h
@@ -26,11 +26,11 @@
 #include <unordered_map>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/traced/data_source_types.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/traced/data_source_types.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/traced/probes/filesystem/file_scanner.h"
 #include "src/traced/probes/filesystem/fs_mount.h"
 #include "src/traced/probes/filesystem/lru_inode_cache.h"
diff --git a/src/traced/probes/filesystem/inode_file_data_source_unittest.cc b/src/traced/probes/filesystem/inode_file_data_source_unittest.cc
index cc6c85c..f639cba 100644
--- a/src/traced/probes/filesystem/inode_file_data_source_unittest.cc
+++ b/src/traced/probes/filesystem/inode_file_data_source_unittest.cc
@@ -24,8 +24,8 @@
 #include "perfetto/config/inode_file/inode_file_config.pbzero.h"
 #include "perfetto/trace/filesystem/inode_file_map.pbzero.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/filesystem/lru_inode_cache.h b/src/traced/probes/filesystem/lru_inode_cache.h
index 0b36109..629d387 100644
--- a/src/traced/probes/filesystem/lru_inode_cache.h
+++ b/src/traced/probes/filesystem/lru_inode_cache.h
@@ -22,7 +22,7 @@
 #include <string>
 #include <tuple>
 
-#include "perfetto/traced/data_source_types.h"
+#include "perfetto/ext/traced/data_source_types.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/filesystem/lru_inode_cache_unittest.cc b/src/traced/probes/filesystem/lru_inode_cache_unittest.cc
index aafdc97..8fb3429 100644
--- a/src/traced/probes/filesystem/lru_inode_cache_unittest.cc
+++ b/src/traced/probes/filesystem/lru_inode_cache_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/traced/probes/filesystem/lru_inode_cache.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include <string>
 #include <tuple>
diff --git a/src/traced/probes/filesystem/prefix_finder.cc b/src/traced/probes/filesystem/prefix_finder.cc
index 360880a..b22532e 100644
--- a/src/traced/probes/filesystem/prefix_finder.cc
+++ b/src/traced/probes/filesystem/prefix_finder.cc
@@ -16,7 +16,7 @@
 
 #include "src/traced/probes/filesystem/prefix_finder.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_splitter.h"
+#include "perfetto/ext/base/string_splitter.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/filesystem/prefix_finder.h b/src/traced/probes/filesystem/prefix_finder.h
index f788dea..4ae7f60 100644
--- a/src/traced/probes/filesystem/prefix_finder.h
+++ b/src/traced/probes/filesystem/prefix_finder.h
@@ -25,7 +25,7 @@
 #include <vector>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/lookup_set.h"
+#include "perfetto/ext/base/lookup_set.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/filesystem/prefix_finder_unittest.cc b/src/traced/probes/filesystem/prefix_finder_unittest.cc
index b9972c5..0301810 100644
--- a/src/traced/probes/filesystem/prefix_finder_unittest.cc
+++ b/src/traced/probes/filesystem/prefix_finder_unittest.cc
@@ -21,12 +21,12 @@
 #include <stdlib.h>
 #include <sys/stat.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/filesystem/range_tree.h b/src/traced/probes/filesystem/range_tree.h
index b28695a..9821ea7 100644
--- a/src/traced/probes/filesystem/range_tree.h
+++ b/src/traced/probes/filesystem/range_tree.h
@@ -20,7 +20,7 @@
 
 #include <stdio.h>
 
-#include "perfetto/base/small_set.h"
+#include "perfetto/ext/base/small_set.h"
 #include "src/traced/probes/filesystem/inode_file_data_source.h"
 #include "src/traced/probes/filesystem/prefix_finder.h"
 
diff --git a/src/traced/probes/filesystem/range_tree_unittest.cc b/src/traced/probes/filesystem/range_tree_unittest.cc
index 288647a..b600b8e 100644
--- a/src/traced/probes/filesystem/range_tree_unittest.cc
+++ b/src/traced/probes/filesystem/range_tree_unittest.cc
@@ -22,12 +22,12 @@
 #include <stdlib.h>
 #include <sys/stat.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/ftrace/BUILD.gn b/src/traced/probes/ftrace/BUILD.gn
index 3a6767f..a3ae990 100644
--- a/src/traced/probes/ftrace/BUILD.gn
+++ b/src/traced/probes/ftrace/BUILD.gn
@@ -12,9 +12,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import("../../../../gn/fuzzer.gni")
 import("../../../../gn/perfetto.gni")
 import("../../../../gn/proto_library.gni")
-import("../../../../gn/fuzzer.gni")
 import("../../../../gn/protozero_library.gni")
 
 # For use_libfuzzer.
@@ -113,7 +113,7 @@
     ":format_parser",
     "..:data_source",
     "../../../../gn:default_deps",
-    "../../../../include/perfetto/traced",
+    "../../../../include/perfetto/ext/traced",
     "../../../../protos/perfetto/config:lite",
     "../../../android_internal:headers",
     "../../../base",
diff --git a/src/traced/probes/ftrace/atrace_hal_wrapper.h b/src/traced/probes/ftrace/atrace_hal_wrapper.h
index 883388b..db215ae 100644
--- a/src/traced/probes/ftrace/atrace_hal_wrapper.h
+++ b/src/traced/probes/ftrace/atrace_hal_wrapper.h
@@ -21,7 +21,7 @@
 #include <string>
 #include <vector>
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/ftrace/atrace_wrapper.cc b/src/traced/probes/ftrace/atrace_wrapper.cc
index 2acdbe4..bea9995 100644
--- a/src/traced/probes/ftrace/atrace_wrapper.cc
+++ b/src/traced/probes/ftrace/atrace_wrapper.cc
@@ -26,8 +26,9 @@
 #include <unistd.h>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/pipe.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/pipe.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/ftrace/cpu_reader.cc b/src/traced/probes/ftrace/cpu_reader.cc
index 436edfb..9f0603d 100644
--- a/src/traced/probes/ftrace/cpu_reader.cc
+++ b/src/traced/probes/ftrace/cpu_reader.cc
@@ -26,9 +26,9 @@
 
 #include "perfetto/base/build_config.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/metatrace.h"
-#include "perfetto/base/optional.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/metatrace.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/traced/probes/ftrace/ftrace_controller.h"
 #include "src/traced/probes/ftrace/ftrace_data_source.h"
 #include "src/traced/probes/ftrace/ftrace_thread_sync.h"
diff --git a/src/traced/probes/ftrace/cpu_reader.h b/src/traced/probes/ftrace/cpu_reader.h
index 4cfe8c8..9ee0a61 100644
--- a/src/traced/probes/ftrace/cpu_reader.h
+++ b/src/traced/probes/ftrace/cpu_reader.h
@@ -26,14 +26,13 @@
 #include <set>
 #include <thread>
 
-#include "perfetto/base/gtest_prod_util.h"
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/base/pipe.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/thread_checker.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/base/pipe.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/traced/data_source_types.h"
 #include "perfetto/protozero/message.h"
 #include "perfetto/protozero/message_handle.h"
-#include "perfetto/traced/data_source_types.h"
 #include "src/traced/probes/ftrace/ftrace_metadata.h"
 #include "src/traced/probes/ftrace/page_pool.h"
 #include "src/traced/probes/ftrace/proto_translation_table.h"
diff --git a/src/traced/probes/ftrace/cpu_reader_benchmark.cc b/src/traced/probes/ftrace/cpu_reader_benchmark.cc
index 363f66d..a366278 100644
--- a/src/traced/probes/ftrace/cpu_reader_benchmark.cc
+++ b/src/traced/probes/ftrace/cpu_reader_benchmark.cc
@@ -12,17 +12,15 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "benchmark/benchmark.h"
+#include <benchmark/benchmark.h>
 
-#include "src/traced/probes/ftrace/cpu_reader.h"
-#include "src/traced/probes/ftrace/proto_translation_table.h"
-
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/scattered_stream_null_delegate.h"
 #include "perfetto/protozero/scattered_stream_writer.h"
-
 #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
-#include "test/cpu_reader_support.h"
+#include "src/traced/probes/ftrace/cpu_reader.h"
+#include "src/traced/probes/ftrace/proto_translation_table.h"
+#include "src/traced/probes/ftrace/test/cpu_reader_support.h"
 
 namespace {
 
@@ -39,7 +37,7 @@
 00000070: 6561 6400 6572 0000 7002 0000 6100 0000  ead.er..p...a...
 00000080: 0100 0000 0000 0000 4a69 7420 7468 7265  ........Jit thre
 00000090: 6164 2070 6f6f 6c00 140d 0000 8100 0000  ad pool.........
-000000a0: 50c2 0910 2f00 0103 140d 0000 4a69 7420  P.../.......Jit 
+000000a0: 50c2 0910 2f00 0103 140d 0000 4a69 7420  P.../.......Jit
 000000b0: 7468 7265 6164 2070 6f6f 6c00 140d 0000  thread pool.....
 000000c0: 8100 0000 0100 0000 0000 0000 7377 6170  ............swap
 000000d0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
@@ -290,17 +288,17 @@
 
 }  // namespace
 
-using perfetto::ExamplePage;
-using perfetto::EventFilter;
-using perfetto::ProtoTranslationTable;
-using protozero::ScatteredStreamWriterNullDelegate;
-using protozero::ScatteredStreamWriter;
-using perfetto::GetTable;
-using perfetto::PageFromXxd;
-using perfetto::protos::pbzero::FtraceEventBundle;
 using perfetto::CpuReader;
+using perfetto::EventFilter;
+using perfetto::ExamplePage;
 using perfetto::FtraceMetadata;
+using perfetto::GetTable;
 using perfetto::GroupAndName;
+using perfetto::PageFromXxd;
+using perfetto::ProtoTranslationTable;
+using perfetto::protos::pbzero::FtraceEventBundle;
+using protozero::ScatteredStreamWriter;
+using protozero::ScatteredStreamWriterNullDelegate;
 
 static void BM_ParsePageFullOfSchedSwitch(benchmark::State& state) {
   const ExamplePage* test_case = &g_full_page_sched_switch;
diff --git a/src/traced/probes/ftrace/cpu_reader_fuzzer.cc b/src/traced/probes/ftrace/cpu_reader_fuzzer.cc
index 0f79442..1b47bc9 100644
--- a/src/traced/probes/ftrace/cpu_reader_fuzzer.cc
+++ b/src/traced/probes/ftrace/cpu_reader_fuzzer.cc
@@ -20,12 +20,12 @@
 #include <algorithm>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/scattered_stream_null_delegate.h"
 #include "perfetto/protozero/scattered_stream_writer.h"
 #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
 #include "src/traced/probes/ftrace/cpu_reader.h"
-#include "test/cpu_reader_support.h"
+#include "src/traced/probes/ftrace/test/cpu_reader_support.h"
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/ftrace/cpu_reader_unittest.cc b/src/traced/probes/ftrace/cpu_reader_unittest.cc
index 0631ecb..2967f01 100644
--- a/src/traced/probes/ftrace/cpu_reader_unittest.cc
+++ b/src/traced/probes/ftrace/cpu_reader_unittest.cc
@@ -18,13 +18,13 @@
 
 #include <sys/stat.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "src/traced/probes/ftrace/event_info.h"
 #include "src/traced/probes/ftrace/proto_translation_table.h"
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/proto_utils.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "perfetto/protozero/scattered_stream_writer.h"
diff --git a/src/traced/probes/ftrace/cpu_stats_parser.cc b/src/traced/probes/ftrace/cpu_stats_parser.cc
index 8048ee1..65745ca 100644
--- a/src/traced/probes/ftrace/cpu_stats_parser.cc
+++ b/src/traced/probes/ftrace/cpu_stats_parser.cc
@@ -16,8 +16,8 @@
 
 #include "src/traced/probes/ftrace/cpu_stats_parser.h"
 
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/string_utils.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/string_utils.h"
 #include "src/traced/probes/ftrace/ftrace_controller.h"
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 #include "src/traced/probes/ftrace/ftrace_stats.h"
diff --git a/src/traced/probes/ftrace/cpu_stats_parser_unittest.cc b/src/traced/probes/ftrace/cpu_stats_parser_unittest.cc
index f016f19..fdcd173 100644
--- a/src/traced/probes/ftrace/cpu_stats_parser_unittest.cc
+++ b/src/traced/probes/ftrace/cpu_stats_parser_unittest.cc
@@ -1,7 +1,7 @@
 #include "src/traced/probes/ftrace/cpu_stats_parser.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 #include "src/traced/probes/ftrace/ftrace_controller.h"
 #include "src/traced/probes/ftrace/ftrace_stats.h"
diff --git a/src/traced/probes/ftrace/event_info_unittest.cc b/src/traced/probes/ftrace/event_info_unittest.cc
index 76d3d78..d43e9f1 100644
--- a/src/traced/probes/ftrace/event_info_unittest.cc
+++ b/src/traced/probes/ftrace/event_info_unittest.cc
@@ -17,7 +17,7 @@
 #include "src/traced/probes/ftrace/event_info.h"
 #include "perfetto/protozero/proto_utils.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/ftrace/format_parser.cc b/src/traced/probes/ftrace/format_parser.cc
index 6cb1ee3..c5ce2c1 100644
--- a/src/traced/probes/ftrace/format_parser.cc
+++ b/src/traced/probes/ftrace/format_parser.cc
@@ -25,8 +25,8 @@
 #include <vector>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/ftrace/format_parser_unittest.cc b/src/traced/probes/ftrace/format_parser_unittest.cc
index 17a8e84..f6566ad 100644
--- a/src/traced/probes/ftrace/format_parser_unittest.cc
+++ b/src/traced/probes/ftrace/format_parser_unittest.cc
@@ -15,8 +15,8 @@
  */
 #include "src/traced/probes/ftrace/format_parser.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/ftrace/ftrace_config_muxer.cc b/src/traced/probes/ftrace/ftrace_config_muxer.cc
index ca4719b..b75b8cb 100644
--- a/src/traced/probes/ftrace/ftrace_config_muxer.cc
+++ b/src/traced/probes/ftrace/ftrace_config_muxer.cc
@@ -23,7 +23,7 @@
 
 #include <algorithm>
 
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/traced/probes/ftrace/atrace_wrapper.h"
 
 namespace perfetto {
diff --git a/src/traced/probes/ftrace/ftrace_config_muxer_unittest.cc b/src/traced/probes/ftrace/ftrace_config_muxer_unittest.cc
index 936d02c..a697207 100644
--- a/src/traced/probes/ftrace/ftrace_config_muxer_unittest.cc
+++ b/src/traced/probes/ftrace/ftrace_config_muxer_unittest.cc
@@ -18,8 +18,8 @@
 
 #include <memory>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "src/traced/probes/ftrace/atrace_wrapper.h"
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 #include "src/traced/probes/ftrace/proto_translation_table.h"
diff --git a/src/traced/probes/ftrace/ftrace_config_unittest.cc b/src/traced/probes/ftrace/ftrace_config_unittest.cc
index ab3cb7b..7a27cd3 100644
--- a/src/traced/probes/ftrace/ftrace_config_unittest.cc
+++ b/src/traced/probes/ftrace/ftrace_config_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/traced/probes/ftrace/ftrace_config_utils.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 using testing::Contains;
 
diff --git a/src/traced/probes/ftrace/ftrace_controller.cc b/src/traced/probes/ftrace/ftrace_controller.cc
index b1b3793..9a82289 100644
--- a/src/traced/probes/ftrace/ftrace_controller.cc
+++ b/src/traced/probes/ftrace/ftrace_controller.cc
@@ -29,11 +29,11 @@
 #include <utility>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/metatrace.h"
-#include "perfetto/base/time.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/metatrace.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/traced/probes/ftrace/cpu_reader.h"
 #include "src/traced/probes/ftrace/cpu_stats_parser.h"
 #include "src/traced/probes/ftrace/event_info.h"
diff --git a/src/traced/probes/ftrace/ftrace_controller.h b/src/traced/probes/ftrace/ftrace_controller.h
index b2af959..63514b7 100644
--- a/src/traced/probes/ftrace/ftrace_controller.h
+++ b/src/traced/probes/ftrace/ftrace_controller.h
@@ -27,11 +27,10 @@
 #include <set>
 #include <string>
 
-#include "perfetto/base/gtest_prod_util.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 #include "src/traced/probes/ftrace/ftrace_config_utils.h"
 #include "src/traced/probes/ftrace/ftrace_thread_sync.h"
 
@@ -100,7 +99,6 @@
 
  private:
   friend class TestFtraceController;
-  FRIEND_TEST(FtraceControllerIntegrationTest, EnableDisableEvent);
 
   FtraceController(const FtraceController&) = delete;
   FtraceController& operator=(const FtraceController&) = delete;
diff --git a/src/traced/probes/ftrace/ftrace_controller_unittest.cc b/src/traced/probes/ftrace/ftrace_controller_unittest.cc
index 82fc6c8..51249ac 100644
--- a/src/traced/probes/ftrace/ftrace_controller_unittest.cc
+++ b/src/traced/probes/ftrace/ftrace_controller_unittest.cc
@@ -20,8 +20,8 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "src/traced/probes/ftrace/cpu_reader.h"
 #include "src/traced/probes/ftrace/ftrace_config_muxer.h"
 #include "src/traced/probes/ftrace/ftrace_config_utils.h"
diff --git a/src/traced/probes/ftrace/ftrace_data_source.h b/src/traced/probes/ftrace/ftrace_data_source.h
index c65c463..04e50f5 100644
--- a/src/traced/probes/ftrace/ftrace_data_source.h
+++ b/src/traced/probes/ftrace/ftrace_data_source.h
@@ -24,11 +24,11 @@
 #include <string>
 #include <utility>
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/weak_ptr.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "perfetto/protozero/message_handle.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/trace_writer.h"
 #include "src/traced/probes/ftrace/ftrace_config_utils.h"
 #include "src/traced/probes/ftrace/ftrace_metadata.h"
 #include "src/traced/probes/ftrace/ftrace_stats.h"
diff --git a/src/traced/probes/ftrace/ftrace_procfs.cc b/src/traced/probes/ftrace/ftrace_procfs.cc
index 47e56e7..060f4cd 100644
--- a/src/traced/probes/ftrace/ftrace_procfs.cc
+++ b/src/traced/probes/ftrace/ftrace_procfs.cc
@@ -25,9 +25,9 @@
 #include <sstream>
 #include <string>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/ftrace/ftrace_procfs.h b/src/traced/probes/ftrace/ftrace_procfs.h
index 2860eae..6066858 100644
--- a/src/traced/probes/ftrace/ftrace_procfs.h
+++ b/src/traced/probes/ftrace/ftrace_procfs.h
@@ -21,7 +21,7 @@
 #include <set>
 #include <string>
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/ftrace/ftrace_procfs_integrationtest.cc b/src/traced/probes/ftrace/ftrace_procfs_integrationtest.cc
index b63bd98..022190a 100644
--- a/src/traced/probes/ftrace/ftrace_procfs_integrationtest.cc
+++ b/src/traced/probes/ftrace/ftrace_procfs_integrationtest.cc
@@ -19,9 +19,9 @@
 #include <sstream>
 #include <string>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/file_utils.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/file_utils.h"
 #include "src/traced/probes/ftrace/ftrace_controller.h"
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 
diff --git a/src/traced/probes/ftrace/ftrace_procfs_unittest.cc b/src/traced/probes/ftrace/ftrace_procfs_unittest.cc
index cac3360..4a10854 100644
--- a/src/traced/probes/ftrace/ftrace_procfs_unittest.cc
+++ b/src/traced/probes/ftrace/ftrace_procfs_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 using testing::AnyNumber;
 using testing::IsEmpty;
diff --git a/src/traced/probes/ftrace/ftrace_thread_sync.h b/src/traced/probes/ftrace/ftrace_thread_sync.h
index b2c4a9a..fbfa3fd 100644
--- a/src/traced/probes/ftrace/ftrace_thread_sync.h
+++ b/src/traced/probes/ftrace/ftrace_thread_sync.h
@@ -23,8 +23,8 @@
 #include <condition_variable>
 #include <mutex>
 
-#include "perfetto/base/utils.h"
-#include "perfetto/base/weak_ptr.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/base/weak_ptr.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/ftrace/page_pool.h b/src/traced/probes/ftrace/page_pool.h
index e558d85..b0b5fc3 100644
--- a/src/traced/probes/ftrace/page_pool.h
+++ b/src/traced/probes/ftrace/page_pool.h
@@ -23,10 +23,10 @@
 #include <vector>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/ftrace/page_pool_unittest.cc b/src/traced/probes/ftrace/page_pool_unittest.cc
index 3e7fbcb..e99b7cb 100644
--- a/src/traced/probes/ftrace/page_pool_unittest.cc
+++ b/src/traced/probes/ftrace/page_pool_unittest.cc
@@ -22,7 +22,7 @@
 #include <thread>
 #include <vector>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/probes/ftrace/proto_translation_table.cc b/src/traced/probes/ftrace/proto_translation_table.cc
index 16b87fd..9c2b8a6 100644
--- a/src/traced/probes/ftrace/proto_translation_table.cc
+++ b/src/traced/probes/ftrace/proto_translation_table.cc
@@ -21,7 +21,7 @@
 
 #include <algorithm>
 
-#include "perfetto/base/string_utils.h"
+#include "perfetto/ext/base/string_utils.h"
 #include "perfetto/protozero/proto_utils.h"
 #include "src/traced/probes/ftrace/event_info.h"
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
@@ -34,8 +34,8 @@
 
 namespace {
 
-using protozero::proto_utils::ProtoSchemaType;
 using protos::pbzero::GenericFtraceEvent;
+using protozero::proto_utils::ProtoSchemaType;
 
 ProtoTranslationTable::FtracePageHeaderSpec MakeFtracePageHeaderSpec(
     const std::vector<FtraceEvent::Field>& fields) {
@@ -74,8 +74,9 @@
   if (commit_size < 8 && uname(&sysinfo) == 0) {
     // Arm returns armv# for its machine type. The first (and only currently)
     // arm processor that supports 64bit is the armv8 series.
-    commit_size = strstr(sysinfo.machine, "64") ||
-                  strstr(sysinfo.machine, "armv8") ? 8 : 4;
+    commit_size =
+        strstr(sysinfo.machine, "64") || strstr(sysinfo.machine, "armv8") ? 8
+                                                                          : 4;
   }
 #endif
 
diff --git a/src/traced/probes/ftrace/proto_translation_table.h b/src/traced/probes/ftrace/proto_translation_table.h
index fe4abde..ea5ff90 100644
--- a/src/traced/probes/ftrace/proto_translation_table.h
+++ b/src/traced/probes/ftrace/proto_translation_table.h
@@ -25,7 +25,7 @@
 #include <string>
 #include <vector>
 
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 #include "src/traced/probes/ftrace/event_info.h"
 #include "src/traced/probes/ftrace/format_parser.h"
 
diff --git a/src/traced/probes/ftrace/proto_translation_table_unittest.cc b/src/traced/probes/ftrace/proto_translation_table_unittest.cc
index 89d6c67..249e0ac 100644
--- a/src/traced/probes/ftrace/proto_translation_table_unittest.cc
+++ b/src/traced/probes/ftrace/proto_translation_table_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/traced/probes/ftrace/proto_translation_table.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "perfetto/trace/ftrace/ftrace_event.pbzero.h"
 #include "perfetto/trace/ftrace/generic.pbzero.h"
 #include "src/base/test/gtest_test_suite.h"
@@ -172,7 +172,6 @@
 	field:u32 field_e;	offset:32;	size:4;	signed:0;
 
 print fmt: "some format")"));
-  ;
 
   EXPECT_CALL(ftrace, ReadPageHeaderFormat()).Times(AnyNumber());
   EXPECT_CALL(ftrace, ReadEventFormat(_, _)).Times(AnyNumber());
diff --git a/src/traced/probes/ftrace/test/cpu_reader_support.cc b/src/traced/probes/ftrace/test/cpu_reader_support.cc
index 4d1ce03..1481b16 100644
--- a/src/traced/probes/ftrace/test/cpu_reader_support.cc
+++ b/src/traced/probes/ftrace/test/cpu_reader_support.cc
@@ -16,7 +16,7 @@
 
 #include "src/traced/probes/ftrace/test/cpu_reader_support.h"
 
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 
 #include <string.h>
diff --git a/src/traced/probes/main.cc b/src/traced/probes/main.cc
index 71b9d4e..a91a6b6 100644
--- a/src/traced/probes/main.cc
+++ b/src/traced/probes/main.cc
@@ -15,7 +15,7 @@
  */
 
 #include <stdio.h>
-#include "perfetto/traced/traced.h"
+#include "perfetto/ext/traced/traced.h"
 
 int main(int argc, char** argv) {
   return perfetto::ProbesMain(argc, argv);
diff --git a/src/traced/probes/packages_list/BUILD.gn b/src/traced/probes/packages_list/BUILD.gn
index d896aef..1a9653b 100644
--- a/src/traced/probes/packages_list/BUILD.gn
+++ b/src/traced/probes/packages_list/BUILD.gn
@@ -19,7 +19,7 @@
   deps = [
     "..:data_source",
     "../../../../gn:default_deps",
-    "../../../../include/perfetto/traced",
+    "../../../../include/perfetto/ext/traced",
     "../../../../protos/perfetto/common:zero",
     "../../../../protos/perfetto/trace/android:zero",
     "../../../base",
diff --git a/src/traced/probes/packages_list/packages_list_data_source.cc b/src/traced/probes/packages_list/packages_list_data_source.cc
index 493fdae..d420dea 100644
--- a/src/traced/probes/packages_list/packages_list_data_source.cc
+++ b/src/traced/probes/packages_list/packages_list_data_source.cc
@@ -16,11 +16,11 @@
 
 #include "src/traced/probes/packages_list/packages_list_data_source.h"
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/string_splitter.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/string_splitter.h"
 
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
-#include "perfetto/tracing/core/trace_writer.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/packages_list/packages_list_data_source.h b/src/traced/probes/packages_list/packages_list_data_source.h
index e6026ae..430d433 100644
--- a/src/traced/probes/packages_list/packages_list_data_source.h
+++ b/src/traced/probes/packages_list/packages_list_data_source.h
@@ -22,9 +22,9 @@
 
 #include "perfetto/base/task_runner.h"
 
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
 #include "perfetto/trace/android/packages_list.pbzero.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/data_source_config.h"
 
 #include "src/traced/probes/probes_data_source.h"
 
diff --git a/src/traced/probes/power/BUILD.gn b/src/traced/probes/power/BUILD.gn
index e8dc445..bfdb35c 100644
--- a/src/traced/probes/power/BUILD.gn
+++ b/src/traced/probes/power/BUILD.gn
@@ -20,7 +20,7 @@
   deps = [
     "..:data_source",
     "../../../../gn:default_deps",
-    "../../../../include/perfetto/traced",
+    "../../../../include/perfetto/ext/traced",
     "../../../../protos/perfetto/config:zero",
     "../../../../protos/perfetto/trace/power:zero",
     "../../../android_internal:headers",
diff --git a/src/traced/probes/power/android_power_data_source.cc b/src/traced/probes/power/android_power_data_source.cc
index a1899f5..b1f8197 100644
--- a/src/traced/probes/power/android_power_data_source.cc
+++ b/src/traced/probes/power/android_power_data_source.cc
@@ -21,13 +21,13 @@
 #include <vector>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
-#include "perfetto/base/scoped_file.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/time.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/android_internal/health_hal.h"
 #include "src/android_internal/power_stats_hal.h"
 
diff --git a/src/traced/probes/power/android_power_data_source.h b/src/traced/probes/power/android_power_data_source.h
index b3b45e8..65e4910 100644
--- a/src/traced/probes/power/android_power_data_source.h
+++ b/src/traced/probes/power/android_power_data_source.h
@@ -20,9 +20,9 @@
 #include <bitset>
 #include <memory>
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/data_source_config.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
 #include "src/traced/probes/probes_data_source.h"
 
 namespace perfetto {
diff --git a/src/traced/probes/probes.cc b/src/traced/probes/probes.cc
index 4920138..0d941b4 100644
--- a/src/traced/probes/probes.cc
+++ b/src/traced/probes/probes.cc
@@ -20,8 +20,8 @@
 #include <unistd.h>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/unix_task_runner.h"
-#include "perfetto/traced/traced.h"
+#include "perfetto/ext/base/unix_task_runner.h"
+#include "perfetto/ext/traced/traced.h"
 
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 #include "src/traced/probes/probes_producer.h"
diff --git a/src/traced/probes/probes_data_source.h b/src/traced/probes/probes_data_source.h
index 7c92ede..ef410c7 100644
--- a/src/traced/probes/probes_data_source.h
+++ b/src/traced/probes/probes_data_source.h
@@ -20,7 +20,7 @@
 #include <functional>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 
 namespace perfetto {
 
diff --git a/src/traced/probes/probes_producer.cc b/src/traced/probes/probes_producer.cc
index 87e688d..9e4596c 100644
--- a/src/traced/probes/probes_producer.cc
+++ b/src/traced/probes/probes_producer.cc
@@ -24,14 +24,14 @@
 #include <string>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/traced/traced.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/ipc/producer_ipc_client.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/traced/traced.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
 #include "src/traced/probes/android_log/android_log_data_source.h"
 #include "src/traced/probes/filesystem/inode_file_data_source.h"
 #include "src/traced/probes/ftrace/ftrace_config.h"
diff --git a/src/traced/probes/probes_producer.h b/src/traced/probes/probes_producer.h
index 2184830..9d28c8a 100644
--- a/src/traced/probes/probes_producer.h
+++ b/src/traced/probes/probes_producer.h
@@ -22,11 +22,11 @@
 #include <utility>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/watchdog.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/trace_writer.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/base/watchdog.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 #include "src/traced/probes/filesystem/inode_file_data_source.h"
 #include "src/traced/probes/ftrace/ftrace_controller.h"
 #include "src/traced/probes/ftrace/ftrace_metadata.h"
diff --git a/src/traced/probes/ps/BUILD.gn b/src/traced/probes/ps/BUILD.gn
index 9be871a..fa35b43 100644
--- a/src/traced/probes/ps/BUILD.gn
+++ b/src/traced/probes/ps/BUILD.gn
@@ -19,7 +19,7 @@
   deps = [
     "..:data_source",
     "../../../../gn:default_deps",
-    "../../../../include/perfetto/traced",
+    "../../../../include/perfetto/ext/traced",
     "../../../../protos/perfetto/config:zero",
     "../../../../protos/perfetto/trace/ps:zero",
     "../../../base",
diff --git a/src/traced/probes/ps/process_stats_data_source.cc b/src/traced/probes/ps/process_stats_data_source.cc
index ec18775..86265ed 100644
--- a/src/traced/probes/ps/process_stats_data_source.cc
+++ b/src/traced/probes/ps/process_stats_data_source.cc
@@ -21,13 +21,13 @@
 #include <algorithm>
 #include <utility>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/metatrace.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/string_splitter.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/time.h"
-#include "perfetto/tracing/core/data_source_config.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/metatrace.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
 
 #include "perfetto/config/process_stats/process_stats_config.pbzero.h"
 #include "perfetto/trace/ps/process_stats.pbzero.h"
@@ -140,6 +140,9 @@
 void ProcessStatsDataSource::WriteAllProcesses() {
   PERFETTO_METATRACE("WriteAllProcesses", 0);
   PERFETTO_DCHECK(!cur_ps_tree_);
+
+  CacheProcFsScanStartTimestamp();
+
   base::ScopedDir proc_dir = OpenProcDir();
   if (!proc_dir)
     return;
@@ -203,6 +206,9 @@
 }
 
 void ProcessStatsDataSource::WriteProcessOrThread(int32_t pid) {
+  // In case we're called from outside WriteAllProcesses()
+  CacheProcFsScanStartTimestamp();
+
   std::string proc_status = ReadProcPidFile(pid, "status");
   if (proc_status.empty())
     return;
@@ -284,8 +290,7 @@
   if (cur_packet_)
     return;
   cur_packet_ = writer_->NewTracePacket();
-  uint64_t now = static_cast<uint64_t>(base::GetBootTimeNs().count());
-  cur_packet_->set_timestamp(now);
+  cur_packet_->set_timestamp(CacheProcFsScanStartTimestamp());
 
   if (did_clear_incremental_state_) {
     cur_packet_->set_incremental_state_cleared(true);
@@ -323,9 +328,17 @@
 void ProcessStatsDataSource::FinalizeCurPacket() {
   PERFETTO_DCHECK(!cur_ps_tree_ || cur_packet_);
   PERFETTO_DCHECK(!cur_ps_stats_ || cur_packet_);
-  cur_ps_tree_ = nullptr;
-  cur_ps_stats_ = nullptr;
+  uint64_t now = static_cast<uint64_t>(base::GetBootTimeNs().count());
+  if (cur_ps_tree_) {
+    cur_ps_tree_->set_collection_end_timestamp(now);
+    cur_ps_tree_ = nullptr;
+  }
+  if (cur_ps_stats_) {
+    cur_ps_stats_->set_collection_end_timestamp(now);
+    cur_ps_stats_ = nullptr;
+  }
   cur_ps_stats_process_ = nullptr;
+  cur_procfs_scan_start_timestamp_ = 0;
   cur_packet_ = TraceWriter::TracePacketHandle{};
 }
 
@@ -353,6 +366,7 @@
   // TODO(primiano): Have a pid cache to avoid wasting cycles reading kthreads
   // proc files over and over. Same for non-whitelist processes (see above).
 
+  CacheProcFsScanStartTimestamp();
   PERFETTO_METATRACE("WriteAllProcessStats", 0);
   base::ScopedDir proc_dir = OpenProcDir();
   if (!proc_dir)
@@ -509,6 +523,13 @@
   return proc_status_has_mem_counters;
 }
 
+uint64_t ProcessStatsDataSource::CacheProcFsScanStartTimestamp() {
+  if (!cur_procfs_scan_start_timestamp_)
+    cur_procfs_scan_start_timestamp_ =
+        static_cast<uint64_t>(base::GetBootTimeNs().count());
+  return cur_procfs_scan_start_timestamp_;
+}
+
 void ProcessStatsDataSource::ClearIncrementalState() {
   PERFETTO_DLOG("ProcessStatsDataSource clearing incremental state.");
   seen_pids_.clear();
diff --git a/src/traced/probes/ps/process_stats_data_source.h b/src/traced/probes/ps/process_stats_data_source.h
index 4566ee1..b2fa51c 100644
--- a/src/traced/probes/ps/process_stats_data_source.h
+++ b/src/traced/probes/ps/process_stats_data_source.h
@@ -23,10 +23,10 @@
 #include <unordered_map>
 #include <vector>
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/traced/probes/probes_data_source.h"
 
 namespace perfetto {
@@ -105,11 +105,26 @@
   void WriteAllProcessStats();
   bool WriteMemCounters(int32_t pid, const std::string& proc_status);
 
+  // Read and "latch" the current procfs scan-start timestamp, which
+  // we reset only in FinalizeCurPacket.
+  uint64_t CacheProcFsScanStartTimestamp();
+
   // Common fields used for both process/tree relationships and stats/counters.
   base::TaskRunner* const task_runner_;
   std::unique_ptr<TraceWriter> writer_;
   TraceWriter::TracePacketHandle cur_packet_;
 
+  // Cached before-scan timestamp; zero means cached time is absent.
+  // By the time we create the trace packet into which we dump procfs
+  // scan results, we've already read at least one bit of data from
+  // procfs, and by that point, it's too late to snap a timestamp from
+  // before we started looking at procfs at all, which is what trace
+  // analysis wants.  To solve this problem, we record the scan-start
+  // timestamp here when we first open something in procfs and use
+  // that time when we create the packet.
+  // We reset this field after each FinalizeCurPacket().
+  uint64_t cur_procfs_scan_start_timestamp_ = 0;
+
   // Fields for keeping track of the state of process/tree relationships.
   protos::pbzero::ProcessTree* cur_ps_tree_ = nullptr;
   bool record_thread_names_ = false;
diff --git a/src/traced/probes/ps/process_stats_data_source_unittest.cc b/src/traced/probes/ps/process_stats_data_source_unittest.cc
index 7ef9e12..2ece128 100644
--- a/src/traced/probes/ps/process_stats_data_source_unittest.cc
+++ b/src/traced/probes/ps/process_stats_data_source_unittest.cc
@@ -18,11 +18,11 @@
 
 #include <dirent.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/temp_file.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
-#include "perfetto/tracing/core/data_source_config.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/tracing/core/trace_writer_for_testing.h"
 
diff --git a/src/traced/probes/sys_stats/BUILD.gn b/src/traced/probes/sys_stats/BUILD.gn
index ee6d4d4..2267e73 100644
--- a/src/traced/probes/sys_stats/BUILD.gn
+++ b/src/traced/probes/sys_stats/BUILD.gn
@@ -19,8 +19,8 @@
   deps = [
     "..:data_source",
     "../../../../gn:default_deps",
-    "../../../../include/perfetto/traced",
-    "../../../../include/perfetto/traced:sys_stats_counters",
+    "../../../../include/perfetto/ext/traced",
+    "../../../../include/perfetto/ext/traced:sys_stats_counters",
     "../../../../protos/perfetto/common:zero",
     "../../../../protos/perfetto/config:zero",
     "../../../../protos/perfetto/trace/sys_stats:zero",
diff --git a/src/traced/probes/sys_stats/sys_stats_data_source.cc b/src/traced/probes/sys_stats/sys_stats_data_source.cc
index 3c60854..ee0de79 100644
--- a/src/traced/probes/sys_stats/sys_stats_data_source.cc
+++ b/src/traced/probes/sys_stats/sys_stats_data_source.cc
@@ -24,14 +24,14 @@
 #include <limits>
 #include <utility>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/metatrace.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/string_splitter.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/time.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/traced/sys_stats_counters.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/metatrace.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/traced/sys_stats_counters.h"
 
 #include "perfetto/common/sys_stats_counters.pbzero.h"
 #include "perfetto/config/sys_stats/sys_stats_config.pbzero.h"
@@ -175,6 +175,9 @@
   if (stat_ticks_ && tick_ % stat_ticks_ == 0)
     ReadStat(sys_stats);
 
+  sys_stats->set_collection_end_timestamp(
+      static_cast<uint64_t>(base::GetBootTimeNs().count()));
+
   tick_++;
 }
 
diff --git a/src/traced/probes/sys_stats/sys_stats_data_source.h b/src/traced/probes/sys_stats/sys_stats_data_source.h
index 0fa085c..7510228 100644
--- a/src/traced/probes/sys_stats/sys_stats_data_source.h
+++ b/src/traced/probes/sys_stats/sys_stats_data_source.h
@@ -23,12 +23,12 @@
 #include <memory>
 #include <string>
 
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/traced/probes/probes_data_source.h"
 
 namespace perfetto {
diff --git a/src/traced/probes/sys_stats/sys_stats_data_source_unittest.cc b/src/traced/probes/sys_stats/sys_stats_data_source_unittest.cc
index fef5393..59de74e 100644
--- a/src/traced/probes/sys_stats/sys_stats_data_source_unittest.cc
+++ b/src/traced/probes/sys_stats/sys_stats_data_source_unittest.cc
@@ -16,9 +16,9 @@
 
 #include <unistd.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/temp_file.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/temp_file.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/traced/probes/sys_stats/sys_stats_data_source.h"
diff --git a/src/traced/service/BUILD.gn b/src/traced/service/BUILD.gn
index f351f0e..e19801a 100644
--- a/src/traced/service/BUILD.gn
+++ b/src/traced/service/BUILD.gn
@@ -14,7 +14,7 @@
 
 source_set("service") {
   public_deps = [
-    "../../../include/perfetto/traced",
+    "../../../include/perfetto/ext/traced",
   ]
   deps = [
     "../../../gn:default_deps",
diff --git a/src/traced/service/lazy_producer.cc b/src/traced/service/lazy_producer.cc
index d63bdd4..e040bc0 100644
--- a/src/traced/service/lazy_producer.cc
+++ b/src/traced/service/lazy_producer.cc
@@ -18,8 +18,8 @@
 
 #include "perfetto/base/build_config.h"
 
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
 #include <sys/system_properties.h>
diff --git a/src/traced/service/lazy_producer.h b/src/traced/service/lazy_producer.h
index 541b5ba..200e7fa 100644
--- a/src/traced/service/lazy_producer.h
+++ b/src/traced/service/lazy_producer.h
@@ -21,11 +21,11 @@
 #include <string>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/weak_ptr.h"
+#include "perfetto/ext/base/weak_ptr.h"
 
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 namespace perfetto {
 
diff --git a/src/traced/service/lazy_producer_unittest.cc b/src/traced/service/lazy_producer_unittest.cc
index 7707a99..69e6342 100644
--- a/src/traced/service/lazy_producer_unittest.cc
+++ b/src/traced/service/lazy_producer_unittest.cc
@@ -18,10 +18,10 @@
 
 #include "src/base/test/test_task_runner.h"
 
-#include "perfetto/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace {
diff --git a/src/traced/service/main.cc b/src/traced/service/main.cc
index 552ae6b..fe81b4b 100644
--- a/src/traced/service/main.cc
+++ b/src/traced/service/main.cc
@@ -15,7 +15,7 @@
  */
 
 #include <stdio.h>
-#include "perfetto/traced/traced.h"
+#include "perfetto/ext/traced/traced.h"
 
 int main(int argc, char** argv) {
   return perfetto::ServiceMain(argc, argv);
diff --git a/src/traced/service/service.cc b/src/traced/service/service.cc
index c52b246..b894fdd 100644
--- a/src/traced/service/service.cc
+++ b/src/traced/service/service.cc
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include "perfetto/base/unix_task_runner.h"
-#include "perfetto/base/watchdog.h"
-#include "perfetto/traced/traced.h"
-#include "perfetto/tracing/ipc/service_ipc_host.h"
+#include "perfetto/ext/base/unix_task_runner.h"
+#include "perfetto/ext/base/watchdog.h"
+#include "perfetto/ext/traced/traced.h"
+#include "perfetto/ext/tracing/ipc/service_ipc_host.h"
 #include "src/traced/service/lazy_producer.h"
 #include "src/tracing/ipc/default_socket.h"
 
diff --git a/src/tracing/BUILD.gn b/src/tracing/BUILD.gn
index 61aab98..0ac05d4 100644
--- a/src/tracing/BUILD.gn
+++ b/src/tracing/BUILD.gn
@@ -18,17 +18,16 @@
 # Core tracing library, platform independent, no transport layer.
 source_set("tracing") {
   public_deps = [
-    "../../include/perfetto/tracing/core",
+    ":common",
+    "../../include/perfetto/ext/tracing/core",
     "../../protos/perfetto/common:lite",
     "../../protos/perfetto/trace:minimal_lite",
     "../../protos/perfetto/trace:trusted_lite",
     "../../protos/perfetto/trace:zero",
-    "../public:common",
   ]
   deps = [
     "../../gn:default_deps",
-    "../../gn:gtest_prod_config",
-    "../../include/perfetto/public",
+    "../../include/perfetto/tracing",
     "../../protos/perfetto/config:lite",
     "../base",
     "../protozero",
@@ -128,7 +127,7 @@
 source_set("test_support") {
   testonly = true
   public_deps = [
-    "../../include/perfetto/tracing/core",
+    "../../include/perfetto/ext/tracing/core",
     "../../protos/perfetto/trace:lite",
     "../../protos/perfetto/trace:zero",
     "../protozero",
@@ -142,8 +141,9 @@
 if (perfetto_build_standalone || perfetto_build_with_android) {
   executable("consumer_api_test") {
     deps = [
-      ":api",
+      ":consumer_api_deprecated",
       "../../gn:default_deps",
+      "../../include/perfetto/public",
       "../../protos/perfetto/config:lite",
       "../../protos/perfetto/trace:lite",
       "../base",
@@ -166,11 +166,14 @@
 
   # Imlementation of the public-facing consumer API in libperfetto.so (only for
   # Android builds).
-  source_set("api") {
+  # TODO(primiano): remove this. This is a legacy and deprecated API. The only
+  # uses should be moved to the perfetto Client API.
+  source_set("consumer_api_deprecated") {
     deps = [
       ":ipc",
       ":tracing",
       "../../gn:default_deps",
+      "../../include/perfetto/public",
       "../../protos/perfetto/config:lite",
       "../base",
     ]
@@ -185,8 +188,8 @@
   # Provides an IPC transport over a UNIX domain socket.
   static_library("ipc") {
     public_deps = [
-      "../../include/perfetto/tracing/core",
-      "../../include/perfetto/tracing/ipc",
+      "../../include/perfetto/ext/tracing/core",
+      "../../include/perfetto/ext/tracing/ipc",
     ]
     sources = [
       "ipc/consumer/consumer_ipc_client_impl.cc",
@@ -213,3 +216,83 @@
     ]
   }
 }
+
+if (perfetto_build_with_ipc_layer) {
+  source_set("client_api") {
+    deps = [
+      ":common",
+      "../../gn:default_deps",
+      "../base",
+      "../tracing",
+    ]
+    public_deps = [
+      "../../include/perfetto/tracing",
+    ]
+    sources = [
+      "data_source.cc",
+      "internal/in_process_tracing_backend.cc",
+      "internal/in_process_tracing_backend.h",
+      "internal/tracing_muxer_impl.cc",
+      "platform.cc",
+      "tracing.cc",
+      "virtual_destructors.cc",
+    ]
+
+    # TODO there should be an option to compile this out. This pulls IPC
+    # dependencies and bloats binary size.
+    deps += [ "../tracing:ipc" ]
+    sources += [
+      "internal/system_tracing_backend.cc",
+      "internal/system_tracing_backend.h",
+    ]
+  }
+}
+
+# Separate target because the embedder might not want this (e.g. on Windows).
+if (is_linux || is_mac || is_android) {
+  source_set("platform_posix") {
+    deps = [
+      "../../gn:default_deps",
+      "../../include/perfetto/tracing",
+      "../base",
+    ]
+    sources = [
+      "platform_posix.cc",
+    ]
+  }
+}
+
+# Code that both public headers and other non-public sources (e.g.
+# src/tracing/core) need to depend on. It cannot be in the root :tracing target
+# otherwise there would be a cyclic dependency because public itself needs to
+# depend on tracing.
+source_set("common") {
+  deps = [
+    "../../gn:default_deps",
+    "../../include/perfetto/tracing",
+  ]
+  sources = [
+    "trace_writer_base.cc",
+  ]
+}
+
+if (!perfetto_build_with_embedder) {
+  source_set("client_api_integrationtests") {
+    testonly = true
+    deps = [
+      ":client_api",
+      ":platform_posix",
+      "../../gn:default_deps",
+      "../../gn:gtest_deps",
+      "../../protos/perfetto/trace:lite",
+      "../../protos/perfetto/trace:zero",
+      "../base",
+
+      # TODO(primiano): move DataSourceConfig to /include/ and remove this.
+      "../../include/perfetto/ext/tracing/core",
+    ]
+    sources = [
+      "api_integrationtest.cc",
+    ]
+  }
+}
diff --git a/src/tracing/api_impl/consumer_api.cc b/src/tracing/api_impl/consumer_api.cc
index 286b6b4..2436b4f 100644
--- a/src/tracing/api_impl/consumer_api.cc
+++ b/src/tracing/api_impl/consumer_api.cc
@@ -27,21 +27,22 @@
 #include <unistd.h>
 
 #include <atomic>
+#include <condition_variable>
 #include <memory>
 #include <mutex>
 #include <thread>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/event.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/base/unix_task_runner.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/ipc/consumer_ipc_client.h"
+#include "perfetto/ext/base/event.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/base/unix_task_runner.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
 #include "src/tracing/ipc/default_socket.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
@@ -350,7 +351,7 @@
   if (it == sessions_.end()) {
     PERFETTO_ELOG("StartTracing(): Invalid tracing session handle");
     return;
-  };
+  }
   TracingSession* session = it->second.get();
   task_runner_->PostTask([session] { session->StartTracing(); });
 }
diff --git a/src/public/api_integrationtest.cc b/src/tracing/api_integrationtest.cc
similarity index 97%
rename from src/public/api_integrationtest.cc
rename to src/tracing/api_integrationtest.cc
index 95986da..d484140 100644
--- a/src/public/api_integrationtest.cc
+++ b/src/tracing/api_integrationtest.cc
@@ -21,29 +21,29 @@
 #include <mutex>
 #include <vector>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
-#include "perfetto/public/tracing.h"
 #include "perfetto/trace/test_event.pbzero.h"
 #include "perfetto/trace/trace.pb.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
+#include "perfetto/tracing.h"
 
 // Deliberately not pulling any non-public perfetto header to spot accidental
 // header public -> non-public dependency while building this file.
 
 // TODO(primiano): move these generated classes to /public/.
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
 
 namespace {
 
 using ::testing::_;
-using ::testing::StrEq;
-using ::testing::Property;
-using ::testing::NiceMock;
 using ::testing::Invoke;
 using ::testing::InvokeWithoutArgs;
+using ::testing::NiceMock;
+using ::testing::Property;
+using ::testing::StrEq;
 
 // ------------------------------
 // Declarations of helper classes
diff --git a/src/tracing/core/chrome_config.cc b/src/tracing/core/chrome_config.cc
index 2603ffc..ba03651 100644
--- a/src/tracing/core/chrome_config.cc
+++ b/src/tracing/core/chrome_config.cc
@@ -25,7 +25,7 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#include "perfetto/tracing/core/chrome_config.h"
+#include "perfetto/ext/tracing/core/chrome_config.h"
 
 #include "perfetto/config/chrome/chrome_config.pb.h"
 
diff --git a/src/tracing/core/commit_data_request.cc b/src/tracing/core/commit_data_request.cc
index d83cefa..5c123f7 100644
--- a/src/tracing/core/commit_data_request.cc
+++ b/src/tracing/core/commit_data_request.cc
@@ -25,7 +25,7 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#include "perfetto/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
 
 #include "perfetto/common/commit_data_request.pb.h"
 
diff --git a/src/tracing/core/data_source_config.cc b/src/tracing/core/data_source_config.cc
index d5f20dd..d71ddba 100644
--- a/src/tracing/core/data_source_config.cc
+++ b/src/tracing/core/data_source_config.cc
@@ -25,7 +25,7 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#include "perfetto/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
 
 #include "perfetto/config/chrome/chrome_config.pb.h"
 #include "perfetto/config/data_source_config.pb.h"
diff --git a/src/tracing/core/data_source_descriptor.cc b/src/tracing/core/data_source_descriptor.cc
index 23edcbc..9c2ccb2 100644
--- a/src/tracing/core/data_source_descriptor.cc
+++ b/src/tracing/core/data_source_descriptor.cc
@@ -25,7 +25,7 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#include "perfetto/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
 
 #include "perfetto/config/data_source_descriptor.pb.h"
 
diff --git a/src/tracing/core/id_allocator_unittest.cc b/src/tracing/core/id_allocator_unittest.cc
index 53b8034..1eac8a2 100644
--- a/src/tracing/core/id_allocator_unittest.cc
+++ b/src/tracing/core/id_allocator_unittest.cc
@@ -16,7 +16,7 @@
 
 #include "src/tracing/core/id_allocator.h"
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace {
diff --git a/src/tracing/core/null_trace_writer.cc b/src/tracing/core/null_trace_writer.cc
index 5ca2e71..0573fca 100644
--- a/src/tracing/core/null_trace_writer.cc
+++ b/src/tracing/core/null_trace_writer.cc
@@ -17,7 +17,7 @@
 #include "src/tracing/core/null_trace_writer.h"
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 
 #include "perfetto/protozero/message.h"
 
diff --git a/src/tracing/core/null_trace_writer.h b/src/tracing/core/null_trace_writer.h
index a6a81e2..fabbca7 100644
--- a/src/tracing/core/null_trace_writer.h
+++ b/src/tracing/core/null_trace_writer.h
@@ -17,9 +17,9 @@
 #ifndef SRC_TRACING_CORE_NULL_TRACE_WRITER_H_
 #define SRC_TRACING_CORE_NULL_TRACE_WRITER_H_
 
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "perfetto/protozero/message_handle.h"
 #include "perfetto/protozero/scattered_stream_null_delegate.h"
-#include "perfetto/tracing/core/trace_writer.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/core/null_trace_writer_unittest.cc b/src/tracing/core/null_trace_writer_unittest.cc
index f2d2f54..b63c54d 100644
--- a/src/tracing/core/null_trace_writer_unittest.cc
+++ b/src/tracing/core/null_trace_writer_unittest.cc
@@ -16,10 +16,10 @@
 
 #include "src/tracing/core/null_trace_writer.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 
 #include "perfetto/trace/test_event.pbzero.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
diff --git a/src/tracing/core/observable_events.cc b/src/tracing/core/observable_events.cc
index e43489e..82bc5b2 100644
--- a/src/tracing/core/observable_events.cc
+++ b/src/tracing/core/observable_events.cc
@@ -25,7 +25,7 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#include "perfetto/tracing/core/observable_events.h"
+#include "perfetto/ext/tracing/core/observable_events.h"
 
 #include "perfetto/common/observable_events.pb.h"
 
diff --git a/src/tracing/core/packet_stream_validator_unittest.cc b/src/tracing/core/packet_stream_validator_unittest.cc
index 03f15f4..d1ff038 100644
--- a/src/tracing/core/packet_stream_validator_unittest.cc
+++ b/src/tracing/core/packet_stream_validator_unittest.cc
@@ -18,7 +18,7 @@
 
 #include <string>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 #include "perfetto/trace/trace_packet.pb.h"
 
diff --git a/src/tracing/core/patch_list.h b/src/tracing/core/patch_list.h
index c356006..f4a1618 100644
--- a/src/tracing/core/patch_list.h
+++ b/src/tracing/core/patch_list.h
@@ -21,8 +21,8 @@
 #include <forward_list>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/core/patch_list_unittest.cc b/src/tracing/core/patch_list_unittest.cc
index 99609b2..7112740 100644
--- a/src/tracing/core/patch_list_unittest.cc
+++ b/src/tracing/core/patch_list_unittest.cc
@@ -18,8 +18,8 @@
 
 #include <ostream>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 using testing::ElementsAre;
 
diff --git a/src/tracing/core/shared_memory_abi.cc b/src/tracing/core/shared_memory_abi.cc
index 0b82dfc..df3a631 100644
--- a/src/tracing/core/shared_memory_abi.cc
+++ b/src/tracing/core/shared_memory_abi.cc
@@ -13,17 +13,17 @@
  * express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
-#include "perfetto/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/time.h"
 
 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 #include <sys/mman.h>
 #endif
 
-#include "perfetto/base/utils.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/core/shared_memory_abi_unittest.cc b/src/tracing/core/shared_memory_abi_unittest.cc
index 0ce7a02..baa09f9 100644
--- a/src/tracing/core/shared_memory_abi_unittest.cc
+++ b/src/tracing/core/shared_memory_abi_unittest.cc
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include "perfetto/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
 
-#include "gtest/gtest.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/tracing/core/basic_types.h"
 #include "src/base/test/gtest_test_suite.h"
 #include "src/tracing/test/aligned_buffer_test.h"
 
diff --git a/src/tracing/core/shared_memory_arbiter_impl.cc b/src/tracing/core/shared_memory_arbiter_impl.cc
index 0131097..8766894 100644
--- a/src/tracing/core/shared_memory_arbiter_impl.cc
+++ b/src/tracing/core/shared_memory_arbiter_impl.cc
@@ -18,10 +18,10 @@
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/time.h"
-#include "perfetto/tracing/core/commit_data_request.h"
-#include "perfetto/tracing/core/shared_memory.h"
-#include "perfetto/tracing/core/startup_trace_writer_registry.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
+#include "perfetto/ext/tracing/core/startup_trace_writer_registry.h"
 #include "src/tracing/core/null_trace_writer.h"
 #include "src/tracing/core/trace_writer_impl.h"
 
diff --git a/src/tracing/core/shared_memory_arbiter_impl.h b/src/tracing/core/shared_memory_arbiter_impl.h
index c92b887..391e37c 100644
--- a/src/tracing/core/shared_memory_arbiter_impl.h
+++ b/src/tracing/core/shared_memory_arbiter_impl.h
@@ -24,11 +24,11 @@
 #include <mutex>
 #include <vector>
 
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/shared_memory_arbiter.h"
-#include "perfetto/tracing/core/startup_trace_writer_registry.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/shared_memory_arbiter.h"
+#include "perfetto/ext/tracing/core/startup_trace_writer_registry.h"
 #include "src/tracing/core/id_allocator.h"
 
 namespace perfetto {
diff --git a/src/tracing/core/shared_memory_arbiter_impl_unittest.cc b/src/tracing/core/shared_memory_arbiter_impl_unittest.cc
index 90682dc..a5de044 100644
--- a/src/tracing/core/shared_memory_arbiter_impl_unittest.cc
+++ b/src/tracing/core/shared_memory_arbiter_impl_unittest.cc
@@ -16,13 +16,13 @@
 
 #include "src/tracing/core/shared_memory_arbiter_impl.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/commit_data_request.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/base/test/gtest_test_suite.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/tracing/core/patch_list.h"
diff --git a/src/tracing/core/sliced_protobuf_input_stream.h b/src/tracing/core/sliced_protobuf_input_stream.h
index 9399528..9e80966 100644
--- a/src/tracing/core/sliced_protobuf_input_stream.h
+++ b/src/tracing/core/sliced_protobuf_input_stream.h
@@ -17,7 +17,7 @@
 #ifndef SRC_TRACING_CORE_SLICED_PROTOBUF_INPUT_STREAM_H_
 #define SRC_TRACING_CORE_SLICED_PROTOBUF_INPUT_STREAM_H_
 
-#include "perfetto/tracing/core/slice.h"
+#include "perfetto/ext/tracing/core/slice.h"
 
 #include <stdint.h>
 
diff --git a/src/tracing/core/sliced_protobuf_input_stream_unittest.cc b/src/tracing/core/sliced_protobuf_input_stream_unittest.cc
index 076afc1..c7ae94c 100644
--- a/src/tracing/core/sliced_protobuf_input_stream_unittest.cc
+++ b/src/tracing/core/sliced_protobuf_input_stream_unittest.cc
@@ -16,8 +16,8 @@
 
 #include "src/tracing/core/sliced_protobuf_input_stream.h"
 
-#include "gtest/gtest.h"
-#include "perfetto/base/utils.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace {
diff --git a/src/tracing/core/startup_trace_writer.cc b/src/tracing/core/startup_trace_writer.cc
index e1c841c..ff5f8fd 100644
--- a/src/tracing/core/startup_trace_writer.cc
+++ b/src/tracing/core/startup_trace_writer.cc
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#include "perfetto/tracing/core/startup_trace_writer.h"
+#include "perfetto/ext/tracing/core/startup_trace_writer.h"
 
 #include <numeric>
 
 #include "perfetto/base/logging.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/startup_trace_writer_registry.h"
 #include "perfetto/protozero/proto_utils.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/startup_trace_writer_registry.h"
 #include "src/tracing/core/patch_list.h"
 #include "src/tracing/core/shared_memory_arbiter_impl.h"
 
diff --git a/src/tracing/core/startup_trace_writer_registry.cc b/src/tracing/core/startup_trace_writer_registry.cc
index 30d904f..dd85768 100644
--- a/src/tracing/core/startup_trace_writer_registry.cc
+++ b/src/tracing/core/startup_trace_writer_registry.cc
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#include "perfetto/tracing/core/startup_trace_writer_registry.h"
+#include "perfetto/ext/tracing/core/startup_trace_writer_registry.h"
 
 #include <functional>
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/tracing/core/startup_trace_writer.h"
+#include "perfetto/ext/tracing/core/startup_trace_writer.h"
 #include "src/tracing/core/shared_memory_arbiter_impl.h"
 
 using ChunkHeader = perfetto::SharedMemoryABI::ChunkHeader;
diff --git a/src/tracing/core/startup_trace_writer_unittest.cc b/src/tracing/core/startup_trace_writer_unittest.cc
index 48cdef7..2a3476a 100644
--- a/src/tracing/core/startup_trace_writer_unittest.cc
+++ b/src/tracing/core/startup_trace_writer_unittest.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "perfetto/tracing/core/startup_trace_writer.h"
+#include "perfetto/ext/tracing/core/startup_trace_writer.h"
 
-#include "gtest/gtest.h"
-#include "perfetto/tracing/core/startup_trace_writer_registry.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/tracing/core/startup_trace_writer_registry.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 #include "src/base/test/gtest_test_suite.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/tracing/core/shared_memory_arbiter_impl.h"
diff --git a/src/tracing/core/test_config.cc b/src/tracing/core/test_config.cc
index 02ba732..ee5e7ac 100644
--- a/src/tracing/core/test_config.cc
+++ b/src/tracing/core/test_config.cc
@@ -25,7 +25,7 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#include "perfetto/tracing/core/test_config.h"
+#include "perfetto/ext/tracing/core/test_config.h"
 
 #include "perfetto/config/test_config.pb.h"
 
diff --git a/src/tracing/core/trace_buffer.cc b/src/tracing/core/trace_buffer.cc
index 4af9b6d..10282d8 100644
--- a/src/tracing/core/trace_buffer.cc
+++ b/src/tracing/core/trace_buffer.cc
@@ -19,9 +19,10 @@
 #include <limits>
 
 #include "perfetto/base/logging.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 #include "perfetto/protozero/proto_utils.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/trace_packet.h"
 
 #define TRACE_BUFFER_VERBOSE_LOGGING() 0  // Set to 1 when debugging unittests.
 #if TRACE_BUFFER_VERBOSE_LOGGING()
diff --git a/src/tracing/core/trace_buffer.h b/src/tracing/core/trace_buffer.h
index 35bd5fb..3d90b7e 100644
--- a/src/tracing/core/trace_buffer.h
+++ b/src/tracing/core/trace_buffer.h
@@ -26,11 +26,12 @@
 #include <tuple>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/base/thread_annotations.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/slice.h"
-#include "perfetto/tracing/core/trace_stats.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/base/thread_annotations.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/slice.h"
+#include "perfetto/ext/tracing/core/trace_stats.h"
 
 namespace perfetto {
 
@@ -596,7 +597,7 @@
       // to read (assuming a well-behaving client), but the risk of introducing
       // a bug that way outweighs the benefit.
       PERFETTO_ANNOTATE_BENIGN_RACE_SIZED(
-          src, size, "Benign race when copying chunk from shared memory.");
+          src, size, "Benign race when copying chunk from shared memory.")
       memcpy(wptr + sizeof(record), src, size);
     } else {
       PERFETTO_DCHECK(size == record.size - sizeof(record));
diff --git a/src/tracing/core/trace_buffer_unittest.cc b/src/tracing/core/trace_buffer_unittest.cc
index b1f60ca..b715b49 100644
--- a/src/tracing/core/trace_buffer_unittest.cc
+++ b/src/tracing/core/trace_buffer_unittest.cc
@@ -21,15 +21,16 @@
 #include <sstream>
 #include <vector>
 
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 #include "perfetto/protozero/proto_utils.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/trace_packet.h"
 #include "src/tracing/core/trace_buffer.h"
 #include "src/tracing/test/fake_packet.h"
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 namespace perfetto {
 
diff --git a/src/tracing/core/trace_config.cc b/src/tracing/core/trace_config.cc
index d44feb9..0b3e829 100644
--- a/src/tracing/core/trace_config.cc
+++ b/src/tracing/core/trace_config.cc
@@ -25,7 +25,7 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#include "perfetto/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
 
 #include "perfetto/config/data_source_config.pb.h"
 #include "perfetto/config/trace_config.pb.h"
diff --git a/src/tracing/core/trace_packet.cc b/src/tracing/core/trace_packet.cc
index ffe6f1a..c3891c5 100644
--- a/src/tracing/core/trace_packet.cc
+++ b/src/tracing/core/trace_packet.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 
 #include "perfetto/base/logging.h"
 #include "perfetto/protozero/proto_utils.h"
diff --git a/src/tracing/core/trace_packet_unittest.cc b/src/tracing/core/trace_packet_unittest.cc
index 61bc001..ad95e7c 100644
--- a/src/tracing/core/trace_packet_unittest.cc
+++ b/src/tracing/core/trace_packet_unittest.cc
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#include "perfetto/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 
 #include <string>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 #include "perfetto/trace/trace.pb.h"
 #include "perfetto/trace/trace_packet.pb.h"
diff --git a/src/tracing/core/trace_stats.cc b/src/tracing/core/trace_stats.cc
index 911e27c..3937e57 100644
--- a/src/tracing/core/trace_stats.cc
+++ b/src/tracing/core/trace_stats.cc
@@ -25,7 +25,7 @@
  * ./tools/gen_tracing_cpp_headers_from_protos
  */
 
-#include "perfetto/tracing/core/trace_stats.h"
+#include "perfetto/ext/tracing/core/trace_stats.h"
 
 #include "perfetto/common/trace_stats.pb.h"
 
diff --git a/src/tracing/core/trace_writer_for_testing.cc b/src/tracing/core/trace_writer_for_testing.cc
index 6cfb559..b4daf0a 100644
--- a/src/tracing/core/trace_writer_for_testing.cc
+++ b/src/tracing/core/trace_writer_for_testing.cc
@@ -17,7 +17,7 @@
 #include "src/tracing/core/trace_writer_for_testing.h"
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/message.h"
 #include "perfetto/trace/trace.pb.h"
 #include "perfetto/trace/trace.pbzero.h"
diff --git a/src/tracing/core/trace_writer_for_testing.h b/src/tracing/core/trace_writer_for_testing.h
index f7a1a88..4353d9f 100644
--- a/src/tracing/core/trace_writer_for_testing.h
+++ b/src/tracing/core/trace_writer_for_testing.h
@@ -18,10 +18,10 @@
 
 #include <vector>
 
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "perfetto/protozero/message_handle.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "perfetto/trace/trace_packet.pb.h"
-#include "perfetto/tracing/core/trace_writer.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/core/trace_writer_impl.h b/src/tracing/core/trace_writer_impl.h
index 4bc1df9..ee3a98e 100644
--- a/src/tracing/core/trace_writer_impl.h
+++ b/src/tracing/core/trace_writer_impl.h
@@ -17,11 +17,11 @@
 #ifndef SRC_TRACING_CORE_TRACE_WRITER_IMPL_H_
 #define SRC_TRACING_CORE_TRACE_WRITER_IMPL_H_
 
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "perfetto/protozero/message_handle.h"
 #include "perfetto/protozero/scattered_stream_writer.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/trace_writer.h"
 #include "src/tracing/core/patch_list.h"
 
 namespace perfetto {
diff --git a/src/tracing/core/trace_writer_impl_unittest.cc b/src/tracing/core/trace_writer_impl_unittest.cc
index 2f01eec..ba2acd9 100644
--- a/src/tracing/core/trace_writer_impl_unittest.cc
+++ b/src/tracing/core/trace_writer_impl_unittest.cc
@@ -16,11 +16,11 @@
 
 #include "src/tracing/core/trace_writer_impl.h"
 
-#include "gtest/gtest.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/tracing/core/commit_data_request.h"
-#include "perfetto/tracing/core/trace_writer.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 #include "src/base/test/gtest_test_suite.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/tracing/core/shared_memory_arbiter_impl.h"
diff --git a/src/tracing/core/tracing_service_impl.cc b/src/tracing/core/tracing_service_impl.cc
index db27f46..083e759 100644
--- a/src/tracing/core/tracing_service_impl.cc
+++ b/src/tracing/core/tracing_service_impl.cc
@@ -38,16 +38,17 @@
 #include <algorithm>
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/shared_memory.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/base/watchdog.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/tracing/core/packet_stream_validator.h"
 #include "src/tracing/core/shared_memory_arbiter_impl.h"
 #include "src/tracing/core/trace_buffer.h"
@@ -2095,7 +2096,7 @@
     PERFETTO_CHECK(packet.SerializeToArray(dst, size_left));
     sync_marker_packet_size_ += static_cast<size_t>(packet.ByteSize());
     PERFETTO_CHECK(sync_marker_packet_size_ <= sizeof(sync_marker_packet_));
-  };
+  }
   packets->emplace_back();
   packets->back().AddSlice(&sync_marker_packet_[0], sync_marker_packet_size_);
 }
@@ -2141,7 +2142,7 @@
         clock.type == protos::ClockSnapshot::Clock::BOOTTIME) {
       packet.set_timestamp(
           static_cast<uint64_t>(base::FromPosixTimespec(clock.ts).count()));
-    };
+    }
     protos::ClockSnapshot::Clock* c = clock_snapshot->add_clocks();
     c->set_type(clock.type);
     c->set_timestamp(
diff --git a/src/tracing/core/tracing_service_impl.h b/src/tracing/core/tracing_service_impl.h
index 10ca797..3d734cd 100644
--- a/src/tracing/core/tracing_service_impl.h
+++ b/src/tracing/core/tracing_service_impl.h
@@ -24,19 +24,18 @@
 #include <set>
 #include <vector>
 
-#include "perfetto/base/gtest_prod_util.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/optional.h"
-#include "perfetto/base/time.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/commit_data_request.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/observable_events.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_stats.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/observable_events.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_stats.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 #include "src/tracing/core/id_allocator.h"
 
 namespace perfetto {
diff --git a/src/tracing/core/tracing_service_impl_unittest.cc b/src/tracing/core/tracing_service_impl_unittest.cc
index 74eeacf..c96d097 100644
--- a/src/tracing/core/tracing_service_impl_unittest.cc
+++ b/src/tracing/core/tracing_service_impl_unittest.cc
@@ -18,18 +18,18 @@
 
 #include <string.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/utils.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/shared_memory.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/tracing/core/shared_memory_arbiter_impl.h"
 #include "src/tracing/core/trace_writer_impl.h"
diff --git a/src/tracing/core/virtual_destructors.cc b/src/tracing/core/virtual_destructors.cc
index aa73453..75fc806 100644
--- a/src/tracing/core/virtual_destructors.cc
+++ b/src/tracing/core/virtual_destructors.cc
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/shared_memory.h"
-#include "perfetto/tracing/core/shared_memory_arbiter.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
+#include "perfetto/ext/tracing/core/shared_memory_arbiter.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 // This translation unit contains the definitions for the destructor of pure
 // virtual interfaces for the current build target. The alternative would be
diff --git a/src/public/data_source.cc b/src/tracing/data_source.cc
similarity index 94%
rename from src/public/data_source.cc
rename to src/tracing/data_source.cc
index 19eb17d..edb00fd 100644
--- a/src/public/data_source.cc
+++ b/src/tracing/data_source.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/public/data_source.h"
+#include "perfetto/tracing/data_source.h"
 
 namespace perfetto {
 
diff --git a/src/public/internal/in_process_tracing_backend.cc b/src/tracing/internal/in_process_tracing_backend.cc
similarity index 94%
rename from src/public/internal/in_process_tracing_backend.cc
rename to src/tracing/internal/in_process_tracing_backend.cc
index 4e09473..6f500c1 100644
--- a/src/public/internal/in_process_tracing_backend.cc
+++ b/src/tracing/internal/in_process_tracing_backend.cc
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#include "src/public/internal/in_process_tracing_backend.h"
+#include "src/tracing/internal/in_process_tracing_backend.h"
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/paged_memory.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/tracing/core/shared_memory.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 // TODO(primiano): When the in-process backend is used, we should never end up
 // in a situation where the thread where the TracingService and Producer live
diff --git a/src/public/internal/in_process_tracing_backend.h b/src/tracing/internal/in_process_tracing_backend.h
similarity index 86%
rename from src/public/internal/in_process_tracing_backend.h
rename to src/tracing/internal/in_process_tracing_backend.h
index d843207..8d07ae7 100644
--- a/src/public/internal/in_process_tracing_backend.h
+++ b/src/tracing/internal/in_process_tracing_backend.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef SRC_PUBLIC_INTERNAL_IN_PROCESS_TRACING_BACKEND_H_
-#define SRC_PUBLIC_INTERNAL_IN_PROCESS_TRACING_BACKEND_H_
+#ifndef SRC_TRACING_INTERNAL_IN_PROCESS_TRACING_BACKEND_H_
+#define SRC_TRACING_INTERNAL_IN_PROCESS_TRACING_BACKEND_H_
 
-#include "perfetto/public/tracing_backend.h"
+#include "perfetto/tracing/tracing_backend.h"
 
 namespace perfetto {
 
@@ -54,4 +54,4 @@
 }  // namespace internal
 }  // namespace perfetto
 
-#endif  // SRC_PUBLIC_INTERNAL_IN_PROCESS_TRACING_BACKEND_H_
+#endif  // SRC_TRACING_INTERNAL_IN_PROCESS_TRACING_BACKEND_H_
diff --git a/src/public/internal/system_tracing_backend.cc b/src/tracing/internal/system_tracing_backend.cc
similarity index 90%
rename from src/public/internal/system_tracing_backend.cc
rename to src/tracing/internal/system_tracing_backend.cc
index bbf4222..66ec4fd 100644
--- a/src/public/internal/system_tracing_backend.cc
+++ b/src/tracing/internal/system_tracing_backend.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "src/public/internal/system_tracing_backend.h"
+#include "src/tracing/internal/system_tracing_backend.h"
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/tracing/core/tracing_service.h"
-#include "perfetto/tracing/ipc/producer_ipc_client.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
 #include "src/tracing/ipc/default_socket.h"
 
 namespace perfetto {
diff --git a/src/public/internal/system_tracing_backend.h b/src/tracing/internal/system_tracing_backend.h
similarity index 87%
rename from src/public/internal/system_tracing_backend.h
rename to src/tracing/internal/system_tracing_backend.h
index a4219ac..ca70242 100644
--- a/src/public/internal/system_tracing_backend.h
+++ b/src/tracing/internal/system_tracing_backend.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef SRC_PUBLIC_INTERNAL_SYSTEM_TRACING_BACKEND_H_
-#define SRC_PUBLIC_INTERNAL_SYSTEM_TRACING_BACKEND_H_
+#ifndef SRC_TRACING_INTERNAL_SYSTEM_TRACING_BACKEND_H_
+#define SRC_TRACING_INTERNAL_SYSTEM_TRACING_BACKEND_H_
 
-#include "perfetto/public/tracing_backend.h"
+#include "perfetto/tracing/tracing_backend.h"
 
 namespace perfetto {
 
@@ -51,4 +51,4 @@
 }  // namespace internal
 }  // namespace perfetto
 
-#endif  // SRC_PUBLIC_INTERNAL_SYSTEM_TRACING_BACKEND_H_
+#endif  // SRC_TRACING_INTERNAL_SYSTEM_TRACING_BACKEND_H_
diff --git a/src/public/internal/tracing_muxer_impl.cc b/src/tracing/internal/tracing_muxer_impl.cc
similarity index 96%
rename from src/public/internal/tracing_muxer_impl.cc
rename to src/tracing/internal/tracing_muxer_impl.cc
index aa04cab..bbb6557 100644
--- a/src/public/internal/tracing_muxer_impl.cc
+++ b/src/tracing/internal/tracing_muxer_impl.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/public/internal/tracing_muxer_impl.h"
+#include "src/tracing/internal/tracing_muxer_impl.h"
 
 #include <algorithm>
 #include <atomic>
@@ -22,18 +22,18 @@
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/public/data_source.h"
-#include "perfetto/public/internal/data_source_internal.h"
-#include "perfetto/public/trace_writer_base.h"
-#include "perfetto/public/tracing.h"
-#include "perfetto/public/tracing_backend.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_writer.h"
-#include "perfetto/tracing/core/tracing_service.h"
-#include "src/public/internal/in_process_tracing_backend.h"
-#include "src/public/internal/system_tracing_backend.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
+#include "perfetto/tracing/data_source.h"
+#include "perfetto/tracing/internal/data_source_internal.h"
+#include "perfetto/tracing/trace_writer_base.h"
+#include "perfetto/tracing/tracing.h"
+#include "perfetto/tracing/tracing_backend.h"
+#include "src/tracing/internal/in_process_tracing_backend.h"
+#include "src/tracing/internal/system_tracing_backend.h"
 
 namespace perfetto {
 namespace internal {
diff --git a/src/public/internal/tracing_muxer_impl.h b/src/tracing/internal/tracing_muxer_impl.h
similarity index 94%
rename from src/public/internal/tracing_muxer_impl.h
rename to src/tracing/internal/tracing_muxer_impl.h
index 87f54f6..c4de1be 100644
--- a/src/public/internal/tracing_muxer_impl.h
+++ b/src/tracing/internal/tracing_muxer_impl.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_PUBLIC_INTERNAL_TRACING_MUXER_IMPL_H_
-#define SRC_PUBLIC_INTERNAL_TRACING_MUXER_IMPL_H_
+#ifndef SRC_TRACING_INTERNAL_TRACING_MUXER_IMPL_H_
+#define SRC_TRACING_INTERNAL_TRACING_MUXER_IMPL_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -26,15 +26,15 @@
 #include <memory>
 #include <vector>
 
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/public/internal/basic_types.h"
-#include "perfetto/public/internal/tracing_muxer.h"
-#include "perfetto/public/tracing.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/trace_config.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/tracing/internal/basic_types.h"
+#include "perfetto/tracing/internal/tracing_muxer.h"
+#include "perfetto/tracing/tracing.h"
 
 namespace perfetto {
 
@@ -264,4 +264,4 @@
 }  // namespace internal
 }  // namespace perfetto
 
-#endif  // SRC_PUBLIC_INTERNAL_TRACING_MUXER_IMPL_H_
+#endif  // SRC_TRACING_INTERNAL_TRACING_MUXER_IMPL_H_
diff --git a/src/tracing/ipc/consumer/consumer_ipc_client_impl.cc b/src/tracing/ipc/consumer/consumer_ipc_client_impl.cc
index 26f4917..b38e2ba 100644
--- a/src/tracing/ipc/consumer/consumer_ipc_client_impl.cc
+++ b/src/tracing/ipc/consumer/consumer_ipc_client_impl.cc
@@ -20,11 +20,11 @@
 #include <string.h>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/ipc/client.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/observable_events.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_stats.h"
+#include "perfetto/ext/ipc/client.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/observable_events.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_stats.h"
 
 // TODO(fmayer): Add a test to check to what happens when ConsumerIPCClientImpl
 // gets destroyed w.r.t. the Consumer pointer. Also think to lifetime of the
diff --git a/src/tracing/ipc/consumer/consumer_ipc_client_impl.h b/src/tracing/ipc/consumer/consumer_ipc_client_impl.h
index e930cee..320e5c0 100644
--- a/src/tracing/ipc/consumer/consumer_ipc_client_impl.h
+++ b/src/tracing/ipc/consumer/consumer_ipc_client_impl.h
@@ -21,13 +21,13 @@
 
 #include <vector>
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/ipc/service_proxy.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/tracing_service.h"
-#include "perfetto/tracing/ipc/consumer_ipc_client.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/ipc/service_proxy.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
 
 #include "perfetto/ipc/consumer_port.ipc.h"
 
diff --git a/src/tracing/ipc/default_socket.cc b/src/tracing/ipc/default_socket.cc
index 7234cb4..5647308 100644
--- a/src/tracing/ipc/default_socket.cc
+++ b/src/tracing/ipc/default_socket.cc
@@ -17,8 +17,8 @@
 #include "src/tracing/ipc/default_socket.h"
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/ipc/basic_types.h"
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/ipc/basic_types.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 
 #include <stdlib.h>
 
diff --git a/src/tracing/ipc/posix_shared_memory.cc b/src/tracing/ipc/posix_shared_memory.cc
index 714284d..d7f0904 100644
--- a/src/tracing/ipc/posix_shared_memory.cc
+++ b/src/tracing/ipc/posix_shared_memory.cc
@@ -29,7 +29,7 @@
 
 #include "perfetto/base/build_config.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/temp_file.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
 #include <linux/memfd.h>
diff --git a/src/tracing/ipc/posix_shared_memory.h b/src/tracing/ipc/posix_shared_memory.h
index 88f410c..4bb3baf 100644
--- a/src/tracing/ipc/posix_shared_memory.h
+++ b/src/tracing/ipc/posix_shared_memory.h
@@ -21,8 +21,8 @@
 
 #include <memory>
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/tracing/core/shared_memory.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/ipc/posix_shared_memory_unittest.cc b/src/tracing/ipc/posix_shared_memory_unittest.cc
index e6739bf..2bb9aee 100644
--- a/src/tracing/ipc/posix_shared_memory_unittest.cc
+++ b/src/tracing/ipc/posix_shared_memory_unittest.cc
@@ -23,12 +23,12 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/utils.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/base/test/vm_test_utils.h"
 
diff --git a/src/tracing/ipc/producer/producer_ipc_client_impl.cc b/src/tracing/ipc/producer/producer_ipc_client_impl.cc
index bf6e1b0..fa7924a 100644
--- a/src/tracing/ipc/producer/producer_ipc_client_impl.cc
+++ b/src/tracing/ipc/producer/producer_ipc_client_impl.cc
@@ -20,14 +20,14 @@
 #include <string.h>
 
 #include "perfetto/base/task_runner.h"
-#include "perfetto/ipc/client.h"
-#include "perfetto/tracing/core/commit_data_request.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/shared_memory_arbiter.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/ipc/client.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/shared_memory_arbiter.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/tracing/ipc/posix_shared_memory.h"
 
 // TODO(fmayer): think to what happens when ProducerIPCClientImpl gets destroyed
diff --git a/src/tracing/ipc/producer/producer_ipc_client_impl.h b/src/tracing/ipc/producer/producer_ipc_client_impl.h
index ba43421..70387b4 100644
--- a/src/tracing/ipc/producer/producer_ipc_client_impl.h
+++ b/src/tracing/ipc/producer/producer_ipc_client_impl.h
@@ -22,12 +22,12 @@
 #include <set>
 #include <vector>
 
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/ipc/service_proxy.h"
-#include "perfetto/tracing/core/basic_types.h"
-#include "perfetto/tracing/core/shared_memory.h"
-#include "perfetto/tracing/core/tracing_service.h"
-#include "perfetto/tracing/ipc/producer_ipc_client.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/ipc/service_proxy.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
 
 #include "perfetto/ipc/producer_port.ipc.h"
 
diff --git a/src/tracing/ipc/service/consumer_ipc_service.cc b/src/tracing/ipc/service/consumer_ipc_service.cc
index 4e5afc9..0d10c3e 100644
--- a/src/tracing/ipc/service/consumer_ipc_service.cc
+++ b/src/tracing/ipc/service/consumer_ipc_service.cc
@@ -19,16 +19,16 @@
 #include <inttypes.h>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/ipc/basic_types.h"
-#include "perfetto/ipc/host.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
-#include "perfetto/tracing/core/slice.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_stats.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/ipc/basic_types.h"
+#include "perfetto/ext/ipc/host.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
+#include "perfetto/ext/tracing/core/slice.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_stats.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/ipc/service/consumer_ipc_service.h b/src/tracing/ipc/service/consumer_ipc_service.h
index 2752fb3..22957a9 100644
--- a/src/tracing/ipc/service/consumer_ipc_service.h
+++ b/src/tracing/ipc/service/consumer_ipc_service.h
@@ -22,10 +22,10 @@
 #include <memory>
 #include <string>
 
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/ipc/basic_types.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/ipc/basic_types.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 #include "perfetto/ipc/consumer_port.ipc.h"
 
diff --git a/src/tracing/ipc/service/producer_ipc_service.cc b/src/tracing/ipc/service/producer_ipc_service.cc
index 15f7808..5e2214e 100644
--- a/src/tracing/ipc/service/producer_ipc_service.cc
+++ b/src/tracing/ipc/service/producer_ipc_service.cc
@@ -20,11 +20,11 @@
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/ipc/host.h"
-#include "perfetto/tracing/core/commit_data_request.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/ipc/host.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 #include "src/tracing/ipc/posix_shared_memory.h"
 
 // The remote Producer(s) are not trusted. All the methods from the ProducerPort
diff --git a/src/tracing/ipc/service/producer_ipc_service.h b/src/tracing/ipc/service/producer_ipc_service.h
index 5954680..200972e 100644
--- a/src/tracing/ipc/service/producer_ipc_service.h
+++ b/src/tracing/ipc/service/producer_ipc_service.h
@@ -21,10 +21,10 @@
 #include <memory>
 #include <string>
 
-#include "perfetto/base/weak_ptr.h"
-#include "perfetto/ipc/basic_types.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/base/weak_ptr.h"
+#include "perfetto/ext/ipc/basic_types.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 #include "perfetto/ipc/producer_port.ipc.h"
 
diff --git a/src/tracing/ipc/service/service_ipc_host_impl.cc b/src/tracing/ipc/service/service_ipc_host_impl.cc
index 9c185ac..ad6f9ef 100644
--- a/src/tracing/ipc/service/service_ipc_host_impl.cc
+++ b/src/tracing/ipc/service/service_ipc_host_impl.cc
@@ -18,8 +18,8 @@
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/ipc/host.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/ipc/host.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 #include "src/tracing/ipc/posix_shared_memory.h"
 #include "src/tracing/ipc/service/consumer_ipc_service.h"
 #include "src/tracing/ipc/service/producer_ipc_service.h"
diff --git a/src/tracing/ipc/service/service_ipc_host_impl.h b/src/tracing/ipc/service/service_ipc_host_impl.h
index e7f5cdd..d06ef60 100644
--- a/src/tracing/ipc/service/service_ipc_host_impl.h
+++ b/src/tracing/ipc/service/service_ipc_host_impl.h
@@ -19,7 +19,7 @@
 
 #include <memory>
 
-#include "perfetto/tracing/ipc/service_ipc_host.h"
+#include "perfetto/ext/tracing/ipc/service_ipc_host.h"
 
 namespace perfetto {
 
diff --git a/src/public/platform.cc b/src/tracing/platform.cc
similarity index 87%
rename from src/public/platform.cc
rename to src/tracing/platform.cc
index 50e0ab8..e6865cd 100644
--- a/src/public/platform.cc
+++ b/src/tracing/platform.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "perfetto/public/platform.h"
-#include "perfetto/public/internal/tracing_tls.h"
-#include "perfetto/public/trace_writer_base.h"
+#include "perfetto/tracing/platform.h"
+#include "perfetto/tracing/internal/tracing_tls.h"
+#include "perfetto/tracing/trace_writer_base.h"
 
 namespace perfetto {
 
diff --git a/src/public/platform_posix.cc b/src/tracing/platform_posix.cc
similarity index 94%
rename from src/public/platform_posix.cc
rename to src/tracing/platform_posix.cc
index dc078bc..75551fc 100644
--- a/src/public/platform_posix.cc
+++ b/src/tracing/platform_posix.cc
@@ -15,11 +15,11 @@
  */
 
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/thread_task_runner.h"
-#include "perfetto/public/internal/tracing_tls.h"
-#include "perfetto/public/platform.h"
-#include "perfetto/public/trace_writer_base.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/thread_task_runner.h"
+#include "perfetto/tracing/internal/tracing_tls.h"
+#include "perfetto/tracing/platform.h"
+#include "perfetto/tracing/trace_writer_base.h"
 
 #include <pthread.h>
 #include <stdlib.h>
diff --git a/src/tracing/test/aligned_buffer_test.h b/src/tracing/test/aligned_buffer_test.h
index 23c8b1c..f9d9932 100644
--- a/src/tracing/test/aligned_buffer_test.h
+++ b/src/tracing/test/aligned_buffer_test.h
@@ -21,8 +21,8 @@
 
 #include <memory>
 
-#include "gtest/gtest.h"
-#include "perfetto/base/utils.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/utils.h"
 #include "src/tracing/test/test_shared_memory.h"
 
 namespace perfetto {
diff --git a/src/tracing/test/fake_packet.cc b/src/tracing/test/fake_packet.cc
index 210f5fa..785933f 100644
--- a/src/tracing/test/fake_packet.cc
+++ b/src/tracing/test/fake_packet.cc
@@ -19,8 +19,8 @@
 #include <ostream>
 
 #include "perfetto/base/logging.h"
+#include "perfetto/ext/tracing/core/shared_memory_abi.h"
 #include "perfetto/protozero/proto_utils.h"
-#include "perfetto/tracing/core/shared_memory_abi.h"
 #include "src/tracing/core/trace_buffer.h"
 
 using protozero::proto_utils::ParseVarInt;
diff --git a/src/tracing/test/fake_packet.h b/src/tracing/test/fake_packet.h
index 6f24408..2e4c494 100644
--- a/src/tracing/test/fake_packet.h
+++ b/src/tracing/test/fake_packet.h
@@ -24,7 +24,7 @@
 #include <string>
 #include <vector>
 
-#include "perfetto/tracing/core/basic_types.h"
+#include "perfetto/ext/tracing/core/basic_types.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/test/fake_producer_endpoint.h b/src/tracing/test/fake_producer_endpoint.h
index 29183b1..8cacf38 100644
--- a/src/tracing/test/fake_producer_endpoint.h
+++ b/src/tracing/test/fake_producer_endpoint.h
@@ -17,8 +17,8 @@
 #ifndef SRC_TRACING_TEST_FAKE_PRODUCER_ENDPOINT_H_
 #define SRC_TRACING_TEST_FAKE_PRODUCER_ENDPOINT_H_
 
-#include "perfetto/tracing/core/commit_data_request.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include "perfetto/ext/tracing/core/commit_data_request.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/test/hello_world_benchmark.cc b/src/tracing/test/hello_world_benchmark.cc
index c554de9..81ea9ac 100644
--- a/src/tracing/test/hello_world_benchmark.cc
+++ b/src/tracing/test/hello_world_benchmark.cc
@@ -1,4 +1,4 @@
-#include "benchmark/benchmark.h"
+#include <benchmark/benchmark.h>
 
 static void BM_StringCreation(benchmark::State& state) {
   while (state.KeepRunning())
diff --git a/src/tracing/test/mock_consumer.cc b/src/tracing/test/mock_consumer.cc
index 15c1400..137ef4e 100644
--- a/src/tracing/test/mock_consumer.cc
+++ b/src/tracing/test/mock_consumer.cc
@@ -16,8 +16,8 @@
 
 #include "src/tracing/test/mock_consumer.h"
 
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_stats.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_stats.h"
 #include "src/base/test/test_task_runner.h"
 
 using ::testing::_;
diff --git a/src/tracing/test/mock_consumer.h b/src/tracing/test/mock_consumer.h
index a297042..53b789f 100644
--- a/src/tracing/test/mock_consumer.h
+++ b/src/tracing/test/mock_consumer.h
@@ -19,10 +19,10 @@
 
 #include <memory>
 
-#include "gmock/gmock.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include <gmock/gmock.h>
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 #include "perfetto/trace/trace_packet.pb.h"
 
diff --git a/src/tracing/test/mock_producer.cc b/src/tracing/test/mock_producer.cc
index 02c33ba..b55f518 100644
--- a/src/tracing/test/mock_producer.cc
+++ b/src/tracing/test/mock_producer.cc
@@ -16,9 +16,9 @@
 
 #include "src/tracing/test/mock_producer.h"
 
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "src/base/test/test_task_runner.h"
 
 using ::testing::_;
diff --git a/src/tracing/test/mock_producer.h b/src/tracing/test/mock_producer.h
index 3094678..6d9fa33 100644
--- a/src/tracing/test/mock_producer.h
+++ b/src/tracing/test/mock_producer.h
@@ -21,10 +21,10 @@
 #include <memory>
 #include <string>
 
-#include "gmock/gmock.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/trace_writer.h"
-#include "perfetto/tracing/core/tracing_service.h"
+#include <gmock/gmock.h>
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/core/tracing_service.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/test/test_shared_memory.h b/src/tracing/test/test_shared_memory.h
index 4b11b92..9db6bcb 100644
--- a/src/tracing/test/test_shared_memory.h
+++ b/src/tracing/test/test_shared_memory.h
@@ -21,8 +21,8 @@
 
 #include <memory>
 
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/tracing/core/shared_memory.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/tracing/core/shared_memory.h"
 
 namespace perfetto {
 
diff --git a/src/tracing/test/tracing_integration_test.cc b/src/tracing/test/tracing_integration_test.cc
index 2ce17ec..0fbc746 100644
--- a/src/tracing/test/tracing_integration_test.cc
+++ b/src/tracing/test/tracing_integration_test.cc
@@ -17,20 +17,20 @@
 #include <inttypes.h>
 #include <unistd.h>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "perfetto/base/temp_file.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_stats.h"
-#include "perfetto/tracing/core/trace_writer.h"
-#include "perfetto/tracing/ipc/consumer_ipc_client.h"
-#include "perfetto/tracing/ipc/producer_ipc_client.h"
-#include "perfetto/tracing/ipc/service_ipc_host.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_stats.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
+#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
+#include "perfetto/ext/tracing/ipc/service_ipc_host.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/ipc/test/test_socket.h"
 #include "src/tracing/core/tracing_service_impl.h"
diff --git a/src/public/trace_writer_base.cc b/src/tracing/trace_writer_base.cc
similarity index 95%
rename from src/public/trace_writer_base.cc
rename to src/tracing/trace_writer_base.cc
index 11c3da7..b1e63c4 100644
--- a/src/public/trace_writer_base.cc
+++ b/src/tracing/trace_writer_base.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/public/trace_writer_base.h"
+#include "perfetto/tracing/trace_writer_base.h"
 
 namespace perfetto {
 
diff --git a/src/public/tracing.cc b/src/tracing/tracing.cc
similarity index 94%
rename from src/public/tracing.cc
rename to src/tracing/tracing.cc
index b1bfc1f..769eb70 100644
--- a/src/public/tracing.cc
+++ b/src/tracing/tracing.cc
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#include "perfetto/public/tracing.h"
-#include "src/public/internal/tracing_muxer_impl.h"
+#include "perfetto/tracing/tracing.h"
+#include "src/tracing/internal/tracing_muxer_impl.h"
 
 #include <condition_variable>
 #include <mutex>
diff --git a/src/public/virtual_destructors.cc b/src/tracing/virtual_destructors.cc
similarity index 89%
rename from src/public/virtual_destructors.cc
rename to src/tracing/virtual_destructors.cc
index 7697252..d548c8e 100644
--- a/src/public/virtual_destructors.cc
+++ b/src/tracing/virtual_destructors.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "perfetto/public/internal/tracing_tls.h"
-#include "perfetto/public/tracing.h"
-#include "perfetto/public/tracing_backend.h"
+#include "perfetto/tracing/internal/tracing_tls.h"
+#include "perfetto/tracing/tracing.h"
+#include "perfetto/tracing/tracing_backend.h"
 
 // This translation unit contains the definitions for the destructor of pure
 // virtual interfaces for the src/public:public target. The alternative would be
diff --git a/test/BUILD.gn b/test/BUILD.gn
index a9518e3..48b9ee3 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -12,9 +12,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import("//build_overrides/build.gni")
 import("../gn/fuzzer.gni")
 import("../gn/perfetto.gni")
-import("//build_overrides/build.gni")
 
 source_set("end_to_end_integrationtests") {
   testonly = true
@@ -24,8 +24,8 @@
     ":test_helper",
     "../gn:default_deps",
     "../gn:gtest_deps",
+    "../include/perfetto/ext/traced",
     "../include/perfetto/protozero",
-    "../include/perfetto/traced",
     "../protos/perfetto/config:zero",
     "../protos/perfetto/trace:lite",
     "../protos/perfetto/trace:zero",
@@ -36,7 +36,8 @@
   sources = [
     "end_to_end_integrationtest.cc",
   ]
-  if (is_android && (perfetto_build_standalone || perfetto_build_with_android)) {
+  if (is_android &&
+      (perfetto_build_standalone || perfetto_build_with_android)) {
     deps += [ "../src/base:android_task_runner" ]
   }
   if (start_daemons_for_testing) {
@@ -88,7 +89,7 @@
   deps = [
     ":task_runner_thread",
     "../gn:default_deps",
-    "../include/perfetto/traced",
+    "../include/perfetto/ext/traced",
     "../src/base:test_support",
     "../src/traced/probes:probes_src",
     "../src/tracing:ipc",
@@ -109,7 +110,7 @@
     ":task_runner_thread",
     ":task_runner_thread_delegates",
     "../gn:default_deps",
-    "../include/perfetto/traced",
+    "../include/perfetto/ext/traced",
     "../protos/perfetto/trace:lite",
     "../protos/perfetto/trace:zero",
     "../src/base:test_support",
@@ -130,10 +131,10 @@
       ":task_runner_thread",
       ":task_runner_thread_delegates",
       ":test_helper",
-      "../../gn:default_deps",
       "../buildtools:benchmark",
+      "../gn:default_deps",
       "../gn:gtest_deps",
-      "../include/perfetto/traced",
+      "../include/perfetto/ext/traced",
       "../protos/perfetto/trace:lite",
       "../protos/perfetto/trace:zero",
       "../src/base:test_support",
@@ -152,8 +153,8 @@
   source_set("benchmark_main") {
     testonly = true
     deps = [
-      "../../gn:default_deps",
       "../buildtools:benchmark",
+      "../gn:default_deps",
     ]
     sources = [
       "benchmark_main.cc",
diff --git a/test/benchmark_main.cc b/test/benchmark_main.cc
index f904cfc..e410e52 100644
--- a/test/benchmark_main.cc
+++ b/test/benchmark_main.cc
@@ -12,6 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "benchmark/benchmark.h"
+#include <benchmark/benchmark.h>
 
 BENCHMARK_MAIN();
diff --git a/test/cts/device_feature_test_cts.cc b/test/cts/device_feature_test_cts.cc
index 3664476..eb57888 100644
--- a/test/cts/device_feature_test_cts.cc
+++ b/test/cts/device_feature_test_cts.cc
@@ -16,7 +16,7 @@
 
 #include <sys/sysinfo.h>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 
diff --git a/test/cts/end_to_end_integrationtest_cts.cc b/test/cts/end_to_end_integrationtest_cts.cc
index d40d92f..d6d5eda 100644
--- a/test/cts/end_to_end_integrationtest_cts.cc
+++ b/test/cts/end_to_end_integrationtest_cts.cc
@@ -17,12 +17,12 @@
 #include <random>
 #include <sys/system_properties.h>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/traced/traced.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 #include "perfetto/trace/test_event.pbzero.h"
 #include "perfetto/trace/trace_packet.pb.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
-#include "perfetto/traced/traced.h"
-#include "perfetto/tracing/core/trace_packet.h"
 #include "src/base/test/test_task_runner.h"
 #include "test/test_helper.h"
 
diff --git a/test/cts/heapprofd_test_cts.cc b/test/cts/heapprofd_test_cts.cc
index 9c856a0..76de7f8 100644
--- a/test/cts/heapprofd_test_cts.cc
+++ b/test/cts/heapprofd_test_cts.cc
@@ -15,12 +15,12 @@
  * limitations under the License.
  */
 
+#include <gtest/gtest.h>
 #include <stdlib.h>
+#include <sys/system_properties.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <sys/system_properties.h>
 
-#include "gtest/gtest.h"
 #include "perfetto/base/logging.h"
 #include "src/base/test/test_task_runner.h"
 #include "test/test_helper.h"
diff --git a/test/cts/producer/jni/fake_producer_jni.cc b/test/cts/producer/jni/fake_producer_jni.cc
index cbeafe2..f45ed0a 100644
--- a/test/cts/producer/jni/fake_producer_jni.cc
+++ b/test/cts/producer/jni/fake_producer_jni.cc
@@ -16,7 +16,7 @@
 
 #include <jni.h>
 
-#include "perfetto/traced/traced.h"
+#include "perfetto/ext/traced/traced.h"
 
 #include "src/base/test/test_task_runner.h"
 #include "src/tracing/ipc/default_socket.h"
diff --git a/test/end_to_end_benchmark.cc b/test/end_to_end_benchmark.cc
index fd1fd3e..f98eee8 100644
--- a/test/end_to_end_benchmark.cc
+++ b/test/end_to_end_benchmark.cc
@@ -12,14 +12,15 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include <gtest/gtest.h>
 #include <random>
 
-#include "benchmark/benchmark.h"
-#include "perfetto/base/time.h"
-#include "perfetto/traced/traced.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
+#include <benchmark/benchmark.h>
+#include <gtest/gtest.h>
+
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/traced/traced.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 #include "src/base/test/test_task_runner.h"
 #include "test/task_runner_thread.h"
 #include "test/task_runner_thread_delegates.h"
diff --git a/test/end_to_end_integrationtest.cc b/test/end_to_end_integrationtest.cc
index d44229c..7d77e58 100644
--- a/test/end_to_end_integrationtest.cc
+++ b/test/end_to_end_integrationtest.cc
@@ -23,17 +23,17 @@
 #include <random>
 #include <thread>
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
 #include "perfetto/base/build_config.h"
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/pipe.h"
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/pipe.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/traced/traced.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
-#include "perfetto/traced/traced.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/traced/probes/ftrace/ftrace_controller.h"
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
diff --git a/test/end_to_end_shared_memory_fuzzer.cc b/test/end_to_end_shared_memory_fuzzer.cc
index 5006afb..bb6d90e 100644
--- a/test/end_to_end_shared_memory_fuzzer.cc
+++ b/test/end_to_end_shared_memory_fuzzer.cc
@@ -20,14 +20,14 @@
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/tracing/core/data_source_config.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
+#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
+#include "perfetto/ext/tracing/ipc/service_ipc_host.h"
 #include "perfetto/trace/test_event.pbzero.h"
-#include "perfetto/tracing/core/data_source_config.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/trace_writer.h"
-#include "perfetto/tracing/ipc/producer_ipc_client.h"
-#include "perfetto/tracing/ipc/service_ipc_host.h"
 #include "src/base/test/test_task_runner.h"
 #include "src/tracing/ipc/default_socket.h"
 #include "test/task_runner_thread.h"
diff --git a/test/fake_producer.cc b/test/fake_producer.cc
index 0ae52a1..993012c 100644
--- a/test/fake_producer.cc
+++ b/test/fake_producer.cc
@@ -19,15 +19,15 @@
 #include <condition_variable>
 #include <mutex>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 #include "perfetto/base/logging.h"
-#include "perfetto/base/time.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/time.h"
+#include "perfetto/ext/base/utils.h"
+#include "perfetto/ext/traced/traced.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/core/trace_writer.h"
 #include "perfetto/trace/test_event.pbzero.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
-#include "perfetto/traced/traced.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/core/trace_writer.h"
 
 namespace perfetto {
 
diff --git a/test/fake_producer.h b/test/fake_producer.h
index 460d88c..4fddb8e 100644
--- a/test/fake_producer.h
+++ b/test/fake_producer.h
@@ -21,11 +21,11 @@
 #include <random>
 #include <string>
 
-#include "perfetto/base/thread_checker.h"
-#include "perfetto/tracing/core/data_source_descriptor.h"
-#include "perfetto/tracing/core/producer.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/ipc/producer_ipc_client.h"
+#include "perfetto/ext/base/thread_checker.h"
+#include "perfetto/ext/tracing/core/data_source_descriptor.h"
+#include "perfetto/ext/tracing/core/producer.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
 #include "src/base/test/test_task_runner.h"
 
 namespace perfetto {
diff --git a/test/metrics/android_startup.out b/test/metrics/android_startup.out
index 222a8fa..5e60861 100644
--- a/test/metrics/android_startup.out
+++ b/test/metrics/android_startup.out
@@ -14,6 +14,10 @@
         interruptible_sleep_dur_ns: 10
       }
       other_processes_spawned_count: 1
+      time_activity_manager {
+        dur_ns: 8
+      }
+      other_process_to_activity_cpu_ratio: 0.975609756098
     }
   }
 }
diff --git a/test/metrics/android_startup.py b/test/metrics/android_startup.py
index a4c7d98..3da85bf 100644
--- a/test/metrics/android_startup.py
+++ b/test/metrics/android_startup.py
@@ -74,4 +74,8 @@
 trace.add_atrace_async_end(ts=510, tid=2, pid=2,
     buf='launching: com.google.android.apps.nexuslauncher')
 
+trace.add_ftrace_packet(cpu=1)
+trace.add_sched(ts=160, prev_pid=0, next_pid=1)
+trace.add_sched(ts=200, prev_pid=1, next_pid=0)
+
 print(trace.trace.SerializeToString())
diff --git a/test/metrics/android_startup_breakdown.out b/test/metrics/android_startup_breakdown.out
new file mode 100644
index 0000000..2dc19cd
--- /dev/null
+++ b/test/metrics/android_startup_breakdown.out
@@ -0,0 +1,31 @@
+android_startup {
+  startup {
+    startup_id: 1
+    package_name: "com.google.android.calendar"
+    process_name: "com.google.android.calendar"
+    zygote_new_process: true
+    to_first_frame {
+      dur_ns: 108
+      main_thread_by_task_state {
+        running_dur_ns: 0
+        runnable_dur_ns: 0
+        uninterruptible_sleep_dur_ns: 0
+        interruptible_sleep_dur_ns: 0
+      }
+      other_processes_spawned_count: 0
+      time_activity_manager {
+        dur_ns: 8
+      }
+      time_bind_application {
+        dur_ns: 10
+      }
+      time_before_start_process {
+        dur_ns: 18
+      }
+      time_during_start_process {
+        dur_ns: 35
+      }
+    }
+    activity_hosting_process_count: 1
+  }
+}
diff --git a/test/metrics/android_startup_breakdown.py b/test/metrics/android_startup_breakdown.py
new file mode 100644
index 0000000..86aa893
--- /dev/null
+++ b/test/metrics/android_startup_breakdown.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python
+# Copyright (C) 2018 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.
+
+from os import sys, path
+
+sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
+import synth_common
+
+trace = synth_common.create_trace()
+trace.add_process_tree_packet()
+trace.add_process(1, 0, 'init')
+trace.add_process(2, 1, 'system_server')
+trace.add_process(3, 1, 'com.google.android.calendar')
+
+trace.add_ftrace_packet(cpu=0)
+
+# Start intent for a successful launch of calendar
+trace.add_atrace_begin(ts=102, tid=2, pid=2,
+    buf='MetricsLogger:launchObserverNotifyIntentStarted')
+trace.add_atrace_end(ts=103, tid=2, pid=2)
+
+trace.add_atrace_async_begin(ts=110, tid=2, pid=2,
+    buf='launching: com.google.android.calendar')
+
+trace.add_atrace_begin(ts=120, tid=2, pid=2,
+    buf='Start proc: com.google.android.calendar')
+trace.add_atrace_end(ts=155, tid=2, pid=2)
+
+# Unrelated process binding, ignored
+trace.add_atrace_begin(ts=125, tid=1, pid=1, buf='bindApplication')
+trace.add_atrace_end(ts=195, tid=1, pid=1)
+
+trace.add_atrace_begin(ts=185, tid=3, pid=3, buf='bindApplication')
+trace.add_atrace_end(ts=195, tid=3, pid=3)
+
+trace.add_atrace_async_end(ts=210, tid=2, pid=2,
+    buf='launching: com.google.android.calendar')
+trace.add_atrace_begin(ts=211, tid=2, pid=2,
+    buf='MetricsLogger:launchObserverNotifyActivityLaunchFinished')
+trace.add_atrace_end(ts=212, tid=2, pid=2)
+
+print(trace.trace.SerializeToString())
diff --git a/test/metrics/index b/test/metrics/index
index 297bd81..dc494c4 100644
--- a/test/metrics/index
+++ b/test/metrics/index
@@ -1,4 +1,5 @@
 ../data/lmk_userspace.pb android_mem android_mem_lmk.out
 ../data/memory_counters.pb android_mem android_mem_counters.out
 android_startup.py android_startup android_startup.out
+android_startup_breakdown.py android_startup android_startup_breakdown.out
 ../data/memory_counters.pb error_statistics error_statistics.out
\ No newline at end of file
diff --git a/test/task_runner_thread.cc b/test/task_runner_thread.cc
index dc9e4ac..37e29c9 100644
--- a/test/task_runner_thread.cc
+++ b/test/task_runner_thread.cc
@@ -23,9 +23,9 @@
 #include <condition_variable>
 #include <thread>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/time.h"
 #include "test/task_runner_thread.h"
 
 namespace perfetto {
diff --git a/test/task_runner_thread_delegates.h b/test/task_runner_thread_delegates.h
index 8eecbba..dc19c0a 100644
--- a/test/task_runner_thread_delegates.h
+++ b/test/task_runner_thread_delegates.h
@@ -17,7 +17,7 @@
 #ifndef TEST_TASK_RUNNER_THREAD_DELEGATES_H_
 #define TEST_TASK_RUNNER_THREAD_DELEGATES_H_
 
-#include "perfetto/tracing/ipc/service_ipc_host.h"
+#include "perfetto/ext/tracing/ipc/service_ipc_host.h"
 #include "src/traced/probes/probes_producer.h"
 #include "test/fake_producer.h"
 #include "test/task_runner_thread.h"
diff --git a/test/test_helper.cc b/test/test_helper.cc
index bcacda3..409014a 100644
--- a/test/test_helper.cc
+++ b/test/test_helper.cc
@@ -16,9 +16,9 @@
 
 #include "test/test_helper.h"
 
-#include "gtest/gtest.h"
-#include "perfetto/traced/traced.h"
-#include "perfetto/tracing/core/trace_packet.h"
+#include <gtest/gtest.h>
+#include "perfetto/ext/traced/traced.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
 #include "test/task_runner_thread_delegates.h"
 
 #include "src/tracing/ipc/default_socket.h"
diff --git a/test/test_helper.h b/test/test_helper.h
index 8b2ae2d..4904db5 100644
--- a/test/test_helper.h
+++ b/test/test_helper.h
@@ -17,11 +17,11 @@
 #ifndef TEST_TEST_HELPER_H_
 #define TEST_TEST_HELPER_H_
 
-#include "perfetto/base/scoped_file.h"
-#include "perfetto/tracing/core/consumer.h"
-#include "perfetto/tracing/core/trace_config.h"
-#include "perfetto/tracing/core/trace_packet.h"
-#include "perfetto/tracing/ipc/consumer_ipc_client.h"
+#include "perfetto/ext/base/scoped_file.h"
+#include "perfetto/ext/tracing/core/consumer.h"
+#include "perfetto/ext/tracing/core/trace_config.h"
+#include "perfetto/ext/tracing/core/trace_packet.h"
+#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
 #include "src/base/test/test_task_runner.h"
 #include "test/fake_producer.h"
 #include "test/task_runner_thread.h"
diff --git a/tools/busy_threads/busy_threads.cc b/tools/busy_threads/busy_threads.cc
index 82efcc9..b4f379b 100644
--- a/tools/busy_threads/busy_threads.cc
+++ b/tools/busy_threads/busy_threads.cc
@@ -22,7 +22,7 @@
 #include <thread>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/time.h"
 
 // Spawns the requested number threads that alternate between busy-waiting and
 // sleeping.
diff --git a/tools/check_include_violations b/tools/check_include_violations
new file mode 100755
index 0000000..5e05b86
--- /dev/null
+++ b/tools/check_include_violations
@@ -0,0 +1,85 @@
+#!/usr/bin/env python
+# Copyright (C) 2019 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.
+
+# This tool checks for layering violations in the include/ directory.
+# It checks that:
+# - public includes don't end up depending on non-public /ext/ includes.
+# - public includes don't end up depending on private src/ headers.
+# - We use consistently <angle brackets> for other libraries.
+
+import os
+import re
+import subprocess
+import sys
+
+ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+
+# TODO(primiano): fix violation from protozero -> base/logging.h .
+WHITELIST = [('include/perfetto/protozero/.*', 'perfetto/ext/base/logging.h')]
+
+
+def whitelisted(rel_path, incl):
+  for path_regex, incl_regex in WHITELIST:
+    if re.match(path_regex, rel_path) and re.match(incl_regex, incl):
+      return True
+  return False
+
+
+def main():
+  errors = 0
+  include_root = os.path.join(ROOT_DIR, 'include')
+  for root, _, files in os.walk(include_root):
+    for fname in files:
+      fpath = os.path.join(root, fname)
+      rel_path = os.path.relpath(fpath, ROOT_DIR)
+      if not os.path.isfile(fpath):
+        continue
+      if fpath.endswith('.cc'):
+        sys.stderr.write(
+            '.cc files not allowed in includes/ : ' + rel_path + '\n')
+        errors += 1
+        continue
+      if fpath.endswith('.h'):
+        with open(fpath) as f:
+          lines = f.readlines()
+        for line in lines:
+          m = re.findall(r'^#include "(.*\.h)"', line)
+          if not m:
+            continue
+          incl = m[0]
+
+          # Allow only #include "perfetto/..." and not "src/..." from include/.
+          if not incl.startswith('perfetto/'):
+            sys.stderr.write(
+                ('Public header %s is trying to include %s which is outside ' +
+                 'of include/. If you are trying to include a library use ' +
+                 ' <angle brackets> instead\n') % (rel_path, incl))
+            errors += 1
+            continue
+
+          # Public (non-/ext/) headers cannot include /ext/ headers.
+          if (not rel_path.startswith('include/perfetto/ext/') and
+                  incl.startswith('perfetto/ext/') and
+                  not whitelisted(rel_path, incl)):
+            sys.stderr.write(('Public header %s cannot include the non-public' +
+                              '/ext/ header %s.\n') % (rel_path, incl))
+            errors += 1
+            continue
+
+  return 0 if errors == 0 else 1
+
+
+if __name__ == '__main__':
+  sys.exit(main())
diff --git a/tools/diff_test_trace_processor.py b/tools/diff_test_trace_processor.py
index 08e15f5..14b0801 100755
--- a/tools/diff_test_trace_processor.py
+++ b/tools/diff_test_trace_processor.py
@@ -62,7 +62,13 @@
   with open(expected_path, "r") as expected_file:
     expected = expected_file.read()
 
-  cmd = [trace_processor_path, '--run-metrics', metric, gen_trace_path]
+  cmd = [
+    trace_processor_path,
+    '--run-metrics',
+    metric,
+    '--metrics-output=binary',
+    gen_trace_path,
+  ]
   actual = subprocess.check_output(cmd)
 
   # Expected will be in text proto format and we'll need to parse it to a real
@@ -144,7 +150,7 @@
   else:
     out_path = os.path.dirname(args.trace_processor)
     trace_protos_path = os.path.join(
-        out_path, "gcc_like_host", "gen", "protos", "trace")
+        out_path, "gen", "protos", "trace")
     trace_descriptor_path = os.path.join(trace_protos_path, "trace.descriptor")
 
   if args.metrics_descriptor:
@@ -152,7 +158,7 @@
   else:
     out_path = os.path.dirname(args.trace_processor)
     metrics_protos_path = os.path.join(
-        out_path, "gcc_like_host", "gen", "protos", "perfetto", "metrics")
+        out_path, "gen", "protos", "perfetto", "metrics")
     metrics_descriptor_path = os.path.join(metrics_protos_path,
                                            "metrics.descriptor")
 
diff --git a/tools/dump_ftrace_stats/main.cc b/tools/dump_ftrace_stats/main.cc
index 032aaa9..c28812d 100644
--- a/tools/dump_ftrace_stats/main.cc
+++ b/tools/dump_ftrace_stats/main.cc
@@ -16,12 +16,12 @@
 
 #include <signal.h>
 
-#include "perfetto/base/file_utils.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/string_utils.h"
-#include "perfetto/base/string_writer.h"
-#include "perfetto/base/unix_task_runner.h"
-#include "perfetto/base/utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/string_utils.h"
+#include "perfetto/ext/base/string_writer.h"
+#include "perfetto/ext/base/unix_task_runner.h"
+#include "perfetto/ext/base/utils.h"
 
 namespace perfetto {
 namespace {
diff --git a/tools/ftrace_proto_gen/ftrace_proto_gen.cc b/tools/ftrace_proto_gen/ftrace_proto_gen.cc
index 72dcbf8..1b7f7ed 100644
--- a/tools/ftrace_proto_gen/ftrace_proto_gen.cc
+++ b/tools/ftrace_proto_gen/ftrace_proto_gen.cc
@@ -23,11 +23,11 @@
 #include <fstream>
 #include <regex>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/pipe.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/string_utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/pipe.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/string_utils.h"
 
 namespace perfetto {
 
diff --git a/tools/ftrace_proto_gen/ftrace_proto_gen_unittest.cc b/tools/ftrace_proto_gen/ftrace_proto_gen_unittest.cc
index 8943e7b..346cc94 100644
--- a/tools/ftrace_proto_gen/ftrace_proto_gen_unittest.cc
+++ b/tools/ftrace_proto_gen/ftrace_proto_gen_unittest.cc
@@ -15,7 +15,7 @@
  */
 
 #include "tools/ftrace_proto_gen/ftrace_proto_gen.h"
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace {
diff --git a/tools/ftrace_proto_gen/main.cc b/tools/ftrace_proto_gen/main.cc
index de15591..082acf5 100644
--- a/tools/ftrace_proto_gen/main.cc
+++ b/tools/ftrace_proto_gen/main.cc
@@ -27,8 +27,8 @@
 #include <google/protobuf/descriptor.h>
 #include <google/protobuf/descriptor.pb.h>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
+#include "perfetto/ext/base/file_utils.h"
 #include "src/traced/probes/ftrace/format_parser.h"
 #include "tools/ftrace_proto_gen/ftrace_descriptor_gen.h"
 #include "tools/ftrace_proto_gen/ftrace_proto_gen.h"
diff --git a/tools/ftrace_proto_gen/proto_gen_utils.cc b/tools/ftrace_proto_gen/proto_gen_utils.cc
index 9e9f34e..79f0ea8 100644
--- a/tools/ftrace_proto_gen/proto_gen_utils.cc
+++ b/tools/ftrace_proto_gen/proto_gen_utils.cc
@@ -23,11 +23,11 @@
 #include <fstream>
 #include <regex>
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/pipe.h"
-#include "perfetto/base/string_splitter.h"
-#include "perfetto/base/string_utils.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/pipe.h"
+#include "perfetto/ext/base/string_splitter.h"
+#include "perfetto/ext/base/string_utils.h"
 
 namespace perfetto {
 
diff --git a/tools/gen_amalgamated b/tools/gen_amalgamated
index 20c64cb..77bc463 100755
--- a/tools/gen_amalgamated
+++ b/tools/gen_amalgamated
@@ -53,6 +53,12 @@
 # Includes which will be removed from the generated source.
 includes_to_remove = r'^(gtest).*$'
 
+default_cflags = [
+    # Since we're expanding header files into the generated source file, some
+    # constant may remain unused.
+    '-Wno-unused-const-variable'
+]
+
 # Build flags to satisfy a protobuf (lite or full) dependency.
 protobuf_cflags = [
     # Note that these point to the local copy of protobuf in buildtools. In
@@ -82,10 +88,6 @@
         'cflags': protobuf_cflags,
     },
     '//buildtools:protoc_lib': {'libs': ['protoc']},
-    # This prevents us from expanding the prod-only gtest header into a full
-    # gtest dependency. This also requires some patching in headers -- see
-    # AmalgamatedProject._patch_header() below.
-    '//gn:gtest_prod_config': {},
 }
 
 # ----------------------------------------------------------------------------
@@ -183,13 +185,15 @@
         self.source_deps = source_deps
         self.header = []
         self.source = []
-        self.cflags = set()  # Note that we don't support multi-arg flags.
+        # Note that we don't support multi-arg flags.
+        self.cflags = set(default_cflags)
         self.ldflags = set()
         self.defines = set()
         self.libs = set()
         self._dependency_tree = DependencyTree()
-        self._included_sources = set()
-        self._included_headers = set()
+        self._processed_sources = set()
+        self._processed_headers = set()
+        self._processed_source_headers = set()  # Header files included from .cc
         self._include_re = re.compile(r'#include "(.*)"')
 
     def add_target(self, target_name):
@@ -197,6 +201,7 @@
         self._dependency_tree.add_dependency(None, target_name)
         self._add_target_dependencies(target_name)
         self._add_target_flags(target_name)
+        self._add_target_headers(target_name)
 
     def _iterate_dep_edges(self, target_name):
         target = self.desc[target_name]
@@ -260,6 +265,15 @@
             self.defines.update(
                 apply_whitelist(define_whitelist, target.get('defines', [])))
 
+    def _add_target_headers(self, target_name):
+        target = self.desc[target_name]
+        if not 'sources' in target:
+            return
+        headers = [gn_utils.label_to_path(s)
+                   for s in target['sources'] if s.endswith('.h')]
+        for header in headers:
+            self._add_header(target_name, header)
+
     def _get_include_dirs(self, target_name):
         include_dirs = set()
         for target_name in self._iterate_target_and_deps(target_name):
@@ -269,28 +283,30 @@
                     [gn_utils.label_to_path(d) for d in target['include_dirs']])
         return include_dirs
 
-    def _add_header(self, include_dirs, allowed_files, header_name):
-        if header_name in self._included_headers:
+    def _add_source_included_header(
+            self, include_dirs, allowed_files, header_name):
+        if header_name in self._processed_source_headers:
             return
-        self._included_headers.add(header_name)
+        self._processed_source_headers.add(header_name)
         for include_dir in include_dirs:
             full_path = os.path.join(include_dir, header_name)
             if os.path.exists(full_path):
                 if not full_path in allowed_files:
                     return
                 with open(full_path) as f:
-                    self.header.append(
+                    self.source.append(
                         '// %s begin header: %s' % (tool_name, full_path))
-                    self.header.extend(self._patch_header(
-                        self._process_includes(include_dirs, allowed_files, f)))
+                    self.source.extend(
+                        self._process_source_includes(
+                            include_dirs, allowed_files, f))
                 return
         msg = 'Looked in %s' % ', '.join('"%s"' % d for d in include_dirs)
         raise Error('Header file %s not found. %s' % (header_name, msg))
 
     def _add_source(self, target_name, source_name):
-        if source_name in self._included_sources:
+        if source_name in self._processed_sources:
             return
-        self._included_sources.add(source_name)
+        self._processed_sources.add(source_name)
         include_dirs = self._get_include_dirs(target_name)
         deps = self.source_deps[source_name]
         if not os.path.exists(source_name):
@@ -300,20 +316,43 @@
                 '// %s begin source: %s' % (tool_name, source_name))
             try:
                 self.source.extend(self._patch_source(source_name,
-                    self._process_includes(include_dirs, deps, f)))
+                    self._process_source_includes(include_dirs, deps, f)))
             except Error as e:
                 raise Error(
                     'Failed adding source %s: %s' % (source_name, e.message))
 
-    def _patch_header(self, lines):
-        result = []
-        for line in lines:
-            # We don't want to propagate any gtest dependencies into the
-            # result, so remove any macros used from gtest_prod_util.h.
-            if 'FRIEND_TEST' in line:
-                continue
-            result.append(line)
-        return result
+    def _add_header_included_header(self, include_dirs, header_name):
+        if header_name in self._processed_headers:
+            return
+        self._processed_headers.add(header_name)
+        for include_dir in include_dirs:
+            full_path = os.path.join(include_dir, header_name)
+            if os.path.exists(full_path):
+                with open(full_path) as f:
+                    self.header.append(
+                        '// %s begin header: %s' % (tool_name, full_path))
+                    self.header.extend(
+                        self._process_header_includes(include_dirs, f))
+                return
+        msg = 'Looked in %s' % ', '.join('"%s"' % d for d in include_dirs)
+        raise Error('Header file %s not found. %s' % (header_name, msg))
+
+    def _add_header(self, target_name, header_name):
+        if header_name in self._processed_headers:
+            return
+        self._processed_headers.add(header_name)
+        include_dirs = self._get_include_dirs(target_name)
+        if not os.path.exists(header_name):
+            raise Error('Header file %s not found' % source_name)
+        with open(header_name) as f:
+            self.header.append(
+                '// %s begin header: %s' % (tool_name, header_name))
+            try:
+                self.header.extend(
+                    self._process_header_includes(include_dirs, f))
+            except Error as e:
+                raise Error(
+                    'Failed adding header %s: %s' % (header_name, e.message))
 
     def _patch_source(self, source_name, lines):
         result = []
@@ -327,7 +366,7 @@
             result.append(line)
         return result
 
-    def _process_includes(self, include_dirs, allowed_files, file):
+    def _process_source_includes(self, include_dirs, allowed_files, file):
         result = []
         for line in file:
             line = line.rstrip('\n')
@@ -339,7 +378,23 @@
                 result.append('// %s removed: %s' % (tool_name, line))
             else:
                 result.append('// %s expanded: %s' % (tool_name, line))
-                self._add_header(include_dirs, allowed_files, m.group(1))
+                self._add_source_included_header(
+                    include_dirs, allowed_files, m.group(1))
+        return result
+
+    def _process_header_includes(self, include_dirs, file):
+        result = []
+        for line in file:
+            line = line.rstrip('\n')
+            m = self._include_re.match(line)
+            if not m:
+                result.append(line)
+                continue
+            elif re.match(includes_to_remove, m.group(1)):
+                result.append('// %s removed: %s' % (tool_name, line))
+            else:
+                result.append('// %s expanded: %s' % (tool_name, line))
+                self._add_header_included_header(include_dirs, m.group(1))
         return result
 
     def generate(self):
diff --git a/tools/gen_android_bp b/tools/gen_android_bp
index b01a476..4118f94 100755
--- a/tools/gen_android_bp
+++ b/tools/gen_android_bp
@@ -164,7 +164,6 @@
 builtin_deps = {
     '//buildtools:gmock': enable_gmock,
     '//buildtools:gtest': enable_gtest,
-    '//gn:gtest_prod_config': enable_gtest_prod,
     '//buildtools:gtest_main': enable_gtest,
     '//buildtools:libunwind': enable_libunwind,
     '//buildtools:protobuf_full': enable_protobuf_full,
diff --git a/tools/gen_bazel b/tools/gen_bazel
index e83bd96..c430fbe 100755
--- a/tools/gen_bazel
+++ b/tools/gen_bazel
@@ -117,7 +117,6 @@
     '//buildtools:protoc': disable_module,
     '//buildtools:sqlite': enable_sqlite,
     '//gn:default_deps': disable_module,
-    '//gn:gtest_prod_config': enable_gtest_prod,
     '//gn:protoc_lib_deps': enable_protobuf_full,
     '//gn/standalone:gen_git_revision': enable_perfetto_version,
 }
@@ -226,7 +225,7 @@
     assert type in ('cc_binary', 'cc_library', 'cc_proto_library',
                     'proto_library', 'filegroup', 'alias',
                     'pbzero_cc_proto_library', 'genrule',
-                    'transitive_descriptor_set', )
+                    'transitive_descriptor_set', 'java_proto_library' )
     self.type = type
     self.name = name
     self.srcs = set()
@@ -369,7 +368,7 @@
         module_desc: JSON GN description of the module.
         visibility: Whether the module is visible with respect to the target.
     """
-    for src in module_desc['sources']:
+    for src in module_desc.get('sources', []):
       label = Label(gn_utils.label_to_path(src))
       if target.type == 'cc_library' and is_public_header(src):
         target.hdrs.add(label)
@@ -400,7 +399,8 @@
 
       # Any source set which has a source generated by an action doesn't need
       # to be depended on as we will depend on the action directly.
-      if any(src in self.action_generated_files for src in dep_desc['sources']):
+      sources = dep_desc.get('sources', [])
+      if any(src in self.action_generated_files for src in sources):
         return
 
       self.apply_module_sources_to_target(target, dep_desc)
@@ -536,6 +536,15 @@
           Label("//third_party/perfetto/protos:" + proto_target.name))
       self.proto_build.add_target(cc_target)
 
+      java_target_name = proto_target.name + "_java_proto"
+      java_target = Target('java_proto_library',
+                           java_target_name,
+                           gn_name=gn_target_name_no_toolchain)
+      java_target.visibility.add("//visibility:public")
+      java_target.deps.add(
+          Label("//third_party/perfetto/protos:" + proto_target.name))
+      self.proto_build.add_target(java_target)
+
     return (proto_target, cc_target)
 
 
diff --git a/tools/gen_merged_sql_metrics.py b/tools/gen_merged_sql_metrics.py
index 319d0d3..ba261b4 100755
--- a/tools/gen_merged_sql_metrics.py
+++ b/tools/gen_merged_sql_metrics.py
@@ -104,7 +104,8 @@
     for path in sql_outputs.keys():
       name = os.path.basename(path)
       variable = filename_to_variable(os.path.splitext(name)[0])
-      output.write('\n  {{"{}", {}}},\n'.format(path, variable))
+      path_escaped = path.encode('string_escape')
+      output.write('\n  {{"{}", {}}},\n'.format(path_escaped, variable))
     output.write("};\n")
 
     output.write(NAMESPACE_END)
diff --git a/tools/gen_tracing_cpp_headers_from_protos b/tools/gen_tracing_cpp_headers_from_protos
index 44187a7..e02c539 100755
--- a/tools/gen_tracing_cpp_headers_from_protos
+++ b/tools/gen_tracing_cpp_headers_from_protos
@@ -17,7 +17,7 @@
 import subprocess
 import sys
 
-CORE_H = 'include/perfetto/tracing/core'
+CORE_H = 'include/perfetto/ext/tracing/core'
 CORE_CPP = 'src/tracing/core'
 
 PROTOS = (
@@ -40,9 +40,9 @@
 )
 
 
-HEADER_PATH = 'include/perfetto/tracing/core'
+HEADER_PATH = 'include/perfetto/ext/tracing/core'
 CPP_PATH = 'src/tracing/core'
-INCLUDE_PATH = 'perfetto/tracing/core'
+INCLUDE_PATH = 'perfetto/ext/tracing/core'
 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 
diff --git a/tools/heap_profile b/tools/heap_profile
index 96681a3..7f9395a 100755
--- a/tools/heap_profile
+++ b/tools/heap_profile
@@ -137,6 +137,11 @@
                       "profile.")
   parser.add_argument("--no-versions", action="store_true",
                       help="Do not get version information about APKs.")
+  parser.add_argument("--no-running", action="store_true",
+                      help="Do not target already running processes.")
+  parser.add_argument("--no-startup", action="store_true",
+                      help="Do not target processes that start during "
+                      "the profile.")
   parser.add_argument("--shmem-size", help="Size of buffer between client and "
                       "heapprofd. Default 8MiB. Needs to be a power of two "
                       "multiple of 4096, at least 8192.", type=int,
@@ -183,6 +188,10 @@
     target_cfg += "block_client: true\n"
   if args.idle_allocations:
     target_cfg += "idle_allocations: true\n"
+  if args.no_startup:
+    target_cfg += "no_startup: true\n"
+  if args.no_running:
+    target_cfg += "no_running: true\n"
   if args.pid:
     for pid in args.pid.split(','):
       try:
diff --git a/tools/install-build-deps b/tools/install-build-deps
index f8c41dd..aa755e1 100755
--- a/tools/install-build-deps
+++ b/tools/install-build-deps
@@ -44,43 +44,43 @@
 BUILD_DEPS_HOST = [
   # GN
   ('buildtools/mac/gn',
-   'https://storage.googleapis.com/chromium-gn/9be792dd9010ce303a9c3a497a67bcc5ac8c7666',
-   '9be792dd9010ce303a9c3a497a67bcc5ac8c7666',
+   'https://storage.googleapis.com/perfetto/gn-mac-b5b65ca39d93a7cde9fa713be31b114755252f28',
+   'b5b65ca39d93a7cde9fa713be31b114755252f28',
    'darwin'
   ),
   ('buildtools/linux64/gn',
-   'https://storage.googleapis.com/chromium-gn/2f27ff0b6118e5886df976da5effa6003d19d1ce',
-   '2f27ff0b6118e5886df976da5effa6003d19d1ce',
+   'https://storage.googleapis.com/perfetto/gn-linux64-1370d9c5358868b7b66292821b6fe61950826870',
+   '1370d9c5358868b7b66292821b6fe61950826870',
    'linux2'
   ),
 
   # clang-format
   ('buildtools/mac/clang-format',
-   'https://storage.googleapis.com/chromium-clang-format/0679b295e2ce2fce7919d1e8d003e497475f24a3',
-   '0679b295e2ce2fce7919d1e8d003e497475f24a3',
+   'https://storage.googleapis.com/chromium-clang-format/025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
+   '025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
    'darwin'
   ),
   ('buildtools/linux64/clang-format',
-   'https://storage.googleapis.com/chromium-clang-format/5349d1954e17f6ccafb6e6663b0f13cdb2bb33c8',
-   '5349d1954e17f6ccafb6e6663b0f13cdb2bb33c8',
+   'https://storage.googleapis.com/chromium-clang-format/942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
+   '942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
    'linux2'
   ),
   # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
   ('buildtools/clang_format/script',
    'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
-   '0653eee0c81ea04715c635dd0885e8096ff6ba6d',
+   '96636aa0e9f047f17447f2d45a094d0b59ed7917',
    'all'
   ),
 
   # Ninja
   ('buildtools/mac/ninja',
-   'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/mac/a1db595e824c50cf565fbf0af2437fd91b7babf4',
-   'a1db595e824c50cf565fbf0af2437fd91b7babf4',
+   'https://storage.googleapis.com/perfetto/ninja-mac-c15b0698da038b2bd2e8970c14c75fadc06b1add',
+   'c15b0698da038b2bd2e8970c14c75fadc06b1add',
    'darwin'
   ),
   ('buildtools/linux64/ninja',
-   'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/linux64/d35b36c84a09f7e38b25947cafada10e8bf835bc',
-   'd35b36c84a09f7e38b25947cafada10e8bf835bc',
+   'https://storage.googleapis.com/perfetto/ninja-linux64-c866952bda50c29a669222477309287119bbb7e8',
+   'c866952bda50c29a669222477309287119bbb7e8',
    'linux2'
   ),
 
@@ -102,25 +102,25 @@
   # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
   ('buildtools/libcxx',
    'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
-   '2199647acb904b91eea0a5e045f5b227c87d6e85',
+   '5938e0582bac570a41edb3d6a2217c299adc1bc6',
    'all'
   ),
   ('buildtools/libcxxabi',
    'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
-   'c3f4753f7139c73063304235781e4f7788a94c06',
+   '0d529660e32d77d9111912d73f2c74fc5fa2a858',
    'all'
   ),
   ('buildtools/libunwind',
    'https://chromium.googlesource.com/external/llvm.org/libunwind.git',
-   '317087cfd8e608bd24e53934d59b5b85e0a9ded6',
+   '69d9b84cca8354117b9fe9705a4430d789ee599b',
    'all'
   ),
 
-  # Keep the revision in sync with Chrome's CLANG_REVISION in
+  # Keep the revision in sync with Chrome's PACKAGE_VERSION in
   # tools/clang/scripts/update.py.
   ('buildtools/clang.tgz',
-   'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-346388-1.tgz',
-   'c2998d67a9c623fe12e01a33e8b7cf437b396099',
+   'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-361212-67510fac-4.tgz',
+   '5bdd995f13a8b67c98062394d0869c6a409e02b8',
    'linux2'
   ),
 
diff --git a/tools/sanitizers_unittests/sanitizers_unittest.cc b/tools/sanitizers_unittests/sanitizers_unittest.cc
index 5f61f2f..85de65c 100644
--- a/tools/sanitizers_unittests/sanitizers_unittest.cc
+++ b/tools/sanitizers_unittests/sanitizers_unittest.cc
@@ -21,7 +21,7 @@
 
 #include <memory>
 
-#include "gtest/gtest.h"
+#include <gtest/gtest.h>
 
 namespace perfetto {
 namespace {
diff --git a/tools/skippy/skippy.cc b/tools/skippy/skippy.cc
index 7c33ea8..f2e202f 100644
--- a/tools/skippy/skippy.cc
+++ b/tools/skippy/skippy.cc
@@ -20,7 +20,7 @@
 #include <unistd.h>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/time.h"
+#include "perfetto/ext/base/time.h"
 
 // Skippy is a program that produces a visually identifiable stepping pattern
 // in the systrace UI that is useful for debugging dropped or corrupted data.
diff --git a/tools/trace_processor b/tools/trace_processor
new file mode 100755
index 0000000..8301889
--- /dev/null
+++ b/tools/trace_processor
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+# Copyright (C) 2019 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.
+
+# This file should do the same thing when being invoked in any of these ways:
+# ./trace_processor
+# python trace_processor
+# bash trace_processor
+# cat ./trace_processor | bash
+# cat ./trace_processor | python -
+
+BASH_FALLBACK=""" "
+exec python - "$@" <<'#'EOF
+#"""
+
+import hashlib
+import os
+import sys
+import tempfile
+import urllib
+
+TRACE_PROCESSOR_SHELL_SHAS = {
+  'linux': 'c8208066d1b88f42abc59c3389acce62ae0e8773',
+  'mac': '66df2dc8c52c2c93569aa34d7997cdcd7bd09e3b',
+}
+TRACE_PROCESSOR_SHELL_PATH = tempfile.gettempdir()
+TRACE_PROCESSOR_SHELL_BASE_URL = (
+    'https://storage.googleapis.com/perfetto/')
+
+def check_hash(file_name, sha_value):
+  with open(file_name, 'rb') as fd:
+    file_hash = hashlib.sha1(fd.read()).hexdigest()
+    return file_hash == sha_value
+
+def load_trace_processor_shell(platform):
+  sha_value = TRACE_PROCESSOR_SHELL_SHAS[platform]
+  file_name = 'trace_processor_shell-' + platform + '-' + sha_value
+  local_file = os.path.join(TRACE_PROCESSOR_SHELL_PATH, file_name)
+
+  if os.path.exists(local_file):
+    if not check_hash(local_file, sha_value):
+      os.remove(local_file)
+    else:
+      return local_file
+
+  url = TRACE_PROCESSOR_SHELL_BASE_URL + file_name
+  urllib.urlretrieve(url, local_file)
+  if not check_hash(local_file, sha_value):
+    os.remove(local_file)
+    raise ValueError("Invalid signature.")
+  os.chmod(local_file, 0o755)
+  return local_file
+
+def main(argv):
+  platform = None
+  if sys.platform.startswith('linux'):
+    platform = 'linux'
+  elif sys.platform.startswith('darwin'):
+    platform = 'mac'
+  else:
+    print("Invalid platform: {}".format(sys.platform))
+    return 1
+
+  trace_processor_shell_binary = load_trace_processor_shell(platform)
+  os.execv(trace_processor_shell_binary,
+           [trace_processor_shell_binary] + argv[1:])
+
+if __name__ == '__main__':
+  sys.exit(main(sys.argv))
+
+#EOF
diff --git a/tools/trace_to_text/BUILD.gn b/tools/trace_to_text/BUILD.gn
index 94f1c06..82a2de7 100644
--- a/tools/trace_to_text/BUILD.gn
+++ b/tools/trace_to_text/BUILD.gn
@@ -22,8 +22,7 @@
   public_deps = [
     "../../gn:default_deps",
     "../../include/perfetto/base",
-    "../../include/perfetto/trace_processor:trace_processor",
-    "../../include/perfetto/traced:sys_stats_counters",
+    "../../include/perfetto/ext/traced:sys_stats_counters",
     "../../protos/perfetto/trace:lite",
     "../../protos/perfetto/trace/ftrace:lite",
     "../../protos/perfetto/trace/profiling:lite",
diff --git a/tools/trace_to_text/trace_to_profile.cc b/tools/trace_to_text/trace_to_profile.cc
index fce5f0b..1170a39 100644
--- a/tools/trace_to_text/trace_to_profile.cc
+++ b/tools/trace_to_text/trace_to_profile.cc
@@ -26,9 +26,10 @@
 
 #include "tools/trace_to_text/utils.h"
 
-#include "perfetto/base/file_utils.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/temp_file.h"
+#include "perfetto/ext/base/file_utils.h"
+#include "perfetto/ext/base/temp_file.h"
+#include "perfetto/ext/base/utils.h"
 
 #include "perfetto/trace/profiling/profile_packet.pb.h"
 #include "perfetto/trace/trace.pb.h"
diff --git a/tools/trace_to_text/trace_to_systrace.cc b/tools/trace_to_text/trace_to_systrace.cc
index 0b5d793..ffb503e 100644
--- a/tools/trace_to_text/trace_to_systrace.cc
+++ b/tools/trace_to_text/trace_to_systrace.cc
@@ -27,8 +27,9 @@
 
 #include "perfetto/base/build_config.h"
 #include "perfetto/base/logging.h"
-#include "perfetto/base/paged_memory.h"
-#include "perfetto/base/string_writer.h"
+#include "perfetto/ext/base/paged_memory.h"
+#include "perfetto/ext/base/string_writer.h"
+#include "perfetto/ext/base/utils.h"
 #include "perfetto/trace_processor/trace_processor.h"
 
 // When running in Web Assembly, fflush() is a no-op and the stdio buffering
diff --git a/tools/trace_to_text/trace_to_text.cc b/tools/trace_to_text/trace_to_text.cc
index 872fe58..899192f 100644
--- a/tools/trace_to_text/trace_to_text.cc
+++ b/tools/trace_to_text/trace_to_text.cc
@@ -22,7 +22,7 @@
 #include <google/protobuf/text_format.h>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/base/scoped_file.h"
+#include "perfetto/ext/base/scoped_file.h"
 #include "tools/trace_to_text/proto_full_utils.h"
 #include "tools/trace_to_text/utils.h"
 
diff --git a/tools/trace_to_text/utils.cc b/tools/trace_to_text/utils.cc
index 111f5d4..328d25e 100644
--- a/tools/trace_to_text/utils.cc
+++ b/tools/trace_to_text/utils.cc
@@ -24,8 +24,8 @@
 #include <utility>
 
 #include "perfetto/base/logging.h"
+#include "perfetto/ext/traced/sys_stats_counters.h"
 #include "perfetto/trace/ftrace/ftrace_stats.pb.h"
-#include "perfetto/traced/sys_stats_counters.h"
 
 #include "perfetto/trace/trace.pb.h"
 #include "perfetto/trace/trace_packet.pb.h"
diff --git a/tools/update_trace_processor b/tools/update_trace_processor
new file mode 100755
index 0000000..6601ca9
--- /dev/null
+++ b/tools/update_trace_processor
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+set -e
+
+DIR=$(mktemp -d out/perfetto.XXXXXX)
+
+function cleanup {
+  rm -rf "$DIR"
+  echo "Deleted temp working directory $DIR"
+}
+
+#trap cleanup EXIT
+
+function is_mac {
+  ! test -d /proc
+  return $?
+}
+
+tools/gn gen $DIR --args='is_clang=true is_debug=false'
+tools/ninja -C $DIR trace_processor_shell
+
+if which shasum; then
+  NEW_SHA=$(shasum $DIR/trace_processor_shell | cut -f1 -d' ') # Mac OS
+else
+  NEW_SHA=$(sha1sum $DIR/trace_processor_shell | cut -f1 -d' ') # Linux
+fi
+
+if is_mac; then
+  platform=mac
+else
+  platform=linux
+fi
+
+name=trace_processor_shell-$platform-$NEW_SHA
+
+gsutil cp $DIR/trace_processor_shell gs://perfetto/$name
+gsutil acl ch -u AllUsers:R gs://perfetto/$name
+
+echo 'Now run the following command to update tools/trace_processor:'
+echo "sed \"s/'$platform': '[^']*',/'$platform': '$NEW_SHA',/\" --in-place tools/trace_processor"
diff --git a/ui/src/common/actions.ts b/ui/src/common/actions.ts
index 2ae7a31..844448b 100644
--- a/ui/src/common/actions.ts
+++ b/ui/src/common/actions.ts
@@ -25,10 +25,19 @@
   State,
   Status,
   TraceTime,
+  TrackState,
 } from './state';
 
 type StateDraft = Draft<State>;
 
+export interface AddTrackArgs {
+  id?: string;
+  engineId: string;
+  kind: string;
+  name: string;
+  trackGroup?: string;
+  config: {};
+}
 
 function clearTraceState(state: StateDraft) {
   const nextId = state.nextId;
@@ -73,7 +82,20 @@
     state.route = `/viewer`;
   },
 
-  addTrack(state: StateDraft, args: {
+  addTracks(state: StateDraft, args: {tracks: AddTrackArgs[]}) {
+    args.tracks.forEach(track => {
+      const id = track.id === undefined ? `${state.nextId++}` : track.id;
+      track.id = id;
+      state.tracks[id] = track as TrackState;
+      if (track.trackGroup === SCROLLING_TRACK_GROUP) {
+        state.scrollingTracks.push(id);
+      } else if (track.trackGroup !== undefined) {
+        assertExists(state.trackGroups[track.trackGroup]).tracks.push(id);
+      }
+    });
+  },
+
+  addTrack(state: State, args: {
     id?: string; engineId: string; kind: string; name: string;
     trackGroup?: string;
     config: {};
diff --git a/ui/src/common/state.ts b/ui/src/common/state.ts
index 2f250c2..ee4a025 100644
--- a/ui/src/common/state.ts
+++ b/ui/src/common/state.ts
@@ -178,6 +178,7 @@
   cpuFreq: boolean;
   cpuCoarse: boolean;
   cpuCoarsePollMs: number;
+  cpuSyscall: boolean;
 
   ftrace: boolean;
   atrace: boolean;
@@ -220,6 +221,7 @@
     cpuSched: false,
     cpuLatency: false,
     cpuFreq: false,
+    cpuSyscall: false,
 
     ftrace: false,
     atrace: false,
diff --git a/ui/src/controller/record_controller.ts b/ui/src/controller/record_controller.ts
index 9d1bf84..7f62ba6 100644
--- a/ui/src/controller/record_controller.ts
+++ b/ui/src/controller/record_controller.ts
@@ -90,6 +90,11 @@
     ftraceEvents.add('power/suspend_resume');
   }
 
+  if (uiCfg.cpuSyscall) {
+    ftraceEvents.add('raw_syscalls/sys_enter');
+    ftraceEvents.add('raw_syscalls/sys_exit');
+  }
+
   if (procThreadAssociationFtrace) {
     ftraceEvents.add('sched/sched_process_exit');
     ftraceEvents.add('sched/sched_process_free');
diff --git a/ui/src/controller/trace_controller.ts b/ui/src/controller/trace_controller.ts
index dd3dc3f..937fb6a 100644
--- a/ui/src/controller/trace_controller.ts
+++ b/ui/src/controller/trace_controller.ts
@@ -19,6 +19,7 @@
 import {assertExists, assertTrue} from '../base/logging';
 import {
   Actions,
+  AddTrackArgs,
   DeferredAction,
 } from '../common/actions';
 import {Engine} from '../common/engine';
@@ -48,7 +49,6 @@
 
 type States = 'init'|'loading_trace'|'ready';
 
-
 declare interface FileReaderSync { readAsArrayBuffer(blob: Blob): ArrayBuffer; }
 
 declare var FileReaderSync:
@@ -221,8 +221,8 @@
     this.updateStatus('Loading tracks');
 
     const engine = assertExists<Engine>(this.engine);
-    const addToTrackActions: DeferredAction[] = [];
     const numCpus = await engine.getNumberOfCpus();
+    const tracksToAdd: AddTrackArgs[] = [];
 
     // TODO(hjd): Renable Vsync tracks when fixed.
     //// TODO(hjd): Move this code out of TraceController.
@@ -250,7 +250,7 @@
     `);
 
     for (let cpu = 0; cpu < numCpus; cpu++) {
-      addToTrackActions.push(Actions.addTrack({
+      tracksToAdd.push({
         engineId: this.engineId,
         kind: CPU_SLICE_TRACK_KIND,
         name: `Cpu ${cpu}`,
@@ -258,7 +258,7 @@
         config: {
           cpu,
         }
-      }));
+      });
     }
 
     for (let cpu = 0; cpu < numCpus; cpu++) {
@@ -273,7 +273,7 @@
         limit 1;
       `);
       if (freqExists.numRecords > 0) {
-        addToTrackActions.push(Actions.addTrack({
+        tracksToAdd.push({
           engineId: this.engineId,
           kind: CPU_FREQ_TRACK_KIND,
           name: `Cpu ${cpu} Frequency`,
@@ -282,42 +282,49 @@
             cpu,
             maximumValue: +maxFreq.columns[0].doubleValues![0],
           }
-        }));
+        });
       }
     }
 
     const counters = await engine.query(`
-      select name, ref, ref_type, count(ref_type)
+      select name, ref, ref_type
       from counter_definitions
       where ref is not null
       group by name, ref, ref_type
       order by ref_type desc
     `);
-    const counterUpids = new Set<number>();
-    const counterUtids = new Set<number>();
-    for (let i = 0; i < counters.numRecords; i++) {
-      const ref = +counters.columns[1].longValues![i];
-      const refType = counters.columns[2].stringValues![i];
-      if (refType === 'upid') counterUpids.add(ref);
-      if (refType === 'utid') counterUtids.add(ref);
+
+    interface CounterMap {
+      [index: number]: string[];
     }
 
-    // Add all the global counter tracks that are not bound to any pid/tid,
-    // the ones for which refType == NULL.
+    const counterUpids: CounterMap = new Array();
+    const counterUtids: CounterMap = new Array();
     for (let i = 0; i < counters.numRecords; i++) {
       const name = counters.columns[0].stringValues![i];
+      const ref = +counters.columns[1].longValues![i];
       const refType = counters.columns[2].stringValues![i];
-      if (refType !== '[NULL]') continue;
-      addToTrackActions.push(Actions.addTrack({
-        engineId: this.engineId,
-        kind: 'CounterTrack',
-        name,
-        trackGroup: SCROLLING_TRACK_GROUP,
-        config: {
+      if (refType === 'upid') {
+        const el = counterUpids[ref];
+        el === undefined ? counterUpids[ref] = [name] :
+                           counterUpids[ref].push(name);
+      } else if (refType === 'utid') {
+        const el = counterUtids[ref];
+        el === undefined ? counterUtids[ref] = [name] :
+                           counterUtids[ref].push(name);
+      } else if (refType === '[NULL]') {
+        // Add global counter tracks that are not bound to any pid/tid.
+        tracksToAdd.push({
+          engineId: this.engineId,
+          kind: 'CounterTrack',
           name,
-          ref: 0,
-        }
-      }));
+          trackGroup: SCROLLING_TRACK_GROUP,
+          config: {
+            name,
+            ref: 0,
+          }
+        });
+      }
     }
 
     // Local experiments shows getting maxDepth separately is ~2x faster than
@@ -357,7 +364,6 @@
 
     const upidToUuid = new Map<number, string>();
     const utidToUuid = new Map<number, string>();
-    const addSummaryTrackActions: DeferredAction[] = [];
     const addTrackGroupActions: DeferredAction[] = [];
 
     for (const row of rawQueryToRows(threadQuery, {
@@ -382,13 +388,14 @@
 
       const maxDepth = utid === null ? undefined : utidToMaxDepth.get(utid);
       if (maxDepth === undefined &&
-          (upid === null || !counterUpids.has(upid)) &&
-          !counterUtids.has(utid) && !threadHasSched) {
+          (upid === null || counterUpids[upid] === undefined) &&
+          counterUtids[utid] === undefined && !threadHasSched) {
         continue;
       }
 
       // Group by upid if present else by utid.
       let pUuid = upid === null ? utidToUuid.get(utid) : upidToUuid.get(upid);
+      // These should only happen once for each track group.
       if (pUuid === undefined) {
         pUuid = uuidv4();
         const summaryTrackId = uuidv4();
@@ -401,13 +408,14 @@
         const pidForColor = pid || tid || upid || utid || 0;
         const kind = hasSchedEvents ? PROCESS_SCHEDULING_TRACK_KIND :
                                       PROCESS_SUMMARY_TRACK;
-        addSummaryTrackActions.push(Actions.addTrack({
+
+        tracksToAdd.push({
           id: summaryTrackId,
           engineId: this.engineId,
           kind,
           name: `${upid === null ? tid : pid} summary`,
           config: {pidForColor, upid, utid},
-        }));
+        });
 
         addTrackGroupActions.push(Actions.addTrackGroup({
           engineId: this.engineId,
@@ -418,77 +426,73 @@
           collapsed: true,
         }));
 
-        for (let i = 0; i < counters.numRecords; i++) {
-          const name = counters.columns[0].stringValues![i];
-          const ref = counters.columns[1].longValues![i];
-          const refType = counters.columns[2].stringValues![i];
-          if (refType !== 'upid' || ref !== upid) continue;
-          addTrackGroupActions.push(Actions.addTrack({
-            engineId: this.engineId,
-            kind: 'CounterTrack',
-            name,
-            trackGroup: pUuid,
-            config: {
-              name,
-              ref,
-            }
-          }));
+        if (upid !== null) {
+          const counterNames = counterUpids[upid];
+          if (counterNames !== undefined) {
+            counterNames.forEach(element => {
+              tracksToAdd.push({
+                engineId: this.engineId,
+                kind: 'CounterTrack',
+                name: element,
+                trackGroup: pUuid,
+                config: {
+                  name: element,
+                  ref: upid,
+                }
+              });
+            });
+          }
         }
       }
-
-      for (let i = 0; i < counters.numRecords; i++) {
-        const name = counters.columns[0].stringValues![i];
-        const ref = counters.columns[1].longValues![i];
-        const refType = counters.columns[2].stringValues![i];
-
-        if (refType !== 'utid' || ref !== utid) continue;
-        addTrackGroupActions.push(Actions.addTrack({
-          engineId: this.engineId,
-          kind: 'CounterTrack',
-          name,
-          trackGroup: pUuid,
-          config: {
-            name,
-            ref,
-          }
-        }));
+      const counterThreadNames = counterUtids[utid];
+      if (counterThreadNames !== undefined) {
+        counterThreadNames.forEach(element => {
+          tracksToAdd.push({
+            engineId: this.engineId,
+            kind: 'CounterTrack',
+            name: element,
+            trackGroup: pUuid,
+            config: {
+              name: element,
+              ref: utid,
+            }
+          });
+        });
       }
-
       if (threadHasSched) {
-        addToTrackActions.push(Actions.addTrack({
+        tracksToAdd.push({
           engineId: this.engineId,
           kind: THREAD_STATE_TRACK_KIND,
           name: `${threadName} [${tid}]`,
           trackGroup: pUuid,
           config: {utid}
-        }));
+        });
       }
 
       if (maxDepth !== undefined) {
-        addToTrackActions.push(Actions.addTrack({
+        tracksToAdd.push({
           engineId: this.engineId,
           kind: SLICE_TRACK_KIND,
           name: `${threadName} [${tid}]`,
           trackGroup: pUuid,
           config: {upid, utid, maxDepth},
-        }));
+        });
       }
     }
 
     const logCount = await engine.query(`select count(1) from android_logs`);
     if (logCount.columns[0].longValues![0] > 0) {
-      addToTrackActions.push(Actions.addTrack({
+      tracksToAdd.push({
         engineId: this.engineId,
         kind: ANDROID_LOGS_TRACK_KIND,
         name: 'Android logs',
         trackGroup: SCROLLING_TRACK_GROUP,
         config: {}
-      }));
+      });
     }
 
-    const allActions =
-        addSummaryTrackActions.concat(addTrackGroupActions, addToTrackActions);
-    globals.dispatchMultiple(allActions);
+    addTrackGroupActions.push(Actions.addTracks({tracks: tracksToAdd}));
+    globals.dispatchMultiple(addTrackGroupActions);
   }
 
   private async listThreads() {
diff --git a/ui/src/frontend/record_page.ts b/ui/src/frontend/record_page.ts
index f2072ea..207b261 100644
--- a/ui/src/frontend/record_page.ts
+++ b/ui/src/frontend/record_page.ts
@@ -237,6 +237,12 @@
                 task Y that X's transition (e.g. posting a semaphore).`,
         setEnabled: (cfg, val) => cfg.cpuLatency = val,
         isEnabled: (cfg) => cfg.cpuLatency
+      } as ProbeAttrs),
+      m(Probe, {
+        title: 'Syscalls',
+        descr: `Tracks the enter and exit of all syscalls.`,
+        setEnabled: (cfg, val) => cfg.cpuSyscall = val,
+        isEnabled: (cfg) => cfg.cpuSyscall
       } as ProbeAttrs));
 }