• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <async_safe/log.h>
31 
32 #include "NativeInfo.h"
33 
NativePrintf(const char * fmt,...)34 void NativePrintf(const char* fmt, ...) {
35   va_list args;
36   va_start(args, fmt);
37   char buffer[512];
38   int buffer_len = async_safe_format_buffer_va_list(buffer, sizeof(buffer), fmt, args);
39   va_end(args);
40 
41   (void)write(STDOUT_FILENO, buffer, buffer_len);
42 }
43 
NativeFormatFloat(char * buffer,size_t buffer_len,uint64_t value,uint64_t divisor)44 void NativeFormatFloat(char* buffer, size_t buffer_len, uint64_t value, uint64_t divisor) {
45   uint64_t hundreds = ((((value % divisor) * 1000) / divisor) + 5) / 10;
46   async_safe_format_buffer(buffer, buffer_len, "%" PRIu64 ".%02" PRIu64, value / divisor, hundreds);
47 }
48 
49 // This function is not re-entrant since it uses a static buffer for
50 // the line data.
NativeGetInfo(int smaps_fd,size_t * rss_bytes,size_t * va_bytes)51 void NativeGetInfo(int smaps_fd, size_t* rss_bytes, size_t* va_bytes) {
52   size_t total_rss_bytes = 0;
53   size_t total_va_bytes = 0;
54   bool native_map = false;
55 
56   char buf[1024];
57   size_t buf_start = 0;
58   size_t buf_bytes = 0;
59   while (true) {
60     ssize_t bytes =
61         TEMP_FAILURE_RETRY(read(smaps_fd, buf + buf_bytes, sizeof(buf) - buf_bytes - 1));
62     if (bytes <= 0) {
63       break;
64     }
65     buf_bytes += bytes;
66     while (buf_bytes > 0) {
67       char* newline = reinterpret_cast<char*>(memchr(&buf[buf_start], '\n', buf_bytes));
68       if (newline == nullptr) {
69         break;
70       }
71       *newline = '\0';
72       uintptr_t start, end;
73       int name_pos;
74       size_t native_rss_kB;
75       if (sscanf(&buf[buf_start], "%" SCNxPTR "-%" SCNxPTR " %*4s %*x %*x:%*x %*d %n", &start, &end,
76                  &name_pos) == 2) {
77         char* map_name = &buf[buf_start + name_pos];
78         if (strcmp(map_name, "[anon:libc_malloc]") == 0 || strcmp(map_name, "[heap]") == 0 ||
79             strncmp(map_name, "[anon:scudo:", 12) == 0 ||
80             strncmp(map_name, "[anon:GWP-ASan", 14) == 0) {
81           total_va_bytes += end - start;
82           native_map = true;
83         } else {
84           native_map = false;
85         }
86       } else if (native_map && sscanf(&buf[buf_start], "Rss: %zu", &native_rss_kB) == 1) {
87         total_rss_bytes += native_rss_kB * 1024;
88       }
89       buf_bytes -= newline - &buf[buf_start] + 1;
90       buf_start = newline - buf + 1;
91     }
92     if (buf_start > 0) {
93       if (buf_bytes > 0) {
94         memmove(buf, &buf[buf_start], buf_bytes);
95       }
96       buf_start = 0;
97     }
98   }
99   *rss_bytes = total_rss_bytes;
100   *va_bytes = total_va_bytes;
101 }
102 
NativePrintInfo(const char * preamble)103 void NativePrintInfo(const char* preamble) {
104   size_t rss_bytes;
105   size_t va_bytes;
106 
107   android::base::unique_fd smaps_fd(open("/proc/self/smaps", O_RDONLY));
108   if (smaps_fd == -1) {
109     err(1, "Cannot open /proc/self/smaps: %s\n", strerror(errno));
110   }
111 
112   NativeGetInfo(smaps_fd, &rss_bytes, &va_bytes);
113 
114   // Avoid any allocations, so use special non-allocating printfs.
115   char buffer[256];
116   NativeFormatFloat(buffer, sizeof(buffer), rss_bytes, 1024 * 1024);
117   NativePrintf("%sNative RSS: %zu bytes %sMB\n", preamble, rss_bytes, buffer);
118   NativeFormatFloat(buffer, sizeof(buffer), va_bytes, 1024 * 1024);
119   NativePrintf("%sNative VA Space: %zu bytes %sMB\n", preamble, va_bytes, buffer);
120 }
121