xref: /aosp_15_r20/external/perfetto/src/profiling/memory/java_hprof_producer.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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,
56              FlushFlags) override;
ClearIncrementalState(const DataSourceInstanceID *,size_t)57   void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/,
58                              size_t /*num_data_sources*/) override {}
59   // TODO(fmayer): Refactor once/if we have generic reconnect logic.
60   void ConnectWithRetries(const char* socket_name);
61   void SetProducerEndpoint(
62       std::unique_ptr<TracingService::ProducerEndpoint> endpoint);
63 
64  private:
65   // State of the connection to tracing service (traced).
66   enum State {
67     kNotStarted = 0,
68     kNotConnected,
69     kConnecting,
70     kConnected,
71   };
72 
73   class DataSource {
74    public:
75     DataSource(DataSourceConfig ds_config,
76                JavaHprofConfig config,
77                std::vector<std::string> target_cmdlines);
78     void CollectPids();
79     void SendSignal() const;
80 
config()81     const JavaHprofConfig& config() const { return config_; }
ds_config()82     const DataSourceConfig& ds_config() const { return ds_config_; }
83 
84    private:
85     DataSourceConfig ds_config_;
86     JavaHprofConfig config_;
87     std::vector<std::string> target_cmdlines_;
88 
89     std::set<pid_t> pids_;
90   };
91 
92   void ConnectService();
93   void Restart();
94   void ResetConnectionBackoff();
95   void IncreaseConnectionBackoff();
96 
97   void DoContinuousDump(DataSourceInstanceID id, uint32_t dump_interval);
98 
99   // State of connection to the tracing service.
100   State state_ = kNotStarted;
101   uint32_t connection_backoff_ms_ = 0;
102   const char* producer_sock_name_ = nullptr;
103 
104   base::TaskRunner* const task_runner_;
105   std::unique_ptr<TracingService::ProducerEndpoint> endpoint_;
106 
107   std::map<DataSourceInstanceID, DataSource> data_sources_;
108 
109   base::WeakPtrFactory<JavaHprofProducer> weak_factory_;  // Keep last.
110 };
111 
112 }  // namespace profiling
113 }  // namespace perfetto
114 
115 #endif  // SRC_PROFILING_MEMORY_JAVA_HPROF_PRODUCER_H_
116