1 /*
2 * Copyright (C) 2014 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 <err.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <android-base/unique_fd.h>
30
31 #include "NativeInfo.h"
32
NativeFormatFloat(char * buffer,size_t buffer_len,uint64_t value,uint64_t divisor)33 void NativeFormatFloat(char* buffer, size_t buffer_len, uint64_t value, uint64_t divisor) {
34 uint64_t hundreds = ((((value % divisor) * 1000) / divisor) + 5) / 10;
35 snprintf(buffer, buffer_len, "%" PRIu64 ".%02" PRIu64, value / divisor, hundreds);
36 }
37
38 // This function is not re-entrant since it uses a static buffer for
39 // the line data.
NativeGetInfo(int smaps_fd,size_t * rss_bytes,size_t * va_bytes)40 void NativeGetInfo(int smaps_fd, size_t* rss_bytes, size_t* va_bytes) {
41 size_t total_rss_bytes = 0;
42 size_t total_va_bytes = 0;
43 bool native_map = false;
44
45 char buf[1024];
46 size_t buf_start = 0;
47 size_t buf_bytes = 0;
48 while (true) {
49 ssize_t bytes =
50 TEMP_FAILURE_RETRY(read(smaps_fd, buf + buf_bytes, sizeof(buf) - buf_bytes - 1));
51 if (bytes <= 0) {
52 break;
53 }
54 buf_bytes += bytes;
55 while (buf_bytes > 0) {
56 char* newline = reinterpret_cast<char*>(memchr(&buf[buf_start], '\n', buf_bytes));
57 if (newline == nullptr) {
58 break;
59 }
60 *newline = '\0';
61 uintptr_t start, end;
62 int name_pos;
63 size_t native_rss_kB;
64 if (sscanf(&buf[buf_start], "%" SCNxPTR "-%" SCNxPTR " %*4s %*x %*x:%*x %*d %n", &start, &end,
65 &name_pos) == 2) {
66 char* map_name = &buf[buf_start + name_pos];
67 if (strcmp(map_name, "[anon:libc_malloc]") == 0 || strcmp(map_name, "[heap]") == 0 ||
68 strncmp(map_name, "[anon:scudo:", 12) == 0 ||
69 strncmp(map_name, "[anon:GWP-ASan", 14) == 0) {
70 total_va_bytes += end - start;
71 native_map = true;
72 } else {
73 native_map = false;
74 }
75 } else if (native_map && sscanf(&buf[buf_start], "Rss: %zu", &native_rss_kB) == 1) {
76 total_rss_bytes += native_rss_kB * 1024;
77 }
78 buf_bytes -= newline - &buf[buf_start] + 1;
79 buf_start = newline - buf + 1;
80 }
81 if (buf_start > 0) {
82 if (buf_bytes > 0) {
83 memmove(buf, &buf[buf_start], buf_bytes);
84 }
85 buf_start = 0;
86 }
87 }
88 *rss_bytes = total_rss_bytes;
89 *va_bytes = total_va_bytes;
90 }
91
NativePrintInfo(const char * preamble)92 void NativePrintInfo(const char* preamble) {
93 size_t rss_bytes;
94 size_t va_bytes;
95
96 android::base::unique_fd smaps_fd(open("/proc/self/smaps", O_RDONLY));
97 if (smaps_fd == -1) {
98 err(1, "Cannot open /proc/self/smaps");
99 }
100
101 NativeGetInfo(smaps_fd, &rss_bytes, &va_bytes);
102
103 // Avoid any allocations, so use special non-allocating printfs.
104 char buffer[256];
105 NativeFormatFloat(buffer, sizeof(buffer), rss_bytes, 1024 * 1024);
106 dprintf(STDOUT_FILENO, "%sNative RSS: %zu bytes %sMB\n", preamble, rss_bytes, buffer);
107 NativeFormatFloat(buffer, sizeof(buffer), va_bytes, 1024 * 1024);
108 dprintf(STDOUT_FILENO, "%sNative VA Space: %zu bytes %sMB\n", preamble, va_bytes, buffer);
109 }
110