blob: d79b6a21c19fa5b4544f291663161992be3b091d [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
yro0feae942017-11-15 14:38:48 -08002 * Copyright (C) 2017 The Android Open Source Project
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07003 *
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
Yao Chen49954cd2018-04-18 13:48:02 -070017#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070019
Yao Chenef99c4f2017-09-22 16:26:54 -070020#include "StatsService.h"
Yao Chen49954cd2018-04-18 13:48:02 -070021#include "socket/StatsSocketListener.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070022
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080023#include <android/binder_interface_utils.h>
24#include <android/binder_process.h>
25#include <android/binder_manager.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070026#include <utils/Looper.h>
George Burgess IVef8262c2018-04-23 09:32:41 -070027
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070028#include <stdio.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070029#include <sys/stat.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070030#include <sys/types.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070031#include <unistd.h>
32
33using namespace android;
Bookatz906a35c2017-09-20 15:26:44 -070034using namespace android::os::statsd;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080035using ::ndk::SharedRefBase;
36using std::shared_ptr;
37using std::make_shared;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070038
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080039shared_ptr<StatsService> gStatsService = nullptr;
Yangster-mac97e7d202018-10-09 11:05:39 -070040
41void sigHandler(int sig) {
42 if (gStatsService != nullptr) {
43 gStatsService->Terminate();
44 }
Eric Jeong2d997182019-10-03 13:33:48 -070045 ALOGW("statsd terminated on receiving signal %d.", sig);
46 exit(1);
Yangster-mac97e7d202018-10-09 11:05:39 -070047}
48
49void registerSigHandler()
50{
51 struct sigaction sa;
52 sigemptyset(&sa.sa_mask);
53 sa.sa_flags = 0;
54 sa.sa_handler = sigHandler;
55 sigaction(SIGHUP, &sa, nullptr);
56 sigaction(SIGINT, &sa, nullptr);
57 sigaction(SIGQUIT, &sa, nullptr);
58 sigaction(SIGTERM, &sa, nullptr);
59}
60
Yao Chenef99c4f2017-09-22 16:26:54 -070061int main(int /*argc*/, char** /*argv*/) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070062 // Set up the looper
63 sp<Looper> looper(Looper::prepare(0 /* opts */));
64
65 // Set up the binder
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080066 ABinderProcess_setThreadPoolMaxThreadCount(9);
67 ABinderProcess_startThreadPool();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070068
Yao Chen0f861862019-03-27 11:51:15 -070069 std::shared_ptr<LogEventQueue> eventQueue =
70 std::make_shared<LogEventQueue>(2000 /*buffer limit. Buffer is NOT pre-allocated*/);
71
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070072 // Create the service
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080073 gStatsService = SharedRefBase::make<StatsService>(looper, eventQueue);
74 // TODO(b/149582373): Set DUMP_FLAG_PROTO once libbinder_ndk supports
75 // setting dumpsys priorities.
76 binder_status_t status = AServiceManager_addService(gStatsService->asBinder().get(), "stats");
77 if (status != STATUS_OK) {
Howard Roa46b6582018-09-18 16:45:02 -070078 ALOGE("Failed to add service as AIDL service");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070079 return -1;
80 }
Howard Roa46b6582018-09-18 16:45:02 -070081
Yangster-mac97e7d202018-10-09 11:05:39 -070082 registerSigHandler();
Bookatzb487b552017-09-18 11:26:01 -070083
Yangster-mac97e7d202018-10-09 11:05:39 -070084 gStatsService->sayHiToStatsCompanion();
Yao Chen49954cd2018-04-18 13:48:02 -070085
Yangster-mac97e7d202018-10-09 11:05:39 -070086 gStatsService->Startup();
87
Yao Chen0f861862019-03-27 11:51:15 -070088 sp<StatsSocketListener> socketListener = new StatsSocketListener(eventQueue);
Yao Chen49954cd2018-04-18 13:48:02 -070089
Yao Chen0f861862019-03-27 11:51:15 -070090 ALOGI("Statsd starts to listen to socket.");
91 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
92 if (socketListener->startListener(600)) {
93 exit(1);
94 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070095
96 // Loop forever -- the reports run on this thread in a handler, and the
97 // binder calls remain responsive in their pool of one thread.
98 while (true) {
99 looper->pollAll(-1 /* timeoutMillis */);
100 }
101 ALOGW("statsd escaped from its loop.");
102
103 return 1;
104}