blob: 166c64f8fd26027e25f8102a116e6606224c4462 [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_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_
18#define SRC_TRACING_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_
19
20#include <stdint.h>
21
22#include <vector>
23
Primiano Tucciaf429f92017-12-19 01:51:50 +010024#include "perfetto/base/thread_checker.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000025#include "perfetto/ipc/service_proxy.h"
26#include "perfetto/tracing/core/basic_types.h"
27#include "perfetto/tracing/core/service.h"
Oystein Eftevaag6d0bc7f2018-01-13 12:21:55 -080028#include "perfetto/tracing/core/shared_memory.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000029#include "perfetto/tracing/ipc/producer_ipc_client.h"
Primiano Tucci114b6492017-12-11 23:09:45 +000030
Primiano Tucci20b760c2018-01-19 12:36:12 +000031#include "perfetto/ipc/producer_port.ipc.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000032
33namespace perfetto {
34
35namespace base {
36class TaskRunner;
37} // namespace base
38
39namespace ipc {
40class Client;
41} // namespace ipc
42
43class Producer;
44class PosixSharedMemory;
Primiano Tucciaf429f92017-12-19 01:51:50 +010045class SharedMemoryArbiter;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000046
47// Exposes a Service endpoint to Producer(s), proxying all requests through a
48// IPC channel to the remote Service. This class is the glue layer between the
49// generic Service interface exposed to the clients of the library and the
50// actual IPC transport.
51class ProducerIPCClientImpl : public Service::ProducerEndpoint,
52 public ipc::ServiceProxy::EventListener {
53 public:
54 ProducerIPCClientImpl(const char* service_sock_name,
55 Producer*,
56 base::TaskRunner*);
57 ~ProducerIPCClientImpl() override;
58
59 // Service::ProducerEndpoint implementation.
60 // These methods are invoked by the actual Producer(s) code by clients of the
61 // tracing library, which know nothing about the IPC transport.
62 void RegisterDataSource(const DataSourceDescriptor&,
63 RegisterDataSourceCallback) override;
64 void UnregisterDataSource(DataSourceID) override;
Primiano Tucci79f3f912018-03-02 12:00:31 +000065 void CommitData(const CommitDataRequest&) override;
Primiano Tucciaf429f92017-12-19 01:51:50 +010066 std::unique_ptr<TraceWriter> CreateTraceWriter(
67 BufferID target_buffer) override;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000068 SharedMemory* shared_memory() const override;
69
70 // ipc::ServiceProxy::EventListener implementation.
71 // These methods are invoked by the IPC layer, which knows nothing about
72 // tracing, producers and consumers.
73 void OnConnect() override;
74 void OnDisconnect() override;
75
76 private:
77 // Invoked soon after having established the connection with the service.
78 void OnConnectionInitialized(bool connection_succeeded);
79
80 // Invoked when the remote Service sends an IPC to tell us to do something
81 // (e.g. start/stop a data source).
Primiano Tucci954d8ed2018-02-28 23:08:53 +000082 void OnServiceRequest(const protos::GetAsyncCommandResponse&);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000083
84 // TODO think to destruction order, do we rely on any specific dtor sequence?
85 Producer* const producer_;
86 base::TaskRunner* const task_runner_;
87
88 // The object that owns the client socket and takes care of IPC traffic.
89 std::unique_ptr<ipc::Client> ipc_channel_;
90
91 // The proxy interface for the producer port of the service. It is bound
92 // to |ipc_channel_| and (de)serializes method invocations over the wire.
Primiano Tucci954d8ed2018-02-28 23:08:53 +000093 protos::ProducerPortProxy producer_port_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000094
95 std::unique_ptr<PosixSharedMemory> shared_memory_;
Primiano Tucciaf429f92017-12-19 01:51:50 +010096 std::unique_ptr<SharedMemoryArbiter> shared_memory_arbiter_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000097 bool connected_ = false;
Primiano Tucciaf429f92017-12-19 01:51:50 +010098 PERFETTO_THREAD_CHECKER(thread_checker_)
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000099};
100
101} // namespace perfetto
102
103#endif // SRC_TRACING_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_