blob: bda8c29c0a56f4a31cd2c805ff1e1b744190aded [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*,
Oystein Eftevaagc8d2f072019-03-29 09:41:04 -070078 const std::string& producer_name,
79 bool in_process);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000080 ~ProducerEndpointImpl() override;
81
Florian Mayer6a1a4d52018-06-08 16:47:07 +010082 // TracingService::ProducerEndpoint implementation.
Primiano Tucci9daa4832018-03-28 23:28:17 +010083 void RegisterDataSource(const DataSourceDescriptor&) override;
84 void UnregisterDataSource(const std::string& name) override;
Eric Seckler1c4e1ac2018-11-29 10:23:14 +000085 void RegisterTraceWriter(uint32_t writer_id,
86 uint32_t target_buffer) override;
87 void UnregisterTraceWriter(uint32_t writer_id) override;
Primiano Tucci3e69ed92018-03-14 14:52:29 +000088 void CommitData(const CommitDataRequest&, CommitDataCallback) override;
Isabelle Taylor69faa902018-03-21 15:42:03 +000089 void SetSharedMemory(std::unique_ptr<SharedMemory>);
Primiano Tucciaf429f92017-12-19 01:51:50 +010090 std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override;
Primiano Tuccid52e6272018-04-06 19:06:53 +020091 void NotifyFlushComplete(FlushRequestID) override;
Eric Seckler4ff03e52019-03-15 10:10:30 +000092 void NotifyDataSourceStarted(DataSourceInstanceID) override;
Primiano Tuccibaeecf12018-07-25 12:02:20 +010093 void NotifyDataSourceStopped(DataSourceInstanceID) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000094 SharedMemory* shared_memory() const override;
Isabelle Taylor69faa902018-03-21 15:42:03 +000095 size_t shared_buffer_page_size_kb() const override;
Stephen Nusko1393ffd2019-03-22 13:54:58 +000096 void ActivateTriggers(const std::vector<std::string>&) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000097
Primiano Tuccibaeecf12018-07-25 12:02:20 +010098 void OnTracingSetup();
Primiano Tucci674076d2018-10-01 10:41:09 +010099 void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&);
Primiano Tucciafb72b52018-09-25 09:37:24 +0100100 void StartDataSource(DataSourceInstanceID, const DataSourceConfig&);
Primiano Tucci674076d2018-10-01 10:41:09 +0100101 void StopDataSource(DataSourceInstanceID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100102 void Flush(FlushRequestID, const std::vector<DataSourceInstanceID>&);
Eric Seckler6dc23592018-11-30 10:59:06 +0000103 void OnFreeBuffers(const std::vector<BufferID>& target_buffers);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100104
Eric Secklerdd0ad102018-12-06 11:32:04 +0000105 bool is_allowed_target_buffer(BufferID buffer_id) const {
106 return allowed_target_buffers_.count(buffer_id);
107 }
108
Eric Secklerf3f524b2018-12-13 09:09:34 +0000109 base::Optional<BufferID> buffer_id_for_writer(WriterID writer_id) const {
110 const auto it = writers_.find(writer_id);
111 if (it != writers_.end())
112 return it->second;
113 return base::nullopt;
114 }
115
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000116 private:
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100117 friend class TracingServiceImpl;
118 friend class TracingServiceImplTest;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000119 ProducerEndpointImpl(const ProducerEndpointImpl&) = delete;
120 ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete;
Oystein Eftevaagc8d2f072019-03-29 09:41:04 -0700121 SharedMemoryArbiterImpl* GetShmemArbiter();
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000122
123 ProducerID const id_;
Sami Kyostila32e0b542018-02-14 08:55:43 +0000124 const uid_t uid_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100125 TracingServiceImpl* const service_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000126 base::TaskRunner* const task_runner_;
127 Producer* producer_;
128 std::unique_ptr<SharedMemory> shared_memory_;
Isabelle Taylor69faa902018-03-21 15:42:03 +0000129 size_t shared_buffer_page_size_kb_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100130 SharedMemoryABI shmem_abi_;
Primiano Tucci1a1951d2018-04-04 21:08:16 +0200131 size_t shmem_size_hint_bytes_ = 0;
Isabelle Taylor86262cb2018-03-27 16:00:54 +0100132 const std::string name_;
Oystein Eftevaagc8d2f072019-03-29 09:41:04 -0700133 bool in_process_;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100134
Eric Seckler6dc23592018-11-30 10:59:06 +0000135 // Set of the global target_buffer IDs that the producer is configured to
136 // write into in any active tracing session.
137 std::set<BufferID> allowed_target_buffers_;
138
Eric Secklerf3f524b2018-12-13 09:09:34 +0000139 // Maps registered TraceWriter IDs to their target buffers as registered by
140 // the producer. Note that producers aren't required to register their
141 // writers, so we may see commits of chunks with WriterIDs that aren't
142 // contained in this map. However, if a producer does register a writer, the
143 // service will prevent the writer from writing into any other buffer than
144 // the one associated with it here. The BufferIDs stored in this map are
145 // untrusted, so need to be verified against |allowed_target_buffers_|
146 // before use.
147 std::map<WriterID, BufferID> writers_;
148
Oystein Eftevaagc8d2f072019-03-29 09:41:04 -0700149 // This is used only in in-process configurations.
Primiano Tucci14219ff2019-02-27 12:41:05 +0100150 // SharedMemoryArbiterImpl methods themselves are thread-safe.
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,
Oystein Eftevaagc8d2f072019-03-29 09:41:04 -0700261 size_t shared_memory_size_hint_bytes = 0,
262 bool in_process = false) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000263
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100264 std::unique_ptr<TracingService::ConsumerEndpoint> ConnectConsumer(
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100265 Consumer*,
266 uid_t) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000267
Eric Secklera01e28a2019-01-08 11:21:04 +0000268 void SetSMBScrapingEnabled(bool enabled) override {
269 smb_scraping_enabled_ = enabled;
270 }
271
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000272 // Exposed mainly for testing.
273 size_t num_producers() const { return producers_.size(); }
274 ProducerEndpointImpl* GetProducer(ProducerID) const;
275
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100276 uint32_t override_data_source_test_timeout_ms_for_testing = 0;
277
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000278 private:
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100279 friend class TracingServiceImplTest;
Primiano Tucci081d46a2018-02-28 11:09:43 +0000280
Primiano Tucci53589332017-12-19 11:31:13 +0100281 struct RegisteredDataSource {
282 ProducerID producer_id;
Primiano Tucci53589332017-12-19 11:31:13 +0100283 DataSourceDescriptor descriptor;
284 };
285
Sami Kyostila06487a22018-02-27 13:48:38 +0000286 // Represents an active data source for a tracing session.
287 struct DataSourceInstance {
Primiano Tucci5403e4f2018-11-27 10:07:03 +0000288 DataSourceInstance(DataSourceInstanceID id,
289 const DataSourceConfig& cfg,
290 const std::string& ds_name,
Eric Seckler4ff03e52019-03-15 10:10:30 +0000291 bool notify_on_start,
292 bool notify_on_stop)
Primiano Tucci5403e4f2018-11-27 10:07:03 +0000293 : instance_id(id),
294 config(cfg),
295 data_source_name(ds_name),
Eric Seckler4ff03e52019-03-15 10:10:30 +0000296 will_notify_on_start(notify_on_start),
297 will_notify_on_stop(notify_on_stop) {}
Primiano Tucci674076d2018-10-01 10:41:09 +0100298 DataSourceInstance(const DataSourceInstance&) = delete;
299 DataSourceInstance& operator=(const DataSourceInstance&) = delete;
Primiano Tucci674076d2018-10-01 10:41:09 +0100300
Sami Kyostila06487a22018-02-27 13:48:38 +0000301 DataSourceInstanceID instance_id;
Primiano Tucci674076d2018-10-01 10:41:09 +0100302 DataSourceConfig config;
Primiano Tucci9daa4832018-03-28 23:28:17 +0100303 std::string data_source_name;
Eric Seckler4ff03e52019-03-15 10:10:30 +0000304 bool will_notify_on_start;
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100305 bool will_notify_on_stop;
Eric Seckler4ff03e52019-03-15 10:10:30 +0000306
307 enum DataSourceInstanceState {
308 CONFIGURED,
309 STARTING,
310 STARTED,
311 STOPPING,
312 STOPPED
313 };
314 DataSourceInstanceState state = CONFIGURED;
Sami Kyostila06487a22018-02-27 13:48:38 +0000315 };
316
Primiano Tuccid52e6272018-04-06 19:06:53 +0200317 struct PendingFlush {
318 std::set<ProducerID> producers;
319 ConsumerEndpoint::FlushCallback callback;
320 explicit PendingFlush(decltype(callback) cb) : callback(std::move(cb)) {}
321 };
322
Primiano Tucci53589332017-12-19 11:31:13 +0100323 // Holds the state of a tracing session. A tracing session is uniquely bound
324 // a specific Consumer. Each Consumer can own one or more sessions.
325 struct TracingSession {
Primiano Tucci674076d2018-10-01 10:41:09 +0100326 enum State {
327 DISABLED = 0,
328 CONFIGURED,
329 STARTED,
330 DISABLING_WAITING_STOP_ACKS
331 };
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100332
333 TracingSession(TracingSessionID, ConsumerEndpointImpl*, const TraceConfig&);
Primiano Tucci20d441d2018-01-16 09:25:51 +0000334
335 size_t num_buffers() const { return buffers_index.size(); }
336
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100337 uint32_t delay_to_next_write_period_ms() const {
Sami Kyostila01c45f02018-03-29 15:43:10 +0100338 PERFETTO_DCHECK(write_period_ms > 0);
339 return write_period_ms -
340 (base::GetWallTimeMs().count() % write_period_ms);
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100341 }
342
Florian Mayere563f662019-01-09 11:04:50 +0000343 uint32_t flush_timeout_ms() {
344 uint32_t timeout_ms = config.flush_timeout_ms();
345 return timeout_ms ? timeout_ms : kDefaultFlushTimeoutMs;
346 }
347
Eric Secklerd0ac7ca2019-02-06 09:13:45 +0000348 PacketSequenceID GetPacketSequenceID(ProducerID producer_id,
349 WriterID writer_id) {
350 auto key = std::make_pair(producer_id, writer_id);
351 auto it = packet_sequence_ids.find(key);
352 if (it != packet_sequence_ids.end())
353 return it->second;
354 // We shouldn't run out of sequence IDs (producer ID is 16 bit, writer IDs
355 // are limited to 1024).
356 static_assert(kMaxPacketSequenceID > kMaxProducerID * kMaxWriterID,
357 "PacketSequenceID value space doesn't cover service "
358 "sequence ID and all producer/writer ID combinations!");
359 PERFETTO_DCHECK(last_packet_sequence_id < kMaxPacketSequenceID);
360 PacketSequenceID sequence_id = ++last_packet_sequence_id;
361 packet_sequence_ids[key] = sequence_id;
362 return sequence_id;
363 }
364
Eric Seckler4ff03e52019-03-15 10:10:30 +0000365 DataSourceInstance* GetDataSourceInstance(
366 ProducerID producer_id,
367 DataSourceInstanceID instance_id) {
368 for (auto& inst_kv : data_source_instances) {
369 if (inst_kv.first != producer_id ||
370 inst_kv.second.instance_id != instance_id) {
371 continue;
372 }
373 return &inst_kv.second;
374 }
375 return nullptr;
376 }
377
378 bool AllDataSourceInstancesStopped() {
379 for (const auto& inst_kv : data_source_instances) {
380 if (inst_kv.second.state != DataSourceInstance::STOPPED)
381 return false;
382 }
383 return true;
384 }
385
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100386 const TracingSessionID id;
387
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100388 // The consumer that started the session.
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100389 // Can be nullptr if the consumer detached from the session.
390 ConsumerEndpointImpl* consumer_maybe_null;
391
392 // Unix uid of the consumer. This is valid even after the consumer detaches
393 // and does not change for the entire duration of the session. It is used to
394 // prevent that a consumer re-attaches to a session from a different uid.
395 uid_t const consumer_uid;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100396
Stephen Nusko59847292019-03-22 13:54:08 +0000397 // The list of triggers this session received while alive and the time they
398 // were received at. This is used to insert 'fake' packets back to the
399 // consumer so they can tell when some event happened. The order matches the
400 // order they were received.
Stephen Nusko70ea3302019-04-01 19:44:40 +0100401 struct TriggerInfo {
402 uint64_t boot_time_ns;
403 std::string trigger_name;
404 std::string producer_name;
405 uid_t producer_uid;
406 };
407 std::vector<TriggerInfo> received_triggers;
Stephen Nusko59847292019-03-22 13:54:08 +0000408
Oystein Eftevaagcb6e4c82019-03-06 15:38:26 -0800409 // The trace config provided by the Consumer when calling
410 // EnableTracing(), plus any updates performed by ChangeTraceConfig.
411 TraceConfig config;
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800412
Primiano Tucci53589332017-12-19 11:31:13 +0100413 // List of data source instances that have been enabled on the various
414 // producers for this tracing session.
Sami Kyostila06487a22018-02-27 13:48:38 +0000415 std::multimap<ProducerID, DataSourceInstance> data_source_instances;
Primiano Tucci53589332017-12-19 11:31:13 +0100416
Primiano Tuccid52e6272018-04-06 19:06:53 +0200417 // For each Flush(N) request, keeps track of the set of producers for which
418 // we are still awaiting a NotifyFlushComplete(N) ack.
419 std::map<FlushRequestID, PendingFlush> pending_flushes;
420
Primiano Tucci20d441d2018-01-16 09:25:51 +0000421 // Maps a per-trace-session buffer index into the corresponding global
422 // BufferID (shared namespace amongst all consumers). This vector has as
423 // many entries as |config.buffers_size()|.
424 std::vector<BufferID> buffers_index;
Sami Kyostilafbccb3c2018-03-21 14:00:47 +0000425
Eric Secklerd0ac7ca2019-02-06 09:13:45 +0000426 std::map<std::pair<ProducerID, WriterID>, PacketSequenceID>
427 packet_sequence_ids;
428 PacketSequenceID last_packet_sequence_id = kServicePacketSequenceID;
429
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100430 // When the last snapshots (clock, stats, sync marker) were emitted into
431 // the output stream.
432 base::TimeMillis last_snapshot_time = {};
Primiano Tucci5e33cad2018-04-30 14:41:25 +0100433
Sami Kyostila200bd2e2018-03-26 12:24:10 +0100434 // Whether we mirrored the trace config back to the trace output yet.
435 bool did_emit_config = false;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100436
Hector Dearman685f7522019-03-12 14:28:56 +0000437 // Whether we put the system info into the trace output yet.
438 bool did_emit_system_info = false;
439
Stephen Nusko70ea3302019-04-01 19:44:40 +0100440 // The number of received triggers we've emitted into the trace output.
441 size_t num_triggers_emitted_into_trace = 0;
442
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100443 State state = DISABLED;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100444
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100445 // If the consumer detached the session, this variable defines the key used
446 // for identifying the session later when reattaching.
447 std::string detach_key;
448
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100449 // This is set when the Consumer calls sets |write_into_file| == true in the
450 // TraceConfig. In this case this represents the file we should stream the
451 // trace packets into, rather than returning it to the consumer via
452 // OnTraceData().
453 base::ScopedFile write_into_file;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100454 uint32_t write_period_ms = 0;
455 uint64_t max_file_size_bytes = 0;
456 uint64_t bytes_written_into_file = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100457 };
458
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100459 TracingServiceImpl(const TracingServiceImpl&) = delete;
460 TracingServiceImpl& operator=(const TracingServiceImpl&) = delete;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000461
Primiano Tucci674076d2018-10-01 10:41:09 +0100462 DataSourceInstance* SetupDataSource(const TraceConfig::DataSource&,
463 const TraceConfig::ProducerConfig&,
464 const RegisteredDataSource&,
465 TracingSession*);
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800466
Primiano Tucci081d46a2018-02-28 11:09:43 +0000467 // Returns the next available ProducerID that is not in |producers_|.
468 ProducerID GetNextProducerID();
469
Primiano Tucci20d441d2018-01-16 09:25:51 +0000470 // Returns a pointer to the |tracing_sessions_| entry or nullptr if the
471 // session doesn't exists.
472 TracingSession* GetTracingSession(TracingSessionID);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000473
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100474 // Returns a pointer to the |tracing_sessions_| entry, matching the given
475 // uid and detach key, or nullptr if no such session exists.
476 TracingSession* GetDetachedSession(uid_t, const std::string& key);
477
Lalit Maganti485faff2018-03-06 11:51:35 +0000478 // Update the memory guard rail by using the latest information from the
479 // shared memory and trace buffers.
480 void UpdateMemoryGuardrail();
481
Eric Seckler4ff03e52019-03-15 10:10:30 +0000482 void StartDataSourceInstance(ProducerEndpointImpl* producer,
483 TracingSession* tracing_session,
484 DataSourceInstance* instance);
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100485 void SnapshotSyncMarker(std::vector<TracePacket>*);
486 void SnapshotClocks(std::vector<TracePacket>*);
487 void SnapshotStats(TracingSession*, std::vector<TracePacket>*);
Eric Secklereaf29ed2019-01-23 09:53:55 +0000488 TraceStats GetTraceStats(TracingSession* tracing_session);
Sami Kyostila200bd2e2018-03-26 12:24:10 +0100489 void MaybeEmitTraceConfig(TracingSession*, std::vector<TracePacket>*);
Hector Dearman685f7522019-03-12 14:28:56 +0000490 void MaybeEmitSystemInfo(TracingSession*, std::vector<TracePacket>*);
Stephen Nusko70ea3302019-04-01 19:44:40 +0100491 void MaybeEmitReceivedTriggers(TracingSession*, std::vector<TracePacket>*);
Primiano Tuccid52e6272018-04-06 19:06:53 +0200492 void OnFlushTimeout(TracingSessionID, FlushRequestID);
Primiano Tuccibaeecf12018-07-25 12:02:20 +0100493 void OnDisableTracingTimeout(TracingSessionID);
494 void DisableTracingNotifyConsumerAndFlushFile(TracingSession*);
Primiano Tuccicaa57802018-11-25 11:07:07 +0000495 void PeriodicFlushTask(TracingSessionID, bool post_next_only);
Eric Secklera01e28a2019-01-08 11:21:04 +0000496 void CompleteFlush(TracingSessionID tsid,
497 ConsumerEndpoint::FlushCallback callback,
498 bool success);
499 void ScrapeSharedMemoryBuffers(TracingSession* tracing_session,
500 ProducerEndpointImpl* producer);
Hector Dearman6214c8f2018-03-27 16:16:22 +0100501 TraceBuffer* GetBufferByID(BufferID);
Stephen Nusko59847292019-03-22 13:54:08 +0000502 void OnStartTriggersTimeout(TracingSessionID tsid);
Primiano Tucciecf9e4a2018-03-14 14:51:58 +0000503
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000504 base::TaskRunner* const task_runner_;
Primiano Tucci53589332017-12-19 11:31:13 +0100505 std::unique_ptr<SharedMemory::Factory> shm_factory_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000506 ProducerID last_producer_id_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100507 DataSourceInstanceID last_data_source_instance_id_ = 0;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000508 TracingSessionID last_tracing_session_id_ = 0;
Primiano Tuccid52e6272018-04-06 19:06:53 +0200509 FlushRequestID last_flush_request_id_ = 0;
Primiano Tucci5e33cad2018-04-30 14:41:25 +0100510 uid_t uid_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100511
512 // Buffer IDs are global across all consumers (because a Producer can produce
513 // data for more than one trace session, hence more than one consumer).
Primiano Tucci20d441d2018-01-16 09:25:51 +0000514 IdAllocator<BufferID> buffer_ids_;
Primiano Tucci53589332017-12-19 11:31:13 +0100515
516 std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000517 std::map<ProducerID, ProducerEndpointImpl*> producers_;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000518 std::set<ConsumerEndpointImpl*> consumers_;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000519 std::map<TracingSessionID, TracingSession> tracing_sessions_;
Hector Dearman6214c8f2018-03-27 16:16:22 +0100520 std::map<BufferID, std::unique_ptr<TraceBuffer>> buffers_;
Primiano Tucci20d441d2018-01-16 09:25:51 +0000521
Eric Secklera01e28a2019-01-08 11:21:04 +0000522 bool smb_scraping_enabled_ = false;
Florian Mayer61c55482018-03-06 14:43:54 +0000523 bool lockdown_mode_ = false;
Primiano Tucci9754d0d2018-09-15 12:41:46 +0100524 uint32_t min_write_period_ms_ = 100; // Overridable for testing.
525
526 uint8_t sync_marker_packet_[32]; // Lazily initialized.
527 size_t sync_marker_packet_size_ = 0;
Florian Mayer61c55482018-03-06 14:43:54 +0000528
Eric Seckler2c72bd82019-02-08 15:01:34 +0000529 // Stats.
530 uint64_t chunks_discarded_ = 0;
531 uint64_t patches_discarded_ = 0;
532
Florian Mayercd08ec62018-01-31 17:49:25 +0000533 PERFETTO_THREAD_CHECKER(thread_checker_)
534
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100535 base::WeakPtrFactory<TracingServiceImpl>
536 weak_ptr_factory_; // Keep at the end.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000537};
538
539} // namespace perfetto
540
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100541#endif // SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_