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" |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 27 | #include "external/StatsPullerManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 28 | #include "stats_util.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 29 | #include "storage/StorageManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 30 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 31 | #include <log/log_event_list.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 32 | #include <utils/Errors.h> |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 33 | |
| 34 | using namespace android; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 35 | using android::base::StringPrintf; |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 36 | using android::util::FIELD_COUNT_REPEATED; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 37 | using android::util::FIELD_TYPE_BOOL; |
| 38 | using android::util::FIELD_TYPE_FLOAT; |
| 39 | using android::util::FIELD_TYPE_INT32; |
| 40 | using android::util::FIELD_TYPE_INT64; |
| 41 | using android::util::FIELD_TYPE_MESSAGE; |
| 42 | using android::util::FIELD_TYPE_STRING; |
| 43 | using android::util::ProtoOutputStream; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 44 | using std::make_unique; |
| 45 | using std::unique_ptr; |
| 46 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 47 | |
| 48 | namespace android { |
| 49 | namespace os { |
| 50 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 51 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 52 | // for ConfigMetricsReportList |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 53 | const int FIELD_ID_CONFIG_KEY = 1; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 54 | const int FIELD_ID_REPORTS = 2; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 55 | // for ConfigKey |
| 56 | const int FIELD_ID_UID = 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 57 | const int FIELD_ID_ID = 2; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 58 | // for ConfigMetricsReport |
| 59 | const int FIELD_ID_METRICS = 1; |
| 60 | const int FIELD_ID_UID_MAP = 2; |
| 61 | |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 62 | #define STATS_DATA_DIR "/data/misc/stats-data" |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 63 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 64 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 65 | const sp<AnomalyMonitor>& anomalyMonitor, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 66 | const long timeBaseSec, |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 67 | const std::function<void(const ConfigKey&)>& sendBroadcast) |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 68 | : mUidMap(uidMap), |
| 69 | mAnomalyMonitor(anomalyMonitor), |
| 70 | mSendBroadcast(sendBroadcast), |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 71 | mTimeBaseSec(timeBaseSec) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 72 | // On each initialization of StatsLogProcessor, check stats-data directory to see if there is |
| 73 | // any left over data to be read. |
| 74 | StorageManager::sendBroadcast(STATS_DATA_DIR, mSendBroadcast); |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 75 | StatsPullerManager statsPullerManager; |
| 76 | statsPullerManager.SetTimeBaseSec(mTimeBaseSec); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 79 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 82 | void StatsLogProcessor::onAnomalyAlarmFired( |
| 83 | const uint64_t timestampNs, |
| 84 | unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 85 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 86 | for (const auto& itr : mMetricsManagers) { |
| 87 | itr.second->onAnomalyAlarmFired(timestampNs, anomalySet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 91 | // 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] | 92 | void StatsLogProcessor::OnLogEvent(const LogEvent& msg) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 93 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 94 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 95 | StatsdStats::getInstance().noteAtomLogged(msg.GetTagId(), msg.GetTimestampNs() / NS_PER_SEC); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 96 | // pass the event to metrics managers. |
Yao Chen | 312e898 | 2017-12-05 15:29:03 -0800 | [diff] [blame] | 97 | for (auto& pair : mMetricsManagers) { |
| 98 | pair.second->onLogEvent(msg); |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 99 | flushIfNecessaryLocked(msg.GetTimestampNs(), pair.first, *(pair.second)); |
Yao Chen | 312e898 | 2017-12-05 15:29:03 -0800 | [diff] [blame] | 100 | } |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 101 | // 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] | 102 | // 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] | 103 | if (msg.GetTagId() == android::util::ISOLATED_UID_CHANGED) { |
| 104 | status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR; |
| 105 | bool is_create = msg.GetBool(3, &err); |
| 106 | auto parent_uid = int(msg.GetLong(1, &err2)); |
| 107 | auto isolated_uid = int(msg.GetLong(2, &err3)); |
| 108 | if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) { |
| 109 | if (is_create) { |
| 110 | mUidMap->assignIsolatedUid(isolated_uid, parent_uid); |
| 111 | } else { |
| 112 | mUidMap->removeIsolatedUid(isolated_uid, parent_uid); |
| 113 | } |
| 114 | } |
| 115 | } |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 118 | void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 119 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 120 | ALOGD("Updated configuration for key %s", key.ToString().c_str()); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 121 | sp<MetricsManager> newMetricsManager = new MetricsManager(key, config, mTimeBaseSec, mUidMap); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 122 | auto it = mMetricsManagers.find(key); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 123 | if (it == mMetricsManagers.end() && mMetricsManagers.size() > StatsdStats::kMaxConfigCount) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 124 | ALOGE("Can't accept more configs!"); |
| 125 | return; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 128 | if (newMetricsManager->isConfigValid()) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 129 | mUidMap->OnConfigUpdated(key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 130 | newMetricsManager->setAnomalyMonitor(mAnomalyMonitor); |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 131 | if (newMetricsManager->shouldAddUidMapListener()) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 132 | // We have to add listener after the MetricsManager is constructed because it's |
| 133 | // not safe to create wp or sp from this pointer inside its constructor. |
| 134 | mUidMap->addListener(newMetricsManager.get()); |
| 135 | } |
| 136 | mMetricsManagers[key] = newMetricsManager; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 137 | // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)}); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 138 | VLOG("StatsdConfig valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 139 | } else { |
| 140 | // If there is any error in the config, don't use it. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 141 | ALOGE("StatsdConfig NOT valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 142 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 143 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 144 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 145 | size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 146 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 147 | auto it = mMetricsManagers.find(key); |
| 148 | if (it == mMetricsManagers.end()) { |
| 149 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 150 | return 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 151 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 152 | return it->second->byteSize(); |
| 153 | } |
| 154 | |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 155 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, const uint64_t& dumpTimeStampNs, |
| 156 | ConfigMetricsReportList* report) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 157 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 158 | auto it = mMetricsManagers.find(key); |
| 159 | if (it == mMetricsManagers.end()) { |
| 160 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
| 161 | return; |
| 162 | } |
| 163 | report->mutable_config_key()->set_uid(key.GetUid()); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 164 | report->mutable_config_key()->set_id(key.GetId()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 165 | ConfigMetricsReport* configMetricsReport = report->add_reports(); |
| 166 | it->second->onDumpReport(dumpTimeStampNs, configMetricsReport); |
| 167 | // TODO: dump uid mapping. |
| 168 | } |
| 169 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 170 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, vector<uint8_t>* outData) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 171 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 172 | auto it = mMetricsManagers.find(key); |
| 173 | if (it == mMetricsManagers.end()) { |
| 174 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // This allows another broadcast to be sent within the rate-limit period if we get close to |
| 179 | // filling the buffer again soon. |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 180 | mLastBroadcastTimes.erase(key); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 181 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 182 | ProtoOutputStream proto; |
| 183 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 184 | // Start of ConfigKey. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 185 | long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); |
| 186 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 187 | proto.write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId()); |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 188 | proto.end(configKeyToken); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 189 | // End of ConfigKey. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 190 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 191 | // Start of ConfigMetricsReport (reports). |
| 192 | long long reportsToken = |
| 193 | proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS); |
| 194 | |
| 195 | // First, fill in ConfigMetricsReport using current data on memory, which |
| 196 | // starts from filling in StatsLogReport's. |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 197 | it->second->onDumpReport(&proto); |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 198 | |
| 199 | // Fill in UidMap. |
| 200 | auto uidMap = mUidMap->getOutput(key); |
| 201 | const int uidMapSize = uidMap.ByteSize(); |
| 202 | char uidMapBuffer[uidMapSize]; |
| 203 | uidMap.SerializeToArray(&uidMapBuffer[0], uidMapSize); |
| 204 | proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize); |
| 205 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 206 | // End of ConfigMetricsReport (reports). |
| 207 | proto.end(reportsToken); |
| 208 | |
| 209 | // Then, check stats-data directory to see there's any file containing |
| 210 | // ConfigMetricsReport from previous shutdowns to concatenate to reports. |
| 211 | StorageManager::appendConfigMetricsReport(STATS_DATA_DIR, proto); |
| 212 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 213 | if (outData != nullptr) { |
| 214 | outData->clear(); |
| 215 | outData->resize(proto.size()); |
| 216 | size_t pos = 0; |
| 217 | auto iter = proto.data(); |
| 218 | while (iter.readBuffer() != NULL) { |
| 219 | size_t toRead = iter.currentToRead(); |
| 220 | std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead); |
| 221 | pos += toRead; |
| 222 | iter.rp()->move(toRead); |
| 223 | } |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 224 | } |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 225 | StatsdStats::getInstance().noteMetricsReportSent(key); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 228 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 229 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 230 | auto it = mMetricsManagers.find(key); |
| 231 | if (it != mMetricsManagers.end()) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 232 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 233 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 234 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 235 | StatsdStats::getInstance().noteConfigRemoved(key); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 236 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 237 | mLastBroadcastTimes.erase(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 240 | void StatsLogProcessor::flushIfNecessaryLocked( |
| 241 | uint64_t timestampNs, const ConfigKey& key, MetricsManager& metricsManager) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 242 | auto lastCheckTime = mLastByteSizeTimes.find(key); |
| 243 | if (lastCheckTime != mLastByteSizeTimes.end()) { |
| 244 | if (timestampNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) { |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // We suspect that the byteSize() computation is expensive, so we set a rate limit. |
| 250 | size_t totalBytes = metricsManager.byteSize(); |
| 251 | mLastByteSizeTimes[key] = timestampNs; |
| 252 | if (totalBytes > |
| 253 | StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data. |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 254 | // TODO(b/70571383): By 12/15/2017 add API to drop data directly |
| 255 | ProtoOutputStream proto; |
| 256 | metricsManager.onDumpReport(&proto); |
David Chen | 1294295 | 2017-12-04 14:28:43 -0800 | [diff] [blame] | 257 | StatsdStats::getInstance().noteDataDropped(key); |
| 258 | VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str()); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 259 | } else if (totalBytes > .9 * StatsdStats::kMaxMetricsBytesPerConfig) { |
| 260 | // Send broadcast so that receivers can pull data. |
| 261 | auto lastBroadcastTime = mLastBroadcastTimes.find(key); |
| 262 | if (lastBroadcastTime != mLastBroadcastTimes.end()) { |
| 263 | if (timestampNs - lastBroadcastTime->second < StatsdStats::kMinBroadcastPeriodNs) { |
| 264 | VLOG("StatsD would've sent a broadcast but the rate limit stopped us."); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | } |
| 268 | mLastBroadcastTimes[key] = timestampNs; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 269 | VLOG("StatsD requesting broadcast for %s", key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 270 | mSendBroadcast(key); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 271 | StatsdStats::getInstance().noteBroadcastSent(key); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 275 | void StatsLogProcessor::WriteDataToDisk() { |
| 276 | mkdir(STATS_DATA_DIR, S_IRWXU); |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 277 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 278 | for (auto& pair : mMetricsManagers) { |
| 279 | const ConfigKey& key = pair.first; |
| 280 | vector<uint8_t> data; |
| 281 | onDumpReport(key, &data); |
| 282 | // TODO: Add a guardrail to prevent accumulation of file on disk. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 283 | string file_name = StringPrintf("%s/%d-%lld-%ld", STATS_DATA_DIR, key.GetUid(), |
| 284 | (long long)key.GetId(), time(nullptr)); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 285 | StorageManager::writeFile(file_name.c_str(), &data[0], data.size()); |
| 286 | } |
| 287 | } |
| 288 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 289 | } // namespace statsd |
| 290 | } // namespace os |
| 291 | } // namespace android |