blob: 2f15d0fe0b71e0e179653f8cd97c1e0d3d8df9ba [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
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
Yao Chen49954cd2018-04-18 13:48:02 -070042const bool kUseLogd = false;
43const bool kUseStatsdSocket = true;
44
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070045/**
46 * Thread function data.
47 */
48struct log_reader_thread_data {
49 sp<StatsService> service;
50};
51
52/**
53 * Thread func for where the log reader runs.
54 */
Yao Chenef99c4f2017-09-22 16:26:54 -070055static void* log_reader_thread_func(void* cookie) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070056 log_reader_thread_data* data = static_cast<log_reader_thread_data*>(cookie);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070057 sp<LogReader> reader = new LogReader(data->service);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070058
Joe Onorato9fc9edf2017-10-15 20:08:52 -070059 // Run the read loop. Never returns.
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070060 reader->Run();
61
62 ALOGW("statsd LogReader.Run() is not supposed to return.");
63
64 delete data;
65 return NULL;
66}
67
68/**
69 * Creates and starts the thread to own the LogReader.
70 */
Yao Chenef99c4f2017-09-22 16:26:54 -070071static status_t start_log_reader_thread(const sp<StatsService>& service) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070072 status_t err;
73 pthread_attr_t attr;
74 pthread_t thread;
75
76 // Thread data.
George Burgess IVef8262c2018-04-23 09:32:41 -070077 std::unique_ptr<log_reader_thread_data> data = std::make_unique<log_reader_thread_data>();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070078 data->service = service;
79
80 // Create the thread
81 err = pthread_attr_init(&attr);
82 if (err != NO_ERROR) {
83 return err;
84 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070085 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
86 if (err != NO_ERROR) {
87 pthread_attr_destroy(&attr);
88 return err;
89 }
George Burgess IVef8262c2018-04-23 09:32:41 -070090 err = pthread_create(&thread, &attr, log_reader_thread_func,
91 static_cast<void*>(data.get()));
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070092 if (err != NO_ERROR) {
93 pthread_attr_destroy(&attr);
94 return err;
95 }
George Burgess IVef8262c2018-04-23 09:32:41 -070096 // Release here rather than in pthread_create, since an error creating the
97 // thread leaves `data` ownerless.
98 data.release();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070099 pthread_attr_destroy(&attr);
100
101 return NO_ERROR;
102}
103
Yao Chenef99c4f2017-09-22 16:26:54 -0700104int main(int /*argc*/, char** /*argv*/) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700105 // Set up the looper
106 sp<Looper> looper(Looper::prepare(0 /* opts */));
107
108 // Set up the binder
109 sp<ProcessState> ps(ProcessState::self());
Yao Chend10f7b12017-12-18 12:53:50 -0800110 ps->setThreadPoolMaxThreadCount(9);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700111 ps->startThreadPool();
112 ps->giveThreadPoolName();
113 IPCThreadState::self()->disableBackgroundScheduling(true);
114
115 // Create the service
116 sp<StatsService> service = new StatsService(looper);
117 if (defaultServiceManager()->addService(String16("stats"), service) != 0) {
118 ALOGE("Failed to add service");
119 return -1;
120 }
Bookatzb487b552017-09-18 11:26:01 -0700121 service->sayHiToStatsCompanion();
122
Yao Chen49954cd2018-04-18 13:48:02 -0700123 service->Startup();
124
125 sp<StatsSocketListener> socketListener = new StatsSocketListener(service);
126
127 if (kUseLogd) {
128 ALOGI("using logd");
129 // Start the log reader thread
130 status_t err = start_log_reader_thread(service);
131 if (err != NO_ERROR) {
132 return 1;
133 }
134 }
135
136 if (kUseStatsdSocket) {
137 ALOGI("using statsd socket");
138 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
139 if (socketListener->startListener(600)) {
140 exit(1);
141 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700142 }
143
144 // Loop forever -- the reports run on this thread in a handler, and the
145 // binder calls remain responsive in their pool of one thread.
146 while (true) {
147 looper->pollAll(-1 /* timeoutMillis */);
148 }
149 ALOGW("statsd escaped from its loop.");
150
151 return 1;
152}