blob: 2e0a6f4951da22a37b04751b289f926ef5016bac [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>
23
24#include "perfetto/tracing/core/basic_types.h"
25#include "perfetto/tracing/core/service.h"
26
27namespace perfetto {
28
29namespace base {
30class TaskRunner;
31} // namespace base
32
33class DataSourceConfig;
34class Producer;
35class SharedMemory;
36
37// The tracing service business logic.
38class ServiceImpl : public Service {
39 public:
40 // The implementation behind the service endpoint exposed to each producer.
41 class ProducerEndpointImpl : public Service::ProducerEndpoint {
42 public:
43 ProducerEndpointImpl(ProducerID,
44 ServiceImpl*,
45 base::TaskRunner*,
46 Producer*,
47 std::unique_ptr<SharedMemory>);
48 ~ProducerEndpointImpl() override;
49
50 Producer* producer() const { return producer_; }
51
52 // Service::ProducerEndpoint implementation.
53 void RegisterDataSource(const DataSourceDescriptor&,
54 RegisterDataSourceCallback) override;
55 void UnregisterDataSource(DataSourceID) override;
56
57 void NotifySharedMemoryUpdate(
58 const std::vector<uint32_t>& changed_pages) override;
59
60 SharedMemory* shared_memory() const override;
61
62 private:
63 ProducerEndpointImpl(const ProducerEndpointImpl&) = delete;
64 ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete;
65
66 ProducerID const id_;
67 ServiceImpl* const service_;
68 base::TaskRunner* const task_runner_;
69 Producer* producer_;
70 std::unique_ptr<SharedMemory> shared_memory_;
71 DataSourceID last_data_source_id_ = 0;
72 };
73
74 explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>,
75 base::TaskRunner*);
76 ~ServiceImpl() override;
77
78 // Called by the ProducerEndpointImpl dtor.
79 void DisconnectProducer(ProducerID);
80
81 // Service implementation.
82 std::unique_ptr<Service::ProducerEndpoint> ConnectProducer(
83 Producer*,
84 size_t shared_buffer_size_hint_bytes = 0) override;
85 void set_observer_for_testing(ObserverForTesting*) override;
86
87 // Exposed mainly for testing.
88 size_t num_producers() const { return producers_.size(); }
89 ProducerEndpointImpl* GetProducer(ProducerID) const;
90
91 private:
92 ServiceImpl(const ServiceImpl&) = delete;
93 ServiceImpl& operator=(const ServiceImpl&) = delete;
94
95 std::unique_ptr<SharedMemory::Factory> shm_factory_;
96 base::TaskRunner* const task_runner_;
97 ProducerID last_producer_id_ = 0;
98 std::map<ProducerID, ProducerEndpointImpl*> producers_;
99 ObserverForTesting* observer_ = nullptr;
100};
101
102} // namespace perfetto
103
104#endif // SRC_TRACING_CORE_SERVICE_IMPL_H_