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