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) |
| 38 | : m_dropbox_writer("all-logs"), 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 | // TODO: Use EventMetric to filter the events we want to log. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 47 | /* TODO: Convert this when we have the generic protobuf writing library in. |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 48 | EventMetricData eventMetricData = parse(msg); |
| 49 | m_dropbox_writer.addEventMetricData(eventMetricData); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 50 | */ |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 51 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 52 | // pass the event to metrics managers. |
| 53 | for (auto& pair : mMetricsManagers) { |
| 54 | pair.second->onLogEvent(msg); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 58 | void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) { |
| 59 | auto it = mMetricsManagers.find(key); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 60 | if (it != mMetricsManagers.end()) { |
| 61 | it->second->finish(); |
| 62 | } |
| 63 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 64 | ALOGD("Updated configuration for key %s", key.ToString().c_str()); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 65 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 66 | unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config); |
| 67 | if (newMetricsManager->isConfigValid()) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 68 | mMetricsManagers[key] = std::move(newMetricsManager); |
| 69 | // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)}); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 70 | ALOGD("StatsdConfig valid"); |
| 71 | } else { |
| 72 | // If there is any error in the config, don't use it. |
| 73 | ALOGD("StatsdConfig NOT valid"); |
| 74 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 75 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 76 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 77 | vector<StatsLogReport> StatsLogProcessor::onDumpReport(const ConfigKey& key) { |
| 78 | auto it = mMetricsManagers.find(key); |
| 79 | if (it == mMetricsManagers.end()) { |
| 80 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
| 81 | return vector<StatsLogReport>(); |
| 82 | } |
| 83 | |
| 84 | return it->second->onDumpReport(); |
| 85 | } |
| 86 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 87 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
| 88 | auto it = mMetricsManagers.find(key); |
| 89 | if (it != mMetricsManagers.end()) { |
| 90 | it->second->finish(); |
| 91 | mMetricsManagers.erase(it); |
| 92 | } |
| 93 | } |
| 94 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 95 | void StatsLogProcessor::addEventMetricData(const EventMetricData& eventMetricData) { |
| 96 | // TODO: Replace this code when MetricsManager.onDumpReport() is ready to |
| 97 | // get a list of byte arrays. |
| 98 | flushIfNecessary(eventMetricData); |
| 99 | const int numBytes = eventMetricData.ByteSize(); |
| 100 | char buffer[numBytes]; |
| 101 | eventMetricData.SerializeToArray(&buffer[0], numBytes); |
| 102 | string bufferString(buffer, numBytes); |
| 103 | mEvents.push_back(bufferString); |
| 104 | mBufferSize += eventMetricData.ByteSize(); |
| 105 | } |
| 106 | |
| 107 | void StatsLogProcessor::flushIfNecessary(const EventMetricData& eventMetricData) { |
| 108 | if (eventMetricData.ByteSize() + mBufferSize > kMaxSerializedBytes) { |
| 109 | flush(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void StatsLogProcessor::flush() { |
| 114 | StatsLogReport logReport; |
| 115 | for (string eventBuffer : mEvents) { |
| 116 | EventMetricData eventFromBuffer; |
| 117 | eventFromBuffer.ParseFromString(eventBuffer); |
| 118 | EventMetricData* newEntry = logReport.mutable_event_metrics()->add_data(); |
| 119 | newEntry->CopyFrom(eventFromBuffer); |
| 120 | } |
| 121 | |
| 122 | const int numBytes = logReport.ByteSize(); |
| 123 | vector<uint8_t> logReportBuffer(numBytes); |
| 124 | logReport.SerializeToArray(&logReportBuffer[0], numBytes); |
| 125 | mPushLog(logReportBuffer); |
| 126 | mEvents.clear(); |
| 127 | mBufferSize = 0; |
| 128 | } |
| 129 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 130 | } // namespace statsd |
| 131 | } // namespace os |
| 132 | } // namespace android |