blob: b478fed49a54c02011b348a90684d369ce6ab540 [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>
yro87d983c2017-11-14 21:31:43 -080034#include <dirent.h>
David Chen0656b7a2017-09-13 15:53:39 -070035#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070036#include <private/android_filesystem_config.h>
Bookatzb223c4e2018-02-01 15:35:04 -080037#include <statslog.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070038#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070039#include <stdlib.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070040#include <sys/system_properties.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070041#include <unistd.h>
Yao Chena80e5c02018-09-04 13:55:29 -070042#include <utils/Looper.h>
43#include <utils/String16.h>
44#include <chrono>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070045
46using namespace android;
47
Jeff Sharkey6b649252018-04-16 09:50:22 -060048using android::base::StringPrintf;
Bookatzff71cad2018-09-20 17:17:49 -070049using android::util::FIELD_COUNT_REPEATED;
50using android::util::FIELD_TYPE_MESSAGE;
Jeff Sharkey6b649252018-04-16 09:50:22 -060051
Bookatz906a35c2017-09-20 15:26:44 -070052namespace android {
53namespace os {
54namespace statsd {
55
David Chenadaf8b32017-11-03 15:42:08 -070056constexpr const char* kPermissionDump = "android.permission.DUMP";
Jeff Sharkey6b649252018-04-16 09:50:22 -060057constexpr const char* kPermissionUsage = "android.permission.PACKAGE_USAGE_STATS";
58
59constexpr const char* kOpUsage = "android:get_usage_stats";
60
yro03faf092017-12-12 00:17:50 -080061#define STATS_SERVICE_DIR "/data/misc/stats-service"
David Chenadaf8b32017-11-03 15:42:08 -070062
Bookatzff71cad2018-09-20 17:17:49 -070063// for StatsDataDumpProto
64const int FIELD_ID_REPORTS_LIST = 1;
65
Jeff Sharkey6b649252018-04-16 09:50:22 -060066static binder::Status ok() {
67 return binder::Status::ok();
68}
69
70static binder::Status exception(uint32_t code, const std::string& msg) {
71 ALOGE("%s (%d)", msg.c_str(), code);
72 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
73}
74
75binder::Status checkUid(uid_t expectedUid) {
76 uid_t uid = IPCThreadState::self()->getCallingUid();
77 if (uid == expectedUid || uid == AID_ROOT) {
78 return ok();
79 } else {
80 return exception(binder::Status::EX_SECURITY,
81 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
82 }
83}
84
85binder::Status checkDumpAndUsageStats(const String16& packageName) {
86 pid_t pid = IPCThreadState::self()->getCallingPid();
87 uid_t uid = IPCThreadState::self()->getCallingUid();
88
89 // Root, system, and shell always have access
90 if (uid == AID_ROOT || uid == AID_SYSTEM || uid == AID_SHELL) {
91 return ok();
92 }
93
94 // Caller must be granted these permissions
95 if (!checkCallingPermission(String16(kPermissionDump))) {
96 return exception(binder::Status::EX_SECURITY,
97 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, kPermissionDump));
98 }
99 if (!checkCallingPermission(String16(kPermissionUsage))) {
100 return exception(binder::Status::EX_SECURITY,
101 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, kPermissionUsage));
102 }
103
104 // Caller must also have usage stats op granted
105 PermissionController pc;
106 switch (pc.noteOp(String16(kOpUsage), uid, packageName)) {
107 case PermissionController::MODE_ALLOWED:
108 case PermissionController::MODE_DEFAULT:
109 return ok();
110 default:
111 return exception(binder::Status::EX_SECURITY,
112 StringPrintf("UID %d / PID %d lacks app-op %s", uid, pid, kOpUsage));
113 }
114}
115
116#define ENFORCE_UID(uid) { \
117 binder::Status status = checkUid((uid)); \
118 if (!status.isOk()) { \
119 return status; \
120 } \
121}
122
123#define ENFORCE_DUMP_AND_USAGE_STATS(packageName) { \
124 binder::Status status = checkDumpAndUsageStats(packageName); \
125 if (!status.isOk()) { \
126 return status; \
127 } \
128}
129
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700130StatsService::StatsService(const sp<Looper>& handlerLooper)
Yangster-mac932ecec2018-02-01 10:23:52 -0800131 : mAnomalyAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
132 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
133 if (sc != nullptr) {
134 sc->setAnomalyAlarm(timeMillis);
135 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
136 }
137 },
138 [](const sp<IStatsCompanionService>& sc) {
139 if (sc != nullptr) {
140 sc->cancelAnomalyAlarm();
141 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
142 }
143 })),
144 mPeriodicAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
145 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
146 if (sc != nullptr) {
147 sc->setAlarmForSubscriberTriggering(timeMillis);
148 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
149 }
150 },
151 [](const sp<IStatsCompanionService>& sc) {
152 if (sc != nullptr) {
153 sc->cancelAlarmForSubscriberTriggering();
154 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
155 }
156
157 })) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700158 mUidMap = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700159 mPullerManager = new StatsPullerManager();
Chenjie Yu80f91122018-01-31 20:24:50 -0800160 StatsPuller::SetUidMap(mUidMap);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700161 mConfigManager = new ConfigManager();
Chenjie Yue2219202018-06-08 10:07:51 -0700162 mProcessor = new StatsLogProcessor(
163 mUidMap, mPullerManager, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor,
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800164 getElapsedRealtimeNs(),
165 [this](const ConfigKey& key) {
Chenjie Yue2219202018-06-08 10:07:51 -0700166 sp<IStatsCompanionService> sc = getStatsCompanionService();
167 auto receiver = mConfigManager->GetConfigReceiver(key);
168 if (sc == nullptr) {
169 VLOG("Could not find StatsCompanionService");
170 return false;
171 } else if (receiver == nullptr) {
172 VLOG("Statscompanion could not find a broadcast receiver for %s",
173 key.ToString().c_str());
174 return false;
175 } else {
176 sc->sendDataBroadcast(receiver, mProcessor->getLastReportTimeNs(key));
177 return true;
178 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800179 },
180 [this](const int& uid, const vector<int64_t>& activeConfigs) {
181 auto receiver = mConfigManager->GetActiveConfigsChangedReceiver(uid);
182 sp<IStatsCompanionService> sc = getStatsCompanionService();
183 if (sc == nullptr) {
184 VLOG("Could not access statsCompanion");
185 return false;
186 } else if (receiver == nullptr) {
187 VLOG("Could not find receiver for uid %d", uid);
188 return false;
189 } else {
190 sc->sendActiveConfigsChangedBroadcast(receiver, activeConfigs);
191 VLOG("StatsService::active configs broadcast succeeded for uid %d" , uid);
192 return true;
193 }
Chenjie Yue2219202018-06-08 10:07:51 -0700194 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700195
196 mConfigManager->AddListener(mProcessor);
197
198 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700199}
200
Yao Chenef99c4f2017-09-22 16:26:54 -0700201StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700202}
203
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700204void StatsService::init_system_properties() {
205 mEngBuild = false;
206 const prop_info* buildType = __system_property_find("ro.build.type");
207 if (buildType != NULL) {
208 __system_property_read_callback(buildType, init_build_type_callback, this);
209 }
David Chen0656b7a2017-09-13 15:53:39 -0700210}
211
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700212void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
213 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700214 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700215 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
216 }
217}
218
219/**
220 * Implement our own because the default binder implementation isn't
221 * properly handling SHELL_COMMAND_TRANSACTION.
222 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700223status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
224 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700225 switch (code) {
226 case SHELL_COMMAND_TRANSACTION: {
227 int in = data.readFileDescriptor();
228 int out = data.readFileDescriptor();
229 int err = data.readFileDescriptor();
230 int argc = data.readInt32();
231 Vector<String8> args;
232 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
233 args.add(String8(data.readString16()));
234 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700235 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
236 sp<IResultReceiver> resultReceiver =
237 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700238
Yao Chena80e5c02018-09-04 13:55:29 -0700239 err = command(in, out, err, args, resultReceiver);
240 resultReceiver->send(err);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700241 return NO_ERROR;
242 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700243 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700244 }
245}
246
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700247/**
Bookatzff71cad2018-09-20 17:17:49 -0700248 * Write data from statsd.
249 * Format for statsdStats: adb shell dumpsys stats --metadata [-v] [--proto]
250 * Format for data report: adb shell dumpsys stats [anything other than --metadata] [--proto]
251 * Anything ending in --proto will be in proto format.
252 * Anything without --metadata as the first argument will be report information.
253 * (bugreports call "adb shell dumpsys stats --dump-priority NORMAL -a --proto")
254 * TODO: Come up with a more robust method of enacting <serviceutils/PriorityDumper.h>.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700255 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700256status_t StatsService::dump(int fd, const Vector<String16>& args) {
Tej Singhdd83d702018-04-10 17:24:50 -0700257 if (!checkCallingPermission(String16(kPermissionDump))) {
258 return PERMISSION_DENIED;
259 }
Bookatzff71cad2018-09-20 17:17:49 -0700260 int lastArg = args.size() - 1;
261 bool asProto = false;
262 if (lastArg >= 0 && !args[lastArg].compare(String16("--proto"))) { // last argument
263 asProto = true;
264 lastArg--;
Yao Chen884c8c12018-01-26 10:36:25 -0800265 }
Bookatzff71cad2018-09-20 17:17:49 -0700266 if (args.size() > 0 && !args[0].compare(String16("--metadata"))) { // first argument
267 // Request is to dump statsd stats.
268 bool verbose = false;
269 if (lastArg >= 0 && !args[lastArg].compare(String16("-v"))) {
270 verbose = true;
271 lastArg--;
272 }
273 dumpStatsdStats(fd, verbose, asProto);
274 } else {
275 // Request is to dump statsd report data.
276 if (asProto) {
277 dumpIncidentSection(fd);
278 } else {
279 dprintf(fd, "Non-proto format of stats data dump not available; see proto version.\n");
280 }
Tej Singh41b3f9a2018-04-03 17:06:35 -0700281 }
Yao Chen884c8c12018-01-26 10:36:25 -0800282
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700283 return NO_ERROR;
284}
285
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700286/**
Tej Singh41b3f9a2018-04-03 17:06:35 -0700287 * Write debugging data about statsd in text or proto format.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700288 */
Bookatzff71cad2018-09-20 17:17:49 -0700289void StatsService::dumpStatsdStats(int out, bool verbose, bool proto) {
Tej Singh41b3f9a2018-04-03 17:06:35 -0700290 if (proto) {
291 vector<uint8_t> data;
292 StatsdStats::getInstance().dumpStats(&data, false); // does not reset statsdStats.
293 for (size_t i = 0; i < data.size(); i ++) {
Yao Chena80e5c02018-09-04 13:55:29 -0700294 dprintf(out, "%c", data[i]);
Tej Singh41b3f9a2018-04-03 17:06:35 -0700295 }
296 } else {
297 StatsdStats::getInstance().dumpStats(out);
298 mProcessor->dumpStates(out, verbose);
299 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700300}
301
302/**
Bookatzff71cad2018-09-20 17:17:49 -0700303 * Write stats report data in StatsDataDumpProto incident section format.
304 */
305void StatsService::dumpIncidentSection(int out) {
306 ProtoOutputStream proto;
307 for (const ConfigKey& configKey : mConfigManager->GetAllConfigKeys()) {
308 uint64_t reportsListToken =
309 proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS_LIST);
310 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(),
311 true /* includeCurrentBucket */, false /* erase_data */,
312 ADB_DUMP, &proto);
313 proto.end(reportsListToken);
314 proto.flush(out);
Bookatzc71d9012018-12-19 12:28:38 -0800315 proto.clear();
Bookatzff71cad2018-09-20 17:17:49 -0700316 }
317}
318
319/**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700320 * Implementation of the adb shell cmd stats command.
321 */
Yao Chena80e5c02018-09-04 13:55:29 -0700322status_t StatsService::command(int in, int out, int err, Vector<String8>& args,
323 sp<IResultReceiver> resultReceiver) {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600324 uid_t uid = IPCThreadState::self()->getCallingUid();
325 if (uid != AID_ROOT && uid != AID_SHELL) {
326 return PERMISSION_DENIED;
327 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700328
329 const int argCount = args.size();
330 if (argCount >= 1) {
331 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700332 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700333 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700334 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700335
David Chende701692017-10-05 13:16:02 -0700336 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800337 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700338 }
Yao Chen729093d2017-10-16 10:33:26 -0700339
340 if (!args[0].compare(String8("dump-report"))) {
Bookatzff71cad2018-09-20 17:17:49 -0700341 return cmd_dump_report(out, args);
Yao Chen729093d2017-10-16 10:33:26 -0700342 }
David Chen1481fe12017-10-16 13:16:34 -0700343
344 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
345 return cmd_print_pulled_metrics(out, args);
346 }
David Chenadaf8b32017-11-03 15:42:08 -0700347
348 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800349 return cmd_trigger_broadcast(out, args);
350 }
351
352 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800353 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700354 }
yro87d983c2017-11-14 21:31:43 -0800355
Yao Chen8d9989b2017-11-18 18:54:50 -0800356 if (!args[0].compare(String8("meminfo"))) {
357 return cmd_dump_memory_info(out);
358 }
yro947fbce2017-11-15 22:50:23 -0800359
360 if (!args[0].compare(String8("write-to-disk"))) {
361 return cmd_write_data_to_disk(out);
362 }
Bookatzb223c4e2018-02-01 15:35:04 -0800363
David Chen0b5c90c2018-01-25 16:51:49 -0800364 if (!args[0].compare(String8("log-app-breadcrumb"))) {
365 return cmd_log_app_breadcrumb(out, args);
Bookatzb223c4e2018-02-01 15:35:04 -0800366 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800367
368 if (!args[0].compare(String8("clear-puller-cache"))) {
369 return cmd_clear_puller_cache(out);
370 }
Yao Chen876889c2018-05-02 11:16:16 -0700371
372 if (!args[0].compare(String8("print-logs"))) {
373 return cmd_print_logs(out, args);
374 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800375 if (!args[0].compare(String8("send-active-configs"))) {
376 return cmd_trigger_active_config_broadcast(out, args);
377 }
Yao Chena80e5c02018-09-04 13:55:29 -0700378 if (!args[0].compare(String8("data-subscribe"))) {
379 if (mShellSubscriber == nullptr) {
Yao Chen41e606c2018-10-05 15:54:11 -0700380 mShellSubscriber = new ShellSubscriber(mUidMap, mPullerManager);
Yao Chena80e5c02018-09-04 13:55:29 -0700381 }
Yao Chen35cb8d62019-01-03 16:49:14 -0800382 int timeoutSec = -1;
383 if (argCount >= 2) {
384 timeoutSec = atoi(args[1].c_str());
385 }
386 mShellSubscriber->startNewSubscription(in, out, resultReceiver, timeoutSec);
Yao Chena80e5c02018-09-04 13:55:29 -0700387 return NO_ERROR;
388 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700389 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700390
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700391 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700392 return NO_ERROR;
393}
394
Yao Chena80e5c02018-09-04 13:55:29 -0700395void StatsService::print_cmd_help(int out) {
396 dprintf(out,
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700397 "usage: adb shell cmd stats print-stats-log [tag_required] "
398 "[timestamp_nsec_optional]\n");
Yao Chena80e5c02018-09-04 13:55:29 -0700399 dprintf(out, "\n");
400 dprintf(out, "\n");
401 dprintf(out, "usage: adb shell cmd stats meminfo\n");
402 dprintf(out, "\n");
403 dprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
404 dprintf(out, " # adb shell stop\n");
405 dprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
406 dprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
407 dprintf(out, " # adb shell start\n");
408 dprintf(out, "\n");
409 dprintf(out, "\n");
410 dprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
411 dprintf(out, "\n");
412 dprintf(out, " Prints the UID, app name, version mapping.\n");
413 dprintf(out, " PKG Optional package name to print the uids of the package\n");
414 dprintf(out, "\n");
415 dprintf(out, "\n");
416 dprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
417 dprintf(out, "\n");
418 dprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
419 dprintf(out, "\n");
420 dprintf(out, "\n");
421 dprintf(out, "usage: adb shell cmd stats write-to-disk \n");
422 dprintf(out, "\n");
423 dprintf(out, " Flushes all data on memory to disk.\n");
424 dprintf(out, "\n");
425 dprintf(out, "\n");
426 dprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n");
427 dprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n");
428 dprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
429 dprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
430 dprintf(out, " uid is used.\n");
431 dprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
432 dprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
433 dprintf(out, "\n");
434 dprintf(out, "\n");
435 dprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
436 dprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
437 dprintf(out, "\n");
438 dprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
439 dprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
440 dprintf(out, " provided, then all configs will be removed from memory and disk.\n");
441 dprintf(out, "\n");
442 dprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
443 dprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
444 dprintf(out, " uid is used.\n");
445 dprintf(out, " NAME The per-uid name to use\n");
446 dprintf(out, "\n");
447 dprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
448 dprintf(out, "\n be removed from memory and disk!\n");
449 dprintf(out, "\n");
450 dprintf(out,
Bookatz3e906582018-12-10 17:26:58 -0800451 "usage: adb shell cmd stats dump-report [UID] NAME [--keep_data] "
452 "[--include_current_bucket] [--proto]\n");
Yao Chena80e5c02018-09-04 13:55:29 -0700453 dprintf(out, " Dump all metric data for a configuration.\n");
454 dprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
455 dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
456 dprintf(out, " calling uid is used.\n");
457 dprintf(out, " NAME The name of the configuration\n");
Bookatz3e906582018-12-10 17:26:58 -0800458 dprintf(out, " --keep_data Do NOT erase the data upon dumping it.\n");
Yao Chena80e5c02018-09-04 13:55:29 -0700459 dprintf(out, " --proto Print proto binary.\n");
460 dprintf(out, "\n");
461 dprintf(out, "\n");
462 dprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
463 dprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
464 dprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
465 dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
466 dprintf(out, " calling uid is used.\n");
467 dprintf(out, " NAME The name of the configuration\n");
468 dprintf(out, "\n");
469 dprintf(out, "\n");
Tej Singh6ede28b2019-01-29 17:06:54 -0800470 dprintf(out,
471 "usage: adb shell cmd stats send-active-configs [--uid=UID] [--configs] "
472 "[NAME1] [NAME2] [NAME3..]\n");
473 dprintf(out, " Send a broadcast that informs the subscriber of the current active configs.\n");
474 dprintf(out, " --uid=UID The uid of the configurations. It is only possible to pass\n");
475 dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
476 dprintf(out, " calling uid is used.\n");
477 dprintf(out, " --configs Send the list of configs in the name list instead of\n");
478 dprintf(out, " the currently active configs\n");
479 dprintf(out, " NAME LIST List of configuration names to be included in the broadcast.\n");
480
481 dprintf(out, "\n");
482 dprintf(out, "\n");
Yao Chena80e5c02018-09-04 13:55:29 -0700483 dprintf(out, "usage: adb shell cmd stats print-stats\n");
484 dprintf(out, " Prints some basic stats.\n");
485 dprintf(out, " --proto Print proto binary instead of string format.\n");
486 dprintf(out, "\n");
487 dprintf(out, "\n");
488 dprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
489 dprintf(out, " Clear cached puller data.\n");
490 dprintf(out, "\n");
491 dprintf(out, "usage: adb shell cmd stats print-logs\n");
492 dprintf(out, " Only works on eng build\n");
David Chenadaf8b32017-11-03 15:42:08 -0700493}
494
Yao Chena80e5c02018-09-04 13:55:29 -0700495status_t StatsService::cmd_trigger_broadcast(int out, Vector<String8>& args) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800496 string name;
497 bool good = false;
498 int uid;
499 const int argCount = args.size();
500 if (argCount == 2) {
501 // Automatically pick the UID
502 uid = IPCThreadState::self()->getCallingUid();
David Chen1d7b0cd2017-11-15 14:20:04 -0800503 name.assign(args[1].c_str(), args[1].size());
504 good = true;
505 } else if (argCount == 3) {
Bookatzd2386572018-12-14 15:53:14 -0800506 good = getUidFromArgs(args, 1, uid);
507 if (!good) {
508 dprintf(out, "Invalid UID. Note that the metrics can only be dumped for "
509 "other UIDs on eng or userdebug builds.\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800510 }
Bookatzd2386572018-12-14 15:53:14 -0800511 name.assign(args[2].c_str(), args[2].size());
David Chen1d7b0cd2017-11-15 14:20:04 -0800512 }
513 if (!good) {
514 print_cmd_help(out);
515 return UNKNOWN_ERROR;
516 }
David Chend37bc232018-04-12 18:05:11 -0700517 ConfigKey key(uid, StrToInt64(name));
518 auto receiver = mConfigManager->GetConfigReceiver(key);
yro4d889e62017-11-17 15:44:48 -0800519 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen661f7912018-01-22 17:46:24 -0800520 if (sc == nullptr) {
521 VLOG("Could not access statsCompanion");
522 } else if (receiver == nullptr) {
523 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str())
524 } else {
David Chend37bc232018-04-12 18:05:11 -0700525 sc->sendDataBroadcast(receiver, mProcessor->getLastReportTimeNs(key));
yro74fed972017-11-27 14:42:42 -0800526 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
527 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800528 }
529
David Chenadaf8b32017-11-03 15:42:08 -0700530 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700531}
532
Tej Singh6ede28b2019-01-29 17:06:54 -0800533status_t StatsService::cmd_trigger_active_config_broadcast(int out, Vector<String8>& args) {
534 const int argCount = args.size();
535 int uid;
536 vector<int64_t> configIds;
537 if (argCount == 1) {
538 // Automatically pick the uid and send a broadcast that has no active configs.
539 uid = IPCThreadState::self()->getCallingUid();
540 mProcessor->GetActiveConfigs(uid, configIds);
541 } else {
542 int curArg = 1;
543 if(args[curArg].find("--uid=") == 0) {
544 string uidArgStr(args[curArg].c_str());
545 string uidStr = uidArgStr.substr(6);
546 if (!getUidFromString(uidStr.c_str(), uid)) {
547 dprintf(out, "Invalid UID. Note that the config can only be set for "
548 "other UIDs on eng or userdebug builds.\n");
549 return UNKNOWN_ERROR;
550 }
551 curArg++;
552 } else {
553 uid = IPCThreadState::self()->getCallingUid();
554 }
555 if (curArg == argCount || args[curArg] != "--configs") {
556 VLOG("Reached end of args, or specify configs not set. Sending actual active configs,");
557 mProcessor->GetActiveConfigs(uid, configIds);
558 } else {
559 // Flag specified, use the given list of configs.
560 curArg++;
561 for (int i = curArg; i < argCount; i++) {
562 char* endp;
563 int64_t configID = strtoll(args[i].c_str(), &endp, 10);
564 if (endp == args[i].c_str() || *endp != '\0') {
565 dprintf(out, "Error parsing config ID.\n");
566 return UNKNOWN_ERROR;
567 }
568 VLOG("Adding config id %ld", static_cast<long>(configID));
569 configIds.push_back(configID);
570 }
571 }
572 }
573 auto receiver = mConfigManager->GetActiveConfigsChangedReceiver(uid);
574 sp<IStatsCompanionService> sc = getStatsCompanionService();
575 if (sc == nullptr) {
576 VLOG("Could not access statsCompanion");
577 } else if (receiver == nullptr) {
578 VLOG("Could not find receiver for uid %d", uid);
579 } else {
580 sc->sendActiveConfigsChangedBroadcast(receiver, configIds);
581 VLOG("StatsService::trigger active configs changed broadcast succeeded for uid %d" , uid);
582 }
583 return NO_ERROR;
584}
585
Yao Chena80e5c02018-09-04 13:55:29 -0700586status_t StatsService::cmd_config(int in, int out, int err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700587 const int argCount = args.size();
588 if (argCount >= 2) {
589 if (args[1] == "update" || args[1] == "remove") {
590 bool good = false;
591 int uid = -1;
592 string name;
593
594 if (argCount == 3) {
595 // Automatically pick the UID
596 uid = IPCThreadState::self()->getCallingUid();
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700597 name.assign(args[2].c_str(), args[2].size());
598 good = true;
599 } else if (argCount == 4) {
Bookatzd2386572018-12-14 15:53:14 -0800600 good = getUidFromArgs(args, 2, uid);
601 if (!good) {
602 dprintf(err, "Invalid UID. Note that the config can only be set for "
603 "other UIDs on eng or userdebug builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700604 }
Bookatzd2386572018-12-14 15:53:14 -0800605 name.assign(args[3].c_str(), args[3].size());
yroe5f82922018-01-22 18:37:27 -0800606 } else if (argCount == 2 && args[1] == "remove") {
607 good = true;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700608 }
609
610 if (!good) {
611 // If arg parsing failed, print the help text and return an error.
612 print_cmd_help(out);
613 return UNKNOWN_ERROR;
614 }
615
616 if (args[1] == "update") {
yro255f72e2018-02-26 15:15:17 -0800617 char* endp;
618 int64_t configID = strtoll(name.c_str(), &endp, 10);
619 if (endp == name.c_str() || *endp != '\0') {
Yao Chena80e5c02018-09-04 13:55:29 -0700620 dprintf(err, "Error parsing config ID.\n");
yro255f72e2018-02-26 15:15:17 -0800621 return UNKNOWN_ERROR;
622 }
623
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700624 // Read stream into buffer.
625 string buffer;
Yao Chena80e5c02018-09-04 13:55:29 -0700626 if (!android::base::ReadFdToString(in, &buffer)) {
627 dprintf(err, "Error reading stream for StatsConfig.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700628 return UNKNOWN_ERROR;
629 }
630
631 // Parse buffer.
632 StatsdConfig config;
633 if (!config.ParseFromString(buffer)) {
Yao Chena80e5c02018-09-04 13:55:29 -0700634 dprintf(err, "Error parsing proto stream for StatsConfig.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700635 return UNKNOWN_ERROR;
636 }
637
638 // Add / update the config.
yro255f72e2018-02-26 15:15:17 -0800639 mConfigManager->UpdateConfig(ConfigKey(uid, configID), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700640 } else {
yro74fed972017-11-27 14:42:42 -0800641 if (argCount == 2) {
642 cmd_remove_all_configs(out);
643 } else {
644 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800645 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800646 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700647 }
648
649 return NO_ERROR;
650 }
David Chen0656b7a2017-09-13 15:53:39 -0700651 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700652 print_cmd_help(out);
653 return UNKNOWN_ERROR;
654}
655
Bookatzff71cad2018-09-20 17:17:49 -0700656status_t StatsService::cmd_dump_report(int out, const Vector<String8>& args) {
Yao Chen729093d2017-10-16 10:33:26 -0700657 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800658 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700659 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800660 bool proto = false;
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700661 bool includeCurrentBucket = false;
Bookatz3e906582018-12-10 17:26:58 -0800662 bool eraseData = true;
Yao Chen729093d2017-10-16 10:33:26 -0700663 int uid;
664 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800665 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
666 proto = true;
667 argCount -= 1;
668 }
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700669 if (!std::strcmp("--include_current_bucket", args[argCount-1].c_str())) {
670 includeCurrentBucket = true;
671 argCount -= 1;
672 }
Bookatz3e906582018-12-10 17:26:58 -0800673 if (!std::strcmp("--keep_data", args[argCount-1].c_str())) {
674 eraseData = false;
675 argCount -= 1;
676 }
Yao Chen729093d2017-10-16 10:33:26 -0700677 if (argCount == 2) {
678 // Automatically pick the UID
679 uid = IPCThreadState::self()->getCallingUid();
Yao Chen5154a3792017-10-30 22:57:06 -0700680 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700681 good = true;
682 } else if (argCount == 3) {
Bookatzd2386572018-12-14 15:53:14 -0800683 good = getUidFromArgs(args, 1, uid);
684 if (!good) {
685 dprintf(out, "Invalid UID. Note that the metrics can only be dumped for "
686 "other UIDs on eng or userdebug builds.\n");
Yao Chen729093d2017-10-16 10:33:26 -0700687 }
Bookatzd2386572018-12-14 15:53:14 -0800688 name.assign(args[2].c_str(), args[2].size());
Yao Chen729093d2017-10-16 10:33:26 -0700689 }
690 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800691 vector<uint8_t> data;
David Chen926fc752018-02-23 13:31:43 -0800692 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(),
Bookatz3e906582018-12-10 17:26:58 -0800693 includeCurrentBucket, eraseData, ADB_DUMP, &data);
Chenjie Yub236c862017-11-28 22:20:44 -0800694 if (proto) {
695 for (size_t i = 0; i < data.size(); i ++) {
Yao Chena80e5c02018-09-04 13:55:29 -0700696 dprintf(out, "%c", data[i]);
Chenjie Yub236c862017-11-28 22:20:44 -0800697 }
698 } else {
Bookatzff71cad2018-09-20 17:17:49 -0700699 dprintf(out, "Non-proto stats data dump not currently supported.\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800700 }
Yao Chen729093d2017-10-16 10:33:26 -0700701 return android::OK;
702 } else {
703 // If arg parsing failed, print the help text and return an error.
704 print_cmd_help(out);
705 return UNKNOWN_ERROR;
706 }
707 } else {
Yao Chena80e5c02018-09-04 13:55:29 -0700708 dprintf(out, "Log processor does not exist...\n");
Yao Chen729093d2017-10-16 10:33:26 -0700709 return UNKNOWN_ERROR;
710 }
711}
712
Yao Chena80e5c02018-09-04 13:55:29 -0700713status_t StatsService::cmd_print_stats(int out, const Vector<String8>& args) {
Tej Singh41b3f9a2018-04-03 17:06:35 -0700714 int argCount = args.size();
715 bool proto = false;
716 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
717 proto = true;
718 argCount -= 1;
David Chen1d7b0cd2017-11-15 14:20:04 -0800719 }
Yao Chenb3561512017-11-21 18:07:17 -0800720 StatsdStats& statsdStats = StatsdStats::getInstance();
Tej Singh41b3f9a2018-04-03 17:06:35 -0700721 if (proto) {
722 vector<uint8_t> data;
723 statsdStats.dumpStats(&data, false); // does not reset statsdStats.
724 for (size_t i = 0; i < data.size(); i ++) {
Yao Chena80e5c02018-09-04 13:55:29 -0700725 dprintf(out, "%c", data[i]);
Tej Singh41b3f9a2018-04-03 17:06:35 -0700726 }
727
728 } else {
729 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
730 for (const ConfigKey& key : configs) {
Yao Chena80e5c02018-09-04 13:55:29 -0700731 dprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
Tej Singh41b3f9a2018-04-03 17:06:35 -0700732 mProcessor->GetMetricsSize(key));
733 }
734 statsdStats.dumpStats(out);
735 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800736 return NO_ERROR;
737}
738
Yao Chena80e5c02018-09-04 13:55:29 -0700739status_t StatsService::cmd_print_uid_map(int out, const Vector<String8>& args) {
Yao Chend10f7b12017-12-18 12:53:50 -0800740 if (args.size() > 1) {
741 string pkg;
742 pkg.assign(args[1].c_str(), args[1].size());
743 auto uids = mUidMap->getAppUid(pkg);
Yao Chena80e5c02018-09-04 13:55:29 -0700744 dprintf(out, "%s -> [ ", pkg.c_str());
Yao Chend10f7b12017-12-18 12:53:50 -0800745 for (const auto& uid : uids) {
Yao Chena80e5c02018-09-04 13:55:29 -0700746 dprintf(out, "%d ", uid);
Yao Chend10f7b12017-12-18 12:53:50 -0800747 }
Yao Chena80e5c02018-09-04 13:55:29 -0700748 dprintf(out, "]\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800749 } else {
750 mUidMap->printUidMap(out);
751 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700752 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700753}
754
Yao Chena80e5c02018-09-04 13:55:29 -0700755status_t StatsService::cmd_write_data_to_disk(int out) {
756 dprintf(out, "Writing data to disk\n");
Yangster-mac892f3d32018-05-02 14:16:48 -0700757 mProcessor->WriteDataToDisk(ADB_DUMP);
yro947fbce2017-11-15 22:50:23 -0800758 return NO_ERROR;
759}
760
Yao Chena80e5c02018-09-04 13:55:29 -0700761status_t StatsService::cmd_log_app_breadcrumb(int out, const Vector<String8>& args) {
Bookatzb223c4e2018-02-01 15:35:04 -0800762 bool good = false;
763 int32_t uid;
764 int32_t label;
765 int32_t state;
766 const int argCount = args.size();
767 if (argCount == 3) {
768 // Automatically pick the UID
769 uid = IPCThreadState::self()->getCallingUid();
770 label = atoi(args[1].c_str());
771 state = atoi(args[2].c_str());
772 good = true;
773 } else if (argCount == 4) {
Bookatzd2386572018-12-14 15:53:14 -0800774 good = getUidFromArgs(args, 1, uid);
775 if (!good) {
Yao Chena80e5c02018-09-04 13:55:29 -0700776 dprintf(out,
Bookatzd2386572018-12-14 15:53:14 -0800777 "Invalid UID. Note that selecting a UID for writing AppBreadcrumb can only be "
778 "done for other UIDs on eng or userdebug builds.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800779 }
Bookatzd2386572018-12-14 15:53:14 -0800780 label = atoi(args[2].c_str());
781 state = atoi(args[3].c_str());
Bookatzb223c4e2018-02-01 15:35:04 -0800782 }
783 if (good) {
Yao Chena80e5c02018-09-04 13:55:29 -0700784 dprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state);
David Chen0b5c90c2018-01-25 16:51:49 -0800785 android::util::stats_write(android::util::APP_BREADCRUMB_REPORTED, uid, label, state);
Bookatzb223c4e2018-02-01 15:35:04 -0800786 } else {
787 print_cmd_help(out);
788 return UNKNOWN_ERROR;
789 }
790 return NO_ERROR;
791}
792
Yao Chena80e5c02018-09-04 13:55:29 -0700793status_t StatsService::cmd_print_pulled_metrics(int out, const Vector<String8>& args) {
David Chen1481fe12017-10-16 13:16:34 -0700794 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700795 vector<shared_ptr<LogEvent> > stats;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800796 if (mPullerManager->Pull(s, &stats)) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700797 for (const auto& it : stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700798 dprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700799 }
Yao Chena80e5c02018-09-04 13:55:29 -0700800 dprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700801 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700802 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700803 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700804}
805
Yao Chena80e5c02018-09-04 13:55:29 -0700806status_t StatsService::cmd_remove_all_configs(int out) {
807 dprintf(out, "Removing all configs...\n");
yro74fed972017-11-27 14:42:42 -0800808 VLOG("StatsService::cmd_remove_all_configs was called");
809 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800810 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800811 return NO_ERROR;
812}
813
Yao Chena80e5c02018-09-04 13:55:29 -0700814status_t StatsService::cmd_dump_memory_info(int out) {
815 dprintf(out, "meminfo not available.\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800816 return NO_ERROR;
817}
818
Yao Chena80e5c02018-09-04 13:55:29 -0700819status_t StatsService::cmd_clear_puller_cache(int out) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800820 IPCThreadState* ipc = IPCThreadState::self();
Yangster-mac932ecec2018-02-01 10:23:52 -0800821 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i",
822 ipc->getCallingPid(), ipc->getCallingUid());
Chenjie Yufa22d652018-02-05 14:37:48 -0800823 if (checkCallingPermission(String16(kPermissionDump))) {
Chenjie Yue2219202018-06-08 10:07:51 -0700824 int cleared = mPullerManager->ForceClearPullerCache();
Yao Chena80e5c02018-09-04 13:55:29 -0700825 dprintf(out, "Puller removed %d cached data!\n", cleared);
Chenjie Yufa22d652018-02-05 14:37:48 -0800826 return NO_ERROR;
827 } else {
828 return PERMISSION_DENIED;
829 }
Chenjie Yue72252b2018-02-01 13:19:35 -0800830}
831
Yao Chena80e5c02018-09-04 13:55:29 -0700832status_t StatsService::cmd_print_logs(int out, const Vector<String8>& args) {
Yao Chen876889c2018-05-02 11:16:16 -0700833 IPCThreadState* ipc = IPCThreadState::self();
834 VLOG("StatsService::cmd_print_logs with Pid %i, Uid %i", ipc->getCallingPid(),
835 ipc->getCallingUid());
836 if (checkCallingPermission(String16(kPermissionDump))) {
837 bool enabled = true;
838 if (args.size() >= 2) {
839 enabled = atoi(args[1].c_str()) != 0;
840 }
841 mProcessor->setPrintLogs(enabled);
842 return NO_ERROR;
843 } else {
844 return PERMISSION_DENIED;
845 }
846}
847
Bookatzd2386572018-12-14 15:53:14 -0800848bool StatsService::getUidFromArgs(const Vector<String8>& args, size_t uidArgIndex, int32_t& uid) {
Tej Singh6ede28b2019-01-29 17:06:54 -0800849 return getUidFromString(args[uidArgIndex].c_str(), uid);
850}
851
852bool StatsService::getUidFromString(const char* s, int32_t& uid) {
Bookatzd2386572018-12-14 15:53:14 -0800853 if (*s == '\0') {
854 return false;
855 }
856 char* endc = NULL;
857 int64_t longUid = strtol(s, &endc, 0);
858 if (*endc != '\0') {
859 return false;
860 }
861 int32_t goodUid = static_cast<int32_t>(longUid);
862 if (longUid < 0 || static_cast<uint64_t>(longUid) != static_cast<uid_t>(goodUid)) {
863 return false; // It was not of uid_t type.
864 }
865 uid = goodUid;
866
867 int32_t callingUid = IPCThreadState::self()->getCallingUid();
868 return mEngBuild // UserDebug/EngBuild are allowed to impersonate uids.
869 || (callingUid == goodUid) // Anyone can 'impersonate' themselves.
870 || (callingUid == AID_ROOT && goodUid == AID_SHELL); // ROOT can impersonate SHELL.
871}
872
Dianne Hackborn3accca02013-09-20 09:32:11 -0700873Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
dwchen730403e2018-10-29 11:41:56 -0700874 const vector<String16>& version_string,
875 const vector<String16>& app,
876 const vector<String16>& installer) {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600877 ENFORCE_UID(AID_SYSTEM);
878
yro74fed972017-11-27 14:42:42 -0800879 VLOG("StatsService::informAllUidData was called");
dwchen730403e2018-10-29 11:41:56 -0700880 mUidMap->updateMap(getElapsedRealtimeNs(), uid, version, version_string, app, installer);
yro74fed972017-11-27 14:42:42 -0800881 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700882
883 return Status::ok();
884}
885
dwchen730403e2018-10-29 11:41:56 -0700886Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version,
887 const String16& version_string, const String16& installer) {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600888 ENFORCE_UID(AID_SYSTEM);
David Chende701692017-10-05 13:16:02 -0700889
Jeff Sharkey6b649252018-04-16 09:50:22 -0600890 VLOG("StatsService::informOnePackage was called");
dwchen730403e2018-10-29 11:41:56 -0700891 mUidMap->updateApp(getElapsedRealtimeNs(), app, uid, version, version_string, installer);
David Chende701692017-10-05 13:16:02 -0700892 return Status::ok();
893}
894
895Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600896 ENFORCE_UID(AID_SYSTEM);
David Chende701692017-10-05 13:16:02 -0700897
Jeff Sharkey6b649252018-04-16 09:50:22 -0600898 VLOG("StatsService::informOnePackageRemoved was called");
David Chenbd125272018-04-04 19:02:50 -0700899 mUidMap->removeApp(getElapsedRealtimeNs(), app, uid);
yro01924022018-02-20 18:20:49 -0800900 mConfigManager->RemoveConfigs(uid);
David Chende701692017-10-05 13:16:02 -0700901 return Status::ok();
902}
903
Yao Chenef99c4f2017-09-22 16:26:54 -0700904Status StatsService::informAnomalyAlarmFired() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600905 ENFORCE_UID(AID_SYSTEM);
906
yro74fed972017-11-27 14:42:42 -0800907 VLOG("StatsService::informAnomalyAlarmFired was called");
Yangster-macb142cc82018-03-30 15:22:08 -0700908 int64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800909 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
910 mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
911 if (alarmSet.size() > 0) {
Bookatz66fe0612018-02-07 18:51:48 -0800912 VLOG("Found an anomaly alarm that fired.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800913 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
Bookatz66fe0612018-02-07 18:51:48 -0800914 } else {
915 VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
916 }
Bookatz1b0b1142017-09-08 11:58:42 -0700917 return Status::ok();
918}
919
Yangster-mac932ecec2018-02-01 10:23:52 -0800920Status StatsService::informAlarmForSubscriberTriggeringFired() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600921 ENFORCE_UID(AID_SYSTEM);
922
Yangster-mac932ecec2018-02-01 10:23:52 -0800923 VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
Yangster-macb142cc82018-03-30 15:22:08 -0700924 int64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800925 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
926 mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
927 if (alarmSet.size() > 0) {
928 VLOG("Found periodic alarm fired.");
929 mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
930 } else {
931 ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
932 }
933 return Status::ok();
934}
935
Yao Chenef99c4f2017-09-22 16:26:54 -0700936Status StatsService::informPollAlarmFired() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600937 ENFORCE_UID(AID_SYSTEM);
938
yro74fed972017-11-27 14:42:42 -0800939 VLOG("StatsService::informPollAlarmFired was called");
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700940 mProcessor->informPullAlarmFired(getElapsedRealtimeNs());
yro74fed972017-11-27 14:42:42 -0800941 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700942 return Status::ok();
943}
944
Yao Chenef99c4f2017-09-22 16:26:54 -0700945Status StatsService::systemRunning() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600946 ENFORCE_UID(AID_SYSTEM);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700947
948 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800949 VLOG("StatsService::systemRunning");
Bookatzb487b552017-09-18 11:26:01 -0700950 sayHiToStatsCompanion();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700951 return Status::ok();
952}
953
Yangster-mac892f3d32018-05-02 14:16:48 -0700954Status StatsService::informDeviceShutdown() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600955 ENFORCE_UID(AID_SYSTEM);
Chenjie Yue36018b2018-04-16 15:18:30 -0700956 VLOG("StatsService::informDeviceShutdown");
Yangster-mac892f3d32018-05-02 14:16:48 -0700957 mProcessor->WriteDataToDisk(DEVICE_SHUTDOWN);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800958 mProcessor->WriteMetricsActivationToDisk(getElapsedRealtimeNs());
yro947fbce2017-11-15 22:50:23 -0800959 return Status::ok();
960}
961
Yao Chenef99c4f2017-09-22 16:26:54 -0700962void StatsService::sayHiToStatsCompanion() {
Bookatzb487b552017-09-18 11:26:01 -0700963 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
964 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800965 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700966 statsCompanion->statsdReady();
967 } else {
yro74fed972017-11-27 14:42:42 -0800968 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700969 }
970}
971
Yao Chenef99c4f2017-09-22 16:26:54 -0700972Status StatsService::statsCompanionReady() {
Jeff Sharkey6b649252018-04-16 09:50:22 -0600973 ENFORCE_UID(AID_SYSTEM);
974
yro74fed972017-11-27 14:42:42 -0800975 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700976 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
977 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700978 return Status::fromExceptionCode(
979 Status::EX_NULL_POINTER,
980 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700981 }
yro74fed972017-11-27 14:42:42 -0800982 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700983 IInterface::asBinder(statsCompanion)->linkToDeath(this);
Chenjie Yue2219202018-06-08 10:07:51 -0700984 mPullerManager->SetStatsCompanionService(statsCompanion);
Yangster-mac932ecec2018-02-01 10:23:52 -0800985 mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion);
986 mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800987 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700988 return Status::ok();
989}
990
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700991void StatsService::Startup() {
992 mConfigManager->Startup();
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800993 mProcessor->LoadMetricsActivationFromDisk();
Bookatz906a35c2017-09-20 15:26:44 -0700994}
995
Yangster-mac97e7d202018-10-09 11:05:39 -0700996void StatsService::Terminate() {
997 ALOGI("StatsService::Terminating");
998 if (mProcessor != nullptr) {
999 mProcessor->WriteDataToDisk(TERMINATION_SIGNAL_RECEIVED);
1000 }
1001}
1002
Yao Chen3ff3a492018-08-06 16:17:37 -07001003void StatsService::OnLogEvent(LogEvent* event) {
1004 mProcessor->OnLogEvent(event);
Yao Chena80e5c02018-09-04 13:55:29 -07001005 if (mShellSubscriber != nullptr) {
1006 mShellSubscriber->onLogEvent(*event);
1007 }
Bookatz906a35c2017-09-20 15:26:44 -07001008}
1009
Jeff Sharkey6b649252018-04-16 09:50:22 -06001010Status StatsService::getData(int64_t key, const String16& packageName, vector<uint8_t>* output) {
1011 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1012
David Chenadaf8b32017-11-03 15:42:08 -07001013 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -08001014 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Bookatz4f716292018-04-10 17:15:12 -07001015 ConfigKey configKey(ipc->getCallingUid(), key);
David Chen56ae0d92018-05-11 16:00:22 -07001016 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), false /* include_current_bucket*/,
Bookatzff71cad2018-09-20 17:17:49 -07001017 true /* erase_data */, GET_DATA_CALLED, output);
Bookatz4f716292018-04-10 17:15:12 -07001018 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -07001019}
1020
Jeff Sharkey6b649252018-04-16 09:50:22 -06001021Status StatsService::getMetadata(const String16& packageName, vector<uint8_t>* output) {
1022 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1023
David Chen2e8f3802017-11-22 10:56:48 -08001024 IPCThreadState* ipc = IPCThreadState::self();
1025 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
1026 ipc->getCallingUid());
Bookatz4f716292018-04-10 17:15:12 -07001027 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
1028 return Status::ok();
David Chen2e8f3802017-11-22 10:56:48 -08001029}
1030
Jeff Sharkey6b649252018-04-16 09:50:22 -06001031Status StatsService::addConfiguration(int64_t key, const vector <uint8_t>& config,
1032 const String16& packageName) {
1033 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1034
David Chenadaf8b32017-11-03 15:42:08 -07001035 IPCThreadState* ipc = IPCThreadState::self();
Bookatz4f716292018-04-10 17:15:12 -07001036 if (addConfigurationChecked(ipc->getCallingUid(), key, config)) {
David Chen661f7912018-01-22 17:46:24 -08001037 return Status::ok();
1038 } else {
Bookatz4f716292018-04-10 17:15:12 -07001039 ALOGE("Could not parse malformatted StatsdConfig");
1040 return Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT,
1041 "config does not correspond to a StatsdConfig proto");
David Chen661f7912018-01-22 17:46:24 -08001042 }
1043}
1044
David Chen9fdd4032018-03-20 14:38:56 -07001045bool StatsService::addConfigurationChecked(int uid, int64_t key, const vector<uint8_t>& config) {
1046 ConfigKey configKey(uid, key);
1047 StatsdConfig cfg;
1048 if (config.size() > 0) { // If the config is empty, skip parsing.
1049 if (!cfg.ParseFromArray(&config[0], config.size())) {
1050 return false;
1051 }
1052 }
1053 mConfigManager->UpdateConfig(configKey, cfg);
1054 return true;
1055}
1056
Jeff Sharkey6b649252018-04-16 09:50:22 -06001057Status StatsService::removeDataFetchOperation(int64_t key, const String16& packageName) {
1058 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1059
Bookatz4f716292018-04-10 17:15:12 -07001060 IPCThreadState* ipc = IPCThreadState::self();
1061 ConfigKey configKey(ipc->getCallingUid(), key);
1062 mConfigManager->RemoveConfigReceiver(configKey);
1063 return Status::ok();
David Chen661f7912018-01-22 17:46:24 -08001064}
1065
Jeff Sharkey6b649252018-04-16 09:50:22 -06001066Status StatsService::setDataFetchOperation(int64_t key,
1067 const sp<android::IBinder>& intentSender,
1068 const String16& packageName) {
1069 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1070
Bookatz4f716292018-04-10 17:15:12 -07001071 IPCThreadState* ipc = IPCThreadState::self();
1072 ConfigKey configKey(ipc->getCallingUid(), key);
1073 mConfigManager->SetConfigReceiver(configKey, intentSender);
David Chen48944902018-05-03 10:29:11 -07001074 if (StorageManager::hasConfigMetricsReport(configKey)) {
1075 VLOG("StatsService::setDataFetchOperation marking configKey %s to dump reports on disk",
1076 configKey.ToString().c_str());
1077 mProcessor->noteOnDiskData(configKey);
1078 }
Bookatz4f716292018-04-10 17:15:12 -07001079 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -07001080}
1081
Tej Singh2c9ef2a2019-01-22 11:33:51 -08001082Status StatsService::setActiveConfigsChangedOperation(const sp<android::IBinder>& intentSender,
1083 const String16& packageName,
1084 vector<int64_t>* output) {
1085 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1086
1087 IPCThreadState* ipc = IPCThreadState::self();
Tej Singh6ede28b2019-01-29 17:06:54 -08001088 int uid = ipc->getCallingUid();
1089 mConfigManager->SetActiveConfigsChangedReceiver(uid, intentSender);
1090 if (output != nullptr) {
1091 mProcessor->GetActiveConfigs(uid, *output);
1092 } else {
1093 ALOGW("StatsService::setActiveConfigsChanged output was nullptr");
1094 }
Tej Singh2c9ef2a2019-01-22 11:33:51 -08001095 return Status::ok();
1096}
1097
1098Status StatsService::removeActiveConfigsChangedOperation(const String16& packageName) {
1099 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1100
1101 IPCThreadState* ipc = IPCThreadState::self();
1102 mConfigManager->RemoveActiveConfigsChangedReceiver(ipc->getCallingUid());
1103 return Status::ok();
1104}
1105
Jeff Sharkey6b649252018-04-16 09:50:22 -06001106Status StatsService::removeConfiguration(int64_t key, const String16& packageName) {
1107 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1108
Bookatz4f716292018-04-10 17:15:12 -07001109 IPCThreadState* ipc = IPCThreadState::self();
1110 ConfigKey configKey(ipc->getCallingUid(), key);
1111 mConfigManager->RemoveConfig(configKey);
1112 SubscriberReporter::getInstance().removeConfig(configKey);
1113 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -07001114}
1115
Bookatzc6977972018-01-16 16:55:05 -08001116Status StatsService::setBroadcastSubscriber(int64_t configId,
1117 int64_t subscriberId,
Jeff Sharkey6b649252018-04-16 09:50:22 -06001118 const sp<android::IBinder>& intentSender,
1119 const String16& packageName) {
1120 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1121
Bookatzc6977972018-01-16 16:55:05 -08001122 VLOG("StatsService::setBroadcastSubscriber called.");
Bookatz4f716292018-04-10 17:15:12 -07001123 IPCThreadState* ipc = IPCThreadState::self();
1124 ConfigKey configKey(ipc->getCallingUid(), configId);
1125 SubscriberReporter::getInstance()
1126 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
1127 return Status::ok();
Bookatzc6977972018-01-16 16:55:05 -08001128}
1129
1130Status StatsService::unsetBroadcastSubscriber(int64_t configId,
Jeff Sharkey6b649252018-04-16 09:50:22 -06001131 int64_t subscriberId,
1132 const String16& packageName) {
1133 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1134
Bookatzc6977972018-01-16 16:55:05 -08001135 VLOG("StatsService::unsetBroadcastSubscriber called.");
Bookatz4f716292018-04-10 17:15:12 -07001136 IPCThreadState* ipc = IPCThreadState::self();
1137 ConfigKey configKey(ipc->getCallingUid(), configId);
1138 SubscriberReporter::getInstance()
1139 .unsetBroadcastSubscriber(configKey, subscriberId);
1140 return Status::ok();
Bookatzc6977972018-01-16 16:55:05 -08001141}
1142
yrobe6d7f92018-05-04 13:02:53 -07001143Status StatsService::sendAppBreadcrumbAtom(int32_t label, int32_t state) {
1144 // Permission check not necessary as it's meant for applications to write to
1145 // statsd.
1146 android::util::stats_write(util::APP_BREADCRUMB_REPORTED,
1147 IPCThreadState::self()->getCallingUid(), label,
1148 state);
1149 return Status::ok();
1150}
1151
Tej Singha0c89dd2019-01-25 16:39:18 -08001152Status StatsService::registerPullerCallback(int32_t atomTag,
1153 const sp<android::os::IStatsPullerCallback>& pullerCallback,
1154 const String16& packageName) {
1155 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1156
1157 VLOG("StatsService::registerPullerCallback called.");
1158 mPullerManager->RegisterPullerCallback(atomTag, pullerCallback);
1159 return Status::ok();
1160}
1161
1162Status StatsService::unregisterPullerCallback(int32_t atomTag, const String16& packageName) {
1163 ENFORCE_DUMP_AND_USAGE_STATS(packageName);
1164
1165 VLOG("StatsService::unregisterPullerCallback called.");
1166 mPullerManager->UnregisterPullerCallback(atomTag);
1167 return Status::ok();
1168}
1169
Howard Roa46b6582018-09-18 16:45:02 -07001170hardware::Return<void> StatsService::reportSpeakerImpedance(
1171 const SpeakerImpedance& speakerImpedance) {
1172 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), speakerImpedance);
1173 mProcessor->OnLogEvent(&event);
1174
1175 return hardware::Void();
1176}
1177
1178hardware::Return<void> StatsService::reportHardwareFailed(const HardwareFailed& hardwareFailed) {
1179 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), hardwareFailed);
1180 mProcessor->OnLogEvent(&event);
1181
1182 return hardware::Void();
1183}
1184
1185hardware::Return<void> StatsService::reportPhysicalDropDetected(
1186 const PhysicalDropDetected& physicalDropDetected) {
1187 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), physicalDropDetected);
1188 mProcessor->OnLogEvent(&event);
1189
1190 return hardware::Void();
1191}
1192
1193hardware::Return<void> StatsService::reportChargeCycles(const ChargeCycles& chargeCycles) {
1194 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), chargeCycles);
1195 mProcessor->OnLogEvent(&event);
1196
1197 return hardware::Void();
1198}
1199
1200hardware::Return<void> StatsService::reportBatteryHealthSnapshot(
1201 const BatteryHealthSnapshotArgs& batteryHealthSnapshotArgs) {
1202 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(),
1203 batteryHealthSnapshotArgs);
1204 mProcessor->OnLogEvent(&event);
1205
1206 return hardware::Void();
1207}
1208
1209hardware::Return<void> StatsService::reportSlowIo(const SlowIo& slowIo) {
1210 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), slowIo);
1211 mProcessor->OnLogEvent(&event);
1212
1213 return hardware::Void();
1214}
1215
1216hardware::Return<void> StatsService::reportBatteryCausedShutdown(
1217 const BatteryCausedShutdown& batteryCausedShutdown) {
1218 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), batteryCausedShutdown);
1219 mProcessor->OnLogEvent(&event);
1220
1221 return hardware::Void();
1222}
1223
Maggie Whitefc1aa592018-11-28 21:55:23 -08001224hardware::Return<void> StatsService::reportUsbPortOverheatEvent(
1225 const UsbPortOverheatEvent& usbPortOverheatEvent) {
1226 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), usbPortOverheatEvent);
1227 mProcessor->OnLogEvent(&event);
1228
1229 return hardware::Void();
1230}
1231
Carter Hsub8fd1e92019-01-11 15:24:45 +08001232hardware::Return<void> StatsService::reportSpeechDspStat(
1233 const SpeechDspStat& speechDspStat) {
1234 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), speechDspStat);
1235 mProcessor->OnLogEvent(&event);
1236
1237 return hardware::Void();
1238}
1239
Maggie White58174da2019-01-18 15:23:35 -08001240hardware::Return<void> StatsService::reportVendorAtom(const VendorAtom& vendorAtom) {
1241 std::string reverseDomainName = (std::string) vendorAtom.reverseDomainName;
1242 if (vendorAtom.atomId < 100000 || vendorAtom.atomId >= 200000) {
1243 ALOGE("Atom ID %ld is not a valid vendor atom ID", (long) vendorAtom.atomId);
1244 return hardware::Void();
1245 }
1246 if (reverseDomainName.length() > 50) {
1247 ALOGE("Vendor atom reverse domain name %s is too long.", reverseDomainName.c_str());
1248 return hardware::Void();
1249 }
1250 LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), vendorAtom);
1251 mProcessor->OnLogEvent(&event);
1252
1253 return hardware::Void();
1254}
1255
David Chen1d7b0cd2017-11-15 14:20:04 -08001256void StatsService::binderDied(const wp <IBinder>& who) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -07001257 ALOGW("statscompanion service died");
Yangster-mac892f3d32018-05-02 14:16:48 -07001258 StatsdStats::getInstance().noteSystemServerRestart(getWallClockSec());
1259 if (mProcessor != nullptr) {
Howard Roe60992b2018-08-30 14:37:29 -07001260 ALOGW("Reset statsd upon system server restarts.");
Yangster-mac892f3d32018-05-02 14:16:48 -07001261 mProcessor->WriteDataToDisk(STATSCOMPANION_DIED);
1262 mProcessor->resetConfigs();
1263 }
Chenjie Yuaa5b2012018-03-21 13:53:15 -07001264 mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
1265 mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
1266 SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
Chenjie Yue2219202018-06-08 10:07:51 -07001267 mPullerManager->SetStatsCompanionService(nullptr);
yro31eb67b2017-10-24 13:33:21 -07001268}
1269
Yao Chenef99c4f2017-09-22 16:26:54 -07001270} // namespace statsd
1271} // namespace os
1272} // namespace android