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