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 <stdint.h> 18 19 #include <string> 20 21 #include <android-base/file.h> 22 #include <gtest/gtest.h> 23 24 #include "NativeInfo.h" 25 26 class NativeInfoTest : public ::testing::Test { 27 protected: 28 void SetUp() override { 29 tmp_file_ = new TemporaryFile(); 30 ASSERT_TRUE(tmp_file_->fd != -1); 31 } 32 33 void TearDown() override { 34 delete tmp_file_; 35 } 36 37 TemporaryFile* tmp_file_ = nullptr; 38 }; 39 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: 12 kB\n" 45 "Pss: 0 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 rss_bytes = 1; 63 size_t va_bytes = 1; 64 NativeGetInfo(tmp_file_->fd, &rss_bytes, &va_bytes); 65 ASSERT_EQ(0U, rss_bytes); 66 ASSERT_EQ(0U, va_bytes); 67 } 68 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: 12 kB\n" 74 "Pss: 0 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: 20 kB\n" 90 "Pss: 0 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: 24 kB\n" 106 "Pss: 0 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 rss_bytes = 1; 124 size_t va_bytes = 1; 125 NativeGetInfo(tmp_file_->fd, &rss_bytes, &va_bytes); 126 ASSERT_EQ(32768U, rss_bytes); 127 ASSERT_EQ(12288U, va_bytes); 128 } 129 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: 24 kB\n" 135 "Pss: 0 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: 20 kB\n" 151 "Pss: 0 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: 24 kB\n" 167 "Pss: 0 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 rss_bytes = 1; 185 size_t va_bytes = 1; 186 NativeGetInfo(tmp_file_->fd, &rss_bytes, &va_bytes); 187 ASSERT_EQ(45056U, rss_bytes); 188 ASSERT_EQ(12288U, va_bytes); 189 } 190 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: 32 kB\n" 196 "Pss: 0 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: 32 kB\n" 212 "Pss: 0 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: 40 kB\n" 228 "Pss: 0 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: 24 kB\n" 244 "Pss: 0 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 "b6f4e000-b6f6f000 rw-p 00000000 00:00 0 [anon:scudo:test]\n" 258 "Size: 8 kB\n" 259 "Rss: 52 kB\n" 260 "Pss: 0 kB\n" 261 "Shared_Clean: 0 kB\n" 262 "Shared_Dirty: 0 kB\n" 263 "Private_Clean: 0 kB\n" 264 "Private_Dirty: 0 kB\n" 265 "Referenced: 0 kB\n" 266 "Anonymous: 0 kB\n" 267 "AnonHugePages: 0 kB\n" 268 "Swap: 0 kB\n" 269 "KernelPageSize: 4 kB\n" 270 "MMUPageSize: 4 kB\n" 271 "Locked: 0 kB\n" 272 "Name: [anon:scudo:test]\n" 273 "b8f4c000-b8f4d000 rw-p 00000000 00:00 0 [anon:GWP-ASan Guard Page]\n" 274 "Size: 4 kB\n" 275 "Rss: 0 kB\n" 276 "Pss: 0 kB\n" 277 "Shared_Clean: 0 kB\n" 278 "Shared_Dirty: 0 kB\n" 279 "Private_Clean: 0 kB\n" 280 "Private_Dirty: 0 kB\n" 281 "Referenced: 0 kB\n" 282 "Anonymous: 0 kB\n" 283 "AnonHugePages: 0 kB\n" 284 "Swap: 0 kB\n" 285 "KernelPageSize: 4 kB\n" 286 "MMUPageSize: 4 kB\n" 287 "Locked: 0 kB\n" 288 "Name: [anon:GWP-ASan Guard Page]\n" 289 "b8f4d000-b8f4e000 rw-p 00000000 00:00 0 [anon:GWP-ASan Alive Slot]\n" 290 "Size: 4 kB\n" 291 "Rss: 4 kB\n" 292 "Pss: 0 kB\n" 293 "Shared_Clean: 0 kB\n" 294 "Shared_Dirty: 0 kB\n" 295 "Private_Clean: 0 kB\n" 296 "Private_Dirty: 0 kB\n" 297 "Referenced: 0 kB\n" 298 "Anonymous: 0 kB\n" 299 "AnonHugePages: 0 kB\n" 300 "Swap: 0 kB\n" 301 "KernelPageSize: 4 kB\n" 302 "MMUPageSize: 4 kB\n" 303 "Locked: 0 kB\n" 304 "Name: [anon:GWP-ASan Alive Slot]\n" 305 "b8f4e000-b8f4f000 rw-p 00000000 00:00 0 [anon:GWP-ASan Guard Page]\n" 306 "Size: 4 kB\n" 307 "Rss: 0 kB\n" 308 "Pss: 0 kB\n" 309 "Shared_Clean: 0 kB\n" 310 "Shared_Dirty: 0 kB\n" 311 "Private_Clean: 0 kB\n" 312 "Private_Dirty: 0 kB\n" 313 "Referenced: 0 kB\n" 314 "Anonymous: 0 kB\n" 315 "AnonHugePages: 0 kB\n" 316 "Swap: 0 kB\n" 317 "KernelPageSize: 4 kB\n" 318 "MMUPageSize: 4 kB\n" 319 "Locked: 0 kB\n" 320 "Name: [anon:GWP-ASan Guard Page]\n"; 321 322 ASSERT_TRUE(TEMP_FAILURE_RETRY( 323 write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1); 324 ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); 325 326 size_t rss_bytes = 1; 327 size_t va_bytes = 1; 328 NativeGetInfo(tmp_file_->fd, &rss_bytes, &va_bytes); 329 EXPECT_EQ(131072U, rss_bytes); 330 EXPECT_EQ(159744U, va_bytes); 331 } 332