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