blob: 92d879a2a3c3d019bfe2e674b9851bfabe9b1e13 [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
yro0feae942017-11-15 14:38:48 -08002 * Copyright (C) 2017 The Android Open Source Project
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07003 *
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 Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070019
20#include "StatsService.h"
Yangster-mac330af582018-02-08 15:24:38 -080021#include "stats_log_util.h"
Yao Chen8d9989b2017-11-18 18:54:50 -080022#include "android-base/stringprintf.h"
David Chenadaf8b32017-11-03 15:42:08 -070023#include "config/ConfigKey.h"
24#include "config/ConfigManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080025#include "guardrail/StatsdStats.h"
yro947fbce2017-11-15 22:50:23 -080026#include "storage/StorageManager.h"
Bookatzc6977972018-01-16 16:55:05 -080027#include "subscriber/SubscriberReporter.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070028
David Chen0656b7a2017-09-13 15:53:39 -070029#include <android-base/file.h>
Jeff Sharkey6b649252018-04-16 09:50:22 -060030#include <android-base/stringprintf.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Jeff Sharkey6b649252018-04-16 09:50:22 -060033#include <binder/PermissionController.h>
Chenjie Yu6b1667c2019-01-18 10:09:33 -080034#include <cutils/multiuser.h>
yro87d983c2017-11-14 21:31:43 -080035#include <dirent.h>
David Chen0656b7a2017-09-13 15:53:39 -070036#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070037#include <private/android_filesystem_config.h>
Bookatzb223c4e2018-02-01 15:35:04 -080038#include <statslog.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070039#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070040#include <stdlib.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070041#include <sys/system_properties.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070042#include <unistd.h>
Yao Chena80e5c02018-09-04 13:55:29 -070043#include <utils/Looper.h>
44#include <utils/String16.h>
45#include <chrono>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070046
47using namespace android;
48
Jeff Sharkey6b649252018-04-16 09:50:22 -060049using android::base::StringPrintf;
Bookatzff71cad2018-09-20 17:17:49 -070050using android::util::FIELD_COUNT_REPEATED;
Chenjie Yu6b1667c2019-01-18 10:09:33 -080051using android::util::FIELD_TYPE_INT64;
Bookatzff71cad2018-09-20 17:17:49 -070052using android::util::FIELD_TYPE_MESSAGE;
Jeff Sharkey6b649252018-04-16 09:50:22 -060053
Bookatz906a35c2017-09-20 15:26:44 -070054namespace android {
55namespace os {
56namespace statsd {
57
Chenjie Yu6b1667c2019-01-18 10:09:33 -080058const int FIELD_ID_EXPERIMENT_ID = 1;
59const int FIELD_ID_EXPERIMENT_ID_MSG = 7;
60
David Chenadaf8b32017-11-03 15:42:08 -070061constexpr const char* kPermissionDump = "android.permission.DUMP";
Jeff Sharkey6b649252018-04-16 09:50:22 -060062constexpr const char* kPermissionUsage = "android.permission.PACKAGE_USAGE_STATS";
63
64constexpr const char* kOpUsage = "android:get_usage_stats";
65
yro03faf092017-12-12 00:17:50 -080066#define STATS_SERVICE_DIR "/data/misc/stats-service"
David Chenadaf8b32017-11-03 15:42:08 -070067
Bookatzff71cad2018-09-20 17:17:49 -070068// for StatsDataDumpProto
69const int FIELD_ID_REPORTS_LIST = 1;
70
Jeff Sharkey6b649252018-04-16 09:50:22 -060071static binder::Status ok() {
72 return binder::Status::ok();
73}
74
75static binder::Status exception(uint32_t code, const std::string& msg) {
76 ALOGE("%s (%d)", msg.c_str(), code);
77 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
78}
79
80binder::Status checkUid(uid_t expectedUid) {
81 uid_t uid = IPCThreadState::self()->getCallingUid();
82 if (uid == expectedUid || uid == AID_ROOT) {
83 return ok();
84 } else {
85 return exception(binder::Status::EX_SECURITY,
86 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
87 }
88}
89
90binder::Status checkDumpAndUsageStats(const String16& packageName) {
91 pid_t pid = IPCThreadState::self()->getCallingPid();
92 uid_t uid = IPCThreadState::self()->getCallingUid();
93
94 // Root, system, and shell always have access
95 if (uid == AID_ROOT || uid == AID_SYSTEM || uid == AID_SHELL) {
96 return ok();
97 }
98
99 // Caller must be granted these permissions
100 if (!checkCallingPermission(String16(kPermissionDump))) {
101 return exception(binder::Status::EX_SECURITY,
102 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, kPermissionDump));
103 }
104 if (!checkCallingPermission(String16(kPermissionUsage))) {
105 return exception(binder::Status::EX_SECURITY,
106 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, kPermissionUsage));
107 }
108
109 // Caller must also have usage stats op granted
110 PermissionController pc;
111 switch (pc.noteOp(String16(kOpUsage), uid, packageName)) {
112 case PermissionController::MODE_ALLOWED:
113 case PermissionController::MODE_DEFAULT:
114 return ok();
115 default:
116 return exception(binder::Status::EX_SECURITY,
117 StringPrintf("UID %d / PID %d lacks app-op %s", uid, pid, kOpUsage));
118 }
119}
120
121#define ENFORCE_UID(uid) { \
122 binder::Status status = checkUid((uid)); \
123 if (!status.isOk()) { \
124 return status; \
125 } \
126}
127
128#define ENFORCE_DUMP_AND_USAGE_STATS(packageName) { \
129 binder::Status status = checkDumpAndUsageStats(packageName); \
130 if (!status.isOk()) { \
131 return status; \
132 } \
133}
134
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700135StatsService::StatsService(const sp<Looper>& handlerLooper)
Yangster-mac932ecec2018-02-01 10:23:52 -0800136 : mAnomalyAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
137 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
138 if (sc != nullptr) {
139 sc->setAnomalyAlarm(timeMillis);
140 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
141 }
142 },
143 [](const sp<IStatsCompanionService>& sc) {
144 if (sc != nullptr) {
145 sc->cancelAnomalyAlarm();
146 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
147 }
148 })),
149 mPeriodicAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
150 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
151 if (sc != nullptr) {
152 sc->setAlarmForSubscriberTriggering(timeMillis);
153 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
154 }
155 },
156 [](const sp<IStatsCompanionService>& sc) {
157 if (sc != nullptr) {
158 sc->cancelAlarmForSubscriberTriggering();
159 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
160 }
161
162 })) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700163 mUidMap = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700164 mPullerManager = new StatsPullerManager();
Chenjie Yu80f91122018-01-31 20:24:50 -0800165 StatsPuller::SetUidMap(mUidMap);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700166 mConfigManager = new ConfigManager();
Chenjie Yue2219202018-06-08 10:07:51 -0700167 mProcessor = new StatsLogProcessor(
168 mUidMap, mPullerManager, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor,
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800169 getElapsedRealtimeNs(),
170 [this](const ConfigKey& key) {
Chenjie Yue2219202018-06-08 10:07:51 -0700171 sp<IStatsCompanionService> sc = getStatsCompanionService();
172 auto receiver = mConfigManager->GetConfigReceiver(key);
173 if (sc == nullptr) {
174 VLOG("Could not find StatsCompanionService");
175 return false;
176 } else if (receiver == nullptr) {
177 VLOG("Statscompanion could not find a broadcast receiver for %s",
178 key.ToString().c_str());
179 return false;
180 } else {
181 sc->sendDataBroadcast(receiver, mProcessor->getLastReportTimeNs(key));
182 return true;
183 }
184 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700185
186 mConfigManager->AddListener(mProcessor);
187
188 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700189}
190
Yao Chenef99c4f2017-09-22 16:26:54 -0700191StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700192}
193
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700194void StatsService::init_system_properties() {
195 mEngBuild = false;
196 const prop_info* buildType = __system_property_find("ro.build.type");
197 if (buildType != NULL) {
198 __system_property_read_callback(buildType, init_build_type_callback, this);
199 }
David Chen0656b7a2017-09-13 15:53:39 -0700200}
201
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700202void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
203 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700204 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700205 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
206 }
207}
208
209/**
210 * Implement our own because the default binder implementation isn't
211 * properly handling SHELL_COMMAND_TRANSACTION.
212 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700213status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
214 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700215 switch (code) {
216 case SHELL_COMMAND_TRANSACTION: {
217 int in = data.readFileDescriptor();
218 int out = data.readFileDescriptor();
219 int err = data.readFileDescriptor();
220 int argc = data.readInt32();
221 Vector<String8> args;
222 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
223 args.add(String8(data.readString16()));
224 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700225 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
226 sp<IResultReceiver> resultReceiver =
227 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700228
Yao Chena80e5c02018-09-04 13:55:29 -0700229 err = command(in, out, err, args, resultReceiver);
230 resultReceiver->send(err);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700231 return NO_ERROR;
232 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700233 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700234 }
235}
236
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700237/**
Bookatzff71cad2018-09-20 17:17:49 -0700238 * Write data from statsd.
239 * Format for statsdStats: adb shell dumpsys stats --metadata [-v] [--proto]
240 * Format for data report: adb shell dumpsys stats [anything other than --metadata] [--proto]
241 * Anything ending in --proto will be in proto format.
242 * Anything without --metadata as the first argument will be report information.
243 * (bugreports call "adb shell dumpsys stats --dump-priority NORMAL -a --proto")
244 * TODO: Come up with a more robust method of enacting <serviceutils/PriorityDumper.h>.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700245 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700246status_t StatsService::dump(int fd, const Vector<String16>& args) {
Tej Singhdd83d702018-04-10 17:24:50 -0700247 if (!checkCallingPermission(String16(kPermissionDump))) {
248 return PERMISSION_DENIED;
249 }
Bookatzff71cad2018-09-20 17:17:49 -0700250 int lastArg = args.size() - 1;
251 bool asProto = false;
252 if (lastArg >= 0 && !args[lastArg].compare(String16("--proto"))) { // last argument
253 asProto = true;
254 lastArg--;
Yao Chen884c8c12018-01-26 10:36:25 -0800255 }
Bookatzff71cad2018-09-20 17:17:49 -0700256 if (args.size() > 0 && !args[0].compare(String16("--metadata"))) { // first argument
257 // Request is to dump statsd stats.
258 bool verbose = false;
259 if (lastArg >= 0 && !args[lastArg].compare(String16("-v"))) {
260 verbose = true;
261 lastArg--;
262 }
263 dumpStatsdStats(fd, verbose, asProto);
264 } else {
265 // Request is to dump statsd report data.
266 if (asProto) {
267 dumpIncidentSection(fd);
268 } else {
269 dprintf(fd, "Non-proto format of stats data dump not available; see proto version.\n");
270 }
Tej Singh41b3f9a2018-04-03 17:06:35 -0700271 }
Yao Chen884c8c12018-01-26 10:36:25 -0800272
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700273 return NO_ERROR;
274}
275
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700276/**
Tej Singh41b3f9a2018-04-03 17:06:35 -0700277 * Write debugging data about statsd in text or proto format.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700278 */
Bookatzff71cad2018-09-20 17:17:49 -0700279void StatsService::dumpStatsdStats(int out, bool verbose, bool proto) {
Tej Singh41b3f9a2018-04-03 17:06:35 -0700280 if (proto) {
281 vector<uint8_t> data;
282 StatsdStats::getInstance().dumpStats(&data, false); // does not reset statsdStats.
283 for (size_t i = 0; i < data.size(); i ++) {
Yao Chena80e5c02018-09-04 13:55:29 -0700284 dprintf(out, "%c", data[i]);
Tej Singh41b3f9a2018-04-03 17:06:35 -0700285 }
286 } else {
287 StatsdStats::getInstance().dumpStats(out);
288 mProcessor->dumpStates(out, verbose);
289 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700290}
291
292/**
Bookatzff71cad2018-09-20 17:17:49 -0700293 * Write stats report data in StatsDataDumpProto incident section format.
294 */
295void StatsService::dumpIncidentSection(int out) {
296 ProtoOutputStream proto;
297 for (const ConfigKey& configKey : mConfigManager->GetAllConfigKeys()) {
298 uint64_t reportsListToken =
299 proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS_LIST);
300 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(),
301 true /* includeCurrentBucket */, false /* erase_data */,
302 ADB_DUMP, &proto);
303 proto.end(reportsListToken);
304 proto.flush(out);
Bookatzc71d9012018-12-19 12:28:38 -0800305 proto.clear();
Bookatzff71cad2018-09-20 17:17:49 -0700306 }
307}
308
309/**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700310 * Implementation of the adb shell cmd stats command.
311 */
Yao Chena80e5c02018-09-04 13:55:29 -0700312status_t StatsService::command(int in, int out, int err, Vector<String8>& args,
313 sp<IResultReceiver> resultReceiver) {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600314 uid_t uid = IPCThreadState::self()->getCallingUid();
315 if (uid != AID_ROOT && uid != AID_SHELL) {
316 return PERMISSION_DENIED;
317 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700318
319 const int argCount = args.size();
320 if (argCount >= 1) {
321 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700322 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700323 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700324 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700325
David Chende701692017-10-05 13:16:02 -0700326 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800327 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700328 }
Yao Chen729093d2017-10-16 10:33:26 -0700329
330 if (!args[0].compare(String8("dump-report"))) {
Bookatzff71cad2018-09-20 17:17:49 -0700331 return cmd_dump_report(out, args);
Yao Chen729093d2017-10-16 10:33:26 -0700332 }
David Chen1481fe12017-10-16 13:16:34 -0700333
334 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
335 return cmd_print_pulled_metrics(out, args);
336 }
David Chenadaf8b32017-11-03 15:42:08 -0700337
338 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800339 return cmd_trigger_broadcast(out, args);
340 }
341
342 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800343 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700344 }
yro87d983c2017-11-14 21:31:43 -0800345
Yao Chen8d9989b2017-11-18 18:54:50 -0800346 if (!args[0].compare(String8("meminfo"))) {
347 return cmd_dump_memory_info(out);
348 }
yro947fbce2017-11-15 22:50:23 -0800349
350 if (!args[0].compare(String8("write-to-disk"))) {
351 return cmd_write_data_to_disk(out);
352 }
Bookatzb223c4e2018-02-01 15:35:04 -0800353
David Chen0b5c90c2018-01-25 16:51:49 -0800354 if (!args[0].compare(String8("log-app-breadcrumb"))) {
355 return cmd_log_app_breadcrumb(out, args);
Bookatzb223c4e2018-02-01 15:35:04 -0800356 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800357
358 if (!args[0].compare(String8("clear-puller-cache"))) {
359 return cmd_clear_puller_cache(out);
360 }
Yao Chen876889c2018-05-02 11:16:16 -0700361
362 if (!args[0].compare(String8("print-logs"))) {
363 return cmd_print_logs(out, args);
364 }
Yao Chena80e5c02018-09-04 13:55:29 -0700365 if (!args[0].compare(String8("data-subscribe"))) {
366 if (mShellSubscriber == nullptr) {
Yao Chen41e606c2018-10-05 15:54:11 -0700367 mShellSubscriber = new ShellSubscriber(mUidMap, mPullerManager);
Yao Chena80e5c02018-09-04 13:55:29 -0700368 }
Yao Chen35cb8d62019-01-03 16:49:14 -0800369 int timeoutSec = -1;
370 if (argCount >= 2) {
371 timeoutSec = atoi(args[1].c_str());
372 }
373 mShellSubscriber->startNewSubscription(in, out, resultReceiver, timeoutSec);
Yao Chena80e5c02018-09-04 13:55:29 -0700374 return NO_ERROR;
375 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700376 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700377
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700378 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700379 return NO_ERROR;
380}
381
Yao Chena80e5c02018-09-04 13:55:29 -0700382void StatsService::print_cmd_help(int out) {
383 dprintf(out,
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700384 "usage: adb shell cmd stats print-stats-log [tag_required] "
385 "[timestamp_nsec_optional]\n");
Yao Chena80e5c02018-09-04 13:55:29 -0700386 dprintf(out, "\n");
387 dprintf(out, "\n");
388 dprintf(out, "usage: adb shell cmd stats meminfo\n");
389 dprintf(out, "\n");
390 dprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
391 dprintf(out, " # adb shell stop\n");
392 dprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
393 dprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
394 dprintf(out, " # adb shell start\n");
395 dprintf(out, "\n");
396 dprintf(out, "\n");
397 dprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
398 dprintf(out, "\n");
399 dprintf(out, " Prints the UID, app name, version mapping.\n");
400 dprintf(out, " PKG Optional package name to print the uids of the package\n");
401 dprintf(out, "\n");
402 dprintf(out, "\n");
403 dprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
404 dprintf(out, "\n");
405 dprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
406 dprintf(out, "\n");
407 dprintf(out, "\n");
408 dprintf(out, "usage: adb shell cmd stats write-to-disk \n");
409 dprintf(out, "\n");
410 dprintf(out, " Flushes all data on memory to disk.\n");
411 dprintf(out, "\n");
412 dprintf(out, "\n");
413 dprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n");
414 dprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n");
415 dprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
416 dprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
417 dprintf(out, " uid is used.\n");
418 dprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
419 dprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
420 dprintf(out, "\n");
421 dprintf(out, "\n");
422 dprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
423 dprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
424 dprintf(out, "\n");
425 dprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
426 dprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
427 dprintf(out, " provided, then all configs will be removed from memory and disk.\n");
428 dprintf(out, "\n");
429 dprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
430 dprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
431 dprintf(out, " uid is used.\n");
432 dprintf(out, " NAME The per-uid name to use\n");
433 dprintf(out, "\n");
434 dprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
435 dprintf(out, "\n be removed from memory and disk!\n");
436 dprintf(out, "\n");
437 dprintf(out,
Bookatz3e906582018-12-10 17:26:58 -0800438 "usage: adb shell cmd stats dump-report [UID] NAME [--keep_data] "
439 "[--include_current_bucket] [--proto]\n");
Yao Chena80e5c02018-09-04 13:55:29 -0700440 dprintf(out, " Dump all metric data for a configuration.\n");
441 dprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
442 dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
443 dprintf(out, " calling uid is used.\n");
444 dprintf(out, " NAME The name of the configuration\n");
Bookatz3e906582018-12-10 17:26:58 -0800445 dprintf(out, " --keep_data Do NOT erase the data upon dumping it.\n");
Yao Chena80e5c02018-09-04 13:55:29 -0700446 dprintf(out, " --proto Print proto binary.\n");
447 dprintf(out, "\n");
448 dprintf(out, "\n");
449 dprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
450 dprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
451 dprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
452 dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
453 dprintf(out, " calling uid is used.\n");
454 dprintf(out, " NAME The name of the configuration\n");
455 dprintf(out, "\n");
456 dprintf(out, "\n");
457 dprintf(out, "usage: adb shell cmd stats print-stats\n");
458 dprintf(out, " Prints some basic stats.\n");
459 dprintf(out, " --proto Print proto binary instead of string format.\n");
460 dprintf(out, "\n");
461 dprintf(out, "\n");
462 dprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
463 dprintf(out, " Clear cached puller data.\n");
464 dprintf(out, "\n");
465 dprintf(out, "usage: adb shell cmd stats print-logs\n");
466 dprintf(out, " Only works on eng build\n");
David Chenadaf8b32017-11-03 15:42:08 -0700467}
468
Yao Chena80e5c02018-09-04 13:55:29 -0700469status_t StatsService::cmd_trigger_broadcast(int out, Vector<String8>& args) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800470 string name;
471 bool good = false;
472 int uid;
473 const int argCount = args.size();
474 if (argCount == 2) {
475 // Automatically pick the UID
476 uid = IPCThreadState::self()->getCallingUid();
David Chen1d7b0cd2017-11-15 14:20:04 -0800477 name.assign(args[1].c_str(), args[1].size());
478 good = true;
479 } else if (argCount == 3) {
Bookatzd2386572018-12-14 15:53:14 -0800480 good = getUidFromArgs(args, 1, uid);
481 if (!good) {
482 dprintf(out, "Invalid UID. Note that the metrics can only be dumped for "
483 "other UIDs on eng or userdebug builds.\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800484 }
Bookatzd2386572018-12-14 15:53:14 -0800485 name.assign(args[2].c_str(), args[2].size());
David Chen1d7b0cd2017-11-15 14:20:04 -0800486 }
487 if (!good) {
488 print_cmd_help(out);
489 return UNKNOWN_ERROR;
490 }
David Chend37bc232018-04-12 18:05:11 -0700491 ConfigKey key(uid, StrToInt64(name));
492 auto receiver = mConfigManager->GetConfigReceiver(key);
yro4d889e62017-11-17 15:44:48 -0800493 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen661f7912018-01-22 17:46:24 -0800494 if (sc == nullptr) {
495 VLOG("Could not access statsCompanion");
496 } else if (receiver == nullptr) {
497 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str())
498 } else {
David Chend37bc232018-04-12 18:05:11 -0700499 sc->sendDataBroadcast(receiver, mProcessor->getLastReportTimeNs(key));
yro74fed972017-11-27 14:42:42 -0800500 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
501 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800502 }
503
David Chenadaf8b32017-11-03 15:42:08 -0700504 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700505}
506
Yao Chena80e5c02018-09-04 13:55:29 -0700507status_t StatsService::cmd_config(int in, int out, int err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700508 const int argCount = args.size();
509 if (argCount >= 2) {
510 if (args[1] == "update" || args[1] == "remove") {
511 bool good = false;
512 int uid = -1;
513 string name;
514
515 if (argCount == 3) {
516 // Automatically pick the UID
517 uid = IPCThreadState::self()->getCallingUid();
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700518 name.assign(args[2].c_str(), args[2].size());
519 good = true;
520 } else if (argCount == 4) {
Bookatzd2386572018-12-14 15:53:14 -0800521 good = getUidFromArgs(args, 2, uid);
522 if (!good) {
523 dprintf(err, "Invalid UID. Note that the config can only be set for "
524 "other UIDs on eng or userdebug builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700525 }
Bookatzd2386572018-12-14 15:53:14 -0800526 name.assign(args[3].c_str(), args[3].size());
yroe5f82922018-01-22 18:37:27 -0800527 } else if (argCount == 2 && args[1] == "remove") {
528 good = true;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700529 }
530
531 if (!good) {
532 // If arg parsing failed, print the help text and return an error.
533 print_cmd_help(out);
534 return UNKNOWN_ERROR;
535 }
536
537 if (args[1] == "update") {
yro255f72e2018-02-26 15:15:17 -0800538 char* endp;
539 int64_t configID = strtoll(name.c_str(), &endp, 10);
540 if (endp == name.c_str() || *endp != '\0') {
Yao Chena80e5c02018-09-04 13:55:29 -0700541 dprintf(err, "Error parsing config ID.\n");
yro255f72e2018-02-26 15:15:17 -0800542 return UNKNOWN_ERROR;
543 }
544
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700545 // Read stream into buffer.
546 string buffer;
Yao Chena80e5c02018-09-04 13:55:29 -0700547 if (!android::base::ReadFdToString(in, &buffer)) {
548 dprintf(err, "Error reading stream for StatsConfig.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700549 return UNKNOWN_ERROR;
550 }
551
552 // Parse buffer.
553 StatsdConfig config;
554 if (!config.ParseFromString(buffer)) {
Yao Chena80e5c02018-09-04 13:55:29 -0700555 dprintf(err, "Error parsing proto stream for StatsConfig.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700556 return UNKNOWN_ERROR;
557 }
558
559 // Add / update the config.
yro255f72e2018-02-26 15:15:17 -0800560 mConfigManager->UpdateConfig(ConfigKey(uid, configID), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700561 } else {
yro74fed972017-11-27 14:42:42 -0800562 if (argCount == 2) {
563 cmd_remove_all_configs(out);
564 } else {
565 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800566 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800567 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700568 }
569
570 return NO_ERROR;
571 }
David Chen0656b7a2017-09-13 15:53:39 -0700572 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700573 print_cmd_help(out);
574 return UNKNOWN_ERROR;
575}
576
Bookatzff71cad2018-09-20 17:17:49 -0700577status_t StatsService::cmd_dump_report(int out, const Vector<String8>& args) {
Yao Chen729093d2017-10-16 10:33:26 -0700578 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800579 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700580 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800581 bool proto = false;
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700582 bool includeCurrentBucket = false;
Bookatz3e906582018-12-10 17:26:58 -0800583 bool eraseData = true;
Yao Chen729093d2017-10-16 10:33:26 -0700584 int uid;
585 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800586 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
587 proto = true;
588 argCount -= 1;
589 }
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700590 if (!std::strcmp("--include_current_bucket", args[argCount-1].c_str())) {
591 includeCurrentBucket = true;
592 argCount -= 1;
593 }
Bookatz3e906582018-12-10 17:26:58 -0800594 if (!std::strcmp("--keep_data", args[argCount-1].c_str())) {
595 eraseData = false;
596 argCount -= 1;
597 }
Yao Chen729093d2017-10-16 10:33:26 -0700598 if (argCount == 2) {
599 // Automatically pick the UID
600 uid = IPCThreadState::self()->getCallingUid();
Yao Chen5154a372017-10-30 22:57:06 -0700601 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700602 good = true;
603 } else if (argCount == 3) {
Bookatzd2386572018-12-14 15:53:14 -0800604 good = getUidFromArgs(args, 1, uid);
605 if (!good) {
606 dprintf(out, "Invalid UID. Note that the metrics can only be dumped for "
607 "other UIDs on eng or userdebug builds.\n");
Yao Chen729093d2017-10-16 10:33:26 -0700608 }
Bookatzd2386572018-12-14 15:53:14 -0800609 name.assign(args[2].c_str(), args[2].size());
Yao Chen729093d2017-10-16 10:33:26 -0700610 }
611 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800612 vector<uint8_t> data;
David Chen926fc752018-02-23 13:31:43 -0800613 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(),
Bookatz3e906582018-12-10 17:26:58 -0800614 includeCurrentBucket, eraseData, ADB_DUMP, &data);
Chenjie Yub236c862017-11-28 22:20:44 -0800615 if (proto) {
616 for (size_t i = 0; i < data.size(); i ++) {
Yao Chena80e5c02018-09-04 13:55:29 -0700617 dprintf(out, "%c", data[i]);
Chenjie Yub236c862017-11-28 22:20:44 -0800618 }
619 } else {
Bookatzff71cad2018-09-20 17:17:49 -0700620 dprintf(out, "Non-proto stats data dump not currently supported.\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800621 }
Yao Chen729093d2017-10-16 10:33:26 -0700622 return android::OK;
623 } else {
624 // If arg parsing failed, print the help text and return an error.
625 print_cmd_help(out);
626 return UNKNOWN_ERROR;
627 }
628 } else {
Yao Chena80e5c02018-09-04 13:55:29 -0700629 dprintf(out, "Log processor does not exist...\n");
Yao Chen729093d2017-10-16 10:33:26 -0700630 return UNKNOWN_ERROR;
631 }
632}
633
Yao Chena80e5c02018-09-04 13:55:29 -0700634status_t StatsService::cmd_print_stats(int out, const Vector<String8>& args) {
Tej Singh41b3f9a2018-04-03 17:06:35 -0700635 int argCount = args.size();
636 bool proto = false;
637 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
638 proto = true;
639 argCount -= 1;
David Chen1d7b0cd2017-11-15 14:20:04 -0800640 }
Yao Chenb3561512017-11-21 18:07:17 -0800641 StatsdStats& statsdStats = StatsdStats::getInstance();
Tej Singh41b3f9a2018-04-03 17:06:35 -0700642 if (proto) {
643 vector<uint8_t> data;
644 statsdStats.dumpStats(&data, false); // does not reset statsdStats.
645 for (size_t i = 0; i < data.size(); i ++) {
Yao Chena80e5c02018-09-04 13:55:29 -0700646 dprintf(out, "%c", data[i]);
Tej Singh41b3f9a2018-04-03 17:06:35 -0700647 }
648
649 } else {
650 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
651 for (const ConfigKey& key : configs) {
Yao Chena80e5c02018-09-04 13:55:29 -0700652 dprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
Tej Singh41b3f9a2018-04-03 17:06:35 -0700653 mProcessor->GetMetricsSize(key));
654 }
655 statsdStats.dumpStats(out);
656 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800657 return NO_ERROR;
658}
659
Yao Chena80e5c02018-09-04 13:55:29 -0700660status_t StatsService::cmd_print_uid_map(int out, const Vector<String8>& args) {
Yao Chend10f7b12017-12-18 12:53:50 -0800661 if (args.size() > 1) {
662 string pkg;
663 pkg.assign(args[1].c_str(), args[1].size());
664 auto uids = mUidMap->getAppUid(pkg);
Yao Chena80e5c02018-09-04 13:55:29 -0700665 dprintf(out, "%s -> [ ", pkg.c_str());
Yao Chend10f7b12017-12-18 12:53:50 -0800666 for (const auto& uid : uids) {
Yao Chena80e5c02018-09-04 13:55:29 -0700667 dprintf(out, "%d ", uid);
Yao Chend10f7b12017-12-18 12:53:50 -0800668 }
Yao Chena80e5c02018-09-04 13:55:29 -0700669 dprintf(out, "]\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800670 } else {
671 mUidMap->printUidMap(out);
672 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700673 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700674}
675
Yao Chena80e5c02018-09-04 13:55:29 -0700676status_t StatsService::cmd_write_data_to_disk(int out) {
677 dprintf(out, "Writing data to disk\n");
Yangster-mac892f3d32018-05-02 14:16:48 -0700678 mProcessor->WriteDataToDisk(ADB_DUMP);
yro947fbce2017-11-15 22:50:23 -0800679 return NO_ERROR;
680}
681
Yao Chena80e5c02018-09-04 13:55:29 -0700682status_t StatsService::cmd_log_app_breadcrumb(int out, const Vector<String8>& args) {
Bookatzb223c4e2018-02-01 15:35:04 -0800683 bool good = false;
684 int32_t uid;
685 int32_t label;
686 int32_t state;
687 const int argCount = args.size();
688 if (argCount == 3) {
689 // Automatically pick the UID
690 uid = IPCThreadState::self()->getCallingUid();
691 label = atoi(args[1].c_str());
692 state = atoi(args[2].c_str());
693 good = true;
694 } else if (argCount == 4) {
Bookatzd2386572018-12-14 15:53:14 -0800695 good = getUidFromArgs(args, 1, uid);
696 if (!good) {
Yao Chena80e5c02018-09-04 13:55:29 -0700697 dprintf(out,
Bookatzd2386572018-12-14 15:53:14 -0800698 "Invalid UID. Note that selecting a UID for writing AppBreadcrumb can only be "
699 "done for other UIDs on eng or userdebug builds.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800700 }
Bookatzd2386572018-12-14 15:53:14 -0800701 label = atoi(args[2].c_str());
702 state = atoi(args[3].c_str());
Bookatzb223c4e2018-02-01 15:35:04 -0800703 }
704 if (good) {
Yao Chena80e5c02018-09-04 13:55:29 -0700705 dprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state);
David Chen0b5c90c2018-01-25 16:51:49 -0800706 android::util::stats_write(android::util::APP_BREADCRUMB_REPORTED, uid, label, state);
Bookatzb223c4e2018-02-01 15:35:04 -0800707 } else {
708 print_cmd_help(out);
709 return UNKNOWN_ERROR;
710 }
711 return NO_ERROR;
712}
713
Yao Chena80e5c02018-09-04 13:55:29 -0700714status_t StatsService::cmd_print_pulled_metrics(int out, const Vector<String8>& args) {
David Chen1481fe12017-10-16 13:16:34 -0700715 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700716 vector<shared_ptr<LogEvent> > stats;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800717 if (mPullerManager->Pull(s, &stats)) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700718 for (const auto& it : stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700719 dprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700720 }
Yao Chena80e5c02018-09-04 13:55:29 -0700721 dprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700722 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700723 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700724 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700725}
726
Yao Chena80e5c02018-09-04 13:55:29 -0700727status_t StatsService::cmd_remove_all_configs(int out) {
728 dprintf(out, "Removing all configs...\n");
yro74fed972017-11-27 14:42:42 -0800729 VLOG("StatsService::cmd_remove_all_configs was called");
730 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800731 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800732 return NO_ERROR;
733}
734
Yao Chena80e5c02018-09-04 13:55:29 -0700735status_t StatsService::cmd_dump_memory_info(int out) {
736 dprintf(out, "meminfo not available.\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800737 return NO_ERROR;
738}
739
Yao Chena80e5c02018-09-04 13:55:29 -0700740status_t StatsService::cmd_clear_puller_cache(int out) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800741 IPCThreadState* ipc = IPCThreadState::self();
Yangster-mac932ecec2018-02-01 10:23:52 -0800742 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i",
743 ipc->getCallingPid(), ipc->getCallingUid());
Chenjie Yufa22d652018-02-05 14:37:48 -0800744 if (checkCallingPermission(String16(kPermissionDump))) {
Chenjie Yue2219202018-06-08 10:07:51 -0700745 int cleared = mPullerManager->ForceClearPullerCache();
Yao Chena80e5c02018-09-04 13:55:29 -0700746 dprintf(out, "Puller removed %d cached data!\n", cleared);
Chenjie Yufa22d652018-02-05 14:37:48 -0800747 return NO_ERROR;
748 } else {
749 return PERMISSION_DENIED;
750 }
Chenjie Yue72252b2018-02-01 13:19:35 -0800751}
752
Yao Chena80e5c02018-09-04 13:55:29 -0700753status_t StatsService::cmd_print_logs(int out, const Vector<String8>& args) {
Yao Chen876889c2018-05-02 11:16:16 -0700754 IPCThreadState* ipc = IPCThreadState::self();
755 VLOG("StatsService::cmd_print_logs with Pid %i, Uid %i", ipc->getCallingPid(),
756 ipc->getCallingUid());
757 if (checkCallingPermission(String16(kPermissionDump))) {
758 bool enabled = true;
759 if (args.size() >= 2) {
760 enabled = atoi(args[1].c_str()) != 0;
761 }
762 mProcessor->setPrintLogs(enabled);
763 return NO_ERROR;
764 } else {
765 return PERMISSION_DENIED;
766 }
767}
768
Bookatzd2386572018-12-14 15:53:14 -0800769bool StatsService::getUidFromArgs(const Vector<String8>& args, size_t uidArgIndex, int32_t& uid) {
770 const char* s = args[uidArgIndex].c_str();
771 if (*s == '\0') {
772 return false;
773 }
774 char* endc = NULL;
775 int64_t longUid = strtol(s, &endc, 0);
776 if (*endc != '\0') {
777 return false;
778 }
779 int32_t goodUid = static_cast<int32_t>(longUid);
780 if (longUid < 0 || static_cast<uint64_t>(longUid) != static_cast<uid_t>(goodUid)) {
781 return false; // It was not of uid_t type.
782 }
783 uid = goodUid;
784
785 int32_t callingUid = IPCThreadState::self()->getCallingUid();
786 return mEngBuild // UserDebug/EngBuild are allowed to impersonate uids.
787 || (callingUid == goodUid) // Anyone can 'impersonate' themselves.
788 || (callingUid == AID_ROOT && goodUid == AID_SHELL); // ROOT can impersonate SHELL.
789}
790
Dianne Hackborn3accca02013-09-20 09:32:11 -0700791Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
dwchen730403e2018-10-29 11:41:56 -0700792 const vector<String16>& version_string,
793 const vector<String16>& app,
794 const vector<String16>& installer) {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600795 ENFORCE_UID(AID_SYSTEM);
796
yro74fed972017-11-27 14:42:42 -0800797 VLOG("StatsService::informAllUidData was called");
dwchen730403e2018-10-29 11:41:56 -0700798 mUidMap->updateMap(getElapsedRealtimeNs(), uid, version, version_string, app, installer);
yro74fed972017-11-27 14:42:42 -0800799 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700800
801 return Status::ok();
802}
803
dwchen730403e2018-10-29 11:41:56 -0700804Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version,
805 const String16& version_string, const String16& installer) {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600806 ENFORCE_UID(AID_SYSTEM);
David Chende701692017-10-05 13:16:02 -0700807
Jeff Sharkey6b649252018-04-16 09:50:22 -0600808 VLOG("StatsService::informOnePackage was called");
dwchen730403e2018-10-29 11:41:56 -0700809 mUidMap->updateApp(getElapsedRealtimeNs(), app, uid, version, version_string, installer);
David Chende701692017-10-05 13:16:02 -0700810 return Status::ok();
811}
812
813Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600814 ENFORCE_UID(AID_SYSTEM);
David Chende701692017-10-05 13:16:02 -0700815
Jeff Sharkey6b649252018-04-16 09:50:22 -0600816 VLOG("StatsService::informOnePackageRemoved was called");
David Chenbd125272018-04-04 19:02:50 -0700817 mUidMap->removeApp(getElapsedRealtimeNs(), app, uid);
yro01924022018-02-20 18:20:49 -0800818 mConfigManager->RemoveConfigs(uid);
David Chende701692017-10-05 13:16:02 -0700819 return Status::ok();
820}
821
Yao Chenef99c4f2017-09-22 16:26:54 -0700822Status StatsService::informAnomalyAlarmFired() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600823 ENFORCE_UID(AID_SYSTEM);
824
yro74fed972017-11-27 14:42:42 -0800825 VLOG("StatsService::informAnomalyAlarmFired was called");
Yangster-macb142cc82018-03-30 15:22:08 -0700826 int64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800827 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
828 mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
829 if (alarmSet.size() > 0) {
Bookatz66fe0612018-02-07 18:51:48 -0800830 VLOG("Found an anomaly alarm that fired.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800831 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
Bookatz66fe0612018-02-07 18:51:48 -0800832 } else {
833 VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
834 }
Bookatz1b0b1142017-09-08 11:58:42 -0700835 return Status::ok();
836}
837
Yangster-mac932ecec2018-02-01 10:23:52 -0800838Status StatsService::informAlarmForSubscriberTriggeringFired() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600839 ENFORCE_UID(AID_SYSTEM);
840
Yangster-mac932ecec2018-02-01 10:23:52 -0800841 VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
Yangster-macb142cc82018-03-30 15:22:08 -0700842 int64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800843 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
844 mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
845 if (alarmSet.size() > 0) {
846 VLOG("Found periodic alarm fired.");
847 mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
848 } else {
849 ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
850 }
851 return Status::ok();
852}
853
Yao Chenef99c4f2017-09-22 16:26:54 -0700854Status StatsService::informPollAlarmFired() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600855 ENFORCE_UID(AID_SYSTEM);
856
yro74fed972017-11-27 14:42:42 -0800857 VLOG("StatsService::informPollAlarmFired was called");
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700858 mProcessor->informPullAlarmFired(getElapsedRealtimeNs());
yro74fed972017-11-27 14:42:42 -0800859 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700860 return Status::ok();
861}
862
Yao Chenef99c4f2017-09-22 16:26:54 -0700863Status StatsService::systemRunning() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600864 ENFORCE_UID(AID_SYSTEM);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700865
866 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800867 VLOG("StatsService::systemRunning");
Bookatzb487b552017-09-18 11:26:01 -0700868 sayHiToStatsCompanion();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700869 return Status::ok();
870}
871
Yangster-mac892f3d32018-05-02 14:16:48 -0700872Status StatsService::informDeviceShutdown() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600873 ENFORCE_UID(AID_SYSTEM);
Chenjie Yue36018b2018-04-16 15:18:30 -0700874 VLOG("StatsService::informDeviceShutdown");
Yangster-mac892f3d32018-05-02 14:16:48 -0700875 mProcessor->WriteDataToDisk(DEVICE_SHUTDOWN);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800876 mProcessor->WriteMetricsActivationToDisk(getElapsedRealtimeNs());
yro947fbce2017-11-15 22:50:23 -0800877 return Status::ok();
878}
879
Yao Chenef99c4f2017-09-22 16:26:54 -0700880void StatsService::sayHiToStatsCompanion() {
Bookatzb487b552017-09-18 11:26:01 -0700881 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
882 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800883 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700884 statsCompanion->statsdReady();
885 } else {
yro74fed972017-11-27 14:42:42 -0800886 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700887 }
888}
889
Yao Chenef99c4f2017-09-22 16:26:54 -0700890Status StatsService::statsCompanionReady() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600891 ENFORCE_UID(AID_SYSTEM);
892
yro74fed972017-11-27 14:42:42 -0800893 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700894 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
895 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700896 return Status::fromExceptionCode(
897 Status::EX_NULL_POINTER,
898 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700899 }
yro74fed972017-11-27 14:42:42 -0800900 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700901 IInterface::asBinder(statsCompanion)->linkToDeath(this);
Chenjie Yue2219202018-06-08 10:07:51 -0700902 mPullerManager->SetStatsCompanionService(statsCompanion);
Yangster-mac932ecec2018-02-01 10:23:52 -0800903 mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion);
904 mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800905 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700906 return Status::ok();
907}
908
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700909void StatsService::Startup() {
910 mConfigManager->Startup();
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800911 mProcessor->LoadMetricsActivationFromDisk();
Bookatz906a35c2017-09-20 15:26:44 -0700912}
913
Yangster-mac97e7d202018-10-09 11:05:39 -0700914void StatsService::Terminate() {
915 ALOGI("StatsService::Terminating");
916 if (mProcessor != nullptr) {
917 mProcessor->WriteDataToDisk(TERMINATION_SIGNAL_RECEIVED);
918 }
919}
920
Yao Chen3ff3a492018-08-06 16:17:37 -0700921void StatsService::OnLogEvent(LogEvent* event) {
922 mProcessor->OnLogEvent(event);
Yao Chena80e5c02018-09-04 13:55:29 -0700923 if (mShellSubscriber != nullptr) {
924 mShellSubscriber->onLogEvent(*event);
925 }
Bookatz906a35c2017-09-20 15:26:44 -0700926}
927
Jeff Sharkey6b649252018-04-16 09:50:22 -0600928Status StatsService::getData(int64_t key, const String16& packageName, vector<uint8_t>* output) {
929 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
930
David Chenadaf8b32017-11-03 15:42:08 -0700931 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800932 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Bookatz4f716292018-04-10 17:15:12 -0700933 ConfigKey configKey(ipc->getCallingUid(), key);
David Chen56ae0d92018-05-11 16:00:22 -0700934 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), false /* include_current_bucket*/,
Bookatzff71cad2018-09-20 17:17:49 -0700935 true /* erase_data */, GET_DATA_CALLED, output);
Bookatz4f716292018-04-10 17:15:12 -0700936 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -0700937}
938
Jeff Sharkey6b649252018-04-16 09:50:22 -0600939Status StatsService::getMetadata(const String16& packageName, vector<uint8_t>* output) {
940 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
941
David Chen2e8f3802017-11-22 10:56:48 -0800942 IPCThreadState* ipc = IPCThreadState::self();
943 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
944 ipc->getCallingUid());
Bookatz4f716292018-04-10 17:15:12 -0700945 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
946 return Status::ok();
David Chen2e8f3802017-11-22 10:56:48 -0800947}
948
Jeff Sharkey6b649252018-04-16 09:50:22 -0600949Status StatsService::addConfiguration(int64_t key, const vector <uint8_t>& config,
950 const String16& packageName) {
951 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
952
David Chenadaf8b32017-11-03 15:42:08 -0700953 IPCThreadState* ipc = IPCThreadState::self();
Bookatz4f716292018-04-10 17:15:12 -0700954 if (addConfigurationChecked(ipc->getCallingUid(), key, config)) {
David Chen661f7912018-01-22 17:46:24 -0800955 return Status::ok();
956 } else {
Bookatz4f716292018-04-10 17:15:12 -0700957 ALOGE("Could not parse malformatted StatsdConfig");
958 return Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT,
959 "config does not correspond to a StatsdConfig proto");
David Chen661f7912018-01-22 17:46:24 -0800960 }
961}
962
David Chen9fdd4032018-03-20 14:38:56 -0700963bool StatsService::addConfigurationChecked(int uid, int64_t key, const vector<uint8_t>& config) {
964 ConfigKey configKey(uid, key);
965 StatsdConfig cfg;
966 if (config.size() > 0) { // If the config is empty, skip parsing.
967 if (!cfg.ParseFromArray(&config[0], config.size())) {
968 return false;
969 }
970 }
971 mConfigManager->UpdateConfig(configKey, cfg);
972 return true;
973}
974
Jeff Sharkey6b649252018-04-16 09:50:22 -0600975Status StatsService::removeDataFetchOperation(int64_t key, const String16& packageName) {
976 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
977
Bookatz4f716292018-04-10 17:15:12 -0700978 IPCThreadState* ipc = IPCThreadState::self();
979 ConfigKey configKey(ipc->getCallingUid(), key);
980 mConfigManager->RemoveConfigReceiver(configKey);
981 return Status::ok();
David Chen661f7912018-01-22 17:46:24 -0800982}
983
Jeff Sharkey6b649252018-04-16 09:50:22 -0600984Status StatsService::setDataFetchOperation(int64_t key,
985 const sp<android::IBinder>& intentSender,
986 const String16& packageName) {
987 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
988
Bookatz4f716292018-04-10 17:15:12 -0700989 IPCThreadState* ipc = IPCThreadState::self();
990 ConfigKey configKey(ipc->getCallingUid(), key);
991 mConfigManager->SetConfigReceiver(configKey, intentSender);
David Chen48944902018-05-03 10:29:11 -0700992 if (StorageManager::hasConfigMetricsReport(configKey)) {
993 VLOG("StatsService::setDataFetchOperation marking configKey %s to dump reports on disk",
994 configKey.ToString().c_str());
995 mProcessor->noteOnDiskData(configKey);
996 }
Bookatz4f716292018-04-10 17:15:12 -0700997 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -0700998}
999
Tej Singh2c9ef2a2019-01-22 11:33:51 -08001000Status StatsService::setActiveConfigsChangedOperation(const sp<android::IBinder>& intentSender,
1001 const String16& packageName,
1002 vector<int64_t>* output) {
1003 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1004
1005 IPCThreadState* ipc = IPCThreadState::self();
1006 mConfigManager->SetActiveConfigsChangedReceiver(ipc->getCallingUid(), intentSender);
1007 //TODO: Return the list of configs that are already active
1008 return Status::ok();
1009}
1010
1011Status StatsService::removeActiveConfigsChangedOperation(const String16& packageName) {
1012 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1013
1014 IPCThreadState* ipc = IPCThreadState::self();
1015 mConfigManager->RemoveActiveConfigsChangedReceiver(ipc->getCallingUid());
1016 return Status::ok();
1017}
1018
Jeff Sharkey6b649252018-04-16 09:50:22 -06001019Status StatsService::removeConfiguration(int64_t key, const String16& packageName) {
1020 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1021
Bookatz4f716292018-04-10 17:15:12 -07001022 IPCThreadState* ipc = IPCThreadState::self();
1023 ConfigKey configKey(ipc->getCallingUid(), key);
1024 mConfigManager->RemoveConfig(configKey);
1025 SubscriberReporter::getInstance().removeConfig(configKey);
1026 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -07001027}
1028
Bookatzc6977972018-01-16 16:55:05 -08001029Status StatsService::setBroadcastSubscriber(int64_t configId,
1030 int64_t subscriberId,
Jeff Sharkey6b649252018-04-16 09:50:22 -06001031 const sp<android::IBinder>& intentSender,
1032 const String16& packageName) {
1033 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1034
Bookatzc6977972018-01-16 16:55:05 -08001035 VLOG("StatsService::setBroadcastSubscriber called.");
Bookatz4f716292018-04-10 17:15:12 -07001036 IPCThreadState* ipc = IPCThreadState::self();
1037 ConfigKey configKey(ipc->getCallingUid(), configId);
1038 SubscriberReporter::getInstance()
1039 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
1040 return Status::ok();
Bookatzc6977972018-01-16 16:55:05 -08001041}
1042
1043Status StatsService::unsetBroadcastSubscriber(int64_t configId,
Jeff Sharkey6b649252018-04-16 09:50:22 -06001044 int64_t subscriberId,
1045 const String16& packageName) {
1046 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1047
Bookatzc6977972018-01-16 16:55:05 -08001048 VLOG("StatsService::unsetBroadcastSubscriber called.");
Bookatz4f716292018-04-10 17:15:12 -07001049 IPCThreadState* ipc = IPCThreadState::self();
1050 ConfigKey configKey(ipc->getCallingUid(), configId);
1051 SubscriberReporter::getInstance()
1052 .unsetBroadcastSubscriber(configKey, subscriberId);
1053 return Status::ok();
Bookatzc6977972018-01-16 16:55:05 -08001054}
1055
yrobe6d7f92018-05-04 13:02:53 -07001056Status StatsService::sendAppBreadcrumbAtom(int32_t label, int32_t state) {
1057 // Permission check not necessary as it's meant for applications to write to
1058 // statsd.
1059 android::util::stats_write(util::APP_BREADCRUMB_REPORTED,
1060 IPCThreadState::self()->getCallingUid(), label,
1061 state);
1062 return Status::ok();
1063}
1064
Tej Singha0c89dd2019-01-25 16:39:18 -08001065Status StatsService::registerPullerCallback(int32_t atomTag,
1066 const sp<android::os::IStatsPullerCallback>& pullerCallback,
1067 const String16& packageName) {
1068 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1069
1070 VLOG("StatsService::registerPullerCallback called.");
1071 mPullerManager->RegisterPullerCallback(atomTag, pullerCallback);
1072 return Status::ok();
1073}
1074
1075Status StatsService::unregisterPullerCallback(int32_t atomTag, const String16& packageName) {
1076 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1077
1078 VLOG("StatsService::unregisterPullerCallback called.");
1079 mPullerManager->UnregisterPullerCallback(atomTag);
1080 return Status::ok();
1081}
1082
Chenjie Yu6b1667c2019-01-18 10:09:33 -08001083Status StatsService::sendBinaryPushStateChangedAtom(const android::String16& trainName,
1084 int64_t trainVersionCode, int options,
1085 int32_t state,
1086 const std::vector<int64_t>& experimentIds) {
1087 uid_t uid = IPCThreadState::self()->getCallingUid();
1088 // For testing
1089 if (uid == AID_ROOT || uid == AID_SYSTEM || uid == AID_SHELL) {
1090 return ok();
1091 }
1092
1093 // Caller must be granted these permissions
1094 if (!checkCallingPermission(String16(kPermissionDump))) {
1095 return exception(binder::Status::EX_SECURITY,
1096 StringPrintf("UID %d lacks permission %s", uid, kPermissionDump));
1097 }
1098 if (!checkCallingPermission(String16(kPermissionUsage))) {
1099 return exception(binder::Status::EX_SECURITY,
1100 StringPrintf("UID %d lacks permission %s", uid, kPermissionUsage));
1101 }
1102 // TODO: add verifier permission
1103
1104 userid_t userId = multiuser_get_user_id(uid);
1105
1106 bool requiresStaging = options | IStatsManager::FLAG_REQUIRE_STAGING;
1107 bool rollbackEnabled = options | IStatsManager::FLAG_ROLLBACK_ENABLED;
1108 bool requiresLowLatencyMonitor = options | IStatsManager::FLAG_REQUIRE_LOW_LATENCY_MONITOR;
1109
1110 ProtoOutputStream proto;
1111 uint64_t protoToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_EXPERIMENT_ID_MSG);
1112 for (const auto& expId : experimentIds) {
1113 proto.write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_EXPERIMENT_ID,
1114 (long long)expId);
1115 }
1116 proto.end(protoToken);
1117
1118 vector<uint8_t> buffer;
1119 buffer.resize(proto.size());
1120 size_t pos = 0;
1121 auto iter = proto.data();
1122 while (iter.readBuffer() != NULL) {
1123 size_t toRead = iter.currentToRead();
1124 std::memcpy(&(buffer[pos]), iter.readBuffer(), toRead);
1125 pos += toRead;
1126 iter.rp()->move(toRead);
1127 }
1128 LogEvent event(std::string(String8(trainName).string()), trainVersionCode, requiresStaging,
1129 rollbackEnabled, requiresLowLatencyMonitor, state, buffer, userId);
1130 mProcessor->OnLogEvent(&event);
1131 StorageManager::writeTrainInfo(trainVersionCode, buffer);
1132 return Status::ok();
1133}
1134
Howard Roa46b6582018-09-18 16:45:02 -07001135hardware::Return<void> StatsService::reportSpeakerImpedance(
1136 const SpeakerImpedance& speakerImpedance) {
1137 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), speakerImpedance);
1138 mProcessor->OnLogEvent(&event);
1139
1140 return hardware::Void();
1141}
1142
1143hardware::Return<void> StatsService::reportHardwareFailed(const HardwareFailed& hardwareFailed) {
1144 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), hardwareFailed);
1145 mProcessor->OnLogEvent(&event);
1146
1147 return hardware::Void();
1148}
1149
1150hardware::Return<void> StatsService::reportPhysicalDropDetected(
1151 const PhysicalDropDetected& physicalDropDetected) {
1152 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), physicalDropDetected);
1153 mProcessor->OnLogEvent(&event);
1154
1155 return hardware::Void();
1156}
1157
1158hardware::Return<void> StatsService::reportChargeCycles(const ChargeCycles& chargeCycles) {
1159 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), chargeCycles);
1160 mProcessor->OnLogEvent(&event);
1161
1162 return hardware::Void();
1163}
1164
1165hardware::Return<void> StatsService::reportBatteryHealthSnapshot(
1166 const BatteryHealthSnapshotArgs& batteryHealthSnapshotArgs) {
1167 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(),
1168 batteryHealthSnapshotArgs);
1169 mProcessor->OnLogEvent(&event);
1170
1171 return hardware::Void();
1172}
1173
1174hardware::Return<void> StatsService::reportSlowIo(const SlowIo& slowIo) {
1175 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), slowIo);
1176 mProcessor->OnLogEvent(&event);
1177
1178 return hardware::Void();
1179}
1180
1181hardware::Return<void> StatsService::reportBatteryCausedShutdown(
1182 const BatteryCausedShutdown& batteryCausedShutdown) {
1183 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), batteryCausedShutdown);
1184 mProcessor->OnLogEvent(&event);
1185
1186 return hardware::Void();
1187}
1188
Maggie Whitefc1aa592018-11-28 21:55:23 -08001189hardware::Return<void> StatsService::reportUsbPortOverheatEvent(
1190 const UsbPortOverheatEvent& usbPortOverheatEvent) {
1191 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), usbPortOverheatEvent);
1192 mProcessor->OnLogEvent(&event);
1193
1194 return hardware::Void();
1195}
1196
Carter Hsub8fd1e92019-01-11 15:24:45 +08001197hardware::Return<void> StatsService::reportSpeechDspStat(
1198 const SpeechDspStat& speechDspStat) {
1199 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), speechDspStat);
1200 mProcessor->OnLogEvent(&event);
1201
1202 return hardware::Void();
1203}
1204
Maggie White58174da2019-01-18 15:23:35 -08001205hardware::Return<void> StatsService::reportVendorAtom(const VendorAtom& vendorAtom) {
1206 std::string reverseDomainName = (std::string) vendorAtom.reverseDomainName;
1207 if (vendorAtom.atomId < 100000 || vendorAtom.atomId >= 200000) {
1208 ALOGE("Atom ID %ld is not a valid vendor atom ID", (long) vendorAtom.atomId);
1209 return hardware::Void();
1210 }
1211 if (reverseDomainName.length() > 50) {
1212 ALOGE("Vendor atom reverse domain name %s is too long.", reverseDomainName.c_str());
1213 return hardware::Void();
1214 }
1215 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), vendorAtom);
1216 mProcessor->OnLogEvent(&event);
1217
1218 return hardware::Void();
1219}
1220
David Chen1d7b0cd2017-11-15 14:20:04 -08001221void StatsService::binderDied(const wp <IBinder>& who) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -07001222 ALOGW("statscompanion service died");
Yangster-mac892f3d32018-05-02 14:16:48 -07001223 StatsdStats::getInstance().noteSystemServerRestart(getWallClockSec());
1224 if (mProcessor != nullptr) {
Howard Roe60992b2018-08-30 14:37:29 -07001225 ALOGW("Reset statsd upon system server restarts.");
Yangster-mac892f3d32018-05-02 14:16:48 -07001226 mProcessor->WriteDataToDisk(STATSCOMPANION_DIED);
1227 mProcessor->resetConfigs();
1228 }
Chenjie Yuaa5b2012018-03-21 13:53:15 -07001229 mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
1230 mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
1231 SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
Chenjie Yue2219202018-06-08 10:07:51 -07001232 mPullerManager->SetStatsCompanionService(nullptr);
yro31eb67b2017-10-24 13:33:21 -07001233}
1234
Yao Chenef99c4f2017-09-22 16:26:54 -07001235} // namespace statsd
1236} // namespace os
1237} // namespace android