blob: 9031ed018df2cdeffaa1991dbe7c1e4cdd78f749 [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"
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 Chenb3561512017-11-21 18:07:17 -080065CountMetricProducer::CountMetricProducer(const ConfigKey& key, const CountMetric& metric,
66 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070067 const sp<ConditionWizard>& wizard,
68 const uint64_t startTimeNs)
Yao Chenf09569f2017-12-13 17:00:51 -080069 : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard) {
Yao Chen44cf27c2017-09-14 22:32:50 -070070 // TODO: evaluate initial conditions. and set mConditionMet.
71 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
Yao Chen729093d2017-10-16 10:33:26 -070072 mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000;
Yao Chen44cf27c2017-09-14 22:32:50 -070073 } else {
Yao Chen729093d2017-10-16 10:33:26 -070074 mBucketSizeNs = LLONG_MAX;
Yao Chen44cf27c2017-09-14 22:32:50 -070075 }
76
Yao Chen729093d2017-10-16 10:33:26 -070077 // TODO: use UidMap if uid->pkg_name is required
78 mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end());
79
80 if (metric.links().size() > 0) {
81 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
82 metric.links().end());
83 mConditionSliced = true;
84 }
85
Yangster-macd1815dc2017-11-13 21:43:15 -080086 VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
Yao Chen729093d2017-10-16 10:33:26 -070087 (long long)mBucketSizeNs, (long long)mStartTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -070088}
89
Yao Chen44cf27c2017-09-14 22:32:50 -070090CountMetricProducer::~CountMetricProducer() {
91 VLOG("~CountMetricProducer() called");
92}
93
Yangsterf2bee6f2017-11-29 12:01:05 -080094void CountMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yao Chenf09569f2017-12-13 17:00:51 -080095 VLOG("Metric %s onSlicedConditionMayChange", mName.c_str());
Yao Chen729093d2017-10-16 10:33:26 -070096}
97
Yao Chen288c6002017-12-12 13:43:18 -080098void CountMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
99 ProtoOutputStream* protoOutput) {
100 flushIfNeededLocked(dumpTimeNs);
Yao Chen6a8c7992017-11-29 20:02:07 +0000101
Yao Chenf09569f2017-12-13 17:00:51 -0800102 protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName);
Yao Chen288c6002017-12-12 13:43:18 -0800103 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
104 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
105
Yao Chenf09569f2017-12-13 17:00:51 -0800106 VLOG("metric %s dump report now...", mName.c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700107
Yao Chen93fe3a32017-11-02 13:52:59 -0700108 for (const auto& counter : mPastBuckets) {
yro24809bd2017-10-31 23:06:53 -0700109 const HashableDimensionKey& hashableKey = counter.first;
Yao Chend5aa01b32017-12-19 16:46:36 -0800110 const vector<KeyValuePair>& kvs = hashableKey.getKeyValuePairs();
Yao Chen93fe3a32017-11-02 13:52:59 -0700111 VLOG(" dimension key %s", hashableKey.c_str());
Yao Chend5aa01b32017-12-19 16:46:36 -0800112
yrob0378b02017-11-09 20:36:25 -0800113 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800114 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Yao Chen729093d2017-10-16 10:33:26 -0700115
yro24809bd2017-10-31 23:06:53 -0700116 // First fill dimension (KeyValuePairs).
Yao Chend5aa01b32017-12-19 16:46:36 -0800117 for (const auto& kv : kvs) {
Yao Chen288c6002017-12-12 13:43:18 -0800118 long long dimensionToken = protoOutput->start(
119 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
120 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key());
yro24809bd2017-10-31 23:06:53 -0700121 if (kv.has_value_str()) {
Yao Chen288c6002017-12-12 13:43:18 -0800122 protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str());
yro24809bd2017-10-31 23:06:53 -0700123 } else if (kv.has_value_int()) {
Yao Chen288c6002017-12-12 13:43:18 -0800124 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int());
yro24809bd2017-10-31 23:06:53 -0700125 } else if (kv.has_value_bool()) {
Yao Chen288c6002017-12-12 13:43:18 -0800126 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool());
yro24809bd2017-10-31 23:06:53 -0700127 } else if (kv.has_value_float()) {
Yao Chen288c6002017-12-12 13:43:18 -0800128 protoOutput->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float());
yro24809bd2017-10-31 23:06:53 -0700129 }
Yao Chen288c6002017-12-12 13:43:18 -0800130 protoOutput->end(dimensionToken);
yro24809bd2017-10-31 23:06:53 -0700131 }
132
133 // Then fill bucket_info (CountBucketInfo).
Yao Chen93fe3a32017-11-02 13:52:59 -0700134 for (const auto& bucket : counter.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800135 long long bucketInfoToken = protoOutput->start(
136 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
137 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
138 (long long)bucket.mBucketStartNs);
139 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
140 (long long)bucket.mBucketEndNs);
141 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount);
142 protoOutput->end(bucketInfoToken);
Yao Chen93fe3a32017-11-02 13:52:59 -0700143 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
144 (long long)bucket.mBucketEndNs, (long long)bucket.mCount);
yro24809bd2017-10-31 23:06:53 -0700145 }
Yao Chen288c6002017-12-12 13:43:18 -0800146 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700147 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000148
Yao Chen288c6002017-12-12 13:43:18 -0800149 protoOutput->end(protoToken);
150 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
yro24809bd2017-10-31 23:06:53 -0700151
Yao Chen6a8c7992017-11-29 20:02:07 +0000152 mPastBuckets.clear();
Yao Chen288c6002017-12-12 13:43:18 -0800153 mStartTimeNs = mCurrentBucketStartTimeNs;
Yao Chen6a8c7992017-11-29 20:02:07 +0000154
155 // TODO: Clear mDimensionKeyMap once the report is dumped.
Yao Chen44cf27c2017-09-14 22:32:50 -0700156}
157
Yangsterf2bee6f2017-11-29 12:01:05 -0800158void CountMetricProducer::onConditionChangedLocked(const bool conditionMet,
159 const uint64_t eventTime) {
Yao Chenf09569f2017-12-13 17:00:51 -0800160 VLOG("Metric %s onConditionChanged", mName.c_str());
Yao Chencaf339d2017-10-06 16:01:10 -0700161 mCondition = conditionMet;
162}
163
Yangsterf2bee6f2017-11-29 12:01:05 -0800164bool CountMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800165 if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) {
166 return false;
167 }
168 // ===========GuardRail==============
169 // 1. Report the tuple count if the tuple count > soft limit
170 if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
171 size_t newTupleCount = mCurrentSlicedCounter->size() + 1;
Yao Chenf09569f2017-12-13 17:00:51 -0800172 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mName, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800173 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
174 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yao Chenf09569f2017-12-13 17:00:51 -0800175 ALOGE("CountMetric %s dropping data for dimension key %s", mName.c_str(),
Yao Chenb3561512017-11-21 18:07:17 -0800176 newKey.c_str());
177 return true;
178 }
179 }
180
181 return false;
182}
Yangsterf2bee6f2017-11-29 12:01:05 -0800183
184void CountMetricProducer::onMatchedLogEventInternalLocked(
Yao Chenb7041772017-10-20 16:59:25 -0700185 const size_t matcherIndex, const HashableDimensionKey& eventKey,
186 const map<string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800187 const LogEvent& event) {
Yao Chen729093d2017-10-16 10:33:26 -0700188 uint64_t eventTimeNs = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -0700189
Yangsterf2bee6f2017-11-29 12:01:05 -0800190 flushIfNeededLocked(eventTimeNs);
Yao Chen729093d2017-10-16 10:33:26 -0700191
Yao Chen6a8c7992017-11-29 20:02:07 +0000192 if (condition == false) {
Yao Chenb7041772017-10-20 16:59:25 -0700193 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700194 }
Yao Chen729093d2017-10-16 10:33:26 -0700195
Yao Chen6a8c7992017-11-29 20:02:07 +0000196 auto it = mCurrentSlicedCounter->find(eventKey);
197
198 if (it == mCurrentSlicedCounter->end()) {
199 // ===========GuardRail==============
Yangsterf2bee6f2017-11-29 12:01:05 -0800200 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800201 return;
202 }
203
Yao Chen6a8c7992017-11-29 20:02:07 +0000204 // create a counter for the new key
205 (*mCurrentSlicedCounter)[eventKey] = 1;
206 } else {
207 // increment the existing value
208 auto& count = it->second;
209 count++;
Yao Chen729093d2017-10-16 10:33:26 -0700210 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000211
212 for (auto& tracker : mAnomalyTrackers) {
213 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey,
214 mCurrentSlicedCounter->find(eventKey)->second);
215 }
216
Yao Chenf09569f2017-12-13 17:00:51 -0800217 VLOG("metric %s %s->%lld", mName.c_str(), eventKey.c_str(),
Yao Chen6a8c7992017-11-29 20:02:07 +0000218 (long long)(*mCurrentSlicedCounter)[eventKey]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700219}
220
Yao Chen729093d2017-10-16 10:33:26 -0700221// When a new matched event comes in, we check if event falls into the current
222// bucket. If not, flush the old counter to past buckets and initialize the new bucket.
Yangsterf2bee6f2017-11-29 12:01:05 -0800223void CountMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800224 if (eventTimeNs < mCurrentBucketStartTimeNs + mBucketSizeNs) {
Yao Chen44cf27c2017-09-14 22:32:50 -0700225 return;
226 }
227
Yao Chen93fe3a32017-11-02 13:52:59 -0700228 CountBucket info;
229 info.mBucketStartNs = mCurrentBucketStartTimeNs;
230 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800231 info.mBucketNum = mCurrentBucketNum;
Yang Lu3eba6212017-10-25 19:54:45 -0700232 for (const auto& counter : *mCurrentSlicedCounter) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700233 info.mCount = counter.second;
234 auto& bucketList = mPastBuckets[counter.first];
235 bucketList.push_back(info);
Yao Chenf09569f2017-12-13 17:00:51 -0800236 VLOG("metric %s, dump key value: %s -> %lld", mName.c_str(), counter.first.c_str(),
Yangster-mace2cd6d52017-11-09 20:38:30 -0800237 (long long)counter.second);
Yao Chen729093d2017-10-16 10:33:26 -0700238 }
239
Yang Lu3eba6212017-10-25 19:54:45 -0700240 for (auto& tracker : mAnomalyTrackers) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800241 tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum);
Yang Lu3eba6212017-10-25 19:54:45 -0700242 }
Bookatzd3606c72017-10-19 10:13:49 -0700243
Yang Lu3eba6212017-10-25 19:54:45 -0700244 // Reset counters (do not clear, since the old one is still referenced in mAnomalyTrackers).
245 mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800246 uint64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700247 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
Yang Lu3eba6212017-10-25 19:54:45 -0700248 mCurrentBucketNum += numBucketsForward;
Yao Chenf09569f2017-12-13 17:00:51 -0800249 VLOG("metric %s: new bucket start time: %lld", mName.c_str(),
Yao Chen729093d2017-10-16 10:33:26 -0700250 (long long)mCurrentBucketStartTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700251}
252
yro24809bd2017-10-31 23:06:53 -0700253// Rough estimate of CountMetricProducer buffer stored. This number will be
254// greater than actual data size as it contains each dimension of
255// CountMetricData is duplicated.
Yangsterf2bee6f2017-11-29 12:01:05 -0800256size_t CountMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800257 size_t totalSize = 0;
258 for (const auto& pair : mPastBuckets) {
259 totalSize += pair.second.size() * kBucketSize;
260 }
261 return totalSize;
yro69007c82017-10-26 20:42:57 -0700262}
263
Yao Chen44cf27c2017-09-14 22:32:50 -0700264} // namespace statsd
265} // namespace os
yro69007c82017-10-26 20:42:57 -0700266} // namespace android