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" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 22 | #include "config/ConfigKey.h" |
| 23 | #include "config/ConfigManager.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 24 | #include "guardrail/MemoryLeakTrackUtil.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> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
| 31 | #include <binder/IServiceManager.h> |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 32 | #include <dirent.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 33 | #include <frameworks/base/cmds/statsd/src/statsd_config.pb.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 34 | #include <private/android_filesystem_config.h> |
| 35 | #include <utils/Looper.h> |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 36 | #include <utils/String16.h> |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 37 | #include <statslog.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 38 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 39 | #include <stdlib.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 40 | #include <sys/system_properties.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 41 | #include <unistd.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 42 | |
| 43 | using namespace android; |
| 44 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace os { |
| 47 | namespace statsd { |
| 48 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 49 | constexpr const char* kPermissionDump = "android.permission.DUMP"; |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 50 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 51 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 52 | // ====================================================================== |
| 53 | /** |
| 54 | * Watches for the death of the stats companion (system process). |
| 55 | */ |
| 56 | class CompanionDeathRecipient : public IBinder::DeathRecipient { |
| 57 | public: |
| 58 | CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor); |
| 59 | virtual void binderDied(const wp<IBinder>& who); |
| 60 | |
| 61 | private: |
| 62 | const sp<AnomalyMonitor> mAnomalyMonitor; |
| 63 | }; |
| 64 | |
| 65 | CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor) |
| 66 | : mAnomalyMonitor(anomalyMonitor) { |
| 67 | } |
| 68 | |
| 69 | void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 70 | ALOGW("statscompanion service died"); |
| 71 | mAnomalyMonitor->setStatsCompanionService(nullptr); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 72 | SubscriberReporter::getInstance().setStatsCompanionService(nullptr); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // ====================================================================== |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 76 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 77 | : mAnomalyMonitor(new AnomalyMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS)) |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 78 | { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 79 | mUidMap = new UidMap(); |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 80 | StatsPuller::SetUidMap(mUidMap); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 81 | mConfigManager = new ConfigManager(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 82 | mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, time(nullptr), [this](const ConfigKey& key) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 83 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 84 | auto receiver = mConfigManager->GetConfigReceiver(key); |
| 85 | if (sc == nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 86 | VLOG("Could not find StatsCompanionService"); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 87 | } else if (receiver == nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 88 | VLOG("Statscompanion could not find a broadcast receiver for %s", |
| 89 | key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 90 | } else { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 91 | sc->sendDataBroadcast(receiver); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 92 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 93 | }); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 94 | |
| 95 | mConfigManager->AddListener(mProcessor); |
| 96 | |
| 97 | init_system_properties(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 100 | StatsService::~StatsService() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 103 | void StatsService::init_system_properties() { |
| 104 | mEngBuild = false; |
| 105 | const prop_info* buildType = __system_property_find("ro.build.type"); |
| 106 | if (buildType != NULL) { |
| 107 | __system_property_read_callback(buildType, init_build_type_callback, this); |
| 108 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 111 | void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value, |
| 112 | uint32_t serial) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 113 | if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 114 | reinterpret_cast<StatsService*>(cookie)->mEngBuild = true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Implement our own because the default binder implementation isn't |
| 120 | * properly handling SHELL_COMMAND_TRANSACTION. |
| 121 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 122 | status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 123 | uint32_t flags) { |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 124 | status_t err; |
| 125 | |
| 126 | switch (code) { |
| 127 | case SHELL_COMMAND_TRANSACTION: { |
| 128 | int in = data.readFileDescriptor(); |
| 129 | int out = data.readFileDescriptor(); |
| 130 | int err = data.readFileDescriptor(); |
| 131 | int argc = data.readInt32(); |
| 132 | Vector<String8> args; |
| 133 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 134 | args.add(String8(data.readString16())); |
| 135 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 136 | sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder()); |
| 137 | sp<IResultReceiver> resultReceiver = |
| 138 | IResultReceiver::asInterface(data.readStrongBinder()); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 139 | |
| 140 | FILE* fin = fdopen(in, "r"); |
| 141 | FILE* fout = fdopen(out, "w"); |
| 142 | FILE* ferr = fdopen(err, "w"); |
| 143 | |
| 144 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 145 | resultReceiver->send(NO_MEMORY); |
| 146 | } else { |
| 147 | err = command(fin, fout, ferr, args); |
| 148 | resultReceiver->send(err); |
| 149 | } |
| 150 | |
| 151 | if (fin != NULL) { |
| 152 | fflush(fin); |
| 153 | fclose(fin); |
| 154 | } |
| 155 | if (fout != NULL) { |
| 156 | fflush(fout); |
| 157 | fclose(fout); |
| 158 | } |
| 159 | if (fout != NULL) { |
| 160 | fflush(ferr); |
| 161 | fclose(ferr); |
| 162 | } |
| 163 | |
| 164 | return NO_ERROR; |
| 165 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 166 | default: { return BnStatsManager::onTransact(code, data, reply, flags); } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Write debugging data about statsd. |
| 172 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 173 | status_t StatsService::dump(int fd, const Vector<String16>& args) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 174 | FILE* out = fdopen(fd, "w"); |
| 175 | if (out == NULL) { |
| 176 | return NO_MEMORY; // the fd is already open |
| 177 | } |
| 178 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 179 | bool verbose = false; |
| 180 | if (args.size() > 0 && !args[0].compare(String16("-v"))) { |
| 181 | verbose = true; |
| 182 | } |
| 183 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 184 | // TODO: Proto format for incident reports |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 185 | dump_impl(out, verbose); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 186 | |
| 187 | fclose(out); |
| 188 | return NO_ERROR; |
| 189 | } |
| 190 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Write debugging data about statsd in text format. |
| 193 | */ |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 194 | void StatsService::dump_impl(FILE* out, bool verbose) { |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 195 | StatsdStats::getInstance().dumpStats(out); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 196 | mProcessor->dumpStates(out, verbose); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Implementation of the adb shell cmd stats command. |
| 201 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 202 | status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 203 | // TODO: Permission check |
| 204 | |
| 205 | const int argCount = args.size(); |
| 206 | if (argCount >= 1) { |
| 207 | // adb shell cmd stats config ... |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 208 | if (!args[0].compare(String8("config"))) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 209 | return cmd_config(in, out, err, args); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 210 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 211 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 212 | if (!args[0].compare(String8("print-uid-map"))) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 213 | return cmd_print_uid_map(out, args); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 214 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 215 | |
| 216 | if (!args[0].compare(String8("dump-report"))) { |
| 217 | return cmd_dump_report(out, err, args); |
| 218 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 219 | |
| 220 | if (!args[0].compare(String8("pull-source")) && args.size() > 1) { |
| 221 | return cmd_print_pulled_metrics(out, args); |
| 222 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 223 | |
| 224 | if (!args[0].compare(String8("send-broadcast"))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 225 | return cmd_trigger_broadcast(out, args); |
| 226 | } |
| 227 | |
| 228 | if (!args[0].compare(String8("print-stats"))) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 229 | return cmd_print_stats(out, args); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 230 | } |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 231 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 232 | if (!args[0].compare(String8("meminfo"))) { |
| 233 | return cmd_dump_memory_info(out); |
| 234 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 235 | |
| 236 | if (!args[0].compare(String8("write-to-disk"))) { |
| 237 | return cmd_write_data_to_disk(out); |
| 238 | } |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 239 | |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame^] | 240 | if (!args[0].compare(String8("log-app-breadcrumb"))) { |
| 241 | return cmd_log_app_breadcrumb(out, args); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 242 | } |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 243 | |
| 244 | if (!args[0].compare(String8("clear-puller-cache"))) { |
| 245 | return cmd_clear_puller_cache(out); |
| 246 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 247 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 248 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 249 | print_cmd_help(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 250 | return NO_ERROR; |
| 251 | } |
| 252 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 253 | void StatsService::print_cmd_help(FILE* out) { |
| 254 | fprintf(out, |
| 255 | "usage: adb shell cmd stats print-stats-log [tag_required] " |
| 256 | "[timestamp_nsec_optional]\n"); |
| 257 | fprintf(out, "\n"); |
| 258 | fprintf(out, "\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 259 | fprintf(out, "usage: adb shell cmd stats meminfo\n"); |
| 260 | fprintf(out, "\n"); |
| 261 | fprintf(out, " Prints the malloc debug information. You need to run the following first: \n"); |
| 262 | fprintf(out, " # adb shell stop\n"); |
| 263 | fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n"); |
| 264 | fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n"); |
| 265 | fprintf(out, " # adb shell start\n"); |
| 266 | fprintf(out, "\n"); |
| 267 | fprintf(out, "\n"); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 268 | fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 269 | fprintf(out, "\n"); |
| 270 | fprintf(out, " Prints the UID, app name, version mapping.\n"); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 271 | 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] | 272 | fprintf(out, "\n"); |
| 273 | fprintf(out, "\n"); |
yro | 3fca5ba | 2017-11-17 13:22:52 -0800 | [diff] [blame] | 274 | fprintf(out, "usage: adb shell cmd stats pull-source [int] \n"); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 275 | fprintf(out, "\n"); |
| 276 | fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n"); |
| 277 | fprintf(out, "\n"); |
| 278 | fprintf(out, "\n"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 279 | fprintf(out, "usage: adb shell cmd stats write-to-disk \n"); |
| 280 | fprintf(out, "\n"); |
| 281 | fprintf(out, " Flushes all data on memory to disk.\n"); |
| 282 | fprintf(out, "\n"); |
| 283 | fprintf(out, "\n"); |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame^] | 284 | fprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n"); |
| 285 | fprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n"); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 286 | fprintf(out, " UID The uid to use. It is only possible to pass a UID\n"); |
| 287 | fprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 288 | fprintf(out, " uid is used.\n"); |
| 289 | fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n"); |
| 290 | fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n"); |
| 291 | fprintf(out, "\n"); |
| 292 | fprintf(out, "\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 293 | fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 294 | fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n"); |
| 295 | fprintf(out, "\n"); |
| 296 | 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] | 297 | fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n"); |
| 298 | 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] | 299 | fprintf(out, "\n"); |
| 300 | 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] | 301 | 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] | 302 | fprintf(out, " uid is used.\n"); |
| 303 | fprintf(out, " NAME The per-uid name to use\n"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 304 | fprintf(out, "\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 305 | fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n"); |
| 306 | fprintf(out, "\n be removed from memory and disk!\n"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 307 | fprintf(out, "\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 308 | 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] | 309 | fprintf(out, " Dump all metric data for a configuration.\n"); |
| 310 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 311 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 312 | fprintf(out, " calling uid is used.\n"); |
| 313 | fprintf(out, " NAME The name of the configuration\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 314 | fprintf(out, " --proto Print proto binary.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 315 | fprintf(out, "\n"); |
| 316 | fprintf(out, "\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 317 | fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n"); |
| 318 | fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n"); |
| 319 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 320 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 321 | fprintf(out, " calling uid is used.\n"); |
| 322 | fprintf(out, " NAME The name of the configuration\n"); |
| 323 | fprintf(out, "\n"); |
| 324 | fprintf(out, "\n"); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 325 | fprintf(out, "usage: adb shell cmd stats print-stats\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 326 | fprintf(out, " Prints some basic stats.\n"); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 327 | fprintf(out, "\n"); |
| 328 | fprintf(out, "\n"); |
| 329 | fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n"); |
| 330 | fprintf(out, " Clear cached puller data.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 331 | } |
| 332 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 333 | status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) { |
| 334 | string name; |
| 335 | bool good = false; |
| 336 | int uid; |
| 337 | const int argCount = args.size(); |
| 338 | if (argCount == 2) { |
| 339 | // Automatically pick the UID |
| 340 | uid = IPCThreadState::self()->getCallingUid(); |
| 341 | // TODO: What if this isn't a binder call? Should we fail? |
| 342 | name.assign(args[1].c_str(), args[1].size()); |
| 343 | good = true; |
| 344 | } else if (argCount == 3) { |
| 345 | // If it's a userdebug or eng build, then the shell user can |
| 346 | // impersonate other uids. |
| 347 | if (mEngBuild) { |
| 348 | const char* s = args[1].c_str(); |
| 349 | if (*s != '\0') { |
| 350 | char* end = NULL; |
| 351 | uid = strtol(s, &end, 0); |
| 352 | if (*end == '\0') { |
| 353 | name.assign(args[2].c_str(), args[2].size()); |
| 354 | good = true; |
| 355 | } |
| 356 | } |
| 357 | } else { |
| 358 | fprintf(out, |
| 359 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 360 | "builds.\n"); |
| 361 | } |
| 362 | } |
| 363 | if (!good) { |
| 364 | print_cmd_help(out); |
| 365 | return UNKNOWN_ERROR; |
| 366 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 367 | auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name))); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 368 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 369 | if (sc == nullptr) { |
| 370 | VLOG("Could not access statsCompanion"); |
| 371 | } else if (receiver == nullptr) { |
| 372 | VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str()) |
| 373 | } else { |
| 374 | sc->sendDataBroadcast(receiver); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 375 | VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), |
| 376 | args[2].c_str()); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 377 | } |
| 378 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 379 | return NO_ERROR; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 383 | const int argCount = args.size(); |
| 384 | if (argCount >= 2) { |
| 385 | if (args[1] == "update" || args[1] == "remove") { |
| 386 | bool good = false; |
| 387 | int uid = -1; |
| 388 | string name; |
| 389 | |
| 390 | if (argCount == 3) { |
| 391 | // Automatically pick the UID |
| 392 | uid = IPCThreadState::self()->getCallingUid(); |
| 393 | // TODO: What if this isn't a binder call? Should we fail? |
| 394 | name.assign(args[2].c_str(), args[2].size()); |
| 395 | good = true; |
| 396 | } else if (argCount == 4) { |
| 397 | // If it's a userdebug or eng build, then the shell user can |
| 398 | // impersonate other uids. |
| 399 | if (mEngBuild) { |
| 400 | const char* s = args[2].c_str(); |
| 401 | if (*s != '\0') { |
| 402 | char* end = NULL; |
| 403 | uid = strtol(s, &end, 0); |
| 404 | if (*end == '\0') { |
| 405 | name.assign(args[3].c_str(), args[3].size()); |
| 406 | good = true; |
| 407 | } |
| 408 | } |
| 409 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 410 | fprintf(err, |
| 411 | "The config can only be set for other UIDs on eng or userdebug " |
| 412 | "builds.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 413 | } |
yro | e5f8292 | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 414 | } else if (argCount == 2 && args[1] == "remove") { |
| 415 | good = true; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if (!good) { |
| 419 | // If arg parsing failed, print the help text and return an error. |
| 420 | print_cmd_help(out); |
| 421 | return UNKNOWN_ERROR; |
| 422 | } |
| 423 | |
| 424 | if (args[1] == "update") { |
| 425 | // Read stream into buffer. |
| 426 | string buffer; |
| 427 | if (!android::base::ReadFdToString(fileno(in), &buffer)) { |
| 428 | fprintf(err, "Error reading stream for StatsConfig.\n"); |
| 429 | return UNKNOWN_ERROR; |
| 430 | } |
| 431 | |
| 432 | // Parse buffer. |
| 433 | StatsdConfig config; |
| 434 | if (!config.ParseFromString(buffer)) { |
| 435 | fprintf(err, "Error parsing proto stream for StatsConfig.\n"); |
| 436 | return UNKNOWN_ERROR; |
| 437 | } |
| 438 | |
| 439 | // Add / update the config. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 440 | mConfigManager->UpdateConfig(ConfigKey(uid, StrToInt64(name)), config); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 441 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 442 | if (argCount == 2) { |
| 443 | cmd_remove_all_configs(out); |
| 444 | } else { |
| 445 | // Remove the config. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 446 | mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name))); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 447 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return NO_ERROR; |
| 451 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 452 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 453 | print_cmd_help(out); |
| 454 | return UNKNOWN_ERROR; |
| 455 | } |
| 456 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 457 | status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) { |
| 458 | if (mProcessor != nullptr) { |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 459 | int argCount = args.size(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 460 | bool good = false; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 461 | bool proto = false; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 462 | int uid; |
| 463 | string name; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 464 | if (!std::strcmp("--proto", args[argCount-1].c_str())) { |
| 465 | proto = true; |
| 466 | argCount -= 1; |
| 467 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 468 | if (argCount == 2) { |
| 469 | // Automatically pick the UID |
| 470 | uid = IPCThreadState::self()->getCallingUid(); |
| 471 | // TODO: What if this isn't a binder call? Should we fail? |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 472 | name.assign(args[1].c_str(), args[1].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 473 | good = true; |
| 474 | } else if (argCount == 3) { |
| 475 | // If it's a userdebug or eng build, then the shell user can |
| 476 | // impersonate other uids. |
| 477 | if (mEngBuild) { |
| 478 | const char* s = args[1].c_str(); |
| 479 | if (*s != '\0') { |
| 480 | char* end = NULL; |
| 481 | uid = strtol(s, &end, 0); |
| 482 | if (*end == '\0') { |
| 483 | name.assign(args[2].c_str(), args[2].size()); |
| 484 | good = true; |
| 485 | } |
| 486 | } |
| 487 | } else { |
| 488 | fprintf(out, |
| 489 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 490 | "builds.\n"); |
| 491 | } |
| 492 | } |
| 493 | if (good) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 494 | vector<uint8_t> data; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 495 | mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), time(nullptr) * NS_PER_SEC, |
| 496 | &data); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 497 | // TODO: print the returned StatsLogReport to file instead of printing to logcat. |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 498 | if (proto) { |
| 499 | for (size_t i = 0; i < data.size(); i ++) { |
| 500 | fprintf(out, "%c", data[i]); |
| 501 | } |
| 502 | } else { |
| 503 | fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str()); |
| 504 | fprintf(out, "See the StatsLogReport in logcat...\n"); |
| 505 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 506 | return android::OK; |
| 507 | } else { |
| 508 | // If arg parsing failed, print the help text and return an error. |
| 509 | print_cmd_help(out); |
| 510 | return UNKNOWN_ERROR; |
| 511 | } |
| 512 | } else { |
| 513 | fprintf(out, "Log processor does not exist...\n"); |
| 514 | return UNKNOWN_ERROR; |
| 515 | } |
| 516 | } |
| 517 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 518 | status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 519 | vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys(); |
| 520 | for (const ConfigKey& key : configs) { |
| 521 | fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(), |
| 522 | mProcessor->GetMetricsSize(key)); |
| 523 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 524 | StatsdStats& statsdStats = StatsdStats::getInstance(); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 525 | statsdStats.dumpStats(out); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 526 | return NO_ERROR; |
| 527 | } |
| 528 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 529 | status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) { |
| 530 | if (args.size() > 1) { |
| 531 | string pkg; |
| 532 | pkg.assign(args[1].c_str(), args[1].size()); |
| 533 | auto uids = mUidMap->getAppUid(pkg); |
| 534 | fprintf(out, "%s -> [ ", pkg.c_str()); |
| 535 | for (const auto& uid : uids) { |
| 536 | fprintf(out, "%d ", uid); |
| 537 | } |
| 538 | fprintf(out, "]\n"); |
| 539 | } else { |
| 540 | mUidMap->printUidMap(out); |
| 541 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 542 | return NO_ERROR; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 543 | } |
| 544 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 545 | status_t StatsService::cmd_write_data_to_disk(FILE* out) { |
| 546 | fprintf(out, "Writing data to disk\n"); |
| 547 | mProcessor->WriteDataToDisk(); |
| 548 | return NO_ERROR; |
| 549 | } |
| 550 | |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame^] | 551 | status_t StatsService::cmd_log_app_breadcrumb(FILE* out, const Vector<String8>& args) { |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 552 | bool good = false; |
| 553 | int32_t uid; |
| 554 | int32_t label; |
| 555 | int32_t state; |
| 556 | const int argCount = args.size(); |
| 557 | if (argCount == 3) { |
| 558 | // Automatically pick the UID |
| 559 | uid = IPCThreadState::self()->getCallingUid(); |
| 560 | label = atoi(args[1].c_str()); |
| 561 | state = atoi(args[2].c_str()); |
| 562 | good = true; |
| 563 | } else if (argCount == 4) { |
| 564 | uid = atoi(args[1].c_str()); |
| 565 | // If it's a userdebug or eng build, then the shell user can impersonate other uids. |
| 566 | // Otherwise, the uid must match the actual caller's uid. |
| 567 | if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) { |
| 568 | label = atoi(args[2].c_str()); |
| 569 | state = atoi(args[3].c_str()); |
| 570 | good = true; |
| 571 | } else { |
| 572 | fprintf(out, |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame^] | 573 | "Selecting a UID for writing Appbreadcrumb can only be dumped for other UIDs on eng" |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 574 | " or userdebug builds.\n"); |
| 575 | } |
| 576 | } |
| 577 | if (good) { |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame^] | 578 | fprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state); |
| 579 | android::util::stats_write(android::util::APP_BREADCRUMB_REPORTED, uid, label, state); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 580 | } else { |
| 581 | print_cmd_help(out); |
| 582 | return UNKNOWN_ERROR; |
| 583 | } |
| 584 | return NO_ERROR; |
| 585 | } |
| 586 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 587 | status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) { |
| 588 | int s = atoi(args[1].c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 589 | vector<shared_ptr<LogEvent> > stats; |
| 590 | if (mStatsPullerManager.Pull(s, &stats)) { |
| 591 | for (const auto& it : stats) { |
| 592 | fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); |
| 593 | } |
| 594 | fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); |
| 595 | return NO_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 596 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 597 | return UNKNOWN_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 598 | } |
| 599 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 600 | status_t StatsService::cmd_remove_all_configs(FILE* out) { |
| 601 | fprintf(out, "Removing all configs...\n"); |
| 602 | VLOG("StatsService::cmd_remove_all_configs was called"); |
| 603 | mConfigManager->RemoveAllConfigs(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 604 | StorageManager::deleteAllFiles(STATS_SERVICE_DIR); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 605 | return NO_ERROR; |
| 606 | } |
| 607 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 608 | status_t StatsService::cmd_dump_memory_info(FILE* out) { |
| 609 | std::string s = dumpMemInfo(100); |
| 610 | fprintf(out, "Memory Info\n"); |
| 611 | fprintf(out, "%s", s.c_str()); |
| 612 | return NO_ERROR; |
| 613 | } |
| 614 | |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 615 | status_t StatsService::cmd_clear_puller_cache(FILE* out) { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 616 | IPCThreadState* ipc = IPCThreadState::self(); |
| 617 | VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid()); |
| 618 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 619 | int cleared = mStatsPullerManager.ForceClearPullerCache(); |
| 620 | fprintf(out, "Puller removed %d cached data!\n", cleared); |
| 621 | return NO_ERROR; |
| 622 | } else { |
| 623 | return PERMISSION_DENIED; |
| 624 | } |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 627 | 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] | 628 | const vector<String16>& app) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 629 | VLOG("StatsService::informAllUidData was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 630 | |
| 631 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 632 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 633 | "Only system uid can call informAllUidData"); |
| 634 | } |
| 635 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 636 | mUidMap->updateMap(uid, version, app); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 637 | VLOG("StatsService::informAllUidData succeeded"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 638 | |
| 639 | return Status::ok(); |
| 640 | } |
| 641 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 642 | Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 643 | VLOG("StatsService::informOnePackage was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 644 | |
| 645 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 646 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 647 | "Only system uid can call informOnePackage"); |
| 648 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 649 | mUidMap->updateApp(app, uid, version); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 650 | return Status::ok(); |
| 651 | } |
| 652 | |
| 653 | Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 654 | VLOG("StatsService::informOnePackageRemoved was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 655 | |
| 656 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 657 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 658 | "Only system uid can call informOnePackageRemoved"); |
| 659 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 660 | mUidMap->removeApp(app, uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 661 | return Status::ok(); |
| 662 | } |
| 663 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 664 | Status StatsService::informAnomalyAlarmFired() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 665 | VLOG("StatsService::informAnomalyAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 666 | |
| 667 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 668 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 669 | "Only system uid can call informAnomalyAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 672 | // TODO: This may be a bug. time(nullptr) can be off (wrt AlarmManager's time) and cause us to |
| 673 | // miss the alarm! Eventually we will switch to using elapsedRealTime everywhere, |
| 674 | // which may hopefully fix the problem, so we'll leave this alone for now. |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 675 | uint64_t currentTimeSec = time(nullptr); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 676 | std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet = |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 677 | mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 678 | if (anomalySet.size() > 0) { |
| 679 | VLOG("Found an anomaly alarm that fired."); |
| 680 | mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, anomalySet); |
| 681 | } else { |
| 682 | VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled."); |
| 683 | } |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 684 | return Status::ok(); |
| 685 | } |
| 686 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 687 | Status StatsService::informPollAlarmFired() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 688 | VLOG("StatsService::informPollAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 689 | |
| 690 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 691 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 692 | "Only system uid can call informPollAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 695 | mStatsPullerManager.OnAlarmFired(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 696 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 697 | VLOG("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 698 | |
| 699 | return Status::ok(); |
| 700 | } |
| 701 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 702 | Status StatsService::systemRunning() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 703 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 704 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 705 | "Only system uid can call systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // When system_server is up and running, schedule the dropbox task to run. |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 709 | VLOG("StatsService::systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 710 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 711 | sayHiToStatsCompanion(); |
| 712 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 713 | return Status::ok(); |
| 714 | } |
| 715 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 716 | Status StatsService::writeDataToDisk() { |
| 717 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 718 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 719 | "Only system uid can call systemRunning"); |
| 720 | } |
| 721 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 722 | VLOG("StatsService::writeDataToDisk"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 723 | |
| 724 | mProcessor->WriteDataToDisk(); |
| 725 | |
| 726 | return Status::ok(); |
| 727 | } |
| 728 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 729 | void StatsService::sayHiToStatsCompanion() { |
| 730 | // TODO: This method needs to be private. It is temporarily public and unsecured for testing |
| 731 | // purposes. |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 732 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 733 | if (statsCompanion != nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 734 | VLOG("Telling statsCompanion that statsd is ready"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 735 | statsCompanion->statsdReady(); |
| 736 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 737 | VLOG("Could not access statsCompanion"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 741 | sp<IStatsCompanionService> StatsService::getStatsCompanionService() { |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 742 | sp<IStatsCompanionService> statsCompanion = nullptr; |
| 743 | // Get statscompanion service from service manager |
| 744 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 745 | if (sm != nullptr) { |
| 746 | const String16 name("statscompanion"); |
| 747 | statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name)); |
| 748 | if (statsCompanion == nullptr) { |
| 749 | ALOGW("statscompanion service unavailable!"); |
| 750 | return nullptr; |
| 751 | } |
| 752 | } |
| 753 | return statsCompanion; |
| 754 | } |
| 755 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 756 | Status StatsService::statsCompanionReady() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 757 | VLOG("StatsService::statsCompanionReady was called"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 758 | |
| 759 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 760 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 761 | "Only system uid can call statsCompanionReady"); |
| 762 | } |
| 763 | |
| 764 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 765 | if (statsCompanion == nullptr) { |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 766 | return Status::fromExceptionCode( |
| 767 | Status::EX_NULL_POINTER, |
| 768 | "statscompanion unavailable despite it contacting statsd!"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 769 | } |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 770 | VLOG("StatsService::statsCompanionReady linking to statsCompanion."); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 771 | IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor)); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 772 | mAnomalyMonitor->setStatsCompanionService(statsCompanion); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 773 | SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 774 | |
| 775 | return Status::ok(); |
| 776 | } |
| 777 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 778 | void StatsService::Startup() { |
| 779 | mConfigManager->Startup(); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 782 | void StatsService::OnLogEvent(LogEvent* event) { |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 783 | mProcessor->OnLogEvent(event); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 786 | Status StatsService::getData(int64_t key, vector<uint8_t>* output) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 787 | IPCThreadState* ipc = IPCThreadState::self(); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 788 | VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid()); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 789 | if (checkCallingPermission(String16(kPermissionDump))) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 790 | ConfigKey configKey(ipc->getCallingUid(), key); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 791 | mProcessor->onDumpReport(configKey, time(nullptr) * NS_PER_SEC, output); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 792 | return Status::ok(); |
| 793 | } else { |
| 794 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 795 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 796 | } |
| 797 | |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 798 | Status StatsService::getMetadata(vector<uint8_t>* output) { |
| 799 | IPCThreadState* ipc = IPCThreadState::self(); |
| 800 | VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(), |
| 801 | ipc->getCallingUid()); |
| 802 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 803 | StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters. |
| 804 | return Status::ok(); |
| 805 | } else { |
| 806 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 807 | } |
| 808 | } |
| 809 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 810 | Status StatsService::addConfiguration(int64_t key, |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 811 | const vector <uint8_t>& config, |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 812 | bool* success) { |
| 813 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 814 | if (checkCallingPermission(String16(kPermissionDump))) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 815 | ConfigKey configKey(ipc->getCallingUid(), key); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 816 | StatsdConfig cfg; |
Yangster-mac | bbd056a | 2018-01-22 13:37:02 -0800 | [diff] [blame] | 817 | if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) { |
David Chen | 7d8aa4d | 2017-12-27 13:37:01 -0800 | [diff] [blame] | 818 | *success = false; |
| 819 | return Status::ok(); |
| 820 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 821 | mConfigManager->UpdateConfig(configKey, cfg); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 822 | *success = true; |
| 823 | return Status::ok(); |
| 824 | } else { |
| 825 | *success = false; |
| 826 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | Status StatsService::removeDataFetchOperation(int64_t key, bool* success) { |
| 831 | IPCThreadState* ipc = IPCThreadState::self(); |
| 832 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 833 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 834 | mConfigManager->RemoveConfigReceiver(configKey); |
| 835 | *success = true; |
| 836 | return Status::ok(); |
| 837 | } else { |
| 838 | *success = false; |
| 839 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender, |
| 844 | bool* success) { |
| 845 | IPCThreadState* ipc = IPCThreadState::self(); |
| 846 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 847 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 848 | mConfigManager->SetConfigReceiver(configKey, intentSender); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 849 | *success = true; |
| 850 | return Status::ok(); |
| 851 | } else { |
Yao Chen | aa39bc7 | 2017-12-01 11:16:50 -0800 | [diff] [blame] | 852 | *success = false; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 853 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 854 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 857 | Status StatsService::removeConfiguration(int64_t key, bool* success) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 858 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 859 | if (checkCallingPermission(String16(kPermissionDump))) { |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 860 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 861 | mConfigManager->RemoveConfig(configKey); |
| 862 | SubscriberReporter::getInstance().removeConfig(configKey); |
Yao Chen | aa39bc7 | 2017-12-01 11:16:50 -0800 | [diff] [blame] | 863 | *success = true; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 864 | return Status::ok(); |
| 865 | } else { |
| 866 | *success = false; |
| 867 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 868 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 871 | Status StatsService::setBroadcastSubscriber(int64_t configId, |
| 872 | int64_t subscriberId, |
| 873 | const sp<android::IBinder>& intentSender, |
| 874 | bool* success) { |
| 875 | VLOG("StatsService::setBroadcastSubscriber called."); |
| 876 | IPCThreadState* ipc = IPCThreadState::self(); |
| 877 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 878 | ConfigKey configKey(ipc->getCallingUid(), configId); |
| 879 | SubscriberReporter::getInstance() |
| 880 | .setBroadcastSubscriber(configKey, subscriberId, intentSender); |
| 881 | *success = true; |
| 882 | return Status::ok(); |
| 883 | } else { |
| 884 | *success = false; |
| 885 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | Status StatsService::unsetBroadcastSubscriber(int64_t configId, |
| 890 | int64_t subscriberId, |
| 891 | bool* success) { |
| 892 | VLOG("StatsService::unsetBroadcastSubscriber called."); |
| 893 | IPCThreadState* ipc = IPCThreadState::self(); |
| 894 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 895 | ConfigKey configKey(ipc->getCallingUid(), configId); |
| 896 | SubscriberReporter::getInstance() |
| 897 | .unsetBroadcastSubscriber(configKey, subscriberId); |
| 898 | *success = true; |
| 899 | return Status::ok(); |
| 900 | } else { |
| 901 | *success = false; |
| 902 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 907 | void StatsService::binderDied(const wp <IBinder>& who) { |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 910 | } // namespace statsd |
| 911 | } // namespace os |
| 912 | } // namespace android |