blob: 2c721c74f1171239df710823fbe732bffe884fd3 [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));
60 reader->AddListener(new StatsLogProcessor());
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070061
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}