blob: cdeccb91aab803b416f411e910c02808155fe985 [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
17#ifndef SRC_TRACING_CORE_SERVICE_IMPL_H_
18#define SRC_TRACING_CORE_SERVICE_IMPL_H_
19
20#include <functional>
21#include <map>
22#include <memory>
Primiano Tucci42e2de12017-12-07 16:46:04 +000023#include <set>
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000024
Primiano Tuccibbaa58c2017-12-20 13:48:20 +010025#include "perfetto/base/page_allocator.h"
Primiano Tucci42e2de12017-12-07 16:46:04 +000026#include "perfetto/base/weak_ptr.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000027#include "perfetto/tracing/core/basic_types.h"
Primiano Tucci53589332017-12-19 11:31:13 +010028#include "perfetto/tracing/core/data_source_descriptor.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000029#include "perfetto/tracing/core/service.h"
Primiano Tucci53589332017-12-19 11:31:13 +010030#include "perfetto/tracing/core/shared_memory_abi.h"
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -080031#include "perfetto/tracing/core/trace_config.h"
Primiano Tucci53589332017-12-19 11:31:13 +010032#include "src/tracing/core/id_allocator.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000033
34namespace perfetto {
35
36namespace base {
37class TaskRunner;
38} // namespace base
39
Primiano Tucci42e2de12017-12-07 16:46:04 +000040class Consumer;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000041class DataSourceConfig;
42class Producer;
43class SharedMemory;
Primiano Tucci42e2de12017-12-07 16:46:04 +000044class TraceConfig;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000045
46// The tracing service business logic.
47class ServiceImpl : public Service {
48 public:
49 // The implementation behind the service endpoint exposed to each producer.
50 class ProducerEndpointImpl : public Service::ProducerEndpoint {
51 public:
52 ProducerEndpointImpl(ProducerID,
53 ServiceImpl*,
54 base::TaskRunner*,
55 Producer*,
56 std::unique_ptr<SharedMemory>);
57 ~ProducerEndpointImpl() override;
58
59 Producer* producer() const { return producer_; }
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -080060 ProducerID id() const { return id_; }
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000061
62 // Service::ProducerEndpoint implementation.
63 void RegisterDataSource(const DataSourceDescriptor&,
64 RegisterDataSourceCallback) override;
65 void UnregisterDataSource(DataSourceID) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000066 void NotifySharedMemoryUpdate(
67 const std::vector<uint32_t>& changed_pages) override;
Primiano Tucciaf429f92017-12-19 01:51:50 +010068 std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000069 SharedMemory* shared_memory() const override;
70
71 private:
72 ProducerEndpointImpl(const ProducerEndpointImpl&) = delete;
73 ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete;
74
75 ProducerID const id_;
76 ServiceImpl* const service_;
77 base::TaskRunner* const task_runner_;
78 Producer* producer_;
79 std::unique_ptr<SharedMemory> shared_memory_;
Primiano Tucci53589332017-12-19 11:31:13 +010080 SharedMemoryABI shmem_abi_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000081 DataSourceID last_data_source_id_ = 0;
82 };
83
Primiano Tucci42e2de12017-12-07 16:46:04 +000084 // The implementation behind the service endpoint exposed to each consumer.
85 class ConsumerEndpointImpl : public Service::ConsumerEndpoint {
86 public:
87 ConsumerEndpointImpl(ServiceImpl*, base::TaskRunner*, Consumer*);
88 ~ConsumerEndpointImpl() override;
89
90 Consumer* consumer() const { return consumer_; }
91 base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr();
92
93 // Service::ConsumerEndpoint implementation.
94 void EnableTracing(const TraceConfig&) override;
95 void DisableTracing() override;
96 void ReadBuffers() override;
97 void FreeBuffers() override;
98
99 private:
100 ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete;
101 ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete;
102
103 ServiceImpl* const service_;
104 Consumer* const consumer_;
105 base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_;
106 };
107
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000108 explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>,
109 base::TaskRunner*);
110 ~ServiceImpl() override;
111
Primiano Tucci42e2de12017-12-07 16:46:04 +0000112 // Called by ProducerEndpointImpl.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000113 void DisconnectProducer(ProducerID);
Primiano Tucci53589332017-12-19 11:31:13 +0100114 void RegisterDataSource(ProducerID,
115 DataSourceID,
116 const DataSourceDescriptor&);
117 void CopyProducerPageIntoLogBuffer(ProducerID,
118 BufferID,
119 const uint8_t*,
120 size_t);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000121
Primiano Tucci42e2de12017-12-07 16:46:04 +0000122 // Called by ConsumerEndpointImpl.
123 void DisconnectConsumer(ConsumerEndpointImpl*);
124 void EnableTracing(ConsumerEndpointImpl*, const TraceConfig&);
125 void DisableTracing(ConsumerEndpointImpl*);
126 void ReadBuffers(ConsumerEndpointImpl*);
127 void FreeBuffers(ConsumerEndpointImpl*);
128
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000129 // Service implementation.
130 std::unique_ptr<Service::ProducerEndpoint> ConnectProducer(
131 Producer*,
132 size_t shared_buffer_size_hint_bytes = 0) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000133
134 std::unique_ptr<Service::ConsumerEndpoint> ConnectConsumer(
135 Consumer*) override;
136
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000137 // Exposed mainly for testing.
138 size_t num_producers() const { return producers_.size(); }
139 ProducerEndpointImpl* GetProducer(ProducerID) const;
140
141 private:
Primiano Tucci53589332017-12-19 11:31:13 +0100142 struct RegisteredDataSource {
143 ProducerID producer_id;
144 DataSourceID data_source_id;
145 DataSourceDescriptor descriptor;
146 };
147
148 struct TraceBuffer {
149 // TODO(primiano): make this configurable.
150 static constexpr size_t kBufferPageSize = 4096;
151 explicit TraceBuffer(size_t size);
152 ~TraceBuffer();
153 TraceBuffer(TraceBuffer&&) noexcept;
154 TraceBuffer& operator=(TraceBuffer&&);
155
Primiano Tuccibbaa58c2017-12-20 13:48:20 +0100156 bool is_valid() const { return !!data; }
Primiano Tucci53589332017-12-19 11:31:13 +0100157 size_t num_pages() const { return size / kBufferPageSize; }
158
159 uint8_t* get_page(size_t page) {
160 PERFETTO_DCHECK(page < num_pages());
161 return reinterpret_cast<uint8_t*>(data.get()) + page * kBufferPageSize;
162 }
163
164 uint8_t* get_next_page() {
165 size_t cur = cur_page;
166 cur_page = cur_page == num_pages() - 1 ? 0 : cur_page + 1;
167 return get_page(cur);
168 }
169
170 size_t size;
171 size_t cur_page = 0; // Write pointer in the ring buffer.
Primiano Tuccibbaa58c2017-12-20 13:48:20 +0100172 base::PageAllocator::UniquePtr data;
Primiano Tucci53589332017-12-19 11:31:13 +0100173
174 // TODO(primiano): The TraceBuffer is not shared and there is no reason to
175 // use the SharedMemoryABI. This is just a a temporary workaround to reuse
176 // the convenience of SharedMemoryABI for bookkeeping of the buffer when
177 // implementing ReadBuffers().
178 std::unique_ptr<SharedMemoryABI> abi;
179 };
180
181 // Holds the state of a tracing session. A tracing session is uniquely bound
182 // a specific Consumer. Each Consumer can own one or more sessions.
183 struct TracingSession {
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800184 TracingSession(const TraceConfig& new_config) : config(new_config) {}
185
Primiano Tucci53589332017-12-19 11:31:13 +0100186 // List of data source instances that have been enabled on the various
187 // producers for this tracing session.
188 std::multimap<ProducerID, DataSourceInstanceID> data_source_instances;
189
190 // The key of this map matches the |target_buffer| in the
191 // SharedMemoryABI::ChunkHeader.
192 std::map<BufferID, TraceBuffer> trace_buffers;
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800193
194 TraceConfig config;
Primiano Tucci53589332017-12-19 11:31:13 +0100195 };
196
Oystein Eftevaag1269b4a2018-01-10 16:29:38 -0800197 void CreateDataSourceInstanceForProducer(
198 const TraceConfig::DataSource& cfg_data_source,
199 ProducerEndpointImpl* producer,
200 TracingSession* tracing_session);
201
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000202 ServiceImpl(const ServiceImpl&) = delete;
203 ServiceImpl& operator=(const ServiceImpl&) = delete;
204
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000205 base::TaskRunner* const task_runner_;
Primiano Tucci53589332017-12-19 11:31:13 +0100206 std::unique_ptr<SharedMemory::Factory> shm_factory_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000207 ProducerID last_producer_id_ = 0;
Primiano Tucci53589332017-12-19 11:31:13 +0100208 DataSourceInstanceID last_data_source_instance_id_ = 0;
209
210 // Buffer IDs are global across all consumers (because a Producer can produce
211 // data for more than one trace session, hence more than one consumer).
212 IdAllocator buffer_ids_;
213
214 std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_;
215
216 // TODO(primiano): There doesn't seem to be any good reason why |producers_|
217 // is a map indexed by ID and not just a set<ProducerEndpointImpl*>.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000218 std::map<ProducerID, ProducerEndpointImpl*> producers_;
Primiano Tucci53589332017-12-19 11:31:13 +0100219
Primiano Tucci42e2de12017-12-07 16:46:04 +0000220 std::set<ConsumerEndpointImpl*> consumers_;
Primiano Tucci53589332017-12-19 11:31:13 +0100221 std::map<ConsumerEndpointImpl*, TracingSession> tracing_sessions_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000222};
223
224} // namespace perfetto
225
226#endif // SRC_TRACING_CORE_SERVICE_IMPL_H_