blob: 93405cb6bf95f06a093e305ba76ce6304ce145de [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"
22
23#include <binder/IInterface.h>
24#include <binder/IPCThreadState.h>
25#include <binder/IServiceManager.h>
26#include <binder/ProcessState.h>
27#include <binder/Status.h>
28#include <cutils/log.h>
29#include <utils/Looper.h>
30#include <utils/StrongPointer.h>
31
32#include <stdio.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <unistd.h>
36
37using namespace android;
38
39// ================================================================================
40/**
41 * Thread function data.
42 */
43struct log_reader_thread_data {
44 sp<StatsService> service;
45};
46
47/**
48 * Thread func for where the log reader runs.
49 */
50static void*
51log_reader_thread_func(void* cookie)
52{
53 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.
58 if (true) {
59 reader->AddListener(new LogEntryPrinter(STDOUT_FILENO));
60 }
61
62 // TODO: Construct and add real LogListners here.
63
64 reader->Run();
65
66 ALOGW("statsd LogReader.Run() is not supposed to return.");
67
68 delete data;
69 return NULL;
70}
71
72/**
73 * Creates and starts the thread to own the LogReader.
74 */
75static status_t
76start_log_reader_thread(const sp<StatsService>& service)
77{
78 status_t err;
79 pthread_attr_t attr;
80 pthread_t thread;
81
82 // Thread data.
83 log_reader_thread_data* data = new log_reader_thread_data();
84 data->service = service;
85
86 // Create the thread
87 err = pthread_attr_init(&attr);
88 if (err != NO_ERROR) {
89 return err;
90 }
91 // TODO: Do we need to tweak thread priority?
92 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
93 if (err != NO_ERROR) {
94 pthread_attr_destroy(&attr);
95 return err;
96 }
97 err = pthread_create(&thread, &attr, log_reader_thread_func, static_cast<void*>(data));
98 if (err != NO_ERROR) {
99 pthread_attr_destroy(&attr);
100 return err;
101 }
102 pthread_attr_destroy(&attr);
103
104 return NO_ERROR;
105}
106
107// ================================================================================
108int
109main(int /*argc*/, char** /*argv*/)
110{
111 status_t err;
112
113 // Set up the looper
114 sp<Looper> looper(Looper::prepare(0 /* opts */));
115
116 // Set up the binder
117 sp<ProcessState> ps(ProcessState::self());
118 ps->setThreadPoolMaxThreadCount(1); // everything is oneway, let it queue and save ram
119 ps->startThreadPool();
120 ps->giveThreadPoolName();
121 IPCThreadState::self()->disableBackgroundScheduling(true);
122
123 // Create the service
124 sp<StatsService> service = new StatsService(looper);
125 if (defaultServiceManager()->addService(String16("stats"), service) != 0) {
126 ALOGE("Failed to add service");
127 return -1;
128 }
129
130 // Start the log reader thread
131 err = start_log_reader_thread(service);
132 if (err != NO_ERROR) {
133 return 1;
134 }
135
136 // Loop forever -- the reports run on this thread in a handler, and the
137 // binder calls remain responsive in their pool of one thread.
138 while (true) {
139 looper->pollAll(-1 /* timeoutMillis */);
140 }
141 ALOGW("statsd escaped from its loop.");
142
143 return 1;
144}