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