Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 22 | #include "StatsLogProcessor.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 23 | |
| 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 | |
| 38 | using namespace android; |
| 39 | |
| 40 | // ================================================================================ |
| 41 | /** |
| 42 | * Thread function data. |
| 43 | */ |
| 44 | struct log_reader_thread_data { |
| 45 | sp<StatsService> service; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Thread func for where the log reader runs. |
| 50 | */ |
| 51 | static void* |
| 52 | log_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 Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 59 | reader->AddListener(new LogEntryPrinter(STDOUT_FILENO)); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 60 | sp<StatsLogProcessor> main_processor = new StatsLogProcessor(); |
| 61 | data->service->setProcessor(main_processor); |
| 62 | reader->AddListener(main_processor); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 63 | |
| 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 | */ |
| 77 | static status_t |
| 78 | start_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 | // ================================================================================ |
| 110 | int |
| 111 | main(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 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame^] | 132 | // 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 Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 136 | // 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 | } |