2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/types.h>
28 #include "LineBuffer.h"
29 #include "NativeInfo.h"
31 // This function is not re-entrant since it uses a static buffer for
33 void GetNativeInfo(int smaps_fd, size_t* pss_bytes, size_t* va_bytes) {
34 static char map_buffer[65535];
35 LineBuffer line_buf(smaps_fd, map_buffer, sizeof(map_buffer));
37 size_t total_pss_bytes = 0;
38 size_t total_va_bytes = 0;
40 bool native_map = false;
41 while (line_buf.GetLine(&line, &line_len)) {
45 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %*4s %*x %*x:%*x %*d %n",
46 &start, &end, &name_pos) == 2) {
47 if (strcmp(line + name_pos, "[anon:libc_malloc]") == 0 ||
48 strcmp(line + name_pos, "[heap]") == 0) {
49 total_va_bytes += end - start;
54 } else if (native_map && sscanf(line, "Pss: %zu", &native_pss_kB) == 1) {
55 total_pss_bytes += native_pss_kB * 1024;
59 *pss_bytes = total_pss_bytes;
60 *va_bytes = total_va_bytes;
63 void PrintNativeInfo(const char* preamble) {
67 int smaps_fd = open("/proc/self/smaps", O_RDONLY);
69 err(1, "Cannot open /proc/self/smaps: %s\n", strerror(errno));
72 GetNativeInfo(smaps_fd, &pss_bytes, &va_bytes);
73 printf("%sNative PSS: %zu bytes %0.2fMB\n", preamble, pss_bytes, pss_bytes/(1024*1024.0));
74 printf("%sNative VA Space: %zu bytes %0.2fMB\n", preamble, va_bytes, va_bytes/(1024*1024.0));