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 | |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [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 | |
| 20 | #include "StatsService.h" |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 21 | #include "stats_log_util.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 23 | #include "config/ConfigKey.h" |
| 24 | #include "config/ConfigManager.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | #include "guardrail/StatsdStats.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 26 | #include "storage/StorageManager.h" |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 27 | #include "subscriber/SubscriberReporter.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 28 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 30 | #include <android-base/strings.h> |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 31 | #include <cutils/multiuser.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 32 | #include <frameworks/base/cmds/statsd/src/statsd_config.pb.h> |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 33 | #include <frameworks/base/cmds/statsd/src/uid_data.pb.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 34 | #include <private/android_filesystem_config.h> |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 35 | #include <statslog_statsd.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 36 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 37 | #include <stdlib.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 38 | #include <sys/system_properties.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 39 | #include <unistd.h> |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 40 | #include <utils/String16.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 41 | |
| 42 | using namespace android; |
| 43 | |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 44 | using android::base::StringPrintf; |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 45 | using android::util::FIELD_COUNT_REPEATED; |
| 46 | using android::util::FIELD_TYPE_MESSAGE; |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 47 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 48 | using Status = ::ndk::ScopedAStatus; |
| 49 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 50 | namespace android { |
| 51 | namespace os { |
| 52 | namespace statsd { |
| 53 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 54 | constexpr const char* kPermissionDump = "android.permission.DUMP"; |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 55 | |
Tej Singh | 10458ec | 2020-03-17 11:04:02 -0700 | [diff] [blame] | 56 | constexpr const char* kPermissionRegisterPullAtom = "android.permission.REGISTER_STATS_PULL_ATOM"; |
| 57 | |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 58 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 59 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 60 | // for StatsDataDumpProto |
| 61 | const int FIELD_ID_REPORTS_LIST = 1; |
| 62 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 63 | static Status exception(int32_t code, const std::string& msg) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 64 | ALOGE("%s (%d)", msg.c_str(), code); |
Tej Singh | 10458ec | 2020-03-17 11:04:02 -0700 | [diff] [blame] | 65 | return Status::fromExceptionCodeWithMessage(code, msg.c_str()); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 68 | static bool checkPermission(const char* permission) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 69 | pid_t pid = AIBinder_getCallingPid(); |
| 70 | uid_t uid = AIBinder_getCallingUid(); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 71 | return checkPermissionForIds(permission, pid, uid); |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 74 | Status checkUid(uid_t expectedUid) { |
| 75 | uid_t uid = AIBinder_getCallingUid(); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 76 | if (uid == expectedUid || uid == AID_ROOT) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 77 | return Status::ok(); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 78 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 79 | return exception(EX_SECURITY, |
| 80 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | #define ENFORCE_UID(uid) { \ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 85 | Status status = checkUid((uid)); \ |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 86 | if (!status.isOk()) { \ |
| 87 | return status; \ |
| 88 | } \ |
| 89 | } |
| 90 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 91 | StatsService::StatsService(const sp<Looper>& handlerLooper, shared_ptr<LogEventQueue> queue) |
| 92 | : mAnomalyAlarmMonitor(new AlarmMonitor( |
| 93 | MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 94 | [](const shared_ptr<IStatsCompanionService>& sc, int64_t timeMillis) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 95 | if (sc != nullptr) { |
| 96 | sc->setAnomalyAlarm(timeMillis); |
| 97 | StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged(); |
| 98 | } |
| 99 | }, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 100 | [](const shared_ptr<IStatsCompanionService>& sc) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 101 | if (sc != nullptr) { |
| 102 | sc->cancelAnomalyAlarm(); |
| 103 | StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged(); |
| 104 | } |
| 105 | })), |
| 106 | mPeriodicAlarmMonitor(new AlarmMonitor( |
| 107 | MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 108 | [](const shared_ptr<IStatsCompanionService>& sc, int64_t timeMillis) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 109 | if (sc != nullptr) { |
| 110 | sc->setAlarmForSubscriberTriggering(timeMillis); |
| 111 | StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged(); |
| 112 | } |
| 113 | }, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 114 | [](const shared_ptr<IStatsCompanionService>& sc) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 115 | if (sc != nullptr) { |
| 116 | sc->cancelAlarmForSubscriberTriggering(); |
| 117 | StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged(); |
| 118 | } |
| 119 | })), |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 120 | mEventQueue(queue), |
| 121 | mStatsCompanionServiceDeathRecipient(AIBinder_DeathRecipient_new( |
| 122 | StatsService::statsCompanionServiceDied)) { |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 123 | mUidMap = UidMap::getInstance(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 124 | mPullerManager = new StatsPullerManager(); |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 125 | StatsPuller::SetUidMap(mUidMap); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 126 | mConfigManager = new ConfigManager(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 127 | mProcessor = new StatsLogProcessor( |
| 128 | mUidMap, mPullerManager, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor, |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 129 | getElapsedRealtimeNs(), |
| 130 | [this](const ConfigKey& key) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 131 | shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetConfigReceiver(key); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 132 | if (receiver == nullptr) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 133 | VLOG("Could not find a broadcast receiver for %s", key.ToString().c_str()); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 134 | return false; |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 135 | } else if (receiver->sendDataBroadcast( |
| 136 | mProcessor->getLastReportTimeNs(key)).isOk()) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 137 | return true; |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 138 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 139 | VLOG("Failed to send a broadcast for receiver %s", key.ToString().c_str()); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 140 | return false; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 141 | } |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 142 | }, |
| 143 | [this](const int& uid, const vector<int64_t>& activeConfigs) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 144 | shared_ptr<IPendingIntentRef> receiver = |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 145 | mConfigManager->GetActiveConfigsChangedReceiver(uid); |
| 146 | if (receiver == nullptr) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 147 | VLOG("Could not find receiver for uid %d", uid); |
| 148 | return false; |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 149 | } else if (receiver->sendActiveConfigsChangedBroadcast(activeConfigs).isOk()) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 150 | VLOG("StatsService::active configs broadcast succeeded for uid %d" , uid); |
| 151 | return true; |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 152 | } else { |
| 153 | VLOG("StatsService::active configs broadcast failed for uid %d" , uid); |
| 154 | return false; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 155 | } |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 156 | }); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 157 | |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 158 | mUidMap->setListener(mProcessor); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 159 | mConfigManager->AddListener(mProcessor); |
| 160 | |
| 161 | init_system_properties(); |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 162 | |
| 163 | if (mEventQueue != nullptr) { |
| 164 | std::thread pushedEventThread([this] { readLogs(); }); |
| 165 | pushedEventThread.detach(); |
| 166 | } |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 169 | StatsService::~StatsService() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 172 | /* Runs on a dedicated thread to process pushed events. */ |
| 173 | void StatsService::readLogs() { |
| 174 | // Read forever..... long live statsd |
| 175 | while (1) { |
| 176 | // Block until an event is available. |
| 177 | auto event = mEventQueue->waitPop(); |
| 178 | // Pass it to StatsLogProcess to all configs/metrics |
| 179 | // At this point, the LogEventQueue is not blocked, so that the socketListener |
| 180 | // can read events from the socket and write to buffer to avoid data drop. |
| 181 | mProcessor->OnLogEvent(event.get()); |
| 182 | // The ShellSubscriber is only used by shell for local debugging. |
| 183 | if (mShellSubscriber != nullptr) { |
| 184 | mShellSubscriber->onLogEvent(*event); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 189 | void StatsService::init_system_properties() { |
| 190 | mEngBuild = false; |
| 191 | const prop_info* buildType = __system_property_find("ro.build.type"); |
| 192 | if (buildType != NULL) { |
| 193 | __system_property_read_callback(buildType, init_build_type_callback, this); |
| 194 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 197 | void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value, |
| 198 | uint32_t serial) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 199 | if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 200 | reinterpret_cast<StatsService*>(cookie)->mEngBuild = true; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 205 | * Write data from statsd. |
| 206 | * Format for statsdStats: adb shell dumpsys stats --metadata [-v] [--proto] |
| 207 | * Format for data report: adb shell dumpsys stats [anything other than --metadata] [--proto] |
| 208 | * Anything ending in --proto will be in proto format. |
| 209 | * Anything without --metadata as the first argument will be report information. |
| 210 | * (bugreports call "adb shell dumpsys stats --dump-priority NORMAL -a --proto") |
| 211 | * TODO: Come up with a more robust method of enacting <serviceutils/PriorityDumper.h>. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 212 | */ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 213 | status_t StatsService::dump(int fd, const char** args, uint32_t numArgs) { |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 214 | if (!checkPermission(kPermissionDump)) { |
Tej Singh | dd83d70 | 2018-04-10 17:24:50 -0700 | [diff] [blame] | 215 | return PERMISSION_DENIED; |
| 216 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 217 | |
| 218 | int lastArg = numArgs - 1; |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 219 | bool asProto = false; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 220 | if (lastArg >= 0 && string(args[lastArg]) == "--proto") { // last argument |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 221 | asProto = true; |
| 222 | lastArg--; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 223 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 224 | if (numArgs > 0 && string(args[0]) == "--metadata") { // first argument |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 225 | // Request is to dump statsd stats. |
| 226 | bool verbose = false; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 227 | if (lastArg >= 0 && string(args[lastArg]) == "-v") { |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 228 | verbose = true; |
| 229 | lastArg--; |
| 230 | } |
| 231 | dumpStatsdStats(fd, verbose, asProto); |
| 232 | } else { |
| 233 | // Request is to dump statsd report data. |
| 234 | if (asProto) { |
| 235 | dumpIncidentSection(fd); |
| 236 | } else { |
| 237 | dprintf(fd, "Non-proto format of stats data dump not available; see proto version.\n"); |
| 238 | } |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 239 | } |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 240 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 241 | return NO_ERROR; |
| 242 | } |
| 243 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 244 | /** |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 245 | * Write debugging data about statsd in text or proto format. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 246 | */ |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 247 | void StatsService::dumpStatsdStats(int out, bool verbose, bool proto) { |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 248 | if (proto) { |
| 249 | vector<uint8_t> data; |
| 250 | StatsdStats::getInstance().dumpStats(&data, false); // does not reset statsdStats. |
| 251 | for (size_t i = 0; i < data.size(); i ++) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 252 | dprintf(out, "%c", data[i]); |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 253 | } |
| 254 | } else { |
| 255 | StatsdStats::getInstance().dumpStats(out); |
| 256 | mProcessor->dumpStates(out, verbose); |
| 257 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 261 | * Write stats report data in StatsDataDumpProto incident section format. |
| 262 | */ |
| 263 | void StatsService::dumpIncidentSection(int out) { |
| 264 | ProtoOutputStream proto; |
| 265 | for (const ConfigKey& configKey : mConfigManager->GetAllConfigKeys()) { |
| 266 | uint64_t reportsListToken = |
| 267 | proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS_LIST); |
| 268 | mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), |
| 269 | true /* includeCurrentBucket */, false /* erase_data */, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 270 | ADB_DUMP, |
| 271 | FAST, |
| 272 | &proto); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 273 | proto.end(reportsListToken); |
| 274 | proto.flush(out); |
Bookatz | c71d901 | 2018-12-19 12:28:38 -0800 | [diff] [blame] | 275 | proto.clear(); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 280 | * Implementation of the adb shell cmd stats command. |
| 281 | */ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 282 | status_t StatsService::handleShellCommand(int in, int out, int err, const char** argv, |
| 283 | uint32_t argc) { |
| 284 | uid_t uid = AIBinder_getCallingUid(); |
Stanislav Zholnin | d7674c2 | 2020-02-17 17:48:12 +0000 | [diff] [blame] | 285 | if (uid != AID_ROOT && uid != AID_SHELL) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 286 | return PERMISSION_DENIED; |
| 287 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 288 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 289 | Vector<String8> utf8Args; |
| 290 | utf8Args.setCapacity(argc); |
| 291 | for (uint32_t i = 0; i < argc; i++) { |
| 292 | utf8Args.push(String8(argv[i])); |
| 293 | } |
| 294 | |
| 295 | if (argc >= 1) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 296 | // adb shell cmd stats config ... |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 297 | if (!utf8Args[0].compare(String8("config"))) { |
| 298 | return cmd_config(in, out, err, utf8Args); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 299 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 300 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 301 | if (!utf8Args[0].compare(String8("print-uid-map"))) { |
| 302 | return cmd_print_uid_map(out, utf8Args); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 303 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 304 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 305 | if (!utf8Args[0].compare(String8("dump-report"))) { |
| 306 | return cmd_dump_report(out, utf8Args); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 307 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 308 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 309 | if (!utf8Args[0].compare(String8("pull-source")) && argc > 1) { |
| 310 | return cmd_print_pulled_metrics(out, utf8Args); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 311 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 312 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 313 | if (!utf8Args[0].compare(String8("send-broadcast"))) { |
| 314 | return cmd_trigger_broadcast(out, utf8Args); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 317 | if (!utf8Args[0].compare(String8("print-stats"))) { |
| 318 | return cmd_print_stats(out, utf8Args); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 319 | } |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 320 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 321 | if (!utf8Args[0].compare(String8("meminfo"))) { |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 322 | return cmd_dump_memory_info(out); |
| 323 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 324 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 325 | if (!utf8Args[0].compare(String8("write-to-disk"))) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 326 | return cmd_write_data_to_disk(out); |
| 327 | } |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 328 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 329 | if (!utf8Args[0].compare(String8("log-app-breadcrumb"))) { |
| 330 | return cmd_log_app_breadcrumb(out, utf8Args); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 331 | } |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 332 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 333 | if (!utf8Args[0].compare(String8("log-binary-push"))) { |
| 334 | return cmd_log_binary_push(out, utf8Args); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 337 | if (!utf8Args[0].compare(String8("clear-puller-cache"))) { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 338 | return cmd_clear_puller_cache(out); |
| 339 | } |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 340 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 341 | if (!utf8Args[0].compare(String8("print-logs"))) { |
| 342 | return cmd_print_logs(out, utf8Args); |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 343 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 344 | if (!utf8Args[0].compare(String8("send-active-configs"))) { |
| 345 | return cmd_trigger_active_config_broadcast(out, utf8Args); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 346 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 347 | if (!utf8Args[0].compare(String8("data-subscribe"))) { |
Tej Singh | c9250ce | 2019-09-20 19:28:53 -0700 | [diff] [blame] | 348 | { |
| 349 | std::lock_guard<std::mutex> lock(mShellSubscriberMutex); |
| 350 | if (mShellSubscriber == nullptr) { |
| 351 | mShellSubscriber = new ShellSubscriber(mUidMap, mPullerManager); |
| 352 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 353 | } |
Yao Chen | 35cb8d6 | 2019-01-03 16:49:14 -0800 | [diff] [blame] | 354 | int timeoutSec = -1; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 355 | if (argc >= 2) { |
| 356 | timeoutSec = atoi(utf8Args[1].c_str()); |
Yao Chen | 35cb8d6 | 2019-01-03 16:49:14 -0800 | [diff] [blame] | 357 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 358 | mShellSubscriber->startNewSubscription(in, out, timeoutSec); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 359 | return NO_ERROR; |
| 360 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 361 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 362 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 363 | print_cmd_help(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 364 | return NO_ERROR; |
| 365 | } |
| 366 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 367 | void StatsService::print_cmd_help(int out) { |
| 368 | dprintf(out, |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 369 | "usage: adb shell cmd stats print-stats-log [tag_required] " |
| 370 | "[timestamp_nsec_optional]\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 371 | dprintf(out, "\n"); |
| 372 | dprintf(out, "\n"); |
| 373 | dprintf(out, "usage: adb shell cmd stats meminfo\n"); |
| 374 | dprintf(out, "\n"); |
| 375 | dprintf(out, " Prints the malloc debug information. You need to run the following first: \n"); |
| 376 | dprintf(out, " # adb shell stop\n"); |
| 377 | dprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n"); |
| 378 | dprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n"); |
| 379 | dprintf(out, " # adb shell start\n"); |
| 380 | dprintf(out, "\n"); |
| 381 | dprintf(out, "\n"); |
| 382 | dprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n"); |
| 383 | dprintf(out, "\n"); |
| 384 | dprintf(out, " Prints the UID, app name, version mapping.\n"); |
| 385 | dprintf(out, " PKG Optional package name to print the uids of the package\n"); |
| 386 | dprintf(out, "\n"); |
| 387 | dprintf(out, "\n"); |
| 388 | dprintf(out, "usage: adb shell cmd stats pull-source [int] \n"); |
| 389 | dprintf(out, "\n"); |
| 390 | dprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n"); |
| 391 | dprintf(out, "\n"); |
| 392 | dprintf(out, "\n"); |
| 393 | dprintf(out, "usage: adb shell cmd stats write-to-disk \n"); |
| 394 | dprintf(out, "\n"); |
| 395 | dprintf(out, " Flushes all data on memory to disk.\n"); |
| 396 | dprintf(out, "\n"); |
| 397 | dprintf(out, "\n"); |
| 398 | dprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n"); |
| 399 | dprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n"); |
| 400 | dprintf(out, " UID The uid to use. It is only possible to pass a UID\n"); |
| 401 | dprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 402 | dprintf(out, " uid is used.\n"); |
| 403 | dprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n"); |
| 404 | dprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n"); |
| 405 | dprintf(out, "\n"); |
| 406 | dprintf(out, "\n"); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 407 | dprintf(out, |
| 408 | "usage: adb shell cmd stats log-binary-push NAME VERSION STAGING ROLLBACK_ENABLED " |
| 409 | "LOW_LATENCY STATE EXPERIMENT_IDS\n"); |
| 410 | dprintf(out, " Log a binary push state changed event.\n"); |
| 411 | dprintf(out, " NAME The train name.\n"); |
| 412 | dprintf(out, " VERSION The train version code.\n"); |
| 413 | dprintf(out, " STAGING If this train requires a restart.\n"); |
| 414 | dprintf(out, " ROLLBACK_ENABLED If rollback should be enabled for this install.\n"); |
| 415 | dprintf(out, " LOW_LATENCY If the train requires low latency monitoring.\n"); |
| 416 | dprintf(out, " STATE The status of the train push.\n"); |
| 417 | dprintf(out, " Integer value of the enum in atoms.proto.\n"); |
| 418 | dprintf(out, " EXPERIMENT_IDS Comma separated list of experiment ids.\n"); |
| 419 | dprintf(out, " Leave blank for none.\n"); |
| 420 | dprintf(out, "\n"); |
| 421 | dprintf(out, "\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 422 | dprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n"); |
| 423 | dprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n"); |
| 424 | dprintf(out, "\n"); |
| 425 | dprintf(out, " Adds, updates or removes a configuration. The proto should be in\n"); |
| 426 | dprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n"); |
| 427 | dprintf(out, " provided, then all configs will be removed from memory and disk.\n"); |
| 428 | dprintf(out, "\n"); |
| 429 | dprintf(out, " UID The uid to use. It is only possible to pass the UID\n"); |
| 430 | dprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 431 | dprintf(out, " uid is used.\n"); |
| 432 | dprintf(out, " NAME The per-uid name to use\n"); |
| 433 | dprintf(out, "\n"); |
| 434 | dprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n"); |
| 435 | dprintf(out, "\n be removed from memory and disk!\n"); |
| 436 | dprintf(out, "\n"); |
| 437 | dprintf(out, |
Bookatz | 3e90658 | 2018-12-10 17:26:58 -0800 | [diff] [blame] | 438 | "usage: adb shell cmd stats dump-report [UID] NAME [--keep_data] " |
| 439 | "[--include_current_bucket] [--proto]\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 440 | dprintf(out, " Dump all metric data for a configuration.\n"); |
| 441 | dprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 442 | dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 443 | dprintf(out, " calling uid is used.\n"); |
| 444 | dprintf(out, " NAME The name of the configuration\n"); |
Bookatz | 3e90658 | 2018-12-10 17:26:58 -0800 | [diff] [blame] | 445 | dprintf(out, " --keep_data Do NOT erase the data upon dumping it.\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 446 | dprintf(out, " --proto Print proto binary.\n"); |
| 447 | dprintf(out, "\n"); |
| 448 | dprintf(out, "\n"); |
| 449 | dprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n"); |
| 450 | dprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n"); |
| 451 | dprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 452 | dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 453 | dprintf(out, " calling uid is used.\n"); |
| 454 | dprintf(out, " NAME The name of the configuration\n"); |
| 455 | dprintf(out, "\n"); |
| 456 | dprintf(out, "\n"); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 457 | dprintf(out, |
| 458 | "usage: adb shell cmd stats send-active-configs [--uid=UID] [--configs] " |
| 459 | "[NAME1] [NAME2] [NAME3..]\n"); |
| 460 | dprintf(out, " Send a broadcast that informs the subscriber of the current active configs.\n"); |
| 461 | dprintf(out, " --uid=UID The uid of the configurations. It is only possible to pass\n"); |
| 462 | dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 463 | dprintf(out, " calling uid is used.\n"); |
| 464 | dprintf(out, " --configs Send the list of configs in the name list instead of\n"); |
| 465 | dprintf(out, " the currently active configs\n"); |
| 466 | dprintf(out, " NAME LIST List of configuration names to be included in the broadcast.\n"); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 467 | dprintf(out, "\n"); |
| 468 | dprintf(out, "\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 469 | dprintf(out, "usage: adb shell cmd stats print-stats\n"); |
| 470 | dprintf(out, " Prints some basic stats.\n"); |
| 471 | dprintf(out, " --proto Print proto binary instead of string format.\n"); |
| 472 | dprintf(out, "\n"); |
| 473 | dprintf(out, "\n"); |
| 474 | dprintf(out, "usage: adb shell cmd stats clear-puller-cache\n"); |
| 475 | dprintf(out, " Clear cached puller data.\n"); |
| 476 | dprintf(out, "\n"); |
| 477 | dprintf(out, "usage: adb shell cmd stats print-logs\n"); |
| 478 | dprintf(out, " Only works on eng build\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 481 | status_t StatsService::cmd_trigger_broadcast(int out, Vector<String8>& args) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 482 | string name; |
| 483 | bool good = false; |
| 484 | int uid; |
| 485 | const int argCount = args.size(); |
| 486 | if (argCount == 2) { |
| 487 | // Automatically pick the UID |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 488 | uid = AIBinder_getCallingUid(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 489 | name.assign(args[1].c_str(), args[1].size()); |
| 490 | good = true; |
| 491 | } else if (argCount == 3) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 492 | good = getUidFromArgs(args, 1, uid); |
| 493 | if (!good) { |
| 494 | dprintf(out, "Invalid UID. Note that the metrics can only be dumped for " |
| 495 | "other UIDs on eng or userdebug builds.\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 496 | } |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 497 | name.assign(args[2].c_str(), args[2].size()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 498 | } |
| 499 | if (!good) { |
| 500 | print_cmd_help(out); |
| 501 | return UNKNOWN_ERROR; |
| 502 | } |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 503 | ConfigKey key(uid, StrToInt64(name)); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 504 | shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetConfigReceiver(key); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 505 | if (receiver == nullptr) { |
| 506 | VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str()); |
| 507 | return UNKNOWN_ERROR; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 508 | } else if (receiver->sendDataBroadcast(mProcessor->getLastReportTimeNs(key)).isOk()) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 509 | VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), |
| 510 | args[2].c_str()); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 511 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 512 | VLOG("StatsService::trigger broadcast failed to %s, %s", args[1].c_str(), args[2].c_str()); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 513 | return UNKNOWN_ERROR; |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 514 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 515 | return NO_ERROR; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 518 | status_t StatsService::cmd_trigger_active_config_broadcast(int out, Vector<String8>& args) { |
| 519 | const int argCount = args.size(); |
| 520 | int uid; |
| 521 | vector<int64_t> configIds; |
| 522 | if (argCount == 1) { |
| 523 | // Automatically pick the uid and send a broadcast that has no active configs. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 524 | uid = AIBinder_getCallingUid(); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 525 | mProcessor->GetActiveConfigs(uid, configIds); |
| 526 | } else { |
| 527 | int curArg = 1; |
| 528 | if(args[curArg].find("--uid=") == 0) { |
| 529 | string uidArgStr(args[curArg].c_str()); |
| 530 | string uidStr = uidArgStr.substr(6); |
| 531 | if (!getUidFromString(uidStr.c_str(), uid)) { |
| 532 | dprintf(out, "Invalid UID. Note that the config can only be set for " |
| 533 | "other UIDs on eng or userdebug builds.\n"); |
| 534 | return UNKNOWN_ERROR; |
| 535 | } |
| 536 | curArg++; |
| 537 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 538 | uid = AIBinder_getCallingUid(); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 539 | } |
| 540 | if (curArg == argCount || args[curArg] != "--configs") { |
| 541 | VLOG("Reached end of args, or specify configs not set. Sending actual active configs,"); |
| 542 | mProcessor->GetActiveConfigs(uid, configIds); |
| 543 | } else { |
| 544 | // Flag specified, use the given list of configs. |
| 545 | curArg++; |
| 546 | for (int i = curArg; i < argCount; i++) { |
| 547 | char* endp; |
| 548 | int64_t configID = strtoll(args[i].c_str(), &endp, 10); |
| 549 | if (endp == args[i].c_str() || *endp != '\0') { |
| 550 | dprintf(out, "Error parsing config ID.\n"); |
| 551 | return UNKNOWN_ERROR; |
| 552 | } |
| 553 | VLOG("Adding config id %ld", static_cast<long>(configID)); |
| 554 | configIds.push_back(configID); |
| 555 | } |
| 556 | } |
| 557 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 558 | shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetActiveConfigsChangedReceiver(uid); |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 559 | if (receiver == nullptr) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 560 | VLOG("Could not find receiver for uid %d", uid); |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 561 | return UNKNOWN_ERROR; |
| 562 | } else if (receiver->sendActiveConfigsChangedBroadcast(configIds).isOk()) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 563 | VLOG("StatsService::trigger active configs changed broadcast succeeded for uid %d" , uid); |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 564 | } else { |
| 565 | VLOG("StatsService::trigger active configs changed broadcast failed for uid %d", uid); |
| 566 | return UNKNOWN_ERROR; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 567 | } |
| 568 | return NO_ERROR; |
| 569 | } |
| 570 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 571 | status_t StatsService::cmd_config(int in, int out, int err, Vector<String8>& args) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 572 | const int argCount = args.size(); |
| 573 | if (argCount >= 2) { |
| 574 | if (args[1] == "update" || args[1] == "remove") { |
| 575 | bool good = false; |
| 576 | int uid = -1; |
| 577 | string name; |
| 578 | |
| 579 | if (argCount == 3) { |
| 580 | // Automatically pick the UID |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 581 | uid = AIBinder_getCallingUid(); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 582 | name.assign(args[2].c_str(), args[2].size()); |
| 583 | good = true; |
| 584 | } else if (argCount == 4) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 585 | good = getUidFromArgs(args, 2, uid); |
| 586 | if (!good) { |
| 587 | dprintf(err, "Invalid UID. Note that the config can only be set for " |
| 588 | "other UIDs on eng or userdebug builds.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 589 | } |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 590 | name.assign(args[3].c_str(), args[3].size()); |
yro | e5f8292 | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 591 | } else if (argCount == 2 && args[1] == "remove") { |
| 592 | good = true; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | if (!good) { |
| 596 | // If arg parsing failed, print the help text and return an error. |
| 597 | print_cmd_help(out); |
| 598 | return UNKNOWN_ERROR; |
| 599 | } |
| 600 | |
| 601 | if (args[1] == "update") { |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 602 | char* endp; |
| 603 | int64_t configID = strtoll(name.c_str(), &endp, 10); |
| 604 | if (endp == name.c_str() || *endp != '\0') { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 605 | dprintf(err, "Error parsing config ID.\n"); |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 606 | return UNKNOWN_ERROR; |
| 607 | } |
| 608 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 609 | // Read stream into buffer. |
| 610 | string buffer; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 611 | if (!android::base::ReadFdToString(in, &buffer)) { |
| 612 | dprintf(err, "Error reading stream for StatsConfig.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 613 | return UNKNOWN_ERROR; |
| 614 | } |
| 615 | |
| 616 | // Parse buffer. |
| 617 | StatsdConfig config; |
| 618 | if (!config.ParseFromString(buffer)) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 619 | dprintf(err, "Error parsing proto stream for StatsConfig.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 620 | return UNKNOWN_ERROR; |
| 621 | } |
| 622 | |
| 623 | // Add / update the config. |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 624 | mConfigManager->UpdateConfig(ConfigKey(uid, configID), config); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 625 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 626 | if (argCount == 2) { |
| 627 | cmd_remove_all_configs(out); |
| 628 | } else { |
| 629 | // Remove the config. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 630 | mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name))); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 631 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | return NO_ERROR; |
| 635 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 636 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 637 | print_cmd_help(out); |
| 638 | return UNKNOWN_ERROR; |
| 639 | } |
| 640 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 641 | status_t StatsService::cmd_dump_report(int out, const Vector<String8>& args) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 642 | if (mProcessor != nullptr) { |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 643 | int argCount = args.size(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 644 | bool good = false; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 645 | bool proto = false; |
Chenjie Yu | bd1a28f | 2018-07-17 14:55:19 -0700 | [diff] [blame] | 646 | bool includeCurrentBucket = false; |
Bookatz | 3e90658 | 2018-12-10 17:26:58 -0800 | [diff] [blame] | 647 | bool eraseData = true; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 648 | int uid; |
| 649 | string name; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 650 | if (!std::strcmp("--proto", args[argCount-1].c_str())) { |
| 651 | proto = true; |
| 652 | argCount -= 1; |
| 653 | } |
Chenjie Yu | bd1a28f | 2018-07-17 14:55:19 -0700 | [diff] [blame] | 654 | if (!std::strcmp("--include_current_bucket", args[argCount-1].c_str())) { |
| 655 | includeCurrentBucket = true; |
| 656 | argCount -= 1; |
| 657 | } |
Bookatz | 3e90658 | 2018-12-10 17:26:58 -0800 | [diff] [blame] | 658 | if (!std::strcmp("--keep_data", args[argCount-1].c_str())) { |
| 659 | eraseData = false; |
| 660 | argCount -= 1; |
| 661 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 662 | if (argCount == 2) { |
| 663 | // Automatically pick the UID |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 664 | uid = AIBinder_getCallingUid(); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 665 | name.assign(args[1].c_str(), args[1].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 666 | good = true; |
| 667 | } else if (argCount == 3) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 668 | good = getUidFromArgs(args, 1, uid); |
| 669 | if (!good) { |
| 670 | dprintf(out, "Invalid UID. Note that the metrics can only be dumped for " |
| 671 | "other UIDs on eng or userdebug builds.\n"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 672 | } |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 673 | name.assign(args[2].c_str(), args[2].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 674 | } |
| 675 | if (good) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 676 | vector<uint8_t> data; |
David Chen | 926fc75 | 2018-02-23 13:31:43 -0800 | [diff] [blame] | 677 | mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(), |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 678 | includeCurrentBucket, eraseData, ADB_DUMP, |
| 679 | NO_TIME_CONSTRAINTS, |
| 680 | &data); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 681 | if (proto) { |
| 682 | for (size_t i = 0; i < data.size(); i ++) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 683 | dprintf(out, "%c", data[i]); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 684 | } |
| 685 | } else { |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 686 | dprintf(out, "Non-proto stats data dump not currently supported.\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 687 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 688 | return android::OK; |
| 689 | } else { |
| 690 | // If arg parsing failed, print the help text and return an error. |
| 691 | print_cmd_help(out); |
| 692 | return UNKNOWN_ERROR; |
| 693 | } |
| 694 | } else { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 695 | dprintf(out, "Log processor does not exist...\n"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 696 | return UNKNOWN_ERROR; |
| 697 | } |
| 698 | } |
| 699 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 700 | status_t StatsService::cmd_print_stats(int out, const Vector<String8>& args) { |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 701 | int argCount = args.size(); |
| 702 | bool proto = false; |
| 703 | if (!std::strcmp("--proto", args[argCount-1].c_str())) { |
| 704 | proto = true; |
| 705 | argCount -= 1; |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 706 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 707 | StatsdStats& statsdStats = StatsdStats::getInstance(); |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 708 | if (proto) { |
| 709 | vector<uint8_t> data; |
| 710 | statsdStats.dumpStats(&data, false); // does not reset statsdStats. |
| 711 | for (size_t i = 0; i < data.size(); i ++) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 712 | dprintf(out, "%c", data[i]); |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | } else { |
| 716 | vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys(); |
| 717 | for (const ConfigKey& key : configs) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 718 | dprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(), |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 719 | mProcessor->GetMetricsSize(key)); |
| 720 | } |
| 721 | statsdStats.dumpStats(out); |
| 722 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 723 | return NO_ERROR; |
| 724 | } |
| 725 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 726 | status_t StatsService::cmd_print_uid_map(int out, const Vector<String8>& args) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 727 | if (args.size() > 1) { |
| 728 | string pkg; |
| 729 | pkg.assign(args[1].c_str(), args[1].size()); |
| 730 | auto uids = mUidMap->getAppUid(pkg); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 731 | dprintf(out, "%s -> [ ", pkg.c_str()); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 732 | for (const auto& uid : uids) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 733 | dprintf(out, "%d ", uid); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 734 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 735 | dprintf(out, "]\n"); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 736 | } else { |
| 737 | mUidMap->printUidMap(out); |
| 738 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 739 | return NO_ERROR; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 742 | status_t StatsService::cmd_write_data_to_disk(int out) { |
| 743 | dprintf(out, "Writing data to disk\n"); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 744 | mProcessor->WriteDataToDisk(ADB_DUMP, NO_TIME_CONSTRAINTS); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 745 | return NO_ERROR; |
| 746 | } |
| 747 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 748 | status_t StatsService::cmd_log_app_breadcrumb(int out, const Vector<String8>& args) { |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 749 | bool good = false; |
| 750 | int32_t uid; |
| 751 | int32_t label; |
| 752 | int32_t state; |
| 753 | const int argCount = args.size(); |
| 754 | if (argCount == 3) { |
| 755 | // Automatically pick the UID |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 756 | uid = AIBinder_getCallingUid(); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 757 | label = atoi(args[1].c_str()); |
| 758 | state = atoi(args[2].c_str()); |
| 759 | good = true; |
| 760 | } else if (argCount == 4) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 761 | good = getUidFromArgs(args, 1, uid); |
| 762 | if (!good) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 763 | dprintf(out, |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 764 | "Invalid UID. Note that selecting a UID for writing AppBreadcrumb can only be " |
| 765 | "done for other UIDs on eng or userdebug builds.\n"); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 766 | } |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 767 | label = atoi(args[2].c_str()); |
| 768 | state = atoi(args[3].c_str()); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 769 | } |
| 770 | if (good) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 771 | dprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state); |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 772 | android::os::statsd::util::stats_write( |
| 773 | android::os::statsd::util::APP_BREADCRUMB_REPORTED, uid, label, state); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 774 | } else { |
| 775 | print_cmd_help(out); |
| 776 | return UNKNOWN_ERROR; |
| 777 | } |
| 778 | return NO_ERROR; |
| 779 | } |
| 780 | |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 781 | status_t StatsService::cmd_log_binary_push(int out, const Vector<String8>& args) { |
| 782 | // Security checks are done in the sendBinaryPushStateChanged atom. |
| 783 | const int argCount = args.size(); |
| 784 | if (argCount != 7 && argCount != 8) { |
| 785 | dprintf(out, "Incorrect number of argument supplied\n"); |
| 786 | return UNKNOWN_ERROR; |
| 787 | } |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 788 | string trainName = string(args[1].c_str()); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 789 | int64_t trainVersion = strtoll(args[2].c_str(), nullptr, 10); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 790 | int32_t state = atoi(args[6].c_str()); |
| 791 | vector<int64_t> experimentIds; |
| 792 | if (argCount == 8) { |
| 793 | vector<string> experimentIdsString = android::base::Split(string(args[7].c_str()), ","); |
| 794 | for (string experimentIdString : experimentIdsString) { |
| 795 | int64_t experimentId = strtoll(experimentIdString.c_str(), nullptr, 10); |
| 796 | experimentIds.push_back(experimentId); |
| 797 | } |
| 798 | } |
| 799 | dprintf(out, "Logging BinaryPushStateChanged\n"); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 800 | vector<uint8_t> experimentIdBytes; |
| 801 | writeExperimentIdsToProto(experimentIds, &experimentIdBytes); |
| 802 | LogEvent event(trainName, trainVersion, args[3], args[4], args[5], state, experimentIdBytes, 0); |
| 803 | mProcessor->OnLogEvent(&event); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 804 | return NO_ERROR; |
| 805 | } |
| 806 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 807 | status_t StatsService::cmd_print_pulled_metrics(int out, const Vector<String8>& args) { |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 808 | int s = atoi(args[1].c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 809 | vector<shared_ptr<LogEvent> > stats; |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 810 | if (mPullerManager->Pull(s, &stats)) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 811 | for (const auto& it : stats) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 812 | dprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 813 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 814 | dprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 815 | return NO_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 816 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 817 | return UNKNOWN_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 820 | status_t StatsService::cmd_remove_all_configs(int out) { |
| 821 | dprintf(out, "Removing all configs...\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 822 | VLOG("StatsService::cmd_remove_all_configs was called"); |
| 823 | mConfigManager->RemoveAllConfigs(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 824 | StorageManager::deleteAllFiles(STATS_SERVICE_DIR); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 825 | return NO_ERROR; |
| 826 | } |
| 827 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 828 | status_t StatsService::cmd_dump_memory_info(int out) { |
| 829 | dprintf(out, "meminfo not available.\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 830 | return NO_ERROR; |
| 831 | } |
| 832 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 833 | status_t StatsService::cmd_clear_puller_cache(int out) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 834 | VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 835 | AIBinder_getCallingPid(), AIBinder_getCallingUid()); |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 836 | if (checkPermission(kPermissionDump)) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 837 | int cleared = mPullerManager->ForceClearPullerCache(); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 838 | dprintf(out, "Puller removed %d cached data!\n", cleared); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 839 | return NO_ERROR; |
| 840 | } else { |
| 841 | return PERMISSION_DENIED; |
| 842 | } |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 843 | } |
| 844 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 845 | status_t StatsService::cmd_print_logs(int out, const Vector<String8>& args) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 846 | VLOG("StatsService::cmd_print_logs with Pid %i, Uid %i", AIBinder_getCallingPid(), |
| 847 | AIBinder_getCallingUid()); |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 848 | if (checkPermission(kPermissionDump)) { |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 849 | bool enabled = true; |
| 850 | if (args.size() >= 2) { |
| 851 | enabled = atoi(args[1].c_str()) != 0; |
| 852 | } |
| 853 | mProcessor->setPrintLogs(enabled); |
| 854 | return NO_ERROR; |
| 855 | } else { |
| 856 | return PERMISSION_DENIED; |
| 857 | } |
| 858 | } |
| 859 | |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 860 | bool StatsService::getUidFromArgs(const Vector<String8>& args, size_t uidArgIndex, int32_t& uid) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 861 | return getUidFromString(args[uidArgIndex].c_str(), uid); |
| 862 | } |
| 863 | |
| 864 | bool StatsService::getUidFromString(const char* s, int32_t& uid) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 865 | if (*s == '\0') { |
| 866 | return false; |
| 867 | } |
| 868 | char* endc = NULL; |
| 869 | int64_t longUid = strtol(s, &endc, 0); |
| 870 | if (*endc != '\0') { |
| 871 | return false; |
| 872 | } |
| 873 | int32_t goodUid = static_cast<int32_t>(longUid); |
| 874 | if (longUid < 0 || static_cast<uint64_t>(longUid) != static_cast<uid_t>(goodUid)) { |
| 875 | return false; // It was not of uid_t type. |
| 876 | } |
| 877 | uid = goodUid; |
| 878 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 879 | int32_t callingUid = AIBinder_getCallingUid(); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 880 | return mEngBuild // UserDebug/EngBuild are allowed to impersonate uids. |
| 881 | || (callingUid == goodUid) // Anyone can 'impersonate' themselves. |
| 882 | || (callingUid == AID_ROOT && goodUid == AID_SHELL); // ROOT can impersonate SHELL. |
| 883 | } |
| 884 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 885 | Status StatsService::informAllUidData(const ScopedFileDescriptor& fd) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 886 | ENFORCE_UID(AID_SYSTEM); |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 887 | // Read stream into buffer. |
| 888 | string buffer; |
| 889 | if (!android::base::ReadFdToString(fd.get(), &buffer)) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 890 | return exception(EX_ILLEGAL_ARGUMENT, "Failed to read all data from the pipe."); |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 891 | } |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 892 | |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 893 | // Parse buffer. |
| 894 | UidData uidData; |
| 895 | if (!uidData.ParseFromString(buffer)) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 896 | return exception(EX_ILLEGAL_ARGUMENT, "Error parsing proto stream for UidData."); |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 897 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 898 | |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 899 | vector<String16> versionStrings; |
| 900 | vector<String16> installers; |
| 901 | vector<String16> packageNames; |
| 902 | vector<int32_t> uids; |
| 903 | vector<int64_t> versions; |
| 904 | |
| 905 | const auto numEntries = uidData.app_info_size(); |
| 906 | versionStrings.reserve(numEntries); |
| 907 | installers.reserve(numEntries); |
| 908 | packageNames.reserve(numEntries); |
| 909 | uids.reserve(numEntries); |
| 910 | versions.reserve(numEntries); |
| 911 | |
| 912 | for (const auto& appInfo: uidData.app_info()) { |
| 913 | packageNames.emplace_back(String16(appInfo.package_name().c_str())); |
| 914 | uids.push_back(appInfo.uid()); |
| 915 | versions.push_back(appInfo.version()); |
| 916 | versionStrings.emplace_back(String16(appInfo.version_string().c_str())); |
| 917 | installers.emplace_back(String16(appInfo.installer().c_str())); |
| 918 | } |
| 919 | |
| 920 | mUidMap->updateMap(getElapsedRealtimeNs(), |
| 921 | uids, |
| 922 | versions, |
| 923 | versionStrings, |
| 924 | packageNames, |
| 925 | installers); |
| 926 | |
| 927 | VLOG("StatsService::informAllUidData UidData proto parsed successfully."); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 928 | return Status::ok(); |
| 929 | } |
| 930 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 931 | Status StatsService::informOnePackage(const string& app, int32_t uid, int64_t version, |
| 932 | const string& versionString, const string& installer) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 933 | ENFORCE_UID(AID_SYSTEM); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 934 | |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 935 | VLOG("StatsService::informOnePackage was called"); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 936 | String16 utf16App = String16(app.c_str()); |
| 937 | String16 utf16VersionString = String16(versionString.c_str()); |
| 938 | String16 utf16Installer = String16(installer.c_str()); |
| 939 | |
| 940 | mUidMap->updateApp(getElapsedRealtimeNs(), utf16App, uid, version, utf16VersionString, |
| 941 | utf16Installer); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 942 | return Status::ok(); |
| 943 | } |
| 944 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 945 | Status StatsService::informOnePackageRemoved(const string& app, int32_t uid) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 946 | ENFORCE_UID(AID_SYSTEM); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 947 | |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 948 | VLOG("StatsService::informOnePackageRemoved was called"); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 949 | String16 utf16App = String16(app.c_str()); |
| 950 | mUidMap->removeApp(getElapsedRealtimeNs(), utf16App, uid); |
yro | 0192402 | 2018-02-20 18:20:49 -0800 | [diff] [blame] | 951 | mConfigManager->RemoveConfigs(uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 952 | return Status::ok(); |
| 953 | } |
| 954 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 955 | Status StatsService::informAnomalyAlarmFired() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 956 | ENFORCE_UID(AID_SYSTEM); |
| 957 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 958 | VLOG("StatsService::informAnomalyAlarmFired was called"); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 959 | int64_t currentTimeSec = getElapsedRealtimeSec(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 960 | std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet = |
| 961 | mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
| 962 | if (alarmSet.size() > 0) { |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 963 | VLOG("Found an anomaly alarm that fired."); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 964 | mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet); |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 965 | } else { |
| 966 | VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled."); |
| 967 | } |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 968 | return Status::ok(); |
| 969 | } |
| 970 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 971 | Status StatsService::informAlarmForSubscriberTriggeringFired() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 972 | ENFORCE_UID(AID_SYSTEM); |
| 973 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 974 | VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called"); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 975 | int64_t currentTimeSec = getElapsedRealtimeSec(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 976 | std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet = |
| 977 | mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
| 978 | if (alarmSet.size() > 0) { |
| 979 | VLOG("Found periodic alarm fired."); |
| 980 | mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet); |
| 981 | } else { |
| 982 | ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled."); |
| 983 | } |
| 984 | return Status::ok(); |
| 985 | } |
| 986 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 987 | Status StatsService::informPollAlarmFired() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 988 | ENFORCE_UID(AID_SYSTEM); |
| 989 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 990 | VLOG("StatsService::informPollAlarmFired was called"); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 991 | mProcessor->informPullAlarmFired(getElapsedRealtimeNs()); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 992 | VLOG("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 993 | return Status::ok(); |
| 994 | } |
| 995 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 996 | Status StatsService::systemRunning() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 997 | ENFORCE_UID(AID_SYSTEM); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 998 | |
| 999 | // When system_server is up and running, schedule the dropbox task to run. |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1000 | VLOG("StatsService::systemRunning"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1001 | sayHiToStatsCompanion(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1002 | return Status::ok(); |
| 1003 | } |
| 1004 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1005 | Status StatsService::informDeviceShutdown() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1006 | ENFORCE_UID(AID_SYSTEM); |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 1007 | VLOG("StatsService::informDeviceShutdown"); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1008 | mProcessor->WriteDataToDisk(DEVICE_SHUTDOWN, FAST); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1009 | mProcessor->SaveActiveConfigsToDisk(getElapsedRealtimeNs()); |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame^] | 1010 | mProcessor->SaveMetadataToDisk(getWallClockNs(), getElapsedRealtimeNs()); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 1011 | return Status::ok(); |
| 1012 | } |
| 1013 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1014 | void StatsService::sayHiToStatsCompanion() { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1015 | shared_ptr<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1016 | if (statsCompanion != nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1017 | VLOG("Telling statsCompanion that statsd is ready"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1018 | statsCompanion->statsdReady(); |
| 1019 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1020 | VLOG("Could not access statsCompanion"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1024 | Status StatsService::statsCompanionReady() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1025 | ENFORCE_UID(AID_SYSTEM); |
| 1026 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1027 | VLOG("StatsService::statsCompanionReady was called"); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1028 | shared_ptr<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1029 | if (statsCompanion == nullptr) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1030 | return exception(EX_NULL_POINTER, |
| 1031 | "StatsCompanion unavailable despite it contacting statsd."); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1032 | } |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1033 | VLOG("StatsService::statsCompanionReady linking to statsCompanion."); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1034 | AIBinder_linkToDeath(statsCompanion->asBinder().get(), |
| 1035 | mStatsCompanionServiceDeathRecipient.get(), this); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 1036 | mPullerManager->SetStatsCompanionService(statsCompanion); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 1037 | mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion); |
| 1038 | mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1039 | return Status::ok(); |
| 1040 | } |
| 1041 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 1042 | void StatsService::Startup() { |
| 1043 | mConfigManager->Startup(); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1044 | mProcessor->LoadActiveConfigsFromDisk(); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 1047 | void StatsService::Terminate() { |
| 1048 | ALOGI("StatsService::Terminating"); |
| 1049 | if (mProcessor != nullptr) { |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1050 | mProcessor->WriteDataToDisk(TERMINATION_SIGNAL_RECEIVED, FAST); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1051 | mProcessor->SaveActiveConfigsToDisk(getElapsedRealtimeNs()); |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame^] | 1052 | mProcessor->SaveMetadataToDisk(getWallClockNs(), getElapsedRealtimeNs()); |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 1056 | // Test only interface!!! |
Yao Chen | 3ff3a49 | 2018-08-06 16:17:37 -0700 | [diff] [blame] | 1057 | void StatsService::OnLogEvent(LogEvent* event) { |
| 1058 | mProcessor->OnLogEvent(event); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 1059 | if (mShellSubscriber != nullptr) { |
| 1060 | mShellSubscriber->onLogEvent(*event); |
| 1061 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1064 | Status StatsService::getData(int64_t key, const int32_t callingUid, vector<int8_t>* output) { |
Jeffrey Huang | 04f948b | 2020-01-07 10:05:25 -0800 | [diff] [blame] | 1065 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1066 | |
Jeffrey Huang | 04f948b | 2020-01-07 10:05:25 -0800 | [diff] [blame] | 1067 | VLOG("StatsService::getData with Uid %i", callingUid); |
| 1068 | ConfigKey configKey(callingUid, key); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1069 | // TODO(b/149254662): Since libbinder_ndk uses int8_t instead of uint8_t, |
| 1070 | // there are inconsistencies with internal statsd logic. Instead of |
| 1071 | // modifying lots of files, we create a temporary output array of int8_t and |
| 1072 | // copy its data into output. This is a bad hack, but hopefully |
| 1073 | // libbinder_ndk will transition to using uint8_t soon: progress is tracked |
| 1074 | // in b/144957764. Same applies to StatsService::getMetadata. |
| 1075 | vector<uint8_t> unsignedOutput; |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1076 | // The dump latency does not matter here since we do not include the current bucket, we do not |
| 1077 | // need to pull any new data anyhow. |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 1078 | mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), false /* include_current_bucket*/, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1079 | true /* erase_data */, GET_DATA_CALLED, FAST, &unsignedOutput); |
| 1080 | *output = vector<int8_t>(unsignedOutput.begin(), unsignedOutput.end()); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1081 | return Status::ok(); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1084 | Status StatsService::getMetadata(vector<int8_t>* output) { |
Jeffrey Huang | 9613a97 | 2020-01-07 10:05:03 -0800 | [diff] [blame] | 1085 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1086 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1087 | vector<uint8_t> unsignedOutput; |
| 1088 | StatsdStats::getInstance().dumpStats(&unsignedOutput, false); // Don't reset the counters. |
| 1089 | *output = vector<int8_t>(unsignedOutput.begin(), unsignedOutput.end()); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1090 | return Status::ok(); |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1093 | Status StatsService::addConfiguration(int64_t key, const vector <int8_t>& config, |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 1094 | const int32_t callingUid) { |
| 1095 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1096 | |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 1097 | if (addConfigurationChecked(callingUid, key, config)) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1098 | return Status::ok(); |
| 1099 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1100 | return exception(EX_ILLEGAL_ARGUMENT, "Could not parse malformatted StatsdConfig."); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1104 | bool StatsService::addConfigurationChecked(int uid, int64_t key, const vector<int8_t>& config) { |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 1105 | ConfigKey configKey(uid, key); |
| 1106 | StatsdConfig cfg; |
| 1107 | if (config.size() > 0) { // If the config is empty, skip parsing. |
| 1108 | if (!cfg.ParseFromArray(&config[0], config.size())) { |
| 1109 | return false; |
| 1110 | } |
| 1111 | } |
| 1112 | mConfigManager->UpdateConfig(configKey, cfg); |
| 1113 | return true; |
| 1114 | } |
| 1115 | |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 1116 | Status StatsService::removeDataFetchOperation(int64_t key, |
| 1117 | const int32_t callingUid) { |
| 1118 | ENFORCE_UID(AID_SYSTEM); |
| 1119 | ConfigKey configKey(callingUid, key); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1120 | mConfigManager->RemoveConfigReceiver(configKey); |
| 1121 | return Status::ok(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1124 | Status StatsService::setDataFetchOperation(int64_t key, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1125 | const shared_ptr<IPendingIntentRef>& pir, |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 1126 | const int32_t callingUid) { |
| 1127 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1128 | |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 1129 | ConfigKey configKey(callingUid, key); |
| 1130 | mConfigManager->SetConfigReceiver(configKey, pir); |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 1131 | if (StorageManager::hasConfigMetricsReport(configKey)) { |
| 1132 | VLOG("StatsService::setDataFetchOperation marking configKey %s to dump reports on disk", |
| 1133 | configKey.ToString().c_str()); |
| 1134 | mProcessor->noteOnDiskData(configKey); |
| 1135 | } |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1136 | return Status::ok(); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1139 | Status StatsService::setActiveConfigsChangedOperation(const shared_ptr<IPendingIntentRef>& pir, |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1140 | const int32_t callingUid, |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1141 | vector<int64_t>* output) { |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1142 | ENFORCE_UID(AID_SYSTEM); |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1143 | |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1144 | mConfigManager->SetActiveConfigsChangedReceiver(callingUid, pir); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 1145 | if (output != nullptr) { |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1146 | mProcessor->GetActiveConfigs(callingUid, *output); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 1147 | } else { |
| 1148 | ALOGW("StatsService::setActiveConfigsChanged output was nullptr"); |
| 1149 | } |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1150 | return Status::ok(); |
| 1151 | } |
| 1152 | |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1153 | Status StatsService::removeActiveConfigsChangedOperation(const int32_t callingUid) { |
| 1154 | ENFORCE_UID(AID_SYSTEM); |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1155 | |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1156 | mConfigManager->RemoveActiveConfigsChangedReceiver(callingUid); |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1157 | return Status::ok(); |
| 1158 | } |
| 1159 | |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 1160 | Status StatsService::removeConfiguration(int64_t key, const int32_t callingUid) { |
| 1161 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1162 | |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 1163 | ConfigKey configKey(callingUid, key); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1164 | mConfigManager->RemoveConfig(configKey); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1165 | return Status::ok(); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1168 | Status StatsService::setBroadcastSubscriber(int64_t configId, |
| 1169 | int64_t subscriberId, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1170 | const shared_ptr<IPendingIntentRef>& pir, |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1171 | const int32_t callingUid) { |
| 1172 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1173 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1174 | VLOG("StatsService::setBroadcastSubscriber called."); |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1175 | ConfigKey configKey(callingUid, configId); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1176 | SubscriberReporter::getInstance() |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1177 | .setBroadcastSubscriber(configKey, subscriberId, pir); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1178 | return Status::ok(); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | Status StatsService::unsetBroadcastSubscriber(int64_t configId, |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1182 | int64_t subscriberId, |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1183 | const int32_t callingUid) { |
| 1184 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1185 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1186 | VLOG("StatsService::unsetBroadcastSubscriber called."); |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1187 | ConfigKey configKey(callingUid, configId); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1188 | SubscriberReporter::getInstance() |
| 1189 | .unsetBroadcastSubscriber(configKey, subscriberId); |
| 1190 | return Status::ok(); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 1193 | Status StatsService::sendAppBreadcrumbAtom(int32_t label, int32_t state) { |
| 1194 | // Permission check not necessary as it's meant for applications to write to |
| 1195 | // statsd. |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 1196 | android::os::statsd::util::stats_write(android::os::statsd::util::APP_BREADCRUMB_REPORTED, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1197 | (int32_t) AIBinder_getCallingUid(), label, |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 1198 | state); |
| 1199 | return Status::ok(); |
| 1200 | } |
| 1201 | |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1202 | Status StatsService::registerPullAtomCallback(int32_t uid, int32_t atomTag, int64_t coolDownMillis, |
| 1203 | int64_t timeoutMillis, |
| 1204 | const std::vector<int32_t>& additiveFields, |
| 1205 | const shared_ptr<IPullAtomCallback>& pullerCallback) { |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 1206 | ENFORCE_UID(AID_SYSTEM); |
Tej Singh | b780251 | 2019-12-04 17:57:04 -0800 | [diff] [blame] | 1207 | VLOG("StatsService::registerPullAtomCallback called."); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1208 | mPullerManager->RegisterPullAtomCallback(uid, atomTag, MillisToNano(coolDownMillis), |
| 1209 | MillisToNano(timeoutMillis), additiveFields, |
Tej Singh | b780251 | 2019-12-04 17:57:04 -0800 | [diff] [blame] | 1210 | pullerCallback); |
| 1211 | return Status::ok(); |
| 1212 | } |
| 1213 | |
Tej Singh | 73597dc | 2020-03-13 18:42:40 -0700 | [diff] [blame] | 1214 | Status StatsService::registerNativePullAtomCallback( |
| 1215 | int32_t atomTag, int64_t coolDownMillis, int64_t timeoutMillis, |
| 1216 | const std::vector<int32_t>& additiveFields, |
| 1217 | const shared_ptr<IPullAtomCallback>& pullerCallback) { |
Tej Singh | 10458ec | 2020-03-17 11:04:02 -0700 | [diff] [blame] | 1218 | if (!checkPermission(kPermissionRegisterPullAtom)) { |
| 1219 | return exception( |
| 1220 | EX_SECURITY, |
| 1221 | StringPrintf("Uid %d does not have the %s permission when registering atom %d", |
| 1222 | AIBinder_getCallingUid(), kPermissionRegisterPullAtom, atomTag)); |
| 1223 | } |
Tej Singh | b780251 | 2019-12-04 17:57:04 -0800 | [diff] [blame] | 1224 | VLOG("StatsService::registerNativePullAtomCallback called."); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1225 | int32_t uid = AIBinder_getCallingUid(); |
Tej Singh | 73597dc | 2020-03-13 18:42:40 -0700 | [diff] [blame] | 1226 | mPullerManager->RegisterPullAtomCallback(uid, atomTag, MillisToNano(coolDownMillis), |
| 1227 | MillisToNano(timeoutMillis), additiveFields, |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 1228 | pullerCallback); |
Tej Singh | 5918429 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 1229 | return Status::ok(); |
| 1230 | } |
| 1231 | |
Tej Singh | fa1c137 | 2019-12-05 20:36:54 -0800 | [diff] [blame] | 1232 | Status StatsService::unregisterPullAtomCallback(int32_t uid, int32_t atomTag) { |
| 1233 | ENFORCE_UID(AID_SYSTEM); |
| 1234 | VLOG("StatsService::unregisterPullAtomCallback called."); |
| 1235 | mPullerManager->UnregisterPullAtomCallback(uid, atomTag); |
| 1236 | return Status::ok(); |
| 1237 | } |
| 1238 | |
Tej Singh | 8f35860 | 2020-01-15 16:05:39 -0800 | [diff] [blame] | 1239 | Status StatsService::unregisterNativePullAtomCallback(int32_t atomTag) { |
Tej Singh | 10458ec | 2020-03-17 11:04:02 -0700 | [diff] [blame] | 1240 | if (!checkPermission(kPermissionRegisterPullAtom)) { |
| 1241 | return exception( |
| 1242 | EX_SECURITY, |
| 1243 | StringPrintf("Uid %d does not have the %s permission when unregistering atom %d", |
| 1244 | AIBinder_getCallingUid(), kPermissionRegisterPullAtom, atomTag)); |
| 1245 | } |
Tej Singh | 8f35860 | 2020-01-15 16:05:39 -0800 | [diff] [blame] | 1246 | VLOG("StatsService::unregisterNativePullAtomCallback called."); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1247 | int32_t uid = AIBinder_getCallingUid(); |
Tej Singh | 8f35860 | 2020-01-15 16:05:39 -0800 | [diff] [blame] | 1248 | mPullerManager->UnregisterPullAtomCallback(uid, atomTag); |
| 1249 | return Status::ok(); |
| 1250 | } |
| 1251 | |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1252 | Status StatsService::getRegisteredExperimentIds(std::vector<int64_t>* experimentIdsOut) { |
Jeffrey Huang | 80c9a97 | 2020-01-07 10:04:27 -0800 | [diff] [blame] | 1253 | ENFORCE_UID(AID_SYSTEM); |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1254 | // TODO: add verifier permission |
| 1255 | |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 1256 | experimentIdsOut->clear(); |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1257 | // Read the latest train info |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 1258 | vector<InstallTrainInfo> trainInfoList = StorageManager::readAllTrainInfo(); |
| 1259 | if (trainInfoList.empty()) { |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1260 | // No train info means no experiment IDs, return an empty list |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1261 | return Status::ok(); |
| 1262 | } |
| 1263 | |
| 1264 | // Copy the experiment IDs to the out vector |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 1265 | for (InstallTrainInfo& trainInfo : trainInfoList) { |
| 1266 | experimentIdsOut->insert(experimentIdsOut->end(), |
| 1267 | trainInfo.experimentIds.begin(), |
| 1268 | trainInfo.experimentIds.end()); |
| 1269 | } |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 1270 | return Status::ok(); |
| 1271 | } |
| 1272 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1273 | |
| 1274 | void StatsService::statsCompanionServiceDied(void* cookie) { |
| 1275 | auto thiz = static_cast<StatsService*>(cookie); |
| 1276 | thiz->statsCompanionServiceDiedImpl(); |
| 1277 | } |
| 1278 | |
| 1279 | void StatsService::statsCompanionServiceDiedImpl() { |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 1280 | ALOGW("statscompanion service died"); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1281 | StatsdStats::getInstance().noteSystemServerRestart(getWallClockSec()); |
| 1282 | if (mProcessor != nullptr) { |
Howard Ro | e60992b | 2018-08-30 14:37:29 -0700 | [diff] [blame] | 1283 | ALOGW("Reset statsd upon system server restarts."); |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1284 | int64_t systemServerRestartNs = getElapsedRealtimeNs(); |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame^] | 1285 | ProtoOutputStream activeConfigsProto; |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1286 | mProcessor->WriteActiveConfigsToProtoOutputStream(systemServerRestartNs, |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame^] | 1287 | STATSCOMPANION_DIED, &activeConfigsProto); |
| 1288 | metadata::StatsMetadataList metadataList; |
| 1289 | mProcessor->WriteMetadataToProto(getWallClockNs(), |
| 1290 | systemServerRestartNs, &metadataList); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1291 | mProcessor->WriteDataToDisk(STATSCOMPANION_DIED, FAST); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1292 | mProcessor->resetConfigs(); |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1293 | |
| 1294 | std::string serializedActiveConfigs; |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame^] | 1295 | if (activeConfigsProto.serializeToString(&serializedActiveConfigs)) { |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1296 | ActiveConfigList activeConfigs; |
| 1297 | if (activeConfigs.ParseFromString(serializedActiveConfigs)) { |
| 1298 | mProcessor->SetConfigsActiveState(activeConfigs, systemServerRestartNs); |
| 1299 | } |
| 1300 | } |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1301 | } |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 1302 | mAnomalyAlarmMonitor->setStatsCompanionService(nullptr); |
| 1303 | mPeriodicAlarmMonitor->setStatsCompanionService(nullptr); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 1304 | mPullerManager->SetStatsCompanionService(nullptr); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1307 | } // namespace statsd |
| 1308 | } // namespace os |
| 1309 | } // namespace android |