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