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