blob: 140ef4e9cea9f1a17dc1e6e377eb32a47ab995bc [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
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
25#include <binder/ProcessState.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;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070035
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070036/**
37 * Thread function data.
38 */
39struct log_reader_thread_data {
40 sp<StatsService> service;
41};
42
Yangster-mac97e7d202018-10-09 11:05:39 -070043
44sp<StatsService> gStatsService = nullptr;
45
46void sigHandler(int sig) {
47 if (gStatsService != nullptr) {
48 gStatsService->Terminate();
49 }
Eric Jeong2d997182019-10-03 13:33:48 -070050 ALOGW("statsd terminated on receiving signal %d.", sig);
51 exit(1);
Yangster-mac97e7d202018-10-09 11:05:39 -070052}
53
54void registerSigHandler()
55{
56 struct sigaction sa;
57 sigemptyset(&sa.sa_mask);
58 sa.sa_flags = 0;
59 sa.sa_handler = sigHandler;
60 sigaction(SIGHUP, &sa, nullptr);
61 sigaction(SIGINT, &sa, nullptr);
62 sigaction(SIGQUIT, &sa, nullptr);
63 sigaction(SIGTERM, &sa, nullptr);
64}
65
Yao Chenef99c4f2017-09-22 16:26:54 -070066int main(int /*argc*/, char** /*argv*/) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070067 // Set up the looper
68 sp<Looper> looper(Looper::prepare(0 /* opts */));
69
70 // Set up the binder
71 sp<ProcessState> ps(ProcessState::self());
Yao Chend10f7b12017-12-18 12:53:50 -080072 ps->setThreadPoolMaxThreadCount(9);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070073 ps->startThreadPool();
74 ps->giveThreadPoolName();
75 IPCThreadState::self()->disableBackgroundScheduling(true);
76
Yao Chen0f861862019-03-27 11:51:15 -070077 std::shared_ptr<LogEventQueue> eventQueue =
78 std::make_shared<LogEventQueue>(2000 /*buffer limit. Buffer is NOT pre-allocated*/);
79
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070080 // Create the service
Yao Chen0f861862019-03-27 11:51:15 -070081 gStatsService = new StatsService(looper, eventQueue);
Bookatzff71cad2018-09-20 17:17:49 -070082 if (defaultServiceManager()->addService(String16("stats"), gStatsService, false,
83 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL | IServiceManager::DUMP_FLAG_PROTO)
84 != 0) {
Howard Roa46b6582018-09-18 16:45:02 -070085 ALOGE("Failed to add service as AIDL service");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070086 return -1;
87 }
Howard Roa46b6582018-09-18 16:45:02 -070088
Yangster-mac97e7d202018-10-09 11:05:39 -070089 registerSigHandler();
Bookatzb487b552017-09-18 11:26:01 -070090
Yangster-mac97e7d202018-10-09 11:05:39 -070091 gStatsService->sayHiToStatsCompanion();
Yao Chen49954cd2018-04-18 13:48:02 -070092
Yangster-mac97e7d202018-10-09 11:05:39 -070093 gStatsService->Startup();
94
Yao Chen0f861862019-03-27 11:51:15 -070095 sp<StatsSocketListener> socketListener = new StatsSocketListener(eventQueue);
Yao Chen49954cd2018-04-18 13:48:02 -070096
Yao Chen0f861862019-03-27 11:51:15 -070097 ALOGI("Statsd starts to listen to socket.");
98 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
99 if (socketListener->startListener(600)) {
100 exit(1);
101 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700102
103 // Loop forever -- the reports run on this thread in a handler, and the
104 // binder calls remain responsive in their pool of one thread.
105 while (true) {
106 looper->pollAll(-1 /* timeoutMillis */);
107 }
108 ALOGW("statsd escaped from its loop.");
109
110 return 1;
111}