blob: 8910523aeda39ab6944d1de9d77c4642787fc7b8 [file] [log] [blame]
Yao Chenab273e22017-09-06 12:53:50 -07001/*
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 Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
Yao Chenab273e22017-09-06 12:53:50 -070018
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019#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
yro00698da2017-09-15 10:06:40 -070024#include <log/log_event_list.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070025#include <utils/Errors.h>
Yao Chenab273e22017-09-06 12:53:50 -070026
27using namespace android;
Yao Chen44cf27c2017-09-14 22:32:50 -070028using std::make_unique;
29using std::unique_ptr;
30using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070031
32namespace android {
33namespace os {
34namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070035
yro31eb67b2017-10-24 13:33:21 -070036StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
37 const std::function<void(const vector<uint8_t>&)>& pushLog)
yrofd05a4e2017-10-26 14:08:26 -070038 : mUidMap(uidMap), mPushLog(pushLog) {
Yao Chenab273e22017-09-06 12:53:50 -070039}
40
Yao Chenef99c4f2017-09-22 16:26:54 -070041StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070042}
43
Yao Chen44cf27c2017-09-14 22:32:50 -070044// TODO: what if statsd service restarts? How do we know what logs are already processed before?
Joe Onoratoc4dfae52017-10-17 23:38:21 -070045void StatsLogProcessor::OnLogEvent(const LogEvent& msg) {
Yao Chen44cf27c2017-09-14 22:32:50 -070046 // pass the event to metrics managers.
47 for (auto& pair : mMetricsManagers) {
48 pair.second->onLogEvent(msg);
yro69007c82017-10-26 20:42:57 -070049 flushIfNecessary(msg.GetTimestampNs(), pair.first, pair.second);
Yao Chenab273e22017-09-06 12:53:50 -070050 }
51}
52
Joe Onorato9fc9edf2017-10-15 20:08:52 -070053void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
54 auto it = mMetricsManagers.find(key);
Yao Chen44cf27c2017-09-14 22:32:50 -070055 if (it != mMetricsManagers.end()) {
56 it->second->finish();
57 }
58
Joe Onorato9fc9edf2017-10-15 20:08:52 -070059 ALOGD("Updated configuration for key %s", key.ToString().c_str());
Yao Chen44cf27c2017-09-14 22:32:50 -070060
Yao Chencaf339d2017-10-06 16:01:10 -070061 unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config);
62 if (newMetricsManager->isConfigValid()) {
David Chend6896892017-10-25 11:49:03 -070063 mUidMap->OnConfigUpdated(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070064 mMetricsManagers[key] = std::move(newMetricsManager);
65 // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
Yao Chencaf339d2017-10-06 16:01:10 -070066 ALOGD("StatsdConfig valid");
67 } else {
68 // If there is any error in the config, don't use it.
69 ALOGD("StatsdConfig NOT valid");
70 }
yro00698da2017-09-15 10:06:40 -070071}
Bookatz906a35c2017-09-20 15:26:44 -070072
David Chend6896892017-10-25 11:49:03 -070073ConfigMetricsReport StatsLogProcessor::onDumpReport(const ConfigKey& key) {
74 ConfigMetricsReport report;
75
Yao Chen729093d2017-10-16 10:33:26 -070076 auto it = mMetricsManagers.find(key);
77 if (it == mMetricsManagers.end()) {
78 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chend6896892017-10-25 11:49:03 -070079 return report;
Yao Chen729093d2017-10-16 10:33:26 -070080 }
81
David Chend6896892017-10-25 11:49:03 -070082 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 Chen93fe3a32017-11-02 13:52:59 -070092 report.mutable_uid_map()->Swap(&temp);
David Chend6896892017-10-25 11:49:03 -070093 return report;
Yao Chen729093d2017-10-16 10:33:26 -070094}
95
Joe Onorato9fc9edf2017-10-15 20:08:52 -070096void 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 Chend6896892017-10-25 11:49:03 -0700101 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700102 }
yro69007c82017-10-26 20:42:57 -0700103 auto flushTime = mLastFlushTimes.find(key);
104 if (flushTime != mLastFlushTimes.end()) {
105 mLastFlushTimes.erase(flushTime);
106 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700107}
108
yro69007c82017-10-26 20:42:57 -0700109void 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 }
yro31eb67b2017-10-24 13:33:21 -0700118
yro69007c82017-10-26 20:42:57 -0700119 size_t totalBytes = metricsManager->byteSize();
120 if (totalBytes > kMaxSerializedBytes) {
121 flush();
122 mLastFlushTimes[key] = std::move(timestampNs);
yro31eb67b2017-10-24 13:33:21 -0700123 }
124}
125
126void StatsLogProcessor::flush() {
yro69007c82017-10-26 20:42:57 -0700127 // 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 /*
yro31eb67b2017-10-24 13:33:21 -0700131 StatsLogReport logReport;
yro31eb67b2017-10-24 13:33:21 -0700132 const int numBytes = logReport.ByteSize();
133 vector<uint8_t> logReportBuffer(numBytes);
134 logReport.SerializeToArray(&logReportBuffer[0], numBytes);
135 mPushLog(logReportBuffer);
yro69007c82017-10-26 20:42:57 -0700136 */
yro31eb67b2017-10-24 13:33:21 -0700137}
138
Yao Chenef99c4f2017-09-22 16:26:54 -0700139} // namespace statsd
140} // namespace os
141} // namespace android