Chromium Code Reviews| Index: components/tracing/graphics_memory_dump_provider_android.cc |
| diff --git a/components/tracing/graphics_memory_dump_provider_android.cc b/components/tracing/graphics_memory_dump_provider_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e72fa373167f96ac4dc283ea43e165d757872fb3 |
| --- /dev/null |
| +++ b/components/tracing/graphics_memory_dump_provider_android.cc |
| @@ -0,0 +1,112 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/tracing/graphics_memory_dump_provider_android.h" |
| + |
| +#include <fcntl.h> |
| +#include <sys/socket.h> |
| +#include <sys/time.h> |
| +#include <sys/types.h> |
| +#include <sys/un.h> |
| + |
| +#include "base/trace_event/process_memory_dump.h" |
| +#include "base/trace_event/process_memory_totals.h" |
| + |
| +using base::trace_event::MemoryAllocatorDump; |
| + |
| +namespace tracing { |
| + |
| +// static |
| +GraphicsMemoryDumpProvider* GraphicsMemoryDumpProvider::GetInstance() { |
| + return base::Singleton< |
| + GraphicsMemoryDumpProvider, |
| + base::LeakySingletonTraits<GraphicsMemoryDumpProvider>>::get(); |
| +} |
| + |
| +bool GraphicsMemoryDumpProvider::OnMemoryDump( |
| + const base::trace_event::MemoryDumpArgs& args, |
| + base::trace_event::ProcessMemoryDump* pmd) { |
| + const char kAbstractSocketName[] = "chrome_tracing_memtrack_helper"; |
| + int sock; |
| + struct sockaddr_un addr; |
| + |
| + sock = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
|
Robert Sesek
2015/10/09 17:22:21
Consider using base/sync_socket.h, since it has RA
Primiano Tucci (use gerrit)
2015/10/12 13:00:04
Oh, excellent point.
Actually turns out I can't ju
|
| + if (sock == -1) |
| + return false; |
| + |
| + // Set recv timeout to 250ms |
|
Robert Sesek
2015/10/09 17:22:21
nit: punctuation.
Primiano Tucci (use gerrit)
2015/10/12 13:00:04
Done.
|
| + struct timeval tv; |
| + tv.tv_sec = 0; |
| + tv.tv_usec = 250000; |
| + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| + |
| + // Connect to the UNIX abstract (i.e. no physical filesystem link) socket. |
| + memset(&addr, 0, sizeof(addr)); |
| + addr.sun_family = AF_UNIX; |
| + strncpy(&addr.sun_path[1], kAbstractSocketName, sizeof(addr.sun_path) - 2); |
| + |
| + if (connect(sock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) { |
| + LOG(WARNING) << "Could not connect to the memtrack_helper daemon. Please " |
| + "build memtrack_helper, adb push to the device and run it " |
| + "before starting the trace to get graphics memory data."; |
| + return false; |
| + } |
| + |
| + // Check that the socket is owned by root (the memtrack_helper) process and |
| + // not an (untrusted) user process. |
| + struct ucred cred; |
| + socklen_t cred_len = sizeof(cred); |
| + if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0 || |
| + (static_cast<unsigned>(cred_len) < sizeof(cred)) || |
| + cred.uid != 0 /* root */) { |
| + LOG(WARNING) << "Untrusted (!= root) memtrack_helper daemon detected."; |
| + return false; |
| + } |
| + |
| + // Send the trace(PID) request. |
| + char buf[4096]; |
| + snprintf(buf, sizeof(buf) - 1, "%d", getpid()); |
|
Robert Sesek
2015/10/09 17:22:21
base::IntToString() and then c_str() and size() +
Primiano Tucci (use gerrit)
2015/10/12 13:00:04
Hmm but that would make another copy to construct
|
| + if (send(sock, buf, strlen(buf) + 1, 0) <= 0) |
|
Robert Sesek
2015/10/09 17:22:21
What about EINTR?
Primiano Tucci (use gerrit)
2015/10/12 13:00:04
another excellent point
|
| + return false; |
| + |
| + // The response consists of a few lines, each one with a key value pair. E.g.: |
| + // graphics_total 10616832 |
| + // graphics_pss 10616832 |
| + // gl_total 17911808 |
| + // gl_pss 17911808 |
| + if (recv(sock, buf, sizeof(buf), 0) <= 0) |
|
Robert Sesek
2015/10/09 17:22:21
EINTR again.
Primiano Tucci (use gerrit)
2015/10/12 13:00:04
Done.
|
| + return false; |
| + |
| + close(sock); |
| + |
| + buf[sizeof(buf) - 1] = '\0'; |
| + std::stringstream ss(std::string(buf, sizeof(buf))); |
|
Robert Sesek
2015/10/09 17:22:21
The char buf -> std::string copy can be avoided by
Primiano Tucci (use gerrit)
2015/10/12 13:00:04
Hmm but in theory I am not supposed to write to th
|
| + std::string row_name; |
| + std::string column_name; |
| + uint64 value; |
|
Robert Sesek
2015/10/09 17:22:21
Use the stdint.h types (uint64_t) rather than the
Primiano Tucci (use gerrit)
2015/10/12 13:00:04
Oh, right, a piece in my brain is still hardcoded
|
| + while ((ss >> row_name) && (ss >> value)) { |
| + // Turn entries like graphics_total into a row named "graphics" and |
| + // column named "total". |
| + const char kDumpBaseName[] = "gpu/android_memtrack/"; |
| + size_t key_split_point = row_name.find_last_of('_'); |
| + if (key_split_point > 0 && key_split_point < row_name.size() - 1) { |
|
Robert Sesek
2015/10/09 17:22:21
This will always be >0 since it's size_t (unsigned
Primiano Tucci (use gerrit)
2015/10/12 13:00:04
Well this could be 0 if the string starts with _,
|
| + column_name = row_name.substr(key_split_point + 1); |
| + row_name = row_name.substr(0, key_split_point); |
| + } else { |
| + column_name = "unknown"; |
| + } |
| + std::string dump_name = kDumpBaseName + row_name; |
| + MemoryAllocatorDump* mad = pmd->GetOrCreateAllocatorDump(dump_name); |
| + const auto& long_lived_column_name = key_names_.insert(column_name).first; |
| + mad->AddScalar(long_lived_column_name->c_str(), |
| + MemoryAllocatorDump::kUnitsBytes, value); |
| + } |
| + return true; |
| +} |
| + |
| +GraphicsMemoryDumpProvider::GraphicsMemoryDumpProvider() {} |
| + |
| +GraphicsMemoryDumpProvider::~GraphicsMemoryDumpProvider() {} |
| + |
| +} // namespace tracing |