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); | |
35 if (sock == -1) | |
36 return false; | |
37 | |
38 // Set recv timeout to 250ms | |
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. Hard crash otherwise. | |
ericrk
2015/10/02 18:30:11
Doesn't this let a user process masquerading as ou
petrcermak
2015/10/05 08:29:27
+1. Why don't you just return false like in the re
Primiano Tucci (use gerrit)
2015/10/05 14:24:25
Makes sense. Done.
| |
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 CHECK(0); | |
64 } | |
65 | |
66 // Send the trace(PID) request. | |
67 char buf[4096]; | |
68 snprintf(buf, sizeof(buf) - 1, "%d", getpid()); | |
69 if (send(sock, buf, strlen(buf) + 1, 0) <= 0) | |
70 return false; | |
71 | |
72 // The response consists of a few lines, each one with a key value pair. E.g.: | |
73 // graphics_total 10616832 | |
74 // graphics_pss 10616832 | |
75 // gl_total 17911808 | |
76 // gl_pss 17911808 | |
77 if (recv(sock, buf, sizeof(buf), 0) <= 0) | |
78 return false; | |
79 | |
80 close(sock); | |
81 | |
82 buf[sizeof(buf) - 1] = '\0'; | |
83 std::stringstream ss(std::string(buf, sizeof(buf))); | |
84 std::string row_name; | |
85 std::string column_name; | |
86 uint64 value; | |
87 while ((ss >> row_name) && (ss >> value)) { | |
88 // Turn entries like graphics_total into a row named "graphics" and | |
89 // column named "total". | |
90 const char kDumpBaseName[] = "gpu/android_memtrack/"; | |
91 size_t key_split_point = row_name.find_last_of('_'); | |
92 if (key_split_point > 0 && key_split_point < row_name.size() - 1) { | |
93 column_name = row_name.substr(key_split_point + 1); | |
94 row_name = row_name.substr(0, key_split_point); | |
95 } else { | |
96 column_name = "unknown"; | |
97 } | |
98 std::string dump_name = kDumpBaseName + row_name; | |
99 MemoryAllocatorDump* mad = pmd->GetOrCreateAllocatorDump(dump_name); | |
100 const auto& long_lived_column_name = key_names_.insert(column_name).first; | |
101 mad->AddScalar(long_lived_column_name->c_str(), | |
102 MemoryAllocatorDump::kUnitsBytes, value); | |
103 } | |
104 return true; | |
petrcermak
2015/10/05 08:29:27
Strictly speaking, shouldn't you check that at lea
Primiano Tucci (use gerrit)
2015/10/05 14:24:25
Yeah but this seems a problem with the helper daem
| |
105 } | |
106 | |
107 GraphicsMemoryDumpProvider::GraphicsMemoryDumpProvider() {} | |
108 | |
109 GraphicsMemoryDumpProvider::~GraphicsMemoryDumpProvider() {} | |
110 | |
111 } // namespace tracing | |
OLD | NEW |