• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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_PROFILING_MEMORY_JAVA_HPROF_PRODUCER_H_
18 #define SRC_PROFILING_MEMORY_JAVA_HPROF_PRODUCER_H_
19 
20 #include <memory>
21 #include <set>
22 #include <vector>
23 
24 #include "perfetto/ext/base/unix_task_runner.h"
25 #include "perfetto/ext/base/weak_ptr.h"
26 #include "perfetto/ext/tracing/core/producer.h"
27 #include "perfetto/ext/tracing/core/tracing_service.h"
28 #include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
29 #include "perfetto/tracing/core/data_source_config.h"
30 #include "perfetto/tracing/core/data_source_descriptor.h"
31 
32 #include "protos/perfetto/config/profiling/java_hprof_config.gen.h"
33 
34 namespace perfetto {
35 namespace profiling {
36 
37 using JavaHprofConfig = protos::gen::JavaHprofConfig;
38 
39 class JavaHprofProducer : public Producer {
40  public:
JavaHprofProducer(base::TaskRunner * task_runner)41   JavaHprofProducer(base::TaskRunner* task_runner)
42       : task_runner_(task_runner), weak_factory_(this) {}
43 
44   // Producer Impl:
45   void OnConnect() override;
46   void OnDisconnect() override;
47   void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&) override;
48   void StartDataSource(DataSourceInstanceID, const DataSourceConfig&) override;
49   void StopDataSource(DataSourceInstanceID) override;
OnTracingSetup()50   void OnTracingSetup() override {}
51   void Flush(FlushRequestID,
52              const DataSourceInstanceID* data_source_ids,
53              size_t num_data_sources) override;
ClearIncrementalState(const DataSourceInstanceID *,size_t)54   void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/,
55                              size_t /*num_data_sources*/) override {}
56   // TODO(fmayer): Refactor once/if we have generic reconnect logic.
57   void ConnectWithRetries(const char* socket_name);
58   void SetProducerEndpoint(
59       std::unique_ptr<TracingService::ProducerEndpoint> endpoint);
60 
61  private:
62   // State of the connection to tracing service (traced).
63   enum State {
64     kNotStarted = 0,
65     kNotConnected,
66     kConnecting,
67     kConnected,
68   };
69 
70   struct DataSource {
71     DataSourceInstanceID id;
72     std::set<pid_t> pids;
73     JavaHprofConfig config;
74   };
75 
76   void ConnectService();
77   void Restart();
78   void ResetConnectionBackoff();
79   void IncreaseConnectionBackoff();
80 
81   void DoContinuousDump(DataSourceInstanceID id, uint32_t dump_interval);
82   static void SignalDataSource(const DataSource& ds);
83 
84   // State of connection to the tracing service.
85   State state_ = kNotStarted;
86   uint32_t connection_backoff_ms_ = 0;
87   const char* producer_sock_name_ = nullptr;
88 
89   base::TaskRunner* const task_runner_;
90   std::unique_ptr<TracingService::ProducerEndpoint> endpoint_;
91 
92   std::map<DataSourceInstanceID, DataSource> data_sources_;
93 
94   base::WeakPtrFactory<JavaHprofProducer> weak_factory_;  // Keep last.
95 };
96 
97 }  // namespace profiling
98 }  // namespace perfetto
99 
100 #endif  // SRC_PROFILING_MEMORY_JAVA_HPROF_PRODUCER_H_
101