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 HARDWARE_GOOGLE_PIXEL_PIXELSTATS_MMMETRICSREPORTER_H
18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_MMMETRICSREPORTER_H
19 
20 #include <map>
21 #include <string>
22 
23 #include <aidl/android/frameworks/stats/IStats.h>
24 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
25 
26 namespace android {
27 namespace hardware {
28 namespace google {
29 namespace pixel {
30 
31 using aidl::android::frameworks::stats::IStats;
32 using aidl::android::frameworks::stats::VendorAtomValue;
33 
34 /**
35  * A class to upload Pixel MM health metrics
36  */
37 class MmMetricsReporter {
38   public:
39     MmMetricsReporter();
40     void logPixelMmMetricsPerHour(const std::shared_ptr<IStats> &stats_client);
41     void logPixelMmMetricsPerDay(const std::shared_ptr<IStats> &stats_client);
42     void logCmaStatus(const std::shared_ptr<IStats> &stats_client);
43 
44   private:
45     struct MmMetricsInfo {
46         std::string name;
47         int atom_key;
48         bool update_diff;
49     };
50 
51     enum CmaType {
52         FARAWIMG = 0,
53         FAIMG = 1,
54         FATPU = 2,
55         FAPREV = 3,
56         VFRAME = 4,
57         VSTREAM = 5,
58     };
59 
60     static const std::vector<MmMetricsInfo> kMmMetricsPerHourInfo;
61     static const std::vector<MmMetricsInfo> kMmMetricsPerDayInfo;
62     static const std::vector<MmMetricsInfo> kCmaStatusInfo;
63     static const std::vector<MmMetricsInfo> kCmaStatusExtInfo;
64     static const std::map<std::string, CmaType> kCmaTypeInfo;
65 
66     bool ReadFileToUint(const char *const path, uint64_t *val);
67     bool reportVendorAtom(const std::shared_ptr<IStats> &stats_client, int atom_id,
68                           const std::vector<VendorAtomValue> &values, const std::string &atom_name);
69     std::map<std::string, uint64_t> readVmStat(const char *path);
70     uint64_t getIonTotalPools();
71     uint64_t getGpuMemory();
72     void fillAtomValues(const std::vector<MmMetricsInfo> &metrics_info,
73                         const std::map<std::string, uint64_t> &mm_metrics,
74                         std::map<std::string, uint64_t> *prev_mm_metrics,
75                         std::vector<VendorAtomValue> *atom_values);
76     bool isValidPid(int pid, const char *name);
77     int findPidByProcessName(const char *name);
78     uint64_t getStimeByPid(int pid);
79     void fillProcessStime(int atom_key, const char *name, int *pid, uint64_t *prev_stime,
80                           std::vector<VendorAtomValue> *atom_values);
81     std::map<std::string, uint64_t> readCmaStat(const std::string &cma_type,
82                                                 const std::vector<MmMetricsInfo> &metrics_info);
83     void reportCmaStatusAtom(const std::shared_ptr<IStats> &stats_client, int atom_id,
84                              const std::string &cma_type, CmaType type_idx,
85                              const std::vector<MmMetricsInfo> &metrics_info,
86                              std::map<CmaType, std::map<std::string, uint64_t>> *all_prev_cma_stat);
87 
88     const char *const kVmstatPath;
89     const char *const kIonTotalPoolsPath;
90     const char *const kIonTotalPoolsPathForLegacy;
91     const char *const kGpuTotalPages;
92     const char *const kPixelStatMm;
93     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
94     // store everything in the values array at the index of the field number
95     // -2.
96     const int kVendorAtomOffset = 2;
97 
98     std::map<std::string, uint64_t> prev_hour_vmstat_;
99     std::map<std::string, uint64_t> prev_day_vmstat_;
100     std::map<std::string, uint64_t> prev_day_pixel_vmstat_;
101     std::map<CmaType, std::map<std::string, uint64_t>> prev_cma_stat_;
102     std::map<CmaType, std::map<std::string, uint64_t>> prev_cma_stat_ext_;
103     int kswapd_pid_ = -1;
104     int kcompactd_pid_ = -1;
105     uint64_t prev_kswapd_stime_ = 0;
106     uint64_t prev_kcompactd_stime_ = 0;
107 };
108 
109 }  // namespace pixel
110 }  // namespace google
111 }  // namespace hardware
112 }  // namespace android
113 
114 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_BATTERYCAPACITYREPORTER_H
115