Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 17 | #define DEBUG true // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 19 | #include "statslog.h" |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 20 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 21 | #include <android-base/file.h> |
| 22 | #include <dirent.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 23 | #include "StatsLogProcessor.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 24 | #include "android-base/stringprintf.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | #include "guardrail/StatsdStats.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 26 | #include "metrics/CountMetricProducer.h" |
| 27 | #include "stats_util.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 28 | #include "storage/StorageManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 29 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 30 | #include <log/log_event_list.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 31 | #include <utils/Errors.h> |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 32 | |
| 33 | using namespace android; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 34 | using android::base::StringPrintf; |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 35 | using android::util::FIELD_COUNT_REPEATED; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 36 | using android::util::FIELD_TYPE_BOOL; |
| 37 | using android::util::FIELD_TYPE_FLOAT; |
| 38 | using android::util::FIELD_TYPE_INT32; |
| 39 | using android::util::FIELD_TYPE_INT64; |
| 40 | using android::util::FIELD_TYPE_MESSAGE; |
| 41 | using android::util::FIELD_TYPE_STRING; |
| 42 | using android::util::ProtoOutputStream; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 43 | using std::make_unique; |
| 44 | using std::unique_ptr; |
| 45 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 46 | |
| 47 | namespace android { |
| 48 | namespace os { |
| 49 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 50 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 51 | // for ConfigMetricsReportList |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 52 | const int FIELD_ID_CONFIG_KEY = 1; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 53 | const int FIELD_ID_REPORTS = 2; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 54 | // for ConfigKey |
| 55 | const int FIELD_ID_UID = 1; |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 56 | const int FIELD_ID_NAME = 2; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 57 | // for ConfigMetricsReport |
| 58 | const int FIELD_ID_METRICS = 1; |
| 59 | const int FIELD_ID_UID_MAP = 2; |
| 60 | |
| 61 | #define STATS_DATA_DIR "/data/system/stats-data" |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 62 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 63 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 64 | const sp<AnomalyMonitor>& anomalyMonitor, |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 65 | const std::function<void(const ConfigKey&)>& sendBroadcast) |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 66 | : mUidMap(uidMap), mAnomalyMonitor(anomalyMonitor), mSendBroadcast(sendBroadcast) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 67 | // On each initialization of StatsLogProcessor, check stats-data directory to see if there is |
| 68 | // any left over data to be read. |
| 69 | StorageManager::sendBroadcast(STATS_DATA_DIR, mSendBroadcast); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 72 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 75 | void StatsLogProcessor::onAnomalyAlarmFired( |
| 76 | const uint64_t timestampNs, |
| 77 | unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet) { |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 78 | for (const auto& itr : mMetricsManagers) { |
| 79 | itr.second->onAnomalyAlarmFired(timestampNs, anomalySet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 83 | // TODO: what if statsd service restarts? How do we know what logs are already processed before? |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 84 | void StatsLogProcessor::OnLogEvent(const LogEvent& msg) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 85 | StatsdStats::getInstance().noteAtomLogged(msg.GetTagId(), msg.GetTimestampNs() / NS_PER_SEC); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 86 | // pass the event to metrics managers. |
| 87 | for (auto& pair : mMetricsManagers) { |
| 88 | pair.second->onLogEvent(msg); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 89 | flushIfNecessary(msg.GetTimestampNs(), pair.first, pair.second); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 90 | } |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 91 | |
| 92 | // Hard-coded logic to update the isolated uid's in the uid-map. |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 93 | // The field numbers need to be currently updated by hand with atoms.proto |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 94 | if (msg.GetTagId() == android::util::ISOLATED_UID_CHANGED) { |
| 95 | status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR; |
| 96 | bool is_create = msg.GetBool(3, &err); |
| 97 | auto parent_uid = int(msg.GetLong(1, &err2)); |
| 98 | auto isolated_uid = int(msg.GetLong(2, &err3)); |
| 99 | if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) { |
| 100 | if (is_create) { |
| 101 | mUidMap->assignIsolatedUid(isolated_uid, parent_uid); |
| 102 | } else { |
| 103 | mUidMap->removeIsolatedUid(isolated_uid, parent_uid); |
| 104 | } |
| 105 | } |
| 106 | } |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 109 | void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 110 | ALOGD("Updated configuration for key %s", key.ToString().c_str()); |
| 111 | unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(key, config); |
| 112 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 113 | auto it = mMetricsManagers.find(key); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 114 | if (it != mMetricsManagers.end()) { |
| 115 | it->second->finish(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 116 | } else if (mMetricsManagers.size() > StatsdStats::kMaxConfigCount) { |
| 117 | ALOGE("Can't accept more configs!"); |
| 118 | return; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 121 | if (newMetricsManager->isConfigValid()) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 122 | mUidMap->OnConfigUpdated(key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 123 | newMetricsManager->setAnomalyMonitor(mAnomalyMonitor); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 124 | mMetricsManagers[key] = std::move(newMetricsManager); |
| 125 | // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)}); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 126 | VLOG("StatsdConfig valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 127 | } else { |
| 128 | // If there is any error in the config, don't use it. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 129 | ALOGE("StatsdConfig NOT valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 130 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 131 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 132 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 133 | size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 134 | auto it = mMetricsManagers.find(key); |
| 135 | if (it == mMetricsManagers.end()) { |
| 136 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 137 | return 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 138 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 139 | return it->second->byteSize(); |
| 140 | } |
| 141 | |
| 142 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, vector<uint8_t>* outData) { |
| 143 | auto it = mMetricsManagers.find(key); |
| 144 | if (it == mMetricsManagers.end()) { |
| 145 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | // This allows another broadcast to be sent within the rate-limit period if we get close to |
| 150 | // filling the buffer again soon. |
| 151 | mBroadcastTimesMutex.lock(); |
| 152 | mLastBroadcastTimes.erase(key); |
| 153 | mBroadcastTimesMutex.unlock(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 154 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 155 | ProtoOutputStream proto; |
| 156 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 157 | // Start of ConfigKey. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 158 | long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); |
| 159 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); |
| 160 | proto.write(FIELD_TYPE_STRING | FIELD_ID_NAME, key.GetName()); |
| 161 | proto.end(configKeyToken); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 162 | // End of ConfigKey. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 163 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 164 | // Start of ConfigMetricsReport (reports). |
| 165 | long long reportsToken = |
| 166 | proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS); |
| 167 | |
| 168 | // First, fill in ConfigMetricsReport using current data on memory, which |
| 169 | // starts from filling in StatsLogReport's. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 170 | for (auto& m : it->second->onDumpReport()) { |
| 171 | // Add each vector of StatsLogReport into a repeated field. |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 172 | proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS, |
| 173 | reinterpret_cast<char*>(m.get()->data()), m.get()->size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 174 | } |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 175 | |
| 176 | // Fill in UidMap. |
| 177 | auto uidMap = mUidMap->getOutput(key); |
| 178 | const int uidMapSize = uidMap.ByteSize(); |
| 179 | char uidMapBuffer[uidMapSize]; |
| 180 | uidMap.SerializeToArray(&uidMapBuffer[0], uidMapSize); |
| 181 | proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize); |
| 182 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 183 | // End of ConfigMetricsReport (reports). |
| 184 | proto.end(reportsToken); |
| 185 | |
| 186 | // Then, check stats-data directory to see there's any file containing |
| 187 | // ConfigMetricsReport from previous shutdowns to concatenate to reports. |
| 188 | StorageManager::appendConfigMetricsReport(STATS_DATA_DIR, proto); |
| 189 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 190 | if (outData != nullptr) { |
| 191 | outData->clear(); |
| 192 | outData->resize(proto.size()); |
| 193 | size_t pos = 0; |
| 194 | auto iter = proto.data(); |
| 195 | while (iter.readBuffer() != NULL) { |
| 196 | size_t toRead = iter.currentToRead(); |
| 197 | std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead); |
| 198 | pos += toRead; |
| 199 | iter.rp()->move(toRead); |
| 200 | } |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 201 | } |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 202 | StatsdStats::getInstance().noteMetricsReportSent(key); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 205 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
| 206 | auto it = mMetricsManagers.find(key); |
| 207 | if (it != mMetricsManagers.end()) { |
| 208 | it->second->finish(); |
| 209 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 210 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 211 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 212 | StatsdStats::getInstance().noteConfigRemoved(key); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 213 | |
| 214 | std::lock_guard<std::mutex> lock(mBroadcastTimesMutex); |
| 215 | mLastBroadcastTimes.erase(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 216 | } |
| 217 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 218 | void StatsLogProcessor::flushIfNecessary(uint64_t timestampNs, |
| 219 | const ConfigKey& key, |
| 220 | const unique_ptr<MetricsManager>& metricsManager) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 221 | std::lock_guard<std::mutex> lock(mBroadcastTimesMutex); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 222 | |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 223 | size_t totalBytes = metricsManager->byteSize() + mUidMap->getBytesUsed(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 224 | if (totalBytes > .9 * kMaxSerializedBytes) { // Send broadcast so that receivers can pull data. |
| 225 | auto lastFlushNs = mLastBroadcastTimes.find(key); |
| 226 | if (lastFlushNs != mLastBroadcastTimes.end()) { |
| 227 | if (timestampNs - lastFlushNs->second < kMinBroadcastPeriod) { |
| 228 | return; |
| 229 | } |
| 230 | } |
| 231 | mLastBroadcastTimes[key] = timestampNs; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 232 | VLOG("StatsD requesting broadcast for %s", key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 233 | mSendBroadcast(key); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 234 | StatsdStats::getInstance().noteBroadcastSent(key); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 235 | } else if (totalBytes > kMaxSerializedBytes) { // Too late. We need to start clearing data. |
| 236 | // We ignore the return value so we force each metric producer to clear its contents. |
| 237 | metricsManager->onDumpReport(); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 238 | StatsdStats::getInstance().noteDataDropped(key); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 239 | VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str()); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 243 | void StatsLogProcessor::WriteDataToDisk() { |
| 244 | mkdir(STATS_DATA_DIR, S_IRWXU); |
| 245 | for (auto& pair : mMetricsManagers) { |
| 246 | const ConfigKey& key = pair.first; |
| 247 | vector<uint8_t> data; |
| 248 | onDumpReport(key, &data); |
| 249 | // TODO: Add a guardrail to prevent accumulation of file on disk. |
| 250 | string file_name = StringPrintf("%s/%d-%s-%ld", STATS_DATA_DIR, key.GetUid(), |
| 251 | key.GetName().c_str(), time(nullptr)); |
| 252 | StorageManager::writeFile(file_name.c_str(), &data[0], data.size()); |
| 253 | } |
| 254 | } |
| 255 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 256 | } // namespace statsd |
| 257 | } // namespace os |
| 258 | } // namespace android |