blob: ae4df3ee92f07eae50f43e51f2ed4ad4c7373de5 [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 Chen3c0b95c2017-12-16 14:34:20 -080017#define DEBUG false // 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"
Yangster-mac20877162017-12-22 17:19:39 -080023#include "stats_log_util.h"
Yangster-mac93694462018-01-22 20:49:31 -080024#include "dimension.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070025
Yao Chen44cf27c2017-09-14 22:32:50 -070026#include <limits.h>
27#include <stdlib.h>
28
yrob0378b02017-11-09 20:36:25 -080029using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080030using android::util::FIELD_TYPE_BOOL;
31using android::util::FIELD_TYPE_FLOAT;
32using android::util::FIELD_TYPE_INT32;
33using android::util::FIELD_TYPE_INT64;
34using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080035using android::util::FIELD_TYPE_STRING;
yro24809bd2017-10-31 23:06:53 -070036using android::util::ProtoOutputStream;
Yao Chen729093d2017-10-16 10:33:26 -070037using std::map;
38using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070039using std::unordered_map;
Yao Chen729093d2017-10-16 10:33:26 -070040using std::vector;
Yao Chen44cf27c2017-09-14 22:32:50 -070041
42namespace android {
43namespace os {
44namespace statsd {
45
yro24809bd2017-10-31 23:06:53 -070046// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080047const int FIELD_ID_ID = 1;
yro24809bd2017-10-31 23:06:53 -070048const int FIELD_ID_START_REPORT_NANOS = 2;
49const int FIELD_ID_END_REPORT_NANOS = 3;
50const int FIELD_ID_COUNT_METRICS = 5;
51// for CountMetricDataWrapper
52const int FIELD_ID_DATA = 1;
53// for CountMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080054const int FIELD_ID_DIMENSION_IN_WHAT = 1;
55const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
56const int FIELD_ID_BUCKET_INFO = 3;
yro24809bd2017-10-31 23:06:53 -070057// for CountBucketInfo
58const int FIELD_ID_START_BUCKET_NANOS = 1;
59const int FIELD_ID_END_BUCKET_NANOS = 2;
60const int FIELD_ID_COUNT = 3;
61
Yao Chenb3561512017-11-21 18:07:17 -080062CountMetricProducer::CountMetricProducer(const ConfigKey& key, const CountMetric& metric,
63 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070064 const sp<ConditionWizard>& wizard,
65 const uint64_t startTimeNs)
Yangster-mac94e197c2018-01-02 16:03:03 -080066 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) {
Yao Chen44cf27c2017-09-14 22:32:50 -070067 // TODO: evaluate initial conditions. and set mConditionMet.
Yangster-macb8144812018-01-04 10:56:23 -080068 if (metric.has_bucket()) {
69 mBucketSizeNs = TimeUnitToBucketSizeInMillis(metric.bucket()) * 1000000;
Yao Chen44cf27c2017-09-14 22:32:50 -070070 } else {
Yao Chen729093d2017-10-16 10:33:26 -070071 mBucketSizeNs = LLONG_MAX;
Yao Chen44cf27c2017-09-14 22:32:50 -070072 }
73
Yao Chen729093d2017-10-16 10:33:26 -070074 // TODO: use UidMap if uid->pkg_name is required
Yangster-mac93694462018-01-22 20:49:31 -080075 mDimensionsInWhat = metric.dimensions_in_what();
76 mDimensionsInCondition = metric.dimensions_in_condition();
Yao Chen729093d2017-10-16 10:33:26 -070077
78 if (metric.links().size() > 0) {
79 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
80 metric.links().end());
Yao Chen729093d2017-10-16 10:33:26 -070081 }
Yangster-mac93694462018-01-22 20:49:31 -080082 mConditionSliced = (metric.links().size() > 0)||
83 (mDimensionsInCondition.has_field() && mDimensionsInCondition.child_size() > 0);
Yao Chen729093d2017-10-16 10:33:26 -070084
Yangster-mac94e197c2018-01-02 16:03:03 -080085 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen729093d2017-10-16 10:33:26 -070086 (long long)mBucketSizeNs, (long long)mStartTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -070087}
88
Yao Chen44cf27c2017-09-14 22:32:50 -070089CountMetricProducer::~CountMetricProducer() {
90 VLOG("~CountMetricProducer() called");
91}
92
Yangsterf2bee6f2017-11-29 12:01:05 -080093void CountMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -080094 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -070095}
96
Yangster-mac20877162017-12-22 17:19:39 -080097void CountMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) {
98 flushIfNeededLocked(dumpTimeNs);
Yangster-mac94e197c2018-01-02 16:03:03 -080099 report->set_metric_id(mMetricId);
Yangster-mac20877162017-12-22 17:19:39 -0800100 report->set_start_report_nanos(mStartTimeNs);
101
102 auto count_metrics = report->mutable_count_metrics();
103 for (const auto& counter : mPastBuckets) {
104 CountMetricData* metricData = count_metrics->add_data();
Yangster-mac93694462018-01-22 20:49:31 -0800105 *metricData->mutable_dimensions_in_what() =
106 counter.first.getDimensionKeyInWhat().getDimensionsValue();
107 *metricData->mutable_dimensions_in_condition() =
108 counter.first.getDimensionKeyInCondition().getDimensionsValue();
Yangster-mac20877162017-12-22 17:19:39 -0800109 for (const auto& bucket : counter.second) {
110 CountBucketInfo* bucketInfo = metricData->add_bucket_info();
111 bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs);
112 bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs);
113 bucketInfo->set_count(bucket.mCount);
114 }
115 }
116}
117
Yao Chen288c6002017-12-12 13:43:18 -0800118void CountMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
119 ProtoOutputStream* protoOutput) {
120 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800121 if (mPastBuckets.empty()) {
122 return;
123 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000124
Yangster-mac94e197c2018-01-02 16:03:03 -0800125 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800126 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
127 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
128
Yangster-mac94e197c2018-01-02 16:03:03 -0800129 VLOG("metric %lld dump report now...",(long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700130
Yao Chen93fe3a32017-11-02 13:52:59 -0700131 for (const auto& counter : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800132 const MetricDimensionKey& dimensionKey = counter.first;
133 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chend5aa01b32017-12-19 16:46:36 -0800134
yrob0378b02017-11-09 20:36:25 -0800135 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800136 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Yao Chen729093d2017-10-16 10:33:26 -0700137
Yangster-mac20877162017-12-22 17:19:39 -0800138 // First fill dimension.
Yangster-mac93694462018-01-22 20:49:31 -0800139 long long dimensionInWhatToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800140 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac93694462018-01-22 20:49:31 -0800141 writeDimensionsValueProtoToStream(
142 dimensionKey.getDimensionKeyInWhat().getDimensionsValue(), protoOutput);
143 protoOutput->end(dimensionInWhatToken);
144
145 if (dimensionKey.hasDimensionKeyInCondition()) {
146 long long dimensionInConditionToken = protoOutput->start(
147 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
148 writeDimensionsValueProtoToStream(
149 dimensionKey.getDimensionKeyInCondition().getDimensionsValue(), protoOutput);
150 protoOutput->end(dimensionInConditionToken);
151 }
yro24809bd2017-10-31 23:06:53 -0700152
153 // Then fill bucket_info (CountBucketInfo).
Yao Chen93fe3a32017-11-02 13:52:59 -0700154 for (const auto& bucket : counter.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800155 long long bucketInfoToken = protoOutput->start(
156 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
157 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
158 (long long)bucket.mBucketStartNs);
159 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
160 (long long)bucket.mBucketEndNs);
161 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount);
162 protoOutput->end(bucketInfoToken);
Yao Chen93fe3a32017-11-02 13:52:59 -0700163 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
164 (long long)bucket.mBucketEndNs, (long long)bucket.mCount);
yro24809bd2017-10-31 23:06:53 -0700165 }
Yao Chen288c6002017-12-12 13:43:18 -0800166 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700167 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000168
Yao Chen288c6002017-12-12 13:43:18 -0800169 protoOutput->end(protoToken);
170 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
yro24809bd2017-10-31 23:06:53 -0700171
Yao Chen6a8c7992017-11-29 20:02:07 +0000172 mPastBuckets.clear();
Yao Chen288c6002017-12-12 13:43:18 -0800173 mStartTimeNs = mCurrentBucketStartTimeNs;
Yao Chen6a8c7992017-11-29 20:02:07 +0000174
175 // TODO: Clear mDimensionKeyMap once the report is dumped.
Yao Chen44cf27c2017-09-14 22:32:50 -0700176}
177
Yangsterf2bee6f2017-11-29 12:01:05 -0800178void CountMetricProducer::onConditionChangedLocked(const bool conditionMet,
179 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800180 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chencaf339d2017-10-06 16:01:10 -0700181 mCondition = conditionMet;
182}
183
Yangster-mac93694462018-01-22 20:49:31 -0800184bool CountMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800185 if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) {
186 return false;
187 }
188 // ===========GuardRail==============
189 // 1. Report the tuple count if the tuple count > soft limit
190 if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
191 size_t newTupleCount = mCurrentSlicedCounter->size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800192 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800193 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
194 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800195 ALOGE("CountMetric %lld dropping data for dimension key %s",
196 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800197 return true;
198 }
199 }
200
201 return false;
202}
Yangsterf2bee6f2017-11-29 12:01:05 -0800203
204void CountMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800205 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800206 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800207 const LogEvent& event) {
Yao Chen729093d2017-10-16 10:33:26 -0700208 uint64_t eventTimeNs = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -0700209
Yangsterf2bee6f2017-11-29 12:01:05 -0800210 flushIfNeededLocked(eventTimeNs);
Yao Chen729093d2017-10-16 10:33:26 -0700211
Yao Chen6a8c7992017-11-29 20:02:07 +0000212 if (condition == false) {
Yao Chenb7041772017-10-20 16:59:25 -0700213 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700214 }
Yao Chen729093d2017-10-16 10:33:26 -0700215
Yao Chen6a8c7992017-11-29 20:02:07 +0000216 auto it = mCurrentSlicedCounter->find(eventKey);
217
218 if (it == mCurrentSlicedCounter->end()) {
219 // ===========GuardRail==============
Yangsterf2bee6f2017-11-29 12:01:05 -0800220 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800221 return;
222 }
223
Yao Chen6a8c7992017-11-29 20:02:07 +0000224 // create a counter for the new key
225 (*mCurrentSlicedCounter)[eventKey] = 1;
226 } else {
227 // increment the existing value
228 auto& count = it->second;
229 count++;
Yao Chen729093d2017-10-16 10:33:26 -0700230 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000231
232 for (auto& tracker : mAnomalyTrackers) {
233 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey,
234 mCurrentSlicedCounter->find(eventKey)->second);
235 }
236
Yangster-mac94e197c2018-01-02 16:03:03 -0800237 VLOG("metric %lld %s->%lld", (long long)mMetricId, eventKey.c_str(),
Yao Chen6a8c7992017-11-29 20:02:07 +0000238 (long long)(*mCurrentSlicedCounter)[eventKey]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700239}
240
Yao Chen729093d2017-10-16 10:33:26 -0700241// When a new matched event comes in, we check if event falls into the current
242// bucket. If not, flush the old counter to past buckets and initialize the new bucket.
Yangsterf2bee6f2017-11-29 12:01:05 -0800243void CountMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800244 if (eventTimeNs < mCurrentBucketStartTimeNs + mBucketSizeNs) {
Yao Chen44cf27c2017-09-14 22:32:50 -0700245 return;
246 }
247
Yao Chen93fe3a32017-11-02 13:52:59 -0700248 CountBucket info;
249 info.mBucketStartNs = mCurrentBucketStartTimeNs;
250 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800251 info.mBucketNum = mCurrentBucketNum;
Yang Lu3eba6212017-10-25 19:54:45 -0700252 for (const auto& counter : *mCurrentSlicedCounter) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700253 info.mCount = counter.second;
254 auto& bucketList = mPastBuckets[counter.first];
255 bucketList.push_back(info);
Yangster-mac94e197c2018-01-02 16:03:03 -0800256 VLOG("metric %lld, dump key value: %s -> %lld",
257 (long long)mMetricId, counter.first.c_str(), (long long)counter.second);
Yao Chen729093d2017-10-16 10:33:26 -0700258 }
259
Yang Lu3eba6212017-10-25 19:54:45 -0700260 for (auto& tracker : mAnomalyTrackers) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800261 tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum);
Yang Lu3eba6212017-10-25 19:54:45 -0700262 }
Bookatzd3606c72017-10-19 10:13:49 -0700263
Yang Lu3eba6212017-10-25 19:54:45 -0700264 // Reset counters (do not clear, since the old one is still referenced in mAnomalyTrackers).
265 mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800266 uint64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700267 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
Yang Lu3eba6212017-10-25 19:54:45 -0700268 mCurrentBucketNum += numBucketsForward;
Yangster-mac94e197c2018-01-02 16:03:03 -0800269 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
Yao Chen729093d2017-10-16 10:33:26 -0700270 (long long)mCurrentBucketStartTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700271}
272
yro24809bd2017-10-31 23:06:53 -0700273// Rough estimate of CountMetricProducer buffer stored. This number will be
274// greater than actual data size as it contains each dimension of
275// CountMetricData is duplicated.
Yangsterf2bee6f2017-11-29 12:01:05 -0800276size_t CountMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800277 size_t totalSize = 0;
278 for (const auto& pair : mPastBuckets) {
279 totalSize += pair.second.size() * kBucketSize;
280 }
281 return totalSize;
yro69007c82017-10-26 20:42:57 -0700282}
283
Yao Chen44cf27c2017-09-14 22:32:50 -0700284} // namespace statsd
285} // namespace os
yro69007c82017-10-26 20:42:57 -0700286} // namespace android