blob: 91db0fb8c7ec11ce544e4279534b2c3718a7048f [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 Tucci42e2de12017-12-07 16:46:04 +000025#include "perfetto/base/weak_ptr.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000026#include "perfetto/tracing/core/basic_types.h"
27#include "perfetto/tracing/core/service.h"
28
29namespace perfetto {
30
31namespace base {
32class TaskRunner;
33} // namespace base
34
Primiano Tucci42e2de12017-12-07 16:46:04 +000035class Consumer;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000036class DataSourceConfig;
37class Producer;
38class SharedMemory;
Primiano Tucci42e2de12017-12-07 16:46:04 +000039class TraceConfig;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000040
41// The tracing service business logic.
42class ServiceImpl : public Service {
43 public:
44 // The implementation behind the service endpoint exposed to each producer.
45 class ProducerEndpointImpl : public Service::ProducerEndpoint {
46 public:
47 ProducerEndpointImpl(ProducerID,
48 ServiceImpl*,
49 base::TaskRunner*,
50 Producer*,
51 std::unique_ptr<SharedMemory>);
52 ~ProducerEndpointImpl() override;
53
54 Producer* producer() const { return producer_; }
55
56 // Service::ProducerEndpoint implementation.
57 void RegisterDataSource(const DataSourceDescriptor&,
58 RegisterDataSourceCallback) override;
59 void UnregisterDataSource(DataSourceID) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000060 void NotifySharedMemoryUpdate(
61 const std::vector<uint32_t>& changed_pages) override;
Primiano Tucciaf429f92017-12-19 01:51:50 +010062 std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000063 SharedMemory* shared_memory() const override;
64
65 private:
66 ProducerEndpointImpl(const ProducerEndpointImpl&) = delete;
67 ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete;
68
69 ProducerID const id_;
70 ServiceImpl* const service_;
71 base::TaskRunner* const task_runner_;
72 Producer* producer_;
73 std::unique_ptr<SharedMemory> shared_memory_;
74 DataSourceID last_data_source_id_ = 0;
75 };
76
Primiano Tucci42e2de12017-12-07 16:46:04 +000077 // The implementation behind the service endpoint exposed to each consumer.
78 class ConsumerEndpointImpl : public Service::ConsumerEndpoint {
79 public:
80 ConsumerEndpointImpl(ServiceImpl*, base::TaskRunner*, Consumer*);
81 ~ConsumerEndpointImpl() override;
82
83 Consumer* consumer() const { return consumer_; }
84 base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr();
85
86 // Service::ConsumerEndpoint implementation.
87 void EnableTracing(const TraceConfig&) override;
88 void DisableTracing() override;
89 void ReadBuffers() override;
90 void FreeBuffers() override;
91
92 private:
93 ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete;
94 ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete;
95
96 ServiceImpl* const service_;
97 Consumer* const consumer_;
98 base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_;
99 };
100
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000101 explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>,
102 base::TaskRunner*);
103 ~ServiceImpl() override;
104
Primiano Tucci42e2de12017-12-07 16:46:04 +0000105 // Called by ProducerEndpointImpl.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000106 void DisconnectProducer(ProducerID);
107
Primiano Tucci42e2de12017-12-07 16:46:04 +0000108 // Called by ConsumerEndpointImpl.
109 void DisconnectConsumer(ConsumerEndpointImpl*);
110 void EnableTracing(ConsumerEndpointImpl*, const TraceConfig&);
111 void DisableTracing(ConsumerEndpointImpl*);
112 void ReadBuffers(ConsumerEndpointImpl*);
113 void FreeBuffers(ConsumerEndpointImpl*);
114
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000115 // Service implementation.
116 std::unique_ptr<Service::ProducerEndpoint> ConnectProducer(
117 Producer*,
118 size_t shared_buffer_size_hint_bytes = 0) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000119
120 std::unique_ptr<Service::ConsumerEndpoint> ConnectConsumer(
121 Consumer*) override;
122
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000123 // Exposed mainly for testing.
124 size_t num_producers() const { return producers_.size(); }
125 ProducerEndpointImpl* GetProducer(ProducerID) const;
126
127 private:
128 ServiceImpl(const ServiceImpl&) = delete;
129 ServiceImpl& operator=(const ServiceImpl&) = delete;
130
131 std::unique_ptr<SharedMemory::Factory> shm_factory_;
132 base::TaskRunner* const task_runner_;
133 ProducerID last_producer_id_ = 0;
134 std::map<ProducerID, ProducerEndpointImpl*> producers_;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000135 std::set<ConsumerEndpointImpl*> consumers_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000136};
137
138} // namespace perfetto
139
140#endif // SRC_TRACING_CORE_SERVICE_IMPL_H_