blob: 24a39bbd39e88dc0a0b1c69aa77de54adc7bb07f [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;
60
61 void NotifySharedMemoryUpdate(
62 const std::vector<uint32_t>& changed_pages) override;
63
64 SharedMemory* shared_memory() const override;
65
66 private:
67 ProducerEndpointImpl(const ProducerEndpointImpl&) = delete;
68 ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete;
69
70 ProducerID const id_;
71 ServiceImpl* const service_;
72 base::TaskRunner* const task_runner_;
73 Producer* producer_;
74 std::unique_ptr<SharedMemory> shared_memory_;
75 DataSourceID last_data_source_id_ = 0;
76 };
77
Primiano Tucci42e2de12017-12-07 16:46:04 +000078 // The implementation behind the service endpoint exposed to each consumer.
79 class ConsumerEndpointImpl : public Service::ConsumerEndpoint {
80 public:
81 ConsumerEndpointImpl(ServiceImpl*, base::TaskRunner*, Consumer*);
82 ~ConsumerEndpointImpl() override;
83
84 Consumer* consumer() const { return consumer_; }
85 base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr();
86
87 // Service::ConsumerEndpoint implementation.
88 void EnableTracing(const TraceConfig&) override;
89 void DisableTracing() override;
90 void ReadBuffers() override;
91 void FreeBuffers() override;
92
93 private:
94 ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete;
95 ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete;
96
97 ServiceImpl* const service_;
98 Consumer* const consumer_;
99 base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_;
100 };
101
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000102 explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>,
103 base::TaskRunner*);
104 ~ServiceImpl() override;
105
Primiano Tucci42e2de12017-12-07 16:46:04 +0000106 // Called by ProducerEndpointImpl.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000107 void DisconnectProducer(ProducerID);
108
Primiano Tucci42e2de12017-12-07 16:46:04 +0000109 // Called by ConsumerEndpointImpl.
110 void DisconnectConsumer(ConsumerEndpointImpl*);
111 void EnableTracing(ConsumerEndpointImpl*, const TraceConfig&);
112 void DisableTracing(ConsumerEndpointImpl*);
113 void ReadBuffers(ConsumerEndpointImpl*);
114 void FreeBuffers(ConsumerEndpointImpl*);
115
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000116 // Service implementation.
117 std::unique_ptr<Service::ProducerEndpoint> ConnectProducer(
118 Producer*,
119 size_t shared_buffer_size_hint_bytes = 0) override;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000120
121 std::unique_ptr<Service::ConsumerEndpoint> ConnectConsumer(
122 Consumer*) override;
123
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000124 void set_observer_for_testing(ObserverForTesting*) override;
125
126 // Exposed mainly for testing.
127 size_t num_producers() const { return producers_.size(); }
128 ProducerEndpointImpl* GetProducer(ProducerID) const;
129
130 private:
131 ServiceImpl(const ServiceImpl&) = delete;
132 ServiceImpl& operator=(const ServiceImpl&) = delete;
133
134 std::unique_ptr<SharedMemory::Factory> shm_factory_;
135 base::TaskRunner* const task_runner_;
136 ProducerID last_producer_id_ = 0;
137 std::map<ProducerID, ProducerEndpointImpl*> producers_;
Primiano Tucci42e2de12017-12-07 16:46:04 +0000138 std::set<ConsumerEndpointImpl*> consumers_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000139 ObserverForTesting* observer_ = nullptr;
140};
141
142} // namespace perfetto
143
144#endif // SRC_TRACING_CORE_SERVICE_IMPL_H_