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