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 #include "BatteryEEPROMReporter.h"
23 #include "MitigationStatsReporter.h"
24 #include "MmMetricsReporter.h"
25 
26 namespace android {
27 namespace hardware {
28 namespace google {
29 namespace pixel {
30 
31 using aidl::android::frameworks::stats::IStats;
32 using android::hardware::google::pixel::PixelAtoms::VendorSlowIo;
33 
34 class SysfsCollector {
35   public:
36     struct SysfsPaths {
37         const char *const SlowioReadCntPath;
38         const char *const SlowioWriteCntPath;
39         const char *const SlowioUnmapCntPath;
40         const char *const SlowioSyncCntPath;
41         const char *const CycleCountBinsPath;
42         const char *const ImpedancePath;
43         const char *const CodecPath;
44         const char *const Codec1Path;
45         const char *const SpeechDspPath;
46         const char *const BatteryCapacityCC;
47         const char *const BatteryCapacityVFSOC;
48         const char *const UFSLifetimeA;
49         const char *const UFSLifetimeB;
50         const char *const UFSLifetimeC;
51         const char *const F2fsStatsPath;
52         const char *const UserdataBlockProp;
53         const char *const ZramMmStatPath;
54         const char *const ZramBdStatPath;
55         const char *const EEPROMPath;
56         const char *const MitigationPath;
57         const char *const SpeakerTemperaturePath;
58         const char *const SpeakerExcursionPath;
59         const char *const SpeakerHeartBeatPath;
60         const std::vector<std::string> UFSErrStatsPath;
61     };
62 
63     SysfsCollector(const struct SysfsPaths &paths);
64     void collect();
65 
66   private:
67     bool ReadFileToInt(const std::string &path, int *val);
68     bool ReadFileToInt(const char *path, int *val);
69     void logPerDay();
70     void logPerHour();
71 
72     void logBatteryChargeCycles(const std::shared_ptr<IStats> &stats_client);
73     void logCodecFailed(const std::shared_ptr<IStats> &stats_client);
74     void logCodec1Failed(const std::shared_ptr<IStats> &stats_client);
75     void logSlowIO(const std::shared_ptr<IStats> &stats_client);
76     void logSpeakerImpedance(const std::shared_ptr<IStats> &stats_client);
77     void logSpeechDspStat(const std::shared_ptr<IStats> &stats_client);
78     void logBatteryCapacity(const std::shared_ptr<IStats> &stats_client);
79     void logUFSLifetime(const std::shared_ptr<IStats> &stats_client);
80     void logUFSErrorStats(const std::shared_ptr<IStats> &stats_client);
81     void logF2fsStats(const std::shared_ptr<IStats> &stats_client);
82     void logF2fsCompressionInfo(const std::shared_ptr<IStats> &stats_client);
83     void logF2fsGcSegmentInfo(const std::shared_ptr<IStats> &stats_client);
84     void logZramStats(const std::shared_ptr<IStats> &stats_client);
85     void logBootStats(const std::shared_ptr<IStats> &stats_client);
86     void logBatteryEEPROM(const std::shared_ptr<IStats> &stats_client);
87     void logSpeakerHealthStats(const std::shared_ptr<IStats> &stats_client);
88 
89     void reportSlowIoFromFile(const std::shared_ptr<IStats> &stats_client, const char *path,
90                               const VendorSlowIo::IoOperation &operation_s);
91     void reportZramMmStat(const std::shared_ptr<IStats> &stats_client);
92     void reportZramBdStat(const std::shared_ptr<IStats> &stats_client);
93     int getReclaimedSegments(const std::string &mode);
94 
95     const char *const kSlowioReadCntPath;
96     const char *const kSlowioWriteCntPath;
97     const char *const kSlowioUnmapCntPath;
98     const char *const kSlowioSyncCntPath;
99     const char *const kCycleCountBinsPath;
100     const char *const kImpedancePath;
101     const char *const kCodecPath;
102     const char *const kCodec1Path;
103     const char *const kSpeechDspPath;
104     const char *const kBatteryCapacityCC;
105     const char *const kBatteryCapacityVFSOC;
106     const char *const kUFSLifetimeA;
107     const char *const kUFSLifetimeB;
108     const char *const kUFSLifetimeC;
109     const char *const kF2fsStatsPath;
110     const char *const kZramMmStatPath;
111     const char *const kZramBdStatPath;
112     const char *const kEEPROMPath;
113     const char *const kPowerMitigationStatsPath;
114     const char *const kSpeakerTemperaturePath;
115     const char *const kSpeakerExcursionPath;
116     const char *const kSpeakerHeartbeatPath;
117     const std::vector<std::string> kUFSErrStatsPath;
118 
119     BatteryEEPROMReporter battery_EEPROM_reporter_;
120     MmMetricsReporter mm_metrics_reporter_;
121     MitigationStatsReporter mitigation_stats_reporter_;
122 
123     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
124     // store everything in the values array at the index of the field number
125     // -2.
126     const int kVendorAtomOffset = 2;
127 
128     bool log_once_reported = false;
129     int64_t prev_huge_pages_since_boot_ = -1;
130 };
131 
132 }  // namespace pixel
133 }  // namespace google
134 }  // namespace hardware
135 }  // namespace android
136 
137 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_SYSFSCOLLECTOR_H
138