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