Home | History | Annotate | Download | only in producer
      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_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 <set>
     23 #include <vector>
     24 
     25 #include "perfetto/base/thread_checker.h"
     26 #include "perfetto/ipc/service_proxy.h"
     27 #include "perfetto/tracing/core/basic_types.h"
     28 #include "perfetto/tracing/core/shared_memory.h"
     29 #include "perfetto/tracing/core/tracing_service.h"
     30 #include "perfetto/tracing/ipc/producer_ipc_client.h"
     31 
     32 #include "perfetto/ipc/producer_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 Producer;
     45 class PosixSharedMemory;
     46 class SharedMemoryArbiter;
     47 
     48 // Exposes a Service endpoint to Producer(s), proxying all requests through a
     49 // IPC channel to the remote Service. This class is the glue layer between the
     50 // generic Service interface exposed to the clients of the library and the
     51 // actual IPC transport.
     52 class ProducerIPCClientImpl : public TracingService::ProducerEndpoint,
     53                               public ipc::ServiceProxy::EventListener {
     54  public:
     55   ProducerIPCClientImpl(const char* service_sock_name,
     56                         Producer*,
     57                         const std::string& producer_name,
     58                         base::TaskRunner*,
     59                         TracingService::ProducerSMBScrapingMode);
     60   ~ProducerIPCClientImpl() override;
     61 
     62   // TracingService::ProducerEndpoint implementation.
     63   // These methods are invoked by the actual Producer(s) code by clients of the
     64   // tracing library, which know nothing about the IPC transport.
     65   void RegisterDataSource(const DataSourceDescriptor&) override;
     66   void UnregisterDataSource(const std::string& name) override;
     67   void RegisterTraceWriter(uint32_t writer_id, uint32_t target_buffer) override;
     68   void UnregisterTraceWriter(uint32_t writer_id) override;
     69   void CommitData(const CommitDataRequest&, CommitDataCallback) override;
     70   void NotifyDataSourceStarted(DataSourceInstanceID) override;
     71   void NotifyDataSourceStopped(DataSourceInstanceID) override;
     72   void ActivateTriggers(const std::vector<std::string>&) override;
     73 
     74   std::unique_ptr<TraceWriter> CreateTraceWriter(
     75       BufferID target_buffer) override;
     76   SharedMemoryArbiter* GetInProcessShmemArbiter() override;
     77   void NotifyFlushComplete(FlushRequestID) override;
     78   SharedMemory* shared_memory() const override;
     79   size_t shared_buffer_page_size_kb() const override;
     80 
     81   // ipc::ServiceProxy::EventListener implementation.
     82   // These methods are invoked by the IPC layer, which knows nothing about
     83   // tracing, producers and consumers.
     84   void OnConnect() override;
     85   void OnDisconnect() override;
     86 
     87  private:
     88   // Invoked soon after having established the connection with the service.
     89   void OnConnectionInitialized(bool connection_succeeded);
     90 
     91   // Invoked when the remote Service sends an IPC to tell us to do something
     92   // (e.g. start/stop a data source).
     93   void OnServiceRequest(const protos::GetAsyncCommandResponse&);
     94 
     95   // TODO think to destruction order, do we rely on any specific dtor sequence?
     96   Producer* const producer_;
     97   base::TaskRunner* const task_runner_;
     98 
     99   // The object that owns the client socket and takes care of IPC traffic.
    100   std::unique_ptr<ipc::Client> ipc_channel_;
    101 
    102   // The proxy interface for the producer port of the service. It is bound
    103   // to |ipc_channel_| and (de)serializes method invocations over the wire.
    104   protos::ProducerPortProxy producer_port_;
    105 
    106   std::unique_ptr<PosixSharedMemory> shared_memory_;
    107   std::unique_ptr<SharedMemoryArbiter> shared_memory_arbiter_;
    108   size_t shared_buffer_page_size_kb_ = 0;
    109   std::set<DataSourceInstanceID> data_sources_setup_;
    110   bool connected_ = false;
    111   std::string const name_;
    112   TracingService::ProducerSMBScrapingMode const smb_scraping_mode_;
    113   PERFETTO_THREAD_CHECKER(thread_checker_)
    114 };
    115 
    116 }  // namespace perfetto
    117 
    118 #endif  // SRC_TRACING_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_
    119