1 /*
2 * Copyright (C) 2015 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 #include <stdint.h>
19
20 #include <string>
21
22 #include <android-base/test_utils.h>
23
24 #include "NativeInfo.h"
25
26 class NativeInfoTest : public ::testing::Test {
27 protected:
SetUp()28 void SetUp() override {
29 tmp_file_ = new TemporaryFile();
30 ASSERT_TRUE(tmp_file_->fd != -1);
31 }
32
TearDown()33 void TearDown() override {
34 delete tmp_file_;
35 }
36
37 TemporaryFile* tmp_file_ = nullptr;
38 };
39
TEST_F(NativeInfoTest,no_matching)40 TEST_F(NativeInfoTest, no_matching) {
41 std::string smaps_data =
42 "b6f1a000-b6f1c000 rw-p 00000000 00:00 0 [anon:thread signal stack]\n"
43 "Size: 8 kB\n"
44 "Rss: 0 kB\n"
45 "Pss: 12 kB\n"
46 "Shared_Clean: 0 kB\n"
47 "Shared_Dirty: 0 kB\n"
48 "Private_Clean: 0 kB\n"
49 "Private_Dirty: 0 kB\n"
50 "Referenced: 0 kB\n"
51 "Anonymous: 0 kB\n"
52 "AnonHugePages: 0 kB\n"
53 "Swap: 0 kB\n"
54 "KernelPageSize: 4 kB\n"
55 "MMUPageSize: 4 kB\n"
56 "Locked: 0 kB\n"
57 "Name: [anon:thread signal stack]\n";
58 ASSERT_TRUE(TEMP_FAILURE_RETRY(
59 write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1);
60 ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
61
62 size_t pss_bytes = 1;
63 size_t va_bytes = 1;
64 GetNativeInfo(tmp_file_->fd, &pss_bytes, &va_bytes);
65 ASSERT_EQ(0U, pss_bytes);
66 ASSERT_EQ(0U, va_bytes);
67 }
68
TEST_F(NativeInfoTest,multiple_anons)69 TEST_F(NativeInfoTest, multiple_anons) {
70 std::string smaps_data =
71 "b6f1a000-b6f1c000 rw-p 00000000 00:00 0 [anon:libc_malloc]\n"
72 "Size: 8 kB\n"
73 "Rss: 0 kB\n"
74 "Pss: 12 kB\n"
75 "Shared_Clean: 0 kB\n"
76 "Shared_Dirty: 0 kB\n"
77 "Private_Clean: 0 kB\n"
78 "Private_Dirty: 0 kB\n"
79 "Referenced: 0 kB\n"
80 "Anonymous: 0 kB\n"
81 "AnonHugePages: 0 kB\n"
82 "Swap: 0 kB\n"
83 "KernelPageSize: 4 kB\n"
84 "MMUPageSize: 4 kB\n"
85 "Locked: 0 kB\n"
86 "Name: [anon:libc_malloc]\n"
87 "b6f1e000-b6f1f000 rw-p 00000000 00:00 0 [anon:libc_malloc]\n"
88 "Size: 8 kB\n"
89 "Rss: 0 kB\n"
90 "Pss: 20 kB\n"
91 "Shared_Clean: 0 kB\n"
92 "Shared_Dirty: 0 kB\n"
93 "Private_Clean: 0 kB\n"
94 "Private_Dirty: 0 kB\n"
95 "Referenced: 0 kB\n"
96 "Anonymous: 0 kB\n"
97 "AnonHugePages: 0 kB\n"
98 "Swap: 0 kB\n"
99 "KernelPageSize: 4 kB\n"
100 "MMUPageSize: 4 kB\n"
101 "Locked: 0 kB\n"
102 "Name: [anon:libc_malloc]\n"
103 "b6f2e000-b6f2f000 rw-p 00000000 00:00 0\n"
104 "Size: 8 kB\n"
105 "Rss: 0 kB\n"
106 "Pss: 24 kB\n"
107 "Shared_Clean: 0 kB\n"
108 "Shared_Dirty: 0 kB\n"
109 "Private_Clean: 0 kB\n"
110 "Private_Dirty: 0 kB\n"
111 "Referenced: 0 kB\n"
112 "Anonymous: 0 kB\n"
113 "AnonHugePages: 0 kB\n"
114 "Swap: 0 kB\n"
115 "KernelPageSize: 4 kB\n"
116 "MMUPageSize: 4 kB\n"
117 "Locked: 0 kB\n"
118 "Name:\n";
119 ASSERT_TRUE(TEMP_FAILURE_RETRY(
120 write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1);
121 ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
122
123 size_t pss_bytes = 1;
124 size_t va_bytes = 1;
125 GetNativeInfo(tmp_file_->fd, &pss_bytes, &va_bytes);
126 ASSERT_EQ(32768U, pss_bytes);
127 ASSERT_EQ(12288U, va_bytes);
128 }
129
TEST_F(NativeInfoTest,multiple_heaps)130 TEST_F(NativeInfoTest, multiple_heaps) {
131 std::string smaps_data =
132 "b6f1a000-b6f1c000 rw-p 00000000 00:00 0 [heap]\n"
133 "Size: 8 kB\n"
134 "Rss: 0 kB\n"
135 "Pss: 24 kB\n"
136 "Shared_Clean: 0 kB\n"
137 "Shared_Dirty: 0 kB\n"
138 "Private_Clean: 0 kB\n"
139 "Private_Dirty: 0 kB\n"
140 "Referenced: 0 kB\n"
141 "Anonymous: 0 kB\n"
142 "AnonHugePages: 0 kB\n"
143 "Swap: 0 kB\n"
144 "KernelPageSize: 4 kB\n"
145 "MMUPageSize: 4 kB\n"
146 "Locked: 0 kB\n"
147 "Name: [heap]\n"
148 "b6f1e000-b6f1f000 rw-p 00000000 00:00 0 [heap]\n"
149 "Size: 8 kB\n"
150 "Rss: 0 kB\n"
151 "Pss: 20 kB\n"
152 "Shared_Clean: 0 kB\n"
153 "Shared_Dirty: 0 kB\n"
154 "Private_Clean: 0 kB\n"
155 "Private_Dirty: 0 kB\n"
156 "Referenced: 0 kB\n"
157 "Anonymous: 0 kB\n"
158 "AnonHugePages: 0 kB\n"
159 "Swap: 0 kB\n"
160 "KernelPageSize: 4 kB\n"
161 "MMUPageSize: 4 kB\n"
162 "Locked: 0 kB\n"
163 "Name: [heap]\n"
164 "b6f2e000-b6f2f000 rw-p 00000000 00:00 0\n"
165 "Size: 8 kB\n"
166 "Rss: 0 kB\n"
167 "Pss: 24 kB\n"
168 "Shared_Clean: 0 kB\n"
169 "Shared_Dirty: 0 kB\n"
170 "Private_Clean: 0 kB\n"
171 "Private_Dirty: 0 kB\n"
172 "Referenced: 0 kB\n"
173 "Anonymous: 0 kB\n"
174 "AnonHugePages: 0 kB\n"
175 "Swap: 0 kB\n"
176 "KernelPageSize: 4 kB\n"
177 "MMUPageSize: 4 kB\n"
178 "Locked: 0 kB\n"
179 "Name:\n";
180 ASSERT_TRUE(TEMP_FAILURE_RETRY(
181 write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1);
182 ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
183
184 size_t pss_bytes = 1;
185 size_t va_bytes = 1;
186 GetNativeInfo(tmp_file_->fd, &pss_bytes, &va_bytes);
187 ASSERT_EQ(45056U, pss_bytes);
188 ASSERT_EQ(12288U, va_bytes);
189 }
190
TEST_F(NativeInfoTest,mix_heap_anon)191 TEST_F(NativeInfoTest, mix_heap_anon) {
192 std::string smaps_data =
193 "b6f1a000-b6f1c000 rw-p 00000000 00:00 0 [heap]\n"
194 "Size: 8 kB\n"
195 "Rss: 0 kB\n"
196 "Pss: 32 kB\n"
197 "Shared_Clean: 0 kB\n"
198 "Shared_Dirty: 0 kB\n"
199 "Private_Clean: 0 kB\n"
200 "Private_Dirty: 0 kB\n"
201 "Referenced: 0 kB\n"
202 "Anonymous: 0 kB\n"
203 "AnonHugePages: 0 kB\n"
204 "Swap: 0 kB\n"
205 "KernelPageSize: 4 kB\n"
206 "MMUPageSize: 4 kB\n"
207 "Locked: 0 kB\n"
208 "Name: [heap]\n"
209 "b6f1e000-b6f1f000 rw-p 00000000 00:00 0 [anon:skip]\n"
210 "Size: 8 kB\n"
211 "Rss: 0 kB\n"
212 "Pss: 32 kB\n"
213 "Shared_Clean: 0 kB\n"
214 "Shared_Dirty: 0 kB\n"
215 "Private_Clean: 0 kB\n"
216 "Private_Dirty: 0 kB\n"
217 "Referenced: 0 kB\n"
218 "Anonymous: 0 kB\n"
219 "AnonHugePages: 0 kB\n"
220 "Swap: 0 kB\n"
221 "KernelPageSize: 4 kB\n"
222 "MMUPageSize: 4 kB\n"
223 "Locked: 0 kB\n"
224 "Name: [anon:skip]\n"
225 "b6f2e000-b6f2f000 rw-p 00000000 00:00 0 [anon:libc_malloc]\n"
226 "Size: 8 kB\n"
227 "Rss: 0 kB\n"
228 "Pss: 40 kB\n"
229 "Shared_Clean: 0 kB\n"
230 "Shared_Dirty: 0 kB\n"
231 "Private_Clean: 0 kB\n"
232 "Private_Dirty: 0 kB\n"
233 "Referenced: 0 kB\n"
234 "Anonymous: 0 kB\n"
235 "AnonHugePages: 0 kB\n"
236 "Swap: 0 kB\n"
237 "KernelPageSize: 4 kB\n"
238 "MMUPageSize: 4 kB\n"
239 "Locked: 0 kB\n"
240 "Name: [anon:libc_malloc]\n"
241 "b6f3e000-b6f3f000 rw-p 00000000 00:00 0\n"
242 "Size: 8 kB\n"
243 "Rss: 0 kB\n"
244 "Pss: 24 kB\n"
245 "Shared_Clean: 0 kB\n"
246 "Shared_Dirty: 0 kB\n"
247 "Private_Clean: 0 kB\n"
248 "Private_Dirty: 0 kB\n"
249 "Referenced: 0 kB\n"
250 "Anonymous: 0 kB\n"
251 "AnonHugePages: 0 kB\n"
252 "Swap: 0 kB\n"
253 "KernelPageSize: 4 kB\n"
254 "MMUPageSize: 4 kB\n"
255 "Locked: 0 kB\n"
256 "Name:\n";
257 ASSERT_TRUE(TEMP_FAILURE_RETRY(
258 write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1);
259 ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
260
261 size_t pss_bytes = 1;
262 size_t va_bytes = 1;
263 GetNativeInfo(tmp_file_->fd, &pss_bytes, &va_bytes);
264 ASSERT_EQ(73728U, pss_bytes);
265 ASSERT_EQ(12288U, va_bytes);
266 }
267