blob: f0689d2cc8393b4fe7969a57cd7ad912b96a9004 [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"
David Chen21582962017-11-01 17:32:46 -070018#include "statslog.h"
Yao Chenab273e22017-09-06 12:53:50 -070019
Joe Onorato9fc9edf2017-10-15 20:08:52 -070020#include "StatsLogProcessor.h"
21#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
22#include "metrics/CountMetricProducer.h"
23#include "stats_util.h"
24
yro00698da2017-09-15 10:06:40 -070025#include <log/log_event_list.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070026#include <utils/Errors.h>
Yao Chenab273e22017-09-06 12:53:50 -070027
28using namespace android;
Yao Chen44cf27c2017-09-14 22:32:50 -070029using std::make_unique;
30using std::unique_ptr;
31using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070032
33namespace android {
34namespace os {
35namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070036
yro31eb67b2017-10-24 13:33:21 -070037StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
38 const std::function<void(const vector<uint8_t>&)>& pushLog)
yrofd05a4e2017-10-26 14:08:26 -070039 : mUidMap(uidMap), mPushLog(pushLog) {
Yao Chenab273e22017-09-06 12:53:50 -070040}
41
Yao Chenef99c4f2017-09-22 16:26:54 -070042StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070043}
44
Yao Chen44cf27c2017-09-14 22:32:50 -070045// TODO: what if statsd service restarts? How do we know what logs are already processed before?
Joe Onoratoc4dfae52017-10-17 23:38:21 -070046void StatsLogProcessor::OnLogEvent(const LogEvent& msg) {
Yao Chen44cf27c2017-09-14 22:32:50 -070047 // pass the event to metrics managers.
48 for (auto& pair : mMetricsManagers) {
49 pair.second->onLogEvent(msg);
yro69007c82017-10-26 20:42:57 -070050 flushIfNecessary(msg.GetTimestampNs(), pair.first, pair.second);
Yao Chenab273e22017-09-06 12:53:50 -070051 }
David Chen21582962017-11-01 17:32:46 -070052
53 // Hard-coded logic to update the isolated uid's in the uid-map.
54 // The field numbers need to be currently updated by hand with stats_events.proto
55 if (msg.GetTagId() == android::util::ISOLATED_UID_CHANGED) {
56 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
57 bool is_create = msg.GetBool(3, &err);
58 auto parent_uid = int(msg.GetLong(1, &err2));
59 auto isolated_uid = int(msg.GetLong(2, &err3));
60 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
61 if (is_create) {
62 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
63 } else {
64 mUidMap->removeIsolatedUid(isolated_uid, parent_uid);
65 }
66 }
67 }
Yao Chenab273e22017-09-06 12:53:50 -070068}
69
Joe Onorato9fc9edf2017-10-15 20:08:52 -070070void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
71 auto it = mMetricsManagers.find(key);
Yao Chen44cf27c2017-09-14 22:32:50 -070072 if (it != mMetricsManagers.end()) {
73 it->second->finish();
74 }
75
Joe Onorato9fc9edf2017-10-15 20:08:52 -070076 ALOGD("Updated configuration for key %s", key.ToString().c_str());
Yao Chen44cf27c2017-09-14 22:32:50 -070077
Yao Chencaf339d2017-10-06 16:01:10 -070078 unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config);
79 if (newMetricsManager->isConfigValid()) {
David Chend6896892017-10-25 11:49:03 -070080 mUidMap->OnConfigUpdated(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070081 mMetricsManagers[key] = std::move(newMetricsManager);
82 // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
Yao Chencaf339d2017-10-06 16:01:10 -070083 ALOGD("StatsdConfig valid");
84 } else {
85 // If there is any error in the config, don't use it.
86 ALOGD("StatsdConfig NOT valid");
87 }
yro00698da2017-09-15 10:06:40 -070088}
Bookatz906a35c2017-09-20 15:26:44 -070089
David Chend6896892017-10-25 11:49:03 -070090ConfigMetricsReport StatsLogProcessor::onDumpReport(const ConfigKey& key) {
91 ConfigMetricsReport report;
92
Yao Chen729093d2017-10-16 10:33:26 -070093 auto it = mMetricsManagers.find(key);
94 if (it == mMetricsManagers.end()) {
95 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chend6896892017-10-25 11:49:03 -070096 return report;
Yao Chen729093d2017-10-16 10:33:26 -070097 }
98
David Chend6896892017-10-25 11:49:03 -070099 auto set_key = report.mutable_config_key();
100 set_key->set_uid(key.GetUid());
101 set_key->set_name(key.GetName());
102 for (auto m : it->second->onDumpReport()) {
103 // Transfer the vector of StatsLogReport into a field
104 // TODO: perhaps we just have bytes being returned from onDumpReport and transfer bytes
105 auto dest = report.add_metrics();
106 *dest = m;
107 }
108 auto temp = mUidMap->getOutput(key);
Yao Chen93fe3a32017-11-02 13:52:59 -0700109 report.mutable_uid_map()->Swap(&temp);
David Chend6896892017-10-25 11:49:03 -0700110 return report;
Yao Chen729093d2017-10-16 10:33:26 -0700111}
112
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700113void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
114 auto it = mMetricsManagers.find(key);
115 if (it != mMetricsManagers.end()) {
116 it->second->finish();
117 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700118 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700119 }
yro69007c82017-10-26 20:42:57 -0700120 auto flushTime = mLastFlushTimes.find(key);
121 if (flushTime != mLastFlushTimes.end()) {
122 mLastFlushTimes.erase(flushTime);
123 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700124}
125
yro69007c82017-10-26 20:42:57 -0700126void StatsLogProcessor::flushIfNecessary(uint64_t timestampNs,
127 const ConfigKey& key,
128 const unique_ptr<MetricsManager>& metricsManager) {
129 auto lastFlushNs = mLastFlushTimes.find(key);
130 if (lastFlushNs != mLastFlushTimes.end()) {
131 if (timestampNs - lastFlushNs->second < kMinFlushPeriod) {
132 return;
133 }
134 }
yro31eb67b2017-10-24 13:33:21 -0700135
yro69007c82017-10-26 20:42:57 -0700136 size_t totalBytes = metricsManager->byteSize();
137 if (totalBytes > kMaxSerializedBytes) {
138 flush();
139 mLastFlushTimes[key] = std::move(timestampNs);
yro31eb67b2017-10-24 13:33:21 -0700140 }
141}
142
143void StatsLogProcessor::flush() {
yro69007c82017-10-26 20:42:57 -0700144 // TODO: Take ConfigKey as an argument and flush metrics related to the
145 // ConfigKey. Also, create a wrapper that holds a repeated field of
146 // StatsLogReport's.
147 /*
yro31eb67b2017-10-24 13:33:21 -0700148 StatsLogReport logReport;
yro31eb67b2017-10-24 13:33:21 -0700149 const int numBytes = logReport.ByteSize();
150 vector<uint8_t> logReportBuffer(numBytes);
151 logReport.SerializeToArray(&logReportBuffer[0], numBytes);
152 mPushLog(logReportBuffer);
yro69007c82017-10-26 20:42:57 -0700153 */
yro31eb67b2017-10-24 13:33:21 -0700154}
155
Yao Chenef99c4f2017-09-22 16:26:54 -0700156} // namespace statsd
157} // namespace os
158} // namespace android