summaryrefslogtreecommitdiff
path: root/memory_replay/NativeInfo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'memory_replay/NativeInfo.cpp')
-rw-r--r--memory_replay/NativeInfo.cpp100
1 files changed, 29 insertions, 71 deletions
diff --git a/memory_replay/NativeInfo.cpp b/memory_replay/NativeInfo.cpp
index ec131ddc..18c832bb 100644
--- a/memory_replay/NativeInfo.cpp
+++ b/memory_replay/NativeInfo.cpp
@@ -18,89 +18,51 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
-#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
-#include <sys/stat.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <android-base/unique_fd.h>
-#include <async_safe/log.h>
+#include "LineBuffer.h"
#include "NativeInfo.h"
-void NativePrintf(const char* fmt, ...) {
- va_list args;
- va_start(args, fmt);
- char buffer[512];
- int buffer_len = async_safe_format_buffer_va_list(buffer, sizeof(buffer), fmt, args);
- va_end(args);
-
- (void)write(STDOUT_FILENO, buffer, buffer_len);
-}
-
-void NativeFormatFloat(char* buffer, size_t buffer_len, uint64_t value, uint64_t divisor) {
- uint64_t hundreds = ((((value % divisor) * 1000) / divisor) + 5) / 10;
- async_safe_format_buffer(buffer, buffer_len, "%" PRIu64 ".%02" PRIu64, value / divisor, hundreds);
-}
-
// This function is not re-entrant since it uses a static buffer for
// the line data.
-void NativeGetInfo(int smaps_fd, size_t* rss_bytes, size_t* va_bytes) {
- size_t total_rss_bytes = 0;
+void GetNativeInfo(int smaps_fd, size_t* pss_bytes, size_t* va_bytes) {
+ static char map_buffer[65535];
+ LineBuffer line_buf(smaps_fd, map_buffer, sizeof(map_buffer));
+ char* line;
+ size_t total_pss_bytes = 0;
size_t total_va_bytes = 0;
+ size_t line_len;
bool native_map = false;
-
- char buf[1024];
- size_t buf_start = 0;
- size_t buf_bytes = 0;
- while (true) {
- ssize_t bytes =
- TEMP_FAILURE_RETRY(read(smaps_fd, buf + buf_bytes, sizeof(buf) - buf_bytes - 1));
- if (bytes <= 0) {
- break;
- }
- buf_bytes += bytes;
- while (buf_bytes > 0) {
- char* newline = reinterpret_cast<char*>(memchr(&buf[buf_start], '\n', buf_bytes));
- if (newline == nullptr) {
- break;
- }
- *newline = '\0';
- uintptr_t start, end;
- int name_pos;
- size_t native_rss_kB;
- if (sscanf(&buf[buf_start], "%" SCNxPTR "-%" SCNxPTR " %*4s %*x %*x:%*x %*d %n", &start, &end,
- &name_pos) == 2) {
- char* map_name = &buf[buf_start + name_pos];
- if (strcmp(map_name, "[anon:libc_malloc]") == 0 || strcmp(map_name, "[heap]") == 0 ||
- strncmp(map_name, "[anon:scudo:", 12) == 0) {
- total_va_bytes += end - start;
- native_map = true;
- } else {
- native_map = false;
- }
- } else if (native_map && sscanf(&buf[buf_start], "Rss: %zu", &native_rss_kB) == 1) {
- total_rss_bytes += native_rss_kB * 1024;
+ while (line_buf.GetLine(&line, &line_len)) {
+ uintptr_t start, end;
+ int name_pos;
+ size_t native_pss_kB;
+ if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %*4s %*x %*x:%*x %*d %n",
+ &start, &end, &name_pos) == 2) {
+ if (strcmp(line + name_pos, "[anon:libc_malloc]") == 0 ||
+ strcmp(line + name_pos, "[heap]") == 0) {
+ total_va_bytes += end - start;
+ native_map = true;
+ } else {
+ native_map = false;
}
- buf_bytes -= newline - &buf[buf_start] + 1;
- buf_start = newline - buf + 1;
- }
- if (buf_start > 0) {
- if (buf_bytes > 0) {
- memmove(buf, &buf[buf_start], buf_bytes);
- }
- buf_start = 0;
+ } else if (native_map && sscanf(line, "Pss: %zu", &native_pss_kB) == 1) {
+ total_pss_bytes += native_pss_kB * 1024;
}
}
- *rss_bytes = total_rss_bytes;
+ *pss_bytes = total_pss_bytes;
*va_bytes = total_va_bytes;
}
-void NativePrintInfo(const char* preamble) {
- size_t rss_bytes;
+void PrintNativeInfo(const char* preamble) {
+ size_t pss_bytes;
size_t va_bytes;
android::base::unique_fd smaps_fd(open("/proc/self/smaps", O_RDONLY));
@@ -108,12 +70,8 @@ void NativePrintInfo(const char* preamble) {
err(1, "Cannot open /proc/self/smaps: %s\n", strerror(errno));
}
- NativeGetInfo(smaps_fd, &rss_bytes, &va_bytes);
-
- // Avoid any allocations, so use special non-allocating printfs.
- char buffer[256];
- NativeFormatFloat(buffer, sizeof(buffer), rss_bytes, 1024 * 1024);
- NativePrintf("%sNative RSS: %zu bytes %sMB\n", preamble, rss_bytes, buffer);
- NativeFormatFloat(buffer, sizeof(buffer), va_bytes, 1024 * 1024);
- NativePrintf("%sNative VA Space: %zu bytes %sMB\n", preamble, va_bytes, buffer);
+ GetNativeInfo(smaps_fd, &pss_bytes, &va_bytes);
+ printf("%sNative PSS: %zu bytes %0.2fMB\n", preamble, pss_bytes, pss_bytes/(1024*1024.0));
+ printf("%sNative VA Space: %zu bytes %0.2fMB\n", preamble, va_bytes, va_bytes/(1024*1024.0));
+ fflush(stdout);
}