Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
yro | 0feae94 | 2017-11-15 14:38:48 -0800 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #include "Log.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 18 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 19 | #include "StatsService.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "logd/LogReader.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 21 | |
| 22 | #include <binder/IInterface.h> |
| 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <binder/ProcessState.h> |
| 26 | #include <binder/Status.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 27 | #include <utils/Looper.h> |
| 28 | #include <utils/StrongPointer.h> |
| 29 | |
| 30 | #include <stdio.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 31 | #include <sys/stat.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 32 | #include <sys/types.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | using namespace android; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 36 | using namespace android::os::statsd; |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 37 | |
| 38 | // ================================================================================ |
| 39 | /** |
| 40 | * Thread function data. |
| 41 | */ |
| 42 | struct log_reader_thread_data { |
| 43 | sp<StatsService> service; |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * Thread func for where the log reader runs. |
| 48 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 49 | static void* log_reader_thread_func(void* cookie) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 50 | log_reader_thread_data* data = static_cast<log_reader_thread_data*>(cookie); |
| 51 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 52 | sp<LogReader> reader = new LogReader(data->service); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 53 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 54 | // Tell StatsService that we're ready to go. |
| 55 | data->service->Startup(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 56 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 57 | // Run the read loop. Never returns. |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 58 | reader->Run(); |
| 59 | |
| 60 | ALOGW("statsd LogReader.Run() is not supposed to return."); |
| 61 | |
| 62 | delete data; |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates and starts the thread to own the LogReader. |
| 68 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 69 | static status_t start_log_reader_thread(const sp<StatsService>& service) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 70 | status_t err; |
| 71 | pthread_attr_t attr; |
| 72 | pthread_t thread; |
| 73 | |
| 74 | // Thread data. |
| 75 | log_reader_thread_data* data = new log_reader_thread_data(); |
| 76 | data->service = service; |
| 77 | |
| 78 | // Create the thread |
| 79 | err = pthread_attr_init(&attr); |
| 80 | if (err != NO_ERROR) { |
| 81 | return err; |
| 82 | } |
| 83 | // TODO: Do we need to tweak thread priority? |
| 84 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 85 | if (err != NO_ERROR) { |
| 86 | pthread_attr_destroy(&attr); |
| 87 | return err; |
| 88 | } |
| 89 | err = pthread_create(&thread, &attr, log_reader_thread_func, static_cast<void*>(data)); |
| 90 | if (err != NO_ERROR) { |
| 91 | pthread_attr_destroy(&attr); |
| 92 | return err; |
| 93 | } |
| 94 | pthread_attr_destroy(&attr); |
| 95 | |
| 96 | return NO_ERROR; |
| 97 | } |
| 98 | |
| 99 | // ================================================================================ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 100 | int main(int /*argc*/, char** /*argv*/) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 101 | status_t err; |
| 102 | |
| 103 | // Set up the looper |
| 104 | sp<Looper> looper(Looper::prepare(0 /* opts */)); |
| 105 | |
| 106 | // Set up the binder |
| 107 | sp<ProcessState> ps(ProcessState::self()); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 108 | ps->setThreadPoolMaxThreadCount(9); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 109 | ps->startThreadPool(); |
| 110 | ps->giveThreadPoolName(); |
| 111 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 112 | |
| 113 | // Create the service |
| 114 | sp<StatsService> service = new StatsService(looper); |
| 115 | if (defaultServiceManager()->addService(String16("stats"), service) != 0) { |
| 116 | ALOGE("Failed to add service"); |
| 117 | return -1; |
| 118 | } |
| 119 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 120 | // TODO: This line is temporary, since statsd doesn't start up automatically (and therefore |
| 121 | // the call in StatsService::SystemRunning() won't ever be called right now). |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 122 | // TODO: Are you sure? Don't we need to reconnect to the system process if we get restarted? |
| 123 | // --joeo |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 124 | service->sayHiToStatsCompanion(); |
| 125 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 126 | // Start the log reader thread |
| 127 | err = start_log_reader_thread(service); |
| 128 | if (err != NO_ERROR) { |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | // Loop forever -- the reports run on this thread in a handler, and the |
| 133 | // binder calls remain responsive in their pool of one thread. |
| 134 | while (true) { |
| 135 | looper->pollAll(-1 /* timeoutMillis */); |
| 136 | } |
| 137 | ALOGW("statsd escaped from its loop."); |
| 138 | |
| 139 | return 1; |
| 140 | } |