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/trace_event/process_memory_dump.h" | |
14 #include "base/trace_event/process_memory_totals.h" | |
15 | |
16 using base::trace_event::MemoryAllocatorDump; | |
17 | |
18 namespace tracing { | |
19 | |
20 // static | |
21 GraphicsMemoryDumpProvider* GraphicsMemoryDumpProvider::GetInstance() { | |
22 return base::Singleton< | |
23 GraphicsMemoryDumpProvider, | |
24 base::LeakySingletonTraits<GraphicsMemoryDumpProvider>>::get(); | |
25 } | |
26 | |
27 bool GraphicsMemoryDumpProvider::OnMemoryDump( | |
28 const base::trace_event::MemoryDumpArgs& args, | |
29 base::trace_event::ProcessMemoryDump* pmd) { | |
30 const char kAbstractSocketName[] = "chrome_tracing_memtrack_helper"; | |
31 int sock; | |
32 struct sockaddr_un addr; | |
33 | |
34 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
| |
35 if (sock == -1) | |
36 return false; | |
37 | |
38 // 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.
| |
39 struct timeval tv; | |
40 tv.tv_sec = 0; | |
41 tv.tv_usec = 250000; | |
42 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
43 | |
44 // Connect to the UNIX abstract (i.e. no physical filesystem link) socket. | |
45 memset(&addr, 0, sizeof(addr)); | |
46 addr.sun_family = AF_UNIX; | |
47 strncpy(&addr.sun_path[1], kAbstractSocketName, sizeof(addr.sun_path) - 2); | |
48 | |
49 if (connect(sock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) { | |
50 LOG(WARNING) << "Could not connect to the memtrack_helper daemon. Please " | |
51 "build memtrack_helper, adb push to the device and run it " | |
52 "before starting the trace to get graphics memory data."; | |
53 return false; | |
54 } | |
55 | |
56 // Check that the socket is owned by root (the memtrack_helper) process and | |
57 // not an (untrusted) user process. | |
58 struct ucred cred; | |
59 socklen_t cred_len = sizeof(cred); | |
60 if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0 || | |
61 (static_cast<unsigned>(cred_len) < sizeof(cred)) || | |
62 cred.uid != 0 /* root */) { | |
63 LOG(WARNING) << "Untrusted (!= root) memtrack_helper daemon detected."; | |
64 return false; | |
65 } | |
66 | |
67 // Send the trace(PID) request. | |
68 char buf[4096]; | |
69 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
| |
70 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
| |
71 return false; | |
72 | |
73 // The response consists of a few lines, each one with a key value pair. E.g.: | |
74 // graphics_total 10616832 | |
75 // graphics_pss 10616832 | |
76 // gl_total 17911808 | |
77 // gl_pss 17911808 | |
78 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.
| |
79 return false; | |
80 | |
81 close(sock); | |
82 | |
83 buf[sizeof(buf) - 1] = '\0'; | |
84 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
| |
85 std::string row_name; | |
86 std::string column_name; | |
87 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
| |
88 while ((ss >> row_name) && (ss >> value)) { | |
89 // Turn entries like graphics_total into a row named "graphics" and | |
90 // column named "total". | |
91 const char kDumpBaseName[] = "gpu/android_memtrack/"; | |
92 size_t key_split_point = row_name.find_last_of('_'); | |
93 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 _,
| |
94 column_name = row_name.substr(key_split_point + 1); | |
95 row_name = row_name.substr(0, key_split_point); | |
96 } else { | |
97 column_name = "unknown"; | |
98 } | |
99 std::string dump_name = kDumpBaseName + row_name; | |
100 MemoryAllocatorDump* mad = pmd->GetOrCreateAllocatorDump(dump_name); | |
101 const auto& long_lived_column_name = key_names_.insert(column_name).first; | |
102 mad->AddScalar(long_lived_column_name->c_str(), | |
103 MemoryAllocatorDump::kUnitsBytes, value); | |
104 } | |
105 return true; | |
106 } | |
107 | |
108 GraphicsMemoryDumpProvider::GraphicsMemoryDumpProvider() {} | |
109 | |
110 GraphicsMemoryDumpProvider::~GraphicsMemoryDumpProvider() {} | |
111 | |
112 } // namespace tracing | |
OLD | NEW |