blob: ce60eb965d620690be07bb4585170630bbf10752 [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32: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
Yao Chen44cf27c2017-09-14 22:32:50 -070017#define DEBUG true // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070019
Yao Chen729093d2017-10-16 10:33:26 -070020#include "CountMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Yao Chen729093d2017-10-16 10:33:26 -070022#include "stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070023
Yao Chen44cf27c2017-09-14 22:32:50 -070024#include <limits.h>
25#include <stdlib.h>
26
yrob0378b02017-11-09 20:36:25 -080027using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080028using android::util::FIELD_TYPE_BOOL;
29using android::util::FIELD_TYPE_FLOAT;
30using android::util::FIELD_TYPE_INT32;
31using android::util::FIELD_TYPE_INT64;
32using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080033using android::util::FIELD_TYPE_STRING;
yro24809bd2017-10-31 23:06:53 -070034using android::util::ProtoOutputStream;
Yao Chen729093d2017-10-16 10:33:26 -070035using std::map;
36using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070037using std::unordered_map;
Yao Chen729093d2017-10-16 10:33:26 -070038using std::vector;
Yao Chen44cf27c2017-09-14 22:32:50 -070039
40namespace android {
41namespace os {
42namespace statsd {
43
yro24809bd2017-10-31 23:06:53 -070044// for StatsLogReport
Yangster-macd1815dc2017-11-13 21:43:15 -080045const int FIELD_ID_NAME = 1;
yro24809bd2017-10-31 23:06:53 -070046const int FIELD_ID_START_REPORT_NANOS = 2;
47const int FIELD_ID_END_REPORT_NANOS = 3;
48const int FIELD_ID_COUNT_METRICS = 5;
49// for CountMetricDataWrapper
50const int FIELD_ID_DATA = 1;
51// for CountMetricData
52const int FIELD_ID_DIMENSION = 1;
53const int FIELD_ID_BUCKET_INFO = 2;
54// for KeyValuePair
55const int FIELD_ID_KEY = 1;
56const int FIELD_ID_VALUE_STR = 2;
57const int FIELD_ID_VALUE_INT = 3;
58const int FIELD_ID_VALUE_BOOL = 4;
59const int FIELD_ID_VALUE_FLOAT = 5;
60// for CountBucketInfo
61const int FIELD_ID_START_BUCKET_NANOS = 1;
62const int FIELD_ID_END_BUCKET_NANOS = 2;
63const int FIELD_ID_COUNT = 3;
64
Yao Chen729093d2017-10-16 10:33:26 -070065// TODO: add back AnomalyTracker.
Yang Lu3eba6212017-10-25 19:54:45 -070066
Yao Chenb3561512017-11-21 18:07:17 -080067CountMetricProducer::CountMetricProducer(const ConfigKey& key, const CountMetric& metric,
68 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070069 const sp<ConditionWizard>& wizard,
70 const uint64_t startTimeNs)
Yao Chenb3561512017-11-21 18:07:17 -080071 : MetricProducer(key, startTimeNs, conditionIndex, wizard), mMetric(metric) {
Yao Chen44cf27c2017-09-14 22:32:50 -070072 // TODO: evaluate initial conditions. and set mConditionMet.
73 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
Yao Chen729093d2017-10-16 10:33:26 -070074 mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000;
Yao Chen44cf27c2017-09-14 22:32:50 -070075 } else {
Yao Chen729093d2017-10-16 10:33:26 -070076 mBucketSizeNs = LLONG_MAX;
Yao Chen44cf27c2017-09-14 22:32:50 -070077 }
78
Yao Chen729093d2017-10-16 10:33:26 -070079 // TODO: use UidMap if uid->pkg_name is required
80 mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end());
81
82 if (metric.links().size() > 0) {
83 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
84 metric.links().end());
85 mConditionSliced = true;
86 }
87
yro24809bd2017-10-31 23:06:53 -070088 startNewProtoOutputStream(mStartTimeNs);
89
Yangster-macd1815dc2017-11-13 21:43:15 -080090 VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
Yao Chen729093d2017-10-16 10:33:26 -070091 (long long)mBucketSizeNs, (long long)mStartTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -070092}
93
Yao Chen44cf27c2017-09-14 22:32:50 -070094CountMetricProducer::~CountMetricProducer() {
95 VLOG("~CountMetricProducer() called");
96}
97
yro24809bd2017-10-31 23:06:53 -070098void CountMetricProducer::startNewProtoOutputStream(long long startTime) {
99 mProto = std::make_unique<ProtoOutputStream>();
Yangster-macd1815dc2017-11-13 21:43:15 -0800100 mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
yro24809bd2017-10-31 23:06:53 -0700101 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
102 mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
Yao Chen44cf27c2017-09-14 22:32:50 -0700103}
104
yro24809bd2017-10-31 23:06:53 -0700105void CountMetricProducer::finish() {
Yao Chen729093d2017-10-16 10:33:26 -0700106}
107
Yao Chen5154a372017-10-30 22:57:06 -0700108void CountMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800109 VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700110}
111
yro17adac92017-11-08 23:16:29 -0800112std::unique_ptr<std::vector<uint8_t>> CountMetricProducer::onDumpReport() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700113 long long endTime = time(nullptr) * NS_PER_SEC;
Yao Chen729093d2017-10-16 10:33:26 -0700114
115 // Dump current bucket if it's stale.
116 // If current bucket is still on-going, don't force dump current bucket.
117 // In finish(), We can force dump current bucket.
Yangster-mace2cd6d52017-11-09 20:38:30 -0800118 flushIfNeeded(endTime);
Yangster-macd1815dc2017-11-13 21:43:15 -0800119 VLOG("metric %s dump report now...", mMetric.name().c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700120
Yao Chen93fe3a32017-11-02 13:52:59 -0700121 for (const auto& counter : mPastBuckets) {
yro24809bd2017-10-31 23:06:53 -0700122 const HashableDimensionKey& hashableKey = counter.first;
Yao Chen93fe3a32017-11-02 13:52:59 -0700123 VLOG(" dimension key %s", hashableKey.c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700124 auto it = mDimensionKeyMap.find(hashableKey);
125 if (it == mDimensionKeyMap.end()) {
126 ALOGE("Dimension key %s not found?!?! skip...", hashableKey.c_str());
127 continue;
128 }
yrob0378b02017-11-09 20:36:25 -0800129 long long wrapperToken =
130 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Yao Chen729093d2017-10-16 10:33:26 -0700131
yro24809bd2017-10-31 23:06:53 -0700132 // First fill dimension (KeyValuePairs).
133 for (const auto& kv : it->second) {
yrob0378b02017-11-09 20:36:25 -0800134 long long dimensionToken =
135 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
yro24809bd2017-10-31 23:06:53 -0700136 mProto->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key());
137 if (kv.has_value_str()) {
Yao Chen1ff4f432017-11-16 17:01:40 -0800138 mProto->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str());
yro24809bd2017-10-31 23:06:53 -0700139 } else if (kv.has_value_int()) {
140 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int());
141 } else if (kv.has_value_bool()) {
142 mProto->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool());
143 } else if (kv.has_value_float()) {
144 mProto->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float());
145 }
146 mProto->end(dimensionToken);
147 }
148
149 // Then fill bucket_info (CountBucketInfo).
Yao Chen93fe3a32017-11-02 13:52:59 -0700150 for (const auto& bucket : counter.second) {
yrob0378b02017-11-09 20:36:25 -0800151 long long bucketInfoToken =
152 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yao Chen93fe3a32017-11-02 13:52:59 -0700153 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
154 (long long)bucket.mBucketStartNs);
155 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
156 (long long)bucket.mBucketEndNs);
157 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount);
158 mProto->end(bucketInfoToken);
159 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
160 (long long)bucket.mBucketEndNs, (long long)bucket.mCount);
yro24809bd2017-10-31 23:06:53 -0700161 }
yro24809bd2017-10-31 23:06:53 -0700162 mProto->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700163 }
yro24809bd2017-10-31 23:06:53 -0700164
165 mProto->end(mProtoToken);
166 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS,
167 (long long)mCurrentBucketStartTimeNs);
168
Yangster-macd1815dc2017-11-13 21:43:15 -0800169 VLOG("metric %s dump report now...", mMetric.name().c_str());
yro17adac92017-11-08 23:16:29 -0800170 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto();
yro24809bd2017-10-31 23:06:53 -0700171
172 startNewProtoOutputStream(endTime);
Yao Chen93fe3a32017-11-02 13:52:59 -0700173 mPastBuckets.clear();
yro24809bd2017-10-31 23:06:53 -0700174
yro17adac92017-11-08 23:16:29 -0800175 return buffer;
yro24809bd2017-10-31 23:06:53 -0700176
177 // TODO: Clear mDimensionKeyMap once the report is dumped.
Yao Chen44cf27c2017-09-14 22:32:50 -0700178}
179
Yao Chen5154a372017-10-30 22:57:06 -0700180void CountMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800181 VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
Yao Chencaf339d2017-10-06 16:01:10 -0700182 mCondition = conditionMet;
183}
184
Yao Chenb3561512017-11-21 18:07:17 -0800185bool CountMetricProducer::hitGuardRail(const HashableDimensionKey& newKey) {
186 if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) {
187 return false;
188 }
189 // ===========GuardRail==============
190 // 1. Report the tuple count if the tuple count > soft limit
191 if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
192 size_t newTupleCount = mCurrentSlicedCounter->size() + 1;
193 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(),
194 newTupleCount);
195 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
196 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
197 ALOGE("CountMetric %s dropping data for dimension key %s", mMetric.name().c_str(),
198 newKey.c_str());
199 return true;
200 }
201 }
202
203 return false;
204}
Yao Chenb7041772017-10-20 16:59:25 -0700205void CountMetricProducer::onMatchedLogEventInternal(
206 const size_t matcherIndex, const HashableDimensionKey& eventKey,
207 const map<string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yub3dda412017-10-24 13:41:59 -0700208 const LogEvent& event, bool scheduledPull) {
Yao Chen729093d2017-10-16 10:33:26 -0700209 uint64_t eventTimeNs = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -0700210
Yangster-mace2cd6d52017-11-09 20:38:30 -0800211 flushIfNeeded(eventTimeNs);
Yao Chen729093d2017-10-16 10:33:26 -0700212
Yao Chenb7041772017-10-20 16:59:25 -0700213 if (condition == false) {
214 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700215 }
Yao Chen729093d2017-10-16 10:33:26 -0700216
Yang Lu3eba6212017-10-25 19:54:45 -0700217 auto it = mCurrentSlicedCounter->find(eventKey);
Yao Chen729093d2017-10-16 10:33:26 -0700218
Yang Lu3eba6212017-10-25 19:54:45 -0700219 if (it == mCurrentSlicedCounter->end()) {
Yao Chenb3561512017-11-21 18:07:17 -0800220 // ===========GuardRail==============
221 if (hitGuardRail(eventKey)) {
222 return;
223 }
224
Yao Chen729093d2017-10-16 10:33:26 -0700225 // create a counter for the new key
Yang Lu3eba6212017-10-25 19:54:45 -0700226 (*mCurrentSlicedCounter)[eventKey] = 1;
Yao Chen729093d2017-10-16 10:33:26 -0700227 } else {
228 // increment the existing value
229 auto& count = it->second;
230 count++;
231 }
232
Yangster-mace2cd6d52017-11-09 20:38:30 -0800233 for (auto& tracker : mAnomalyTrackers) {
234 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey,
235 mCurrentSlicedCounter->find(eventKey)->second);
236 }
237
238 VLOG("metric %s %s->%lld", mMetric.name().c_str(), eventKey.c_str(),
239 (long long)(*mCurrentSlicedCounter)[eventKey]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700240}
241
Yao Chen729093d2017-10-16 10:33:26 -0700242// When a new matched event comes in, we check if event falls into the current
243// bucket. If not, flush the old counter to past buckets and initialize the new bucket.
Yangster-mace2cd6d52017-11-09 20:38:30 -0800244void CountMetricProducer::flushIfNeeded(const uint64_t eventTimeNs) {
245 if (eventTimeNs < mCurrentBucketStartTimeNs + mBucketSizeNs) {
Yao Chen44cf27c2017-09-14 22:32:50 -0700246 return;
247 }
248
Yao Chen93fe3a32017-11-02 13:52:59 -0700249 CountBucket info;
250 info.mBucketStartNs = mCurrentBucketStartTimeNs;
251 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800252 info.mBucketNum = mCurrentBucketNum;
Yang Lu3eba6212017-10-25 19:54:45 -0700253 for (const auto& counter : *mCurrentSlicedCounter) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700254 info.mCount = counter.second;
255 auto& bucketList = mPastBuckets[counter.first];
256 bucketList.push_back(info);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800257 VLOG("metric %s, dump key value: %s -> %lld", mMetric.name().c_str(), counter.first.c_str(),
258 (long long)counter.second);
Yao Chen729093d2017-10-16 10:33:26 -0700259 }
260
Yang Lu3eba6212017-10-25 19:54:45 -0700261 for (auto& tracker : mAnomalyTrackers) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800262 tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum);
Yang Lu3eba6212017-10-25 19:54:45 -0700263 }
Bookatzd3606c72017-10-19 10:13:49 -0700264
Yang Lu3eba6212017-10-25 19:54:45 -0700265 // Reset counters (do not clear, since the old one is still referenced in mAnomalyTrackers).
266 mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800267 uint64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700268 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
Yang Lu3eba6212017-10-25 19:54:45 -0700269 mCurrentBucketNum += numBucketsForward;
Yangster-macd1815dc2017-11-13 21:43:15 -0800270 VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(),
Yao Chen729093d2017-10-16 10:33:26 -0700271 (long long)mCurrentBucketStartTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700272}
273
yro24809bd2017-10-31 23:06:53 -0700274// Rough estimate of CountMetricProducer buffer stored. This number will be
275// greater than actual data size as it contains each dimension of
276// CountMetricData is duplicated.
Yangster7c334a12017-11-22 14:24:24 -0800277size_t CountMetricProducer::byteSize() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800278 size_t totalSize = 0;
279 for (const auto& pair : mPastBuckets) {
280 totalSize += pair.second.size() * kBucketSize;
281 }
282 return totalSize;
yro69007c82017-10-26 20:42:57 -0700283}
284
Yao Chen44cf27c2017-09-14 22:32:50 -0700285} // namespace statsd
286} // namespace os
yro69007c82017-10-26 20:42:57 -0700287} // namespace android