Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
yro | 0feae94 | 2017-11-15 14:38:48 -0800 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 19 | |
| 20 | #include "StatsService.h" |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 21 | #include "stats_log_util.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 23 | #include "config/ConfigKey.h" |
| 24 | #include "config/ConfigManager.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | #include "guardrail/StatsdStats.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 26 | #include "storage/StorageManager.h" |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 27 | #include "subscriber/SubscriberReporter.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 28 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 30 | #include <android-base/strings.h> |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 31 | #include <cutils/multiuser.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 32 | #include <frameworks/base/cmds/statsd/src/statsd_config.pb.h> |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 33 | #include <frameworks/base/cmds/statsd/src/uid_data.pb.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 34 | #include <private/android_filesystem_config.h> |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 35 | #include <statslog_statsd.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 36 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 37 | #include <stdlib.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 38 | #include <sys/system_properties.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 39 | #include <unistd.h> |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 40 | #include <utils/String16.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 41 | |
| 42 | using namespace android; |
| 43 | |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 44 | using android::base::StringPrintf; |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 45 | using android::util::FIELD_COUNT_REPEATED; |
| 46 | using android::util::FIELD_TYPE_MESSAGE; |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 47 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 48 | using Status = ::ndk::ScopedAStatus; |
| 49 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 50 | namespace android { |
| 51 | namespace os { |
| 52 | namespace statsd { |
| 53 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 54 | constexpr const char* kPermissionDump = "android.permission.DUMP"; |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 55 | |
Tej Singh | 10458ec | 2020-03-17 11:04:02 -0700 | [diff] [blame] | 56 | constexpr const char* kPermissionRegisterPullAtom = "android.permission.REGISTER_STATS_PULL_ATOM"; |
| 57 | |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 58 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 59 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 60 | // for StatsDataDumpProto |
| 61 | const int FIELD_ID_REPORTS_LIST = 1; |
| 62 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 63 | static Status exception(int32_t code, const std::string& msg) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 64 | ALOGE("%s (%d)", msg.c_str(), code); |
Tej Singh | 10458ec | 2020-03-17 11:04:02 -0700 | [diff] [blame] | 65 | return Status::fromExceptionCodeWithMessage(code, msg.c_str()); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 68 | static bool checkPermission(const char* permission) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 69 | pid_t pid = AIBinder_getCallingPid(); |
| 70 | uid_t uid = AIBinder_getCallingUid(); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 71 | return checkPermissionForIds(permission, pid, uid); |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 74 | Status checkUid(uid_t expectedUid) { |
| 75 | uid_t uid = AIBinder_getCallingUid(); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 76 | if (uid == expectedUid || uid == AID_ROOT) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 77 | return Status::ok(); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 78 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 79 | return exception(EX_SECURITY, |
| 80 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | #define ENFORCE_UID(uid) { \ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 85 | Status status = checkUid((uid)); \ |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 86 | if (!status.isOk()) { \ |
| 87 | return status; \ |
| 88 | } \ |
| 89 | } |
| 90 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 91 | StatsService::StatsService(const sp<Looper>& handlerLooper, shared_ptr<LogEventQueue> queue) |
| 92 | : mAnomalyAlarmMonitor(new AlarmMonitor( |
| 93 | MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 94 | [](const shared_ptr<IStatsCompanionService>& sc, int64_t timeMillis) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 95 | if (sc != nullptr) { |
| 96 | sc->setAnomalyAlarm(timeMillis); |
| 97 | StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged(); |
| 98 | } |
| 99 | }, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 100 | [](const shared_ptr<IStatsCompanionService>& sc) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 101 | if (sc != nullptr) { |
| 102 | sc->cancelAnomalyAlarm(); |
| 103 | StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged(); |
| 104 | } |
| 105 | })), |
| 106 | mPeriodicAlarmMonitor(new AlarmMonitor( |
| 107 | MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 108 | [](const shared_ptr<IStatsCompanionService>& sc, int64_t timeMillis) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 109 | if (sc != nullptr) { |
| 110 | sc->setAlarmForSubscriberTriggering(timeMillis); |
| 111 | StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged(); |
| 112 | } |
| 113 | }, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 114 | [](const shared_ptr<IStatsCompanionService>& sc) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 115 | if (sc != nullptr) { |
| 116 | sc->cancelAlarmForSubscriberTriggering(); |
| 117 | StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged(); |
| 118 | } |
| 119 | })), |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 120 | mEventQueue(queue), |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame^] | 121 | mBootCompleteTrigger({kBootCompleteTag, kUidMapReceivedTag, kAllPullersRegisteredTag}, |
| 122 | [this]() { mProcessor->onStatsdInitCompleted(getElapsedRealtimeNs()); }), |
Tej Singh | 769f35f | 2020-04-11 03:39:12 -0700 | [diff] [blame] | 123 | mStatsCompanionServiceDeathRecipient( |
| 124 | AIBinder_DeathRecipient_new(StatsService::statsCompanionServiceDied)) { |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 125 | mUidMap = UidMap::getInstance(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 126 | mPullerManager = new StatsPullerManager(); |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 127 | StatsPuller::SetUidMap(mUidMap); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 128 | mConfigManager = new ConfigManager(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 129 | mProcessor = new StatsLogProcessor( |
| 130 | mUidMap, mPullerManager, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor, |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 131 | getElapsedRealtimeNs(), |
| 132 | [this](const ConfigKey& key) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 133 | shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetConfigReceiver(key); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 134 | if (receiver == nullptr) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 135 | VLOG("Could not find a broadcast receiver for %s", key.ToString().c_str()); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 136 | return false; |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 137 | } else if (receiver->sendDataBroadcast( |
| 138 | mProcessor->getLastReportTimeNs(key)).isOk()) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 139 | return true; |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 140 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 141 | VLOG("Failed to send a broadcast for receiver %s", key.ToString().c_str()); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 142 | return false; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 143 | } |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 144 | }, |
| 145 | [this](const int& uid, const vector<int64_t>& activeConfigs) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 146 | shared_ptr<IPendingIntentRef> receiver = |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 147 | mConfigManager->GetActiveConfigsChangedReceiver(uid); |
| 148 | if (receiver == nullptr) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 149 | VLOG("Could not find receiver for uid %d", uid); |
| 150 | return false; |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 151 | } else if (receiver->sendActiveConfigsChangedBroadcast(activeConfigs).isOk()) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 152 | VLOG("StatsService::active configs broadcast succeeded for uid %d" , uid); |
| 153 | return true; |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 154 | } else { |
| 155 | VLOG("StatsService::active configs broadcast failed for uid %d" , uid); |
| 156 | return false; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 157 | } |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 158 | }); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 159 | |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 160 | mUidMap->setListener(mProcessor); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 161 | mConfigManager->AddListener(mProcessor); |
| 162 | |
| 163 | init_system_properties(); |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 164 | |
| 165 | if (mEventQueue != nullptr) { |
| 166 | std::thread pushedEventThread([this] { readLogs(); }); |
| 167 | pushedEventThread.detach(); |
| 168 | } |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 171 | StatsService::~StatsService() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 174 | /* Runs on a dedicated thread to process pushed events. */ |
| 175 | void StatsService::readLogs() { |
| 176 | // Read forever..... long live statsd |
| 177 | while (1) { |
| 178 | // Block until an event is available. |
| 179 | auto event = mEventQueue->waitPop(); |
| 180 | // Pass it to StatsLogProcess to all configs/metrics |
| 181 | // At this point, the LogEventQueue is not blocked, so that the socketListener |
| 182 | // can read events from the socket and write to buffer to avoid data drop. |
| 183 | mProcessor->OnLogEvent(event.get()); |
| 184 | // The ShellSubscriber is only used by shell for local debugging. |
| 185 | if (mShellSubscriber != nullptr) { |
| 186 | mShellSubscriber->onLogEvent(*event); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 191 | void StatsService::init_system_properties() { |
| 192 | mEngBuild = false; |
| 193 | const prop_info* buildType = __system_property_find("ro.build.type"); |
| 194 | if (buildType != NULL) { |
| 195 | __system_property_read_callback(buildType, init_build_type_callback, this); |
| 196 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 199 | void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value, |
| 200 | uint32_t serial) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 201 | if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 202 | reinterpret_cast<StatsService*>(cookie)->mEngBuild = true; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 207 | * Write data from statsd. |
| 208 | * Format for statsdStats: adb shell dumpsys stats --metadata [-v] [--proto] |
| 209 | * Format for data report: adb shell dumpsys stats [anything other than --metadata] [--proto] |
| 210 | * Anything ending in --proto will be in proto format. |
| 211 | * Anything without --metadata as the first argument will be report information. |
| 212 | * (bugreports call "adb shell dumpsys stats --dump-priority NORMAL -a --proto") |
| 213 | * TODO: Come up with a more robust method of enacting <serviceutils/PriorityDumper.h>. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 214 | */ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 215 | status_t StatsService::dump(int fd, const char** args, uint32_t numArgs) { |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 216 | if (!checkPermission(kPermissionDump)) { |
Tej Singh | dd83d70 | 2018-04-10 17:24:50 -0700 | [diff] [blame] | 217 | return PERMISSION_DENIED; |
| 218 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 219 | |
| 220 | int lastArg = numArgs - 1; |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 221 | bool asProto = false; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 222 | if (lastArg >= 0 && string(args[lastArg]) == "--proto") { // last argument |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 223 | asProto = true; |
| 224 | lastArg--; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 225 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 226 | if (numArgs > 0 && string(args[0]) == "--metadata") { // first argument |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 227 | // Request is to dump statsd stats. |
| 228 | bool verbose = false; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 229 | if (lastArg >= 0 && string(args[lastArg]) == "-v") { |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 230 | verbose = true; |
| 231 | lastArg--; |
| 232 | } |
| 233 | dumpStatsdStats(fd, verbose, asProto); |
| 234 | } else { |
| 235 | // Request is to dump statsd report data. |
| 236 | if (asProto) { |
| 237 | dumpIncidentSection(fd); |
| 238 | } else { |
| 239 | dprintf(fd, "Non-proto format of stats data dump not available; see proto version.\n"); |
| 240 | } |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 241 | } |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 242 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 243 | return NO_ERROR; |
| 244 | } |
| 245 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 246 | /** |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 247 | * Write debugging data about statsd in text or proto format. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 248 | */ |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 249 | void StatsService::dumpStatsdStats(int out, bool verbose, bool proto) { |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 250 | if (proto) { |
| 251 | vector<uint8_t> data; |
| 252 | StatsdStats::getInstance().dumpStats(&data, false); // does not reset statsdStats. |
| 253 | for (size_t i = 0; i < data.size(); i ++) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 254 | dprintf(out, "%c", data[i]); |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 255 | } |
| 256 | } else { |
| 257 | StatsdStats::getInstance().dumpStats(out); |
| 258 | mProcessor->dumpStates(out, verbose); |
| 259 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 263 | * Write stats report data in StatsDataDumpProto incident section format. |
| 264 | */ |
| 265 | void StatsService::dumpIncidentSection(int out) { |
| 266 | ProtoOutputStream proto; |
| 267 | for (const ConfigKey& configKey : mConfigManager->GetAllConfigKeys()) { |
| 268 | uint64_t reportsListToken = |
| 269 | proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS_LIST); |
| 270 | mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), |
| 271 | true /* includeCurrentBucket */, false /* erase_data */, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 272 | ADB_DUMP, |
| 273 | FAST, |
| 274 | &proto); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 275 | proto.end(reportsListToken); |
| 276 | proto.flush(out); |
Bookatz | c71d901 | 2018-12-19 12:28:38 -0800 | [diff] [blame] | 277 | proto.clear(); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 282 | * Implementation of the adb shell cmd stats command. |
| 283 | */ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 284 | status_t StatsService::handleShellCommand(int in, int out, int err, const char** argv, |
| 285 | uint32_t argc) { |
| 286 | uid_t uid = AIBinder_getCallingUid(); |
Stanislav Zholnin | d7674c2 | 2020-02-17 17:48:12 +0000 | [diff] [blame] | 287 | if (uid != AID_ROOT && uid != AID_SHELL) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 288 | return PERMISSION_DENIED; |
| 289 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 290 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 291 | Vector<String8> utf8Args; |
| 292 | utf8Args.setCapacity(argc); |
| 293 | for (uint32_t i = 0; i < argc; i++) { |
| 294 | utf8Args.push(String8(argv[i])); |
| 295 | } |
| 296 | |
| 297 | if (argc >= 1) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 298 | // adb shell cmd stats config ... |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 299 | if (!utf8Args[0].compare(String8("config"))) { |
| 300 | return cmd_config(in, out, err, utf8Args); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 301 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 302 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 303 | if (!utf8Args[0].compare(String8("print-uid-map"))) { |
| 304 | return cmd_print_uid_map(out, utf8Args); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 305 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 306 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 307 | if (!utf8Args[0].compare(String8("dump-report"))) { |
| 308 | return cmd_dump_report(out, utf8Args); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 309 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 310 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 311 | if (!utf8Args[0].compare(String8("pull-source")) && argc > 1) { |
| 312 | return cmd_print_pulled_metrics(out, utf8Args); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 313 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 314 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 315 | if (!utf8Args[0].compare(String8("send-broadcast"))) { |
| 316 | return cmd_trigger_broadcast(out, utf8Args); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 319 | if (!utf8Args[0].compare(String8("print-stats"))) { |
| 320 | return cmd_print_stats(out, utf8Args); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 321 | } |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 322 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 323 | if (!utf8Args[0].compare(String8("meminfo"))) { |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 324 | return cmd_dump_memory_info(out); |
| 325 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 326 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 327 | if (!utf8Args[0].compare(String8("write-to-disk"))) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 328 | return cmd_write_data_to_disk(out); |
| 329 | } |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 330 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 331 | if (!utf8Args[0].compare(String8("log-app-breadcrumb"))) { |
| 332 | return cmd_log_app_breadcrumb(out, utf8Args); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 333 | } |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 334 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 335 | if (!utf8Args[0].compare(String8("log-binary-push"))) { |
| 336 | return cmd_log_binary_push(out, utf8Args); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 339 | if (!utf8Args[0].compare(String8("clear-puller-cache"))) { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 340 | return cmd_clear_puller_cache(out); |
| 341 | } |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 342 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 343 | if (!utf8Args[0].compare(String8("print-logs"))) { |
| 344 | return cmd_print_logs(out, utf8Args); |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 345 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 346 | if (!utf8Args[0].compare(String8("send-active-configs"))) { |
| 347 | return cmd_trigger_active_config_broadcast(out, utf8Args); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 348 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 349 | if (!utf8Args[0].compare(String8("data-subscribe"))) { |
Tej Singh | c9250ce | 2019-09-20 19:28:53 -0700 | [diff] [blame] | 350 | { |
| 351 | std::lock_guard<std::mutex> lock(mShellSubscriberMutex); |
| 352 | if (mShellSubscriber == nullptr) { |
| 353 | mShellSubscriber = new ShellSubscriber(mUidMap, mPullerManager); |
| 354 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 355 | } |
Yao Chen | 35cb8d6 | 2019-01-03 16:49:14 -0800 | [diff] [blame] | 356 | int timeoutSec = -1; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 357 | if (argc >= 2) { |
| 358 | timeoutSec = atoi(utf8Args[1].c_str()); |
Yao Chen | 35cb8d6 | 2019-01-03 16:49:14 -0800 | [diff] [blame] | 359 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 360 | mShellSubscriber->startNewSubscription(in, out, timeoutSec); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 361 | return NO_ERROR; |
| 362 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 363 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 364 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 365 | print_cmd_help(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 366 | return NO_ERROR; |
| 367 | } |
| 368 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 369 | void StatsService::print_cmd_help(int out) { |
| 370 | dprintf(out, |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 371 | "usage: adb shell cmd stats print-stats-log [tag_required] " |
| 372 | "[timestamp_nsec_optional]\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 373 | dprintf(out, "\n"); |
| 374 | dprintf(out, "\n"); |
| 375 | dprintf(out, "usage: adb shell cmd stats meminfo\n"); |
| 376 | dprintf(out, "\n"); |
| 377 | dprintf(out, " Prints the malloc debug information. You need to run the following first: \n"); |
| 378 | dprintf(out, " # adb shell stop\n"); |
| 379 | dprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n"); |
| 380 | dprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n"); |
| 381 | dprintf(out, " # adb shell start\n"); |
| 382 | dprintf(out, "\n"); |
| 383 | dprintf(out, "\n"); |
| 384 | dprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n"); |
| 385 | dprintf(out, "\n"); |
| 386 | dprintf(out, " Prints the UID, app name, version mapping.\n"); |
| 387 | dprintf(out, " PKG Optional package name to print the uids of the package\n"); |
| 388 | dprintf(out, "\n"); |
| 389 | dprintf(out, "\n"); |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 390 | dprintf(out, "usage: adb shell cmd stats pull-source ATOM_TAG [PACKAGE] \n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 391 | dprintf(out, "\n"); |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 392 | dprintf(out, " Prints the output of a pulled atom\n"); |
| 393 | dprintf(out, " UID The atom to pull\n"); |
| 394 | dprintf(out, " PACKAGE The package to pull from. Default is AID_SYSTEM\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 395 | dprintf(out, "\n"); |
| 396 | dprintf(out, "\n"); |
| 397 | dprintf(out, "usage: adb shell cmd stats write-to-disk \n"); |
| 398 | dprintf(out, "\n"); |
| 399 | dprintf(out, " Flushes all data on memory to disk.\n"); |
| 400 | dprintf(out, "\n"); |
| 401 | dprintf(out, "\n"); |
| 402 | dprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n"); |
| 403 | dprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n"); |
| 404 | dprintf(out, " UID The uid to use. It is only possible to pass a UID\n"); |
| 405 | dprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 406 | dprintf(out, " uid is used.\n"); |
| 407 | dprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n"); |
| 408 | dprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n"); |
| 409 | dprintf(out, "\n"); |
| 410 | dprintf(out, "\n"); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 411 | dprintf(out, |
| 412 | "usage: adb shell cmd stats log-binary-push NAME VERSION STAGING ROLLBACK_ENABLED " |
| 413 | "LOW_LATENCY STATE EXPERIMENT_IDS\n"); |
| 414 | dprintf(out, " Log a binary push state changed event.\n"); |
| 415 | dprintf(out, " NAME The train name.\n"); |
| 416 | dprintf(out, " VERSION The train version code.\n"); |
| 417 | dprintf(out, " STAGING If this train requires a restart.\n"); |
| 418 | dprintf(out, " ROLLBACK_ENABLED If rollback should be enabled for this install.\n"); |
| 419 | dprintf(out, " LOW_LATENCY If the train requires low latency monitoring.\n"); |
| 420 | dprintf(out, " STATE The status of the train push.\n"); |
| 421 | dprintf(out, " Integer value of the enum in atoms.proto.\n"); |
| 422 | dprintf(out, " EXPERIMENT_IDS Comma separated list of experiment ids.\n"); |
| 423 | dprintf(out, " Leave blank for none.\n"); |
| 424 | dprintf(out, "\n"); |
| 425 | dprintf(out, "\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 426 | dprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n"); |
| 427 | dprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n"); |
| 428 | dprintf(out, "\n"); |
| 429 | dprintf(out, " Adds, updates or removes a configuration. The proto should be in\n"); |
| 430 | dprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n"); |
| 431 | dprintf(out, " provided, then all configs will be removed from memory and disk.\n"); |
| 432 | dprintf(out, "\n"); |
| 433 | dprintf(out, " UID The uid to use. It is only possible to pass the UID\n"); |
| 434 | dprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 435 | dprintf(out, " uid is used.\n"); |
| 436 | dprintf(out, " NAME The per-uid name to use\n"); |
| 437 | dprintf(out, "\n"); |
| 438 | dprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n"); |
| 439 | dprintf(out, "\n be removed from memory and disk!\n"); |
| 440 | dprintf(out, "\n"); |
| 441 | dprintf(out, |
Bookatz | 3e90658 | 2018-12-10 17:26:58 -0800 | [diff] [blame] | 442 | "usage: adb shell cmd stats dump-report [UID] NAME [--keep_data] " |
| 443 | "[--include_current_bucket] [--proto]\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 444 | dprintf(out, " Dump all metric data for a configuration.\n"); |
| 445 | dprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 446 | dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 447 | dprintf(out, " calling uid is used.\n"); |
| 448 | dprintf(out, " NAME The name of the configuration\n"); |
Bookatz | 3e90658 | 2018-12-10 17:26:58 -0800 | [diff] [blame] | 449 | dprintf(out, " --keep_data Do NOT erase the data upon dumping it.\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 450 | dprintf(out, " --proto Print proto binary.\n"); |
| 451 | dprintf(out, "\n"); |
| 452 | dprintf(out, "\n"); |
| 453 | dprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n"); |
| 454 | dprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n"); |
| 455 | dprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 456 | dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 457 | dprintf(out, " calling uid is used.\n"); |
| 458 | dprintf(out, " NAME The name of the configuration\n"); |
| 459 | dprintf(out, "\n"); |
| 460 | dprintf(out, "\n"); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 461 | dprintf(out, |
| 462 | "usage: adb shell cmd stats send-active-configs [--uid=UID] [--configs] " |
| 463 | "[NAME1] [NAME2] [NAME3..]\n"); |
| 464 | dprintf(out, " Send a broadcast that informs the subscriber of the current active configs.\n"); |
| 465 | dprintf(out, " --uid=UID The uid of the configurations. It is only possible to pass\n"); |
| 466 | dprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 467 | dprintf(out, " calling uid is used.\n"); |
| 468 | dprintf(out, " --configs Send the list of configs in the name list instead of\n"); |
| 469 | dprintf(out, " the currently active configs\n"); |
| 470 | dprintf(out, " NAME LIST List of configuration names to be included in the broadcast.\n"); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 471 | dprintf(out, "\n"); |
| 472 | dprintf(out, "\n"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 473 | dprintf(out, "usage: adb shell cmd stats print-stats\n"); |
| 474 | dprintf(out, " Prints some basic stats.\n"); |
| 475 | dprintf(out, " --proto Print proto binary instead of string format.\n"); |
| 476 | dprintf(out, "\n"); |
| 477 | dprintf(out, "\n"); |
| 478 | dprintf(out, "usage: adb shell cmd stats clear-puller-cache\n"); |
| 479 | dprintf(out, " Clear cached puller data.\n"); |
| 480 | dprintf(out, "\n"); |
| 481 | dprintf(out, "usage: adb shell cmd stats print-logs\n"); |
| 482 | dprintf(out, " Only works on eng build\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 485 | status_t StatsService::cmd_trigger_broadcast(int out, Vector<String8>& args) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 486 | string name; |
| 487 | bool good = false; |
| 488 | int uid; |
| 489 | const int argCount = args.size(); |
| 490 | if (argCount == 2) { |
| 491 | // Automatically pick the UID |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 492 | uid = AIBinder_getCallingUid(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 493 | name.assign(args[1].c_str(), args[1].size()); |
| 494 | good = true; |
| 495 | } else if (argCount == 3) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 496 | good = getUidFromArgs(args, 1, uid); |
| 497 | if (!good) { |
| 498 | dprintf(out, "Invalid UID. Note that the metrics can only be dumped for " |
| 499 | "other UIDs on eng or userdebug builds.\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 500 | } |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 501 | name.assign(args[2].c_str(), args[2].size()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 502 | } |
| 503 | if (!good) { |
| 504 | print_cmd_help(out); |
| 505 | return UNKNOWN_ERROR; |
| 506 | } |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 507 | ConfigKey key(uid, StrToInt64(name)); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 508 | shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetConfigReceiver(key); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 509 | if (receiver == nullptr) { |
| 510 | VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str()); |
| 511 | return UNKNOWN_ERROR; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 512 | } else if (receiver->sendDataBroadcast(mProcessor->getLastReportTimeNs(key)).isOk()) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 513 | VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), |
| 514 | args[2].c_str()); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 515 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 516 | VLOG("StatsService::trigger broadcast failed to %s, %s", args[1].c_str(), args[2].c_str()); |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 517 | return UNKNOWN_ERROR; |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 518 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 519 | return NO_ERROR; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 522 | status_t StatsService::cmd_trigger_active_config_broadcast(int out, Vector<String8>& args) { |
| 523 | const int argCount = args.size(); |
| 524 | int uid; |
| 525 | vector<int64_t> configIds; |
| 526 | if (argCount == 1) { |
| 527 | // Automatically pick the uid and send a broadcast that has no active configs. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 528 | uid = AIBinder_getCallingUid(); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 529 | mProcessor->GetActiveConfigs(uid, configIds); |
| 530 | } else { |
| 531 | int curArg = 1; |
| 532 | if(args[curArg].find("--uid=") == 0) { |
| 533 | string uidArgStr(args[curArg].c_str()); |
| 534 | string uidStr = uidArgStr.substr(6); |
| 535 | if (!getUidFromString(uidStr.c_str(), uid)) { |
| 536 | dprintf(out, "Invalid UID. Note that the config can only be set for " |
| 537 | "other UIDs on eng or userdebug builds.\n"); |
| 538 | return UNKNOWN_ERROR; |
| 539 | } |
| 540 | curArg++; |
| 541 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 542 | uid = AIBinder_getCallingUid(); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 543 | } |
| 544 | if (curArg == argCount || args[curArg] != "--configs") { |
| 545 | VLOG("Reached end of args, or specify configs not set. Sending actual active configs,"); |
| 546 | mProcessor->GetActiveConfigs(uid, configIds); |
| 547 | } else { |
| 548 | // Flag specified, use the given list of configs. |
| 549 | curArg++; |
| 550 | for (int i = curArg; i < argCount; i++) { |
| 551 | char* endp; |
| 552 | int64_t configID = strtoll(args[i].c_str(), &endp, 10); |
| 553 | if (endp == args[i].c_str() || *endp != '\0') { |
| 554 | dprintf(out, "Error parsing config ID.\n"); |
| 555 | return UNKNOWN_ERROR; |
| 556 | } |
| 557 | VLOG("Adding config id %ld", static_cast<long>(configID)); |
| 558 | configIds.push_back(configID); |
| 559 | } |
| 560 | } |
| 561 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 562 | shared_ptr<IPendingIntentRef> receiver = mConfigManager->GetActiveConfigsChangedReceiver(uid); |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 563 | if (receiver == nullptr) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 564 | VLOG("Could not find receiver for uid %d", uid); |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 565 | return UNKNOWN_ERROR; |
| 566 | } else if (receiver->sendActiveConfigsChangedBroadcast(configIds).isOk()) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 567 | VLOG("StatsService::trigger active configs changed broadcast succeeded for uid %d" , uid); |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 568 | } else { |
| 569 | VLOG("StatsService::trigger active configs changed broadcast failed for uid %d", uid); |
| 570 | return UNKNOWN_ERROR; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 571 | } |
| 572 | return NO_ERROR; |
| 573 | } |
| 574 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 575 | status_t StatsService::cmd_config(int in, int out, int err, Vector<String8>& args) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 576 | const int argCount = args.size(); |
| 577 | if (argCount >= 2) { |
| 578 | if (args[1] == "update" || args[1] == "remove") { |
| 579 | bool good = false; |
| 580 | int uid = -1; |
| 581 | string name; |
| 582 | |
| 583 | if (argCount == 3) { |
| 584 | // Automatically pick the UID |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 585 | uid = AIBinder_getCallingUid(); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 586 | name.assign(args[2].c_str(), args[2].size()); |
| 587 | good = true; |
| 588 | } else if (argCount == 4) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 589 | good = getUidFromArgs(args, 2, uid); |
| 590 | if (!good) { |
| 591 | dprintf(err, "Invalid UID. Note that the config can only be set for " |
| 592 | "other UIDs on eng or userdebug builds.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 593 | } |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 594 | name.assign(args[3].c_str(), args[3].size()); |
yro | e5f8292 | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 595 | } else if (argCount == 2 && args[1] == "remove") { |
| 596 | good = true; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | if (!good) { |
| 600 | // If arg parsing failed, print the help text and return an error. |
| 601 | print_cmd_help(out); |
| 602 | return UNKNOWN_ERROR; |
| 603 | } |
| 604 | |
| 605 | if (args[1] == "update") { |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 606 | char* endp; |
| 607 | int64_t configID = strtoll(name.c_str(), &endp, 10); |
| 608 | if (endp == name.c_str() || *endp != '\0') { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 609 | dprintf(err, "Error parsing config ID.\n"); |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 610 | return UNKNOWN_ERROR; |
| 611 | } |
| 612 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 613 | // Read stream into buffer. |
| 614 | string buffer; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 615 | if (!android::base::ReadFdToString(in, &buffer)) { |
| 616 | dprintf(err, "Error reading stream for StatsConfig.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 617 | return UNKNOWN_ERROR; |
| 618 | } |
| 619 | |
| 620 | // Parse buffer. |
| 621 | StatsdConfig config; |
| 622 | if (!config.ParseFromString(buffer)) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 623 | dprintf(err, "Error parsing proto stream for StatsConfig.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 624 | return UNKNOWN_ERROR; |
| 625 | } |
| 626 | |
| 627 | // Add / update the config. |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 628 | mConfigManager->UpdateConfig(ConfigKey(uid, configID), config); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 629 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 630 | if (argCount == 2) { |
| 631 | cmd_remove_all_configs(out); |
| 632 | } else { |
| 633 | // Remove the config. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 634 | mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name))); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 635 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | return NO_ERROR; |
| 639 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 640 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 641 | print_cmd_help(out); |
| 642 | return UNKNOWN_ERROR; |
| 643 | } |
| 644 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 645 | status_t StatsService::cmd_dump_report(int out, const Vector<String8>& args) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 646 | if (mProcessor != nullptr) { |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 647 | int argCount = args.size(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 648 | bool good = false; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 649 | bool proto = false; |
Chenjie Yu | bd1a28f | 2018-07-17 14:55:19 -0700 | [diff] [blame] | 650 | bool includeCurrentBucket = false; |
Bookatz | 3e90658 | 2018-12-10 17:26:58 -0800 | [diff] [blame] | 651 | bool eraseData = true; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 652 | int uid; |
| 653 | string name; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 654 | if (!std::strcmp("--proto", args[argCount-1].c_str())) { |
| 655 | proto = true; |
| 656 | argCount -= 1; |
| 657 | } |
Chenjie Yu | bd1a28f | 2018-07-17 14:55:19 -0700 | [diff] [blame] | 658 | if (!std::strcmp("--include_current_bucket", args[argCount-1].c_str())) { |
| 659 | includeCurrentBucket = true; |
| 660 | argCount -= 1; |
| 661 | } |
Bookatz | 3e90658 | 2018-12-10 17:26:58 -0800 | [diff] [blame] | 662 | if (!std::strcmp("--keep_data", args[argCount-1].c_str())) { |
| 663 | eraseData = false; |
| 664 | argCount -= 1; |
| 665 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 666 | if (argCount == 2) { |
| 667 | // Automatically pick the UID |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 668 | uid = AIBinder_getCallingUid(); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 669 | name.assign(args[1].c_str(), args[1].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 670 | good = true; |
| 671 | } else if (argCount == 3) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 672 | good = getUidFromArgs(args, 1, uid); |
| 673 | if (!good) { |
| 674 | dprintf(out, "Invalid UID. Note that the metrics can only be dumped for " |
| 675 | "other UIDs on eng or userdebug builds.\n"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 676 | } |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 677 | name.assign(args[2].c_str(), args[2].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 678 | } |
| 679 | if (good) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 680 | vector<uint8_t> data; |
David Chen | 926fc75 | 2018-02-23 13:31:43 -0800 | [diff] [blame] | 681 | mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(), |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 682 | includeCurrentBucket, eraseData, ADB_DUMP, |
| 683 | NO_TIME_CONSTRAINTS, |
| 684 | &data); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 685 | if (proto) { |
| 686 | for (size_t i = 0; i < data.size(); i ++) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 687 | dprintf(out, "%c", data[i]); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 688 | } |
| 689 | } else { |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 690 | dprintf(out, "Non-proto stats data dump not currently supported.\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 691 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 692 | return android::OK; |
| 693 | } else { |
| 694 | // If arg parsing failed, print the help text and return an error. |
| 695 | print_cmd_help(out); |
| 696 | return UNKNOWN_ERROR; |
| 697 | } |
| 698 | } else { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 699 | dprintf(out, "Log processor does not exist...\n"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 700 | return UNKNOWN_ERROR; |
| 701 | } |
| 702 | } |
| 703 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 704 | status_t StatsService::cmd_print_stats(int out, const Vector<String8>& args) { |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 705 | int argCount = args.size(); |
| 706 | bool proto = false; |
| 707 | if (!std::strcmp("--proto", args[argCount-1].c_str())) { |
| 708 | proto = true; |
| 709 | argCount -= 1; |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 710 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 711 | StatsdStats& statsdStats = StatsdStats::getInstance(); |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 712 | if (proto) { |
| 713 | vector<uint8_t> data; |
| 714 | statsdStats.dumpStats(&data, false); // does not reset statsdStats. |
| 715 | for (size_t i = 0; i < data.size(); i ++) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 716 | dprintf(out, "%c", data[i]); |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | } else { |
| 720 | vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys(); |
| 721 | for (const ConfigKey& key : configs) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 722 | dprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(), |
Tej Singh | 41b3f9a | 2018-04-03 17:06:35 -0700 | [diff] [blame] | 723 | mProcessor->GetMetricsSize(key)); |
| 724 | } |
| 725 | statsdStats.dumpStats(out); |
| 726 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 727 | return NO_ERROR; |
| 728 | } |
| 729 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 730 | status_t StatsService::cmd_print_uid_map(int out, const Vector<String8>& args) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 731 | if (args.size() > 1) { |
| 732 | string pkg; |
| 733 | pkg.assign(args[1].c_str(), args[1].size()); |
| 734 | auto uids = mUidMap->getAppUid(pkg); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 735 | dprintf(out, "%s -> [ ", pkg.c_str()); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 736 | for (const auto& uid : uids) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 737 | dprintf(out, "%d ", uid); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 738 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 739 | dprintf(out, "]\n"); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 740 | } else { |
| 741 | mUidMap->printUidMap(out); |
| 742 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 743 | return NO_ERROR; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 746 | status_t StatsService::cmd_write_data_to_disk(int out) { |
| 747 | dprintf(out, "Writing data to disk\n"); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 748 | mProcessor->WriteDataToDisk(ADB_DUMP, NO_TIME_CONSTRAINTS); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 749 | return NO_ERROR; |
| 750 | } |
| 751 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 752 | status_t StatsService::cmd_log_app_breadcrumb(int out, const Vector<String8>& args) { |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 753 | bool good = false; |
| 754 | int32_t uid; |
| 755 | int32_t label; |
| 756 | int32_t state; |
| 757 | const int argCount = args.size(); |
| 758 | if (argCount == 3) { |
| 759 | // Automatically pick the UID |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 760 | uid = AIBinder_getCallingUid(); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 761 | label = atoi(args[1].c_str()); |
| 762 | state = atoi(args[2].c_str()); |
| 763 | good = true; |
| 764 | } else if (argCount == 4) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 765 | good = getUidFromArgs(args, 1, uid); |
| 766 | if (!good) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 767 | dprintf(out, |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 768 | "Invalid UID. Note that selecting a UID for writing AppBreadcrumb can only be " |
| 769 | "done for other UIDs on eng or userdebug builds.\n"); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 770 | } |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 771 | label = atoi(args[2].c_str()); |
| 772 | state = atoi(args[3].c_str()); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 773 | } |
| 774 | if (good) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 775 | dprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state); |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 776 | android::os::statsd::util::stats_write( |
| 777 | android::os::statsd::util::APP_BREADCRUMB_REPORTED, uid, label, state); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 778 | } else { |
| 779 | print_cmd_help(out); |
| 780 | return UNKNOWN_ERROR; |
| 781 | } |
| 782 | return NO_ERROR; |
| 783 | } |
| 784 | |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 785 | status_t StatsService::cmd_log_binary_push(int out, const Vector<String8>& args) { |
| 786 | // Security checks are done in the sendBinaryPushStateChanged atom. |
| 787 | const int argCount = args.size(); |
| 788 | if (argCount != 7 && argCount != 8) { |
| 789 | dprintf(out, "Incorrect number of argument supplied\n"); |
| 790 | return UNKNOWN_ERROR; |
| 791 | } |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 792 | string trainName = string(args[1].c_str()); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 793 | int64_t trainVersion = strtoll(args[2].c_str(), nullptr, 10); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 794 | int32_t state = atoi(args[6].c_str()); |
| 795 | vector<int64_t> experimentIds; |
| 796 | if (argCount == 8) { |
| 797 | vector<string> experimentIdsString = android::base::Split(string(args[7].c_str()), ","); |
| 798 | for (string experimentIdString : experimentIdsString) { |
| 799 | int64_t experimentId = strtoll(experimentIdString.c_str(), nullptr, 10); |
| 800 | experimentIds.push_back(experimentId); |
| 801 | } |
| 802 | } |
| 803 | dprintf(out, "Logging BinaryPushStateChanged\n"); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 804 | vector<uint8_t> experimentIdBytes; |
| 805 | writeExperimentIdsToProto(experimentIds, &experimentIdBytes); |
| 806 | LogEvent event(trainName, trainVersion, args[3], args[4], args[5], state, experimentIdBytes, 0); |
| 807 | mProcessor->OnLogEvent(&event); |
Tej Singh | 53f9dee | 2019-04-30 17:45:54 -0700 | [diff] [blame] | 808 | return NO_ERROR; |
| 809 | } |
| 810 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 811 | status_t StatsService::cmd_print_pulled_metrics(int out, const Vector<String8>& args) { |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 812 | int s = atoi(args[1].c_str()); |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 813 | vector<int32_t> uids; |
| 814 | if (args.size() > 2) { |
| 815 | string package = string(args[2].c_str()); |
| 816 | auto it = UidMap::sAidToUidMapping.find(package); |
| 817 | if (it != UidMap::sAidToUidMapping.end()) { |
| 818 | uids.push_back(it->second); |
| 819 | } else { |
| 820 | set<int32_t> uids_set = mUidMap->getAppUid(package); |
| 821 | uids.insert(uids.end(), uids_set.begin(), uids_set.end()); |
| 822 | } |
| 823 | } else { |
| 824 | uids.push_back(AID_SYSTEM); |
| 825 | } |
| 826 | vector<shared_ptr<LogEvent>> stats; |
| 827 | if (mPullerManager->Pull(s, uids, &stats)) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 828 | for (const auto& it : stats) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 829 | dprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 830 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 831 | dprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 832 | return NO_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 833 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 834 | return UNKNOWN_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 837 | status_t StatsService::cmd_remove_all_configs(int out) { |
| 838 | dprintf(out, "Removing all configs...\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 839 | VLOG("StatsService::cmd_remove_all_configs was called"); |
| 840 | mConfigManager->RemoveAllConfigs(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 841 | StorageManager::deleteAllFiles(STATS_SERVICE_DIR); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 842 | return NO_ERROR; |
| 843 | } |
| 844 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 845 | status_t StatsService::cmd_dump_memory_info(int out) { |
| 846 | dprintf(out, "meminfo not available.\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 847 | return NO_ERROR; |
| 848 | } |
| 849 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 850 | status_t StatsService::cmd_clear_puller_cache(int out) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 851 | VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 852 | AIBinder_getCallingPid(), AIBinder_getCallingUid()); |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 853 | if (checkPermission(kPermissionDump)) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 854 | int cleared = mPullerManager->ForceClearPullerCache(); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 855 | dprintf(out, "Puller removed %d cached data!\n", cleared); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 856 | return NO_ERROR; |
| 857 | } else { |
| 858 | return PERMISSION_DENIED; |
| 859 | } |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 862 | status_t StatsService::cmd_print_logs(int out, const Vector<String8>& args) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 863 | VLOG("StatsService::cmd_print_logs with Pid %i, Uid %i", AIBinder_getCallingPid(), |
| 864 | AIBinder_getCallingUid()); |
Ruchir Rastogi | 0563e3b | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 865 | if (checkPermission(kPermissionDump)) { |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 866 | bool enabled = true; |
| 867 | if (args.size() >= 2) { |
| 868 | enabled = atoi(args[1].c_str()) != 0; |
| 869 | } |
| 870 | mProcessor->setPrintLogs(enabled); |
| 871 | return NO_ERROR; |
| 872 | } else { |
| 873 | return PERMISSION_DENIED; |
| 874 | } |
| 875 | } |
| 876 | |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 877 | bool StatsService::getUidFromArgs(const Vector<String8>& args, size_t uidArgIndex, int32_t& uid) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 878 | return getUidFromString(args[uidArgIndex].c_str(), uid); |
| 879 | } |
| 880 | |
| 881 | bool StatsService::getUidFromString(const char* s, int32_t& uid) { |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 882 | if (*s == '\0') { |
| 883 | return false; |
| 884 | } |
| 885 | char* endc = NULL; |
| 886 | int64_t longUid = strtol(s, &endc, 0); |
| 887 | if (*endc != '\0') { |
| 888 | return false; |
| 889 | } |
| 890 | int32_t goodUid = static_cast<int32_t>(longUid); |
| 891 | if (longUid < 0 || static_cast<uint64_t>(longUid) != static_cast<uid_t>(goodUid)) { |
| 892 | return false; // It was not of uid_t type. |
| 893 | } |
| 894 | uid = goodUid; |
| 895 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 896 | int32_t callingUid = AIBinder_getCallingUid(); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 897 | return mEngBuild // UserDebug/EngBuild are allowed to impersonate uids. |
| 898 | || (callingUid == goodUid) // Anyone can 'impersonate' themselves. |
| 899 | || (callingUid == AID_ROOT && goodUid == AID_SHELL); // ROOT can impersonate SHELL. |
| 900 | } |
| 901 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 902 | Status StatsService::informAllUidData(const ScopedFileDescriptor& fd) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 903 | ENFORCE_UID(AID_SYSTEM); |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 904 | // Read stream into buffer. |
| 905 | string buffer; |
| 906 | if (!android::base::ReadFdToString(fd.get(), &buffer)) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 907 | return exception(EX_ILLEGAL_ARGUMENT, "Failed to read all data from the pipe."); |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 908 | } |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 909 | |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 910 | // Parse buffer. |
| 911 | UidData uidData; |
| 912 | if (!uidData.ParseFromString(buffer)) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 913 | return exception(EX_ILLEGAL_ARGUMENT, "Error parsing proto stream for UidData."); |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 914 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 915 | |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 916 | vector<String16> versionStrings; |
| 917 | vector<String16> installers; |
| 918 | vector<String16> packageNames; |
| 919 | vector<int32_t> uids; |
| 920 | vector<int64_t> versions; |
| 921 | |
| 922 | const auto numEntries = uidData.app_info_size(); |
| 923 | versionStrings.reserve(numEntries); |
| 924 | installers.reserve(numEntries); |
| 925 | packageNames.reserve(numEntries); |
| 926 | uids.reserve(numEntries); |
| 927 | versions.reserve(numEntries); |
| 928 | |
| 929 | for (const auto& appInfo: uidData.app_info()) { |
| 930 | packageNames.emplace_back(String16(appInfo.package_name().c_str())); |
| 931 | uids.push_back(appInfo.uid()); |
| 932 | versions.push_back(appInfo.version()); |
| 933 | versionStrings.emplace_back(String16(appInfo.version_string().c_str())); |
| 934 | installers.emplace_back(String16(appInfo.installer().c_str())); |
| 935 | } |
| 936 | |
| 937 | mUidMap->updateMap(getElapsedRealtimeNs(), |
| 938 | uids, |
| 939 | versions, |
| 940 | versionStrings, |
| 941 | packageNames, |
| 942 | installers); |
| 943 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame^] | 944 | mBootCompleteTrigger.markComplete(kUidMapReceivedTag); |
Max Dashouk | 11e0d40 | 2019-05-16 16:58:07 -0700 | [diff] [blame] | 945 | VLOG("StatsService::informAllUidData UidData proto parsed successfully."); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 946 | return Status::ok(); |
| 947 | } |
| 948 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 949 | Status StatsService::informOnePackage(const string& app, int32_t uid, int64_t version, |
| 950 | const string& versionString, const string& installer) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 951 | ENFORCE_UID(AID_SYSTEM); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 952 | |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 953 | VLOG("StatsService::informOnePackage was called"); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 954 | String16 utf16App = String16(app.c_str()); |
| 955 | String16 utf16VersionString = String16(versionString.c_str()); |
| 956 | String16 utf16Installer = String16(installer.c_str()); |
| 957 | |
| 958 | mUidMap->updateApp(getElapsedRealtimeNs(), utf16App, uid, version, utf16VersionString, |
| 959 | utf16Installer); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 960 | return Status::ok(); |
| 961 | } |
| 962 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 963 | Status StatsService::informOnePackageRemoved(const string& app, int32_t uid) { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 964 | ENFORCE_UID(AID_SYSTEM); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 965 | |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 966 | VLOG("StatsService::informOnePackageRemoved was called"); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 967 | String16 utf16App = String16(app.c_str()); |
| 968 | mUidMap->removeApp(getElapsedRealtimeNs(), utf16App, uid); |
yro | 0192402 | 2018-02-20 18:20:49 -0800 | [diff] [blame] | 969 | mConfigManager->RemoveConfigs(uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 970 | return Status::ok(); |
| 971 | } |
| 972 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 973 | Status StatsService::informAnomalyAlarmFired() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 974 | ENFORCE_UID(AID_SYSTEM); |
| 975 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 976 | VLOG("StatsService::informAnomalyAlarmFired was called"); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 977 | int64_t currentTimeSec = getElapsedRealtimeSec(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 978 | std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet = |
| 979 | mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
| 980 | if (alarmSet.size() > 0) { |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 981 | VLOG("Found an anomaly alarm that fired."); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 982 | mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet); |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 983 | } else { |
| 984 | VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled."); |
| 985 | } |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 986 | return Status::ok(); |
| 987 | } |
| 988 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 989 | Status StatsService::informAlarmForSubscriberTriggeringFired() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 990 | ENFORCE_UID(AID_SYSTEM); |
| 991 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 992 | VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called"); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 993 | int64_t currentTimeSec = getElapsedRealtimeSec(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 994 | std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet = |
| 995 | mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
| 996 | if (alarmSet.size() > 0) { |
| 997 | VLOG("Found periodic alarm fired."); |
| 998 | mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet); |
| 999 | } else { |
| 1000 | ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled."); |
| 1001 | } |
| 1002 | return Status::ok(); |
| 1003 | } |
| 1004 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1005 | Status StatsService::informPollAlarmFired() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1006 | ENFORCE_UID(AID_SYSTEM); |
| 1007 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1008 | VLOG("StatsService::informPollAlarmFired was called"); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 1009 | mProcessor->informPullAlarmFired(getElapsedRealtimeNs()); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1010 | VLOG("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 1011 | return Status::ok(); |
| 1012 | } |
| 1013 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1014 | Status StatsService::systemRunning() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1015 | ENFORCE_UID(AID_SYSTEM); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1016 | |
| 1017 | // When system_server is up and running, schedule the dropbox task to run. |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1018 | VLOG("StatsService::systemRunning"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1019 | sayHiToStatsCompanion(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1020 | return Status::ok(); |
| 1021 | } |
| 1022 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1023 | Status StatsService::informDeviceShutdown() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1024 | ENFORCE_UID(AID_SYSTEM); |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 1025 | VLOG("StatsService::informDeviceShutdown"); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1026 | mProcessor->WriteDataToDisk(DEVICE_SHUTDOWN, FAST); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1027 | mProcessor->SaveActiveConfigsToDisk(getElapsedRealtimeNs()); |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 1028 | mProcessor->SaveMetadataToDisk(getWallClockNs(), getElapsedRealtimeNs()); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 1029 | return Status::ok(); |
| 1030 | } |
| 1031 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1032 | void StatsService::sayHiToStatsCompanion() { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1033 | shared_ptr<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1034 | if (statsCompanion != nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1035 | VLOG("Telling statsCompanion that statsd is ready"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1036 | statsCompanion->statsdReady(); |
| 1037 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1038 | VLOG("Could not access statsCompanion"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1042 | Status StatsService::statsCompanionReady() { |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1043 | ENFORCE_UID(AID_SYSTEM); |
| 1044 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1045 | VLOG("StatsService::statsCompanionReady was called"); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1046 | shared_ptr<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1047 | if (statsCompanion == nullptr) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1048 | return exception(EX_NULL_POINTER, |
| 1049 | "StatsCompanion unavailable despite it contacting statsd."); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1050 | } |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 1051 | VLOG("StatsService::statsCompanionReady linking to statsCompanion."); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1052 | AIBinder_linkToDeath(statsCompanion->asBinder().get(), |
| 1053 | mStatsCompanionServiceDeathRecipient.get(), this); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 1054 | mPullerManager->SetStatsCompanionService(statsCompanion); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 1055 | mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion); |
| 1056 | mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1057 | return Status::ok(); |
| 1058 | } |
| 1059 | |
Jeffrey Huang | d8f5330 | 2020-04-06 18:09:55 -0700 | [diff] [blame] | 1060 | Status StatsService::bootCompleted() { |
| 1061 | ENFORCE_UID(AID_SYSTEM); |
| 1062 | |
| 1063 | VLOG("StatsService::bootCompleted was called"); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame^] | 1064 | mBootCompleteTrigger.markComplete(kBootCompleteTag); |
Jeffrey Huang | d8f5330 | 2020-04-06 18:09:55 -0700 | [diff] [blame] | 1065 | return Status::ok(); |
| 1066 | } |
| 1067 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 1068 | void StatsService::Startup() { |
| 1069 | mConfigManager->Startup(); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1070 | mProcessor->LoadActiveConfigsFromDisk(); |
Jeffrey Huang | 475677e | 2020-03-30 19:52:07 -0700 | [diff] [blame] | 1071 | mProcessor->LoadMetadataFromDisk(getWallClockNs(), getElapsedRealtimeNs()); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 1074 | void StatsService::Terminate() { |
| 1075 | ALOGI("StatsService::Terminating"); |
| 1076 | if (mProcessor != nullptr) { |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1077 | mProcessor->WriteDataToDisk(TERMINATION_SIGNAL_RECEIVED, FAST); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1078 | mProcessor->SaveActiveConfigsToDisk(getElapsedRealtimeNs()); |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 1079 | mProcessor->SaveMetadataToDisk(getWallClockNs(), getElapsedRealtimeNs()); |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 1083 | // Test only interface!!! |
Yao Chen | 3ff3a49 | 2018-08-06 16:17:37 -0700 | [diff] [blame] | 1084 | void StatsService::OnLogEvent(LogEvent* event) { |
| 1085 | mProcessor->OnLogEvent(event); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 1086 | if (mShellSubscriber != nullptr) { |
| 1087 | mShellSubscriber->onLogEvent(*event); |
| 1088 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1091 | Status StatsService::getData(int64_t key, const int32_t callingUid, vector<int8_t>* output) { |
Jeffrey Huang | 04f948b | 2020-01-07 10:05:25 -0800 | [diff] [blame] | 1092 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1093 | |
Jeffrey Huang | 04f948b | 2020-01-07 10:05:25 -0800 | [diff] [blame] | 1094 | VLOG("StatsService::getData with Uid %i", callingUid); |
| 1095 | ConfigKey configKey(callingUid, key); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1096 | // TODO(b/149254662): Since libbinder_ndk uses int8_t instead of uint8_t, |
| 1097 | // there are inconsistencies with internal statsd logic. Instead of |
| 1098 | // modifying lots of files, we create a temporary output array of int8_t and |
| 1099 | // copy its data into output. This is a bad hack, but hopefully |
| 1100 | // libbinder_ndk will transition to using uint8_t soon: progress is tracked |
| 1101 | // in b/144957764. Same applies to StatsService::getMetadata. |
| 1102 | vector<uint8_t> unsignedOutput; |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1103 | // The dump latency does not matter here since we do not include the current bucket, we do not |
| 1104 | // need to pull any new data anyhow. |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 1105 | mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), false /* include_current_bucket*/, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1106 | true /* erase_data */, GET_DATA_CALLED, FAST, &unsignedOutput); |
| 1107 | *output = vector<int8_t>(unsignedOutput.begin(), unsignedOutput.end()); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1108 | return Status::ok(); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1111 | Status StatsService::getMetadata(vector<int8_t>* output) { |
Jeffrey Huang | 9613a97 | 2020-01-07 10:05:03 -0800 | [diff] [blame] | 1112 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1113 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1114 | vector<uint8_t> unsignedOutput; |
| 1115 | StatsdStats::getInstance().dumpStats(&unsignedOutput, false); // Don't reset the counters. |
| 1116 | *output = vector<int8_t>(unsignedOutput.begin(), unsignedOutput.end()); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1117 | return Status::ok(); |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1120 | Status StatsService::addConfiguration(int64_t key, const vector <int8_t>& config, |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 1121 | const int32_t callingUid) { |
| 1122 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1123 | |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 1124 | if (addConfigurationChecked(callingUid, key, config)) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1125 | return Status::ok(); |
| 1126 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1127 | return exception(EX_ILLEGAL_ARGUMENT, "Could not parse malformatted StatsdConfig."); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1131 | bool StatsService::addConfigurationChecked(int uid, int64_t key, const vector<int8_t>& config) { |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 1132 | ConfigKey configKey(uid, key); |
| 1133 | StatsdConfig cfg; |
| 1134 | if (config.size() > 0) { // If the config is empty, skip parsing. |
| 1135 | if (!cfg.ParseFromArray(&config[0], config.size())) { |
| 1136 | return false; |
| 1137 | } |
| 1138 | } |
| 1139 | mConfigManager->UpdateConfig(configKey, cfg); |
| 1140 | return true; |
| 1141 | } |
| 1142 | |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 1143 | Status StatsService::removeDataFetchOperation(int64_t key, |
| 1144 | const int32_t callingUid) { |
| 1145 | ENFORCE_UID(AID_SYSTEM); |
| 1146 | ConfigKey configKey(callingUid, key); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1147 | mConfigManager->RemoveConfigReceiver(configKey); |
| 1148 | return Status::ok(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1151 | Status StatsService::setDataFetchOperation(int64_t key, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1152 | const shared_ptr<IPendingIntentRef>& pir, |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 1153 | const int32_t callingUid) { |
| 1154 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1155 | |
Jeffrey Huang | ad21374 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 1156 | ConfigKey configKey(callingUid, key); |
| 1157 | mConfigManager->SetConfigReceiver(configKey, pir); |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 1158 | if (StorageManager::hasConfigMetricsReport(configKey)) { |
| 1159 | VLOG("StatsService::setDataFetchOperation marking configKey %s to dump reports on disk", |
| 1160 | configKey.ToString().c_str()); |
| 1161 | mProcessor->noteOnDiskData(configKey); |
| 1162 | } |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1163 | return Status::ok(); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1166 | Status StatsService::setActiveConfigsChangedOperation(const shared_ptr<IPendingIntentRef>& pir, |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1167 | const int32_t callingUid, |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1168 | vector<int64_t>* output) { |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1169 | ENFORCE_UID(AID_SYSTEM); |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1170 | |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1171 | mConfigManager->SetActiveConfigsChangedReceiver(callingUid, pir); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 1172 | if (output != nullptr) { |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1173 | mProcessor->GetActiveConfigs(callingUid, *output); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 1174 | } else { |
| 1175 | ALOGW("StatsService::setActiveConfigsChanged output was nullptr"); |
| 1176 | } |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1177 | return Status::ok(); |
| 1178 | } |
| 1179 | |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1180 | Status StatsService::removeActiveConfigsChangedOperation(const int32_t callingUid) { |
| 1181 | ENFORCE_UID(AID_SYSTEM); |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1182 | |
Jeffrey Huang | 47537a1 | 2020-01-06 15:35:34 -0800 | [diff] [blame] | 1183 | mConfigManager->RemoveActiveConfigsChangedReceiver(callingUid); |
Tej Singh | 2c9ef2a | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 1184 | return Status::ok(); |
| 1185 | } |
| 1186 | |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 1187 | Status StatsService::removeConfiguration(int64_t key, const int32_t callingUid) { |
| 1188 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1189 | |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 1190 | ConfigKey configKey(callingUid, key); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1191 | mConfigManager->RemoveConfig(configKey); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1192 | return Status::ok(); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1195 | Status StatsService::setBroadcastSubscriber(int64_t configId, |
| 1196 | int64_t subscriberId, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1197 | const shared_ptr<IPendingIntentRef>& pir, |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1198 | const int32_t callingUid) { |
| 1199 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1200 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1201 | VLOG("StatsService::setBroadcastSubscriber called."); |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1202 | ConfigKey configKey(callingUid, configId); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1203 | SubscriberReporter::getInstance() |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1204 | .setBroadcastSubscriber(configKey, subscriberId, pir); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1205 | return Status::ok(); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | Status StatsService::unsetBroadcastSubscriber(int64_t configId, |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1209 | int64_t subscriberId, |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1210 | const int32_t callingUid) { |
| 1211 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 1212 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1213 | VLOG("StatsService::unsetBroadcastSubscriber called."); |
Jeffrey Huang | 4f2e6bd | 2020-01-06 16:24:45 -0800 | [diff] [blame] | 1214 | ConfigKey configKey(callingUid, configId); |
Bookatz | 4f71629 | 2018-04-10 17:15:12 -0700 | [diff] [blame] | 1215 | SubscriberReporter::getInstance() |
| 1216 | .unsetBroadcastSubscriber(configKey, subscriberId); |
| 1217 | return Status::ok(); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 1220 | Status StatsService::sendAppBreadcrumbAtom(int32_t label, int32_t state) { |
| 1221 | // Permission check not necessary as it's meant for applications to write to |
| 1222 | // statsd. |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 1223 | android::os::statsd::util::stats_write(android::os::statsd::util::APP_BREADCRUMB_REPORTED, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1224 | (int32_t) AIBinder_getCallingUid(), label, |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 1225 | state); |
| 1226 | return Status::ok(); |
| 1227 | } |
| 1228 | |
Jeffrey Huang | d7fda53 | 2020-04-06 18:19:46 -0700 | [diff] [blame] | 1229 | Status StatsService::allPullersFromBootRegistered() { |
| 1230 | ENFORCE_UID(AID_SYSTEM); |
| 1231 | |
| 1232 | VLOG("StatsService::allPullersFromBootRegistered was called"); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame^] | 1233 | mBootCompleteTrigger.markComplete(kAllPullersRegisteredTag); |
Jeffrey Huang | d7fda53 | 2020-04-06 18:19:46 -0700 | [diff] [blame] | 1234 | return Status::ok(); |
| 1235 | } |
| 1236 | |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1237 | Status StatsService::registerPullAtomCallback(int32_t uid, int32_t atomTag, int64_t coolDownMillis, |
| 1238 | int64_t timeoutMillis, |
| 1239 | const std::vector<int32_t>& additiveFields, |
| 1240 | const shared_ptr<IPullAtomCallback>& pullerCallback) { |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 1241 | ENFORCE_UID(AID_SYSTEM); |
Tej Singh | b780251 | 2019-12-04 17:57:04 -0800 | [diff] [blame] | 1242 | VLOG("StatsService::registerPullAtomCallback called."); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1243 | mPullerManager->RegisterPullAtomCallback(uid, atomTag, MillisToNano(coolDownMillis), |
| 1244 | MillisToNano(timeoutMillis), additiveFields, |
Tej Singh | b780251 | 2019-12-04 17:57:04 -0800 | [diff] [blame] | 1245 | pullerCallback); |
| 1246 | return Status::ok(); |
| 1247 | } |
| 1248 | |
Tej Singh | 73597dc | 2020-03-13 18:42:40 -0700 | [diff] [blame] | 1249 | Status StatsService::registerNativePullAtomCallback( |
| 1250 | int32_t atomTag, int64_t coolDownMillis, int64_t timeoutMillis, |
| 1251 | const std::vector<int32_t>& additiveFields, |
| 1252 | const shared_ptr<IPullAtomCallback>& pullerCallback) { |
Tej Singh | 10458ec | 2020-03-17 11:04:02 -0700 | [diff] [blame] | 1253 | if (!checkPermission(kPermissionRegisterPullAtom)) { |
| 1254 | return exception( |
| 1255 | EX_SECURITY, |
| 1256 | StringPrintf("Uid %d does not have the %s permission when registering atom %d", |
| 1257 | AIBinder_getCallingUid(), kPermissionRegisterPullAtom, atomTag)); |
| 1258 | } |
Tej Singh | b780251 | 2019-12-04 17:57:04 -0800 | [diff] [blame] | 1259 | VLOG("StatsService::registerNativePullAtomCallback called."); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1260 | int32_t uid = AIBinder_getCallingUid(); |
Tej Singh | 73597dc | 2020-03-13 18:42:40 -0700 | [diff] [blame] | 1261 | mPullerManager->RegisterPullAtomCallback(uid, atomTag, MillisToNano(coolDownMillis), |
| 1262 | MillisToNano(timeoutMillis), additiveFields, |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 1263 | pullerCallback); |
Tej Singh | 5918429 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 1264 | return Status::ok(); |
| 1265 | } |
| 1266 | |
Tej Singh | fa1c137 | 2019-12-05 20:36:54 -0800 | [diff] [blame] | 1267 | Status StatsService::unregisterPullAtomCallback(int32_t uid, int32_t atomTag) { |
| 1268 | ENFORCE_UID(AID_SYSTEM); |
| 1269 | VLOG("StatsService::unregisterPullAtomCallback called."); |
| 1270 | mPullerManager->UnregisterPullAtomCallback(uid, atomTag); |
| 1271 | return Status::ok(); |
| 1272 | } |
| 1273 | |
Tej Singh | 8f35860 | 2020-01-15 16:05:39 -0800 | [diff] [blame] | 1274 | Status StatsService::unregisterNativePullAtomCallback(int32_t atomTag) { |
Tej Singh | 10458ec | 2020-03-17 11:04:02 -0700 | [diff] [blame] | 1275 | if (!checkPermission(kPermissionRegisterPullAtom)) { |
| 1276 | return exception( |
| 1277 | EX_SECURITY, |
| 1278 | StringPrintf("Uid %d does not have the %s permission when unregistering atom %d", |
| 1279 | AIBinder_getCallingUid(), kPermissionRegisterPullAtom, atomTag)); |
| 1280 | } |
Tej Singh | 8f35860 | 2020-01-15 16:05:39 -0800 | [diff] [blame] | 1281 | VLOG("StatsService::unregisterNativePullAtomCallback called."); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1282 | int32_t uid = AIBinder_getCallingUid(); |
Tej Singh | 8f35860 | 2020-01-15 16:05:39 -0800 | [diff] [blame] | 1283 | mPullerManager->UnregisterPullAtomCallback(uid, atomTag); |
| 1284 | return Status::ok(); |
| 1285 | } |
| 1286 | |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1287 | Status StatsService::getRegisteredExperimentIds(std::vector<int64_t>* experimentIdsOut) { |
Jeffrey Huang | 80c9a97 | 2020-01-07 10:04:27 -0800 | [diff] [blame] | 1288 | ENFORCE_UID(AID_SYSTEM); |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1289 | // TODO: add verifier permission |
| 1290 | |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 1291 | experimentIdsOut->clear(); |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1292 | // Read the latest train info |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 1293 | vector<InstallTrainInfo> trainInfoList = StorageManager::readAllTrainInfo(); |
| 1294 | if (trainInfoList.empty()) { |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1295 | // No train info means no experiment IDs, return an empty list |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 1296 | return Status::ok(); |
| 1297 | } |
| 1298 | |
| 1299 | // Copy the experiment IDs to the out vector |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 1300 | for (InstallTrainInfo& trainInfo : trainInfoList) { |
| 1301 | experimentIdsOut->insert(experimentIdsOut->end(), |
| 1302 | trainInfo.experimentIds.begin(), |
| 1303 | trainInfo.experimentIds.end()); |
| 1304 | } |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 1305 | return Status::ok(); |
| 1306 | } |
| 1307 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1308 | |
| 1309 | void StatsService::statsCompanionServiceDied(void* cookie) { |
| 1310 | auto thiz = static_cast<StatsService*>(cookie); |
| 1311 | thiz->statsCompanionServiceDiedImpl(); |
| 1312 | } |
| 1313 | |
| 1314 | void StatsService::statsCompanionServiceDiedImpl() { |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 1315 | ALOGW("statscompanion service died"); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1316 | StatsdStats::getInstance().noteSystemServerRestart(getWallClockSec()); |
| 1317 | if (mProcessor != nullptr) { |
Howard Ro | e60992b | 2018-08-30 14:37:29 -0700 | [diff] [blame] | 1318 | ALOGW("Reset statsd upon system server restarts."); |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1319 | int64_t systemServerRestartNs = getElapsedRealtimeNs(); |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 1320 | ProtoOutputStream activeConfigsProto; |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1321 | mProcessor->WriteActiveConfigsToProtoOutputStream(systemServerRestartNs, |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 1322 | STATSCOMPANION_DIED, &activeConfigsProto); |
| 1323 | metadata::StatsMetadataList metadataList; |
| 1324 | mProcessor->WriteMetadataToProto(getWallClockNs(), |
| 1325 | systemServerRestartNs, &metadataList); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1326 | mProcessor->WriteDataToDisk(STATSCOMPANION_DIED, FAST); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1327 | mProcessor->resetConfigs(); |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1328 | |
| 1329 | std::string serializedActiveConfigs; |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 1330 | if (activeConfigsProto.serializeToString(&serializedActiveConfigs)) { |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1331 | ActiveConfigList activeConfigs; |
| 1332 | if (activeConfigs.ParseFromString(serializedActiveConfigs)) { |
| 1333 | mProcessor->SetConfigsActiveState(activeConfigs, systemServerRestartNs); |
| 1334 | } |
| 1335 | } |
Jeffrey Huang | 475677e | 2020-03-30 19:52:07 -0700 | [diff] [blame] | 1336 | mProcessor->SetMetadataState(metadataList, getWallClockNs(), systemServerRestartNs); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1337 | } |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 1338 | mAnomalyAlarmMonitor->setStatsCompanionService(nullptr); |
| 1339 | mPeriodicAlarmMonitor->setStatsCompanionService(nullptr); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 1340 | mPullerManager->SetStatsCompanionService(nullptr); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1343 | } // namespace statsd |
| 1344 | } // namespace os |
| 1345 | } // namespace android |