1 /*
2  * Copyright (C) 2024 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_TEST_MOCKMMMETRICS_H
18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_TEST_MOCKMMMETRICS_H
19 
20 #include <android-base/stringprintf.h>
21 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
22 #include <sys/stat.h>
23 
24 #include <map>
25 #include <string>
26 
27 #include "pixelstats/MmMetricsReporter.h"
28 
29 namespace android {
30 namespace hardware {
31 namespace google {
32 namespace pixel {
33 
34 using aidl::android::frameworks::stats::IStats;
35 using android::hardware::google::pixel::PixelAtoms::VendorSlowIo;
36 
37 /**
38  * mock version of MmMetricsReporter class
39  * Testing on the mock version only
40  */
41 class MockMmMetricsReporter : public MmMetricsReporter {
42   public:
MockMmMetricsReporter()43     MockMmMetricsReporter() : MmMetricsReporter() {}
~MockMmMetricsReporter()44     virtual ~MockMmMetricsReporter() {}
setBasePath(const std::string & path)45     void setBasePath(const std::string &path) { base_path_ = path; }
46 
47   private:
48     /**
49      * This is the base path of the following map (see getSysfsPath() below).
50      *
51      * The test code can modify this path (by setBasePath()) for redirecting
52      * the sysfs read to a set test data files.  Since one sysfs node could
53      * be read multiple times (e.g. create and the diff), the test
54      * code can use this base_path_ to select which set of test data files
55      * to read.
56      */
57     std::string base_path_;
58 
59     /**
60      * map (redirect) the sysfs node read path to the test data file
61      * for test data injection.
62      */
63     const std::map<const std::string, const char *> mock_path_map = {
64             {"/sys/kernel/pixel_stat/mm/compaction/mm_compaction_duration", "compaction_duration"},
65             {"/sys/kernel/pixel_stat/mm/vmscan/direct_reclaim/native/latency_stat",
66              "direct_reclaim_native_latency_stat"},
67             {"/sys/kernel/pixel_stat/mm/vmscan/direct_reclaim/other/latency_stat",
68              "direct_reclaim_other_latency_stat"},
69             {"/sys/kernel/pixel_stat/mm/vmscan/direct_reclaim/top/latency_stat",
70              "direct_reclaim_top_latency_stat"},
71             {"/sys/kernel/pixel_stat/mm/vmscan/direct_reclaim/visible/latency_stat",
72              "direct_reclaim_visible_latency_stat"},
73             {"/sys/kernel/dma_heap/total_pools_kb", "dma_heap_total_pools"},
74             {"/sys/kernel/pixel_stat/gpu/mem/total_page_count", "gpu_pages"},
75             {"/sys/kernel/ion/total_pools_kb", "ion_total_pools"},
76             {"/sys/kernel/pixel_stat/mm/vmstat", "pixel_vmstat"},
77             {"/proc/meminfo", "proc_meminfo"},
78             {"/proc/stat", "proc_stat"},
79             {"/proc/vmstat", "proc_vmstat"},
80             {"/proc/pressure/cpu", "psi_cpu"},
81             {"/proc/pressure/io", "psi_io"},
82             {"/proc/pressure/memory", "psi_memory"},
83             {"kswapd0", "kswapd0_stat"},
84             {"kcompactd0", "kcompactd0_stat"},
85             {"/proc/vendor_mm/memory_usage_by_oom_score", "oom_mm_usage"},
86             {"/sys/kernel/vendor_mm/gcma/cached", "gcma_cached"},
87             {"/sys/kernel/vendor_mm/gcma/discarded", "gcma_discarded"},
88             {"/sys/kernel/vendor_mm/gcma/evicted", "gcma_evicted"},
89             {"/sys/kernel/vendor_mm/gcma/loaded", "gcma_loaded"},
90             {"/sys/kernel/vendor_mm/gcma/stored", "gcma_stored"},
91             {"/sys/kernel/vendor_mm/gcma/latency_low", "gcma_latency_low"},
92             {"/sys/kernel/vendor_mm/gcma/latency_mid", "gcma_latency_mid"},
93             {"/sys/kernel/vendor_mm/gcma/latency_high", "gcma_latency_high"},
94             {"/sys/kernel/vendor_mm/gcma/latency_extreme_high", "gcma_latency_extreme_high"},
95     };
96 
getSysfsPath(const std::string & path)97     virtual std::string getSysfsPath(const std::string &path) {
98         std::string ret(base_path_ + '/');
99         if (mock_path_map.find(path) == mock_path_map.end()) {
100             /*
101              * This mapped file won't exist in the test directory,
102              * so this effectively emulates a 'file-not-found' condition
103              * for testing the failed cases.
104              */
105             return ret + "not_found";
106         } else {
107             return ret + mock_path_map.at(path);
108         }
109     }
110 
getProcessStatPath(const std::string & name,int * prev_pid)111     virtual std::string getProcessStatPath(const std::string &name, int *prev_pid) {
112         (void)(prev_pid);  // unused parameter
113         return getSysfsPath(name);
114     }
115 };
116 
117 }  // namespace pixel
118 }  // namespace google
119 }  // namespace hardware
120 }  // namespace android
121 
122 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_TEST_MOCKMMMETRICS_H
123