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