1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/profiling/memory/heapprofd.h"
18
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <array>
22 #include <memory>
23 #include <vector>
24
25 #include <signal.h>
26
27 #include "perfetto/ext/base/event_fd.h"
28 #include "perfetto/ext/base/getopt.h"
29 #include "perfetto/ext/base/scoped_file.h"
30 #include "perfetto/ext/base/unix_socket.h"
31 #include "perfetto/ext/base/watchdog.h"
32 #include "perfetto/tracing/default_socket.h"
33 #include "src/profiling/memory/heapprofd_producer.h"
34 #include "src/profiling/memory/java_hprof_producer.h"
35 #include "src/profiling/memory/wire_protocol.h"
36
37 #include "perfetto/ext/base/unix_task_runner.h"
38
39 // TODO(rsavitski): the task runner watchdog spawns a thread (normally for
40 // tracking cpu/mem usage) that we don't strictly need.
41
42 namespace perfetto {
43 namespace profiling {
44 namespace {
45
46 int StartCentralHeapprofd();
47
GetListeningSocket()48 int GetListeningSocket() {
49 const char* sock_fd = getenv(kHeapprofdSocketEnvVar);
50 if (sock_fd == nullptr)
51 PERFETTO_FATAL("Did not inherit socket from init.");
52 char* end;
53 int raw_fd = static_cast<int>(strtol(sock_fd, &end, 10));
54 if (*end != '\0')
55 PERFETTO_FATAL("Invalid %s. Expected decimal integer.",
56 kHeapprofdSocketEnvVar);
57 return raw_fd;
58 }
59
60 base::EventFd* g_dump_evt = nullptr;
61
StartCentralHeapprofd()62 int StartCentralHeapprofd() {
63 // We set this up before launching any threads, so we do not have to use a
64 // std::atomic for g_dump_evt.
65 g_dump_evt = new base::EventFd();
66
67 base::UnixTaskRunner task_runner;
68 base::Watchdog::GetInstance()->Start(); // crash on exceedingly long tasks
69 HeapprofdProducer producer(HeapprofdMode::kCentral, &task_runner,
70 /* exit_when_done= */ false);
71
72 int listening_raw_socket = GetListeningSocket();
73 auto listening_socket = base::UnixSocket::Listen(
74 base::ScopedFile(listening_raw_socket), &producer.socket_delegate(),
75 &task_runner, base::SockFamily::kUnix, base::SockType::kStream);
76
77 struct sigaction action = {};
78 action.sa_handler = [](int) { g_dump_evt->Notify(); };
79 // Allow to trigger a full dump by sending SIGUSR1 to heapprofd.
80 // This will allow manually deciding when to dump on userdebug.
81 PERFETTO_CHECK(sigaction(SIGUSR1, &action, nullptr) == 0);
82 task_runner.AddFileDescriptorWatch(g_dump_evt->fd(), [&producer] {
83 g_dump_evt->Clear();
84 producer.DumpAll();
85 });
86 producer.ConnectWithRetries(GetProducerSocket());
87 // TODO(fmayer): Create one producer that manages both heapprofd and Java
88 // producers, so we do not have two connections to traced.
89 JavaHprofProducer java_producer(&task_runner);
90 java_producer.ConnectWithRetries(GetProducerSocket());
91 task_runner.Run();
92 return 0;
93 }
94
95 } // namespace
96
HeapprofdMain(int argc,char ** argv)97 int HeapprofdMain(int argc, char** argv) {
98 bool cleanup_crash = false;
99
100 enum { kCleanupCrash = 256, kTargetPid, kTargetCmd, kInheritFd };
101 static option long_options[] = {
102 {"cleanup-after-crash", no_argument, nullptr, kCleanupCrash},
103 {nullptr, 0, nullptr, 0}};
104 int c;
105 while ((c = getopt_long(argc, argv, "", long_options, nullptr)) != -1) {
106 switch (c) {
107 case kCleanupCrash:
108 cleanup_crash = true;
109 break;
110 }
111 }
112
113 if (cleanup_crash) {
114 PERFETTO_LOG(
115 "Recovering from crash: unsetting heapprofd system properties. "
116 "Expect SELinux denials for unrelated properties.");
117 SystemProperties::ResetHeapprofdProperties();
118 PERFETTO_LOG(
119 "Finished unsetting heapprofd system properties. "
120 "SELinux denials about properties are unexpected after "
121 "this point.");
122 return 0;
123 }
124
125 // start as a central daemon.
126 return StartCentralHeapprofd();
127 }
128
129 } // namespace profiling
130 } // namespace perfetto
131