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