1 /*
2 * Copyright (C) 2019 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 #include <gtest/gtest.h>
18
19 #include <android/api-level.h>
20 #include <android-base/properties.h>
21 #include <meminfo/procmeminfo.h>
22 #include <meminfo/sysmeminfo.h>
23 #include <vintf/VintfObject.h>
24
25 using android::vintf::KernelVersion;
26 using android::vintf::RuntimeInfo;
27 using android::vintf::VintfObject;
28
29 namespace android {
30 namespace meminfo {
31
32 // /proc/<pid>/smaps_rollup support is required.
TEST(SmapsRollup,IsSupported)33 TEST(SmapsRollup, IsSupported) {
34 // Use init's pid for this test since it's the only known pid.
35 ASSERT_TRUE(IsSmapsRollupSupported());
36 }
37
38 // KReclaimable in /proc/meminfo is required.
TEST(SysMemInfo,TestKReclaimable)39 TEST(SysMemInfo, TestKReclaimable) {
40 SysMemInfo mi;
41
42 if (android::base::GetIntProperty("ro.product.first_api_level", 0) < __ANDROID_API_R__) {
43 GTEST_SKIP();
44 }
45 ASSERT_TRUE(mi.ReadMemInfo());
46 // KReclaimable includes SReclaimable, so they should be at least equal
47 ASSERT_TRUE(mi.mem_kreclaimable_kb() >= mi.mem_slab_reclaimable_kb());
48 }
49
50 // /sys/kernel/ion/total_heaps_kb support is required.
TEST(SysMemInfo,TestIonTotalHeapsKb)51 TEST(SysMemInfo, TestIonTotalHeapsKb) {
52 uint64_t size;
53
54 if (android::base::GetIntProperty("ro.product.first_api_level", 0) < __ANDROID_API_R__) {
55 GTEST_SKIP();
56 }
57
58 KernelVersion max_kernel_version = KernelVersion(5, 10, 0);
59 KernelVersion kernel_version =
60 android::vintf::VintfObject::GetInstance()
61 ->getRuntimeInfo(android::vintf::RuntimeInfo::FetchFlag::CPU_VERSION)
62 ->kernelVersion();
63
64 if (kernel_version < max_kernel_version) {
65 ASSERT_TRUE(ReadIonHeapsSizeKb(&size));
66 } else {
67 GTEST_SKIP();
68 }
69 }
70
71 // /sys/kernel/ion/total_pools_kb support is required.
TEST(SysMemInfo,TestIonTotalPoolsKb)72 TEST(SysMemInfo, TestIonTotalPoolsKb) {
73 uint64_t size;
74
75 if (android::base::GetIntProperty("ro.product.first_api_level", 0) < __ANDROID_API_R__) {
76 GTEST_SKIP();
77 }
78
79 KernelVersion max_kernel_version = KernelVersion(5, 10, 0);
80 KernelVersion kernel_version =
81 android::vintf::VintfObject::GetInstance()
82 ->getRuntimeInfo(android::vintf::RuntimeInfo::FetchFlag::CPU_VERSION)
83 ->kernelVersion();
84
85 if (kernel_version < max_kernel_version) {
86 ASSERT_TRUE(ReadIonPoolsSizeKb(&size));
87 } else {
88 GTEST_SKIP();
89 }
90 }
91
92 // /sys/fs/bpf/map_gpuMem_gpu_mem_total_map support is required for devices launching with
93 // Android S and having 5.4 or higher kernel version.
TEST(SysMemInfo,TestGpuTotalUsageKb)94 TEST(SysMemInfo, TestGpuTotalUsageKb) {
95 uint64_t size;
96
97 if (android::base::GetIntProperty("ro.vendor.api_level", 0) < __ANDROID_API_S__) {
98 GTEST_SKIP();
99 }
100
101 KernelVersion min_kernel_version = KernelVersion(5, 4, 0);
102 KernelVersion kernel_version = VintfObject::GetInstance()
103 ->getRuntimeInfo(RuntimeInfo::FetchFlag::CPU_VERSION)
104 ->kernelVersion();
105 if (kernel_version < min_kernel_version) {
106 GTEST_SKIP();
107 }
108
109 ASSERT_TRUE(ReadGpuTotalUsageKb(&size));
110 ASSERT_TRUE(size >= 0);
111 }
112
113 } // namespace meminfo
114 } // namespace android
115