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" |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 18 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 19 | #include "StatsLogProcessor.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "metrics/CountMetricProducer.h" |
| 21 | #include "stats_util.h" |
| 22 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 23 | #include <log/log_event_list.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 24 | #include <utils/Errors.h> |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace android; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame^] | 27 | using android::util::FIELD_TYPE_BOOL; |
| 28 | using android::util::FIELD_TYPE_FLOAT; |
| 29 | using android::util::FIELD_TYPE_INT32; |
| 30 | using android::util::FIELD_TYPE_INT64; |
| 31 | using android::util::FIELD_TYPE_MESSAGE; |
| 32 | using android::util::FIELD_TYPE_STRING; |
| 33 | using android::util::ProtoOutputStream; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 34 | using std::make_unique; |
| 35 | using std::unique_ptr; |
| 36 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | namespace os { |
| 40 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 41 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame^] | 42 | // for ConfigMetricsReport |
| 43 | const int FIELD_ID_CONFIG_KEY = 1; |
| 44 | const int FIELD_ID_METRICS = 2; |
| 45 | const int FIELD_ID_UID_MAP = 3; |
| 46 | // for ConfigKey |
| 47 | const int FIELD_ID_UID = 1; |
| 48 | const int FIELD_ID_NAME = 1; |
| 49 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 50 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
| 51 | const std::function<void(const vector<uint8_t>&)>& pushLog) |
yro | fd05a4e | 2017-10-26 14:08:26 -0700 | [diff] [blame] | 52 | : mUidMap(uidMap), mPushLog(pushLog) { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 55 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 58 | // 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] | 59 | void StatsLogProcessor::OnLogEvent(const LogEvent& msg) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 60 | // pass the event to metrics managers. |
| 61 | for (auto& pair : mMetricsManagers) { |
| 62 | pair.second->onLogEvent(msg); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 63 | flushIfNecessary(msg.GetTimestampNs(), pair.first, pair.second); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 67 | void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) { |
| 68 | auto it = mMetricsManagers.find(key); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 69 | if (it != mMetricsManagers.end()) { |
| 70 | it->second->finish(); |
| 71 | } |
| 72 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 73 | ALOGD("Updated configuration for key %s", key.ToString().c_str()); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 74 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 75 | unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config); |
| 76 | if (newMetricsManager->isConfigValid()) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 77 | mUidMap->OnConfigUpdated(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 78 | mMetricsManagers[key] = std::move(newMetricsManager); |
| 79 | // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)}); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 80 | ALOGD("StatsdConfig valid"); |
| 81 | } else { |
| 82 | // If there is any error in the config, don't use it. |
| 83 | ALOGD("StatsdConfig NOT valid"); |
| 84 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 85 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 86 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame^] | 87 | vector<uint8_t> StatsLogProcessor::onDumpReport(const ConfigKey& key) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 88 | auto it = mMetricsManagers.find(key); |
| 89 | if (it == mMetricsManagers.end()) { |
| 90 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame^] | 91 | return vector<uint8_t>(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 92 | } |
| 93 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame^] | 94 | ProtoOutputStream proto; |
| 95 | |
| 96 | // Fill in ConfigKey. |
| 97 | long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); |
| 98 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); |
| 99 | proto.write(FIELD_TYPE_STRING | FIELD_ID_NAME, key.GetName()); |
| 100 | proto.end(configKeyToken); |
| 101 | |
| 102 | // Fill in StatsLogReport's. |
| 103 | for (auto& m : it->second->onDumpReport()) { |
| 104 | // Add each vector of StatsLogReport into a repeated field. |
| 105 | proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_METRICS, reinterpret_cast<char*>(m.get()->data()), |
| 106 | m.get()->size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 107 | } |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame^] | 108 | |
| 109 | // Fill in UidMap. |
| 110 | auto uidMap = mUidMap->getOutput(key); |
| 111 | const int uidMapSize = uidMap.ByteSize(); |
| 112 | char uidMapBuffer[uidMapSize]; |
| 113 | uidMap.SerializeToArray(&uidMapBuffer[0], uidMapSize); |
| 114 | proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize); |
| 115 | |
| 116 | vector<uint8_t> buffer(proto.size()); |
| 117 | size_t pos = 0; |
| 118 | auto iter = proto.data(); |
| 119 | while (iter.readBuffer() != NULL) { |
| 120 | size_t toRead = iter.currentToRead(); |
| 121 | std::memcpy(&buffer[pos], iter.readBuffer(), toRead); |
| 122 | pos += toRead; |
| 123 | iter.rp()->move(toRead); |
| 124 | } |
| 125 | |
| 126 | return buffer; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 129 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
| 130 | auto it = mMetricsManagers.find(key); |
| 131 | if (it != mMetricsManagers.end()) { |
| 132 | it->second->finish(); |
| 133 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 134 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 135 | } |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 136 | auto flushTime = mLastFlushTimes.find(key); |
| 137 | if (flushTime != mLastFlushTimes.end()) { |
| 138 | mLastFlushTimes.erase(flushTime); |
| 139 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 140 | } |
| 141 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 142 | void StatsLogProcessor::flushIfNecessary(uint64_t timestampNs, |
| 143 | const ConfigKey& key, |
| 144 | const unique_ptr<MetricsManager>& metricsManager) { |
| 145 | auto lastFlushNs = mLastFlushTimes.find(key); |
| 146 | if (lastFlushNs != mLastFlushTimes.end()) { |
| 147 | if (timestampNs - lastFlushNs->second < kMinFlushPeriod) { |
| 148 | return; |
| 149 | } |
| 150 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 151 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 152 | size_t totalBytes = metricsManager->byteSize(); |
| 153 | if (totalBytes > kMaxSerializedBytes) { |
| 154 | flush(); |
| 155 | mLastFlushTimes[key] = std::move(timestampNs); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | void StatsLogProcessor::flush() { |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 160 | // TODO: Take ConfigKey as an argument and flush metrics related to the |
| 161 | // ConfigKey. Also, create a wrapper that holds a repeated field of |
| 162 | // StatsLogReport's. |
| 163 | /* |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 164 | StatsLogReport logReport; |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 165 | const int numBytes = logReport.ByteSize(); |
| 166 | vector<uint8_t> logReportBuffer(numBytes); |
| 167 | logReport.SerializeToArray(&logReportBuffer[0], numBytes); |
| 168 | mPushLog(logReportBuffer); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 169 | */ |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 172 | } // namespace statsd |
| 173 | } // namespace os |
| 174 | } // namespace android |