blob: ef272103a1b6a52c13ca435d0b75f86e0929bcb8 [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"
Yao Chen44cf27c2017-09-14 22:32:50 -070024
Yao Chen44cf27c2017-09-14 22:32:50 -070025#include <limits.h>
26#include <stdlib.h>
27
yrob0378b02017-11-09 20:36:25 -080028using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -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;
Yangster-macd1815dc2017-11-13 21:43:15 -080034using android::util::FIELD_TYPE_STRING;
yro24809bd2017-10-31 23:06:53 -070035using android::util::ProtoOutputStream;
Yao Chen729093d2017-10-16 10:33:26 -070036using std::map;
37using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070038using std::unordered_map;
Yao Chen729093d2017-10-16 10:33:26 -070039using std::vector;
Yao Chen44cf27c2017-09-14 22:32:50 -070040
41namespace android {
42namespace os {
43namespace statsd {
44
yro24809bd2017-10-31 23:06:53 -070045// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080046const int FIELD_ID_ID = 1;
yro24809bd2017-10-31 23:06:53 -070047const int FIELD_ID_START_REPORT_NANOS = 2;
48const int FIELD_ID_END_REPORT_NANOS = 3;
49const int FIELD_ID_COUNT_METRICS = 5;
50// for CountMetricDataWrapper
51const int FIELD_ID_DATA = 1;
52// for CountMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080053const int FIELD_ID_DIMENSION_IN_WHAT = 1;
54const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
55const int FIELD_ID_BUCKET_INFO = 3;
yro24809bd2017-10-31 23:06:53 -070056// for CountBucketInfo
57const int FIELD_ID_START_BUCKET_NANOS = 1;
58const int FIELD_ID_END_BUCKET_NANOS = 2;
59const int FIELD_ID_COUNT = 3;
60
Yao Chenb3561512017-11-21 18:07:17 -080061CountMetricProducer::CountMetricProducer(const ConfigKey& key, const CountMetric& metric,
62 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070063 const sp<ConditionWizard>& wizard,
64 const uint64_t startTimeNs)
Yangster-mac94e197c2018-01-02 16:03:03 -080065 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) {
Yao Chen44cf27c2017-09-14 22:32:50 -070066 // TODO: evaluate initial conditions. and set mConditionMet.
Yangster-macb8144812018-01-04 10:56:23 -080067 if (metric.has_bucket()) {
68 mBucketSizeNs = TimeUnitToBucketSizeInMillis(metric.bucket()) * 1000000;
Yao Chen44cf27c2017-09-14 22:32:50 -070069 } else {
Yao Chen729093d2017-10-16 10:33:26 -070070 mBucketSizeNs = LLONG_MAX;
Yao Chen44cf27c2017-09-14 22:32:50 -070071 }
72
Yao Chen729093d2017-10-16 10:33:26 -070073 // TODO: use UidMap if uid->pkg_name is required
Yangster-mac468ff042018-01-17 12:26:34 -080074 mDimensions = metric.dimensions_in_what();
Yao Chen729093d2017-10-16 10:33:26 -070075
76 if (metric.links().size() > 0) {
77 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
78 metric.links().end());
79 mConditionSliced = true;
80 }
81
Yangster-mac94e197c2018-01-02 16:03:03 -080082 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen729093d2017-10-16 10:33:26 -070083 (long long)mBucketSizeNs, (long long)mStartTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -070084}
85
Yao Chen44cf27c2017-09-14 22:32:50 -070086CountMetricProducer::~CountMetricProducer() {
87 VLOG("~CountMetricProducer() called");
88}
89
Yangsterf2bee6f2017-11-29 12:01:05 -080090void CountMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -080091 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -070092}
93
Yangster-mac20877162017-12-22 17:19:39 -080094void CountMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) {
95 flushIfNeededLocked(dumpTimeNs);
Yangster-mac94e197c2018-01-02 16:03:03 -080096 report->set_metric_id(mMetricId);
Yangster-mac20877162017-12-22 17:19:39 -080097 report->set_start_report_nanos(mStartTimeNs);
98
99 auto count_metrics = report->mutable_count_metrics();
100 for (const auto& counter : mPastBuckets) {
101 CountMetricData* metricData = count_metrics->add_data();
Yangster-mac468ff042018-01-17 12:26:34 -0800102 *metricData->mutable_dimensions_in_what() = counter.first.getDimensionsValue();
Yangster-mac20877162017-12-22 17:19:39 -0800103 for (const auto& bucket : counter.second) {
104 CountBucketInfo* bucketInfo = metricData->add_bucket_info();
105 bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs);
106 bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs);
107 bucketInfo->set_count(bucket.mCount);
108 }
109 }
110}
111
Yao Chen288c6002017-12-12 13:43:18 -0800112void CountMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
113 ProtoOutputStream* protoOutput) {
114 flushIfNeededLocked(dumpTimeNs);
Yao Chen6a8c7992017-11-29 20:02:07 +0000115
Yangster-mac94e197c2018-01-02 16:03:03 -0800116 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800117 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
118 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
119
Yangster-mac94e197c2018-01-02 16:03:03 -0800120 VLOG("metric %lld dump report now...",(long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700121
Yao Chen93fe3a32017-11-02 13:52:59 -0700122 for (const auto& counter : mPastBuckets) {
yro24809bd2017-10-31 23:06:53 -0700123 const HashableDimensionKey& hashableKey = counter.first;
Yao Chen93fe3a32017-11-02 13:52:59 -0700124 VLOG(" dimension key %s", hashableKey.c_str());
Yao Chend5aa01b32017-12-19 16:46:36 -0800125
yrob0378b02017-11-09 20:36:25 -0800126 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800127 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Yao Chen729093d2017-10-16 10:33:26 -0700128
Yangster-mac20877162017-12-22 17:19:39 -0800129 // First fill dimension.
130 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800131 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac20877162017-12-22 17:19:39 -0800132 writeDimensionsValueProtoToStream(hashableKey.getDimensionsValue(), protoOutput);
133 protoOutput->end(dimensionToken);
yro24809bd2017-10-31 23:06:53 -0700134
135 // Then fill bucket_info (CountBucketInfo).
Yao Chen93fe3a32017-11-02 13:52:59 -0700136 for (const auto& bucket : counter.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800137 long long bucketInfoToken = protoOutput->start(
138 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
139 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
140 (long long)bucket.mBucketStartNs);
141 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
142 (long long)bucket.mBucketEndNs);
143 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount);
144 protoOutput->end(bucketInfoToken);
Yao Chen93fe3a32017-11-02 13:52:59 -0700145 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
146 (long long)bucket.mBucketEndNs, (long long)bucket.mCount);
yro24809bd2017-10-31 23:06:53 -0700147 }
Yao Chen288c6002017-12-12 13:43:18 -0800148 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700149 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000150
Yao Chen288c6002017-12-12 13:43:18 -0800151 protoOutput->end(protoToken);
152 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
yro24809bd2017-10-31 23:06:53 -0700153
Yao Chen6a8c7992017-11-29 20:02:07 +0000154 mPastBuckets.clear();
Yao Chen288c6002017-12-12 13:43:18 -0800155 mStartTimeNs = mCurrentBucketStartTimeNs;
Yao Chen6a8c7992017-11-29 20:02:07 +0000156
157 // TODO: Clear mDimensionKeyMap once the report is dumped.
Yao Chen44cf27c2017-09-14 22:32:50 -0700158}
159
Yangsterf2bee6f2017-11-29 12:01:05 -0800160void CountMetricProducer::onConditionChangedLocked(const bool conditionMet,
161 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800162 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chencaf339d2017-10-06 16:01:10 -0700163 mCondition = conditionMet;
164}
165
Yangsterf2bee6f2017-11-29 12:01:05 -0800166bool CountMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800167 if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) {
168 return false;
169 }
170 // ===========GuardRail==============
171 // 1. Report the tuple count if the tuple count > soft limit
172 if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
173 size_t newTupleCount = mCurrentSlicedCounter->size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800174 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800175 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
176 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800177 ALOGE("CountMetric %lld dropping data for dimension key %s",
178 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800179 return true;
180 }
181 }
182
183 return false;
184}
Yangsterf2bee6f2017-11-29 12:01:05 -0800185
186void CountMetricProducer::onMatchedLogEventInternalLocked(
Yao Chenb7041772017-10-20 16:59:25 -0700187 const size_t matcherIndex, const HashableDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800188 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800189 const LogEvent& event) {
Yao Chen729093d2017-10-16 10:33:26 -0700190 uint64_t eventTimeNs = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -0700191
Yangsterf2bee6f2017-11-29 12:01:05 -0800192 flushIfNeededLocked(eventTimeNs);
Yao Chen729093d2017-10-16 10:33:26 -0700193
Yao Chen6a8c7992017-11-29 20:02:07 +0000194 if (condition == false) {
Yao Chenb7041772017-10-20 16:59:25 -0700195 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700196 }
Yao Chen729093d2017-10-16 10:33:26 -0700197
Yao Chen6a8c7992017-11-29 20:02:07 +0000198 auto it = mCurrentSlicedCounter->find(eventKey);
199
200 if (it == mCurrentSlicedCounter->end()) {
201 // ===========GuardRail==============
Yangsterf2bee6f2017-11-29 12:01:05 -0800202 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800203 return;
204 }
205
Yao Chen6a8c7992017-11-29 20:02:07 +0000206 // create a counter for the new key
207 (*mCurrentSlicedCounter)[eventKey] = 1;
208 } else {
209 // increment the existing value
210 auto& count = it->second;
211 count++;
Yao Chen729093d2017-10-16 10:33:26 -0700212 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000213
214 for (auto& tracker : mAnomalyTrackers) {
215 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey,
216 mCurrentSlicedCounter->find(eventKey)->second);
217 }
218
Yangster-mac94e197c2018-01-02 16:03:03 -0800219 VLOG("metric %lld %s->%lld", (long long)mMetricId, eventKey.c_str(),
Yao Chen6a8c7992017-11-29 20:02:07 +0000220 (long long)(*mCurrentSlicedCounter)[eventKey]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700221}
222
Yao Chen729093d2017-10-16 10:33:26 -0700223// When a new matched event comes in, we check if event falls into the current
224// bucket. If not, flush the old counter to past buckets and initialize the new bucket.
Yangsterf2bee6f2017-11-29 12:01:05 -0800225void CountMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800226 if (eventTimeNs < mCurrentBucketStartTimeNs + mBucketSizeNs) {
Yao Chen44cf27c2017-09-14 22:32:50 -0700227 return;
228 }
229
Yao Chen93fe3a32017-11-02 13:52:59 -0700230 CountBucket info;
231 info.mBucketStartNs = mCurrentBucketStartTimeNs;
232 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800233 info.mBucketNum = mCurrentBucketNum;
Yang Lu3eba6212017-10-25 19:54:45 -0700234 for (const auto& counter : *mCurrentSlicedCounter) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700235 info.mCount = counter.second;
236 auto& bucketList = mPastBuckets[counter.first];
237 bucketList.push_back(info);
Yangster-mac94e197c2018-01-02 16:03:03 -0800238 VLOG("metric %lld, dump key value: %s -> %lld",
239 (long long)mMetricId, counter.first.c_str(), (long long)counter.second);
Yao Chen729093d2017-10-16 10:33:26 -0700240 }
241
Yang Lu3eba6212017-10-25 19:54:45 -0700242 for (auto& tracker : mAnomalyTrackers) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800243 tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum);
Yang Lu3eba6212017-10-25 19:54:45 -0700244 }
Bookatzd3606c72017-10-19 10:13:49 -0700245
Yang Lu3eba6212017-10-25 19:54:45 -0700246 // Reset counters (do not clear, since the old one is still referenced in mAnomalyTrackers).
247 mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800248 uint64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700249 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
Yang Lu3eba6212017-10-25 19:54:45 -0700250 mCurrentBucketNum += numBucketsForward;
Yangster-mac94e197c2018-01-02 16:03:03 -0800251 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
Yao Chen729093d2017-10-16 10:33:26 -0700252 (long long)mCurrentBucketStartTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700253}
254
yro24809bd2017-10-31 23:06:53 -0700255// Rough estimate of CountMetricProducer buffer stored. This number will be
256// greater than actual data size as it contains each dimension of
257// CountMetricData is duplicated.
Yangsterf2bee6f2017-11-29 12:01:05 -0800258size_t CountMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800259 size_t totalSize = 0;
260 for (const auto& pair : mPastBuckets) {
261 totalSize += pair.second.size() * kBucketSize;
262 }
263 return totalSize;
yro69007c82017-10-26 20:42:57 -0700264}
265
Yao Chen44cf27c2017-09-14 22:32:50 -0700266} // namespace statsd
267} // namespace os
yro69007c82017-10-26 20:42:57 -0700268} // namespace android