blob: d8fb2afacb5f593086211c6d284aeaed3eb596ac [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;
Stephen Nusko1393ffd2019-03-22 13:54:58 +000095 void ActivateTriggers(const std::vector<std::string>&) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000096
Primiano Tuccibaeecf12018-07-25 12:02:20 +010097 void OnTracingSetup();
Primiano Tucci674076d2018-10-01 10:41:09 +010098 void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&);
Primiano Tucciafb72b52018-09-25 09:37:24 +010099 void StartDataSource(DataSourceInstanceID, const DataSourceConfig&);
Primiano Tucci674076d2018-10-01 10:41:09 +0100100 void StopDataSource(DataSourceInstanceID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100101 void Flush(FlushRequestID, const std::vector<DataSourceInstanceID>&);
Eric Seckler6dc23592018-11-30 10:59:06 +0000102 void OnFreeBuffers(const std::vector<BufferID>& target_buffers);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100103
Eric Secklerdd0ad102018-12-06 11:32:04 +0000104 bool is_allowed_target_buffer(BufferID buffer_id) const {
105 return allowed_target_buffers_.count(buffer_id);
106 }
107
Eric Secklerf3f524b2018-12-13 09:09:34 +0000108 base::Optional<BufferID> buffer_id_for_writer(WriterID writer_id) const {
109 const auto it = writers_.find(writer_id);
110 if (it != writers_.end())
111 return it->second;
112 return base::nullopt;
113 }
114
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000115 private:
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100116 friend class TracingServiceImpl;
117 friend class TracingServiceImplTest;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000118 ProducerEndpointImpl(const ProducerEndpointImpl&) = delete;
119 ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete;
Primiano Tuccidca727d2018-04-04 11:31:55 +0200120 SharedMemoryArbiterImpl* GetOrCreateShmemArbiter();
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000121
122 ProducerID const id_;
Sami Kyostila32e0b542018-02-14 08:55:43 +0000123 const uid_t uid_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100124 TracingServiceImpl* const service_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000125 base::TaskRunner* const task_runner_;
126 Producer* producer_;
127 std::unique_ptr<SharedMemory> shared_memory_;
Isabelle Taylor69faa902018-03-21 15:42:03 +0000128 size_t shared_buffer_page_size_kb_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100129 SharedMemoryABI shmem_abi_;
Primiano Tucci1a1951d2018-04-04 21:08:16 +0200130 size_t shmem_size_hint_bytes_ = 0;
Isabelle Taylor86262cb2018-03-27 16:00:54 +0100131 const std::string name_;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100132
Eric Seckler6dc23592018-11-30 10:59:06 +0000133 // Set of the global target_buffer IDs that the producer is configured to
134 // write into in any active tracing session.
135 std::set<BufferID> allowed_target_buffers_;
136
Eric Secklerf3f524b2018-12-13 09:09:34 +0000137 // Maps registered TraceWriter IDs to their target buffers as registered by
138 // the producer. Note that producers aren't required to register their
139 // writers, so we may see commits of chunks with WriterIDs that aren't
140 // contained in this map. However, if a producer does register a writer, the
141 // service will prevent the writer from writing into any other buffer than
142 // the one associated with it here. The BufferIDs stored in this map are
143 // untrusted, so need to be verified against |allowed_target_buffers_|
144 // before use.
145 std::map<WriterID, BufferID> writers_;
146
Primiano Tucci14219ff2019-02-27 12:41:05 +0100147 // This is used only in in-process configurations. The mutex protects
148 // concurrent construction of |inproc_shmem_arbiter_|.
149 // SharedMemoryArbiterImpl methods themselves are thread-safe.
150 std::mutex inproc_shmem_arbiter_mutex_;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100151 std::unique_ptr<SharedMemoryArbiterImpl> inproc_shmem_arbiter_;
Primiano Tucci14219ff2019-02-27 12:41:05 +0100152
Florian Mayercd08ec62018-01-31 17:49:25 +0000153 PERFETTO_THREAD_CHECKER(thread_checker_)
Primiano Tuccidca727d2018-04-04 11:31:55 +0200154 base::WeakPtrFactory<ProducerEndpointImpl> weak_ptr_factory_; // Keep last.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000155 };
156
Primiano Tucci42e2de12017-12-07 16:46:04 +0000157 // The implementation behind the service endpoint exposed to each consumer.
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100158 class ConsumerEndpointImpl : public TracingService::ConsumerEndpoint {
Primiano Tucci42e2de12017-12-07 16:46:04 +0000159 public:
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100160 ConsumerEndpointImpl(TracingServiceImpl*,
161 base::TaskRunner*,
162 Consumer*,
163 uid_t uid);
Primiano Tucci42e2de12017-12-07 16:46:04 +0000164 ~ConsumerEndpointImpl() override;
165
Primiano Tuccidca727d2018-04-04 11:31:55 +0200166 void NotifyOnTracingDisabled();
Primiano Tucci42e2de12017-12-07 16:46:04 +0000167 base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr();
168
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100169 // TracingService::ConsumerEndpoint implementation.
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100170 void EnableTracing(const TraceConfig&, base::ScopedFile) override;
Oystein Eftevaagcb6e4c82019-03-06 15:38:26 -0800171 void ChangeTraceConfig(const TraceConfig& cfg) override;
Primiano Tucci674076d2018-10-01 10:41:09 +0100172 void StartTracing() override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000173 void DisableTracing() override;
174 void ReadBuffers() override;
175 void FreeBuffers() override;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100176 void Flush(uint32_t timeout_ms, FlushCallback) override;
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100177 void Detach(const std::string& key) override;
178 void Attach(const std::string& key) override;
Eric Secklereaf29ed2019-01-23 09:53:55 +0000179 void GetTraceStats() override;
Eric Seckler7b0c9452019-03-18 13:14:36 +0000180 void ObserveEvents(uint32_t enabled_event_types) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000181
Eric Seckler7b0c9452019-03-18 13:14:36 +0000182 // If |observe_data_source_instances == true|, will queue a task to notify
183 // the consumer about the state change.
Eric Seckler4ff03e52019-03-15 10:10:30 +0000184 void OnDataSourceInstanceStateChange(const ProducerEndpointImpl&,
Eric Seckler7b0c9452019-03-18 13:14:36 +0000185 const DataSourceInstance&);
Eric Seckler4ff03e52019-03-15 10:10:30 +0000186
Primiano Tucci42e2de12017-12-07 16:46:04 +0000187 private:
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100188 friend class TracingServiceImpl;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000189 ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete;
190 ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete;
191
Eric Seckler7b0c9452019-03-18 13:14:36 +0000192 // Returns a pointer to an ObservableEvents object that the caller can fill
193 // and schedules a task to send the ObservableEvents to the consumer.
194 ObservableEvents* AddObservableEvents();
195
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100196 base::TaskRunner* const task_runner_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100197 TracingServiceImpl* const service_;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000198 Consumer* const consumer_;
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100199 uid_t const uid_;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000200 TracingSessionID tracing_session_id_ = 0;
Eric Seckler7b0c9452019-03-18 13:14:36 +0000201
202 // Whether the consumer is interested in DataSourceInstance state change
203 // events.
204 uint32_t enabled_observable_event_types_ = ObservableEventType::kNone;
205 // ObservableEvents that will be sent to the consumer. If set, a task to
206 // flush the events to the consumer has been queued.
207 std::unique_ptr<ObservableEvents> observable_events_;
208
Florian Mayercd08ec62018-01-31 17:49:25 +0000209 PERFETTO_THREAD_CHECKER(thread_checker_)
Primiano Tuccidca727d2018-04-04 11:31:55 +0200210 base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_; // Keep last.
Primiano Tucci42e2de12017-12-07 16:46:04 +0000211 };
212
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100213 explicit TracingServiceImpl(std::unique_ptr<SharedMemory::Factory>,
214 base::TaskRunner*);
215 ~TracingServiceImpl() override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000216
Primiano Tucci42e2de12017-12-07 16:46:04 +0000217 // Called by ProducerEndpointImpl.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000218 void DisconnectProducer(ProducerID);
Primiano Tucci9daa4832018-03-28 23:28:17 +0100219 void RegisterDataSource(ProducerID, const DataSourceDescriptor&);
220 void UnregisterDataSource(ProducerID, const std::string& name);
Primiano Tucci53589332017-12-19 11:31:13 +0100221 void CopyProducerPageIntoLogBuffer(ProducerID,
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000222 uid_t,
223 WriterID,
224 ChunkID,
Primiano Tucci53589332017-12-19 11:31:13 +0100225 BufferID,
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000226 uint16_t num_fragments,
227 uint8_t chunk_flags,
Eric Secklerb77b27e2018-12-17 11:42:52 +0000228 bool chunk_complete,
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000229 const uint8_t* src,
230 size_t size);
231 void ApplyChunkPatches(ProducerID,
232 const std::vector<CommitDataRequest::ChunkToPatch>&);
Primiano Tuccid52e6272018-04-06 19:06:53 +0200233 void NotifyFlushDoneForProducer(ProducerID, FlushRequestID);
Eric Seckler4ff03e52019-03-15 10:10:30 +0000234 void NotifyDataSourceStarted(ProducerID, const DataSourceInstanceID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100235 void NotifyDataSourceStopped(ProducerID, const DataSourceInstanceID);
Stephen Nusko59847292019-03-22 13:54:08 +0000236 void ActivateTriggers(ProducerID, const std::vector<std::string>& triggers);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000237
Primiano Tucci42e2de12017-12-07 16:46:04 +0000238 // Called by ConsumerEndpointImpl.
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100239 bool DetachConsumer(ConsumerEndpointImpl*, const std::string& key);
240 bool AttachConsumer(ConsumerEndpointImpl*, const std::string& key);
Primiano Tucci42e2de12017-12-07 16:46:04 +0000241 void DisconnectConsumer(ConsumerEndpointImpl*);
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100242 bool EnableTracing(ConsumerEndpointImpl*,
243 const TraceConfig&,
244 base::ScopedFile);
Oystein Eftevaagcb6e4c82019-03-06 15:38:26 -0800245 void ChangeTraceConfig(ConsumerEndpointImpl*, const TraceConfig&);
246
Primiano Tucci674076d2018-10-01 10:41:09 +0100247 bool StartTracing(TracingSessionID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100248 void DisableTracing(TracingSessionID, bool disable_immediately = false);
Primiano Tuccid52e6272018-04-06 19:06:53 +0200249 void Flush(TracingSessionID tsid,
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100250 uint32_t timeout_ms,
Primiano Tuccid52e6272018-04-06 19:06:53 +0200251 ConsumerEndpoint::FlushCallback);
252 void FlushAndDisableTracing(TracingSessionID);
Primiano Tucci20d441d2018-01-16 09:25:51 +0000253 void ReadBuffers(TracingSessionID, ConsumerEndpointImpl*);
254 void FreeBuffers(TracingSessionID);
Primiano Tucci42e2de12017-12-07 16:46:04 +0000255
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000256 // Service implementation.
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100257 std::unique_ptr<TracingService::ProducerEndpoint> ConnectProducer(
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000258 Producer*,
Sami Kyostila32e0b542018-02-14 08:55:43 +0000259 uid_t uid,
Isabelle Taylor86262cb2018-03-27 16:00:54 +0100260 const std::string& producer_name,
Isabelle Taylor69faa902018-03-21 15:42:03 +0000261 size_t shared_memory_size_hint_bytes = 0) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000262
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100263 std::unique_ptr<TracingService::ConsumerEndpoint> ConnectConsumer(
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100264 Consumer*,
265 uid_t) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000266
Eric Secklera01e28a2019-01-08 11:21:04 +0000267 void SetSMBScrapingEnabled(bool enabled) override {
268 smb_scraping_enabled_ = enabled;
269 }
270
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000271 // Exposed mainly for testing.
272 size_t num_producers() const { return producers_.size(); }
273 ProducerEndpointImpl* GetProducer(ProducerID) const;
274
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100275 uint32_t override_data_source_test_timeout_ms_for_testing = 0;
276
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000277 private:
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100278 friend class TracingServiceImplTest;
Primiano Tucci081d46a2018-02-28 11:09:43 +0000279
Primiano Tucci53589332017-12-19 11:31:13 +0100280 struct RegisteredDataSource {
281 ProducerID producer_id;
Primiano Tucci53589332017-12-19 11:31:13 +0100282 DataSourceDescriptor descriptor;
283 };
284
Sami Kyostila06487a22018-02-27 13:48:38 +0000285 // Represents an active data source for a tracing session.
286 struct DataSourceInstance {
Primiano Tucci5403e4f2018-11-27 10:07:03 +0000287 DataSourceInstance(DataSourceInstanceID id,
288 const DataSourceConfig& cfg,
289 const std::string& ds_name,
Eric Seckler4ff03e52019-03-15 10:10:30 +0000290 bool notify_on_start,
291 bool notify_on_stop)
Primiano Tucci5403e4f2018-11-27 10:07:03 +0000292 : instance_id(id),
293 config(cfg),
294 data_source_name(ds_name),
Eric Seckler4ff03e52019-03-15 10:10:30 +0000295 will_notify_on_start(notify_on_start),
296 will_notify_on_stop(notify_on_stop) {}
Primiano Tucci674076d2018-10-01 10:41:09 +0100297 DataSourceInstance(const DataSourceInstance&) = delete;
298 DataSourceInstance& operator=(const DataSourceInstance&) = delete;
Primiano Tucci674076d2018-10-01 10:41:09 +0100299
Sami Kyostila06487a22018-02-27 13:48:38 +0000300 DataSourceInstanceID instance_id;
Primiano Tucci674076d2018-10-01 10:41:09 +0100301 DataSourceConfig config;
Primiano Tucci9daa4832018-03-28 23:28:17 +0100302 std::string data_source_name;
Eric Seckler4ff03e52019-03-15 10:10:30 +0000303 bool will_notify_on_start;
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100304 bool will_notify_on_stop;
Eric Seckler4ff03e52019-03-15 10:10:30 +0000305
306 enum DataSourceInstanceState {
307 CONFIGURED,
308 STARTING,
309 STARTED,
310 STOPPING,
311 STOPPED
312 };
313 DataSourceInstanceState state = CONFIGURED;
Sami Kyostila06487a22018-02-27 13:48:38 +0000314 };
315
Primiano Tuccid52e6272018-04-06 19:06:53 +0200316 struct PendingFlush {
317 std::set<ProducerID> producers;
318 ConsumerEndpoint::FlushCallback callback;
319 explicit PendingFlush(decltype(callback) cb) : callback(std::move(cb)) {}
320 };
321
Primiano Tucci53589332017-12-19 11:31:13 +0100322 // Holds the state of a tracing session. A tracing session is uniquely bound
323 // a specific Consumer. Each Consumer can own one or more sessions.
324 struct TracingSession {
Primiano Tucci674076d2018-10-01 10:41:09 +0100325 enum State {
326 DISABLED = 0,
327 CONFIGURED,
328 STARTED,
329 DISABLING_WAITING_STOP_ACKS
330 };
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100331
332 TracingSession(TracingSessionID, ConsumerEndpointImpl*, const TraceConfig&);
Primiano Tucci20d441d2018-01-16 09:25:51 +0000333
334 size_t num_buffers() const { return buffers_index.size(); }
335
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100336 uint32_t delay_to_next_write_period_ms() const {
Sami Kyostila01c45f02018-03-29 15:43:10 +0100337 PERFETTO_DCHECK(write_period_ms > 0);
338 return write_period_ms -
339 (base::GetWallTimeMs().count() % write_period_ms);
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100340 }
341
Florian Mayere563f662019-01-09 11:04:50 +0000342 uint32_t flush_timeout_ms() {
343 uint32_t timeout_ms = config.flush_timeout_ms();
344 return timeout_ms ? timeout_ms : kDefaultFlushTimeoutMs;
345 }
346
Eric Secklerd0ac7ca2019-02-06 09:13:45 +0000347 PacketSequenceID GetPacketSequenceID(ProducerID producer_id,
348 WriterID writer_id) {
349 auto key = std::make_pair(producer_id, writer_id);
350 auto it = packet_sequence_ids.find(key);
351 if (it != packet_sequence_ids.end())
352 return it->second;
353 // We shouldn't run out of sequence IDs (producer ID is 16 bit, writer IDs
354 // are limited to 1024).
355 static_assert(kMaxPacketSequenceID > kMaxProducerID * kMaxWriterID,
356 "PacketSequenceID value space doesn't cover service "
357 "sequence ID and all producer/writer ID combinations!");
358 PERFETTO_DCHECK(last_packet_sequence_id < kMaxPacketSequenceID);
359 PacketSequenceID sequence_id = ++last_packet_sequence_id;
360 packet_sequence_ids[key] = sequence_id;
361 return sequence_id;
362 }
363
Eric Seckler4ff03e52019-03-15 10:10:30 +0000364 DataSourceInstance* GetDataSourceInstance(
365 ProducerID producer_id,
366 DataSourceInstanceID instance_id) {
367 for (auto& inst_kv : data_source_instances) {
368 if (inst_kv.first != producer_id ||
369 inst_kv.second.instance_id != instance_id) {
370 continue;
371 }
372 return &inst_kv.second;
373 }
374 return nullptr;
375 }
376
377 bool AllDataSourceInstancesStopped() {
378 for (const auto& inst_kv : data_source_instances) {
379 if (inst_kv.second.state != DataSourceInstance::STOPPED)
380 return false;
381 }
382 return true;
383 }
384
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100385 const TracingSessionID id;
386
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100387 // The consumer that started the session.
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100388 // Can be nullptr if the consumer detached from the session.
389 ConsumerEndpointImpl* consumer_maybe_null;
390
391 // Unix uid of the consumer. This is valid even after the consumer detaches
392 // and does not change for the entire duration of the session. It is used to
393 // prevent that a consumer re-attaches to a session from a different uid.
394 uid_t const consumer_uid;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100395
Stephen Nusko59847292019-03-22 13:54:08 +0000396 // The list of triggers this session received while alive and the time they
397 // were received at. This is used to insert 'fake' packets back to the
398 // consumer so they can tell when some event happened. The order matches the
399 // order they were received.
400 std::vector<std::pair<uint64_t, TraceConfig::TriggerConfig::Trigger>>
401 received_triggers;
402
Oystein Eftevaagcb6e4c82019-03-06 15:38:26 -0800403 // The trace config provided by the Consumer when calling
404 // EnableTracing(), plus any updates performed by ChangeTraceConfig.
405 TraceConfig config;
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800406
Primiano Tucci53589332017-12-19 11:31:13 +0100407 // List of data source instances that have been enabled on the various
408 // producers for this tracing session.
Sami Kyostila06487a22018-02-27 13:48:38 +0000409 std::multimap<ProducerID, DataSourceInstance> data_source_instances;
Primiano Tucci53589332017-12-19 11:31:13 +0100410
Primiano Tuccid52e6272018-04-06 19:06:53 +0200411 // For each Flush(N) request, keeps track of the set of producers for which
412 // we are still awaiting a NotifyFlushComplete(N) ack.
413 std::map<FlushRequestID, PendingFlush> pending_flushes;
414
Primiano Tucci20d441d2018-01-16 09:25:51 +0000415 // Maps a per-trace-session buffer index into the corresponding global
416 // BufferID (shared namespace amongst all consumers). This vector has as
417 // many entries as |config.buffers_size()|.
418 std::vector<BufferID> buffers_index;
Sami Kyostilafbccb3c2018-03-21 14:00:47 +0000419
Eric Secklerd0ac7ca2019-02-06 09:13:45 +0000420 std::map<std::pair<ProducerID, WriterID>, PacketSequenceID>
421 packet_sequence_ids;
422 PacketSequenceID last_packet_sequence_id = kServicePacketSequenceID;
423
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100424 // When the last snapshots (clock, stats, sync marker) were emitted into
425 // the output stream.
426 base::TimeMillis last_snapshot_time = {};
Primiano Tucci5e33cad2018-04-30 14:41:25 +0100427
Sami Kyostila200bd2e2018-03-26 12:24:10 +0100428 // Whether we mirrored the trace config back to the trace output yet.
429 bool did_emit_config = false;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100430
Hector Dearman685f7522019-03-12 14:28:56 +0000431 // Whether we put the system info into the trace output yet.
432 bool did_emit_system_info = false;
433
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100434 State state = DISABLED;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100435
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100436 // If the consumer detached the session, this variable defines the key used
437 // for identifying the session later when reattaching.
438 std::string detach_key;
439
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100440 // This is set when the Consumer calls sets |write_into_file| == true in the
441 // TraceConfig. In this case this represents the file we should stream the
442 // trace packets into, rather than returning it to the consumer via
443 // OnTraceData().
444 base::ScopedFile write_into_file;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100445 uint32_t write_period_ms = 0;
446 uint64_t max_file_size_bytes = 0;
447 uint64_t bytes_written_into_file = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100448 };
449
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100450 TracingServiceImpl(const TracingServiceImpl&) = delete;
451 TracingServiceImpl& operator=(const TracingServiceImpl&) = delete;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000452
Primiano Tucci674076d2018-10-01 10:41:09 +0100453 DataSourceInstance* SetupDataSource(const TraceConfig::DataSource&,
454 const TraceConfig::ProducerConfig&,
455 const RegisteredDataSource&,
456 TracingSession*);
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800457
Primiano Tucci081d46a2018-02-28 11:09:43 +0000458 // Returns the next available ProducerID that is not in |producers_|.
459 ProducerID GetNextProducerID();
460
Primiano Tucci20d441d2018-01-16 09:25:51 +0000461 // Returns a pointer to the |tracing_sessions_| entry or nullptr if the
462 // session doesn't exists.
463 TracingSession* GetTracingSession(TracingSessionID);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000464
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100465 // Returns a pointer to the |tracing_sessions_| entry, matching the given
466 // uid and detach key, or nullptr if no such session exists.
467 TracingSession* GetDetachedSession(uid_t, const std::string& key);
468
Lalit Maganti485faff2018-03-06 11:51:35 +0000469 // Update the memory guard rail by using the latest information from the
470 // shared memory and trace buffers.
471 void UpdateMemoryGuardrail();
472
Eric Seckler4ff03e52019-03-15 10:10:30 +0000473 void StartDataSourceInstance(ProducerEndpointImpl* producer,
474 TracingSession* tracing_session,
475 DataSourceInstance* instance);
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100476 void SnapshotSyncMarker(std::vector<TracePacket>*);
477 void SnapshotClocks(std::vector<TracePacket>*);
478 void SnapshotStats(TracingSession*, std::vector<TracePacket>*);
Eric Secklereaf29ed2019-01-23 09:53:55 +0000479 TraceStats GetTraceStats(TracingSession* tracing_session);
Sami Kyostila200bd2e2018-03-26 12:24:10 +0100480 void MaybeEmitTraceConfig(TracingSession*, std::vector<TracePacket>*);
Hector Dearman685f7522019-03-12 14:28:56 +0000481 void MaybeEmitSystemInfo(TracingSession*, std::vector<TracePacket>*);
Primiano Tuccid52e6272018-04-06 19:06:53 +0200482 void OnFlushTimeout(TracingSessionID, FlushRequestID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100483 void OnDisableTracingTimeout(TracingSessionID);
484 void DisableTracingNotifyConsumerAndFlushFile(TracingSession*);
Primiano Tuccicaa57802018-11-25 11:07:07 +0000485 void PeriodicFlushTask(TracingSessionID, bool post_next_only);
Eric Secklera01e28a2019-01-08 11:21:04 +0000486 void CompleteFlush(TracingSessionID tsid,
487 ConsumerEndpoint::FlushCallback callback,
488 bool success);
489 void ScrapeSharedMemoryBuffers(TracingSession* tracing_session,
490 ProducerEndpointImpl* producer);
Hector Dearman6214c8f2018-03-27 16:16:22 +0100491 TraceBuffer* GetBufferByID(BufferID);
Stephen Nusko59847292019-03-22 13:54:08 +0000492 void OnStartTriggersTimeout(TracingSessionID tsid);
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000493
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000494 base::TaskRunner* const task_runner_;
Primiano Tucci53589332017-12-19 11:31:13 +0100495 std::unique_ptr<SharedMemory::Factory> shm_factory_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000496 ProducerID last_producer_id_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100497 DataSourceInstanceID last_data_source_instance_id_ = 0;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000498 TracingSessionID last_tracing_session_id_ = 0;
Primiano Tuccid52e6272018-04-06 19:06:53 +0200499 FlushRequestID last_flush_request_id_ = 0;
Primiano Tucci5e33cad2018-04-30 14:41:25 +0100500 uid_t uid_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100501
502 // Buffer IDs are global across all consumers (because a Producer can produce
503 // data for more than one trace session, hence more than one consumer).
Primiano Tucci20d441d2018-01-16 09:25:51 +0000504 IdAllocator<BufferID> buffer_ids_;
Primiano Tucci53589332017-12-19 11:31:13 +0100505
506 std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000507 std::map<ProducerID, ProducerEndpointImpl*> producers_;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000508 std::set<ConsumerEndpointImpl*> consumers_;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000509 std::map<TracingSessionID, TracingSession> tracing_sessions_;
Hector Dearman6214c8f2018-03-27 16:16:22 +0100510 std::map<BufferID, std::unique_ptr<TraceBuffer>> buffers_;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000511
Eric Secklera01e28a2019-01-08 11:21:04 +0000512 bool smb_scraping_enabled_ = false;
Florian Mayer61c55482018-03-06 14:43:54 +0000513 bool lockdown_mode_ = false;
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100514 uint32_t min_write_period_ms_ = 100; // Overridable for testing.
515
516 uint8_t sync_marker_packet_[32]; // Lazily initialized.
517 size_t sync_marker_packet_size_ = 0;
Florian Mayer61c55482018-03-06 14:43:54 +0000518
Eric Seckler2c72bd82019-02-08 15:01:34 +0000519 // Stats.
520 uint64_t chunks_discarded_ = 0;
521 uint64_t patches_discarded_ = 0;
522
Florian Mayercd08ec62018-01-31 17:49:25 +0000523 PERFETTO_THREAD_CHECKER(thread_checker_)
524
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100525 base::WeakPtrFactory<TracingServiceImpl>
526 weak_ptr_factory_; // Keep at the end.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000527};
528
529} // namespace perfetto
530
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100531#endif // SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_