1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define STATSD_DEBUG false // STOPSHIP if true
18 #include "Log.h"
19
20 #include "StatsService.h"
21
22 #include <android-base/file.h>
23 #include <android-base/strings.h>
24 #include <android/binder_ibinder_platform.h>
25 #include <cutils/multiuser.h>
26 #include <private/android_filesystem_config.h>
27 #include <src/statsd_config.pb.h>
28 #include <src/uid_data.pb.h>
29 #include <statslog_statsd.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/random.h>
33 #include <sys/system_properties.h>
34 #include <unistd.h>
35 #include <utils/String16.h>
36
37 #include "android-base/stringprintf.h"
38 #include "config/ConfigKey.h"
39 #include "config/ConfigManager.h"
40 #include "flags/FlagProvider.h"
41 #include "guardrail/StatsdStats.h"
42 #include "stats_log_util.h"
43 #include "storage/StorageManager.h"
44 #include "subscriber/SubscriberReporter.h"
45 #include "utils/DbUtils.h"
46 #include "utils/api_tracing.h"
47
48 using namespace android;
49
50 using android::base::StringPrintf;
51 using android::util::FIELD_COUNT_REPEATED;
52 using android::util::FIELD_TYPE_MESSAGE;
53
54 using Status = ::ndk::ScopedAStatus;
55
56 namespace android {
57 namespace os {
58 namespace statsd {
59
60 constexpr const char* kPermissionDump = "android.permission.DUMP";
61
62 constexpr const char* kTracedProbesSid = "u:r:traced_probes:s0";
63
64 constexpr const char* kPermissionRegisterPullAtom = "android.permission.REGISTER_STATS_PULL_ATOM";
65
66 #define STATS_SERVICE_DIR "/data/misc/stats-service"
67
68 // for StatsDataDumpProto
69 const int FIELD_ID_REPORTS_LIST = 1;
70
exception(int32_t code,const std::string & msg)71 static Status exception(int32_t code, const std::string& msg) {
72 ALOGE("%s (%d)", msg.c_str(), code);
73 return Status::fromExceptionCodeWithMessage(code, msg.c_str());
74 }
75
checkPermission(const char * permission)76 static bool checkPermission(const char* permission) {
77 pid_t pid = AIBinder_getCallingPid();
78 uid_t uid = AIBinder_getCallingUid();
79 return checkPermissionForIds(permission, pid, uid);
80 }
81
checkUid(uid_t expectedUid)82 Status checkUid(uid_t expectedUid) {
83 uid_t uid = AIBinder_getCallingUid();
84 if (uid == expectedUid || uid == AID_ROOT) {
85 return Status::ok();
86 } else {
87 return exception(EX_SECURITY,
88 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
89 }
90 }
91
92 #define ENFORCE_UID(uid) { \
93 Status status = checkUid((uid)); \
94 if (!status.isOk()) { \
95 return status; \
96 } \
97 }
98
checkSid(const char * expectedSid)99 Status checkSid(const char* expectedSid) {
100 const char* sid = nullptr;
101 if (__builtin_available(android __ANDROID_API_U__, *)) {
102 sid = AIBinder_getCallingSid();
103 }
104
105 // root (which is the uid in tests for example) has all permissions.
106 uid_t uid = AIBinder_getCallingUid();
107 if (uid == AID_ROOT) {
108 return Status::ok();
109 }
110
111 if (sid != nullptr && strcmp(expectedSid, sid) == 0) {
112 return Status::ok();
113 } else {
114 return exception(EX_SECURITY,
115 StringPrintf("SID '%s' is not expected SID '%s'", sid, expectedSid));
116 }
117 }
118
119 #define ENFORCE_SID(sid) \
120 { \
121 Status status = checkSid((sid)); \
122 if (!status.isOk()) { \
123 return status; \
124 } \
125 }
126
StatsService(const sp<UidMap> & uidMap,shared_ptr<LogEventQueue> queue,const std::shared_ptr<LogEventFilter> & logEventFilter)127 StatsService::StatsService(const sp<UidMap>& uidMap, shared_ptr<LogEventQueue> queue,
128 const std::shared_ptr<LogEventFilter>& logEventFilter)
129 : mUidMap(uidMap),
130 mAnomalyAlarmMonitor(new AlarmMonitor(
131 MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
132 [this](const shared_ptr<IStatsCompanionService>& /*sc*/, int64_t timeMillis) {
133 mProcessor->setAnomalyAlarm(timeMillis);
134 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
135 },
__anon3b4d5f610202(const shared_ptr<IStatsCompanionService>& ) 136 [this](const shared_ptr<IStatsCompanionService>& /*sc*/) {
137 mProcessor->cancelAnomalyAlarm();
138 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
139 })),
140 mPeriodicAlarmMonitor(new AlarmMonitor(
141 MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
__anon3b4d5f610302(const shared_ptr<IStatsCompanionService>& sc, int64_t timeMillis) 142 [](const shared_ptr<IStatsCompanionService>& sc, int64_t timeMillis) {
143 if (sc != nullptr) {
144 sc->setAlarmForSubscriberTriggering(timeMillis);
145 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
146 }
147 },
__anon3b4d5f610402(const shared_ptr<IStatsCompanionService>& sc) 148 [](const shared_ptr<IStatsCompanionService>& sc) {
149 if (sc != nullptr) {
150 sc->cancelAlarmForSubscriberTriggering();
151 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
152 }
153 })),
154 mEventQueue(std::move(queue)),
155 mLogEventFilter(logEventFilter),
156 mBootCompleteTrigger({kBootCompleteTag, kUidMapReceivedTag, kAllPullersRegisteredTag},
__anon3b4d5f610502() 157 [this]() { onStatsdInitCompleted(kStatsdInitDelaySecs); }),
158 mStatsCompanionServiceDeathRecipient(
159 AIBinder_DeathRecipient_new(StatsService::statsCompanionServiceDied)) {
160 mPullerManager = new StatsPullerManager();
161 StatsPuller::SetUidMap(mUidMap);
162 mConfigManager = new ConfigManager();
163 mProcessor = new StatsLogProcessor(
164 mUidMap, mPullerManager, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor,
165 getElapsedRealtimeNs(),
__anon3b4d5f610602(const ConfigKey& key) 166 [this](const ConfigKey& key) {
167 shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetConfigReceiver(key);
168 if (receiver == nullptr) {
169 VLOG("Could not find a broadcast receiver for %s", key.ToString().c_str());
170 return false;
171 }
172 Status status = receiver->sendDataBroadcast(mProcessor->getLastReportTimeNs(key));
173 if (status.isOk()) {
174 return true;
175 }
176 if (status.getExceptionCode() == EX_TRANSACTION_FAILED &&
177 status.getStatus() == STATUS_DEAD_OBJECT) {
178 mConfigManager->RemoveConfigReceiver(key, receiver);
179 }
180 VLOG("Failed to send a broadcast for receiver %s", key.ToString().c_str());
181 return false;
182 },
__anon3b4d5f610702(const int& uid, const vector<int64_t>& activeConfigs) 183 [this](const int& uid, const vector<int64_t>& activeConfigs) {
184 shared_ptr<IPendingIntentRef> receiver =
185 mConfigManager->GetActiveConfigsChangedReceiver(uid);
186 if (receiver == nullptr) {
187 VLOG("Could not find receiver for uid %d", uid);
188 return false;
189 }
190 Status status = receiver->sendActiveConfigsChangedBroadcast(activeConfigs);
191 if (status.isOk()) {
192 VLOG("StatsService::active configs broadcast succeeded for uid %d" , uid);
193 return true;
194 }
195 if (status.getExceptionCode() == EX_TRANSACTION_FAILED &&
196 status.getStatus() == STATUS_DEAD_OBJECT) {
197 mConfigManager->RemoveActiveConfigsChangedReceiver(uid, receiver);
198 }
199 VLOG("StatsService::active configs broadcast failed for uid %d", uid);
200 return false;
201 },
202 [this](const ConfigKey& key, const string& delegatePackage,
__anon3b4d5f610802(const ConfigKey& key, const string& delegatePackage, const vector<int64_t>& restrictedMetrics) 203 const vector<int64_t>& restrictedMetrics) {
204 set<string> configPackages;
205 set<int32_t> delegateUids;
206 for (const auto& kv : UidMap::sAidToUidMapping) {
207 if (kv.second == static_cast<uint32_t>(key.GetUid())) {
208 configPackages.insert(kv.first);
209 }
210 if (kv.first == delegatePackage) {
211 delegateUids.insert(kv.second);
212 }
213 }
214 if (configPackages.empty()) {
215 configPackages = mUidMap->getAppNamesFromUid(key.GetUid(), true);
216 }
217 if (delegateUids.empty()) {
218 delegateUids = mUidMap->getAppUid(delegatePackage);
219 }
220 mConfigManager->SendRestrictedMetricsBroadcast(configPackages, key.GetId(),
221 delegateUids, restrictedMetrics);
222 },
223 logEventFilter);
224
225 mUidMap->setListener(mProcessor);
226 mConfigManager->AddListener(mProcessor);
227
228 init_system_properties();
229 }
230
~StatsService()231 StatsService::~StatsService() {
232 ATRACE_CALL();
233 onStatsdInitCompletedHandlerTermination();
234 if (mEventQueue != nullptr) {
235 stopReadingLogs();
236 mLogsReaderThread->join();
237 }
238 }
239
240 /* Runs on a dedicated thread to process pushed events. */
readLogs()241 void StatsService::readLogs() {
242 // Read forever..... long live statsd
243 while (1) {
244 // Block until an event is available.
245 auto event = mEventQueue->waitPop();
246
247 // Below flag will be set when statsd is exiting and log event will be pushed to break
248 // out of waitPop.
249 if (mIsStopRequested) {
250 break;
251 }
252
253 // Pass it to StatsLogProcess to all configs/metrics
254 // At this point, the LogEventQueue is not blocked, so that the socketListener
255 // can read events from the socket and write to buffer to avoid data drop.
256 mProcessor->OnLogEvent(event.get());
257 // The ShellSubscriber is only used by shell for local debugging.
258 if (mShellSubscriber != nullptr) {
259 mShellSubscriber->onLogEvent(*event);
260 }
261 }
262 }
263
init_system_properties()264 void StatsService::init_system_properties() {
265 mEngBuild = false;
266 const prop_info* buildType = __system_property_find("ro.build.type");
267 if (buildType != NULL) {
268 __system_property_read_callback(buildType, init_build_type_callback, this);
269 }
270 }
271
init_build_type_callback(void * cookie,const char *,const char * value,uint32_t serial)272 void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
273 uint32_t serial) {
274 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
275 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
276 }
277 }
278
279 /**
280 * Write data from statsd.
281 * Format for statsdStats: adb shell dumpsys stats --metadata [-v] [--proto]
282 * Format for data report: adb shell dumpsys stats [anything other than --metadata] [--proto]
283 * Anything ending in --proto will be in proto format.
284 * Anything without --metadata as the first argument will be report information.
285 * (bugreports call "adb shell dumpsys stats --dump-priority NORMAL -a --proto")
286 * TODO: Come up with a more robust method of enacting <serviceutils/PriorityDumper.h>.
287 */
dump(int fd,const char ** args,uint32_t numArgs)288 status_t StatsService::dump(int fd, const char** args, uint32_t numArgs) {
289 if (!checkPermission(kPermissionDump)) {
290 return PERMISSION_DENIED;
291 }
292
293 int lastArg = numArgs - 1;
294 bool asProto = false;
295 if (lastArg >= 0 && string(args[lastArg]) == "--proto") { // last argument
296 asProto = true;
297 lastArg--;
298 }
299 if (numArgs > 0 && string(args[0]) == "--metadata") { // first argument
300 // Request is to dump statsd stats.
301 bool verbose = false;
302 if (lastArg >= 0 && string(args[lastArg]) == "-v") {
303 verbose = true;
304 lastArg--;
305 }
306 dumpStatsdStats(fd, verbose, asProto);
307 } else {
308 // Request is to dump statsd report data.
309 if (asProto) {
310 dumpIncidentSection(fd);
311 } else {
312 dprintf(fd, "Non-proto format of stats data dump not available; see proto version.\n");
313 }
314 }
315
316 return NO_ERROR;
317 }
318
319 /**
320 * Write debugging data about statsd in text or proto format.
321 */
dumpStatsdStats(int out,bool verbose,bool proto)322 void StatsService::dumpStatsdStats(int out, bool verbose, bool proto) {
323 if (proto) {
324 vector<uint8_t> data;
325 StatsdStats::getInstance().dumpStats(&data, false); // does not reset statsdStats.
326 for (size_t i = 0; i < data.size(); i ++) {
327 dprintf(out, "%c", data[i]);
328 }
329 } else {
330 StatsdStats::getInstance().dumpStats(out);
331 mProcessor->dumpStates(out, verbose);
332 }
333 }
334
335 /**
336 * Write stats report data in StatsDataDumpProto incident section format.
337 */
dumpIncidentSection(int out)338 void StatsService::dumpIncidentSection(int out) {
339 ProtoOutputStream proto;
340 for (const ConfigKey& configKey : mConfigManager->GetAllConfigKeys()) {
341 uint64_t reportsListToken =
342 proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS_LIST);
343 // Don't include the current bucket to avoid skipping buckets.
344 // If we need to include the current bucket later, consider changing to NO_TIME_CONSTRAINTS
345 // or other alternatives to avoid skipping buckets for pulled metrics.
346 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), getWallClockNs(),
347 false /* includeCurrentBucket */, false /* erase_data */, ADB_DUMP,
348 FAST, &proto);
349 proto.end(reportsListToken);
350 proto.flush(out);
351 proto.clear();
352 }
353 }
354
355 /**
356 * Implementation of the adb shell cmd stats command.
357 */
handleShellCommand(int in,int out,int err,const char ** argv,uint32_t argc)358 status_t StatsService::handleShellCommand(int in, int out, int err, const char** argv,
359 uint32_t argc) {
360 uid_t uid = AIBinder_getCallingUid();
361 if (uid != AID_ROOT && uid != AID_SHELL) {
362 return PERMISSION_DENIED;
363 }
364
365 Vector<String8> utf8Args;
366 utf8Args.setCapacity(argc);
367 for (uint32_t i = 0; i < argc; i++) {
368 utf8Args.push(String8(argv[i]));
369 }
370
371 if (argc >= 1) {
372 // adb shell cmd stats config ...
373 if (!utf8Args[0].compare(String8("config"))) {
374 return cmd_config(in, out, err, utf8Args);
375 }
376
377 if (!utf8Args[0].compare(String8("print-uid-map"))) {
378 return cmd_print_uid_map(out, utf8Args);
379 }
380
381 if (!utf8Args[0].compare(String8("dump-report"))) {
382 return cmd_dump_report(out, utf8Args);
383 }
384
385 if (!utf8Args[0].compare(String8("pull-source")) && argc > 1) {
386 return cmd_print_pulled_metrics(out, utf8Args);
387 }
388
389 if (!utf8Args[0].compare(String8("send-broadcast"))) {
390 return cmd_trigger_broadcast(out, utf8Args);
391 }
392
393 if (!utf8Args[0].compare(String8("print-stats"))) {
394 return cmd_print_stats(out, utf8Args);
395 }
396
397 if (!utf8Args[0].compare(String8("meminfo"))) {
398 return cmd_dump_memory_info(out);
399 }
400
401 if (!utf8Args[0].compare(String8("write-to-disk"))) {
402 return cmd_write_data_to_disk(out);
403 }
404
405 if (!utf8Args[0].compare(String8("log-app-breadcrumb"))) {
406 return cmd_log_app_breadcrumb(out, utf8Args);
407 }
408
409 if (!utf8Args[0].compare(String8("log-binary-push"))) {
410 return cmd_log_binary_push(out, utf8Args);
411 }
412
413 if (!utf8Args[0].compare(String8("clear-puller-cache"))) {
414 return cmd_clear_puller_cache(out);
415 }
416
417 if (!utf8Args[0].compare(String8("print-logs"))) {
418 return cmd_print_logs(out, utf8Args);
419 }
420
421 if (!utf8Args[0].compare(String8("send-active-configs"))) {
422 return cmd_trigger_active_config_broadcast(out, utf8Args);
423 }
424
425 if (!utf8Args[0].compare(String8("data-subscribe"))) {
426 initShellSubscriber();
427 int timeoutSec = -1;
428 if (argc >= 2) {
429 timeoutSec = atoi(utf8Args[1].c_str());
430 }
431 mShellSubscriber->startNewSubscription(in, out, timeoutSec);
432 return NO_ERROR;
433 }
434 }
435
436 print_cmd_help(out);
437 return NO_ERROR;
438 }
439
print_cmd_help(int out)440 void StatsService::print_cmd_help(int out) {
441 dprintf(out,
442 "usage: adb shell cmd stats print-stats-log [tag_required] "
443 "[timestamp_nsec_optional]\n");
444 dprintf(out, "\n");
445 dprintf(out, "\n");
446 dprintf(out, "usage: adb shell cmd stats meminfo\n");
447 dprintf(out, "\n");
448 dprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
449 dprintf(out, " # adb shell stop\n");
450 dprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
451 dprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
452 dprintf(out, " # adb shell start\n");
453 dprintf(out, "\n");
454 dprintf(out, "\n");
455 dprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
456 dprintf(out, "usage: adb shell cmd stats print-uid-map --with_certificate_hash\n");
457 dprintf(out, "\n");
458 dprintf(out, " Prints the UID, app name, version mapping.\n");
459 dprintf(out,
460 " PKG Optional package name to print the uids of the "
461 "package\n");
462 dprintf(out, " --with_certificate_hash Print package certificate hash in hex\n");
463 dprintf(out, "\n");
464 dprintf(out, "\n");
465 dprintf(out, "usage: adb shell cmd stats pull-source ATOM_TAG [PACKAGE] \n");
466 dprintf(out, "\n");
467 dprintf(out, " Prints the output of a pulled atom\n");
468 dprintf(out, " UID The atom to pull\n");
469 dprintf(out, " PACKAGE The package to pull from. Default is AID_SYSTEM\n");
470 dprintf(out, "\n");
471 dprintf(out, "\n");
472 dprintf(out, "usage: adb shell cmd stats write-to-disk \n");
473 dprintf(out, "\n");
474 dprintf(out, " Flushes all data on memory to disk.\n");
475 dprintf(out, "\n");
476 dprintf(out, "\n");
477 dprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n");
478 dprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n");
479 dprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
480 dprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
481 dprintf(out, " uid is used.\n");
482 dprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
483 dprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
484 dprintf(out, "\n");
485 dprintf(out, "\n");
486 dprintf(out,
487 "usage: adb shell cmd stats log-binary-push NAME VERSION STAGING ROLLBACK_ENABLED "
488 "LOW_LATENCY STATE EXPERIMENT_IDS\n");
489 dprintf(out, " Log a binary push state changed event.\n");
490 dprintf(out, " NAME The train name.\n");
491 dprintf(out, " VERSION The train version code.\n");
492 dprintf(out, " STAGING If this train requires a restart.\n");
493 dprintf(out, " ROLLBACK_ENABLED If rollback should be enabled for this install.\n");
494 dprintf(out, " LOW_LATENCY If the train requires low latency monitoring.\n");
495 dprintf(out, " STATE The status of the train push.\n");
496 dprintf(out, " Integer value of the enum in atoms.proto.\n");
497 dprintf(out, " EXPERIMENT_IDS Comma separated list of experiment ids.\n");
498 dprintf(out, " Leave blank for none.\n");
499 dprintf(out, "\n");
500 dprintf(out, "\n");
501 dprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
502 dprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
503 dprintf(out, "\n");
504 dprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
505 dprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
506 dprintf(out, " provided, then all configs will be removed from memory and disk.\n");
507 dprintf(out, "\n");
508 dprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
509 dprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
510 dprintf(out, " uid is used.\n");
511 dprintf(out, " NAME The per-uid name to use\n");
512 dprintf(out, "\n");
513 dprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
514 dprintf(out, "\n be removed from memory and disk!\n");
515 dprintf(out, "\n");
516 dprintf(out,
517 "usage: adb shell cmd stats dump-report [UID] NAME [--keep_data] "
518 "[--include_current_bucket] [--proto]\n");
519 dprintf(out, " Dump all metric data for a configuration.\n");
520 dprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
521 dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
522 dprintf(out, " calling uid is used.\n");
523 dprintf(out, " NAME The name of the configuration\n");
524 dprintf(out, " --keep_data Do NOT erase the data upon dumping it.\n");
525 dprintf(out, " --proto Print proto binary.\n");
526 dprintf(out, "\n");
527 dprintf(out, "\n");
528 dprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
529 dprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
530 dprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
531 dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
532 dprintf(out, " calling uid is used.\n");
533 dprintf(out, " NAME The name of the configuration\n");
534 dprintf(out, "\n");
535 dprintf(out, "\n");
536 dprintf(out,
537 "usage: adb shell cmd stats send-active-configs [--uid=UID] [--configs] "
538 "[NAME1] [NAME2] [NAME3..]\n");
539 dprintf(out, " Send a broadcast that informs the subscriber of the current active configs.\n");
540 dprintf(out, " --uid=UID The uid of the configurations. It is only possible to pass\n");
541 dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
542 dprintf(out, " calling uid is used.\n");
543 dprintf(out, " --configs Send the list of configs in the name list instead of\n");
544 dprintf(out, " the currently active configs\n");
545 dprintf(out, " NAME LIST List of configuration names to be included in the broadcast.\n");
546 dprintf(out, "\n");
547 dprintf(out, "\n");
548 dprintf(out, "usage: adb shell cmd stats print-stats\n");
549 dprintf(out, " Prints some basic stats.\n");
550 dprintf(out, " --proto Print proto binary instead of string format.\n");
551 dprintf(out, "\n");
552 dprintf(out, "\n");
553 dprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
554 dprintf(out, " Clear cached puller data.\n");
555 dprintf(out, "\n");
556 dprintf(out, "usage: adb shell cmd stats print-logs\n");
557 dprintf(out, " Requires root privileges.\n");
558 dprintf(out, " Can be disabled by calling adb shell cmd stats print-logs 0\n");
559 }
560
cmd_trigger_broadcast(int out,Vector<String8> & args)561 status_t StatsService::cmd_trigger_broadcast(int out, Vector<String8>& args) {
562 string name;
563 bool good = false;
564 int uid;
565 const int argCount = args.size();
566 if (argCount == 2) {
567 // Automatically pick the UID
568 uid = AIBinder_getCallingUid();
569 name.assign(args[1].c_str(), args[1].size());
570 good = true;
571 } else if (argCount == 3) {
572 good = getUidFromArgs(args, 1, uid);
573 if (!good) {
574 dprintf(out, "Invalid UID. Note that the metrics can only be dumped for "
575 "other UIDs on eng or userdebug builds.\n");
576 }
577 name.assign(args[2].c_str(), args[2].size());
578 }
579 if (!good) {
580 print_cmd_help(out);
581 return UNKNOWN_ERROR;
582 }
583 ConfigKey key(uid, StrToInt64(name));
584 shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetConfigReceiver(key);
585 if (receiver == nullptr) {
586 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str());
587 return UNKNOWN_ERROR;
588 } else if (receiver->sendDataBroadcast(mProcessor->getLastReportTimeNs(key)).isOk()) {
589 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
590 args[2].c_str());
591 } else {
592 VLOG("StatsService::trigger broadcast failed to %s, %s", args[1].c_str(), args[2].c_str());
593 return UNKNOWN_ERROR;
594 }
595 return NO_ERROR;
596 }
597
cmd_trigger_active_config_broadcast(int out,Vector<String8> & args)598 status_t StatsService::cmd_trigger_active_config_broadcast(int out, Vector<String8>& args) {
599 const int argCount = args.size();
600 int uid;
601 vector<int64_t> configIds;
602 if (argCount == 1) {
603 // Automatically pick the uid and send a broadcast that has no active configs.
604 uid = AIBinder_getCallingUid();
605 mProcessor->GetActiveConfigs(uid, configIds);
606 } else {
607 int curArg = 1;
608 if(args[curArg].find("--uid=") == 0) {
609 string uidArgStr(args[curArg].c_str());
610 string uidStr = uidArgStr.substr(6);
611 if (!getUidFromString(uidStr.c_str(), uid)) {
612 dprintf(out, "Invalid UID. Note that the config can only be set for "
613 "other UIDs on eng or userdebug builds.\n");
614 return UNKNOWN_ERROR;
615 }
616 curArg++;
617 } else {
618 uid = AIBinder_getCallingUid();
619 }
620 if (curArg == argCount || args[curArg] != "--configs") {
621 VLOG("Reached end of args, or specify configs not set. Sending actual active configs,");
622 mProcessor->GetActiveConfigs(uid, configIds);
623 } else {
624 // Flag specified, use the given list of configs.
625 curArg++;
626 for (int i = curArg; i < argCount; i++) {
627 char* endp;
628 int64_t configID = strtoll(args[i].c_str(), &endp, 10);
629 if (endp == args[i].c_str() || *endp != '\0') {
630 dprintf(out, "Error parsing config ID.\n");
631 return UNKNOWN_ERROR;
632 }
633 VLOG("Adding config id %ld", static_cast<long>(configID));
634 configIds.push_back(configID);
635 }
636 }
637 }
638 shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetActiveConfigsChangedReceiver(uid);
639 if (receiver == nullptr) {
640 VLOG("Could not find receiver for uid %d", uid);
641 return UNKNOWN_ERROR;
642 } else if (receiver->sendActiveConfigsChangedBroadcast(configIds).isOk()) {
643 VLOG("StatsService::trigger active configs changed broadcast succeeded for uid %d" , uid);
644 } else {
645 VLOG("StatsService::trigger active configs changed broadcast failed for uid %d", uid);
646 return UNKNOWN_ERROR;
647 }
648 return NO_ERROR;
649 }
650
cmd_config(int in,int out,int err,Vector<String8> & args)651 status_t StatsService::cmd_config(int in, int out, int err, Vector<String8>& args) {
652 const int argCount = args.size();
653 if (argCount >= 2) {
654 if (args[1] == "update" || args[1] == "remove") {
655 bool good = false;
656 int uid = -1;
657 string name;
658
659 if (argCount == 3) {
660 // Automatically pick the UID
661 uid = AIBinder_getCallingUid();
662 name.assign(args[2].c_str(), args[2].size());
663 good = true;
664 } else if (argCount == 4) {
665 good = getUidFromArgs(args, 2, uid);
666 if (!good) {
667 dprintf(err, "Invalid UID. Note that the config can only be set for "
668 "other UIDs on eng or userdebug builds.\n");
669 }
670 name.assign(args[3].c_str(), args[3].size());
671 } else if (argCount == 2 && args[1] == "remove") {
672 good = true;
673 }
674
675 if (!good) {
676 // If arg parsing failed, print the help text and return an error.
677 print_cmd_help(out);
678 return UNKNOWN_ERROR;
679 }
680
681 if (args[1] == "update") {
682 char* endp;
683 int64_t configID = strtoll(name.c_str(), &endp, 10);
684 if (endp == name.c_str() || *endp != '\0') {
685 dprintf(err, "Error parsing config ID.\n");
686 return UNKNOWN_ERROR;
687 }
688
689 // Read stream into buffer.
690 string buffer;
691 if (!android::base::ReadFdToString(in, &buffer)) {
692 dprintf(err, "Error reading stream for StatsConfig.\n");
693 return UNKNOWN_ERROR;
694 }
695
696 // Parse buffer.
697 StatsdConfig config;
698 if (!config.ParseFromString(buffer)) {
699 dprintf(err, "Error parsing proto stream for StatsConfig.\n");
700 return UNKNOWN_ERROR;
701 }
702
703 // Add / update the config.
704 mConfigManager->UpdateConfig(ConfigKey(uid, configID), config);
705 } else {
706 if (argCount == 2) {
707 cmd_remove_all_configs(out);
708 } else {
709 // Remove the config.
710 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
711 }
712 }
713
714 return NO_ERROR;
715 }
716 }
717 print_cmd_help(out);
718 return UNKNOWN_ERROR;
719 }
720
cmd_dump_report(int out,const Vector<String8> & args)721 status_t StatsService::cmd_dump_report(int out, const Vector<String8>& args) {
722 if (mProcessor != nullptr) {
723 int argCount = args.size();
724 bool good = false;
725 bool proto = false;
726 bool includeCurrentBucket = false;
727 bool eraseData = true;
728 int uid;
729 string name;
730 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
731 proto = true;
732 argCount -= 1;
733 }
734 if (!std::strcmp("--include_current_bucket", args[argCount-1].c_str())) {
735 includeCurrentBucket = true;
736 argCount -= 1;
737 }
738 if (!std::strcmp("--keep_data", args[argCount-1].c_str())) {
739 eraseData = false;
740 argCount -= 1;
741 }
742 if (argCount == 2) {
743 // Automatically pick the UID
744 uid = AIBinder_getCallingUid();
745 name.assign(args[1].c_str(), args[1].size());
746 good = true;
747 } else if (argCount == 3) {
748 good = getUidFromArgs(args, 1, uid);
749 if (!good) {
750 dprintf(out, "Invalid UID. Note that the metrics can only be dumped for "
751 "other UIDs on eng or userdebug builds.\n");
752 }
753 name.assign(args[2].c_str(), args[2].size());
754 }
755 if (good) {
756 vector<uint8_t> data;
757 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(),
758 getWallClockNs(), includeCurrentBucket, eraseData, ADB_DUMP,
759 NO_TIME_CONSTRAINTS, &data);
760 if (proto) {
761 for (size_t i = 0; i < data.size(); i ++) {
762 dprintf(out, "%c", data[i]);
763 }
764 } else {
765 dprintf(out, "Non-proto stats data dump not currently supported.\n");
766 }
767 return android::OK;
768 } else {
769 // If arg parsing failed, print the help text and return an error.
770 print_cmd_help(out);
771 return UNKNOWN_ERROR;
772 }
773 } else {
774 dprintf(out, "Log processor does not exist...\n");
775 return UNKNOWN_ERROR;
776 }
777 }
778
cmd_print_stats(int out,const Vector<String8> & args)779 status_t StatsService::cmd_print_stats(int out, const Vector<String8>& args) {
780 int argCount = args.size();
781 bool proto = false;
782 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
783 proto = true;
784 argCount -= 1;
785 }
786 StatsdStats& statsdStats = StatsdStats::getInstance();
787 if (proto) {
788 vector<uint8_t> data;
789 statsdStats.dumpStats(&data, false); // does not reset statsdStats.
790 for (size_t i = 0; i < data.size(); i ++) {
791 dprintf(out, "%c", data[i]);
792 }
793
794 } else {
795 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
796 for (const ConfigKey& key : configs) {
797 dprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
798 mProcessor->GetMetricsSize(key));
799 }
800 statsdStats.dumpStats(out);
801 }
802 return NO_ERROR;
803 }
804
cmd_print_uid_map(int out,const Vector<String8> & args)805 status_t StatsService::cmd_print_uid_map(int out, const Vector<String8>& args) {
806 if (args.size() > 1) {
807 if (!std::strcmp("--with_certificate_hash", args[1].c_str())) {
808 mUidMap->printUidMap(out, /* includeCertificateHash */ true);
809 } else {
810 string pkg;
811 pkg.assign(args[1].c_str(), args[1].size());
812 auto uids = mUidMap->getAppUid(pkg);
813 dprintf(out, "%s -> [ ", pkg.c_str());
814 for (const auto& uid : uids) {
815 dprintf(out, "%d ", uid);
816 }
817 dprintf(out, "]\n");
818 }
819 } else {
820 mUidMap->printUidMap(out, /* includeCertificateHash */ false);
821 }
822 return NO_ERROR;
823 }
824
cmd_write_data_to_disk(int out)825 status_t StatsService::cmd_write_data_to_disk(int out) {
826 dprintf(out, "Writing data to disk\n");
827 mProcessor->WriteDataToDisk(ADB_DUMP, NO_TIME_CONSTRAINTS, getElapsedRealtimeNs(),
828 getWallClockNs());
829 return NO_ERROR;
830 }
831
cmd_log_app_breadcrumb(int out,const Vector<String8> & args)832 status_t StatsService::cmd_log_app_breadcrumb(int out, const Vector<String8>& args) {
833 bool good = false;
834 int32_t uid;
835 int32_t label;
836 int32_t state;
837 const int argCount = args.size();
838 if (argCount == 3) {
839 // Automatically pick the UID
840 uid = AIBinder_getCallingUid();
841 label = atoi(args[1].c_str());
842 state = atoi(args[2].c_str());
843 good = true;
844 } else if (argCount == 4) {
845 good = getUidFromArgs(args, 1, uid);
846 if (!good) {
847 dprintf(out,
848 "Invalid UID. Note that selecting a UID for writing AppBreadcrumb can only be "
849 "done for other UIDs on eng or userdebug builds.\n");
850 }
851 label = atoi(args[2].c_str());
852 state = atoi(args[3].c_str());
853 }
854 if (good) {
855 dprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state);
856 util::stats_write(util::APP_BREADCRUMB_REPORTED, uid, label, state);
857 } else {
858 print_cmd_help(out);
859 return UNKNOWN_ERROR;
860 }
861 return NO_ERROR;
862 }
863
cmd_log_binary_push(int out,const Vector<String8> & args)864 status_t StatsService::cmd_log_binary_push(int out, const Vector<String8>& args) {
865 // Security checks are done in the sendBinaryPushStateChanged atom.
866 const int argCount = args.size();
867 if (argCount != 7 && argCount != 8) {
868 dprintf(out, "Incorrect number of argument supplied\n");
869 return UNKNOWN_ERROR;
870 }
871 string trainName = string(args[1].c_str());
872 int64_t trainVersion = strtoll(args[2].c_str(), nullptr, 10);
873 int32_t state = atoi(args[6].c_str());
874 vector<int64_t> experimentIds;
875 if (argCount == 8) {
876 vector<string> experimentIdsStrings = android::base::Split(string(args[7].c_str()), ",");
877 for (const string& experimentIdString : experimentIdsStrings) {
878 int64_t experimentId = strtoll(experimentIdString.c_str(), nullptr, 10);
879 experimentIds.push_back(experimentId);
880 }
881 }
882 dprintf(out, "Logging BinaryPushStateChanged\n");
883 vector<uint8_t> experimentIdBytes;
884 writeExperimentIdsToProto(experimentIds, &experimentIdBytes);
885 LogEvent event(trainName, trainVersion, args[3].c_str(), args[4].c_str(), args[5].c_str(),
886 state, experimentIdBytes, 0);
887 mProcessor->OnLogEvent(&event);
888 return NO_ERROR;
889 }
890
cmd_print_pulled_metrics(int out,const Vector<String8> & args)891 status_t StatsService::cmd_print_pulled_metrics(int out, const Vector<String8>& args) {
892 int s = atoi(args[1].c_str());
893 vector<int32_t> uids;
894 if (args.size() > 2) {
895 string package = string(args[2].c_str());
896 auto it = UidMap::sAidToUidMapping.find(package);
897 if (it != UidMap::sAidToUidMapping.end()) {
898 uids.push_back(it->second);
899 } else {
900 set<int32_t> uids_set = mUidMap->getAppUid(package);
901 uids.insert(uids.end(), uids_set.begin(), uids_set.end());
902 }
903 } else {
904 uids.push_back(AID_SYSTEM);
905 }
906 vector<shared_ptr<LogEvent>> stats;
907 if (mPullerManager->Pull(s, uids, getElapsedRealtimeNs(), &stats)) {
908 for (const auto& it : stats) {
909 dprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
910 }
911 dprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
912 return NO_ERROR;
913 }
914 return UNKNOWN_ERROR;
915 }
916
cmd_remove_all_configs(int out)917 status_t StatsService::cmd_remove_all_configs(int out) {
918 dprintf(out, "Removing all configs...\n");
919 VLOG("StatsService::cmd_remove_all_configs was called");
920 mConfigManager->RemoveAllConfigs();
921 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
922 return NO_ERROR;
923 }
924
cmd_dump_memory_info(int out)925 status_t StatsService::cmd_dump_memory_info(int out) {
926 dprintf(out, "meminfo not available.\n");
927 return NO_ERROR;
928 }
929
cmd_clear_puller_cache(int out)930 status_t StatsService::cmd_clear_puller_cache(int out) {
931 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i",
932 AIBinder_getCallingPid(), AIBinder_getCallingUid());
933 if (checkPermission(kPermissionDump)) {
934 int cleared = mPullerManager->ForceClearPullerCache();
935 dprintf(out, "Puller removed %d cached data!\n", cleared);
936 return NO_ERROR;
937 } else {
938 return PERMISSION_DENIED;
939 }
940 }
941
cmd_print_logs(int out,const Vector<String8> & args)942 status_t StatsService::cmd_print_logs(int out, const Vector<String8>& args) {
943 Status status = checkUid(AID_ROOT);
944 if (!status.isOk()) {
945 return PERMISSION_DENIED;
946 }
947
948 VLOG("StatsService::cmd_print_logs with pid %i, uid %i", AIBinder_getCallingPid(),
949 AIBinder_getCallingUid());
950 bool enabled = true;
951 if (args.size() >= 2) {
952 enabled = atoi(args[1].c_str()) != 0;
953 }
954 mProcessor->setPrintLogs(enabled);
955 return NO_ERROR;
956 }
957
getUidFromArgs(const Vector<String8> & args,size_t uidArgIndex,int32_t & uid)958 bool StatsService::getUidFromArgs(const Vector<String8>& args, size_t uidArgIndex, int32_t& uid) {
959 return getUidFromString(args[uidArgIndex].c_str(), uid);
960 }
961
getUidFromString(const char * s,int32_t & uid)962 bool StatsService::getUidFromString(const char* s, int32_t& uid) {
963 if (*s == '\0') {
964 return false;
965 }
966 char* endc = NULL;
967 int64_t longUid = strtol(s, &endc, 0);
968 if (*endc != '\0') {
969 return false;
970 }
971 int32_t goodUid = static_cast<int32_t>(longUid);
972 if (longUid < 0 || static_cast<uint64_t>(longUid) != static_cast<uid_t>(goodUid)) {
973 return false; // It was not of uid_t type.
974 }
975 uid = goodUid;
976
977 int32_t callingUid = AIBinder_getCallingUid();
978 return mEngBuild // UserDebug/EngBuild are allowed to impersonate uids.
979 || (callingUid == goodUid) // Anyone can 'impersonate' themselves.
980 || (callingUid == AID_ROOT && goodUid == AID_SHELL); // ROOT can impersonate SHELL.
981 }
982
informAllUidData(const ScopedFileDescriptor & fd)983 Status StatsService::informAllUidData(const ScopedFileDescriptor& fd) {
984 ATRACE_CALL();
985 ENFORCE_UID(AID_SYSTEM);
986
987 // Parse fd into proto.
988 UidData uidData;
989 if (!uidData.ParseFromFileDescriptor(fd.get())) {
990 return exception(EX_ILLEGAL_ARGUMENT, "Error parsing proto stream for UidData.");
991 }
992
993 mUidMap->updateMap(getElapsedRealtimeNs(), uidData);
994 mBootCompleteTrigger.markComplete(kUidMapReceivedTag);
995 VLOG("StatsService::informAllUidData UidData proto parsed successfully.");
996 return Status::ok();
997 }
998
informOnePackage(const string & app,int32_t uid,int64_t version,const string & versionString,const string & installer,const vector<uint8_t> & certificateHash)999 Status StatsService::informOnePackage(const string& app, int32_t uid, int64_t version,
1000 const string& versionString, const string& installer,
1001 const vector<uint8_t>& certificateHash) {
1002 ATRACE_CALL();
1003 ENFORCE_UID(AID_SYSTEM);
1004
1005 VLOG("StatsService::informOnePackage was called");
1006
1007 mUidMap->updateApp(getElapsedRealtimeNs(), app, uid, version, versionString, installer,
1008 certificateHash);
1009 return Status::ok();
1010 }
1011
informOnePackageRemoved(const string & app,int32_t uid)1012 Status StatsService::informOnePackageRemoved(const string& app, int32_t uid) {
1013 ATRACE_CALL();
1014 ENFORCE_UID(AID_SYSTEM);
1015
1016 VLOG("StatsService::informOnePackageRemoved was called");
1017 mUidMap->removeApp(getElapsedRealtimeNs(), app, uid);
1018 mConfigManager->RemoveConfigs(uid);
1019 return Status::ok();
1020 }
1021
informAnomalyAlarmFired()1022 Status StatsService::informAnomalyAlarmFired() {
1023 ENFORCE_UID(AID_SYSTEM);
1024 // Anomaly alarms are handled internally now. This code should be fully deleted.
1025 return Status::ok();
1026 }
1027
informAlarmForSubscriberTriggeringFired()1028 Status StatsService::informAlarmForSubscriberTriggeringFired() {
1029 ATRACE_CALL();
1030 ENFORCE_UID(AID_SYSTEM);
1031
1032 VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
1033 int64_t currentTimeSec = getElapsedRealtimeSec();
1034 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
1035 mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
1036 if (alarmSet.size() > 0) {
1037 VLOG("Found periodic alarm fired.");
1038 mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
1039 } else {
1040 ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
1041 }
1042 return Status::ok();
1043 }
1044
informPollAlarmFired()1045 Status StatsService::informPollAlarmFired() {
1046 ATRACE_CALL();
1047 ENFORCE_UID(AID_SYSTEM);
1048
1049 VLOG("StatsService::informPollAlarmFired was called");
1050 mProcessor->informPullAlarmFired(getElapsedRealtimeNs());
1051 VLOG("StatsService::informPollAlarmFired succeeded");
1052 return Status::ok();
1053 }
1054
systemRunning()1055 Status StatsService::systemRunning() {
1056 ATRACE_CALL();
1057 ENFORCE_UID(AID_SYSTEM);
1058
1059 // TODO(b/345534941): This function is never called. It should be deleted.
1060 return Status::ok();
1061 }
1062
informDeviceShutdown()1063 Status StatsService::informDeviceShutdown() {
1064 ATRACE_CALL();
1065 ENFORCE_UID(AID_SYSTEM);
1066 VLOG("StatsService::informDeviceShutdown");
1067 onStatsdInitCompletedHandlerTermination();
1068 int64_t elapsedRealtimeNs = getElapsedRealtimeNs();
1069 int64_t wallClockNs = getWallClockNs();
1070 mProcessor->WriteDataToDisk(DEVICE_SHUTDOWN, FAST, elapsedRealtimeNs, wallClockNs);
1071 mProcessor->SaveActiveConfigsToDisk(elapsedRealtimeNs);
1072 mProcessor->SaveMetadataToDisk(wallClockNs, elapsedRealtimeNs);
1073 return Status::ok();
1074 }
1075
sayHiToStatsCompanion()1076 void StatsService::sayHiToStatsCompanion() {
1077 shared_ptr<IStatsCompanionService> statsCompanion =
1078 getStatsCompanionService(/*blocking=*/false);
1079 if (statsCompanion != nullptr) {
1080 VLOG("Telling statsCompanion that statsd is ready");
1081 statsCompanion->statsdReady();
1082 } else {
1083 VLOG("Could not access statsCompanion");
1084 }
1085 }
1086
statsCompanionReady()1087 Status StatsService::statsCompanionReady() {
1088 ATRACE_CALL();
1089 ENFORCE_UID(AID_SYSTEM);
1090
1091 VLOG("StatsService::statsCompanionReady was called");
1092 shared_ptr<IStatsCompanionService> statsCompanion = getStatsCompanionService(/*blocking*/ true);
1093 if (statsCompanion == nullptr) {
1094 return exception(EX_NULL_POINTER,
1095 "StatsCompanion unavailable despite it contacting statsd.");
1096 }
1097 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
1098 AIBinder_linkToDeath(statsCompanion->asBinder().get(),
1099 mStatsCompanionServiceDeathRecipient.get(), this);
1100 mPullerManager->SetStatsCompanionService(statsCompanion);
1101 mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion);
1102 mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion);
1103 return Status::ok();
1104 }
1105
bootCompleted()1106 Status StatsService::bootCompleted() {
1107 ATRACE_CALL();
1108 ENFORCE_UID(AID_SYSTEM);
1109
1110 VLOG("StatsService::bootCompleted was called");
1111 mBootCompleteTrigger.markComplete(kBootCompleteTag);
1112 return Status::ok();
1113 }
1114
onStatsdInitCompleted(int initEventDelaySecs)1115 void StatsService::onStatsdInitCompleted(int initEventDelaySecs) {
1116 // The hard-coded delay is determined based on perfetto traces evaluation
1117 // for statsd during the boot.
1118 // The delay is required to properly process event storm which often has place
1119 // after device boot.
1120 // This function is called from a dedicated thread without holding locks, so sleeping is ok.
1121 // See MultiConditionTrigger::markComplete() executorThread for details
1122 // For more details see http://b/277958338
1123
1124 std::unique_lock<std::mutex> lk(mStatsdInitCompletedHandlerTerminationFlagMutex);
1125 if (mStatsdInitCompletedHandlerTerminationFlag.wait_for(
1126 lk, std::chrono::seconds(initEventDelaySecs),
1127 [this] { return mStatsdInitCompletedHandlerTerminationRequested; })) {
1128 VLOG("StatsService::onStatsdInitCompleted() Early termination is requested");
1129 return;
1130 }
1131
1132 mProcessor->onStatsdInitCompleted(getElapsedRealtimeNs());
1133 }
1134
Startup()1135 void StatsService::Startup() {
1136 ATRACE_CALL();
1137 mConfigManager->Startup();
1138 int64_t wallClockNs = getWallClockNs();
1139 int64_t elapsedRealtimeNs = getElapsedRealtimeNs();
1140 mProcessor->LoadActiveConfigsFromDisk();
1141 mProcessor->LoadMetadataFromDisk(wallClockNs, elapsedRealtimeNs);
1142 mProcessor->EnforceDataTtls(wallClockNs, elapsedRealtimeNs);
1143
1144 // Now that configs are initialized, begin reading logs
1145 if (mEventQueue != nullptr) {
1146 mLogsReaderThread = std::make_unique<std::thread>([this] { readLogs(); });
1147 if (mLogsReaderThread) {
1148 pthread_setname_np(mLogsReaderThread->native_handle(), "statsd.reader");
1149 }
1150 }
1151 }
1152
Terminate()1153 void StatsService::Terminate() {
1154 ATRACE_CALL();
1155 ALOGI("StatsService::Terminating");
1156 onStatsdInitCompletedHandlerTermination();
1157 if (mProcessor != nullptr) {
1158 int64_t elapsedRealtimeNs = getElapsedRealtimeNs();
1159 int64_t wallClockNs = getWallClockNs();
1160 mProcessor->WriteDataToDisk(TERMINATION_SIGNAL_RECEIVED, FAST, elapsedRealtimeNs,
1161 wallClockNs);
1162 mProcessor->SaveActiveConfigsToDisk(elapsedRealtimeNs);
1163 mProcessor->SaveMetadataToDisk(wallClockNs, elapsedRealtimeNs);
1164 }
1165 }
1166
onStatsdInitCompletedHandlerTermination()1167 void StatsService::onStatsdInitCompletedHandlerTermination() {
1168 {
1169 std::unique_lock<std::mutex> lk(mStatsdInitCompletedHandlerTerminationFlagMutex);
1170 mStatsdInitCompletedHandlerTerminationRequested = true;
1171 }
1172 mStatsdInitCompletedHandlerTerminationFlag.notify_all();
1173 }
1174
1175 // Test only interface!!!
OnLogEvent(LogEvent * event)1176 void StatsService::OnLogEvent(LogEvent* event) {
1177 mProcessor->OnLogEvent(event);
1178 if (mShellSubscriber != nullptr) {
1179 mShellSubscriber->onLogEvent(*event);
1180 }
1181 }
1182
getData(int64_t key,const int32_t callingUid,vector<uint8_t> * output)1183 Status StatsService::getData(int64_t key, const int32_t callingUid, vector<uint8_t>* output) {
1184 ATRACE_CALL();
1185 ENFORCE_UID(AID_SYSTEM);
1186 getDataChecked(key, callingUid, output);
1187 return Status::ok();
1188 }
1189
getDataFd(int64_t key,const int32_t callingUid,const ScopedFileDescriptor & fd)1190 Status StatsService::getDataFd(int64_t key, const int32_t callingUid,
1191 const ScopedFileDescriptor& fd) {
1192 ATRACE_CALL();
1193 ENFORCE_UID(AID_SYSTEM);
1194 vector<uint8_t> reportData;
1195 getDataChecked(key, callingUid, &reportData);
1196
1197 if (reportData.size() >= std::numeric_limits<int32_t>::max()) {
1198 ALOGE("Report size is infeasible big and can not be returned");
1199 return exception(EX_ILLEGAL_STATE, "Report size is infeasible big.");
1200 }
1201
1202 const uint32_t bytesToWrite = static_cast<uint32_t>(reportData.size());
1203 VLOG("StatsService::getDataFd report size %d", bytesToWrite);
1204
1205 // write 4 bytes of report size for correct buffer allocation
1206 const uint32_t bytesToWriteBE = htonl(bytesToWrite);
1207 if (!android::base::WriteFully(fd.get(), &bytesToWriteBE, sizeof(uint32_t))) {
1208 return exception(EX_ILLEGAL_STATE, "Failed to write report data size to file descriptor");
1209 }
1210 if (!android::base::WriteFully(fd.get(), reportData.data(), reportData.size())) {
1211 return exception(EX_ILLEGAL_STATE, "Failed to write report data to file descriptor");
1212 }
1213
1214 VLOG("StatsService::getDataFd written");
1215 return Status::ok();
1216 }
1217
getDataChecked(int64_t key,const int32_t callingUid,vector<uint8_t> * output)1218 void StatsService::getDataChecked(int64_t key, const int32_t callingUid, vector<uint8_t>* output) {
1219 VLOG("StatsService::getData with Uid %i", callingUid);
1220 ConfigKey configKey(callingUid, key);
1221 // The dump latency does not matter here since we do not include the current bucket, we do not
1222 // need to pull any new data anyhow.
1223 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), getWallClockNs(),
1224 false /* include_current_bucket*/, true /* erase_data */,
1225 GET_DATA_CALLED, FAST, output);
1226 }
1227
getMetadata(vector<uint8_t> * output)1228 Status StatsService::getMetadata(vector<uint8_t>* output) {
1229 ATRACE_CALL();
1230 ENFORCE_UID(AID_SYSTEM);
1231
1232 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
1233 return Status::ok();
1234 }
1235
addConfiguration(int64_t key,const vector<uint8_t> & config,const int32_t callingUid)1236 Status StatsService::addConfiguration(int64_t key, const vector <uint8_t>& config,
1237 const int32_t callingUid) {
1238 ATRACE_CALL();
1239 ENFORCE_UID(AID_SYSTEM);
1240
1241 if (addConfigurationChecked(callingUid, key, config)) {
1242 return Status::ok();
1243 } else {
1244 return exception(EX_ILLEGAL_ARGUMENT, "Could not parse malformatted StatsdConfig.");
1245 }
1246 }
1247
addConfigurationChecked(int uid,int64_t key,const vector<uint8_t> & config)1248 bool StatsService::addConfigurationChecked(int uid, int64_t key, const vector<uint8_t>& config) {
1249 ConfigKey configKey(uid, key);
1250 StatsdConfig cfg;
1251 if (config.size() > 0) { // If the config is empty, skip parsing.
1252 if (!cfg.ParseFromArray(&config[0], config.size())) {
1253 return false;
1254 }
1255 }
1256 mConfigManager->UpdateConfig(configKey, cfg);
1257 return true;
1258 }
1259
removeDataFetchOperation(int64_t key,const int32_t callingUid)1260 Status StatsService::removeDataFetchOperation(int64_t key,
1261 const int32_t callingUid) {
1262 ATRACE_CALL();
1263 ENFORCE_UID(AID_SYSTEM);
1264 ConfigKey configKey(callingUid, key);
1265 mConfigManager->RemoveConfigReceiver(configKey);
1266 return Status::ok();
1267 }
1268
setDataFetchOperation(int64_t key,const shared_ptr<IPendingIntentRef> & pir,const int32_t callingUid)1269 Status StatsService::setDataFetchOperation(int64_t key,
1270 const shared_ptr<IPendingIntentRef>& pir,
1271 const int32_t callingUid) {
1272 ATRACE_CALL();
1273 ENFORCE_UID(AID_SYSTEM);
1274
1275 ConfigKey configKey(callingUid, key);
1276 mConfigManager->SetConfigReceiver(configKey, pir);
1277 if (StorageManager::hasConfigMetricsReport(configKey)) {
1278 VLOG("StatsService::setDataFetchOperation marking configKey %s to dump reports on disk",
1279 configKey.ToString().c_str());
1280 mProcessor->noteOnDiskData(configKey);
1281 }
1282 return Status::ok();
1283 }
1284
setActiveConfigsChangedOperation(const shared_ptr<IPendingIntentRef> & pir,const int32_t callingUid,vector<int64_t> * output)1285 Status StatsService::setActiveConfigsChangedOperation(const shared_ptr<IPendingIntentRef>& pir,
1286 const int32_t callingUid,
1287 vector<int64_t>* output) {
1288 ATRACE_CALL();
1289 ENFORCE_UID(AID_SYSTEM);
1290
1291 mConfigManager->SetActiveConfigsChangedReceiver(callingUid, pir);
1292 if (output != nullptr) {
1293 mProcessor->GetActiveConfigs(callingUid, *output);
1294 } else {
1295 ALOGW("StatsService::setActiveConfigsChanged output was nullptr");
1296 }
1297 return Status::ok();
1298 }
1299
removeActiveConfigsChangedOperation(const int32_t callingUid)1300 Status StatsService::removeActiveConfigsChangedOperation(const int32_t callingUid) {
1301 ATRACE_CALL();
1302 ENFORCE_UID(AID_SYSTEM);
1303
1304 mConfigManager->RemoveActiveConfigsChangedReceiver(callingUid);
1305 return Status::ok();
1306 }
1307
removeConfiguration(int64_t key,const int32_t callingUid)1308 Status StatsService::removeConfiguration(int64_t key, const int32_t callingUid) {
1309 ATRACE_CALL();
1310 ENFORCE_UID(AID_SYSTEM);
1311
1312 ConfigKey configKey(callingUid, key);
1313 mConfigManager->RemoveConfig(configKey);
1314 return Status::ok();
1315 }
1316
setBroadcastSubscriber(int64_t configId,int64_t subscriberId,const shared_ptr<IPendingIntentRef> & pir,const int32_t callingUid)1317 Status StatsService::setBroadcastSubscriber(int64_t configId,
1318 int64_t subscriberId,
1319 const shared_ptr<IPendingIntentRef>& pir,
1320 const int32_t callingUid) {
1321 ATRACE_CALL();
1322 VLOG("StatsService::setBroadcastSubscriber called.");
1323 ENFORCE_UID(AID_SYSTEM);
1324
1325 if (pir == nullptr) {
1326 return exception(EX_NULL_POINTER,
1327 "setBroadcastSubscriber provided with null PendingIntentRef");
1328 }
1329
1330 ConfigKey configKey(callingUid, configId);
1331 SubscriberReporter::getInstance()
1332 .setBroadcastSubscriber(configKey, subscriberId, pir);
1333 return Status::ok();
1334 }
1335
unsetBroadcastSubscriber(int64_t configId,int64_t subscriberId,const int32_t callingUid)1336 Status StatsService::unsetBroadcastSubscriber(int64_t configId,
1337 int64_t subscriberId,
1338 const int32_t callingUid) {
1339 ATRACE_CALL();
1340 ENFORCE_UID(AID_SYSTEM);
1341
1342 VLOG("StatsService::unsetBroadcastSubscriber called.");
1343 ConfigKey configKey(callingUid, configId);
1344 SubscriberReporter::getInstance()
1345 .unsetBroadcastSubscriber(configKey, subscriberId);
1346 return Status::ok();
1347 }
1348
allPullersFromBootRegistered()1349 Status StatsService::allPullersFromBootRegistered() {
1350 ATRACE_CALL();
1351 ENFORCE_UID(AID_SYSTEM);
1352
1353 VLOG("StatsService::allPullersFromBootRegistered was called");
1354 mBootCompleteTrigger.markComplete(kAllPullersRegisteredTag);
1355 return Status::ok();
1356 }
1357
registerPullAtomCallback(int32_t uid,int32_t atomTag,int64_t coolDownMillis,int64_t timeoutMillis,const std::vector<int32_t> & additiveFields,const shared_ptr<IPullAtomCallback> & pullerCallback)1358 Status StatsService::registerPullAtomCallback(int32_t uid, int32_t atomTag, int64_t coolDownMillis,
1359 int64_t timeoutMillis,
1360 const std::vector<int32_t>& additiveFields,
1361 const shared_ptr<IPullAtomCallback>& pullerCallback) {
1362 ATRACE_CALL();
1363 ENFORCE_UID(AID_SYSTEM);
1364 VLOG("StatsService::registerPullAtomCallback called.");
1365 mPullerManager->RegisterPullAtomCallback(uid, atomTag, MillisToNano(coolDownMillis),
1366 MillisToNano(timeoutMillis), additiveFields,
1367 pullerCallback);
1368 return Status::ok();
1369 }
1370
registerNativePullAtomCallback(int32_t atomTag,int64_t coolDownMillis,int64_t timeoutMillis,const std::vector<int32_t> & additiveFields,const shared_ptr<IPullAtomCallback> & pullerCallback)1371 Status StatsService::registerNativePullAtomCallback(
1372 int32_t atomTag, int64_t coolDownMillis, int64_t timeoutMillis,
1373 const std::vector<int32_t>& additiveFields,
1374 const shared_ptr<IPullAtomCallback>& pullerCallback) {
1375 ATRACE_CALL();
1376 if (!checkPermission(kPermissionRegisterPullAtom)) {
1377 return exception(
1378 EX_SECURITY,
1379 StringPrintf("Uid %d does not have the %s permission when registering atom %d",
1380 AIBinder_getCallingUid(), kPermissionRegisterPullAtom, atomTag));
1381 }
1382 VLOG("StatsService::registerNativePullAtomCallback called.");
1383 int32_t uid = AIBinder_getCallingUid();
1384 mPullerManager->RegisterPullAtomCallback(uid, atomTag, MillisToNano(coolDownMillis),
1385 MillisToNano(timeoutMillis), additiveFields,
1386 pullerCallback);
1387 return Status::ok();
1388 }
1389
unregisterPullAtomCallback(int32_t uid,int32_t atomTag)1390 Status StatsService::unregisterPullAtomCallback(int32_t uid, int32_t atomTag) {
1391 ATRACE_CALL();
1392 ENFORCE_UID(AID_SYSTEM);
1393 VLOG("StatsService::unregisterPullAtomCallback called.");
1394 mPullerManager->UnregisterPullAtomCallback(uid, atomTag);
1395 return Status::ok();
1396 }
1397
unregisterNativePullAtomCallback(int32_t atomTag)1398 Status StatsService::unregisterNativePullAtomCallback(int32_t atomTag) {
1399 ATRACE_CALL();
1400 if (!checkPermission(kPermissionRegisterPullAtom)) {
1401 return exception(
1402 EX_SECURITY,
1403 StringPrintf("Uid %d does not have the %s permission when unregistering atom %d",
1404 AIBinder_getCallingUid(), kPermissionRegisterPullAtom, atomTag));
1405 }
1406 VLOG("StatsService::unregisterNativePullAtomCallback called.");
1407 int32_t uid = AIBinder_getCallingUid();
1408 mPullerManager->UnregisterPullAtomCallback(uid, atomTag);
1409 return Status::ok();
1410 }
1411
getRegisteredExperimentIds(std::vector<int64_t> * experimentIdsOut)1412 Status StatsService::getRegisteredExperimentIds(std::vector<int64_t>* experimentIdsOut) {
1413 ATRACE_CALL();
1414 ENFORCE_UID(AID_SYSTEM);
1415 // TODO: add verifier permission
1416
1417 experimentIdsOut->clear();
1418 // Read the latest train info
1419 vector<InstallTrainInfo> trainInfoList = StorageManager::readAllTrainInfo();
1420 if (trainInfoList.empty()) {
1421 // No train info means no experiment IDs, return an empty list
1422 return Status::ok();
1423 }
1424
1425 // Copy the experiment IDs to the out vector
1426 for (InstallTrainInfo& trainInfo : trainInfoList) {
1427 experimentIdsOut->insert(experimentIdsOut->end(),
1428 trainInfo.experimentIds.begin(),
1429 trainInfo.experimentIds.end());
1430 }
1431 return Status::ok();
1432 }
1433
updateProperties(const vector<PropertyParcel> & properties)1434 Status StatsService::updateProperties(const vector<PropertyParcel>& properties) {
1435 ENFORCE_UID(AID_SYSTEM);
1436
1437 // TODO(b/281765292): Forward statsd_java properties received here to FlagProvider.
1438
1439 return Status::ok();
1440 }
1441
statsCompanionServiceDied(void * cookie)1442 void StatsService::statsCompanionServiceDied(void* cookie) {
1443 ATRACE_CALL();
1444 auto thiz = static_cast<StatsService*>(cookie);
1445 thiz->statsCompanionServiceDiedImpl();
1446 }
1447
statsCompanionServiceDiedImpl()1448 void StatsService::statsCompanionServiceDiedImpl() {
1449 ALOGW("statscompanion service died");
1450 StatsdStats::getInstance().noteSystemServerRestart(getWallClockSec());
1451 onStatsdInitCompletedHandlerTermination();
1452 if (mProcessor != nullptr) {
1453 ALOGW("Reset statsd upon system server restarts.");
1454 int64_t systemServerRestartNs = getElapsedRealtimeNs();
1455 int64_t wallClockNs = getWallClockNs();
1456 ProtoOutputStream activeConfigsProto;
1457 mProcessor->WriteActiveConfigsToProtoOutputStream(systemServerRestartNs,
1458 STATSCOMPANION_DIED, &activeConfigsProto);
1459 metadata::StatsMetadataList metadataList;
1460 mProcessor->WriteMetadataToProto(wallClockNs, systemServerRestartNs, &metadataList);
1461 mProcessor->WriteDataToDisk(STATSCOMPANION_DIED, FAST, systemServerRestartNs, wallClockNs);
1462 mProcessor->resetConfigs();
1463
1464 std::string serializedActiveConfigs;
1465 if (activeConfigsProto.serializeToString(&serializedActiveConfigs)) {
1466 ActiveConfigList activeConfigs;
1467 if (activeConfigs.ParseFromString(serializedActiveConfigs)) {
1468 mProcessor->SetConfigsActiveState(activeConfigs, systemServerRestartNs);
1469 }
1470 }
1471 mProcessor->SetMetadataState(metadataList, wallClockNs, systemServerRestartNs);
1472 }
1473 mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
1474 mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
1475 mPullerManager->SetStatsCompanionService(nullptr);
1476 }
1477
setRestrictedMetricsChangedOperation(const int64_t configId,const string & configPackage,const shared_ptr<IPendingIntentRef> & pir,const int32_t callingUid,vector<int64_t> * output)1478 Status StatsService::setRestrictedMetricsChangedOperation(const int64_t configId,
1479 const string& configPackage,
1480 const shared_ptr<IPendingIntentRef>& pir,
1481 const int32_t callingUid,
1482 vector<int64_t>* output) {
1483 ATRACE_CALL();
1484 ENFORCE_UID(AID_SYSTEM);
1485 if (!isAtLeastU()) {
1486 ALOGW("setRestrictedMetricsChangedOperation invoked on U- device");
1487 return Status::ok();
1488 }
1489 mConfigManager->SetRestrictedMetricsChangedReceiver(configPackage, configId, callingUid, pir);
1490 if (output != nullptr) {
1491 mProcessor->fillRestrictedMetrics(configId, configPackage, callingUid, output);
1492 } else {
1493 ALOGW("StatsService::setRestrictedMetricsChangedOperation output was nullptr");
1494 }
1495 return Status::ok();
1496 }
1497
removeRestrictedMetricsChangedOperation(const int64_t configId,const string & configPackage,const int32_t callingUid)1498 Status StatsService::removeRestrictedMetricsChangedOperation(const int64_t configId,
1499 const string& configPackage,
1500 const int32_t callingUid) {
1501 ATRACE_CALL();
1502 ENFORCE_UID(AID_SYSTEM);
1503 if (!isAtLeastU()) {
1504 ALOGW("removeRestrictedMetricsChangedOperation invoked on U- device");
1505 return Status::ok();
1506 }
1507 mConfigManager->RemoveRestrictedMetricsChangedReceiver(configPackage, configId, callingUid);
1508 return Status::ok();
1509 }
1510
querySql(const string & sqlQuery,const int32_t minSqlClientVersion,const optional<vector<uint8_t>> & policyConfig,const shared_ptr<IStatsQueryCallback> & callback,const int64_t configKey,const string & configPackage,const int32_t callingUid)1511 Status StatsService::querySql(const string& sqlQuery, const int32_t minSqlClientVersion,
1512 const optional<vector<uint8_t>>& policyConfig,
1513 const shared_ptr<IStatsQueryCallback>& callback,
1514 const int64_t configKey, const string& configPackage,
1515 const int32_t callingUid) {
1516 ATRACE_CALL();
1517 ENFORCE_UID(AID_SYSTEM);
1518 if (callback == nullptr) {
1519 ALOGW("querySql called with null callback.");
1520 StatsdStats::getInstance().noteQueryRestrictedMetricFailed(
1521 configKey, configPackage, std::nullopt, callingUid,
1522 InvalidQueryReason(NULL_CALLBACK));
1523 return Status::ok();
1524 }
1525 mProcessor->querySql(sqlQuery, minSqlClientVersion, policyConfig, callback, configKey,
1526 configPackage, callingUid);
1527 return Status::ok();
1528 }
1529
addSubscription(const vector<uint8_t> & subscriptionConfig,const shared_ptr<IStatsSubscriptionCallback> & callback)1530 Status StatsService::addSubscription(const vector<uint8_t>& subscriptionConfig,
1531 const shared_ptr<IStatsSubscriptionCallback>& callback) {
1532 ATRACE_CALL();
1533 ENFORCE_SID(kTracedProbesSid);
1534
1535 initShellSubscriber();
1536
1537 mShellSubscriber->startNewSubscription(subscriptionConfig, callback);
1538 return Status::ok();
1539 }
1540
removeSubscription(const shared_ptr<IStatsSubscriptionCallback> & callback)1541 Status StatsService::removeSubscription(const shared_ptr<IStatsSubscriptionCallback>& callback) {
1542 ATRACE_CALL();
1543 ENFORCE_SID(kTracedProbesSid);
1544
1545 if (mShellSubscriber != nullptr) {
1546 mShellSubscriber->unsubscribe(callback);
1547 }
1548 return Status::ok();
1549 }
1550
flushSubscription(const shared_ptr<IStatsSubscriptionCallback> & callback)1551 Status StatsService::flushSubscription(const shared_ptr<IStatsSubscriptionCallback>& callback) {
1552 ATRACE_CALL();
1553 ENFORCE_SID(kTracedProbesSid);
1554
1555 if (mShellSubscriber != nullptr) {
1556 mShellSubscriber->flushSubscription(callback);
1557 }
1558 return Status::ok();
1559 }
1560
initShellSubscriber()1561 void StatsService::initShellSubscriber() {
1562 std::lock_guard<std::mutex> lock(mShellSubscriberMutex);
1563 if (mShellSubscriber == nullptr) {
1564 mShellSubscriber = new ShellSubscriber(mUidMap, mPullerManager, mLogEventFilter);
1565 }
1566 }
1567
stopReadingLogs()1568 void StatsService::stopReadingLogs() {
1569 mIsStopRequested = true;
1570 // Push this event so that readLogs will process and break out of the loop
1571 // after the stop is requested.
1572 std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
1573 mEventQueue->push(std::move(logEvent));
1574 }
1575
1576 } // namespace statsd
1577 } // namespace os
1578 } // namespace android
1579