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