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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #include "Log.h" |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 18 | #include "statslog.h" |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 19 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "StatsLogProcessor.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | #include "metrics/CountMetricProducer.h" |
| 22 | #include "stats_util.h" |
| 23 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 24 | #include <log/log_event_list.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 26 | |
| 27 | using namespace android; |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 28 | using android::util::FIELD_COUNT_REPEATED; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 29 | using android::util::FIELD_TYPE_BOOL; |
| 30 | using android::util::FIELD_TYPE_FLOAT; |
| 31 | using android::util::FIELD_TYPE_INT32; |
| 32 | using android::util::FIELD_TYPE_INT64; |
| 33 | using android::util::FIELD_TYPE_MESSAGE; |
| 34 | using android::util::FIELD_TYPE_STRING; |
| 35 | using android::util::ProtoOutputStream; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 36 | using std::make_unique; |
| 37 | using std::unique_ptr; |
| 38 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | namespace os { |
| 42 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 43 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 44 | // for ConfigMetricsReport |
| 45 | const int FIELD_ID_CONFIG_KEY = 1; |
| 46 | const int FIELD_ID_METRICS = 2; |
| 47 | const int FIELD_ID_UID_MAP = 3; |
| 48 | // for ConfigKey |
| 49 | const int FIELD_ID_UID = 1; |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 50 | const int FIELD_ID_NAME = 2; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 51 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 52 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 53 | const sp<AnomalyMonitor>& anomalyMonitor, |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 54 | const std::function<void(const ConfigKey&)>& sendBroadcast) |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 55 | : mUidMap(uidMap), mAnomalyMonitor(anomalyMonitor), mSendBroadcast(sendBroadcast) { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 58 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 61 | void StatsLogProcessor::onAnomalyAlarmFired( |
| 62 | const uint64_t timestampNs, |
| 63 | unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet) { |
| 64 | for (const auto& anomaly : anomalySet) { |
| 65 | for (const auto& itr : mMetricsManagers) { |
| 66 | itr.second->onAnomalyAlarmFired(timestampNs, anomaly); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 71 | // 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] | 72 | void StatsLogProcessor::OnLogEvent(const LogEvent& msg) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 73 | // pass the event to metrics managers. |
| 74 | for (auto& pair : mMetricsManagers) { |
| 75 | pair.second->onLogEvent(msg); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 76 | flushIfNecessary(msg.GetTimestampNs(), pair.first, pair.second); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 77 | } |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 78 | |
| 79 | // 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] | 80 | // 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] | 81 | if (msg.GetTagId() == android::util::ISOLATED_UID_CHANGED) { |
| 82 | status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR; |
| 83 | bool is_create = msg.GetBool(3, &err); |
| 84 | auto parent_uid = int(msg.GetLong(1, &err2)); |
| 85 | auto isolated_uid = int(msg.GetLong(2, &err3)); |
| 86 | if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) { |
| 87 | if (is_create) { |
| 88 | mUidMap->assignIsolatedUid(isolated_uid, parent_uid); |
| 89 | } else { |
| 90 | mUidMap->removeIsolatedUid(isolated_uid, parent_uid); |
| 91 | } |
| 92 | } |
| 93 | } |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 96 | void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) { |
| 97 | auto it = mMetricsManagers.find(key); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 98 | if (it != mMetricsManagers.end()) { |
| 99 | it->second->finish(); |
| 100 | } |
| 101 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 102 | ALOGD("Updated configuration for key %s", key.ToString().c_str()); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 103 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 104 | unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config); |
| 105 | if (newMetricsManager->isConfigValid()) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 106 | mUidMap->OnConfigUpdated(key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 107 | newMetricsManager->setAnomalyMonitor(mAnomalyMonitor); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 108 | mMetricsManagers[key] = std::move(newMetricsManager); |
| 109 | // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)}); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 110 | ALOGD("StatsdConfig valid"); |
| 111 | } else { |
| 112 | // If there is any error in the config, don't use it. |
| 113 | ALOGD("StatsdConfig NOT valid"); |
| 114 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 115 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 116 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 117 | size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 118 | auto it = mMetricsManagers.find(key); |
| 119 | if (it == mMetricsManagers.end()) { |
| 120 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 121 | return 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 122 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 123 | return it->second->byteSize(); |
| 124 | } |
| 125 | |
| 126 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, vector<uint8_t>* outData) { |
| 127 | auto it = mMetricsManagers.find(key); |
| 128 | if (it == mMetricsManagers.end()) { |
| 129 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // This allows another broadcast to be sent within the rate-limit period if we get close to |
| 134 | // filling the buffer again soon. |
| 135 | mBroadcastTimesMutex.lock(); |
| 136 | mLastBroadcastTimes.erase(key); |
| 137 | mBroadcastTimesMutex.unlock(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 138 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 139 | ProtoOutputStream proto; |
| 140 | |
| 141 | // Fill in ConfigKey. |
| 142 | long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); |
| 143 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); |
| 144 | proto.write(FIELD_TYPE_STRING | FIELD_ID_NAME, key.GetName()); |
| 145 | proto.end(configKeyToken); |
| 146 | |
| 147 | // Fill in StatsLogReport's. |
| 148 | for (auto& m : it->second->onDumpReport()) { |
| 149 | // Add each vector of StatsLogReport into a repeated field. |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 150 | proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS, |
| 151 | reinterpret_cast<char*>(m.get()->data()), m.get()->size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 152 | } |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 153 | |
| 154 | // Fill in UidMap. |
| 155 | auto uidMap = mUidMap->getOutput(key); |
| 156 | const int uidMapSize = uidMap.ByteSize(); |
| 157 | char uidMapBuffer[uidMapSize]; |
| 158 | uidMap.SerializeToArray(&uidMapBuffer[0], uidMapSize); |
| 159 | proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize); |
| 160 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 161 | if (outData != nullptr) { |
| 162 | outData->clear(); |
| 163 | outData->resize(proto.size()); |
| 164 | size_t pos = 0; |
| 165 | auto iter = proto.data(); |
| 166 | while (iter.readBuffer() != NULL) { |
| 167 | size_t toRead = iter.currentToRead(); |
| 168 | std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead); |
| 169 | pos += toRead; |
| 170 | iter.rp()->move(toRead); |
| 171 | } |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 172 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 175 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
| 176 | auto it = mMetricsManagers.find(key); |
| 177 | if (it != mMetricsManagers.end()) { |
| 178 | it->second->finish(); |
| 179 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 180 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 181 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 182 | |
| 183 | std::lock_guard<std::mutex> lock(mBroadcastTimesMutex); |
| 184 | mLastBroadcastTimes.erase(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 185 | } |
| 186 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 187 | void StatsLogProcessor::flushIfNecessary(uint64_t timestampNs, |
| 188 | const ConfigKey& key, |
| 189 | const unique_ptr<MetricsManager>& metricsManager) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 190 | std::lock_guard<std::mutex> lock(mBroadcastTimesMutex); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 191 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 192 | size_t totalBytes = metricsManager->byteSize(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 193 | if (totalBytes > .9 * kMaxSerializedBytes) { // Send broadcast so that receivers can pull data. |
| 194 | auto lastFlushNs = mLastBroadcastTimes.find(key); |
| 195 | if (lastFlushNs != mLastBroadcastTimes.end()) { |
| 196 | if (timestampNs - lastFlushNs->second < kMinBroadcastPeriod) { |
| 197 | return; |
| 198 | } |
| 199 | } |
| 200 | mLastBroadcastTimes[key] = timestampNs; |
| 201 | ALOGD("StatsD requesting broadcast for %s", key.ToString().c_str()); |
| 202 | mSendBroadcast(key); |
| 203 | } else if (totalBytes > kMaxSerializedBytes) { // Too late. We need to start clearing data. |
| 204 | // We ignore the return value so we force each metric producer to clear its contents. |
| 205 | metricsManager->onDumpReport(); |
| 206 | ALOGD("StatsD had to toss out metrics for %s", key.ToString().c_str()); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 210 | } // namespace statsd |
| 211 | } // namespace os |
| 212 | } // namespace android |