blob: 161b6304ea8036478564f0dac4e088a318c90dbc [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
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
17#define LOG_TAG "statsd"
18
19#include "LogEntryPrinter.h"
20#include "LogReader.h"
21#include "StatsService.h"
Yao Chenab273e22017-09-06 12:53:50 -070022#include "StatsLogProcessor.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>
29#include <cutils/log.h>
30#include <utils/Looper.h>
31#include <utils/StrongPointer.h>
32
33#include <stdio.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <unistd.h>
37
38using namespace android;
39
40// ================================================================================
41/**
42 * Thread function data.
43 */
44struct log_reader_thread_data {
45 sp<StatsService> service;
46};
47
48/**
49 * Thread func for where the log reader runs.
50 */
51static void*
52log_reader_thread_func(void* cookie)
53{
54 log_reader_thread_data* data = static_cast<log_reader_thread_data*>(cookie);
55
56 sp<LogReader> reader = new LogReader();
57
58 // Put the printer one first, so it will print before the real ones.
Yao Chenab273e22017-09-06 12:53:50 -070059 reader->AddListener(new LogEntryPrinter(STDOUT_FILENO));
David Chen0656b7a2017-09-13 15:53:39 -070060 sp<StatsLogProcessor> main_processor = new StatsLogProcessor();
61 data->service->setProcessor(main_processor);
62 reader->AddListener(main_processor);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070063
64 // TODO: Construct and add real LogListners here.
65
66 reader->Run();
67
68 ALOGW("statsd LogReader.Run() is not supposed to return.");
69
70 delete data;
71 return NULL;
72}
73
74/**
75 * Creates and starts the thread to own the LogReader.
76 */
77static status_t
78start_log_reader_thread(const sp<StatsService>& service)
79{
80 status_t err;
81 pthread_attr_t attr;
82 pthread_t thread;
83
84 // Thread data.
85 log_reader_thread_data* data = new log_reader_thread_data();
86 data->service = service;
87
88 // Create the thread
89 err = pthread_attr_init(&attr);
90 if (err != NO_ERROR) {
91 return err;
92 }
93 // TODO: Do we need to tweak thread priority?
94 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
95 if (err != NO_ERROR) {
96 pthread_attr_destroy(&attr);
97 return err;
98 }
99 err = pthread_create(&thread, &attr, log_reader_thread_func, static_cast<void*>(data));
100 if (err != NO_ERROR) {
101 pthread_attr_destroy(&attr);
102 return err;
103 }
104 pthread_attr_destroy(&attr);
105
106 return NO_ERROR;
107}
108
109// ================================================================================
110int
111main(int /*argc*/, char** /*argv*/)
112{
113 status_t err;
114
115 // Set up the looper
116 sp<Looper> looper(Looper::prepare(0 /* opts */));
117
118 // Set up the binder
119 sp<ProcessState> ps(ProcessState::self());
120 ps->setThreadPoolMaxThreadCount(1); // everything is oneway, let it queue and save ram
121 ps->startThreadPool();
122 ps->giveThreadPoolName();
123 IPCThreadState::self()->disableBackgroundScheduling(true);
124
125 // Create the service
126 sp<StatsService> service = new StatsService(looper);
127 if (defaultServiceManager()->addService(String16("stats"), service) != 0) {
128 ALOGE("Failed to add service");
129 return -1;
130 }
131
Bookatzb487b552017-09-18 11:26:01 -0700132 // TODO: This line is temporary, since statsd doesn't start up automatically (and therefore
133 // the call in StatsService::SystemRunning() won't ever be called right now).
134 service->sayHiToStatsCompanion();
135
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700136 // Start the log reader thread
137 err = start_log_reader_thread(service);
138 if (err != NO_ERROR) {
139 return 1;
140 }
141
142 // Loop forever -- the reports run on this thread in a handler, and the
143 // binder calls remain responsive in their pool of one thread.
144 while (true) {
145 looper->pollAll(-1 /* timeoutMillis */);
146 }
147 ALOGW("statsd escaped from its loop.");
148
149 return 1;
150}