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
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "perfetto/ext/base/getopt.h"
23 #include "perfetto/ext/base/file_utils.h"
24 #include "perfetto/ext/base/unix_task_runner.h"
25 #include "perfetto/ext/base/version.h"
26 #include "perfetto/tracing/default_socket.h"
27 #include "src/profiling/perf/perf_producer.h"
28 #include "src/profiling/perf/proc_descriptors.h"
29
30 namespace perfetto {
31
32 namespace {
33 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
34 static constexpr char kTracedPerfSocketEnvVar[] = "ANDROID_SOCKET_traced_perf";
35
GetRawInheritedListeningSocket()36 int GetRawInheritedListeningSocket() {
37 const char* sock_fd = getenv(kTracedPerfSocketEnvVar);
38 if (sock_fd == nullptr)
39 PERFETTO_FATAL("Did not inherit socket from init.");
40 char* end;
41 int raw_fd = static_cast<int>(strtol(sock_fd, &end, 10));
42 if (*end != '\0')
43 PERFETTO_FATAL("Invalid env variable format. Expected decimal integer.");
44 return raw_fd;
45 }
46 #endif
47 } // namespace
48
49 // TODO(rsavitski): watchdog.
TracedPerfMain(int argc,char ** argv)50 int TracedPerfMain(int argc, char** argv) {
51 enum LongOption {
52 OPT_BACKGROUND = 1000,
53 OPT_VERSION,
54 };
55
56 bool background = false;
57
58 static const option long_options[] = {
59 {"background", no_argument, nullptr, OPT_BACKGROUND},
60 {"version", no_argument, nullptr, OPT_VERSION},
61 {nullptr, 0, nullptr, 0}};
62
63 for (;;) {
64 int option = getopt_long(argc, argv, "", long_options, nullptr);
65 if (option == -1)
66 break;
67 switch (option) {
68 case OPT_BACKGROUND:
69 background = true;
70 break;
71 case OPT_VERSION:
72 printf("%s\n", base::GetVersionString());
73 return 0;
74 default:
75 fprintf(stderr, "Usage: %s [--background] [--version]\n", argv[0]);
76 return 1;
77 }
78 }
79
80 if (background) {
81 base::Daemonize([] { return 0; });
82 }
83
84 base::UnixTaskRunner task_runner;
85
86 // TODO(rsavitski): support standalone --root or similar on android.
87 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
88 AndroidRemoteDescriptorGetter proc_fd_getter{GetRawInheritedListeningSocket(),
89 &task_runner};
90 #else
91 DirectDescriptorGetter proc_fd_getter;
92 #endif
93
94 profiling::PerfProducer producer(&proc_fd_getter, &task_runner);
95 const char* env_notif = getenv("TRACED_PERF_NOTIFY_FD");
96 if (env_notif) {
97 int notif_fd = atoi(env_notif);
98 producer.SetAllDataSourcesRegisteredCb([notif_fd] {
99 PERFETTO_CHECK(base::WriteAll(notif_fd, "1", 1) == 1);
100 PERFETTO_CHECK(base::CloseFile(notif_fd) == 0);
101 });
102 }
103 producer.ConnectWithRetries(GetProducerSocket());
104 task_runner.Run();
105 return 0;
106 }
107
108 } // namespace perfetto
109