blob: 63beaa754f3b8f7450e360cc34dc905f9659186a [file] [log] [blame]
Primiano Tucci4f9b6d72017-12-05 20:59:16 +00001/*
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
Florian Mayer6a1a4d52018-06-08 16:47:07 +010017#ifndef SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_
18#define SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000019
20#include <functional>
21#include <map>
22#include <memory>
Primiano Tucci14219ff2019-02-27 12:41:05 +010023#include <mutex>
Primiano Tucci42e2de12017-12-07 16:46:04 +000024#include <set>
Stephen Nusko59847292019-03-22 13:54:08 +000025#include <vector>
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000026
Eric Seckler57c89d92018-10-26 15:11:55 +010027#include "perfetto/base/gtest_prod_util.h"
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010028#include "perfetto/base/logging.h"
Eric Secklerf3f524b2018-12-13 09:09:34 +000029#include "perfetto/base/optional.h"
Sami Kyostilafbccb3c2018-03-21 14:00:47 +000030#include "perfetto/base/time.h"
Primiano Tucci42e2de12017-12-07 16:46:04 +000031#include "perfetto/base/weak_ptr.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000032#include "perfetto/tracing/core/basic_types.h"
Primiano Tucciecf9e4a2018-03-14 14:51:58 +000033#include "perfetto/tracing/core/commit_data_request.h"
Primiano Tucci53589332017-12-19 11:31:13 +010034#include "perfetto/tracing/core/data_source_descriptor.h"
Eric Seckler7b0c9452019-03-18 13:14:36 +000035#include "perfetto/tracing/core/observable_events.h"
Primiano Tucci53589332017-12-19 11:31:13 +010036#include "perfetto/tracing/core/shared_memory_abi.h"
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -080037#include "perfetto/tracing/core/trace_config.h"
Eric Secklereaf29ed2019-01-23 09:53:55 +000038#include "perfetto/tracing/core/trace_stats.h"
Florian Mayer6a1a4d52018-06-08 16:47:07 +010039#include "perfetto/tracing/core/tracing_service.h"
Primiano Tucci53589332017-12-19 11:31:13 +010040#include "src/tracing/core/id_allocator.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000041
42namespace perfetto {
43
44namespace base {
45class TaskRunner;
46} // namespace base
47
Primiano Tucci42e2de12017-12-07 16:46:04 +000048class Consumer;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000049class DataSourceConfig;
50class Producer;
51class SharedMemory;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010052class SharedMemoryArbiterImpl;
Hector Dearman6214c8f2018-03-27 16:16:22 +010053class TraceBuffer;
Primiano Tucci42e2de12017-12-07 16:46:04 +000054class TraceConfig;
Sami Kyostilafbccb3c2018-03-21 14:00:47 +000055class TracePacket;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000056
57// The tracing service business logic.
Florian Mayer6a1a4d52018-06-08 16:47:07 +010058class TracingServiceImpl : public TracingService {
Eric Seckler4ff03e52019-03-15 10:10:30 +000059 private:
60 struct DataSourceInstance;
61
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000062 public:
Primiano Tucci1a1951d2018-04-04 21:08:16 +020063 static constexpr size_t kDefaultShmSize = 256 * 1024ul;
Primiano Tuccie7ca7c62018-04-07 08:28:03 +020064 static constexpr size_t kMaxShmSize = 32 * 1024 * 1024ul;
Primiano Tuccibaeecf12018-07-25 12:02:20 +010065 static constexpr uint32_t kDataSourceStopTimeoutMs = 5000;
Primiano Tucci9754d0d2018-09-15 12:41:46 +010066 static constexpr uint8_t kSyncMarker[] = {0x82, 0x47, 0x7a, 0x76, 0xb2, 0x8d,
67 0x42, 0xba, 0x81, 0xdc, 0x33, 0x32,
68 0x6d, 0x57, 0xa0, 0x79};
Primiano Tucci1a1951d2018-04-04 21:08:16 +020069
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000070 // The implementation behind the service endpoint exposed to each producer.
Florian Mayer6a1a4d52018-06-08 16:47:07 +010071 class ProducerEndpointImpl : public TracingService::ProducerEndpoint {
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000072 public:
73 ProducerEndpointImpl(ProducerID,
Sami Kyostila32e0b542018-02-14 08:55:43 +000074 uid_t uid,
Florian Mayer6a1a4d52018-06-08 16:47:07 +010075 TracingServiceImpl*,
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000076 base::TaskRunner*,
Isabelle Taylor86262cb2018-03-27 16:00:54 +010077 Producer*,
78 const std::string& producer_name);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000079 ~ProducerEndpointImpl() override;
80
Florian Mayer6a1a4d52018-06-08 16:47:07 +010081 // TracingService::ProducerEndpoint implementation.
Primiano Tucci9daa4832018-03-28 23:28:17 +010082 void RegisterDataSource(const DataSourceDescriptor&) override;
83 void UnregisterDataSource(const std::string& name) override;
Eric Seckler1c4e1ac2018-11-29 10:23:14 +000084 void RegisterTraceWriter(uint32_t writer_id,
85 uint32_t target_buffer) override;
86 void UnregisterTraceWriter(uint32_t writer_id) override;
Primiano Tucci3e69ed92018-03-14 14:52:29 +000087 void CommitData(const CommitDataRequest&, CommitDataCallback) override;
Isabelle Taylor69faa902018-03-21 15:42:03 +000088 void SetSharedMemory(std::unique_ptr<SharedMemory>);
Primiano Tucciaf429f92017-12-19 01:51:50 +010089 std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override;
Primiano Tuccid52e6272018-04-06 19:06:53 +020090 void NotifyFlushComplete(FlushRequestID) override;
Eric Seckler4ff03e52019-03-15 10:10:30 +000091 void NotifyDataSourceStarted(DataSourceInstanceID) override;
Primiano Tuccibaeecf12018-07-25 12:02:20 +010092 void NotifyDataSourceStopped(DataSourceInstanceID) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000093 SharedMemory* shared_memory() const override;
Isabelle Taylor69faa902018-03-21 15:42:03 +000094 size_t shared_buffer_page_size_kb() const override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000095
Primiano Tuccibaeecf12018-07-25 12:02:20 +010096 void OnTracingSetup();
Primiano Tucci674076d2018-10-01 10:41:09 +010097 void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&);
Primiano Tucciafb72b52018-09-25 09:37:24 +010098 void StartDataSource(DataSourceInstanceID, const DataSourceConfig&);
Primiano Tucci674076d2018-10-01 10:41:09 +010099 void StopDataSource(DataSourceInstanceID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100100 void Flush(FlushRequestID, const std::vector<DataSourceInstanceID>&);
Eric Seckler6dc23592018-11-30 10:59:06 +0000101 void OnFreeBuffers(const std::vector<BufferID>& target_buffers);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100102
Eric Secklerdd0ad102018-12-06 11:32:04 +0000103 bool is_allowed_target_buffer(BufferID buffer_id) const {
104 return allowed_target_buffers_.count(buffer_id);
105 }
106
Eric Secklerf3f524b2018-12-13 09:09:34 +0000107 base::Optional<BufferID> buffer_id_for_writer(WriterID writer_id) const {
108 const auto it = writers_.find(writer_id);
109 if (it != writers_.end())
110 return it->second;
111 return base::nullopt;
112 }
113
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000114 private:
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100115 friend class TracingServiceImpl;
116 friend class TracingServiceImplTest;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000117 ProducerEndpointImpl(const ProducerEndpointImpl&) = delete;
118 ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete;
Primiano Tuccidca727d2018-04-04 11:31:55 +0200119 SharedMemoryArbiterImpl* GetOrCreateShmemArbiter();
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000120
121 ProducerID const id_;
Sami Kyostila32e0b542018-02-14 08:55:43 +0000122 const uid_t uid_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100123 TracingServiceImpl* const service_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000124 base::TaskRunner* const task_runner_;
125 Producer* producer_;
126 std::unique_ptr<SharedMemory> shared_memory_;
Isabelle Taylor69faa902018-03-21 15:42:03 +0000127 size_t shared_buffer_page_size_kb_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100128 SharedMemoryABI shmem_abi_;
Primiano Tucci1a1951d2018-04-04 21:08:16 +0200129 size_t shmem_size_hint_bytes_ = 0;
Isabelle Taylor86262cb2018-03-27 16:00:54 +0100130 const std::string name_;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100131
Eric Seckler6dc23592018-11-30 10:59:06 +0000132 // Set of the global target_buffer IDs that the producer is configured to
133 // write into in any active tracing session.
134 std::set<BufferID> allowed_target_buffers_;
135
Eric Secklerf3f524b2018-12-13 09:09:34 +0000136 // Maps registered TraceWriter IDs to their target buffers as registered by
137 // the producer. Note that producers aren't required to register their
138 // writers, so we may see commits of chunks with WriterIDs that aren't
139 // contained in this map. However, if a producer does register a writer, the
140 // service will prevent the writer from writing into any other buffer than
141 // the one associated with it here. The BufferIDs stored in this map are
142 // untrusted, so need to be verified against |allowed_target_buffers_|
143 // before use.
144 std::map<WriterID, BufferID> writers_;
145
Primiano Tucci14219ff2019-02-27 12:41:05 +0100146 // This is used only in in-process configurations. The mutex protects
147 // concurrent construction of |inproc_shmem_arbiter_|.
148 // SharedMemoryArbiterImpl methods themselves are thread-safe.
149 std::mutex inproc_shmem_arbiter_mutex_;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100150 std::unique_ptr<SharedMemoryArbiterImpl> inproc_shmem_arbiter_;
Primiano Tucci14219ff2019-02-27 12:41:05 +0100151
Florian Mayercd08ec62018-01-31 17:49:25 +0000152 PERFETTO_THREAD_CHECKER(thread_checker_)
Primiano Tuccidca727d2018-04-04 11:31:55 +0200153 base::WeakPtrFactory<ProducerEndpointImpl> weak_ptr_factory_; // Keep last.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000154 };
155
Primiano Tucci42e2de12017-12-07 16:46:04 +0000156 // The implementation behind the service endpoint exposed to each consumer.
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100157 class ConsumerEndpointImpl : public TracingService::ConsumerEndpoint {
Primiano Tucci42e2de12017-12-07 16:46:04 +0000158 public:
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100159 ConsumerEndpointImpl(TracingServiceImpl*,
160 base::TaskRunner*,
161 Consumer*,
162 uid_t uid);
Primiano Tucci42e2de12017-12-07 16:46:04 +0000163 ~ConsumerEndpointImpl() override;
164
Primiano Tuccidca727d2018-04-04 11:31:55 +0200165 void NotifyOnTracingDisabled();
Primiano Tucci42e2de12017-12-07 16:46:04 +0000166 base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr();
167
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100168 // TracingService::ConsumerEndpoint implementation.
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100169 void EnableTracing(const TraceConfig&, base::ScopedFile) override;
Oystein Eftevaagcb6e4c82019-03-06 15:38:26 -0800170 void ChangeTraceConfig(const TraceConfig& cfg) override;
Primiano Tucci674076d2018-10-01 10:41:09 +0100171 void StartTracing() override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000172 void DisableTracing() override;
173 void ReadBuffers() override;
174 void FreeBuffers() override;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100175 void Flush(uint32_t timeout_ms, FlushCallback) override;
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100176 void Detach(const std::string& key) override;
177 void Attach(const std::string& key) override;
Eric Secklereaf29ed2019-01-23 09:53:55 +0000178 void GetTraceStats() override;
Eric Seckler7b0c9452019-03-18 13:14:36 +0000179 void ObserveEvents(uint32_t enabled_event_types) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000180
Eric Seckler7b0c9452019-03-18 13:14:36 +0000181 // If |observe_data_source_instances == true|, will queue a task to notify
182 // the consumer about the state change.
Eric Seckler4ff03e52019-03-15 10:10:30 +0000183 void OnDataSourceInstanceStateChange(const ProducerEndpointImpl&,
Eric Seckler7b0c9452019-03-18 13:14:36 +0000184 const DataSourceInstance&);
Eric Seckler4ff03e52019-03-15 10:10:30 +0000185
Primiano Tucci42e2de12017-12-07 16:46:04 +0000186 private:
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100187 friend class TracingServiceImpl;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000188 ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete;
189 ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete;
190
Eric Seckler7b0c9452019-03-18 13:14:36 +0000191 // Returns a pointer to an ObservableEvents object that the caller can fill
192 // and schedules a task to send the ObservableEvents to the consumer.
193 ObservableEvents* AddObservableEvents();
194
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100195 base::TaskRunner* const task_runner_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100196 TracingServiceImpl* const service_;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000197 Consumer* const consumer_;
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100198 uid_t const uid_;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000199 TracingSessionID tracing_session_id_ = 0;
Eric Seckler7b0c9452019-03-18 13:14:36 +0000200
201 // Whether the consumer is interested in DataSourceInstance state change
202 // events.
203 uint32_t enabled_observable_event_types_ = ObservableEventType::kNone;
204 // ObservableEvents that will be sent to the consumer. If set, a task to
205 // flush the events to the consumer has been queued.
206 std::unique_ptr<ObservableEvents> observable_events_;
207
Florian Mayercd08ec62018-01-31 17:49:25 +0000208 PERFETTO_THREAD_CHECKER(thread_checker_)
Primiano Tuccidca727d2018-04-04 11:31:55 +0200209 base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_; // Keep last.
Primiano Tucci42e2de12017-12-07 16:46:04 +0000210 };
211
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100212 explicit TracingServiceImpl(std::unique_ptr<SharedMemory::Factory>,
213 base::TaskRunner*);
214 ~TracingServiceImpl() override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000215
Primiano Tucci42e2de12017-12-07 16:46:04 +0000216 // Called by ProducerEndpointImpl.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000217 void DisconnectProducer(ProducerID);
Primiano Tucci9daa4832018-03-28 23:28:17 +0100218 void RegisterDataSource(ProducerID, const DataSourceDescriptor&);
219 void UnregisterDataSource(ProducerID, const std::string& name);
Primiano Tucci53589332017-12-19 11:31:13 +0100220 void CopyProducerPageIntoLogBuffer(ProducerID,
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000221 uid_t,
222 WriterID,
223 ChunkID,
Primiano Tucci53589332017-12-19 11:31:13 +0100224 BufferID,
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000225 uint16_t num_fragments,
226 uint8_t chunk_flags,
Eric Secklerb77b27e2018-12-17 11:42:52 +0000227 bool chunk_complete,
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000228 const uint8_t* src,
229 size_t size);
230 void ApplyChunkPatches(ProducerID,
231 const std::vector<CommitDataRequest::ChunkToPatch>&);
Primiano Tuccid52e6272018-04-06 19:06:53 +0200232 void NotifyFlushDoneForProducer(ProducerID, FlushRequestID);
Eric Seckler4ff03e52019-03-15 10:10:30 +0000233 void NotifyDataSourceStarted(ProducerID, const DataSourceInstanceID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100234 void NotifyDataSourceStopped(ProducerID, const DataSourceInstanceID);
Stephen Nusko59847292019-03-22 13:54:08 +0000235 void ActivateTriggers(ProducerID, const std::vector<std::string>& triggers);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000236
Primiano Tucci42e2de12017-12-07 16:46:04 +0000237 // Called by ConsumerEndpointImpl.
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100238 bool DetachConsumer(ConsumerEndpointImpl*, const std::string& key);
239 bool AttachConsumer(ConsumerEndpointImpl*, const std::string& key);
Primiano Tucci42e2de12017-12-07 16:46:04 +0000240 void DisconnectConsumer(ConsumerEndpointImpl*);
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100241 bool EnableTracing(ConsumerEndpointImpl*,
242 const TraceConfig&,
243 base::ScopedFile);
Oystein Eftevaagcb6e4c82019-03-06 15:38:26 -0800244 void ChangeTraceConfig(ConsumerEndpointImpl*, const TraceConfig&);
245
Primiano Tucci674076d2018-10-01 10:41:09 +0100246 bool StartTracing(TracingSessionID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100247 void DisableTracing(TracingSessionID, bool disable_immediately = false);
Primiano Tuccid52e6272018-04-06 19:06:53 +0200248 void Flush(TracingSessionID tsid,
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100249 uint32_t timeout_ms,
Primiano Tuccid52e6272018-04-06 19:06:53 +0200250 ConsumerEndpoint::FlushCallback);
251 void FlushAndDisableTracing(TracingSessionID);
Primiano Tucci20d441d2018-01-16 09:25:51 +0000252 void ReadBuffers(TracingSessionID, ConsumerEndpointImpl*);
253 void FreeBuffers(TracingSessionID);
Primiano Tucci42e2de12017-12-07 16:46:04 +0000254
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000255 // Service implementation.
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100256 std::unique_ptr<TracingService::ProducerEndpoint> ConnectProducer(
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000257 Producer*,
Sami Kyostila32e0b542018-02-14 08:55:43 +0000258 uid_t uid,
Isabelle Taylor86262cb2018-03-27 16:00:54 +0100259 const std::string& producer_name,
Isabelle Taylor69faa902018-03-21 15:42:03 +0000260 size_t shared_memory_size_hint_bytes = 0) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000261
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100262 std::unique_ptr<TracingService::ConsumerEndpoint> ConnectConsumer(
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100263 Consumer*,
264 uid_t) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000265
Eric Secklera01e28a2019-01-08 11:21:04 +0000266 void SetSMBScrapingEnabled(bool enabled) override {
267 smb_scraping_enabled_ = enabled;
268 }
269
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000270 // Exposed mainly for testing.
271 size_t num_producers() const { return producers_.size(); }
272 ProducerEndpointImpl* GetProducer(ProducerID) const;
273
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100274 uint32_t override_data_source_test_timeout_ms_for_testing = 0;
275
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000276 private:
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100277 friend class TracingServiceImplTest;
Primiano Tucci081d46a2018-02-28 11:09:43 +0000278
Primiano Tucci53589332017-12-19 11:31:13 +0100279 struct RegisteredDataSource {
280 ProducerID producer_id;
Primiano Tucci53589332017-12-19 11:31:13 +0100281 DataSourceDescriptor descriptor;
282 };
283
Sami Kyostila06487a22018-02-27 13:48:38 +0000284 // Represents an active data source for a tracing session.
285 struct DataSourceInstance {
Primiano Tucci5403e4f2018-11-27 10:07:03 +0000286 DataSourceInstance(DataSourceInstanceID id,
287 const DataSourceConfig& cfg,
288 const std::string& ds_name,
Eric Seckler4ff03e52019-03-15 10:10:30 +0000289 bool notify_on_start,
290 bool notify_on_stop)
Primiano Tucci5403e4f2018-11-27 10:07:03 +0000291 : instance_id(id),
292 config(cfg),
293 data_source_name(ds_name),
Eric Seckler4ff03e52019-03-15 10:10:30 +0000294 will_notify_on_start(notify_on_start),
295 will_notify_on_stop(notify_on_stop) {}
Primiano Tucci674076d2018-10-01 10:41:09 +0100296 DataSourceInstance(const DataSourceInstance&) = delete;
297 DataSourceInstance& operator=(const DataSourceInstance&) = delete;
Primiano Tucci674076d2018-10-01 10:41:09 +0100298
Sami Kyostila06487a22018-02-27 13:48:38 +0000299 DataSourceInstanceID instance_id;
Primiano Tucci674076d2018-10-01 10:41:09 +0100300 DataSourceConfig config;
Primiano Tucci9daa4832018-03-28 23:28:17 +0100301 std::string data_source_name;
Eric Seckler4ff03e52019-03-15 10:10:30 +0000302 bool will_notify_on_start;
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100303 bool will_notify_on_stop;
Eric Seckler4ff03e52019-03-15 10:10:30 +0000304
305 enum DataSourceInstanceState {
306 CONFIGURED,
307 STARTING,
308 STARTED,
309 STOPPING,
310 STOPPED
311 };
312 DataSourceInstanceState state = CONFIGURED;
Sami Kyostila06487a22018-02-27 13:48:38 +0000313 };
314
Primiano Tuccid52e6272018-04-06 19:06:53 +0200315 struct PendingFlush {
316 std::set<ProducerID> producers;
317 ConsumerEndpoint::FlushCallback callback;
318 explicit PendingFlush(decltype(callback) cb) : callback(std::move(cb)) {}
319 };
320
Primiano Tucci53589332017-12-19 11:31:13 +0100321 // Holds the state of a tracing session. A tracing session is uniquely bound
322 // a specific Consumer. Each Consumer can own one or more sessions.
323 struct TracingSession {
Primiano Tucci674076d2018-10-01 10:41:09 +0100324 enum State {
325 DISABLED = 0,
326 CONFIGURED,
327 STARTED,
328 DISABLING_WAITING_STOP_ACKS
329 };
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100330
331 TracingSession(TracingSessionID, ConsumerEndpointImpl*, const TraceConfig&);
Primiano Tucci20d441d2018-01-16 09:25:51 +0000332
333 size_t num_buffers() const { return buffers_index.size(); }
334
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100335 uint32_t delay_to_next_write_period_ms() const {
Sami Kyostila01c45f02018-03-29 15:43:10 +0100336 PERFETTO_DCHECK(write_period_ms > 0);
337 return write_period_ms -
338 (base::GetWallTimeMs().count() % write_period_ms);
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100339 }
340
Florian Mayere563f662019-01-09 11:04:50 +0000341 uint32_t flush_timeout_ms() {
342 uint32_t timeout_ms = config.flush_timeout_ms();
343 return timeout_ms ? timeout_ms : kDefaultFlushTimeoutMs;
344 }
345
Eric Secklerd0ac7ca2019-02-06 09:13:45 +0000346 PacketSequenceID GetPacketSequenceID(ProducerID producer_id,
347 WriterID writer_id) {
348 auto key = std::make_pair(producer_id, writer_id);
349 auto it = packet_sequence_ids.find(key);
350 if (it != packet_sequence_ids.end())
351 return it->second;
352 // We shouldn't run out of sequence IDs (producer ID is 16 bit, writer IDs
353 // are limited to 1024).
354 static_assert(kMaxPacketSequenceID > kMaxProducerID * kMaxWriterID,
355 "PacketSequenceID value space doesn't cover service "
356 "sequence ID and all producer/writer ID combinations!");
357 PERFETTO_DCHECK(last_packet_sequence_id < kMaxPacketSequenceID);
358 PacketSequenceID sequence_id = ++last_packet_sequence_id;
359 packet_sequence_ids[key] = sequence_id;
360 return sequence_id;
361 }
362
Eric Seckler4ff03e52019-03-15 10:10:30 +0000363 DataSourceInstance* GetDataSourceInstance(
364 ProducerID producer_id,
365 DataSourceInstanceID instance_id) {
366 for (auto& inst_kv : data_source_instances) {
367 if (inst_kv.first != producer_id ||
368 inst_kv.second.instance_id != instance_id) {
369 continue;
370 }
371 return &inst_kv.second;
372 }
373 return nullptr;
374 }
375
376 bool AllDataSourceInstancesStopped() {
377 for (const auto& inst_kv : data_source_instances) {
378 if (inst_kv.second.state != DataSourceInstance::STOPPED)
379 return false;
380 }
381 return true;
382 }
383
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100384 const TracingSessionID id;
385
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100386 // The consumer that started the session.
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100387 // Can be nullptr if the consumer detached from the session.
388 ConsumerEndpointImpl* consumer_maybe_null;
389
390 // Unix uid of the consumer. This is valid even after the consumer detaches
391 // and does not change for the entire duration of the session. It is used to
392 // prevent that a consumer re-attaches to a session from a different uid.
393 uid_t const consumer_uid;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100394
Stephen Nusko59847292019-03-22 13:54:08 +0000395 // The list of triggers this session received while alive and the time they
396 // were received at. This is used to insert 'fake' packets back to the
397 // consumer so they can tell when some event happened. The order matches the
398 // order they were received.
399 std::vector<std::pair<uint64_t, TraceConfig::TriggerConfig::Trigger>>
400 received_triggers;
401
Oystein Eftevaagcb6e4c82019-03-06 15:38:26 -0800402 // The trace config provided by the Consumer when calling
403 // EnableTracing(), plus any updates performed by ChangeTraceConfig.
404 TraceConfig config;
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800405
Primiano Tucci53589332017-12-19 11:31:13 +0100406 // List of data source instances that have been enabled on the various
407 // producers for this tracing session.
Sami Kyostila06487a22018-02-27 13:48:38 +0000408 std::multimap<ProducerID, DataSourceInstance> data_source_instances;
Primiano Tucci53589332017-12-19 11:31:13 +0100409
Primiano Tuccid52e6272018-04-06 19:06:53 +0200410 // For each Flush(N) request, keeps track of the set of producers for which
411 // we are still awaiting a NotifyFlushComplete(N) ack.
412 std::map<FlushRequestID, PendingFlush> pending_flushes;
413
Primiano Tucci20d441d2018-01-16 09:25:51 +0000414 // Maps a per-trace-session buffer index into the corresponding global
415 // BufferID (shared namespace amongst all consumers). This vector has as
416 // many entries as |config.buffers_size()|.
417 std::vector<BufferID> buffers_index;
Sami Kyostilafbccb3c2018-03-21 14:00:47 +0000418
Eric Secklerd0ac7ca2019-02-06 09:13:45 +0000419 std::map<std::pair<ProducerID, WriterID>, PacketSequenceID>
420 packet_sequence_ids;
421 PacketSequenceID last_packet_sequence_id = kServicePacketSequenceID;
422
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100423 // When the last snapshots (clock, stats, sync marker) were emitted into
424 // the output stream.
425 base::TimeMillis last_snapshot_time = {};
Primiano Tucci5e33cad2018-04-30 14:41:25 +0100426
Sami Kyostila200bd2e2018-03-26 12:24:10 +0100427 // Whether we mirrored the trace config back to the trace output yet.
428 bool did_emit_config = false;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100429
Hector Dearman685f7522019-03-12 14:28:56 +0000430 // Whether we put the system info into the trace output yet.
431 bool did_emit_system_info = false;
432
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100433 State state = DISABLED;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100434
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100435 // If the consumer detached the session, this variable defines the key used
436 // for identifying the session later when reattaching.
437 std::string detach_key;
438
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100439 // This is set when the Consumer calls sets |write_into_file| == true in the
440 // TraceConfig. In this case this represents the file we should stream the
441 // trace packets into, rather than returning it to the consumer via
442 // OnTraceData().
443 base::ScopedFile write_into_file;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100444 uint32_t write_period_ms = 0;
445 uint64_t max_file_size_bytes = 0;
446 uint64_t bytes_written_into_file = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100447 };
448
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100449 TracingServiceImpl(const TracingServiceImpl&) = delete;
450 TracingServiceImpl& operator=(const TracingServiceImpl&) = delete;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000451
Primiano Tucci674076d2018-10-01 10:41:09 +0100452 DataSourceInstance* SetupDataSource(const TraceConfig::DataSource&,
453 const TraceConfig::ProducerConfig&,
454 const RegisteredDataSource&,
455 TracingSession*);
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800456
Primiano Tucci081d46a2018-02-28 11:09:43 +0000457 // Returns the next available ProducerID that is not in |producers_|.
458 ProducerID GetNextProducerID();
459
Primiano Tucci20d441d2018-01-16 09:25:51 +0000460 // Returns a pointer to the |tracing_sessions_| entry or nullptr if the
461 // session doesn't exists.
462 TracingSession* GetTracingSession(TracingSessionID);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000463
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100464 // Returns a pointer to the |tracing_sessions_| entry, matching the given
465 // uid and detach key, or nullptr if no such session exists.
466 TracingSession* GetDetachedSession(uid_t, const std::string& key);
467
Lalit Maganti485faff2018-03-06 11:51:35 +0000468 // Update the memory guard rail by using the latest information from the
469 // shared memory and trace buffers.
470 void UpdateMemoryGuardrail();
471
Eric Seckler4ff03e52019-03-15 10:10:30 +0000472 void StartDataSourceInstance(ProducerEndpointImpl* producer,
473 TracingSession* tracing_session,
474 DataSourceInstance* instance);
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100475 void SnapshotSyncMarker(std::vector<TracePacket>*);
476 void SnapshotClocks(std::vector<TracePacket>*);
477 void SnapshotStats(TracingSession*, std::vector<TracePacket>*);
Eric Secklereaf29ed2019-01-23 09:53:55 +0000478 TraceStats GetTraceStats(TracingSession* tracing_session);
Sami Kyostila200bd2e2018-03-26 12:24:10 +0100479 void MaybeEmitTraceConfig(TracingSession*, std::vector<TracePacket>*);
Hector Dearman685f7522019-03-12 14:28:56 +0000480 void MaybeEmitSystemInfo(TracingSession*, std::vector<TracePacket>*);
Primiano Tuccid52e6272018-04-06 19:06:53 +0200481 void OnFlushTimeout(TracingSessionID, FlushRequestID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100482 void OnDisableTracingTimeout(TracingSessionID);
483 void DisableTracingNotifyConsumerAndFlushFile(TracingSession*);
Primiano Tuccicaa57802018-11-25 11:07:07 +0000484 void PeriodicFlushTask(TracingSessionID, bool post_next_only);
Eric Secklera01e28a2019-01-08 11:21:04 +0000485 void CompleteFlush(TracingSessionID tsid,
486 ConsumerEndpoint::FlushCallback callback,
487 bool success);
488 void ScrapeSharedMemoryBuffers(TracingSession* tracing_session,
489 ProducerEndpointImpl* producer);
Hector Dearman6214c8f2018-03-27 16:16:22 +0100490 TraceBuffer* GetBufferByID(BufferID);
Stephen Nusko59847292019-03-22 13:54:08 +0000491 void OnStartTriggersTimeout(TracingSessionID tsid);
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000492
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000493 base::TaskRunner* const task_runner_;
Primiano Tucci53589332017-12-19 11:31:13 +0100494 std::unique_ptr<SharedMemory::Factory> shm_factory_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000495 ProducerID last_producer_id_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100496 DataSourceInstanceID last_data_source_instance_id_ = 0;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000497 TracingSessionID last_tracing_session_id_ = 0;
Primiano Tuccid52e6272018-04-06 19:06:53 +0200498 FlushRequestID last_flush_request_id_ = 0;
Primiano Tucci5e33cad2018-04-30 14:41:25 +0100499 uid_t uid_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100500
501 // Buffer IDs are global across all consumers (because a Producer can produce
502 // data for more than one trace session, hence more than one consumer).
Primiano Tucci20d441d2018-01-16 09:25:51 +0000503 IdAllocator<BufferID> buffer_ids_;
Primiano Tucci53589332017-12-19 11:31:13 +0100504
505 std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000506 std::map<ProducerID, ProducerEndpointImpl*> producers_;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000507 std::set<ConsumerEndpointImpl*> consumers_;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000508 std::map<TracingSessionID, TracingSession> tracing_sessions_;
Hector Dearman6214c8f2018-03-27 16:16:22 +0100509 std::map<BufferID, std::unique_ptr<TraceBuffer>> buffers_;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000510
Eric Secklera01e28a2019-01-08 11:21:04 +0000511 bool smb_scraping_enabled_ = false;
Florian Mayer61c55482018-03-06 14:43:54 +0000512 bool lockdown_mode_ = false;
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100513 uint32_t min_write_period_ms_ = 100; // Overridable for testing.
514
515 uint8_t sync_marker_packet_[32]; // Lazily initialized.
516 size_t sync_marker_packet_size_ = 0;
Florian Mayer61c55482018-03-06 14:43:54 +0000517
Eric Seckler2c72bd82019-02-08 15:01:34 +0000518 // Stats.
519 uint64_t chunks_discarded_ = 0;
520 uint64_t patches_discarded_ = 0;
521
Florian Mayercd08ec62018-01-31 17:49:25 +0000522 PERFETTO_THREAD_CHECKER(thread_checker_)
523
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100524 base::WeakPtrFactory<TracingServiceImpl>
525 weak_ptr_factory_; // Keep at the end.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000526};
527
528} // namespace perfetto
529
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100530#endif // SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_