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" |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 21 | #include "socket/StatsSocketListener.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 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> |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 28 | #include <hidl/HidlTransportSupport.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 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Thread function data. |
| 44 | */ |
| 45 | struct log_reader_thread_data { |
| 46 | sp<StatsService> service; |
| 47 | }; |
| 48 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 49 | |
| 50 | sp<StatsService> gStatsService = nullptr; |
| 51 | |
| 52 | void sigHandler(int sig) { |
| 53 | if (gStatsService != nullptr) { |
| 54 | gStatsService->Terminate(); |
| 55 | } |
Howard Ro | 48dfcdb | 2019-10-14 13:56:41 -0700 | [diff] [blame] | 56 | ALOGW("statsd terminated on receiving signal %d.", sig); |
| 57 | exit(1); |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void registerSigHandler() |
| 61 | { |
| 62 | struct sigaction sa; |
| 63 | sigemptyset(&sa.sa_mask); |
| 64 | sa.sa_flags = 0; |
| 65 | sa.sa_handler = sigHandler; |
| 66 | sigaction(SIGHUP, &sa, nullptr); |
| 67 | sigaction(SIGINT, &sa, nullptr); |
| 68 | sigaction(SIGQUIT, &sa, nullptr); |
| 69 | sigaction(SIGTERM, &sa, nullptr); |
| 70 | } |
| 71 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 72 | int main(int /*argc*/, char** /*argv*/) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 73 | // Set up the looper |
| 74 | sp<Looper> looper(Looper::prepare(0 /* opts */)); |
| 75 | |
| 76 | // Set up the binder |
| 77 | sp<ProcessState> ps(ProcessState::self()); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 78 | ps->setThreadPoolMaxThreadCount(9); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 79 | ps->startThreadPool(); |
| 80 | ps->giveThreadPoolName(); |
| 81 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 82 | |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 83 | ::android::hardware::configureRpcThreadpool(1 /*threads*/, false /*willJoin*/); |
| 84 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 85 | std::shared_ptr<LogEventQueue> eventQueue = |
| 86 | std::make_shared<LogEventQueue>(2000 /*buffer limit. Buffer is NOT pre-allocated*/); |
| 87 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 88 | // Create the service |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 89 | gStatsService = new StatsService(looper, eventQueue); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 90 | if (defaultServiceManager()->addService(String16("stats"), gStatsService, false, |
| 91 | IServiceManager::DUMP_FLAG_PRIORITY_NORMAL | IServiceManager::DUMP_FLAG_PROTO) |
| 92 | != 0) { |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 93 | ALOGE("Failed to add service as AIDL service"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 94 | return -1; |
| 95 | } |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 96 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 97 | auto ret = gStatsService->registerAsService(); |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 98 | if (ret != ::android::OK) { |
| 99 | ALOGE("Failed to add service as HIDL service"); |
| 100 | return 1; // or handle error |
| 101 | } |
| 102 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 103 | registerSigHandler(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 104 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 105 | gStatsService->sayHiToStatsCompanion(); |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 106 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 107 | gStatsService->Startup(); |
| 108 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 109 | sp<StatsSocketListener> socketListener = new StatsSocketListener(eventQueue); |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 110 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 111 | ALOGI("Statsd starts to listen to socket."); |
| 112 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
| 113 | if (socketListener->startListener(600)) { |
| 114 | exit(1); |
| 115 | } |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 116 | |
| 117 | // Loop forever -- the reports run on this thread in a handler, and the |
| 118 | // binder calls remain responsive in their pool of one thread. |
| 119 | while (true) { |
| 120 | looper->pollAll(-1 /* timeoutMillis */); |
| 121 | } |
| 122 | ALOGW("statsd escaped from its loop."); |
| 123 | |
| 124 | return 1; |
| 125 | } |