1 /*
2 * Copyright (C) 2017 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 "perfetto/base/unix_task_runner.h"
18 #include "perfetto/base/watchdog.h"
19 #include "perfetto/traced/traced.h"
20 #include "perfetto/tracing/ipc/service_ipc_host.h"
21 #include "src/tracing/ipc/default_socket.h"
22
23 namespace perfetto {
24
ServiceMain(int,char **)25 int __attribute__((visibility("default"))) ServiceMain(int, char**) {
26 base::UnixTaskRunner task_runner;
27 std::unique_ptr<ServiceIPCHost> svc;
28 svc = ServiceIPCHost::CreateInstance(&task_runner);
29
30 // When built as part of the Android tree, the two socket are created and
31 // bonund by init and their fd number is passed in two env variables.
32 // See libcutils' android_get_control_socket().
33 const char* env_prod = getenv("ANDROID_SOCKET_traced_producer");
34 const char* env_cons = getenv("ANDROID_SOCKET_traced_consumer");
35 PERFETTO_CHECK((!env_prod && !env_cons) || (env_prod && env_cons));
36 if (env_prod) {
37 base::ScopedFile producer_fd(atoi(env_prod));
38 base::ScopedFile consumer_fd(atoi(env_cons));
39 svc->Start(std::move(producer_fd), std::move(consumer_fd));
40 } else {
41 unlink(GetProducerSocket());
42 unlink(GetConsumerSocket());
43 svc->Start(GetProducerSocket(), GetConsumerSocket());
44 }
45
46 // Set the CPU limit and start the watchdog running. The memory limit will
47 // be set inside the service code as it relies on the size of buffers.
48 // The CPU limit is 75% over a 30 second interval.
49 base::Watchdog* watchdog = base::Watchdog::GetInstance();
50 watchdog->SetCpuLimit(75, 30 * 1000);
51 watchdog->Start();
52
53 PERFETTO_ILOG("Started traced, listening on %s %s", GetProducerSocket(),
54 GetConsumerSocket());
55 task_runner.Run();
56 return 0;
57 }
58
59 } // namespace perfetto
60