1 //
2 // Copyright (C) 2020 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 #define LOG_TAG "profcollectd"
18 
19 #include "libprofcollectd.h"
20 
21 #include <android-base/logging.h>
22 
23 #include <iostream>
24 
25 #include "binder_service.h"
26 
27 namespace android {
28 namespace profcollectd {
29 
30 using ::com::android::server::profcollect::IProfCollectd;
31 
32 namespace {
33 
GetIProfcollectdService()34 sp<IProfCollectd> GetIProfcollectdService() {
35   sp<ProcessState> proc(ProcessState::self());
36   sp<IBinder> binder =
37       defaultServiceManager()->getService(String16(ProfcollectdBinder::getServiceName()));
38   if (!binder) {
39     std::cerr << "Cannot connect to the daemon, is it running?" << std::endl;
40     exit(EXIT_FAILURE);
41   }
42 
43   return interface_cast<IProfCollectd>(binder);
44 }
45 
46 }  // namespace
47 
InitService(bool start)48 void InitService(bool start) {
49   if (defaultServiceManager()->checkService(String16(ProfcollectdBinder::getServiceName()))) {
50     ALOGE("Another instance of profcollectd is already running");
51     exit(EXIT_FAILURE);
52   }
53 
54   sp<ProcessState> proc(ProcessState::self());
55   sp<IServiceManager> sm(defaultServiceManager());
56   auto svc = sp<ProfcollectdBinder>(new ProfcollectdBinder());
57   sm->addService(String16(ProfcollectdBinder::getServiceName()), svc);
58   if (start) {
59     svc->ScheduleCollection();
60   }
61   ProcessState::self()->startThreadPool();
62   IPCThreadState::self()->joinThreadPool();
63 }
64 
ScheduleCollection()65 void ScheduleCollection() {
66   GetIProfcollectdService()->ScheduleCollection();
67 }
68 
TerminateCollection()69 void TerminateCollection() {
70   GetIProfcollectdService()->TerminateCollection();
71 }
72 
TraceOnce()73 void TraceOnce() {
74   GetIProfcollectdService()->TraceOnce("manual");
75 }
76 
Process()77 void Process() {
78   GetIProfcollectdService()->ProcessProfile();
79 }
80 
ReadConfig()81 void ReadConfig() {
82   GetIProfcollectdService()->ReadConfig();
83 }
84 
85 }  // namespace profcollectd
86 }  // namespace android
87