OSDN Git Service

merge in nyc-mr1-release history after reset to nyc-mr1-dev
[android-x86/system-extras.git] / memory_replay / NativeInfo.cpp
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 <stdint.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include "LineBuffer.h"
29 #include "NativeInfo.h"
30
31 // This function is not re-entrant since it uses a static buffer for
32 // the line data.
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));
36   char* line;
37   size_t total_pss_bytes = 0;
38   size_t total_va_bytes = 0;
39   size_t line_len;
40   bool native_map = false;
41   while (line_buf.GetLine(&line, &line_len)) {
42     uintptr_t start, end;
43     int name_pos;
44     size_t native_pss_kB;
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;
50         native_map = true;
51       } else {
52         native_map = false;
53       }
54     } else if (native_map && sscanf(line, "Pss: %zu", &native_pss_kB) == 1) {
55       total_pss_bytes += native_pss_kB * 1024;
56     }
57   }
58   close(smaps_fd);
59   *pss_bytes = total_pss_bytes;
60   *va_bytes = total_va_bytes;
61 }
62
63 void PrintNativeInfo(const char* preamble) {
64   size_t pss_bytes;
65   size_t va_bytes;
66
67   int smaps_fd = open("/proc/self/smaps", O_RDONLY);
68   if (smaps_fd == -1) {
69     err(1, "Cannot open /proc/self/smaps: %s\n", strerror(errno));
70   }
71
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));
75   fflush(stdout);
76
77   close(smaps_fd);
78 }