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