blob: c1dad4f1ed3f31c5cc07ddb370123e8fe747fe56 [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;
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 */
52static void*
53log_reader_thread_func(void* cookie)
54{
55 log_reader_thread_data* data = static_cast<log_reader_thread_data*>(cookie);
56
57 sp<LogReader> reader = new LogReader();
58
59 // Put the printer one first, so it will print before the real ones.
Yao Chenab273e22017-09-06 12:53:50 -070060 reader->AddListener(new LogEntryPrinter(STDOUT_FILENO));
David Chen0656b7a2017-09-13 15:53:39 -070061 sp<StatsLogProcessor> main_processor = new StatsLogProcessor();
62 data->service->setProcessor(main_processor);
63 reader->AddListener(main_processor);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070064
65 // TODO: Construct and add real LogListners here.
66
67 reader->Run();
68
69 ALOGW("statsd LogReader.Run() is not supposed to return.");
70
71 delete data;
72 return NULL;
73}
74
75/**
76 * Creates and starts the thread to own the LogReader.
77 */
78static status_t
79start_log_reader_thread(const sp<StatsService>& service)
80{
81 status_t err;
82 pthread_attr_t attr;
83 pthread_t thread;
84
85 // Thread data.
86 log_reader_thread_data* data = new log_reader_thread_data();
87 data->service = service;
88
89 // Create the thread
90 err = pthread_attr_init(&attr);
91 if (err != NO_ERROR) {
92 return err;
93 }
94 // TODO: Do we need to tweak thread priority?
95 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
96 if (err != NO_ERROR) {
97 pthread_attr_destroy(&attr);
98 return err;
99 }
100 err = pthread_create(&thread, &attr, log_reader_thread_func, static_cast<void*>(data));
101 if (err != NO_ERROR) {
102 pthread_attr_destroy(&attr);
103 return err;
104 }
105 pthread_attr_destroy(&attr);
106
107 return NO_ERROR;
108}
109
110// ================================================================================
111int
112main(int /*argc*/, char** /*argv*/)
113{
114 status_t err;
115
116 // Set up the looper
117 sp<Looper> looper(Looper::prepare(0 /* opts */));
118
119 // Set up the binder
120 sp<ProcessState> ps(ProcessState::self());
121 ps->setThreadPoolMaxThreadCount(1); // everything is oneway, let it queue and save ram
122 ps->startThreadPool();
123 ps->giveThreadPoolName();
124 IPCThreadState::self()->disableBackgroundScheduling(true);
125
126 // Create the service
127 sp<StatsService> service = new StatsService(looper);
128 if (defaultServiceManager()->addService(String16("stats"), service) != 0) {
129 ALOGE("Failed to add service");
130 return -1;
131 }
132
Bookatzb487b552017-09-18 11:26:01 -0700133 // TODO: This line is temporary, since statsd doesn't start up automatically (and therefore
134 // the call in StatsService::SystemRunning() won't ever be called right now).
135 service->sayHiToStatsCompanion();
136
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700137 // Start the log reader thread
138 err = start_log_reader_thread(service);
139 if (err != NO_ERROR) {
140 return 1;
141 }
142
143 // Loop forever -- the reports run on this thread in a handler, and the
144 // binder calls remain responsive in their pool of one thread.
145 while (true) {
146 looper->pollAll(-1 /* timeoutMillis */);
147 }
148 ALOGW("statsd escaped from its loop.");
149
150 return 1;
151}