1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INCLUDE_PERFETTO_TRACING_INTERNAL_DATA_SOURCE_INTERNAL_H_ 18 #define INCLUDE_PERFETTO_TRACING_INTERNAL_DATA_SOURCE_INTERNAL_H_ 19 20 #include <stddef.h> 21 #include <stdint.h> 22 23 #include <array> 24 #include <atomic> 25 #include <functional> 26 #include <memory> 27 #include <mutex> 28 29 // No perfetto headers (other than tracing/api and protozero) should be here. 30 #include "perfetto/tracing/buffer_exhausted_policy.h" 31 #include "perfetto/tracing/internal/basic_types.h" 32 #include "perfetto/tracing/trace_writer_base.h" 33 34 namespace perfetto { 35 36 class DataSourceBase; 37 class InterceptorBase; 38 class TraceWriterBase; 39 40 namespace internal { 41 42 class TracingTLS; 43 44 // This maintains the internal state of a data source instance that is used only 45 // to implement the tracing mechanics and is not exposed to the API client. 46 // There is one of these object per DataSource instance (up to 47 // kMaxDataSourceInstances). 48 struct DataSourceState { 49 // This boolean flag determines whether the DataSource::Trace() method should 50 // do something or be a no-op. This flag doesn't give the full guarantee 51 // that tracing data will be visible in the trace, it just makes it so that 52 // the client attemps writing trace data and interacting with the service. 53 // For instance, when a tracing session ends the service will reject data 54 // commits that arrive too late even if the producer hasn't received the stop 55 // IPC message. 56 // This flag is set right before calling OnStart() and cleared right before 57 // calling OnStop(), unless using HandleStopAsynchronously() (see comments 58 // in data_source.h). 59 // Keep this flag as the first field. This allows the compiler to directly 60 // dereference the DataSourceState* pointer in the trace fast-path without 61 // doing extra pointr arithmetic. 62 bool trace_lambda_enabled = false; 63 64 // The overall TracingMuxerImpl instance id, which gets incremented by 65 // ResetForTesting. 66 uint32_t muxer_id_for_testing = 0; 67 68 // The central buffer id that all TraceWriter(s) created by this data source 69 // must target. 70 BufferId buffer_id = 0; 71 72 // The index within TracingMuxerImpl.backends_. Practically it allows to 73 // lookup the Producer object, and hence the IPC channel, for this data 74 // source. 75 TracingBackendId backend_id = 0; 76 77 // Each backend may connect to the tracing service multiple times if a 78 // disconnection occurs. This counter is used to uniquely identify each 79 // connection so that trace writers don't get reused across connections. 80 uint32_t backend_connection_id = 0; 81 82 // The instance id as assigned by the tracing service. Note that because a 83 // process can be connected to >1 services, this ID is not globally unique but 84 // is only unique within the scope of its backend. 85 // Only the tuple (backend_id, data_source_instance_id) is globally unique. 86 uint64_t data_source_instance_id = 0; 87 88 // A hash of the trace config used by this instance. This is used to 89 // de-duplicate instances for data sources with identical names (e.g., track 90 // event). 91 uint64_t config_hash = 0; 92 93 // If this data source is being intercepted (see Interceptor), this field 94 // contains the non-zero id of a registered interceptor which should receive 95 // trace packets for this session. Note: interceptor id 1 refers to the first 96 // element of TracingMuxerImpl::interceptors_ with successive numbers using 97 // the following slots. 98 uint32_t interceptor_id = 0; 99 100 // This lock is not held to implement Trace() and it's used only if the trace 101 // code wants to access its own data source state. 102 // This is to prevent that accessing the data source on an arbitrary embedder 103 // thread races with the internal IPC thread destroying the data source 104 // because of a end-of-tracing notification from the service. 105 // This lock is also used to protect access to a possible interceptor for this 106 // data source session. 107 std::recursive_mutex lock; 108 std::unique_ptr<DataSourceBase> data_source; 109 std::unique_ptr<InterceptorBase> interceptor; 110 }; 111 112 // This is to allow lazy-initialization and avoid static initializers and 113 // at-exit destructors. All the entries are initialized via placement-new when 114 // DataSource::Register() is called, see TracingMuxerImpl::RegisterDataSource(). 115 struct DataSourceStateStorage { 116 alignas(DataSourceState) char storage[sizeof(DataSourceState)]{}; 117 }; 118 119 // Per-DataSource-type global state. 120 struct DataSourceStaticState { 121 // System-wide unique id of the data source. 122 uint64_t id = 0; 123 124 // Unique index of the data source, assigned at registration time. 125 uint32_t index = kMaxDataSources; 126 127 // A bitmap that tells about the validity of each |instances| entry. When the 128 // i-th bit of the bitmap it's set, instances[i] is valid. 129 std::atomic<uint32_t> valid_instances{}; 130 std::array<DataSourceStateStorage, kMaxDataSourceInstances> instances{}; 131 132 // Incremented whenever incremental state should be reset for any instance of 133 // this data source. 134 std::atomic<uint32_t> incremental_state_generation{}; 135 136 // Can be used with a cached |valid_instances| bitmap. TryGetCachedDataSourceStaticState137 DataSourceState* TryGetCached(uint32_t cached_bitmap, size_t n) { 138 return cached_bitmap & (1 << n) 139 ? reinterpret_cast<DataSourceState*>(&instances[n]) 140 : nullptr; 141 } 142 TryGetDataSourceStaticState143 DataSourceState* TryGet(size_t n) { 144 return TryGetCached(valid_instances.load(std::memory_order_acquire), n); 145 } 146 CompilerAssertsDataSourceStaticState147 void CompilerAsserts() { 148 static_assert(sizeof(valid_instances.load()) * 8 >= kMaxDataSourceInstances, 149 "kMaxDataSourceInstances too high"); 150 } 151 }; 152 153 // Per-DataSource-instance thread-local state. 154 struct DataSourceInstanceThreadLocalState { 155 ResetDataSourceInstanceThreadLocalState156 void Reset() { 157 trace_writer.reset(); 158 incremental_state.reset(); 159 data_source_custom_tls.reset(); 160 muxer_id_for_testing = 0; 161 backend_id = 0; 162 backend_connection_id = 0; 163 buffer_id = 0; 164 data_source_instance_id = 0; 165 incremental_state_generation = 0; 166 is_intercepted = false; 167 } 168 169 std::unique_ptr<TraceWriterBase> trace_writer; 170 using ObjectWithDeleter = std::unique_ptr<void, void (*)(void*)>; 171 ObjectWithDeleter incremental_state = {nullptr, [](void*) {}}; 172 ObjectWithDeleter data_source_custom_tls = {nullptr, [](void*) {}}; 173 uint32_t incremental_state_generation; 174 uint32_t muxer_id_for_testing; 175 TracingBackendId backend_id; 176 uint32_t backend_connection_id; 177 BufferId buffer_id; 178 uint64_t data_source_instance_id; 179 bool is_intercepted; 180 }; 181 182 // Per-DataSource-type thread-local state. 183 struct DataSourceThreadLocalState { 184 DataSourceStaticState* static_state = nullptr; 185 186 // Pointer to the parent tls object that holds us. Used to retrieve the 187 // generation, which is per-global-TLS and not per data-source. 188 TracingTLS* root_tls = nullptr; 189 190 // One entry per each data source instance. 191 std::array<DataSourceInstanceThreadLocalState, kMaxDataSourceInstances> 192 per_instance{}; 193 }; 194 195 } // namespace internal 196 } // namespace perfetto 197 198 #endif // INCLUDE_PERFETTO_TRACING_INTERNAL_DATA_SOURCE_INTERNAL_H_ 199