OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/tracing/graphics_memory_dump_provider_android.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <sys/socket.h> |
| 9 #include <sys/time.h> |
| 10 #include <sys/types.h> |
| 11 #include <sys/un.h> |
| 12 |
| 13 #include "base/files/scoped_file.h" |
| 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_piece.h" |
| 17 #include "base/strings/string_tokenizer.h" |
| 18 #include "base/trace_event/process_memory_dump.h" |
| 19 #include "base/trace_event/process_memory_totals.h" |
| 20 |
| 21 using base::trace_event::MemoryAllocatorDump; |
| 22 |
| 23 namespace tracing { |
| 24 |
| 25 // static |
| 26 const char GraphicsMemoryDumpProvider::kDumpBaseName[] = |
| 27 "gpu/android_memtrack/"; |
| 28 |
| 29 // static |
| 30 GraphicsMemoryDumpProvider* GraphicsMemoryDumpProvider::GetInstance() { |
| 31 return base::Singleton< |
| 32 GraphicsMemoryDumpProvider, |
| 33 base::LeakySingletonTraits<GraphicsMemoryDumpProvider>>::get(); |
| 34 } |
| 35 |
| 36 bool GraphicsMemoryDumpProvider::OnMemoryDump( |
| 37 const base::trace_event::MemoryDumpArgs& args, |
| 38 base::trace_event::ProcessMemoryDump* pmd) { |
| 39 const char kAbstractSocketName[] = "chrome_tracing_memtrack_helper"; |
| 40 struct sockaddr_un addr; |
| 41 |
| 42 const int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 43 if (sock == -1) |
| 44 return false; |
| 45 // Ensures that sock is always closed, even in case of early returns. |
| 46 base::ScopedFD sock_closer(sock); |
| 47 |
| 48 // Set recv() timeout to 250 ms. |
| 49 struct timeval tv; |
| 50 tv.tv_sec = 0; |
| 51 tv.tv_usec = 250000; |
| 52 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 53 |
| 54 // Connect to the UNIX abstract (i.e. no physical filesystem link) socket. |
| 55 memset(&addr, 0, sizeof(addr)); |
| 56 addr.sun_family = AF_UNIX; |
| 57 strncpy(&addr.sun_path[1], kAbstractSocketName, sizeof(addr.sun_path) - 2); |
| 58 |
| 59 if (connect(sock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) { |
| 60 LOG(WARNING) << "Could not connect to the memtrack_helper daemon. Please " |
| 61 "build memtrack_helper, adb push to the device and run it " |
| 62 "before starting the trace to get graphics memory data."; |
| 63 return false; |
| 64 } |
| 65 |
| 66 // Check that the socket is owned by root (the memtrack_helper) process and |
| 67 // not an (untrusted) user process. |
| 68 struct ucred cred; |
| 69 socklen_t cred_len = sizeof(cred); |
| 70 if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0 || |
| 71 (static_cast<unsigned>(cred_len) < sizeof(cred)) || |
| 72 cred.uid != 0 /* root */) { |
| 73 LOG(WARNING) << "Untrusted (!= root) memtrack_helper daemon detected."; |
| 74 return false; |
| 75 } |
| 76 |
| 77 // Send the trace(PID) request. |
| 78 char buf[4096]; |
| 79 const int buf_pid_len = snprintf(buf, sizeof(buf) - 1, "%d", getpid()); |
| 80 if (HANDLE_EINTR(send(sock, buf, buf_pid_len + 1, 0)) <= 0) |
| 81 return false; |
| 82 |
| 83 // The response consists of a few lines, each one with a key value pair. E.g.: |
| 84 // graphics_total 10616832 |
| 85 // graphics_pss 10616832 |
| 86 // gl_total 17911808 |
| 87 // gl_pss 17911808 |
| 88 ssize_t rsize; |
| 89 if ((rsize = HANDLE_EINTR(recv(sock, buf, sizeof(buf), 0))) <= 0) |
| 90 return false; |
| 91 |
| 92 buf[sizeof(buf) - 1] = '\0'; |
| 93 ParseResponseAndAddToDump(buf, static_cast<size_t>(rsize), pmd); |
| 94 return true; |
| 95 } |
| 96 |
| 97 void GraphicsMemoryDumpProvider::ParseResponseAndAddToDump( |
| 98 const char* response, |
| 99 size_t length, |
| 100 base::trace_event::ProcessMemoryDump* pmd) { |
| 101 base::CStringTokenizer tokenizer(response, response + length, " \n"); |
| 102 while (true) { |
| 103 if (!tokenizer.GetNext()) |
| 104 break; |
| 105 base::StringPiece row_name = tokenizer.token_piece(); |
| 106 if (!tokenizer.GetNext()) |
| 107 break; |
| 108 base::StringPiece value_str = tokenizer.token_piece(); |
| 109 int64_t value; |
| 110 if (!base::StringToInt64(value_str, &value) || value < 0) |
| 111 continue; // Skip invalid or negative values. |
| 112 |
| 113 // Turn entries like graphics_total into a row named "graphics" and |
| 114 // column named "total". |
| 115 std::string column_name = "memtrack_"; |
| 116 size_t key_split_point = row_name.find_last_of('_'); |
| 117 if (key_split_point > 0 && key_split_point < row_name.size() - 1) { |
| 118 row_name.substr(key_split_point + 1).AppendToString(&column_name); |
| 119 row_name = row_name.substr(0, key_split_point); |
| 120 } else { |
| 121 column_name += "unknown"; |
| 122 } |
| 123 |
| 124 // Append a row to the memory dump. |
| 125 std::string dump_name; |
| 126 dump_name.reserve(arraysize(kDumpBaseName) + row_name.size() + 1); |
| 127 dump_name.assign(kDumpBaseName); |
| 128 row_name.AppendToString(&dump_name); |
| 129 MemoryAllocatorDump* mad = pmd->GetOrCreateAllocatorDump(dump_name); |
| 130 const auto& long_lived_column_name = key_names_.insert(column_name).first; |
| 131 mad->AddScalar(long_lived_column_name->c_str(), |
| 132 MemoryAllocatorDump::kUnitsBytes, value); |
| 133 } |
| 134 } |
| 135 |
| 136 GraphicsMemoryDumpProvider::GraphicsMemoryDumpProvider() {} |
| 137 |
| 138 GraphicsMemoryDumpProvider::~GraphicsMemoryDumpProvider() {} |
| 139 |
| 140 } // namespace tracing |
OLD | NEW |