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