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     };
86 
getSysfsPath(const std::string & path)87     virtual std::string getSysfsPath(const std::string &path) {
88         return base_path_ + "/" + mock_path_map.at(path);
89     }
90 
getProcessStatPath(const std::string & name,int * prev_pid)91     virtual std::string getProcessStatPath(const std::string &name, int *prev_pid) {
92         (void)(prev_pid);  // unused parameter
93         return getSysfsPath(name);
94     }
95 };
96 
97 }  // namespace pixel
98 }  // namespace google
99 }  // namespace hardware
100 }  // namespace android
101 
102 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_TEST_MOCKMMMETRICS_H
103