1 /* 2 * Copyright (C) 2017 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 SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_ 18 #define SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_ 19 20 #include <algorithm> 21 #include <functional> 22 #include <map> 23 #include <memory> 24 #include <mutex> 25 #include <set> 26 #include <utility> 27 #include <vector> 28 29 #include "perfetto/base/logging.h" 30 #include "perfetto/base/time.h" 31 #include "perfetto/ext/base/optional.h" 32 #include "perfetto/ext/base/weak_ptr.h" 33 #include "perfetto/ext/tracing/core/basic_types.h" 34 #include "perfetto/ext/tracing/core/commit_data_request.h" 35 #include "perfetto/ext/tracing/core/observable_events.h" 36 #include "perfetto/ext/tracing/core/shared_memory_abi.h" 37 #include "perfetto/ext/tracing/core/trace_stats.h" 38 #include "perfetto/ext/tracing/core/tracing_service.h" 39 #include "perfetto/tracing/core/data_source_config.h" 40 #include "perfetto/tracing/core/data_source_descriptor.h" 41 #include "perfetto/tracing/core/forward_decls.h" 42 #include "perfetto/tracing/core/trace_config.h" 43 #include "src/tracing/core/id_allocator.h" 44 45 namespace perfetto { 46 47 namespace base { 48 class TaskRunner; 49 } // namespace base 50 51 class Consumer; 52 class Producer; 53 class SharedMemory; 54 class SharedMemoryArbiterImpl; 55 class TraceBuffer; 56 class TracePacket; 57 58 // The tracing service business logic. 59 class TracingServiceImpl : public TracingService { 60 private: 61 struct DataSourceInstance; 62 63 public: 64 static constexpr size_t kDefaultShmPageSize = base::kPageSize; 65 static constexpr size_t kDefaultShmSize = 256 * 1024ul; 66 static constexpr size_t kMaxShmSize = 32 * 1024 * 1024ul; 67 static constexpr uint32_t kDataSourceStopTimeoutMs = 5000; 68 static constexpr uint8_t kSyncMarker[] = {0x82, 0x47, 0x7a, 0x76, 0xb2, 0x8d, 69 0x42, 0xba, 0x81, 0xdc, 0x33, 0x32, 70 0x6d, 0x57, 0xa0, 0x79}; 71 72 // The implementation behind the service endpoint exposed to each producer. 73 class ProducerEndpointImpl : public TracingService::ProducerEndpoint { 74 public: 75 ProducerEndpointImpl(ProducerID, 76 uid_t uid, 77 TracingServiceImpl*, 78 base::TaskRunner*, 79 Producer*, 80 const std::string& producer_name, 81 bool in_process, 82 bool smb_scraping_enabled); 83 ~ProducerEndpointImpl() override; 84 85 // TracingService::ProducerEndpoint implementation. 86 void RegisterDataSource(const DataSourceDescriptor&) override; 87 void UnregisterDataSource(const std::string& name) override; 88 void RegisterTraceWriter(uint32_t writer_id, 89 uint32_t target_buffer) override; 90 void UnregisterTraceWriter(uint32_t writer_id) override; 91 void CommitData(const CommitDataRequest&, CommitDataCallback) override; 92 void SetupSharedMemory(std::unique_ptr<SharedMemory>, 93 size_t page_size_bytes, 94 bool provided_by_producer); 95 std::unique_ptr<TraceWriter> CreateTraceWriter( 96 BufferID, 97 BufferExhaustedPolicy) override; 98 SharedMemoryArbiter* MaybeSharedMemoryArbiter() override; 99 bool IsShmemProvidedByProducer() const override; 100 void NotifyFlushComplete(FlushRequestID) override; 101 void NotifyDataSourceStarted(DataSourceInstanceID) override; 102 void NotifyDataSourceStopped(DataSourceInstanceID) override; 103 SharedMemory* shared_memory() const override; 104 size_t shared_buffer_page_size_kb() const override; 105 void ActivateTriggers(const std::vector<std::string>&) override; 106 void Sync(std::function<void()> callback) override; 107 108 void OnTracingSetup(); 109 void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&); 110 void StartDataSource(DataSourceInstanceID, const DataSourceConfig&); 111 void StopDataSource(DataSourceInstanceID); 112 void Flush(FlushRequestID, const std::vector<DataSourceInstanceID>&); 113 void OnFreeBuffers(const std::vector<BufferID>& target_buffers); 114 void ClearIncrementalState(const std::vector<DataSourceInstanceID>&); 115 is_allowed_target_buffer(BufferID buffer_id)116 bool is_allowed_target_buffer(BufferID buffer_id) const { 117 return allowed_target_buffers_.count(buffer_id); 118 } 119 buffer_id_for_writer(WriterID writer_id)120 base::Optional<BufferID> buffer_id_for_writer(WriterID writer_id) const { 121 const auto it = writers_.find(writer_id); 122 if (it != writers_.end()) 123 return it->second; 124 return base::nullopt; 125 } 126 uid()127 uid_t uid() const { return uid_; } 128 129 private: 130 friend class TracingServiceImpl; 131 friend class TracingServiceImplTest; 132 friend class TracingIntegrationTest; 133 ProducerEndpointImpl(const ProducerEndpointImpl&) = delete; 134 ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete; 135 136 ProducerID const id_; 137 const uid_t uid_; 138 TracingServiceImpl* const service_; 139 base::TaskRunner* const task_runner_; 140 Producer* producer_; 141 std::unique_ptr<SharedMemory> shared_memory_; 142 size_t shared_buffer_page_size_kb_ = 0; 143 SharedMemoryABI shmem_abi_; 144 size_t shmem_size_hint_bytes_ = 0; 145 size_t shmem_page_size_hint_bytes_ = 0; 146 bool is_shmem_provided_by_producer_ = false; 147 const std::string name_; 148 bool in_process_; 149 bool smb_scraping_enabled_; 150 151 // Set of the global target_buffer IDs that the producer is configured to 152 // write into in any active tracing session. 153 std::set<BufferID> allowed_target_buffers_; 154 155 // Maps registered TraceWriter IDs to their target buffers as registered by 156 // the producer. Note that producers aren't required to register their 157 // writers, so we may see commits of chunks with WriterIDs that aren't 158 // contained in this map. However, if a producer does register a writer, the 159 // service will prevent the writer from writing into any other buffer than 160 // the one associated with it here. The BufferIDs stored in this map are 161 // untrusted, so need to be verified against |allowed_target_buffers_| 162 // before use. 163 std::map<WriterID, BufferID> writers_; 164 165 // This is used only in in-process configurations. 166 // SharedMemoryArbiterImpl methods themselves are thread-safe. 167 std::unique_ptr<SharedMemoryArbiterImpl> inproc_shmem_arbiter_; 168 169 PERFETTO_THREAD_CHECKER(thread_checker_) 170 base::WeakPtrFactory<ProducerEndpointImpl> weak_ptr_factory_; // Keep last. 171 }; 172 173 // The implementation behind the service endpoint exposed to each consumer. 174 class ConsumerEndpointImpl : public TracingService::ConsumerEndpoint { 175 public: 176 ConsumerEndpointImpl(TracingServiceImpl*, 177 base::TaskRunner*, 178 Consumer*, 179 uid_t uid); 180 ~ConsumerEndpointImpl() override; 181 182 void NotifyOnTracingDisabled(); 183 base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr(); 184 185 // TracingService::ConsumerEndpoint implementation. 186 void EnableTracing(const TraceConfig&, base::ScopedFile) override; 187 void ChangeTraceConfig(const TraceConfig& cfg) override; 188 void StartTracing() override; 189 void DisableTracing() override; 190 void ReadBuffers() override; 191 void FreeBuffers() override; 192 void Flush(uint32_t timeout_ms, FlushCallback) override; 193 void Detach(const std::string& key) override; 194 void Attach(const std::string& key) override; 195 void GetTraceStats() override; 196 void ObserveEvents(uint32_t enabled_event_types) override; 197 void QueryServiceState(QueryServiceStateCallback) override; 198 void QueryCapabilities(QueryCapabilitiesCallback) override; 199 200 // Will queue a task to notify the consumer about the state change. 201 void OnDataSourceInstanceStateChange(const ProducerEndpointImpl&, 202 const DataSourceInstance&); 203 void OnAllDataSourcesStarted(); 204 205 private: 206 friend class TracingServiceImpl; 207 ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete; 208 ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete; 209 210 // Returns a pointer to an ObservableEvents object that the caller can fill 211 // and schedules a task to send the ObservableEvents to the consumer. 212 ObservableEvents* AddObservableEvents(); 213 214 base::TaskRunner* const task_runner_; 215 TracingServiceImpl* const service_; 216 Consumer* const consumer_; 217 uid_t const uid_; 218 TracingSessionID tracing_session_id_ = 0; 219 220 // Whether the consumer is interested in DataSourceInstance state change 221 // events. 222 uint32_t observable_events_mask_ = 0; 223 224 // ObservableEvents that will be sent to the consumer. If set, a task to 225 // flush the events to the consumer has been queued. 226 std::unique_ptr<ObservableEvents> observable_events_; 227 228 PERFETTO_THREAD_CHECKER(thread_checker_) 229 base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_; // Keep last. 230 }; 231 232 explicit TracingServiceImpl(std::unique_ptr<SharedMemory::Factory>, 233 base::TaskRunner*); 234 ~TracingServiceImpl() override; 235 236 // Called by ProducerEndpointImpl. 237 void DisconnectProducer(ProducerID); 238 void RegisterDataSource(ProducerID, const DataSourceDescriptor&); 239 void UnregisterDataSource(ProducerID, const std::string& name); 240 void CopyProducerPageIntoLogBuffer(ProducerID, 241 uid_t, 242 WriterID, 243 ChunkID, 244 BufferID, 245 uint16_t num_fragments, 246 uint8_t chunk_flags, 247 bool chunk_complete, 248 const uint8_t* src, 249 size_t size); 250 void ApplyChunkPatches(ProducerID, 251 const std::vector<CommitDataRequest::ChunkToPatch>&); 252 void NotifyFlushDoneForProducer(ProducerID, FlushRequestID); 253 void NotifyDataSourceStarted(ProducerID, const DataSourceInstanceID); 254 void NotifyDataSourceStopped(ProducerID, const DataSourceInstanceID); 255 void ActivateTriggers(ProducerID, const std::vector<std::string>& triggers); 256 257 // Called by ConsumerEndpointImpl. 258 bool DetachConsumer(ConsumerEndpointImpl*, const std::string& key); 259 bool AttachConsumer(ConsumerEndpointImpl*, const std::string& key); 260 void DisconnectConsumer(ConsumerEndpointImpl*); 261 bool EnableTracing(ConsumerEndpointImpl*, 262 const TraceConfig&, 263 base::ScopedFile); 264 void ChangeTraceConfig(ConsumerEndpointImpl*, const TraceConfig&); 265 266 bool StartTracing(TracingSessionID); 267 void DisableTracing(TracingSessionID, bool disable_immediately = false); 268 void Flush(TracingSessionID tsid, 269 uint32_t timeout_ms, 270 ConsumerEndpoint::FlushCallback); 271 void FlushAndDisableTracing(TracingSessionID); 272 bool ReadBuffers(TracingSessionID, ConsumerEndpointImpl*); 273 void FreeBuffers(TracingSessionID); 274 275 // Service implementation. 276 std::unique_ptr<TracingService::ProducerEndpoint> ConnectProducer( 277 Producer*, 278 uid_t uid, 279 const std::string& producer_name, 280 size_t shared_memory_size_hint_bytes = 0, 281 bool in_process = false, 282 ProducerSMBScrapingMode smb_scraping_mode = 283 ProducerSMBScrapingMode::kDefault, 284 size_t shared_memory_page_size_hint_bytes = 0, 285 std::unique_ptr<SharedMemory> shm = nullptr) override; 286 287 std::unique_ptr<TracingService::ConsumerEndpoint> ConnectConsumer( 288 Consumer*, 289 uid_t) override; 290 291 // Set whether SMB scraping should be enabled by default or not. Producers can 292 // override this setting for their own SMBs. SetSMBScrapingEnabled(bool enabled)293 void SetSMBScrapingEnabled(bool enabled) override { 294 smb_scraping_enabled_ = enabled; 295 } 296 297 // Exposed mainly for testing. num_producers()298 size_t num_producers() const { return producers_.size(); } 299 ProducerEndpointImpl* GetProducer(ProducerID) const; 300 301 private: 302 friend class TracingServiceImplTest; 303 friend class TracingIntegrationTest; 304 305 struct RegisteredDataSource { 306 ProducerID producer_id; 307 DataSourceDescriptor descriptor; 308 }; 309 310 // Represents an active data source for a tracing session. 311 struct DataSourceInstance { DataSourceInstanceDataSourceInstance312 DataSourceInstance(DataSourceInstanceID id, 313 const DataSourceConfig& cfg, 314 const std::string& ds_name, 315 bool notify_on_start, 316 bool notify_on_stop, 317 bool handles_incremental_state_invalidation) 318 : instance_id(id), 319 config(cfg), 320 data_source_name(ds_name), 321 will_notify_on_start(notify_on_start), 322 will_notify_on_stop(notify_on_stop), 323 handles_incremental_state_clear( 324 handles_incremental_state_invalidation) {} 325 DataSourceInstance(const DataSourceInstance&) = delete; 326 DataSourceInstance& operator=(const DataSourceInstance&) = delete; 327 328 DataSourceInstanceID instance_id; 329 DataSourceConfig config; 330 std::string data_source_name; 331 bool will_notify_on_start; 332 bool will_notify_on_stop; 333 bool handles_incremental_state_clear; 334 335 enum DataSourceInstanceState { 336 CONFIGURED, 337 STARTING, 338 STARTED, 339 STOPPING, 340 STOPPED 341 }; 342 DataSourceInstanceState state = CONFIGURED; 343 }; 344 345 struct PendingFlush { 346 std::set<ProducerID> producers; 347 ConsumerEndpoint::FlushCallback callback; PendingFlushPendingFlush348 explicit PendingFlush(decltype(callback) cb) : callback(std::move(cb)) {} 349 }; 350 351 // Holds the state of a tracing session. A tracing session is uniquely bound 352 // a specific Consumer. Each Consumer can own one or more sessions. 353 struct TracingSession { 354 enum State { 355 DISABLED = 0, 356 CONFIGURED, 357 STARTED, 358 DISABLING_WAITING_STOP_ACKS 359 }; 360 361 TracingSession(TracingSessionID, ConsumerEndpointImpl*, const TraceConfig&); 362 num_buffersTracingSession363 size_t num_buffers() const { return buffers_index.size(); } 364 delay_to_next_write_period_msTracingSession365 uint32_t delay_to_next_write_period_ms() const { 366 PERFETTO_DCHECK(write_period_ms > 0); 367 return write_period_ms - 368 (base::GetWallTimeMs().count() % write_period_ms); 369 } 370 flush_timeout_msTracingSession371 uint32_t flush_timeout_ms() { 372 uint32_t timeout_ms = config.flush_timeout_ms(); 373 return timeout_ms ? timeout_ms : kDefaultFlushTimeoutMs; 374 } 375 data_source_stop_timeout_msTracingSession376 uint32_t data_source_stop_timeout_ms() { 377 uint32_t timeout_ms = config.data_source_stop_timeout_ms(); 378 return timeout_ms ? timeout_ms : kDataSourceStopTimeoutMs; 379 } 380 GetPacketSequenceIDTracingSession381 PacketSequenceID GetPacketSequenceID(ProducerID producer_id, 382 WriterID writer_id) { 383 auto key = std::make_pair(producer_id, writer_id); 384 auto it = packet_sequence_ids.find(key); 385 if (it != packet_sequence_ids.end()) 386 return it->second; 387 // We shouldn't run out of sequence IDs (producer ID is 16 bit, writer IDs 388 // are limited to 1024). 389 static_assert(kMaxPacketSequenceID > kMaxProducerID * kMaxWriterID, 390 "PacketSequenceID value space doesn't cover service " 391 "sequence ID and all producer/writer ID combinations!"); 392 PERFETTO_DCHECK(last_packet_sequence_id < kMaxPacketSequenceID); 393 PacketSequenceID sequence_id = ++last_packet_sequence_id; 394 packet_sequence_ids[key] = sequence_id; 395 return sequence_id; 396 } 397 GetDataSourceInstanceTracingSession398 DataSourceInstance* GetDataSourceInstance( 399 ProducerID producer_id, 400 DataSourceInstanceID instance_id) { 401 for (auto& inst_kv : data_source_instances) { 402 if (inst_kv.first != producer_id || 403 inst_kv.second.instance_id != instance_id) { 404 continue; 405 } 406 return &inst_kv.second; 407 } 408 return nullptr; 409 } 410 AllDataSourceInstancesStartedTracingSession411 bool AllDataSourceInstancesStarted() { 412 return std::all_of( 413 data_source_instances.begin(), data_source_instances.end(), 414 [](decltype(data_source_instances)::const_reference x) { 415 return x.second.state == DataSourceInstance::STARTED; 416 }); 417 } 418 AllDataSourceInstancesStoppedTracingSession419 bool AllDataSourceInstancesStopped() { 420 return std::all_of( 421 data_source_instances.begin(), data_source_instances.end(), 422 [](decltype(data_source_instances)::const_reference x) { 423 return x.second.state == DataSourceInstance::STOPPED; 424 }); 425 } 426 427 const TracingSessionID id; 428 429 // The consumer that started the session. 430 // Can be nullptr if the consumer detached from the session. 431 ConsumerEndpointImpl* consumer_maybe_null; 432 433 // Unix uid of the consumer. This is valid even after the consumer detaches 434 // and does not change for the entire duration of the session. It is used to 435 // prevent that a consumer re-attaches to a session from a different uid. 436 uid_t const consumer_uid; 437 438 // The list of triggers this session received while alive and the time they 439 // were received at. This is used to insert 'fake' packets back to the 440 // consumer so they can tell when some event happened. The order matches the 441 // order they were received. 442 struct TriggerInfo { 443 uint64_t boot_time_ns; 444 std::string trigger_name; 445 std::string producer_name; 446 uid_t producer_uid; 447 }; 448 std::vector<TriggerInfo> received_triggers; 449 450 // The trace config provided by the Consumer when calling 451 // EnableTracing(), plus any updates performed by ChangeTraceConfig. 452 TraceConfig config; 453 454 // List of data source instances that have been enabled on the various 455 // producers for this tracing session. 456 // TODO(rsavitski): at the time of writing, the map structure is unused 457 // (even when the calling code has a key). This is also an opportunity to 458 // consider an alternative data type, e.g. a map of vectors. 459 std::multimap<ProducerID, DataSourceInstance> data_source_instances; 460 461 // For each Flush(N) request, keeps track of the set of producers for which 462 // we are still awaiting a NotifyFlushComplete(N) ack. 463 std::map<FlushRequestID, PendingFlush> pending_flushes; 464 465 // Maps a per-trace-session buffer index into the corresponding global 466 // BufferID (shared namespace amongst all consumers). This vector has as 467 // many entries as |config.buffers_size()|. 468 std::vector<BufferID> buffers_index; 469 470 std::map<std::pair<ProducerID, WriterID>, PacketSequenceID> 471 packet_sequence_ids; 472 PacketSequenceID last_packet_sequence_id = kServicePacketSequenceID; 473 474 // Whether we should emit the trace stats next time we reach EOF while 475 // performing ReadBuffers. 476 bool should_emit_stats = false; 477 478 // Whether we should emit the sync marker the next time ReadBuffers() is 479 // called. 480 bool should_emit_sync_marker = false; 481 482 // Whether we mirrored the trace config back to the trace output yet. 483 bool did_emit_config = false; 484 485 // Whether we put the system info into the trace output yet. 486 bool did_emit_system_info = false; 487 488 // The number of received triggers we've emitted into the trace output. 489 size_t num_triggers_emitted_into_trace = 0; 490 491 // Packets that failed validation of the TrustedPacket. 492 uint64_t invalid_packets = 0; 493 494 // Set to true on the first call to OnAllDataSourcesStarted(). 495 bool did_notify_all_data_source_started = false; 496 bool did_emit_all_data_source_started = false; 497 base::TimeNanos time_all_data_source_started = {}; 498 499 using ClockSnapshotData = 500 std::vector<std::pair<uint32_t /*clock_id*/, uint64_t /*ts*/>>; 501 502 // Initial clock snapshot, captured at trace start time (when state goes to 503 // TracingSession::STARTED). Emitted into the trace when the consumer first 504 // begins reading the trace. 505 ClockSnapshotData initial_clock_snapshot_; 506 507 // Most recent clock snapshot, captured periodically after the trace was 508 // started. Emitted into the trace when the consumer next calls 509 // ReadBuffers(). 510 ClockSnapshotData last_clock_snapshot_; 511 512 State state = DISABLED; 513 514 // If the consumer detached the session, this variable defines the key used 515 // for identifying the session later when reattaching. 516 std::string detach_key; 517 518 // This is set when the Consumer calls sets |write_into_file| == true in the 519 // TraceConfig. In this case this represents the file we should stream the 520 // trace packets into, rather than returning it to the consumer via 521 // OnTraceData(). 522 base::ScopedFile write_into_file; 523 uint32_t write_period_ms = 0; 524 uint64_t max_file_size_bytes = 0; 525 uint64_t bytes_written_into_file = 0; 526 }; 527 528 TracingServiceImpl(const TracingServiceImpl&) = delete; 529 TracingServiceImpl& operator=(const TracingServiceImpl&) = delete; 530 531 DataSourceInstance* SetupDataSource(const TraceConfig::DataSource&, 532 const TraceConfig::ProducerConfig&, 533 const RegisteredDataSource&, 534 TracingSession*); 535 536 // Returns the next available ProducerID that is not in |producers_|. 537 ProducerID GetNextProducerID(); 538 539 // Returns a pointer to the |tracing_sessions_| entry or nullptr if the 540 // session doesn't exists. 541 TracingSession* GetTracingSession(TracingSessionID); 542 543 // Returns a pointer to the |tracing_sessions_| entry, matching the given 544 // uid and detach key, or nullptr if no such session exists. 545 TracingSession* GetDetachedSession(uid_t, const std::string& key); 546 547 // Update the memory guard rail by using the latest information from the 548 // shared memory and trace buffers. 549 void UpdateMemoryGuardrail(); 550 551 void StartDataSourceInstance(ProducerEndpointImpl* producer, 552 TracingSession* tracing_session, 553 DataSourceInstance* instance); 554 void StopDataSourceInstance(ProducerEndpointImpl* producer, 555 TracingSession* tracing_session, 556 DataSourceInstance* instance, 557 bool disable_immediately); 558 void PeriodicSnapshotTask(TracingSession* tracing_session, 559 bool is_initial_snapshot); 560 void SnapshotSyncMarker(std::vector<TracePacket>*); 561 void SnapshotClocks(TracingSession::ClockSnapshotData*); 562 void EmitClockSnapshot(TracingSession* tracing_session, 563 TracingSession::ClockSnapshotData, 564 bool set_root_timestamp, 565 std::vector<TracePacket>*); 566 void SnapshotStats(TracingSession*, std::vector<TracePacket>*); 567 TraceStats GetTraceStats(TracingSession* tracing_session); 568 void MaybeEmitServiceEvents(TracingSession*, std::vector<TracePacket>*); 569 void MaybeEmitTraceConfig(TracingSession*, std::vector<TracePacket>*); 570 void MaybeEmitSystemInfo(TracingSession*, std::vector<TracePacket>*); 571 void MaybeEmitReceivedTriggers(TracingSession*, std::vector<TracePacket>*); 572 void MaybeNotifyAllDataSourcesStarted(TracingSession*); 573 void OnFlushTimeout(TracingSessionID, FlushRequestID); 574 void OnDisableTracingTimeout(TracingSessionID); 575 void DisableTracingNotifyConsumerAndFlushFile(TracingSession*); 576 void PeriodicFlushTask(TracingSessionID, bool post_next_only); 577 void CompleteFlush(TracingSessionID tsid, 578 ConsumerEndpoint::FlushCallback callback, 579 bool success); 580 void ScrapeSharedMemoryBuffers(TracingSession* tracing_session, 581 ProducerEndpointImpl* producer); 582 void PeriodicClearIncrementalStateTask(TracingSessionID, bool post_next_only); 583 TraceBuffer* GetBufferByID(BufferID); 584 void OnStartTriggersTimeout(TracingSessionID tsid); 585 586 base::TaskRunner* const task_runner_; 587 std::unique_ptr<SharedMemory::Factory> shm_factory_; 588 ProducerID last_producer_id_ = 0; 589 DataSourceInstanceID last_data_source_instance_id_ = 0; 590 TracingSessionID last_tracing_session_id_ = 0; 591 FlushRequestID last_flush_request_id_ = 0; 592 uid_t uid_ = 0; 593 594 // Buffer IDs are global across all consumers (because a Producer can produce 595 // data for more than one trace session, hence more than one consumer). 596 IdAllocator<BufferID> buffer_ids_; 597 598 std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_; 599 std::map<ProducerID, ProducerEndpointImpl*> producers_; 600 std::set<ConsumerEndpointImpl*> consumers_; 601 std::map<TracingSessionID, TracingSession> tracing_sessions_; 602 std::map<BufferID, std::unique_ptr<TraceBuffer>> buffers_; 603 std::map<std::string, int64_t> session_to_last_trace_s_; 604 605 bool smb_scraping_enabled_ = false; 606 bool lockdown_mode_ = false; 607 uint32_t min_write_period_ms_ = 100; // Overridable for testing. 608 609 uint8_t sync_marker_packet_[32]; // Lazily initialized. 610 size_t sync_marker_packet_size_ = 0; 611 612 // Stats. 613 uint64_t chunks_discarded_ = 0; 614 uint64_t patches_discarded_ = 0; 615 616 PERFETTO_THREAD_CHECKER(thread_checker_) 617 618 base::WeakPtrFactory<TracingServiceImpl> 619 weak_ptr_factory_; // Keep at the end. 620 }; 621 622 } // namespace perfetto 623 624 #endif // SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_ 625