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