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 | |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 19 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 20 | #include "StatsService.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | #include "logd/LogReader.h" |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 22 | #include "socket/StatsSocketListener.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> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 29 | #include <utils/Looper.h> |
| 30 | #include <utils/StrongPointer.h> |
| 31 | |
George Burgess IV | ef8262c | 2018-04-23 09:32:41 -0700 | [diff] [blame] | 32 | #include <memory> |
| 33 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 34 | #include <stdio.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 35 | #include <sys/stat.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 36 | #include <sys/types.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 37 | #include <unistd.h> |
| 38 | |
| 39 | using namespace android; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 40 | using namespace android::os::statsd; |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 41 | |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 42 | const bool kUseLogd = false; |
| 43 | const bool kUseStatsdSocket = true; |
| 44 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Thread function data. |
| 47 | */ |
| 48 | struct log_reader_thread_data { |
| 49 | sp<StatsService> service; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Thread func for where the log reader runs. |
| 54 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 55 | static void* log_reader_thread_func(void* cookie) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 56 | log_reader_thread_data* data = static_cast<log_reader_thread_data*>(cookie); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 57 | sp<LogReader> reader = new LogReader(data->service); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 58 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 59 | // Run the read loop. Never returns. |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 60 | reader->Run(); |
| 61 | |
| 62 | ALOGW("statsd LogReader.Run() is not supposed to return."); |
| 63 | |
| 64 | delete data; |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates and starts the thread to own the LogReader. |
| 70 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 71 | static status_t start_log_reader_thread(const sp<StatsService>& service) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 72 | status_t err; |
| 73 | pthread_attr_t attr; |
| 74 | pthread_t thread; |
| 75 | |
| 76 | // Thread data. |
George Burgess IV | ef8262c | 2018-04-23 09:32:41 -0700 | [diff] [blame] | 77 | std::unique_ptr<log_reader_thread_data> data = std::make_unique<log_reader_thread_data>(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 78 | data->service = service; |
| 79 | |
| 80 | // Create the thread |
| 81 | err = pthread_attr_init(&attr); |
| 82 | if (err != NO_ERROR) { |
| 83 | return err; |
| 84 | } |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 85 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 86 | if (err != NO_ERROR) { |
| 87 | pthread_attr_destroy(&attr); |
| 88 | return err; |
| 89 | } |
George Burgess IV | ef8262c | 2018-04-23 09:32:41 -0700 | [diff] [blame] | 90 | err = pthread_create(&thread, &attr, log_reader_thread_func, |
| 91 | static_cast<void*>(data.get())); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 92 | if (err != NO_ERROR) { |
| 93 | pthread_attr_destroy(&attr); |
| 94 | return err; |
| 95 | } |
George Burgess IV | ef8262c | 2018-04-23 09:32:41 -0700 | [diff] [blame] | 96 | // Release here rather than in pthread_create, since an error creating the |
| 97 | // thread leaves `data` ownerless. |
| 98 | data.release(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 99 | pthread_attr_destroy(&attr); |
| 100 | |
| 101 | return NO_ERROR; |
| 102 | } |
| 103 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 104 | int main(int /*argc*/, char** /*argv*/) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 105 | // Set up the looper |
| 106 | sp<Looper> looper(Looper::prepare(0 /* opts */)); |
| 107 | |
| 108 | // Set up the binder |
| 109 | sp<ProcessState> ps(ProcessState::self()); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 110 | ps->setThreadPoolMaxThreadCount(9); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 111 | ps->startThreadPool(); |
| 112 | ps->giveThreadPoolName(); |
| 113 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 114 | |
| 115 | // Create the service |
| 116 | sp<StatsService> service = new StatsService(looper); |
| 117 | if (defaultServiceManager()->addService(String16("stats"), service) != 0) { |
| 118 | ALOGE("Failed to add service"); |
| 119 | return -1; |
| 120 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 121 | service->sayHiToStatsCompanion(); |
| 122 | |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 123 | service->Startup(); |
| 124 | |
| 125 | sp<StatsSocketListener> socketListener = new StatsSocketListener(service); |
| 126 | |
| 127 | if (kUseLogd) { |
| 128 | ALOGI("using logd"); |
| 129 | // Start the log reader thread |
| 130 | status_t err = start_log_reader_thread(service); |
| 131 | if (err != NO_ERROR) { |
| 132 | return 1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (kUseStatsdSocket) { |
| 137 | ALOGI("using statsd socket"); |
| 138 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
| 139 | if (socketListener->startListener(600)) { |
| 140 | exit(1); |
| 141 | } |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Loop forever -- the reports run on this thread in a handler, and the |
| 145 | // binder calls remain responsive in their pool of one thread. |
| 146 | while (true) { |
| 147 | looper->pollAll(-1 /* timeoutMillis */); |
| 148 | } |
| 149 | ALOGW("statsd escaped from its loop."); |
| 150 | |
| 151 | return 1; |
| 152 | } |