/*
 * Copyright (C) 2017 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.
 */

syntax = "proto2";

package perfetto.protos;

import "protos/perfetto/config/android/android_aflags_config.proto";
import "protos/perfetto/config/android/android_game_intervention_list_config.proto";
import "protos/perfetto/config/android/android_input_event_config.proto";
import "protos/perfetto/config/android/android_log_config.proto";
import "protos/perfetto/config/android/android_polled_state_config.proto";
import "protos/perfetto/config/android/android_system_property_config.proto";
import "protos/perfetto/config/android/android_sdk_sysprop_guard_config.proto";
import "protos/perfetto/config/android/app_wakelock_config.proto";
import "protos/perfetto/config/android/cpu_per_uid_config.proto";
import "protos/perfetto/config/android/inputmethod_config.proto";
import "protos/perfetto/config/android/kernel_wakelocks_config.proto";
import "protos/perfetto/config/android/network_trace_config.proto";
import "protos/perfetto/config/android/packages_list_config.proto";
import "protos/perfetto/config/android/pixel_modem_config.proto";
import "protos/perfetto/config/android/protolog_config.proto";
import "protos/perfetto/config/android/surfaceflinger_layers_config.proto";
import "protos/perfetto/config/android/surfaceflinger_transactions_config.proto";
import "protos/perfetto/config/android/user_list_config.proto";
import "protos/perfetto/config/android/windowmanager_config.proto";
import "protos/perfetto/config/chrome/chrome_config.proto";
import "protos/perfetto/config/chrome/v8_config.proto";
import "protos/perfetto/config/etw/etw_config.proto";
import "protos/perfetto/config/chrome/system_metrics.proto";
import "protos/perfetto/config/ftrace/ftrace_config.proto";
import "protos/perfetto/config/ftrace/frozen_ftrace_config.proto";
import "protos/perfetto/config/gpu/gpu_counter_config.proto";
import "protos/perfetto/config/gpu/vulkan_memory_config.proto";
import "protos/perfetto/config/gpu/gpu_renderstages_config.proto";
import "protos/perfetto/config/inode_file/inode_file_config.proto";
import "protos/perfetto/config/interceptor_config.proto";
import "protos/perfetto/config/power/android_power_config.proto";
import "protos/perfetto/config/protovm/protovm_config.proto";
import "protos/perfetto/config/statsd/statsd_tracing_config.proto";
import "protos/perfetto/config/priority_boost/priority_boost_config.proto";
import "protos/perfetto/config/process_stats/process_stats_config.proto";
import "protos/perfetto/config/profiling/heapprofd_config.proto";
import "protos/perfetto/config/profiling/java_hprof_config.proto";
import "protos/perfetto/config/profiling/perf_event_config.proto";
import "protos/perfetto/config/sys_stats/sys_stats_config.proto";
import "protos/perfetto/config/test_config.proto";
import "protos/perfetto/config/track_event/track_event_config.proto";
import "protos/perfetto/config/system_info/system_info_config.proto";
import "protos/perfetto/config/chrome/histogram_samples.proto";
import "protos/perfetto/config/linux/journald_config.proto";
import "protos/perfetto/config/qnx/qnx_config.proto";

// The configuration that is passed to each data source when starting tracing.
// Next id: 141
message DataSourceConfig {
  enum SessionInitiator {
    SESSION_INITIATOR_UNSPECIFIED = 0;
    // This trace was initiated from a trusted system app has DUMP and
    // USAGE_STATS permission. This system app is expected to not expose the
    // trace to the user of the device.
    // This is determined by checking the UID initiating the trace.
    SESSION_INITIATOR_TRUSTED_SYSTEM = 1;
  };
  // Data source unique name, e.g., "linux.ftrace". This must match
  // the name passed by the data source when it registers (see
  // RegisterDataSource()).
  optional string name = 1;

  // The index of the logging buffer where TracePacket(s) will be stored.
  // This field is quite subtle as it has a double semantic:
  // 1) When the config is passed, this field is a 0-based index relative to the
  //    buffer array in the TraceConfig and defines the mapping between data
  //    sources and config. From v54 this is optional because the user can
  //    instead use target_buffer_name.
  // 2) When the TracingService issues a SetupDataSource/StartDataSource to the
  //    producer, it overwrites this field with the global buffer index (which
  //    depends on other tracing sessions active). This tells the producer which
  //    buffer id should be passed to CreateTraceWriter. In this case, the trace
  //    service always sets the resolved global id, even when using
  //    `target_buffer_name`.
  // In hindsight we should have used two different fields given even in v0 they
  // had a different semantic. But now it's too late as this would be a major
  // protocol breaking change.
  optional uint32 target_buffer = 2;

  // Alternative to |target_buffer|. References a buffer by name (as specified
  // in TraceConfig.BufferConfig.name) rather than by index. This is more
  // readable and less error-prone than using buffer indices.
  // If both |target_buffer| and |target_buffer_name| are specified, they must
  // refer to the same buffer, otherwise the service will reject the config.
  // Using both fields allows configs to work with both old and new versions
  // of the tracing service. Introduced in v54.
  optional string target_buffer_name = 11;

  // Set by the service to indicate the duration of the trace.
  // DO NOT SET in consumer as this will be overridden by the service.
  optional uint32 trace_duration_ms = 3;

  // If true, |trace_duration_ms| should count also time in suspend. This
  // is propagated from TraceConfig.prefer_suspend_clock_for_duration.
  optional bool prefer_suspend_clock_for_duration = 122;

  // Set by the service to indicate how long it waits after StopDataSource.
  // DO NOT SET in consumer as this will be overridden by the service.
  optional uint32 stop_timeout_ms = 7;

  // Set by the service to indicate whether this tracing session has extra
  // guardrails.
  // DO NOT SET in consumer as this will be overridden by the service.
  optional bool enable_extra_guardrails = 6;

  // Set by the service to indicate which user initiated this trace.
  // DO NOT SET in consumer as this will be overridden by the service.
  optional SessionInitiator session_initiator = 8;

  // Set by the service to indicate which tracing session the data source
  // belongs to. The intended use case for this is checking if two data sources,
  // one of which produces metadata for the other one, belong to the same trace
  // session and hence should be linked together.
  // This field was introduced in Aug 2018 after Android P.
  // DO NOT SET in consumer as this will be overridden by the service.
  optional uint64 tracing_session_id = 4;

  enum BufferExhaustedPolicy {
    // The data source will use its default buffer exhausted policy, specified
    // by the code when the data source is registered.
    BUFFER_EXHAUSTED_UNSPECIFIED = 0;
    // The data source will drop packets when there's no space in the shared
    // memory buffer.
    BUFFER_EXHAUSTED_DROP = 1;
    // The data source will wait when there's no space in the shared memory
    // buffer. If there's still not space, after a few seconds, the whole
    // producer process will be aborted.
    BUFFER_EXHAUSTED_STALL_THEN_ABORT = 2;
    // The data source will wait when there's no space in the shared memory
    // buffer.  If there's still not space, after a few seconds, the data source
    // will drop packets.
    BUFFER_EXHAUSTED_STALL_THEN_DROP = 3;
  };
  // How to behave when the producer runs out of space in the shared memory
  // buffer. This is only honored by some data sources (in the SDK, the data
  // sources registered with a configurable buffer exhausted policy).
  optional BufferExhaustedPolicy buffer_exhausted_policy = 9;

  optional PriorityBoostConfig priority_boost = 10;

  // If specified, the data source requires the tracing service to process
  // overwritten packets (patches) using a ProtoVM instance with this config.
  // The ProtoVM program is specified at data source registration time through
  // the descriptor.

  // TODO(primiano): today when we enable a protovm_config, we actually accept
  // that if N producers advertise M different versions of the same program we
  // will create M instances of that program.
  // To overcome this, we could move the program to the TraceConfig. But doing
  // so would bloat the trace config size too big and create problems to statsd.
  // Once the config store (b/482305876) exists we should move protovm programs
  // onto that.
  optional ProtoVmConfig protovm_config = 12;

  // Keep the lower IDs (up to 99) for fields that are *not* specific to
  // data-sources and needs to be processed by the traced daemon.

  // All data source config fields must be marked as [lazy=true]. This prevents
  // the proto-to-cpp generator from recursing into those when generating the
  // cpp classes and polluting tracing/core with data-source-specific classes.
  // Instead they are treated as opaque strings containing raw proto bytes.

  // Data source name: linux.ftrace
  optional FtraceConfig ftrace_config = 100 [lazy = true];
  // Data source name: linux.inode_file_map
  optional InodeFileConfig inode_file_config = 102 [lazy = true];
  // Data source name: linux.process_stats
  optional ProcessStatsConfig process_stats_config = 103 [lazy = true];
  // Data source name: linux.sys_stats
  optional SysStatsConfig sys_stats_config = 104 [lazy = true];
  // Data source name: android.heapprofd
  // Introduced in Android 10.
  optional HeapprofdConfig heapprofd_config = 105 [lazy = true];
  // Data source name: android.java_hprof
  // Introduced in Android 11.
  optional JavaHprofConfig java_hprof_config = 110 [lazy = true];
  // Data source name: android.power
  optional AndroidPowerConfig android_power_config = 106 [lazy = true];
  // Data source name: android.log
  optional AndroidLogConfig android_log_config = 107 [lazy = true];
  // Data source name: gpu.counters
  optional GpuCounterConfig gpu_counter_config = 108 [lazy = true];
  // Data source name: android.game_interventions
  optional AndroidGameInterventionListConfig
      android_game_intervention_list_config = 116 [lazy = true];
  // Data source name: android.packages_list
  optional PackagesListConfig packages_list_config = 109 [lazy = true];
  // Data source name: linux.perf
  optional PerfEventConfig perf_event_config = 111 [lazy = true];
  // Data source name: vulkan.memory_tracker
  optional VulkanMemoryConfig vulkan_memory_config = 112 [lazy = true];
  // Data source name: track_event
  optional TrackEventConfig track_event_config = 113 [lazy = true];
  // Data source name: android.polled_state
  optional AndroidPolledStateConfig android_polled_state_config = 114
      [lazy = true];
  // Data source name: android.system_property
  optional AndroidSystemPropertyConfig android_system_property_config = 118
      [lazy = true];
  // Data source name: android.statsd
  optional StatsdTracingConfig statsd_tracing_config = 117 [lazy = true];
  // Data source name: linux.system_info
  optional SystemInfoConfig system_info_config = 119;
  // Data source name: linux.frozen_ftrace
  optional FrozenFtraceConfig frozen_ftrace_config = 136 [lazy = true];

  // Chrome is special as it doesn't use the perfetto IPC layer. We want to
  // avoid proto serialization and de-serialization there because that would
  // just add extra hops on top of the Mojo ser/des. Instead we auto-generate a
  // C++ class for it so it can pass around plain C++ objets.
  optional ChromeConfig chrome_config = 101;

  // Data source name: code.v8.dev
  optional V8Config v8_config = 127 [lazy = true];

  // If an interceptor is specified here, packets for this data source will be
  // rerouted to the interceptor instead of the main trace buffer. This can be
  // used, for example, to write trace data into ETW or for logging trace points
  // to the console.
  //
  // Note that interceptors are only supported by data sources registered
  // through the Perfetto SDK API. Data sources that don't use that API (e.g.,
  // traced_probes) may not support interception.
  optional InterceptorConfig interceptor_config = 115;

  // Data source name: android.network_packets.
  // Introduced in Android 14 (U).
  optional NetworkPacketTraceConfig network_packet_trace_config = 120
      [lazy = true];

  // Data source name: android.surfaceflinger.layers
  optional SurfaceFlingerLayersConfig surfaceflinger_layers_config = 121
      [lazy = true];

  // Data source name: android.surfaceflinger.transactions
  optional SurfaceFlingerTransactionsConfig surfaceflinger_transactions_config =
      123 [lazy = true];

  // Data source name: android.sdk_sysprop_guard
  // Introduced in Android 14 (U) QPR1.
  optional AndroidSdkSyspropGuardConfig android_sdk_sysprop_guard_config = 124
      [lazy = true];

  // Data source name: windows.etw
  optional EtwConfig etw_config = 125 [lazy = true];

  // Data source name: android.protolog
  optional ProtoLogConfig protolog_config = 126 [lazy = true];

  // Data source name: android.input.inputevent
  optional AndroidInputEventConfig android_input_event_config = 128
      [lazy = true];

  // Data source name: android.pixel.modem
  optional PixelModemConfig pixel_modem_config = 129 [lazy = true];

  // Data source name: android.windowmanager
  optional WindowManagerConfig windowmanager_config = 130 [lazy = true];

  // Data source name: org.chromium.system_metrics
  optional ChromiumSystemMetricsConfig chromium_system_metrics = 131
      [lazy = true];

  // Data source name: android.kernel_wakelocks
  optional KernelWakelocksConfig kernel_wakelocks_config = 132 [lazy = true];

  // Data source name: gpu.renderstages
  optional GpuRenderStagesConfig gpu_renderstages_config = 133 [lazy = true];

  // Data source name: org.chromium.histogram_samples
  optional ChromiumHistogramSamplesConfig chromium_histogram_samples = 134
      [lazy = true];

  // Data source name: android.app_wakelocks
  optional AppWakelocksConfig app_wakelocks_config = 135 [lazy = true];

  // Data source name: android.cpu_per_uid
  optional CpuPerUidConfig cpu_per_uid_config = 137 [lazy = true];

  // Data source name: android.user_list
  optional AndroidUserListConfig user_list_config = 138 [lazy = true];

  // Data source name: android.inputmethod
  optional InputMethodConfig inputmethod_config = 139 [lazy = true];

  // Data source name: android.aflags
  optional AndroidAflagsConfig android_aflags_config = 140 [lazy = true];

  // Data source name: linux.systemd_journald
  optional SystemdJournaldConfig journald_config = 141 [lazy = true];

  // Data source name: qnx.kernel
  optional QnxConfig qnx_config = 150 [lazy = true];

  // This is a fallback mechanism to send a free-form text config to the
  // producer. In theory this should never be needed. All the code that
  // is part of the platform (i.e. traced service) is supposed to *not* truncate
  // the trace config proto and propagate unknown fields. However, if anything
  // in the pipeline (client or backend) ends up breaking this forward compat
  // plan, this field will become the escape hatch to allow future data sources
  // to get some meaningful configuration.
  optional string legacy_config = 1000;

  // This field is only used for testing.
  optional TestConfig for_testing = 1001;

  // Was |for_testing|. Caused more problems then found.
  reserved 268435455;
}
