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" |
| 20 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
| 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; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 28 | using std::make_unique; |
| 29 | using std::unique_ptr; |
| 30 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 35 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 36 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
| 37 | const std::function<void(const vector<uint8_t>&)>& pushLog) |
yro | fd05a4e | 2017-10-26 14:08:26 -0700 | [diff] [blame] | 38 | : mUidMap(uidMap), mPushLog(pushLog) { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 41 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 44 | // 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] | 45 | void StatsLogProcessor::OnLogEvent(const LogEvent& msg) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 46 | // pass the event to metrics managers. |
| 47 | for (auto& pair : mMetricsManagers) { |
| 48 | pair.second->onLogEvent(msg); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 49 | flushIfNecessary(msg.GetTimestampNs(), pair.first, pair.second); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 53 | void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) { |
| 54 | auto it = mMetricsManagers.find(key); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 55 | if (it != mMetricsManagers.end()) { |
| 56 | it->second->finish(); |
| 57 | } |
| 58 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 59 | ALOGD("Updated configuration for key %s", key.ToString().c_str()); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 60 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 61 | unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config); |
| 62 | if (newMetricsManager->isConfigValid()) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 63 | mUidMap->OnConfigUpdated(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 64 | mMetricsManagers[key] = std::move(newMetricsManager); |
| 65 | // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)}); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 66 | ALOGD("StatsdConfig valid"); |
| 67 | } else { |
| 68 | // If there is any error in the config, don't use it. |
| 69 | ALOGD("StatsdConfig NOT valid"); |
| 70 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 71 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 72 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 73 | ConfigMetricsReport StatsLogProcessor::onDumpReport(const ConfigKey& key) { |
| 74 | ConfigMetricsReport report; |
| 75 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 76 | auto it = mMetricsManagers.find(key); |
| 77 | if (it == mMetricsManagers.end()) { |
| 78 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 79 | return report; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 80 | } |
| 81 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 82 | auto set_key = report.mutable_config_key(); |
| 83 | set_key->set_uid(key.GetUid()); |
| 84 | set_key->set_name(key.GetName()); |
| 85 | for (auto m : it->second->onDumpReport()) { |
| 86 | // Transfer the vector of StatsLogReport into a field |
| 87 | // TODO: perhaps we just have bytes being returned from onDumpReport and transfer bytes |
| 88 | auto dest = report.add_metrics(); |
| 89 | *dest = m; |
| 90 | } |
| 91 | auto temp = mUidMap->getOutput(key); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 92 | report.mutable_uid_map()->Swap(&temp); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 93 | return report; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 96 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
| 97 | auto it = mMetricsManagers.find(key); |
| 98 | if (it != mMetricsManagers.end()) { |
| 99 | it->second->finish(); |
| 100 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 101 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 102 | } |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 103 | auto flushTime = mLastFlushTimes.find(key); |
| 104 | if (flushTime != mLastFlushTimes.end()) { |
| 105 | mLastFlushTimes.erase(flushTime); |
| 106 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 107 | } |
| 108 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 109 | void StatsLogProcessor::flushIfNecessary(uint64_t timestampNs, |
| 110 | const ConfigKey& key, |
| 111 | const unique_ptr<MetricsManager>& metricsManager) { |
| 112 | auto lastFlushNs = mLastFlushTimes.find(key); |
| 113 | if (lastFlushNs != mLastFlushTimes.end()) { |
| 114 | if (timestampNs - lastFlushNs->second < kMinFlushPeriod) { |
| 115 | return; |
| 116 | } |
| 117 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 118 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 119 | size_t totalBytes = metricsManager->byteSize(); |
| 120 | if (totalBytes > kMaxSerializedBytes) { |
| 121 | flush(); |
| 122 | mLastFlushTimes[key] = std::move(timestampNs); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | void StatsLogProcessor::flush() { |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 127 | // TODO: Take ConfigKey as an argument and flush metrics related to the |
| 128 | // ConfigKey. Also, create a wrapper that holds a repeated field of |
| 129 | // StatsLogReport's. |
| 130 | /* |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 131 | StatsLogReport logReport; |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 132 | const int numBytes = logReport.ByteSize(); |
| 133 | vector<uint8_t> logReportBuffer(numBytes); |
| 134 | logReport.SerializeToArray(&logReportBuffer[0], numBytes); |
| 135 | mPushLog(logReportBuffer); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 136 | */ |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 139 | } // namespace statsd |
| 140 | } // namespace os |
| 141 | } // namespace android |