1 /*
2 * Copyright (C) 2019 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/perf/traced_perf.h"
18 #include "perfetto/ext/base/unix_task_runner.h"
19 #include "perfetto/ext/tracing/ipc/default_socket.h"
20 #include "src/profiling/perf/perf_producer.h"
21 #include "src/profiling/perf/proc_descriptors.h"
22
23 namespace perfetto {
24
25 namespace {
26 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
27 static constexpr char kTracedPerfSocketEnvVar[] = "ANDROID_SOCKET_traced_perf";
28
GetRawInheritedListeningSocket()29 int GetRawInheritedListeningSocket() {
30 const char* sock_fd = getenv(kTracedPerfSocketEnvVar);
31 if (sock_fd == nullptr)
32 PERFETTO_FATAL("Did not inherit socket from init.");
33 char* end;
34 int raw_fd = static_cast<int>(strtol(sock_fd, &end, 10));
35 if (*end != '\0')
36 PERFETTO_FATAL("Invalid env variable format. Expected decimal integer.");
37 return raw_fd;
38 }
39 #endif
40 } // namespace
41
42 // TODO(rsavitski): watchdog.
TracedPerfMain(int,char **)43 int TracedPerfMain(int, char**) {
44 base::UnixTaskRunner task_runner;
45
46 // TODO(rsavitski): support standalone --root or similar on android.
47 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
48 AndroidRemoteDescriptorGetter proc_fd_getter{GetRawInheritedListeningSocket(),
49 &task_runner};
50 #else
51 DirectDescriptorGetter proc_fd_getter;
52 #endif
53
54 profiling::PerfProducer producer(&proc_fd_getter, &task_runner);
55 producer.ConnectWithRetries(GetProducerSocket());
56 task_runner.Run();
57 return 0;
58 }
59
60 } // namespace perfetto
61