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