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