1 /*
2  * Copyright (C) 2018 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 HARDWARE_GOOGLE_PIXEL_PIXELSTATS_SYSFSCOLLECTOR_H
18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_SYSFSCOLLECTOR_H
19 
20 #include <aidl/android/frameworks/stats/IStats.h>
21 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
22 
23 #include "BatteryEEPROMReporter.h"
24 #include "BatteryHealthReporter.h"
25 #include "BatteryTTFReporter.h"
26 #include "BrownoutDetectedReporter.h"
27 #include "DisplayStatsReporter.h"
28 #include "MitigationDurationReporter.h"
29 #include "MitigationStatsReporter.h"
30 #include "MmMetricsReporter.h"
31 #include "TempResidencyReporter.h"
32 #include "ThermalStatsReporter.h"
33 #include "WaterEventReporter.h"
34 
35 namespace android {
36 namespace hardware {
37 namespace google {
38 namespace pixel {
39 
40 using aidl::android::frameworks::stats::IStats;
41 using android::hardware::google::pixel::PixelAtoms::VendorSlowIo;
42 
43 class SysfsCollector {
44   public:
45     struct SysfsPaths {
46         const char *const SlowioReadCntPath;
47         const char *const SlowioWriteCntPath;
48         const char *const SlowioUnmapCntPath;
49         const char *const SlowioSyncCntPath;
50         const char *const CycleCountBinsPath;
51         const char *const ImpedancePath;
52         const char *const CodecPath;
53         const char *const Codec1Path;
54         const char *const SpeechDspPath;
55         const char *const BatteryCapacityCC;
56         const char *const BatteryCapacityVFSOC;
57         const char *const UFSLifetimeA;
58         const char *const UFSLifetimeB;
59         const char *const UFSLifetimeC;
60         const char *const F2fsStatsPath;
61         const char *const UserdataBlockProp;
62         const char *const ZramMmStatPath;
63         const char *const ZramBdStatPath;
64         const char *const EEPROMPath;
65         const char *const MitigationPath;
66         const char *const MitigationDurationPath;
67         const char *const BrownoutCsvPath;
68         const char *const BrownoutLogPath;
69         const char *const BrownoutReasonProp;
70         const char *const SpeakerTemperaturePath;
71         const char *const SpeakerExcursionPath;
72         const char *const SpeakerHeartBeatPath;
73         const std::vector<std::string> UFSErrStatsPath;
74         const int BlockStatsLength;
75         const char *const AmsRatePath;
76         const std::vector<std::string> ThermalStatsPaths;
77         const std::vector<std::string> DisplayStatsPaths;
78         const std::vector<std::string> DisplayPortStatsPaths;
79         const std::vector<std::string> DisplayPortDSCStatsPaths;
80         const std::vector<std::string> DisplayPortMaxResolutionStatsPaths;
81         const std::vector<std::string> HDCPStatsPaths;
82         const char *const CCARatePath;
83         const std::vector<std::pair<std::string, std::string>> TempResidencyAndResetPaths;
84         const char *const LongIRQMetricsPath;
85         const char *const StormIRQMetricsPath;
86         const char *const IRQStatsResetPath;
87         const char *const ResumeLatencyMetricsPath;
88         const char *const ModemPcieLinkStatsPath;
89         const char *const WifiPcieLinkStatsPath;
90         const char *const PDMStatePath;
91         const char *const WavesPath;
92         const char *const AdaptedInfoCountPath;
93         const char *const AdaptedInfoDurationPath;
94         const char *const PcmLatencyPath;
95         const char *const PcmCountPath;
96         const char *const TotalCallCountPath;
97         const char *const OffloadEffectsIdPath;
98         const char *const OffloadEffectsDurationPath;
99         const char *const BluetoothAudioUsagePath;
100         const std::vector<std::string> GMSRPath;
101         const std::vector<std::string> FGModelLoadingPath;
102         const std::vector<std::string> FGLogBufferPath;
103         const char *const SpeakerVersionPath;
104         const char *const WaterEventPath;
105     };
106 
107     SysfsCollector(const struct SysfsPaths &paths);
108     void collect();
109 
110   private:
111     bool ReadFileToInt(const std::string &path, int *val);
112     bool ReadFileToInt(const char *path, int *val);
113     void aggregatePer5Min();
114     void logOnce();
115     void logBrownout();
116     void logWater();
117     void logPerDay();
118     void logPerHour();
119 
120     void logBatteryChargeCycles(const std::shared_ptr<IStats> &stats_client);
121     void logBatteryHealth(const std::shared_ptr<IStats> &stats_client);
122     void logBatteryTTF(const std::shared_ptr<IStats> &stats_client);
123     void logBlockStatsReported(const std::shared_ptr<IStats> &stats_client);
124     void logCodecFailed(const std::shared_ptr<IStats> &stats_client);
125     void logCodec1Failed(const std::shared_ptr<IStats> &stats_client);
126     void logSlowIO(const std::shared_ptr<IStats> &stats_client);
127     void logSpeakerImpedance(const std::shared_ptr<IStats> &stats_client);
128     void logSpeechDspStat(const std::shared_ptr<IStats> &stats_client);
129     void logBatteryCapacity(const std::shared_ptr<IStats> &stats_client);
130     void logUFSLifetime(const std::shared_ptr<IStats> &stats_client);
131     void logUFSErrorStats(const std::shared_ptr<IStats> &stats_client);
132     void logF2fsStats(const std::shared_ptr<IStats> &stats_client);
133     void logF2fsAtomicWriteInfo(const std::shared_ptr<IStats> &stats_client);
134     void logF2fsCompressionInfo(const std::shared_ptr<IStats> &stats_client);
135     void logF2fsGcSegmentInfo(const std::shared_ptr<IStats> &stats_client);
136     void logZramStats(const std::shared_ptr<IStats> &stats_client);
137     void logBootStats(const std::shared_ptr<IStats> &stats_client);
138     void logBatteryEEPROM(const std::shared_ptr<IStats> &stats_client);
139     void logSpeakerHealthStats(const std::shared_ptr<IStats> &stats_client);
140     void logF2fsSmartIdleMaintEnabled(const std::shared_ptr<IStats> &stats_client);
141     void logThermalStats(const std::shared_ptr<IStats> &stats_client);
142     void logMitigationDurationCounts(const std::shared_ptr<IStats> &stats_client);
143     void logDisplayStats(const std::shared_ptr<IStats> &stats_client);
144     void logDisplayPortStats(const std::shared_ptr<IStats> &stats_client);
145     void logDisplayPortDSCStats(const std::shared_ptr<IStats> &stats_client);
146     void logDisplayPortMaxResolutionStats(const std::shared_ptr<IStats> &stats_client);
147     void logHDCPStats(const std::shared_ptr<IStats> &stats_client);
148     void logVendorAudioPdmStatsReported(const std::shared_ptr<IStats> &stats_client);
149 
150     void reportSlowIoFromFile(const std::shared_ptr<IStats> &stats_client, const char *path,
151                               const VendorSlowIo::IoOperation &operation_s);
152     void logTempResidencyStats(const std::shared_ptr<IStats> &stats_client);
153     void reportZramMmStat(const std::shared_ptr<IStats> &stats_client);
154     void reportZramBdStat(const std::shared_ptr<IStats> &stats_client);
155     int getReclaimedSegments(const std::string &mode);
156     void logVendorAudioHardwareStats(const std::shared_ptr<IStats> &stats_client);
157     void logVendorLongIRQStatsReported(const std::shared_ptr<IStats> &stats_client);
158     void logVendorResumeLatencyStats(const std::shared_ptr<IStats> &stats_client);
159     void logPartitionUsedSpace(const std::shared_ptr<IStats> &stats_client);
160     void logPcieLinkStats(const std::shared_ptr<IStats> &stats_client);
161     void logWavesStats(const std::shared_ptr<IStats> &stats_client);
162     void logAdaptedInfoStats(const std::shared_ptr<IStats> &stats_client);
163     void logPcmUsageStats(const std::shared_ptr<IStats> &stats_client);
164     void logOffloadEffectsStats(const std::shared_ptr<IStats> &stats_client);
165     void logBluetoothAudioUsage(const std::shared_ptr<IStats> &stats_client);
166     void logBatteryGMSR(const std::shared_ptr<IStats> &stats_client);
167     void logDmVerityPartitionReadAmount(const std::shared_ptr<IStats> &stats_client);
168     void logBatteryHistoryValidation();
169 
170     const char *const kSlowioReadCntPath;
171     const char *const kSlowioWriteCntPath;
172     const char *const kSlowioUnmapCntPath;
173     const char *const kSlowioSyncCntPath;
174     const char *const kCycleCountBinsPath;
175     const char *const kImpedancePath;
176     const char *const kCodecPath;
177     const char *const kCodec1Path;
178     const char *const kSpeechDspPath;
179     const char *const kBatteryCapacityCC;
180     const char *const kBatteryCapacityVFSOC;
181     const char *const kUFSLifetimeA;
182     const char *const kUFSLifetimeB;
183     const char *const kUFSLifetimeC;
184     const char *const kF2fsStatsPath;
185     const char *const kZramMmStatPath;
186     const char *const kZramBdStatPath;
187     const char *const kEEPROMPath;
188     const char *const kBrownoutCsvPath;
189     const char *const kBrownoutLogPath;
190     const char *const kBrownoutReasonProp;
191     const char *const kPowerMitigationStatsPath;
192     const char *const kPowerMitigationDurationPath;
193     const char *const kSpeakerTemperaturePath;
194     const char *const kSpeakerExcursionPath;
195     const char *const kSpeakerHeartbeatPath;
196     const std::vector<std::string> kUFSErrStatsPath;
197     const int kBlockStatsLength;
198     const char *const kAmsRatePath;
199     const std::vector<std::string> kThermalStatsPaths;
200     const char *const kCCARatePath;
201     const std::vector<std::pair<std::string, std::string>> kTempResidencyAndResetPaths;
202     const char *const kLongIRQMetricsPath;
203     const char *const kStormIRQMetricsPath;
204     const char *const kIRQStatsResetPath;
205     const char *const kResumeLatencyMetricsPath;
206     const char *const kModemPcieLinkStatsPath;
207     const char *const kWifiPcieLinkStatsPath;
208     const std::vector<std::string> kDisplayStatsPaths;
209     const std::vector<std::string> kDisplayPortStatsPaths;
210     const std::vector<std::string> kDisplayPortDSCStatsPaths;
211     const std::vector<std::string> kDisplayPortMaxResolutionStatsPaths;
212     const std::vector<std::string> kHDCPStatsPaths;
213     const char *const kPDMStatePath;
214     const char *const kWavesPath;
215     const char *const kAdaptedInfoCountPath;
216     const char *const kAdaptedInfoDurationPath;
217     const char *const kPcmLatencyPath;
218     const char *const kPcmCountPath;
219     const char *const kTotalCallCountPath;
220     const char *const kOffloadEffectsIdPath;
221     const char *const kOffloadEffectsDurationPath;
222     const char *const kBluetoothAudioUsagePath;
223     const std::vector<std::string> kGMSRPath;
224     const char *const kMaxfgHistoryPath;
225     const std::vector<std::string> kFGModelLoadingPath;
226     const std::vector<std::string> kFGLogBufferPath;
227     const char *const kSpeakerVersionPath;
228     const char *const kWaterEventPath;
229 
230     BatteryEEPROMReporter battery_EEPROM_reporter_;
231     MmMetricsReporter mm_metrics_reporter_;
232     MitigationStatsReporter mitigation_stats_reporter_;
233     MitigationDurationReporter mitigation_duration_reporter_;
234     BrownoutDetectedReporter brownout_detected_reporter_;
235     ThermalStatsReporter thermal_stats_reporter_;
236     DisplayStatsReporter display_stats_reporter_;
237     BatteryHealthReporter battery_health_reporter_;
238     BatteryTTFReporter battery_time_to_full_reporter_;
239     TempResidencyReporter temp_residency_reporter_;
240     WaterEventReporter water_event_reporter_;
241     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
242     // store everything in the values array at the index of the field number    // -2.
243     const int kVendorAtomOffset = 2;
244 
245     bool log_once_reported = false;
246     int64_t prev_huge_pages_since_boot_ = -1;
247 
248     struct perf_metrics_data {
249         uint64_t resume_latency_sum_ms;
250         int64_t resume_count;
251         std::vector<int64_t> resume_latency_buckets;
252         int bucket_cnt;
253     };
254     struct perf_metrics_data prev_data;
255     const int kMaxResumeLatencyBuckets = 36;
256 };
257 
258 }  // namespace pixel
259 }  // namespace google
260 }  // namespace hardware
261 }  // namespace android
262 
263 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_SYSFSCOLLECTOR_H
264