• 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/basic_types.h"
27 #include "perfetto/ext/tracing/core/producer.h"
28 #include "perfetto/ext/tracing/core/tracing_service.h"
29 #include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
30 #include "perfetto/tracing/core/data_source_config.h"
31 #include "perfetto/tracing/core/data_source_descriptor.h"
32 
33 #include "perfetto/tracing/core/forward_decls.h"
34 #include "protos/perfetto/config/profiling/java_hprof_config.gen.h"
35 
36 namespace perfetto {
37 namespace profiling {
38 
39 using JavaHprofConfig = protos::gen::JavaHprofConfig;
40 
41 class JavaHprofProducer : public Producer {
42  public:
JavaHprofProducer(base::TaskRunner * task_runner)43   explicit JavaHprofProducer(base::TaskRunner* task_runner)
44       : task_runner_(task_runner), weak_factory_(this) {}
45 
46   // Producer Impl:
47   void OnConnect() override;
48   void OnDisconnect() override;
49   void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&) override;
50   void StartDataSource(DataSourceInstanceID, const DataSourceConfig&) override;
51   void StopDataSource(DataSourceInstanceID) override;
OnTracingSetup()52   void OnTracingSetup() override {}
53   void Flush(FlushRequestID,
54              const DataSourceInstanceID* data_source_ids,
55              size_t num_data_sources) override;
ClearIncrementalState(const DataSourceInstanceID *,size_t)56   void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/,
57                              size_t /*num_data_sources*/) override {}
58   // TODO(fmayer): Refactor once/if we have generic reconnect logic.
59   void ConnectWithRetries(const char* socket_name);
60   void SetProducerEndpoint(
61       std::unique_ptr<TracingService::ProducerEndpoint> endpoint);
62 
63  private:
64   // State of the connection to tracing service (traced).
65   enum State {
66     kNotStarted = 0,
67     kNotConnected,
68     kConnecting,
69     kConnected,
70   };
71 
72   class DataSource {
73    public:
74     DataSource(DataSourceConfig ds_config,
75                JavaHprofConfig config,
76                std::vector<std::string> target_cmdlines);
77     void CollectPids();
78     void SendSignal() const;
79 
config()80     const JavaHprofConfig& config() const { return config_; }
ds_config()81     const DataSourceConfig& ds_config() const { return ds_config_; }
82 
83    private:
84     DataSourceConfig ds_config_;
85     JavaHprofConfig config_;
86     std::vector<std::string> target_cmdlines_;
87 
88     std::set<pid_t> pids_;
89   };
90 
91   void ConnectService();
92   void Restart();
93   void ResetConnectionBackoff();
94   void IncreaseConnectionBackoff();
95 
96   void DoContinuousDump(DataSourceInstanceID id, uint32_t dump_interval);
97 
98   // State of connection to the tracing service.
99   State state_ = kNotStarted;
100   uint32_t connection_backoff_ms_ = 0;
101   const char* producer_sock_name_ = nullptr;
102 
103   base::TaskRunner* const task_runner_;
104   std::unique_ptr<TracingService::ProducerEndpoint> endpoint_;
105 
106   std::map<DataSourceInstanceID, DataSource> data_sources_;
107 
108   base::WeakPtrFactory<JavaHprofProducer> weak_factory_;  // Keep last.
109 };
110 
111 }  // namespace profiling
112 }  // namespace perfetto
113 
114 #endif  // SRC_PROFILING_MEMORY_JAVA_HPROF_PRODUCER_H_
115