• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_
18 #define SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_
19 
20 #include <stdint.h>
21 
22 #include <vector>
23 
24 #include "perfetto/base/scoped_file.h"
25 #include "perfetto/base/weak_ptr.h"
26 #include "perfetto/ipc/service_proxy.h"
27 #include "perfetto/tracing/core/basic_types.h"
28 #include "perfetto/tracing/core/trace_packet.h"
29 #include "perfetto/tracing/core/tracing_service.h"
30 #include "perfetto/tracing/ipc/consumer_ipc_client.h"
31 
32 #include "perfetto/ipc/consumer_port.ipc.h"
33 
34 namespace perfetto {
35 
36 namespace base {
37 class TaskRunner;
38 }  // namespace base
39 
40 namespace ipc {
41 class Client;
42 }  // namespace ipc
43 
44 class Consumer;
45 class TraceConfig;
46 
47 // Exposes a Service endpoint to Consumer(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.
51 class ConsumerIPCClientImpl : public TracingService::ConsumerEndpoint,
52                               public ipc::ServiceProxy::EventListener {
53  public:
54   ConsumerIPCClientImpl(const char* service_sock_name,
55                         Consumer*,
56                         base::TaskRunner*);
57   ~ConsumerIPCClientImpl() override;
58 
59   // TracingService::ConsumerEndpoint implementation.
60   // These methods are invoked by the actual Consumer(s) code by clients of the
61   // tracing library, which know nothing about the IPC transport.
62   void EnableTracing(const TraceConfig&, base::ScopedFile) override;
63   void StartTracing() override;
64   void ChangeTraceConfig(const TraceConfig&) override;
65   void DisableTracing() override;
66   void ReadBuffers() override;
67   void FreeBuffers() override;
68   void Flush(uint32_t timeout_ms, FlushCallback) override;
69   void Detach(const std::string& key) override;
70   void Attach(const std::string& key) override;
71   void GetTraceStats() override;
72   void ObserveEvents(uint32_t enabled_event_types) override;
73 
74   // ipc::ServiceProxy::EventListener implementation.
75   // These methods are invoked by the IPC layer, which knows nothing about
76   // tracing, consumers and consumers.
77   void OnConnect() override;
78   void OnDisconnect() override;
79 
80  private:
81   void OnReadBuffersResponse(ipc::AsyncResult<protos::ReadBuffersResponse>);
82   void OnEnableTracingResponse(ipc::AsyncResult<protos::EnableTracingResponse>);
83 
84   // TODO(primiano): think to dtor order, do we rely on any specific sequence?
85   Consumer* const consumer_;
86 
87   // The object that owns the client socket and takes care of IPC traffic.
88   std::unique_ptr<ipc::Client> ipc_channel_;
89 
90   // The proxy interface for the consumer port of the service. It is bound
91   // to |ipc_channel_| and (de)serializes method invocations over the wire.
92   protos::ConsumerPortProxy consumer_port_;
93 
94   bool connected_ = false;
95 
96   // When a packet is too big to fit into a ReadBuffersResponse IPC, the service
97   // will chunk it into several IPCs, each containing few slices of the packet
98   // (a packet's slice is always guaranteed to be << kIPCBufferSize). When
99   // chunking happens this field accumulates the slices received until the
100   // one with |last_slice_for_packet| == true is received.
101   TracePacket partial_packet_;
102 
103   // Keep last.
104   base::WeakPtrFactory<ConsumerIPCClientImpl> weak_ptr_factory_;
105 };
106 
107 }  // namespace perfetto
108 
109 #endif  // SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_
110