blob: b303321d8ef264b106fbc96b4a43bb7de09ebe41 [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"
Yao Chenab273e22017-09-06 12:53:50 -070021#include "StatsLogProcessor.h"
Yao Chenef99c4f2017-09-22 16:26:54 -070022#include "StatsService.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>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070034#include <sys/stat.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070035#include <sys/types.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070036#include <unistd.h>
37
38using namespace android;
Bookatz906a35c2017-09-20 15:26:44 -070039using namespace android::os::statsd;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070040
41// ================================================================================
42/**
43 * Thread function data.
44 */
45struct log_reader_thread_data {
46 sp<StatsService> service;
47};
48
49/**
50 * Thread func for where the log reader runs.
51 */
Yao Chenef99c4f2017-09-22 16:26:54 -070052static void* log_reader_thread_func(void* cookie) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070053 log_reader_thread_data* data = static_cast<log_reader_thread_data*>(cookie);
54
55 sp<LogReader> reader = new LogReader();
56
57 // Put the printer one first, so it will print before the real ones.
Yao Chenab273e22017-09-06 12:53:50 -070058 reader->AddListener(new LogEntryPrinter(STDOUT_FILENO));
David Chen0656b7a2017-09-13 15:53:39 -070059 sp<StatsLogProcessor> main_processor = new StatsLogProcessor();
60 data->service->setProcessor(main_processor);
61 reader->AddListener(main_processor);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070062
63 // TODO: Construct and add real LogListners here.
64
65 reader->Run();
66
67 ALOGW("statsd LogReader.Run() is not supposed to return.");
68
69 delete data;
70 return NULL;
71}
72
73/**
74 * Creates and starts the thread to own the LogReader.
75 */
Yao Chenef99c4f2017-09-22 16:26:54 -070076static status_t start_log_reader_thread(const sp<StatsService>& service) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070077 status_t err;
78 pthread_attr_t attr;
79 pthread_t thread;
80
81 // Thread data.
82 log_reader_thread_data* data = new log_reader_thread_data();
83 data->service = service;
84
85 // Create the thread
86 err = pthread_attr_init(&attr);
87 if (err != NO_ERROR) {
88 return err;
89 }
90 // TODO: Do we need to tweak thread priority?
91 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
92 if (err != NO_ERROR) {
93 pthread_attr_destroy(&attr);
94 return err;
95 }
96 err = pthread_create(&thread, &attr, log_reader_thread_func, static_cast<void*>(data));
97 if (err != NO_ERROR) {
98 pthread_attr_destroy(&attr);
99 return err;
100 }
101 pthread_attr_destroy(&attr);
102
103 return NO_ERROR;
104}
105
106// ================================================================================
Yao Chenef99c4f2017-09-22 16:26:54 -0700107int main(int /*argc*/, char** /*argv*/) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700108 status_t err;
109
110 // Set up the looper
111 sp<Looper> looper(Looper::prepare(0 /* opts */));
112
113 // Set up the binder
114 sp<ProcessState> ps(ProcessState::self());
Yao Chenef99c4f2017-09-22 16:26:54 -0700115 ps->setThreadPoolMaxThreadCount(1); // everything is oneway, let it queue and save ram
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700116 ps->startThreadPool();
117 ps->giveThreadPoolName();
118 IPCThreadState::self()->disableBackgroundScheduling(true);
119
120 // Create the service
121 sp<StatsService> service = new StatsService(looper);
122 if (defaultServiceManager()->addService(String16("stats"), service) != 0) {
123 ALOGE("Failed to add service");
124 return -1;
125 }
126
Bookatzb487b552017-09-18 11:26:01 -0700127 // TODO: This line is temporary, since statsd doesn't start up automatically (and therefore
128 // the call in StatsService::SystemRunning() won't ever be called right now).
129 service->sayHiToStatsCompanion();
130
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700131 // Start the log reader thread
132 err = start_log_reader_thread(service);
133 if (err != NO_ERROR) {
134 return 1;
135 }
136
137 // Loop forever -- the reports run on this thread in a handler, and the
138 // binder calls remain responsive in their pool of one thread.
139 while (true) {
140 looper->pollAll(-1 /* timeoutMillis */);
141 }
142 ALOGW("statsd escaped from its loop.");
143
144 return 1;
145}