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