blob: 58bbd96af7cfe44909c57464544de85ef54025f2 [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"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021#include "logd/LogReader.h"
Yao Chen49954cd2018-04-18 13:48:02 -070022#include "socket/StatsSocketListener.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070023
24#include <binder/IInterface.h>
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27#include <binder/ProcessState.h>
28#include <binder/Status.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070029#include <utils/Looper.h>
30#include <utils/StrongPointer.h>
31
32#include <stdio.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070033#include <sys/stat.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070034#include <sys/types.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070035#include <unistd.h>
36
37using namespace android;
Bookatz906a35c2017-09-20 15:26:44 -070038using namespace android::os::statsd;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070039
Yao Chen49954cd2018-04-18 13:48:02 -070040const bool kUseLogd = false;
41const bool kUseStatsdSocket = true;
42
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070043/**
44 * Thread function data.
45 */
46struct log_reader_thread_data {
47 sp<StatsService> service;
48};
49
50/**
51 * Thread func for where the log reader runs.
52 */
Yao Chenef99c4f2017-09-22 16:26:54 -070053static void* log_reader_thread_func(void* cookie) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070054 log_reader_thread_data* data = static_cast<log_reader_thread_data*>(cookie);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070055 sp<LogReader> reader = new LogReader(data->service);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070056
Joe Onorato9fc9edf2017-10-15 20:08:52 -070057 // Run the read loop. Never returns.
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070058 reader->Run();
59
60 ALOGW("statsd LogReader.Run() is not supposed to return.");
61
62 delete data;
63 return NULL;
64}
65
66/**
67 * Creates and starts the thread to own the LogReader.
68 */
Yao Chenef99c4f2017-09-22 16:26:54 -070069static status_t start_log_reader_thread(const sp<StatsService>& service) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070070 status_t err;
71 pthread_attr_t attr;
72 pthread_t thread;
73
74 // Thread data.
75 log_reader_thread_data* data = new log_reader_thread_data();
76 data->service = service;
77
78 // Create the thread
79 err = pthread_attr_init(&attr);
80 if (err != NO_ERROR) {
81 return err;
82 }
83 // TODO: Do we need to tweak thread priority?
84 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
85 if (err != NO_ERROR) {
86 pthread_attr_destroy(&attr);
87 return err;
88 }
89 err = pthread_create(&thread, &attr, log_reader_thread_func, static_cast<void*>(data));
90 if (err != NO_ERROR) {
91 pthread_attr_destroy(&attr);
92 return err;
93 }
94 pthread_attr_destroy(&attr);
95
96 return NO_ERROR;
97}
98
Yang Lu658bc0e2018-10-19 09:03:51 -070099
100sp<StatsService> gStatsService = nullptr;
101
102void sigHandler(int sig) {
103 if (gStatsService != nullptr) {
104 gStatsService->Terminate();
105 }
106}
107
108void registerSigHandler()
109{
110 struct sigaction sa;
111 sigemptyset(&sa.sa_mask);
112 sa.sa_flags = 0;
113 sa.sa_handler = sigHandler;
114 sigaction(SIGHUP, &sa, nullptr);
115 sigaction(SIGINT, &sa, nullptr);
116 sigaction(SIGQUIT, &sa, nullptr);
117 sigaction(SIGTERM, &sa, nullptr);
118}
119
Yao Chenef99c4f2017-09-22 16:26:54 -0700120int main(int /*argc*/, char** /*argv*/) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700121 // Set up the looper
122 sp<Looper> looper(Looper::prepare(0 /* opts */));
123
124 // Set up the binder
125 sp<ProcessState> ps(ProcessState::self());
Yao Chend10f7b12017-12-18 12:53:50 -0800126 ps->setThreadPoolMaxThreadCount(9);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700127 ps->startThreadPool();
128 ps->giveThreadPoolName();
129 IPCThreadState::self()->disableBackgroundScheduling(true);
130
131 // Create the service
Yang Lu658bc0e2018-10-19 09:03:51 -0700132 gStatsService = new StatsService(looper);
133 if (defaultServiceManager()->addService(String16("stats"), gStatsService) != 0) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700134 ALOGE("Failed to add service");
135 return -1;
136 }
Bookatzb487b552017-09-18 11:26:01 -0700137
Yang Lu658bc0e2018-10-19 09:03:51 -0700138 registerSigHandler();
Yao Chen49954cd2018-04-18 13:48:02 -0700139
Yang Lu658bc0e2018-10-19 09:03:51 -0700140 gStatsService->sayHiToStatsCompanion();
141
142 gStatsService->Startup();
143
144 sp<StatsSocketListener> socketListener = new StatsSocketListener(gStatsService);
Yao Chen49954cd2018-04-18 13:48:02 -0700145
146 if (kUseLogd) {
147 ALOGI("using logd");
148 // Start the log reader thread
Yang Lu658bc0e2018-10-19 09:03:51 -0700149 status_t err = start_log_reader_thread(gStatsService);
Yao Chen49954cd2018-04-18 13:48:02 -0700150 if (err != NO_ERROR) {
151 return 1;
152 }
153 }
154
155 if (kUseStatsdSocket) {
156 ALOGI("using statsd socket");
157 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
158 if (socketListener->startListener(600)) {
159 exit(1);
160 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700161 }
162
163 // Loop forever -- the reports run on this thread in a handler, and the
164 // binder calls remain responsive in their pool of one thread.
165 while (true) {
166 looper->pollAll(-1 /* timeoutMillis */);
167 }
168 ALOGW("statsd escaped from its loop.");
169
170 return 1;
171}