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