blob: 7d446a9a1ed603c78a766d352457c9735515803f [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
23#include <binder/IInterface.h>
24#include <binder/IPCThreadState.h>
25#include <binder/IServiceManager.h>
26#include <binder/ProcessState.h>
27#include <binder/Status.h>
Howard Roa46b6582018-09-18 16:45:02 -070028#include <hidl/HidlTransportSupport.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070029#include <utils/Looper.h>
30#include <utils/StrongPointer.h>
31
George Burgess IVef8262c2018-04-23 09:32:41 -070032#include <memory>
33
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070034#include <stdio.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070035#include <sys/stat.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070036#include <sys/types.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070037#include <unistd.h>
38
39using namespace android;
Bookatz906a35c2017-09-20 15:26:44 -070040using namespace android::os::statsd;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070041
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070042/**
43 * Thread function data.
44 */
45struct log_reader_thread_data {
46 sp<StatsService> service;
47};
48
Yangster-mac97e7d202018-10-09 11:05:39 -070049
50sp<StatsService> gStatsService = nullptr;
51
52void sigHandler(int sig) {
53 if (gStatsService != nullptr) {
54 gStatsService->Terminate();
55 }
Howard Ro48dfcdb2019-10-14 13:56:41 -070056 ALOGW("statsd terminated on receiving signal %d.", sig);
57 exit(1);
Yangster-mac97e7d202018-10-09 11:05:39 -070058}
59
60void registerSigHandler()
61{
62 struct sigaction sa;
63 sigemptyset(&sa.sa_mask);
64 sa.sa_flags = 0;
65 sa.sa_handler = sigHandler;
66 sigaction(SIGHUP, &sa, nullptr);
67 sigaction(SIGINT, &sa, nullptr);
68 sigaction(SIGQUIT, &sa, nullptr);
69 sigaction(SIGTERM, &sa, nullptr);
70}
71
Yao Chenef99c4f2017-09-22 16:26:54 -070072int main(int /*argc*/, char** /*argv*/) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070073 // Set up the looper
74 sp<Looper> looper(Looper::prepare(0 /* opts */));
75
76 // Set up the binder
77 sp<ProcessState> ps(ProcessState::self());
Yao Chend10f7b12017-12-18 12:53:50 -080078 ps->setThreadPoolMaxThreadCount(9);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070079 ps->startThreadPool();
80 ps->giveThreadPoolName();
81 IPCThreadState::self()->disableBackgroundScheduling(true);
82
Tej Singh2e477bf2019-08-28 11:29:06 -070083 ::android::hardware::configureRpcThreadpool(4 /*threads*/, false /*willJoin*/);
Howard Roa46b6582018-09-18 16:45:02 -070084
Yao Chen0f861862019-03-27 11:51:15 -070085 std::shared_ptr<LogEventQueue> eventQueue =
86 std::make_shared<LogEventQueue>(2000 /*buffer limit. Buffer is NOT pre-allocated*/);
87
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070088 // Create the service
Yao Chen0f861862019-03-27 11:51:15 -070089 gStatsService = new StatsService(looper, eventQueue);
Bookatzff71cad2018-09-20 17:17:49 -070090 if (defaultServiceManager()->addService(String16("stats"), gStatsService, false,
91 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL | IServiceManager::DUMP_FLAG_PROTO)
92 != 0) {
Howard Roa46b6582018-09-18 16:45:02 -070093 ALOGE("Failed to add service as AIDL service");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070094 return -1;
95 }
Howard Roa46b6582018-09-18 16:45:02 -070096
Yangster-mac97e7d202018-10-09 11:05:39 -070097 auto ret = gStatsService->registerAsService();
Howard Roa46b6582018-09-18 16:45:02 -070098 if (ret != ::android::OK) {
99 ALOGE("Failed to add service as HIDL service");
100 return 1; // or handle error
101 }
102
Yangster-mac97e7d202018-10-09 11:05:39 -0700103 registerSigHandler();
Bookatzb487b552017-09-18 11:26:01 -0700104
Yangster-mac97e7d202018-10-09 11:05:39 -0700105 gStatsService->sayHiToStatsCompanion();
Yao Chen49954cd2018-04-18 13:48:02 -0700106
Yangster-mac97e7d202018-10-09 11:05:39 -0700107 gStatsService->Startup();
108
Yao Chen0f861862019-03-27 11:51:15 -0700109 sp<StatsSocketListener> socketListener = new StatsSocketListener(eventQueue);
Yao Chen49954cd2018-04-18 13:48:02 -0700110
Yao Chen0f861862019-03-27 11:51:15 -0700111 ALOGI("Statsd starts to listen to socket.");
112 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
113 if (socketListener->startListener(600)) {
114 exit(1);
115 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700116
117 // Loop forever -- the reports run on this thread in a handler, and the
118 // binder calls remain responsive in their pool of one thread.
119 while (true) {
120 looper->pollAll(-1 /* timeoutMillis */);
121 }
122 ALOGW("statsd escaped from its loop.");
123
124 return 1;
125}