• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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_ANDROID_KERNEL_WAKELOCKS_ANDROID_KERNEL_WAKELOCKS_DATA_SOURCE_H_
18 #define SRC_TRACED_PROBES_ANDROID_KERNEL_WAKELOCKS_ANDROID_KERNEL_WAKELOCKS_DATA_SOURCE_H_
19 
20 #include <memory>
21 
22 #include "perfetto/ext/base/flat_hash_map.h"
23 #include "perfetto/ext/base/scoped_file.h"
24 #include "perfetto/ext/base/weak_ptr.h"
25 #include "perfetto/tracing/core/data_source_config.h"
26 #include "src/traced/probes/probes_data_source.h"
27 
28 namespace perfetto {
29 
30 struct KernelWakelockInfo;
31 
32 class TraceWriter;
33 namespace base {
34 class TaskRunner;
35 }
36 
37 class AndroidKernelWakelocksDataSource : public ProbesDataSource {
38  public:
39   static const ProbesDataSource::Descriptor descriptor;
40 
41   AndroidKernelWakelocksDataSource(const DataSourceConfig&,
42                                    base::TaskRunner*,
43                                    TracingSessionID,
44                                    std::unique_ptr<TraceWriter> writer);
45 
46   ~AndroidKernelWakelocksDataSource() override;
47 
48   // ProbesDataSource implementation.
49   void Start() override;
50   void Flush(FlushRequestID, std::function<void()> callback) override;
51   void ClearIncrementalState() override;
52 
53  private:
54   struct DynamicLibLoader;
55 
56   void Tick();
57   void WriteKernelWakelocks();
58   base::WeakPtr<AndroidKernelWakelocksDataSource> GetWeakPtr() const;
59 
60   uint32_t poll_interval_ms_ = 0;
61   base::FlatHashMap<std::string, KernelWakelockInfo> wakelocks_;
62   uint32_t next_id_ = 0;
63 
64   base::TaskRunner* const task_runner_;
65   std::unique_ptr<TraceWriter> writer_;
66   std::unique_ptr<DynamicLibLoader> lib_;
67   base::WeakPtrFactory<AndroidKernelWakelocksDataSource>
68       weak_factory_;  // Keep last.
69 };
70 
71 }  // namespace perfetto
72 
73 #endif  // SRC_TRACED_PROBES_ANDROID_KERNEL_WAKELOCKS_ANDROID_KERNEL_WAKELOCKS_DATA_SOURCE_H_
74