processor: Make sql/sqlite layer optional at compile time

Makes it possible to disable TraceProcessor's SQL support at compile
time, to reduce the binary size for proto2json use in chromium.

We do this by splitting off a TraceProcessorStorage interface and
TraceProcessorStorageImpl base class from TraceProcessor/Impl, which
provides trace parsing and storage.

Similarly, this patch adds a separate the trace_processor:storage
target, on top of which the :lib target adds SQL support and its
dependencies.

Bug: 141459049
Change-Id: Ic651c8b1106402425a707dae850afbd962d2f9ee
diff --git a/Android.bp b/Android.bp
index e42c857..75f8724 100644
--- a/Android.bp
+++ b/Android.bp
@@ -820,11 +820,6 @@
   name: "perfetto_include_perfetto_ext_ipc_ipc",
 }
 
-// GN: //include/perfetto/ext/trace_processor:export_json
-filegroup {
-  name: "perfetto_include_perfetto_ext_trace_processor_export_json",
-}
-
 // GN: //include/perfetto/ext/traced:sys_stats_counters
 filegroup {
   name: "perfetto_include_perfetto_ext_traced_sys_stats_counters",
@@ -865,6 +860,16 @@
   name: "perfetto_include_perfetto_public_public",
 }
 
+// GN: //include/perfetto/trace_processor:basic_types
+filegroup {
+  name: "perfetto_include_perfetto_trace_processor_basic_types",
+}
+
+// GN: //include/perfetto/trace_processor:storage
+filegroup {
+  name: "perfetto_include_perfetto_trace_processor_storage",
+}
+
 // GN: //include/perfetto/trace_processor:trace_processor
 filegroup {
   name: "perfetto_include_perfetto_trace_processor_trace_processor",
@@ -4542,66 +4547,30 @@
   srcs: [
     "src/trace_processor/android_logs_table.cc",
     "src/trace_processor/args_table.cc",
-    "src/trace_processor/args_tracker.cc",
-    "src/trace_processor/binder_tracker.cc",
-    "src/trace_processor/clock_tracker.cc",
     "src/trace_processor/counter_definitions_table.cc",
     "src/trace_processor/counter_values_table.cc",
     "src/trace_processor/cpu_profile_stack_sample_table.cc",
-    "src/trace_processor/event_tracker.cc",
     "src/trace_processor/filtered_row_index.cc",
-    "src/trace_processor/forwarding_trace_parser.cc",
-    "src/trace_processor/ftrace_utils.cc",
-    "src/trace_processor/gzip_trace_parser.cc",
-    "src/trace_processor/heap_graph_tracker.cc",
     "src/trace_processor/heap_profile_allocation_table.cc",
-    "src/trace_processor/heap_profile_tracker.cc",
-    "src/trace_processor/importers/ftrace/ftrace_descriptors.cc",
-    "src/trace_processor/importers/ftrace/ftrace_parser.cc",
-    "src/trace_processor/importers/ftrace/ftrace_tokenizer.cc",
-    "src/trace_processor/importers/ftrace/sched_event_tracker.cc",
-    "src/trace_processor/importers/fuchsia/fuchsia_provider_view.cc",
-    "src/trace_processor/importers/fuchsia/fuchsia_trace_parser.cc",
-    "src/trace_processor/importers/fuchsia/fuchsia_trace_tokenizer.cc",
-    "src/trace_processor/importers/fuchsia/fuchsia_trace_utils.cc",
-    "src/trace_processor/importers/proto/android_probes_parser.cc",
-    "src/trace_processor/importers/proto/graphics_event_parser.cc",
-    "src/trace_processor/importers/proto/proto_trace_parser.cc",
-    "src/trace_processor/importers/proto/proto_trace_tokenizer.cc",
-    "src/trace_processor/importers/proto/system_probes_parser.cc",
-    "src/trace_processor/importers/proto/track_event_parser.cc",
-    "src/trace_processor/importers/proto/track_event_tokenizer.cc",
-    "src/trace_processor/importers/systrace/systrace_parser.cc",
-    "src/trace_processor/importers/systrace/systrace_trace_parser.cc",
     "src/trace_processor/instants_table.cc",
     "src/trace_processor/metadata_table.cc",
     "src/trace_processor/process_table.cc",
-    "src/trace_processor/process_tracker.cc",
     "src/trace_processor/raw_table.cc",
     "src/trace_processor/read_trace.cc",
     "src/trace_processor/row_iterators.cc",
     "src/trace_processor/sched_slice_table.cc",
     "src/trace_processor/slice_table.cc",
-    "src/trace_processor/slice_tracker.cc",
     "src/trace_processor/span_join_operator_table.cc",
     "src/trace_processor/sql_stats_table.cc",
     "src/trace_processor/stack_profile_frame_table.cc",
     "src/trace_processor/stack_profile_mapping_table.cc",
-    "src/trace_processor/stack_profile_tracker.cc",
     "src/trace_processor/stats_table.cc",
     "src/trace_processor/storage_columns.cc",
     "src/trace_processor/storage_schema.cc",
     "src/trace_processor/storage_table.cc",
-    "src/trace_processor/syscall_tracker.cc",
     "src/trace_processor/thread_table.cc",
     "src/trace_processor/trace_processor.cc",
-    "src/trace_processor/trace_processor_context.cc",
     "src/trace_processor/trace_processor_impl.cc",
-    "src/trace_processor/trace_sorter.cc",
-    "src/trace_processor/trace_storage.cc",
-    "src/trace_processor/track_tracker.cc",
-    "src/trace_processor/virtual_destructors.cc",
-    "src/trace_processor/vulkan_memory_tracker.cc",
     "src/trace_processor/window_operator_table.cc",
   ],
 }
@@ -4643,6 +4612,51 @@
   ],
 }
 
+// GN: //src/trace_processor:storage
+filegroup {
+  name: "perfetto_src_trace_processor_storage",
+  srcs: [
+    "src/trace_processor/args_tracker.cc",
+    "src/trace_processor/binder_tracker.cc",
+    "src/trace_processor/clock_tracker.cc",
+    "src/trace_processor/event_tracker.cc",
+    "src/trace_processor/forwarding_trace_parser.cc",
+    "src/trace_processor/ftrace_utils.cc",
+    "src/trace_processor/gzip_trace_parser.cc",
+    "src/trace_processor/heap_graph_tracker.cc",
+    "src/trace_processor/heap_profile_tracker.cc",
+    "src/trace_processor/importers/ftrace/ftrace_descriptors.cc",
+    "src/trace_processor/importers/ftrace/ftrace_parser.cc",
+    "src/trace_processor/importers/ftrace/ftrace_tokenizer.cc",
+    "src/trace_processor/importers/ftrace/sched_event_tracker.cc",
+    "src/trace_processor/importers/fuchsia/fuchsia_provider_view.cc",
+    "src/trace_processor/importers/fuchsia/fuchsia_trace_parser.cc",
+    "src/trace_processor/importers/fuchsia/fuchsia_trace_tokenizer.cc",
+    "src/trace_processor/importers/fuchsia/fuchsia_trace_utils.cc",
+    "src/trace_processor/importers/proto/android_probes_parser.cc",
+    "src/trace_processor/importers/proto/graphics_event_parser.cc",
+    "src/trace_processor/importers/proto/proto_trace_parser.cc",
+    "src/trace_processor/importers/proto/proto_trace_tokenizer.cc",
+    "src/trace_processor/importers/proto/system_probes_parser.cc",
+    "src/trace_processor/importers/proto/track_event_parser.cc",
+    "src/trace_processor/importers/proto/track_event_tokenizer.cc",
+    "src/trace_processor/importers/systrace/systrace_parser.cc",
+    "src/trace_processor/importers/systrace/systrace_trace_parser.cc",
+    "src/trace_processor/process_tracker.cc",
+    "src/trace_processor/slice_tracker.cc",
+    "src/trace_processor/stack_profile_tracker.cc",
+    "src/trace_processor/syscall_tracker.cc",
+    "src/trace_processor/trace_processor_context.cc",
+    "src/trace_processor/trace_processor_storage.cc",
+    "src/trace_processor/trace_processor_storage_impl.cc",
+    "src/trace_processor/trace_sorter.cc",
+    "src/trace_processor/trace_storage.cc",
+    "src/trace_processor/track_tracker.cc",
+    "src/trace_processor/virtual_destructors.cc",
+    "src/trace_processor/vulkan_memory_tracker.cc",
+  ],
+}
+
 // GN: //src/trace_processor/tables:tables
 filegroup {
   name: "perfetto_src_trace_processor_tables_tables",
@@ -5294,13 +5308,14 @@
     ":perfetto_include_perfetto_base_base",
     ":perfetto_include_perfetto_ext_base_base",
     ":perfetto_include_perfetto_ext_ipc_ipc",
-    ":perfetto_include_perfetto_ext_trace_processor_export_json",
     ":perfetto_include_perfetto_ext_traced_sys_stats_counters",
     ":perfetto_include_perfetto_ext_traced_traced",
     ":perfetto_include_perfetto_ext_tracing_core_core",
     ":perfetto_include_perfetto_ext_tracing_ipc_ipc",
     ":perfetto_include_perfetto_profiling_normalize",
     ":perfetto_include_perfetto_protozero_protozero",
+    ":perfetto_include_perfetto_trace_processor_basic_types",
+    ":perfetto_include_perfetto_trace_processor_storage",
     ":perfetto_include_perfetto_trace_processor_trace_processor",
     ":perfetto_include_perfetto_tracing_core_core",
     ":perfetto_include_perfetto_tracing_tracing",
@@ -5401,6 +5416,7 @@
     ":perfetto_src_trace_processor_metrics_unittests",
     ":perfetto_src_trace_processor_sqlite_sqlite",
     ":perfetto_src_trace_processor_sqlite_unittests",
+    ":perfetto_src_trace_processor_storage",
     ":perfetto_src_trace_processor_tables_tables",
     ":perfetto_src_trace_processor_tables_unittests",
     ":perfetto_src_trace_processor_unittests",
@@ -5574,9 +5590,10 @@
   srcs: [
     ":perfetto_include_perfetto_base_base",
     ":perfetto_include_perfetto_ext_base_base",
-    ":perfetto_include_perfetto_ext_trace_processor_export_json",
     ":perfetto_include_perfetto_ext_traced_sys_stats_counters",
     ":perfetto_include_perfetto_protozero_protozero",
+    ":perfetto_include_perfetto_trace_processor_basic_types",
+    ":perfetto_include_perfetto_trace_processor_storage",
     ":perfetto_include_perfetto_trace_processor_trace_processor",
     ":perfetto_protos_perfetto_common_zero_gen",
     ":perfetto_protos_perfetto_config_android_zero_gen",
@@ -5612,6 +5629,7 @@
     ":perfetto_src_trace_processor_lib",
     ":perfetto_src_trace_processor_metrics_lib",
     ":perfetto_src_trace_processor_sqlite_sqlite",
+    ":perfetto_src_trace_processor_storage",
     ":perfetto_src_trace_processor_tables_tables",
     "src/trace_processor/proto_to_json.cc",
     "src/trace_processor/trace_processor_shell.cc",
@@ -5668,10 +5686,11 @@
   srcs: [
     ":perfetto_include_perfetto_base_base",
     ":perfetto_include_perfetto_ext_base_base",
-    ":perfetto_include_perfetto_ext_trace_processor_export_json",
     ":perfetto_include_perfetto_ext_traced_sys_stats_counters",
     ":perfetto_include_perfetto_profiling_symbolizer",
     ":perfetto_include_perfetto_protozero_protozero",
+    ":perfetto_include_perfetto_trace_processor_basic_types",
+    ":perfetto_include_perfetto_trace_processor_storage",
     ":perfetto_include_perfetto_trace_processor_trace_processor",
     ":perfetto_protos_perfetto_common_lite_gen",
     ":perfetto_protos_perfetto_common_zero_gen",
@@ -5732,6 +5751,7 @@
     ":perfetto_src_trace_processor_lib",
     ":perfetto_src_trace_processor_metrics_lib",
     ":perfetto_src_trace_processor_sqlite_sqlite",
+    ":perfetto_src_trace_processor_storage",
     ":perfetto_src_trace_processor_tables_tables",
     ":perfetto_tools_trace_to_text_common",
     ":perfetto_tools_trace_to_text_full",
diff --git a/BUILD b/BUILD
index 951d322..d5b588f 100644
--- a/BUILD
+++ b/BUILD
@@ -366,13 +366,28 @@
     ],
 )
 
+# GN target: //include/perfetto/trace_processor:basic_types
+filegroup(
+    name = "include_perfetto_trace_processor_basic_types",
+    srcs = [
+        "include/perfetto/trace_processor/basic_types.h",
+        "include/perfetto/trace_processor/status.h",
+    ],
+)
+
+# GN target: //include/perfetto/trace_processor:storage
+filegroup(
+    name = "include_perfetto_trace_processor_storage",
+    srcs = [
+        "include/perfetto/trace_processor/trace_processor_storage.h",
+    ],
+)
+
 # GN target: //include/perfetto/trace_processor:trace_processor
 filegroup(
     name = "include_perfetto_trace_processor_trace_processor",
     srcs = [
-        "include/perfetto/trace_processor/basic_types.h",
         "include/perfetto/trace_processor/read_trace.h",
-        "include/perfetto/trace_processor/status.h",
         "include/perfetto/trace_processor/trace_processor.h",
     ],
 )
@@ -644,6 +659,15 @@
     ],
 )
 
+# GN target: //src/trace_processor:export_json
+filegroup(
+    name = "src_trace_processor_export_json",
+    srcs = [
+        "src/trace_processor/export_json.cc",
+        "src/trace_processor/export_json.h",
+    ],
+)
+
 # GN target: //src/trace_processor:lib
 filegroup(
     name = "src_trace_processor_lib",
@@ -652,6 +676,61 @@
         "src/trace_processor/android_logs_table.h",
         "src/trace_processor/args_table.cc",
         "src/trace_processor/args_table.h",
+        "src/trace_processor/counter_definitions_table.cc",
+        "src/trace_processor/counter_definitions_table.h",
+        "src/trace_processor/counter_values_table.cc",
+        "src/trace_processor/counter_values_table.h",
+        "src/trace_processor/cpu_profile_stack_sample_table.cc",
+        "src/trace_processor/cpu_profile_stack_sample_table.h",
+        "src/trace_processor/filtered_row_index.cc",
+        "src/trace_processor/filtered_row_index.h",
+        "src/trace_processor/heap_profile_allocation_table.cc",
+        "src/trace_processor/heap_profile_allocation_table.h",
+        "src/trace_processor/instants_table.cc",
+        "src/trace_processor/instants_table.h",
+        "src/trace_processor/metadata_table.cc",
+        "src/trace_processor/metadata_table.h",
+        "src/trace_processor/process_table.cc",
+        "src/trace_processor/process_table.h",
+        "src/trace_processor/raw_table.cc",
+        "src/trace_processor/raw_table.h",
+        "src/trace_processor/read_trace.cc",
+        "src/trace_processor/row_iterators.cc",
+        "src/trace_processor/row_iterators.h",
+        "src/trace_processor/sched_slice_table.cc",
+        "src/trace_processor/sched_slice_table.h",
+        "src/trace_processor/slice_table.cc",
+        "src/trace_processor/slice_table.h",
+        "src/trace_processor/span_join_operator_table.cc",
+        "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/stack_profile_frame_table.cc",
+        "src/trace_processor/stack_profile_frame_table.h",
+        "src/trace_processor/stack_profile_mapping_table.cc",
+        "src/trace_processor/stack_profile_mapping_table.h",
+        "src/trace_processor/stats_table.cc",
+        "src/trace_processor/stats_table.h",
+        "src/trace_processor/storage_columns.cc",
+        "src/trace_processor/storage_columns.h",
+        "src/trace_processor/storage_schema.cc",
+        "src/trace_processor/storage_schema.h",
+        "src/trace_processor/storage_table.cc",
+        "src/trace_processor/storage_table.h",
+        "src/trace_processor/thread_table.cc",
+        "src/trace_processor/thread_table.h",
+        "src/trace_processor/trace_processor.cc",
+        "src/trace_processor/trace_processor_impl.cc",
+        "src/trace_processor/trace_processor_impl.h",
+        "src/trace_processor/window_operator_table.cc",
+        "src/trace_processor/window_operator_table.h",
+    ],
+)
+
+# GN target: //src/trace_processor:storage
+filegroup(
+    name = "src_trace_processor_storage",
+    srcs = [
         "src/trace_processor/args_tracker.cc",
         "src/trace_processor/args_tracker.h",
         "src/trace_processor/binder_tracker.cc",
@@ -659,18 +738,8 @@
         "src/trace_processor/chunked_trace_reader.h",
         "src/trace_processor/clock_tracker.cc",
         "src/trace_processor/clock_tracker.h",
-        "src/trace_processor/counter_definitions_table.cc",
-        "src/trace_processor/counter_definitions_table.h",
-        "src/trace_processor/counter_values_table.cc",
-        "src/trace_processor/counter_values_table.h",
-        "src/trace_processor/cpu_profile_stack_sample_table.cc",
-        "src/trace_processor/cpu_profile_stack_sample_table.h",
         "src/trace_processor/event_tracker.cc",
         "src/trace_processor/event_tracker.h",
-        "src/trace_processor/export_json.cc",
-        "src/trace_processor/export_json.h",
-        "src/trace_processor/filtered_row_index.cc",
-        "src/trace_processor/filtered_row_index.h",
         "src/trace_processor/forwarding_trace_parser.cc",
         "src/trace_processor/forwarding_trace_parser.h",
         "src/trace_processor/ftrace_utils.cc",
@@ -679,8 +748,6 @@
         "src/trace_processor/gzip_trace_parser.h",
         "src/trace_processor/heap_graph_tracker.cc",
         "src/trace_processor/heap_graph_tracker.h",
-        "src/trace_processor/heap_profile_allocation_table.cc",
-        "src/trace_processor/heap_profile_allocation_table.h",
         "src/trace_processor/heap_profile_tracker.cc",
         "src/trace_processor/heap_profile_tracker.h",
         "src/trace_processor/importers/ftrace/ftrace_descriptors.cc",
@@ -731,61 +798,28 @@
         "src/trace_processor/importers/systrace/systrace_parser.h",
         "src/trace_processor/importers/systrace/systrace_trace_parser.cc",
         "src/trace_processor/importers/systrace/systrace_trace_parser.h",
-        "src/trace_processor/instants_table.cc",
-        "src/trace_processor/instants_table.h",
         "src/trace_processor/metadata.h",
-        "src/trace_processor/metadata_table.cc",
-        "src/trace_processor/metadata_table.h",
-        "src/trace_processor/process_table.cc",
-        "src/trace_processor/process_table.h",
         "src/trace_processor/process_tracker.cc",
         "src/trace_processor/process_tracker.h",
-        "src/trace_processor/raw_table.cc",
-        "src/trace_processor/raw_table.h",
-        "src/trace_processor/read_trace.cc",
-        "src/trace_processor/row_iterators.cc",
-        "src/trace_processor/row_iterators.h",
-        "src/trace_processor/sched_slice_table.cc",
-        "src/trace_processor/sched_slice_table.h",
-        "src/trace_processor/slice_table.cc",
-        "src/trace_processor/slice_table.h",
         "src/trace_processor/slice_tracker.cc",
         "src/trace_processor/slice_tracker.h",
-        "src/trace_processor/span_join_operator_table.cc",
-        "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/stack_profile_frame_table.cc",
-        "src/trace_processor/stack_profile_frame_table.h",
-        "src/trace_processor/stack_profile_mapping_table.cc",
-        "src/trace_processor/stack_profile_mapping_table.h",
         "src/trace_processor/stack_profile_tracker.cc",
         "src/trace_processor/stack_profile_tracker.h",
         "src/trace_processor/stats.h",
-        "src/trace_processor/stats_table.cc",
-        "src/trace_processor/stats_table.h",
-        "src/trace_processor/storage_columns.cc",
-        "src/trace_processor/storage_columns.h",
-        "src/trace_processor/storage_schema.cc",
-        "src/trace_processor/storage_schema.h",
-        "src/trace_processor/storage_table.cc",
-        "src/trace_processor/storage_table.h",
         "src/trace_processor/syscall_tracker.cc",
         "src/trace_processor/syscall_tracker.h",
         "src/trace_processor/syscalls_aarch32.h",
         "src/trace_processor/syscalls_aarch64.h",
         "src/trace_processor/syscalls_armeabi.h",
         "src/trace_processor/syscalls_x86_64.h",
-        "src/trace_processor/thread_table.cc",
-        "src/trace_processor/thread_table.h",
         "src/trace_processor/timestamped_trace_piece.h",
         "src/trace_processor/trace_blob_view.h",
         "src/trace_processor/trace_parser.h",
-        "src/trace_processor/trace_processor.cc",
         "src/trace_processor/trace_processor_context.cc",
         "src/trace_processor/trace_processor_context.h",
-        "src/trace_processor/trace_processor_impl.cc",
-        "src/trace_processor/trace_processor_impl.h",
+        "src/trace_processor/trace_processor_storage.cc",
+        "src/trace_processor/trace_processor_storage_impl.cc",
+        "src/trace_processor/trace_processor_storage_impl.h",
         "src/trace_processor/trace_sorter.cc",
         "src/trace_processor/trace_sorter.h",
         "src/trace_processor/trace_storage.cc",
@@ -796,8 +830,6 @@
         "src/trace_processor/virtual_destructors.cc",
         "src/trace_processor/vulkan_memory_tracker.cc",
         "src/trace_processor/vulkan_memory_tracker.h",
-        "src/trace_processor/window_operator_table.cc",
-        "src/trace_processor/window_operator_table.h",
     ],
 )
 
@@ -2352,9 +2384,11 @@
         ":src_protozero_protozero",
         ":src_trace_processor_common",
         ":src_trace_processor_db_lib",
+        ":src_trace_processor_export_json",
         ":src_trace_processor_lib",
         ":src_trace_processor_metrics_lib",
         ":src_trace_processor_sqlite_sqlite",
+        ":src_trace_processor_storage",
         ":src_trace_processor_tables_tables",
     ],
     hdrs = [
@@ -2363,6 +2397,8 @@
         ":include_perfetto_ext_trace_processor_export_json",
         ":include_perfetto_ext_traced_sys_stats_counters",
         ":include_perfetto_protozero_protozero",
+        ":include_perfetto_trace_processor_basic_types",
+        ":include_perfetto_trace_processor_storage",
         ":include_perfetto_trace_processor_trace_processor",
     ],
     visibility = [
@@ -2416,17 +2452,21 @@
         ":include_perfetto_ext_trace_processor_export_json",
         ":include_perfetto_ext_traced_sys_stats_counters",
         ":include_perfetto_protozero_protozero",
+        ":include_perfetto_trace_processor_basic_types",
+        ":include_perfetto_trace_processor_storage",
         ":include_perfetto_trace_processor_trace_processor",
         ":src_base_base",
         ":src_base_unix_socket",
         ":src_protozero_protozero",
         ":src_trace_processor_common",
         ":src_trace_processor_db_lib",
+        ":src_trace_processor_export_json",
         ":src_trace_processor_lib",
         ":src_trace_processor_metrics_lib",
         ":src_trace_processor_rpc_httpd",
         ":src_trace_processor_rpc_rpc",
         ":src_trace_processor_sqlite_sqlite",
+        ":src_trace_processor_storage",
         ":src_trace_processor_tables_tables",
     ],
     visibility = [
@@ -2509,9 +2549,11 @@
         ":src_protozero_protozero",
         ":src_trace_processor_common",
         ":src_trace_processor_db_lib",
+        ":src_trace_processor_export_json",
         ":src_trace_processor_lib",
         ":src_trace_processor_metrics_lib",
         ":src_trace_processor_sqlite_sqlite",
+        ":src_trace_processor_storage",
         ":src_trace_processor_tables_tables",
         ":tools_trace_to_text_pprofbuilder",
         ":tools_trace_to_text_symbolizer",
@@ -2524,6 +2566,8 @@
         ":include_perfetto_ext_traced_sys_stats_counters",
         ":include_perfetto_profiling_symbolizer",
         ":include_perfetto_protozero_protozero",
+        ":include_perfetto_trace_processor_basic_types",
+        ":include_perfetto_trace_processor_storage",
         ":include_perfetto_trace_processor_trace_processor",
     ],
     visibility = [
@@ -2600,14 +2644,18 @@
         ":include_perfetto_ext_traced_sys_stats_counters",
         ":include_perfetto_profiling_symbolizer",
         ":include_perfetto_protozero_protozero",
+        ":include_perfetto_trace_processor_basic_types",
+        ":include_perfetto_trace_processor_storage",
         ":include_perfetto_trace_processor_trace_processor",
         ":src_base_base",
         ":src_protozero_protozero",
         ":src_trace_processor_common",
         ":src_trace_processor_db_lib",
+        ":src_trace_processor_export_json",
         ":src_trace_processor_lib",
         ":src_trace_processor_metrics_lib",
         ":src_trace_processor_sqlite_sqlite",
+        ":src_trace_processor_storage",
         ":src_trace_processor_tables_tables",
         ":tools_trace_to_text_common",
         ":tools_trace_to_text_full",
diff --git a/BUILD.gn b/BUILD.gn
index 7557952..965d68e 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -225,16 +225,30 @@
       "protos/perfetto/trace/track_event:zero",
     ]
   }
-  component("libtrace_processor") {
+  if (enable_perfetto_trace_processor_sqlite) {
+    component("libtrace_processor") {
+      public_configs = [ "gn:public_config" ]
+      deps = [
+        "src/trace_processor:lib",
+      ]
+      configs -= [ "//build/config/compiler:chromium_code" ]
+      configs += [ "//build/config/compiler:no_chromium_code" ]
+      public_deps = [
+        "include/perfetto/trace_processor",
+      ]
+    }
+  }
+  component("libproto_to_json") {
     public_configs = [ "gn:public_config" ]
     deps = [
-      "src/trace_processor:lib",
+      "src/trace_processor:export_json",
+      "src/trace_processor:storage",
     ]
     configs -= [ "//build/config/compiler:chromium_code" ]
     configs += [ "//build/config/compiler:no_chromium_code" ]
     public_deps = [
       "include/perfetto/ext/trace_processor:export_json",
-      "include/perfetto/trace_processor",
+      "include/perfetto/trace_processor:storage",
     ]
   }
 }
diff --git a/gn/BUILD.gn b/gn/BUILD.gn
index 3812690..a866278 100644
--- a/gn/BUILD.gn
+++ b/gn/BUILD.gn
@@ -77,6 +77,7 @@
     "PERFETTO_FORCE_DLOG_ON=$perfetto_force_dlog_on",
     "PERFETTO_FORCE_DLOG_OFF=$perfetto_force_dlog_off",
     "PERFETTO_VERSION_GEN=$enable_perfetto_version_gen",
+    "PERFETTO_TP_SQLITE=$enable_perfetto_trace_processor_sqlite",
     "PERFETTO_TP_PERCENTILE=$enable_perfetto_trace_processor_percentile",
     "PERFETTO_TP_LINENOISE=$enable_perfetto_trace_processor_linenoise",
     "PERFETTO_TP_METRICS=$enable_perfetto_trace_processor_metrics",
diff --git a/gn/perfetto.gni b/gn/perfetto.gni
index a2ab7ef..d2c5c91 100644
--- a/gn/perfetto.gni
+++ b/gn/perfetto.gni
@@ -163,9 +163,6 @@
   enable_perfetto_tools =
       perfetto_build_standalone || perfetto_build_with_android
 
-  # Allows to build the UI (TypeScript/ HTML / WASM)
-  enable_perfetto_ui = perfetto_build_standalone
-
   enable_perfetto_unittests = perfetto_build_standalone ||
                               build_with_chromium || perfetto_build_with_android
 
@@ -195,7 +192,9 @@
 }
 
 declare_args() {
-  enable_perfetto_trace_processor_sqlite = enable_perfetto_trace_processor
+  # Enables the SQL query layer of trace processor.
+  enable_perfetto_trace_processor_sqlite =
+      enable_perfetto_trace_processor && !build_with_chromium
 
   # Enables the optional SQLite percentile module.
   enable_perfetto_trace_processor_percentile =
@@ -240,6 +239,16 @@
       enable_perfetto_trace_processor && perfetto_build_standalone
 }
 
+declare_args() {
+  # Enables the trace_to_text tool.
+  enable_perfetto_tools_trace_to_text =
+      enable_perfetto_tools && enable_perfetto_trace_processor_sqlite
+
+  # Allows to build the UI (TypeScript/ HTML / WASM)
+  enable_perfetto_ui =
+      perfetto_build_standalone && enable_perfetto_trace_processor_sqlite
+}
+
 # +---------------------------------------------------------------------------+
 # | Cross-checks                                                              |
 # +---------------------------------------------------------------------------+
@@ -275,3 +284,6 @@
 
 assert(perfetto_force_dlog == "" || perfetto_force_dlog == "on" ||
        perfetto_force_dlog == "off")
+
+# The UI requires SQLite support in trace processor.
+assert(!enable_perfetto_ui || enable_perfetto_trace_processor_sqlite)
diff --git a/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h b/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
index 3de1849..1e79a3e 100644
--- a/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
+++ b/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
@@ -15,6 +15,7 @@
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_FORCE_DLOG_ON() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_FORCE_DLOG_OFF() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_VERSION_GEN() (0)
+#define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_SQLITE() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_PERCENTILE() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_LINENOISE() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_METRICS() (1)
diff --git a/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h b/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
index 4dcec86..7dbcb9c 100644
--- a/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
+++ b/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
@@ -15,6 +15,7 @@
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_FORCE_DLOG_ON() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_FORCE_DLOG_OFF() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_VERSION_GEN() (0)
+#define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_SQLITE() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_PERCENTILE() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_LINENOISE() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_METRICS() (1)
diff --git a/include/perfetto/ext/trace_processor/export_json.h b/include/perfetto/ext/trace_processor/export_json.h
index b50de47..d5c5dac 100644
--- a/include/perfetto/ext/trace_processor/export_json.h
+++ b/include/perfetto/ext/trace_processor/export_json.h
@@ -27,7 +27,7 @@
 namespace perfetto {
 namespace trace_processor {
 
-class TraceProcessor;
+class TraceProcessorStorage;
 
 namespace json {
 
@@ -47,9 +47,9 @@
   virtual util::Status AppendString(const std::string&) = 0;
 };
 
-// Public for Chrome. Exports the trace loaded in TraceProcessor to json,
+// Public for Chrome. Exports the trace loaded in TraceProcessorStorage to json,
 // applying argument, metadata and label filtering using the callbacks.
-util::Status PERFETTO_EXPORT ExportJson(TraceProcessor*,
+util::Status PERFETTO_EXPORT ExportJson(TraceProcessorStorage*,
                                         OutputWriter*,
                                         ArgumentFilterPredicate = nullptr,
                                         MetadataFilterPredicate = nullptr,
diff --git a/include/perfetto/trace_processor/BUILD.gn b/include/perfetto/trace_processor/BUILD.gn
index 215be02..1c419f9 100644
--- a/include/perfetto/trace_processor/BUILD.gn
+++ b/include/perfetto/trace_processor/BUILD.gn
@@ -14,9 +14,27 @@
 
 source_set("trace_processor") {
   sources = [
-    "basic_types.h",
     "read_trace.h",
-    "status.h",
     "trace_processor.h",
   ]
+  public_deps = [
+    ":basic_types",
+    ":storage",
+  ]
+}
+
+source_set("storage") {
+  sources = [
+    "trace_processor_storage.h",
+  ]
+  public_deps = [
+    ":basic_types",
+  ]
+}
+
+source_set("basic_types") {
+  sources = [
+    "basic_types.h",
+    "status.h",
+  ]
 }
diff --git a/include/perfetto/trace_processor/trace_processor.h b/include/perfetto/trace_processor/trace_processor.h
index 13f45c8..0d36204 100644
--- a/include/perfetto/trace_processor/trace_processor.h
+++ b/include/perfetto/trace_processor/trace_processor.h
@@ -20,16 +20,18 @@
 #include <memory>
 #include <vector>
 
+#include "perfetto/base/build_config.h"
 #include "perfetto/base/export.h"
 #include "perfetto/trace_processor/basic_types.h"
 #include "perfetto/trace_processor/status.h"
+#include "perfetto/trace_processor/trace_processor_storage.h"
 
 namespace perfetto {
 namespace trace_processor {
 
-// Coordinates the loading of traces from an arbitrary source and allows
-// execution of SQL queries on the events in these traces.
-class PERFETTO_EXPORT TraceProcessor {
+// Extends TraceProcessorStorage to support execution of SQL queries on loaded
+// traces. See TraceProcessorStorage for parsing of trace files.
+class PERFETTO_EXPORT TraceProcessor : public TraceProcessorStorage {
  public:
   class IteratorImpl;
 
@@ -74,22 +76,7 @@
   // Creates a new instance of TraceProcessor.
   static std::unique_ptr<TraceProcessor> CreateInstance(const Config&);
 
-  virtual ~TraceProcessor();
-
-  // The entry point to push trace data into the processor. The trace format
-  // will be automatically discovered on the first push call. It is possible
-  // to make queries between two pushes.
-  // Returns the Ok status if parsing has been succeeding so far, and Error
-  // status if some unrecoverable error happened. If this happens, the
-  // TraceProcessor will ignore the following Parse() requests, drop data on the
-  // floor and return errors forever.
-  virtual util::Status Parse(std::unique_ptr<uint8_t[]>, size_t) = 0;
-
-  // When parsing a bounded file (as opposite to streaming from a device) this
-  // function should be called when the last chunk of the file has been passed
-  // into Parse(). This allows to flush the events queued in the ordering stage,
-  // without having to wait for their time window to expire.
-  virtual void NotifyEndOfFile() = 0;
+  ~TraceProcessor() override;
 
   // Executes a SQLite query on the loaded portion of the trace. The returned
   // iterator can be used to load rows from the result.
diff --git a/include/perfetto/trace_processor/trace_processor_storage.h b/include/perfetto/trace_processor/trace_processor_storage.h
new file mode 100644
index 0000000..9d27edb
--- /dev/null
+++ b/include/perfetto/trace_processor/trace_processor_storage.h
@@ -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.
+ */
+
+#ifndef INCLUDE_PERFETTO_TRACE_PROCESSOR_TRACE_PROCESSOR_STORAGE_H_
+#define INCLUDE_PERFETTO_TRACE_PROCESSOR_TRACE_PROCESSOR_STORAGE_H_
+
+#include <stdint.h>
+
+#include <memory>
+
+#include "perfetto/base/export.h"
+#include "perfetto/trace_processor/basic_types.h"
+#include "perfetto/trace_processor/status.h"
+
+namespace perfetto {
+namespace trace_processor {
+
+// Coordinates the loading of traces from an arbitrary source.
+class PERFETTO_EXPORT TraceProcessorStorage {
+ public:
+  // Creates a new instance of TraceProcessorStorage.
+  static std::unique_ptr<TraceProcessorStorage> CreateInstance(const Config&);
+
+  virtual ~TraceProcessorStorage();
+
+  // The entry point to push trace data into the processor. The trace format
+  // will be automatically discovered on the first push call. It is possible
+  // to make queries between two pushes.
+  // Returns the Ok status if parsing has been succeeding so far, and Error
+  // status if some unrecoverable error happened. If this happens, the
+  // TraceProcessor will ignore the following Parse() requests, drop data on the
+  // floor and return errors forever.
+  virtual util::Status Parse(std::unique_ptr<uint8_t[]>, size_t) = 0;
+
+  // When parsing a bounded file (as opposite to streaming from a device) this
+  // function should be called when the last chunk of the file has been passed
+  // into Parse(). This allows to flush the events queued in the ordering stage,
+  // without having to wait for their time window to expire.
+  virtual void NotifyEndOfFile() = 0;
+};
+
+}  // namespace trace_processor
+}  // namespace perfetto
+
+#endif  // INCLUDE_PERFETTO_TRACE_PROCESSOR_TRACE_PROCESSOR_STORAGE_H_
diff --git a/src/trace_processor/BUILD.gn b/src/trace_processor/BUILD.gn
index c4cdecb..0f1b0fe 100644
--- a/src/trace_processor/BUILD.gn
+++ b/src/trace_processor/BUILD.gn
@@ -23,11 +23,13 @@
 
 # The library which eases processing of Perfetto traces by exposing reading
 # friendly APIs.
-static_library("trace_processor") {
-  complete_static_lib = true
-  deps = [
-    ":lib",
-  ]
+if (enable_perfetto_trace_processor_sqlite) {
+  static_library("trace_processor") {
+    complete_static_lib = true
+    deps = [
+      ":lib",
+    ]
+  }
 }
 
 if (enable_perfetto_ui) {
@@ -42,12 +44,8 @@
   }
 }
 
-source_set("lib") {
+source_set("storage") {
   sources = [
-    "android_logs_table.cc",
-    "android_logs_table.h",
-    "args_table.cc",
-    "args_table.h",
     "args_tracker.cc",
     "args_tracker.h",
     "binder_tracker.cc",
@@ -55,16 +53,8 @@
     "chunked_trace_reader.h",
     "clock_tracker.cc",
     "clock_tracker.h",
-    "counter_definitions_table.cc",
-    "counter_definitions_table.h",
-    "counter_values_table.cc",
-    "counter_values_table.h",
-    "cpu_profile_stack_sample_table.cc",
-    "cpu_profile_stack_sample_table.h",
     "event_tracker.cc",
     "event_tracker.h",
-    "filtered_row_index.cc",
-    "filtered_row_index.h",
     "forwarding_trace_parser.cc",
     "forwarding_trace_parser.h",
     "ftrace_utils.h",
@@ -72,8 +62,6 @@
     "gzip_trace_parser.h",
     "heap_graph_tracker.cc",
     "heap_graph_tracker.h",
-    "heap_profile_allocation_table.cc",
-    "heap_profile_allocation_table.h",
     "heap_profile_tracker.cc",
     "heap_profile_tracker.h",
     "importers/ftrace/ftrace_descriptors.h",
@@ -102,60 +90,28 @@
     "importers/proto/track_event_tokenizer.h",
     "importers/systrace/systrace_parser.h",
     "importers/systrace/systrace_trace_parser.h",
-    "instants_table.cc",
-    "instants_table.h",
     "metadata.h",
-    "metadata_table.cc",
-    "metadata_table.h",
-    "process_table.cc",
-    "process_table.h",
     "process_tracker.cc",
     "process_tracker.h",
-    "raw_table.cc",
-    "raw_table.h",
-    "read_trace.cc",
-    "row_iterators.cc",
-    "row_iterators.h",
-    "sched_slice_table.h",
-    "slice_table.cc",
-    "slice_table.h",
     "slice_tracker.cc",
     "slice_tracker.h",
-    "span_join_operator_table.cc",
-    "span_join_operator_table.h",
-    "sql_stats_table.cc",
-    "sql_stats_table.h",
-    "stack_profile_frame_table.cc",
-    "stack_profile_frame_table.h",
-    "stack_profile_mapping_table.cc",
-    "stack_profile_mapping_table.h",
     "stack_profile_tracker.cc",
     "stack_profile_tracker.h",
     "stats.h",
-    "stats_table.cc",
-    "stats_table.h",
-    "storage_columns.cc",
-    "storage_columns.h",
-    "storage_schema.cc",
-    "storage_schema.h",
-    "storage_table.cc",
-    "storage_table.h",
     "syscall_tracker.cc",
     "syscall_tracker.h",
     "syscalls_aarch32.h",
     "syscalls_aarch64.h",
     "syscalls_armeabi.h",
     "syscalls_x86_64.h",
-    "thread_table.cc",
-    "thread_table.h",
     "timestamped_trace_piece.h",
     "trace_blob_view.h",
     "trace_parser.h",
-    "trace_processor.cc",
     "trace_processor_context.cc",
     "trace_processor_context.h",
-    "trace_processor_impl.cc",
-    "trace_processor_impl.h",
+    "trace_processor_storage.cc",
+    "trace_processor_storage_impl.cc",
+    "trace_processor_storage_impl.h",
     "trace_sorter.cc",
     "trace_sorter.h",
     "trace_storage.cc",
@@ -166,17 +122,12 @@
     "virtual_destructors.cc",
     "vulkan_memory_tracker.cc",
     "vulkan_memory_tracker.h",
-    "window_operator_table.cc",
-    "window_operator_table.h",
   ]
 
   deps = [
     ":common",
     "../../gn:default_deps",
-    "../../gn:sqlite",
     "../../gn:zlib",
-    "../../include/perfetto/ext/trace_processor:export_json",
-    "../../include/perfetto/ext/traced:sys_stats_counters",
     "../../protos/perfetto/common:zero",
     "../../protos/perfetto/config:zero",
     "../../protos/perfetto/trace:zero",
@@ -193,17 +144,13 @@
     "../../protos/perfetto/trace/track_event:zero",
     "../base",
     "../protozero",
-    "db:lib",
-    "sqlite",
     "tables",
   ]
   public_deps = [
-    "../../include/perfetto/trace_processor",
+    "../../include/perfetto/trace_processor:storage",
   ]
   if (enable_perfetto_trace_processor_json) {
     sources += [
-      "export_json.cc",
-      "export_json.h",
       "importers/json/json_trace_parser.cc",
       "importers/json/json_trace_parser.h",
       "importers/json/json_trace_tokenizer.cc",
@@ -221,7 +168,6 @@
       "importers/ftrace/sched_event_tracker.cc",
       "importers/systrace/systrace_parser.cc",
       "importers/systrace/systrace_trace_parser.cc",
-      "sched_slice_table.cc",
     ]
   }
   if (enable_perfetto_trace_processor_ftrace ||
@@ -230,6 +176,7 @@
   }
   if (enable_perfetto_trace_processor_system_probes) {
     sources += [ "importers/proto/system_probes_parser.cc" ]
+    deps += [ "../../include/perfetto/ext/traced:sys_stats_counters" ]
   }
   if (enable_perfetto_trace_processor_android_probes) {
     sources += [ "importers/proto/android_probes_parser.cc" ]
@@ -237,13 +184,6 @@
   if (enable_perfetto_trace_processor_graphics) {
     sources += [ "importers/proto/graphics_event_parser.cc" ]
   }
-  if (enable_perfetto_trace_processor_metrics) {
-    deps += [
-      "../../protos/perfetto/metrics:zero",
-      "../../protos/perfetto/metrics/android:zero",
-      "metrics:lib",
-    ]
-  }
   if (enable_perfetto_trace_processor_fuchsia) {
     sources += [
       "importers/fuchsia/fuchsia_provider_view.cc",
@@ -257,6 +197,110 @@
   }
 }
 
+if (enable_perfetto_trace_processor_json) {
+  source_set("export_json") {
+    sources = [
+      "export_json.cc",
+      "export_json.h",
+    ]
+    deps = [
+      ":storage",
+      "../../gn:default_deps",
+      "../../gn:jsoncpp",
+      "../base",
+    ]
+    public_deps = [
+      "../../include/perfetto/ext/trace_processor:export_json",
+    ]
+  }
+}
+
+if (enable_perfetto_trace_processor_sqlite) {
+  source_set("lib") {
+    sources = [
+      "android_logs_table.cc",
+      "android_logs_table.h",
+      "args_table.cc",
+      "args_table.h",
+      "counter_definitions_table.cc",
+      "counter_definitions_table.h",
+      "counter_values_table.cc",
+      "counter_values_table.h",
+      "cpu_profile_stack_sample_table.cc",
+      "cpu_profile_stack_sample_table.h",
+      "filtered_row_index.cc",
+      "filtered_row_index.h",
+      "heap_profile_allocation_table.cc",
+      "heap_profile_allocation_table.h",
+      "instants_table.cc",
+      "instants_table.h",
+      "metadata_table.cc",
+      "metadata_table.h",
+      "process_table.cc",
+      "process_table.h",
+      "raw_table.cc",
+      "raw_table.h",
+      "read_trace.cc",
+      "row_iterators.cc",
+      "row_iterators.h",
+      "sched_slice_table.h",
+      "slice_table.cc",
+      "slice_table.h",
+      "span_join_operator_table.cc",
+      "span_join_operator_table.h",
+      "sql_stats_table.cc",
+      "sql_stats_table.h",
+      "stack_profile_frame_table.cc",
+      "stack_profile_frame_table.h",
+      "stack_profile_mapping_table.cc",
+      "stack_profile_mapping_table.h",
+      "stats_table.cc",
+      "stats_table.h",
+      "storage_columns.cc",
+      "storage_columns.h",
+      "storage_schema.cc",
+      "storage_schema.h",
+      "storage_table.cc",
+      "storage_table.h",
+      "thread_table.cc",
+      "thread_table.h",
+      "trace_processor.cc",
+      "trace_processor_impl.cc",
+      "trace_processor_impl.h",
+      "window_operator_table.cc",
+      "window_operator_table.h",
+    ]
+
+    deps = [
+      ":common",
+      ":storage",
+      "../../gn:default_deps",
+      "../../gn:sqlite",
+      "../../protos/perfetto/trace/ftrace:zero",
+      "../base",
+      "db:lib",
+      "sqlite",
+      "tables",
+    ]
+    public_deps = [
+      "../../include/perfetto/trace_processor",
+    ]
+    if (enable_perfetto_trace_processor_ftrace) {
+      sources += [ "sched_slice_table.cc" ]
+    }
+    if (enable_perfetto_trace_processor_metrics) {
+      deps += [
+        "../../protos/perfetto/metrics:zero",
+        "../../protos/perfetto/metrics/android:zero",
+        "metrics:lib",
+      ]
+    }
+    if (enable_perfetto_trace_processor_json) {
+      deps += [ ":export_json" ]
+    }
+  }
+}
+
 # TODO(lalitm): we need to find a better home for the classes here.
 source_set("common") {
   sources = [
@@ -301,30 +345,24 @@
 perfetto_unittest_source_set("unittests") {
   testonly = true
   sources = [
-    "args_table_unittest.cc",
     "clock_tracker_unittest.cc",
     "event_tracker_unittest.cc",
-    "filtered_row_index_unittest.cc",
     "forwarding_trace_parser_unittest.cc",
     "heap_profile_tracker_unittest.cc",
     "importers/proto/proto_trace_parser_unittest.cc",
     "importers/systrace/systrace_parser_unittest.cc",
-    "metadata_table_unittest.cc",
     "null_term_string_view_unittest.cc",
-    "process_table_unittest.cc",
     "process_tracker_unittest.cc",
     "slice_tracker_unittest.cc",
-    "span_join_operator_table_unittest.cc",
     "string_pool_unittest.cc",
     "syscall_tracker_unittest.cc",
     "trace_sorter_unittest.cc",
   ]
   deps = [
     ":common",
-    ":lib",
+    ":storage",
     "../../gn:default_deps",
     "../../gn:gtest_and_gmock",
-    "../../gn:sqlite",
     "../../protos/perfetto/common:zero",
     "../../protos/perfetto/trace:minimal_zero",
     "../../protos/perfetto/trace:zero",
@@ -340,10 +378,23 @@
     "../base",
     "../protozero",
     "db:unittests",
-    "sqlite",
-    "sqlite:unittests",
     "tables:unittests",
   ]
+  if (enable_perfetto_trace_processor_sqlite) {
+    sources += [
+      "args_table_unittest.cc",
+      "filtered_row_index_unittest.cc",
+      "metadata_table_unittest.cc",
+      "process_table_unittest.cc",
+      "span_join_operator_table_unittest.cc",
+    ]
+    deps += [
+      ":lib",
+      "../../gn:sqlite",
+      "sqlite",
+      "sqlite:unittests",
+    ]
+  }
   if (enable_perfetto_trace_processor_json) {
     sources += [
       "importers/json/json_trace_tokenizer_unittest.cc",
@@ -353,7 +404,10 @@
       # export_json_unittest.cc uses base::TempFile, which is not supported on
       # windows.
       sources += [ "export_json_unittest.cc" ]
-      deps += [ "../../include/perfetto/ext/trace_processor:export_json" ]
+      deps += [
+        ":export_json",
+        "../../include/perfetto/ext/trace_processor:export_json",
+      ]
     }
     deps += [ "../../gn:jsoncpp" ]
   }
@@ -371,20 +425,24 @@
 
 source_set("integrationtests") {
   testonly = true
-  sources = [
-    "trace_database_integrationtest.cc",
-  ]
-  deps = [
-    ":lib",
-    "../../gn:default_deps",
-    "../../gn:gtest_and_gmock",
-    "../../protos/perfetto/trace:lite",
-    "../base",
-    "../base:test_support",
-    "sqlite",
-  ]
-  if (enable_perfetto_trace_processor_json) {
-    deps += [ "../../gn:jsoncpp" ]
+  sources = []
+  deps = []
+  if (enable_perfetto_trace_processor_sqlite) {
+    sources += [ "trace_database_integrationtest.cc" ]
+    deps += [
+      ":lib",
+      ":storage",
+      "../../gn:default_deps",
+      "../../gn:gtest_and_gmock",
+      "../../protos/perfetto/trace:lite",
+      "../base",
+      "../base:test_support",
+      "sqlite",
+    ]
+
+    if (enable_perfetto_trace_processor_json) {
+      deps += [ "../../gn:jsoncpp" ]
+    }
   }
 }
 
@@ -394,7 +452,7 @@
     "trace_parsing_fuzzer.cc",
   ]
   deps = [
-    ":lib",
+    ":storage",
     "../../gn:default_deps",
     "../base",
   ]
diff --git a/src/trace_processor/export_json.cc b/src/trace_processor/export_json.cc
index f9b2f34..0a5437e 100644
--- a/src/trace_processor/export_json.cc
+++ b/src/trace_processor/export_json.cc
@@ -31,7 +31,7 @@
 #include "perfetto/ext/base/string_splitter.h"
 #include "src/trace_processor/metadata.h"
 #include "src/trace_processor/trace_processor_context.h"
-#include "src/trace_processor/trace_processor_impl.h"
+#include "src/trace_processor/trace_processor_storage_impl.h"
 #include "src/trace_processor/trace_storage.h"
 
 namespace perfetto {
@@ -980,13 +980,14 @@
   return util::OkStatus();
 }
 
-util::Status ExportJson(TraceProcessor* tp,
+util::Status ExportJson(TraceProcessorStorage* tp,
                         OutputWriter* output,
                         ArgumentFilterPredicate argument_filter,
                         MetadataFilterPredicate metadata_filter,
                         LabelFilterPredicate label_filter) {
-  const TraceStorage* storage =
-      reinterpret_cast<TraceProcessorImpl*>(tp)->context()->storage.get();
+  const TraceStorage* storage = reinterpret_cast<TraceProcessorStorageImpl*>(tp)
+                                    ->context()
+                                    ->storage.get();
   return ExportJson(storage, output, argument_filter, metadata_filter,
                     label_filter);
 }
diff --git a/src/trace_processor/sqlite/BUILD.gn b/src/trace_processor/sqlite/BUILD.gn
index 18a3d1a..6f8c9e4 100644
--- a/src/trace_processor/sqlite/BUILD.gn
+++ b/src/trace_processor/sqlite/BUILD.gn
@@ -14,39 +14,41 @@
 
 import("../../../gn/test.gni")
 
-source_set("sqlite") {
-  sources = [
-    "db_sqlite_table.cc",
-    "db_sqlite_table.h",
-    "query_constraints.cc",
-    "query_constraints.h",
-    "scoped_db.h",
-    "sqlite3_str_split.cc",
-    "sqlite3_str_split.h",
-    "sqlite_table.cc",
-    "sqlite_table.h",
-    "sqlite_utils.h",
-  ]
-  deps = [
-    "../../../gn:default_deps",
-    "../../../gn:sqlite",
-    "../../../include/perfetto/trace_processor",
-    "../../base",
-    "../db:lib",
-  ]
-}
+if (enable_perfetto_trace_processor_sqlite) {
+  source_set("sqlite") {
+    sources = [
+      "db_sqlite_table.cc",
+      "db_sqlite_table.h",
+      "query_constraints.cc",
+      "query_constraints.h",
+      "scoped_db.h",
+      "sqlite3_str_split.cc",
+      "sqlite3_str_split.h",
+      "sqlite_table.cc",
+      "sqlite_table.h",
+      "sqlite_utils.h",
+    ]
+    deps = [
+      "../../../gn:default_deps",
+      "../../../gn:sqlite",
+      "../../../include/perfetto/trace_processor",
+      "../../base",
+      "../db:lib",
+    ]
+  }
 
-perfetto_unittest_source_set("unittests") {
-  testonly = true
-  sources = [
-    "query_constraints_unittest.cc",
-    "sqlite3_str_split_unittest.cc",
-  ]
-  deps = [
-    ":sqlite",
-    "../../../gn:default_deps",
-    "../../../gn:gtest_and_gmock",
-    "../../../gn:sqlite",
-    "../../base",
-  ]
-}
+  perfetto_unittest_source_set("unittests") {
+    testonly = true
+    sources = [
+      "query_constraints_unittest.cc",
+      "sqlite3_str_split_unittest.cc",
+    ]
+    deps = [
+      ":sqlite",
+      "../../../gn:default_deps",
+      "../../../gn:gtest_and_gmock",
+      "../../../gn:sqlite",
+      "../../base",
+    ]
+  }
+}  # if (enable_perfetto_trace_processor_sqlite)
diff --git a/src/trace_processor/trace_parsing_fuzzer.cc b/src/trace_processor/trace_parsing_fuzzer.cc
index 2704c48..c15d716 100644
--- a/src/trace_processor/trace_parsing_fuzzer.cc
+++ b/src/trace_processor/trace_parsing_fuzzer.cc
@@ -15,7 +15,7 @@
  */
 
 #include "perfetto/base/logging.h"
-#include "perfetto/trace_processor/trace_processor.h"
+#include "perfetto/trace_processor/trace_processor_storage.h"
 
 namespace perfetto {
 namespace trace_processor {
@@ -23,8 +23,8 @@
 void FuzzTraceProcessor(const uint8_t* data, size_t size);
 
 void FuzzTraceProcessor(const uint8_t* data, size_t size) {
-  std::unique_ptr<TraceProcessor> processor =
-      TraceProcessor::CreateInstance(Config());
+  std::unique_ptr<TraceProcessorStorage> processor =
+      TraceProcessorStorage::CreateInstance(Config());
   std::unique_ptr<uint8_t[]> buf(new uint8_t[size]);
   memcpy(buf.get(), data, size);
   util::Status status = processor->Parse(std::move(buf), size);
diff --git a/src/trace_processor/trace_processor.cc b/src/trace_processor/trace_processor.cc
index 75791cc..0a4942a 100644
--- a/src/trace_processor/trace_processor.cc
+++ b/src/trace_processor/trace_processor.cc
@@ -65,5 +65,6 @@
   // which in turn requires sqlite headers.
   SqliteTable::debug = true;
 }
+
 }  // namespace trace_processor
 }  // namespace perfetto
diff --git a/src/trace_processor/trace_processor_impl.cc b/src/trace_processor/trace_processor_impl.cc
index b72f504..95f3718 100644
--- a/src/trace_processor/trace_processor_impl.cc
+++ b/src/trace_processor/trace_processor_impl.cc
@@ -18,45 +18,23 @@
 
 #include <inttypes.h>
 #include <algorithm>
-#include <fstream>
-#include <functional>
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/time.h"
 #include "perfetto/ext/base/string_splitter.h"
 #include "perfetto/ext/base/string_utils.h"
-#include "perfetto/protozero/scattered_heap_buffer.h"
 #include "src/trace_processor/android_logs_table.h"
 #include "src/trace_processor/args_table.h"
-#include "src/trace_processor/args_tracker.h"
-#include "src/trace_processor/binder_tracker.h"
-#include "src/trace_processor/clock_tracker.h"
 #include "src/trace_processor/counter_definitions_table.h"
 #include "src/trace_processor/counter_values_table.h"
 #include "src/trace_processor/cpu_profile_stack_sample_table.h"
-#include "src/trace_processor/event_tracker.h"
-#include "src/trace_processor/forwarding_trace_parser.h"
-#include "src/trace_processor/heap_graph_tracker.h"
 #include "src/trace_processor/heap_profile_allocation_table.h"
-#include "src/trace_processor/heap_profile_tracker.h"
-#include "src/trace_processor/importers/ftrace/ftrace_module.h"
-#include "src/trace_processor/importers/ftrace/sched_event_tracker.h"
-#include "src/trace_processor/importers/proto/android_probes_module.h"
-#include "src/trace_processor/importers/proto/graphics_event_module.h"
-#include "src/trace_processor/importers/proto/proto_importer_module.h"
-#include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
-#include "src/trace_processor/importers/proto/system_probes_module.h"
-#include "src/trace_processor/importers/proto/track_event_module.h"
-#include "src/trace_processor/importers/systrace/systrace_parser.h"
-#include "src/trace_processor/importers/systrace/systrace_trace_parser.h"
 #include "src/trace_processor/instants_table.h"
 #include "src/trace_processor/metadata_table.h"
 #include "src/trace_processor/process_table.h"
-#include "src/trace_processor/process_tracker.h"
 #include "src/trace_processor/raw_table.h"
 #include "src/trace_processor/sched_slice_table.h"
 #include "src/trace_processor/slice_table.h"
-#include "src/trace_processor/slice_tracker.h"
 #include "src/trace_processor/span_join_operator_table.h"
 #include "src/trace_processor/sql_stats_table.h"
 #include "src/trace_processor/sqlite/db_sqlite_table.h"
@@ -64,14 +42,8 @@
 #include "src/trace_processor/sqlite/sqlite_table.h"
 #include "src/trace_processor/stack_profile_frame_table.h"
 #include "src/trace_processor/stack_profile_mapping_table.h"
-#include "src/trace_processor/stack_profile_tracker.h"
 #include "src/trace_processor/stats_table.h"
-#include "src/trace_processor/syscall_tracker.h"
 #include "src/trace_processor/thread_table.h"
-#include "src/trace_processor/trace_blob_view.h"
-#include "src/trace_processor/trace_sorter.h"
-#include "src/trace_processor/track_tracker.h"
-#include "src/trace_processor/vulkan_memory_tracker.h"
 #include "src/trace_processor/window_operator_table.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS)
@@ -92,7 +64,7 @@
 extern "C" int sqlite3_percentile_init(sqlite3* db,
                                        char** error,
                                        const sqlite3_api_routines* api);
-#endif
+#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_PERCENTILE)
 
 namespace perfetto {
 namespace trace_processor {
@@ -317,7 +289,8 @@
 
 }  // namespace
 
-TraceProcessorImpl::TraceProcessorImpl(const Config& cfg) {
+TraceProcessorImpl::TraceProcessorImpl(const Config& cfg)
+    : TraceProcessorStorageImpl(cfg) {
   sqlite3* db = nullptr;
   PERFETTO_CHECK(sqlite3_initialize() == SQLITE_OK);
   PERFETTO_CHECK(sqlite3_open(":memory:", &db) == SQLITE_OK);
@@ -326,34 +299,6 @@
   CreateBuiltinViews(db);
   db_.reset(std::move(db));
 
-  context_.config = cfg;
-  context_.storage.reset(new TraceStorage());
-  context_.track_tracker.reset(new TrackTracker(&context_));
-  context_.args_tracker.reset(new ArgsTracker(&context_));
-  context_.slice_tracker.reset(new SliceTracker(&context_));
-  context_.event_tracker.reset(new EventTracker(&context_));
-  context_.process_tracker.reset(new ProcessTracker(&context_));
-  context_.syscall_tracker.reset(new SyscallTracker(&context_));
-  context_.clock_tracker.reset(new ClockTracker(&context_));
-  context_.heap_profile_tracker.reset(new HeapProfileTracker(&context_));
-  context_.heap_graph_tracker.reset(new HeapGraphTracker(&context_));
-#if PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE)
-  context_.sched_tracker.reset(new SchedEventTracker(&context_));
-  context_.systrace_parser.reset(new SystraceParser(&context_));
-  context_.binder_tracker.reset(new BinderTracker(&context_));
-#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE)
-  context_.vulkan_memory_tracker.reset(new VulkanMemoryTracker(&context_));
-  context_.ftrace_module.reset(
-      new ProtoImporterModule<FtraceModule>(&context_));
-  context_.track_event_module.reset(
-      new ProtoImporterModule<TrackEventModule>(&context_));
-  context_.system_probes_module.reset(
-      new ProtoImporterModule<SystemProbesModule>(&context_));
-  context_.android_probes_module.reset(
-      new ProtoImporterModule<AndroidProbesModule>(&context_));
-  context_.graphics_event_module.reset(
-      new ProtoImporterModule<GraphicsEventModule>(&context_));
-
 #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
   CreateJsonExportFunction(this->context_.storage.get(), db);
 #endif
@@ -419,32 +364,12 @@
 
 util::Status TraceProcessorImpl::Parse(std::unique_ptr<uint8_t[]> data,
                                        size_t size) {
-  if (size == 0)
-    return util::OkStatus();
-  if (unrecoverable_parse_error_)
-    return util::ErrStatus(
-        "Failed unrecoverably while parsing in a previous Parse call");
-  if (!context_.chunk_reader)
-    context_.chunk_reader.reset(new ForwardingTraceParser(&context_));
-
-  auto scoped_trace = context_.storage->TraceExecutionTimeIntoStats(
-      stats::parse_trace_duration_ns);
-  util::Status status = context_.chunk_reader->Parse(std::move(data), size);
-  unrecoverable_parse_error_ |= !status.ok();
-  return status;
+  return TraceProcessorStorageImpl::Parse(std::move(data), size);
 }
 
 void TraceProcessorImpl::NotifyEndOfFile() {
-  if (unrecoverable_parse_error_ || !context_.chunk_reader)
-    return;
+  TraceProcessorStorageImpl::NotifyEndOfFile();
 
-  if (context_.sorter)
-    context_.sorter->ExtractEventsForced();
-#if PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE)
-  context_.sched_tracker->FlushPendingEvents();
-#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE)
-  context_.event_tracker->FlushPendingEvents();
-  context_.slice_tracker->FlushPendingSlices();
   BuildBoundsTable(*db_, context_.storage->GetTraceTimestampBoundsNs());
 
   // Create a snapshot of all tables and views created so far. This is so later
diff --git a/src/trace_processor/trace_processor_impl.h b/src/trace_processor/trace_processor_impl.h
index 44c3335..22bbbf0 100644
--- a/src/trace_processor/trace_processor_impl.h
+++ b/src/trace_processor/trace_processor_impl.h
@@ -21,7 +21,6 @@
 
 #include <atomic>
 #include <functional>
-#include <memory>
 #include <string>
 #include <vector>
 
@@ -30,7 +29,7 @@
 #include "perfetto/trace_processor/status.h"
 #include "perfetto/trace_processor/trace_processor.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
-#include "src/trace_processor/trace_processor_context.h"
+#include "src/trace_processor/trace_processor_storage_impl.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS)
 #include "src/trace_processor/metrics/descriptors.h"
@@ -38,21 +37,22 @@
 #endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS)
 
 namespace perfetto {
-
 namespace trace_processor {
 
 // Coordinates the loading of traces from an arbitrary source and allows
 // execution of SQL queries on the events in these traces.
-class TraceProcessorImpl : public TraceProcessor {
+class TraceProcessorImpl : public TraceProcessor,
+                           public TraceProcessorStorageImpl {
  public:
   explicit TraceProcessorImpl(const Config&);
 
   ~TraceProcessorImpl() override;
 
+  // TraceProcessorStorage implementation:
   util::Status Parse(std::unique_ptr<uint8_t[]>, size_t) override;
-
   void NotifyEndOfFile() override;
 
+  // TraceProcessor implementation:
   Iterator ExecuteQuery(const std::string& sql,
                         int64_t time_queued = 0) override;
 
@@ -70,15 +70,11 @@
 
   size_t RestoreInitialTables() override;
 
-  TraceProcessorContext* context() { return &context_; }
-
  private:
   // Needed for iterators to be able to delete themselves from the vector.
   friend class IteratorImpl;
 
-  ScopedDb db_;  // Keep first.
-  TraceProcessorContext context_;
-  bool unrecoverable_parse_error_ = false;
+  ScopedDb db_;
 
 #if PERFETTO_BUILDFLAG(PERFETTO_TP_METRICS)
   metrics::DescriptorPool pool_;
diff --git a/src/trace_processor/trace_processor_storage.cc b/src/trace_processor/trace_processor_storage.cc
new file mode 100644
index 0000000..f72c2a6
--- /dev/null
+++ b/src/trace_processor/trace_processor_storage.cc
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+#include "perfetto/trace_processor/trace_processor_storage.h"
+
+#include "src/trace_processor/trace_processor_storage_impl.h"
+
+namespace perfetto {
+namespace trace_processor {
+
+// static
+std::unique_ptr<TraceProcessorStorage> TraceProcessorStorage::CreateInstance(
+    const Config& config) {
+  return std::unique_ptr<TraceProcessorStorage>(
+      new TraceProcessorStorageImpl(config));
+}
+
+TraceProcessorStorage::~TraceProcessorStorage() = default;
+
+}  // namespace trace_processor
+}  // namespace perfetto
diff --git a/src/trace_processor/trace_processor_storage_impl.cc b/src/trace_processor/trace_processor_storage_impl.cc
new file mode 100644
index 0000000..c218db7
--- /dev/null
+++ b/src/trace_processor/trace_processor_storage_impl.cc
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ */
+
+#include "src/trace_processor/trace_processor_storage_impl.h"
+
+#include "perfetto/base/logging.h"
+#include "src/trace_processor/args_tracker.h"
+#include "src/trace_processor/binder_tracker.h"
+#include "src/trace_processor/clock_tracker.h"
+#include "src/trace_processor/event_tracker.h"
+#include "src/trace_processor/forwarding_trace_parser.h"
+#include "src/trace_processor/heap_graph_tracker.h"
+#include "src/trace_processor/heap_profile_tracker.h"
+#include "src/trace_processor/importers/ftrace/ftrace_module.h"
+#include "src/trace_processor/importers/ftrace/sched_event_tracker.h"
+#include "src/trace_processor/importers/proto/android_probes_module.h"
+#include "src/trace_processor/importers/proto/graphics_event_module.h"
+#include "src/trace_processor/importers/proto/proto_importer_module.h"
+#include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
+#include "src/trace_processor/importers/proto/system_probes_module.h"
+#include "src/trace_processor/importers/proto/track_event_module.h"
+#include "src/trace_processor/importers/systrace/systrace_parser.h"
+#include "src/trace_processor/importers/systrace/systrace_trace_parser.h"
+#include "src/trace_processor/process_tracker.h"
+#include "src/trace_processor/slice_tracker.h"
+#include "src/trace_processor/stack_profile_tracker.h"
+#include "src/trace_processor/syscall_tracker.h"
+#include "src/trace_processor/trace_blob_view.h"
+#include "src/trace_processor/trace_sorter.h"
+#include "src/trace_processor/track_tracker.h"
+#include "src/trace_processor/vulkan_memory_tracker.h"
+
+namespace perfetto {
+namespace trace_processor {
+
+TraceProcessorStorageImpl::TraceProcessorStorageImpl(const Config& cfg) {
+  context_.config = cfg;
+  context_.storage.reset(new TraceStorage());
+  context_.track_tracker.reset(new TrackTracker(&context_));
+  context_.args_tracker.reset(new ArgsTracker(&context_));
+  context_.slice_tracker.reset(new SliceTracker(&context_));
+  context_.event_tracker.reset(new EventTracker(&context_));
+  context_.process_tracker.reset(new ProcessTracker(&context_));
+  context_.syscall_tracker.reset(new SyscallTracker(&context_));
+  context_.clock_tracker.reset(new ClockTracker(&context_));
+  context_.heap_profile_tracker.reset(new HeapProfileTracker(&context_));
+  context_.heap_graph_tracker.reset(new HeapGraphTracker(&context_));
+#if PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE)
+  context_.sched_tracker.reset(new SchedEventTracker(&context_));
+  context_.systrace_parser.reset(new SystraceParser(&context_));
+  context_.binder_tracker.reset(new BinderTracker(&context_));
+#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE)
+  context_.vulkan_memory_tracker.reset(new VulkanMemoryTracker(&context_));
+  context_.ftrace_module.reset(
+      new ProtoImporterModule<FtraceModule>(&context_));
+  context_.track_event_module.reset(
+      new ProtoImporterModule<TrackEventModule>(&context_));
+  context_.system_probes_module.reset(
+      new ProtoImporterModule<SystemProbesModule>(&context_));
+  context_.android_probes_module.reset(
+      new ProtoImporterModule<AndroidProbesModule>(&context_));
+  context_.graphics_event_module.reset(
+      new ProtoImporterModule<GraphicsEventModule>(&context_));
+}
+
+TraceProcessorStorageImpl::~TraceProcessorStorageImpl() {}
+
+util::Status TraceProcessorStorageImpl::Parse(std::unique_ptr<uint8_t[]> data,
+                                              size_t size) {
+  if (size == 0)
+    return util::OkStatus();
+  if (unrecoverable_parse_error_)
+    return util::ErrStatus(
+        "Failed unrecoverably while parsing in a previous Parse call");
+  if (!context_.chunk_reader)
+    context_.chunk_reader.reset(new ForwardingTraceParser(&context_));
+
+  auto scoped_trace = context_.storage->TraceExecutionTimeIntoStats(
+      stats::parse_trace_duration_ns);
+  util::Status status = context_.chunk_reader->Parse(std::move(data), size);
+  unrecoverable_parse_error_ |= !status.ok();
+  return status;
+}
+
+void TraceProcessorStorageImpl::NotifyEndOfFile() {
+  if (unrecoverable_parse_error_ || !context_.chunk_reader)
+    return;
+
+  if (context_.sorter)
+    context_.sorter->ExtractEventsForced();
+#if PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE)
+  context_.sched_tracker->FlushPendingEvents();
+#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_FTRACE)
+  context_.event_tracker->FlushPendingEvents();
+  context_.slice_tracker->FlushPendingSlices();
+}
+
+}  // namespace trace_processor
+}  // namespace perfetto
diff --git a/src/trace_processor/trace_processor_storage_impl.h b/src/trace_processor/trace_processor_storage_impl.h
new file mode 100644
index 0000000..41f1273
--- /dev/null
+++ b/src/trace_processor/trace_processor_storage_impl.h
@@ -0,0 +1,48 @@
+/*
+ * 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_TRACE_PROCESSOR_STORAGE_IMPL_H_
+#define SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_STORAGE_IMPL_H_
+
+#include <memory>
+
+#include "perfetto/trace_processor/basic_types.h"
+#include "perfetto/trace_processor/status.h"
+#include "perfetto/trace_processor/trace_processor_storage.h"
+#include "src/trace_processor/trace_processor_context.h"
+
+namespace perfetto {
+namespace trace_processor {
+
+class TraceProcessorStorageImpl : public TraceProcessorStorage {
+ public:
+  explicit TraceProcessorStorageImpl(const Config&);
+  ~TraceProcessorStorageImpl() override;
+
+  util::Status Parse(std::unique_ptr<uint8_t[]>, size_t) override;
+  void NotifyEndOfFile() override;
+
+  TraceProcessorContext* context() { return &context_; }
+
+ protected:
+  TraceProcessorContext context_;
+  bool unrecoverable_parse_error_ = false;
+};
+
+}  // namespace trace_processor
+}  // namespace perfetto
+
+#endif  // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_STORAGE_IMPL_H_
diff --git a/tools/BUILD.gn b/tools/BUILD.gn
index 8ee64e9..6214548 100644
--- a/tools/BUILD.gn
+++ b/tools/BUILD.gn
@@ -26,8 +26,6 @@
     "compact_reencode",
     "ftrace_proto_gen",
     "protoprofile",
-    "trace_to_text",
-    "trace_to_text:trace_to_text_lite",
   ]
   if (is_linux || is_android) {
     deps += [
@@ -37,6 +35,12 @@
       "skippy",
     ]
   }
+  if (enable_perfetto_tools_trace_to_text) {
+    deps += [
+      "trace_to_text",
+      "trace_to_text:trace_to_text_lite",
+    ]
+  }
 }
 
 if (is_linux && enable_perfetto_heapprofd) {