xref: /aosp_15_r20/external/perfetto/src/traced/probes/statsd_client/statsd_binder_data_source.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2023 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_TRACED_PROBES_STATSD_CLIENT_STATSD_BINDER_DATA_SOURCE_H_
18 #define SRC_TRACED_PROBES_STATSD_CLIENT_STATSD_BINDER_DATA_SOURCE_H_
19 
20 #include <memory>
21 
22 #include "perfetto/base/task_runner.h"
23 #include "perfetto/ext/base/utils.h"
24 #include "perfetto/ext/base/weak_ptr.h"
25 #include "perfetto/ext/protozero/proto_ring_buffer.h"
26 #include "perfetto/ext/tracing/core/basic_types.h"
27 #include "perfetto/ext/tracing/core/trace_writer.h"
28 #include "perfetto/tracing/core/forward_decls.h"
29 #include "src/traced/probes/probes_data_source.h"
30 
31 namespace perfetto {
32 
33 class StatsdBinderDataSource : public ProbesDataSource {
34  public:
35   static const ProbesDataSource::Descriptor descriptor;
36 
37   StatsdBinderDataSource(base::TaskRunner*,
38                          TracingSessionID,
39                          std::unique_ptr<TraceWriter> writer,
40                          const DataSourceConfig&);
41   ~StatsdBinderDataSource() override;
42 
43   // ProbesDataSource implementation.
44   void Start() override;
45   void Flush(FlushRequestID, std::function<void()> callback) override;
46   void ClearIncrementalState() override;
47 
48   void OnData(uint32_t reason, const uint8_t* data, size_t sz);
49 
50  private:
51   StatsdBinderDataSource(const StatsdBinderDataSource&) = delete;
52   StatsdBinderDataSource& operator=(const StatsdBinderDataSource&) = delete;
53 
54   base::TaskRunner* const task_runner_;
55   std::unique_ptr<TraceWriter> writer_;
56   std::string shell_subscription_;
57   int32_t subscription_id_ = -1;
58   std::function<void()> pending_flush_callback_;
59 
60   base::WeakPtrFactory<StatsdBinderDataSource> weak_factory_;  // Keep last.
61 };
62 
63 }  // namespace perfetto
64 
65 #endif  // SRC_TRACED_PROBES_STATSD_CLIENT_STATSD_BINDER_DATA_SOURCE_H_
66