xref: /aosp_15_r20/external/perfetto/src/traced/probes/power/linux_power_sysfs_data_source.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2021 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_POWER_LINUX_POWER_SYSFS_DATA_SOURCE_H_
18 #define SRC_TRACED_PROBES_POWER_LINUX_POWER_SYSFS_DATA_SOURCE_H_
19 
20 #include <optional>
21 
22 #include "perfetto/ext/base/weak_ptr.h"
23 #include "perfetto/tracing/core/data_source_config.h"
24 #include "src/traced/probes/probes_data_source.h"
25 
26 namespace perfetto {
27 class BatteryInfo;
28 class TraceWriter;
29 
30 namespace base {
31 class TaskRunner;
32 }
33 
34 class LinuxPowerSysfsDataSource : public ProbesDataSource {
35  public:
36   class BatteryInfo {
37    public:
38     explicit BatteryInfo(
39         const char* power_supply_dir_path = "/sys/class/power_supply");
40     ~BatteryInfo();
41 
42     // The current coloumb counter value in µAh.
43     std::optional<int64_t> GetChargeCounterUah(size_t battery_idx);
44 
45     // The current energy counter in µWh.
46     std::optional<int64_t> GetEnergyCounterUah(size_t battery_idx);
47 
48     // The voltage in µV.
49     std::optional<int64_t> GetVoltageUv(size_t battery_idx);
50 
51     // The battery capacity in percent.
52     std::optional<int64_t> GetCapacityPercent(size_t battery_idx);
53 
54     // The current reading of the battery in µA.
55     std::optional<int64_t> GetCurrentNowUa(size_t battery_idx);
56 
57     // The smoothed current reading of the battery in µA.
58     std::optional<int64_t> GetAverageCurrentUa(size_t battery_idx);
59 
60     // Name of the battery.
61     std::string GetBatteryName(size_t battery_idx);
62 
63     size_t num_batteries() const;
64 
65    private:
66     std::string power_supply_dir_path_;
67     // The subdirectories that contain info of a battery power supply, e.g.
68     // BAT0.
69     std::vector<std::string> sysfs_battery_subdirs_;
70   };
71   static const ProbesDataSource::Descriptor descriptor;
72 
73   LinuxPowerSysfsDataSource(DataSourceConfig,
74                             base::TaskRunner*,
75                             TracingSessionID,
76                             std::unique_ptr<TraceWriter> writer);
77 
78   ~LinuxPowerSysfsDataSource() override;
79 
80   base::WeakPtr<LinuxPowerSysfsDataSource> GetWeakPtr() const;
81 
82   // ProbesDataSource implementation.
83   void Start() override;
84   void Flush(FlushRequestID, std::function<void()> callback) override;
85   // Use the default ClearIncrementalState() implementation: this data source
86   // doesn't have any incremental state.
87 
88  private:
89   void Tick();
90   void WriteBatteryCounters();
91 
92   uint32_t poll_interval_ms_ = 0;
93 
94   base::TaskRunner* const task_runner_;
95   std::unique_ptr<TraceWriter> writer_;
96   std::unique_ptr<BatteryInfo> battery_info_;
97   base::WeakPtrFactory<LinuxPowerSysfsDataSource> weak_factory_;  // Keep last.
98 };
99 
100 }  // namespace perfetto
101 
102 #endif  // SRC_TRACED_PROBES_POWER_LINUX_POWER_SYSFS_DATA_SOURCE_H_
103