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