blob: abd2a35d76dfd78342b09b2848da10e95f329279 [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"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021#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;
yrob0378b02017-11-09 20:36:25 -080028using android::util::FIELD_COUNT_REPEATED;
yro17adac92017-11-08 23:16:29 -080029using android::util::FIELD_TYPE_BOOL;
30using android::util::FIELD_TYPE_FLOAT;
31using android::util::FIELD_TYPE_INT32;
32using android::util::FIELD_TYPE_INT64;
33using android::util::FIELD_TYPE_MESSAGE;
34using android::util::FIELD_TYPE_STRING;
35using android::util::ProtoOutputStream;
Yao Chen44cf27c2017-09-14 22:32:50 -070036using std::make_unique;
37using std::unique_ptr;
38using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070039
40namespace android {
41namespace os {
42namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070043
yro17adac92017-11-08 23:16:29 -080044// for ConfigMetricsReport
45const int FIELD_ID_CONFIG_KEY = 1;
46const int FIELD_ID_METRICS = 2;
47const int FIELD_ID_UID_MAP = 3;
48// for ConfigKey
49const int FIELD_ID_UID = 1;
yrob0378b02017-11-09 20:36:25 -080050const int FIELD_ID_NAME = 2;
yro17adac92017-11-08 23:16:29 -080051
yro31eb67b2017-10-24 13:33:21 -070052StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
David Chen1d7b0cd2017-11-15 14:20:04 -080053 const std::function<void(const ConfigKey&)>& sendBroadcast)
54 : mUidMap(uidMap), mSendBroadcast(sendBroadcast) {
Yao Chenab273e22017-09-06 12:53:50 -070055}
56
Yao Chenef99c4f2017-09-22 16:26:54 -070057StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070058}
59
Yao Chen44cf27c2017-09-14 22:32:50 -070060// TODO: what if statsd service restarts? How do we know what logs are already processed before?
Joe Onoratoc4dfae52017-10-17 23:38:21 -070061void StatsLogProcessor::OnLogEvent(const LogEvent& msg) {
Yao Chen44cf27c2017-09-14 22:32:50 -070062 // pass the event to metrics managers.
63 for (auto& pair : mMetricsManagers) {
64 pair.second->onLogEvent(msg);
yro69007c82017-10-26 20:42:57 -070065 flushIfNecessary(msg.GetTimestampNs(), pair.first, pair.second);
Yao Chenab273e22017-09-06 12:53:50 -070066 }
David Chen21582962017-11-01 17:32:46 -070067
68 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -080069 // The field numbers need to be currently updated by hand with atoms.proto
David Chen21582962017-11-01 17:32:46 -070070 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 Chenab273e22017-09-06 12:53:50 -070083}
84
Joe Onorato9fc9edf2017-10-15 20:08:52 -070085void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
86 auto it = mMetricsManagers.find(key);
Yao Chen44cf27c2017-09-14 22:32:50 -070087 if (it != mMetricsManagers.end()) {
88 it->second->finish();
89 }
90
Joe Onorato9fc9edf2017-10-15 20:08:52 -070091 ALOGD("Updated configuration for key %s", key.ToString().c_str());
Yao Chen44cf27c2017-09-14 22:32:50 -070092
Yao Chencaf339d2017-10-06 16:01:10 -070093 unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config);
94 if (newMetricsManager->isConfigValid()) {
David Chend6896892017-10-25 11:49:03 -070095 mUidMap->OnConfigUpdated(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070096 mMetricsManagers[key] = std::move(newMetricsManager);
97 // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
Yao Chencaf339d2017-10-06 16:01:10 -070098 ALOGD("StatsdConfig valid");
99 } else {
100 // If there is any error in the config, don't use it.
101 ALOGD("StatsdConfig NOT valid");
102 }
yro00698da2017-09-15 10:06:40 -0700103}
Bookatz906a35c2017-09-20 15:26:44 -0700104
David Chen1d7b0cd2017-11-15 14:20:04 -0800105size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) {
Yao Chen729093d2017-10-16 10:33:26 -0700106 auto it = mMetricsManagers.find(key);
107 if (it == mMetricsManagers.end()) {
108 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800109 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700110 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800111 return it->second->byteSize();
112}
113
114void StatsLogProcessor::onDumpReport(const ConfigKey& key, vector<uint8_t>* outData) {
115 auto it = mMetricsManagers.find(key);
116 if (it == mMetricsManagers.end()) {
117 ALOGW("Config source %s does not exist", key.ToString().c_str());
118 return;
119 }
120
121 // This allows another broadcast to be sent within the rate-limit period if we get close to
122 // filling the buffer again soon.
123 mBroadcastTimesMutex.lock();
124 mLastBroadcastTimes.erase(key);
125 mBroadcastTimesMutex.unlock();
Yao Chen729093d2017-10-16 10:33:26 -0700126
yro17adac92017-11-08 23:16:29 -0800127 ProtoOutputStream proto;
128
129 // Fill in ConfigKey.
130 long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
131 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
132 proto.write(FIELD_TYPE_STRING | FIELD_ID_NAME, key.GetName());
133 proto.end(configKeyToken);
134
135 // Fill in StatsLogReport's.
136 for (auto& m : it->second->onDumpReport()) {
137 // Add each vector of StatsLogReport into a repeated field.
yrob0378b02017-11-09 20:36:25 -0800138 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS,
139 reinterpret_cast<char*>(m.get()->data()), m.get()->size());
David Chend6896892017-10-25 11:49:03 -0700140 }
yro17adac92017-11-08 23:16:29 -0800141
142 // Fill in UidMap.
143 auto uidMap = mUidMap->getOutput(key);
144 const int uidMapSize = uidMap.ByteSize();
145 char uidMapBuffer[uidMapSize];
146 uidMap.SerializeToArray(&uidMapBuffer[0], uidMapSize);
147 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize);
148
David Chen1d7b0cd2017-11-15 14:20:04 -0800149 if (outData != nullptr) {
150 outData->clear();
151 outData->resize(proto.size());
152 size_t pos = 0;
153 auto iter = proto.data();
154 while (iter.readBuffer() != NULL) {
155 size_t toRead = iter.currentToRead();
156 std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead);
157 pos += toRead;
158 iter.rp()->move(toRead);
159 }
yro17adac92017-11-08 23:16:29 -0800160 }
Yao Chen729093d2017-10-16 10:33:26 -0700161}
162
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700163void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
164 auto it = mMetricsManagers.find(key);
165 if (it != mMetricsManagers.end()) {
166 it->second->finish();
167 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700168 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700169 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800170
171 std::lock_guard<std::mutex> lock(mBroadcastTimesMutex);
172 mLastBroadcastTimes.erase(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700173}
174
yro69007c82017-10-26 20:42:57 -0700175void StatsLogProcessor::flushIfNecessary(uint64_t timestampNs,
176 const ConfigKey& key,
177 const unique_ptr<MetricsManager>& metricsManager) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800178 std::lock_guard<std::mutex> lock(mBroadcastTimesMutex);
yro31eb67b2017-10-24 13:33:21 -0700179
yro69007c82017-10-26 20:42:57 -0700180 size_t totalBytes = metricsManager->byteSize();
David Chen1d7b0cd2017-11-15 14:20:04 -0800181 if (totalBytes > .9 * kMaxSerializedBytes) { // Send broadcast so that receivers can pull data.
182 auto lastFlushNs = mLastBroadcastTimes.find(key);
183 if (lastFlushNs != mLastBroadcastTimes.end()) {
184 if (timestampNs - lastFlushNs->second < kMinBroadcastPeriod) {
185 return;
186 }
187 }
188 mLastBroadcastTimes[key] = timestampNs;
189 ALOGD("StatsD requesting broadcast for %s", key.ToString().c_str());
190 mSendBroadcast(key);
191 } else if (totalBytes > kMaxSerializedBytes) { // Too late. We need to start clearing data.
192 // We ignore the return value so we force each metric producer to clear its contents.
193 metricsManager->onDumpReport();
194 ALOGD("StatsD had to toss out metrics for %s", key.ToString().c_str());
yro31eb67b2017-10-24 13:33:21 -0700195 }
196}
197
Yao Chenef99c4f2017-09-22 16:26:54 -0700198} // namespace statsd
199} // namespace os
200} // namespace android