Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 17 | #define DEBUG true // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 19 | |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 20 | #include "CountAnomalyTracker.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 21 | #include "CountMetricProducer.h" |
| 22 | #include "stats_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 23 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 24 | #include <limits.h> |
| 25 | #include <stdlib.h> |
| 26 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 27 | using namespace android::util; |
| 28 | using android::util::ProtoOutputStream; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 29 | using std::map; |
| 30 | using std::string; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 31 | using std::unordered_map; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 32 | using std::vector; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | namespace os { |
| 36 | namespace statsd { |
| 37 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 38 | // for StatsLogReport |
| 39 | const int FIELD_ID_METRIC_ID = 1; |
| 40 | const int FIELD_ID_START_REPORT_NANOS = 2; |
| 41 | const int FIELD_ID_END_REPORT_NANOS = 3; |
| 42 | const int FIELD_ID_COUNT_METRICS = 5; |
| 43 | // for CountMetricDataWrapper |
| 44 | const int FIELD_ID_DATA = 1; |
| 45 | // for CountMetricData |
| 46 | const int FIELD_ID_DIMENSION = 1; |
| 47 | const int FIELD_ID_BUCKET_INFO = 2; |
| 48 | // for KeyValuePair |
| 49 | const int FIELD_ID_KEY = 1; |
| 50 | const int FIELD_ID_VALUE_STR = 2; |
| 51 | const int FIELD_ID_VALUE_INT = 3; |
| 52 | const int FIELD_ID_VALUE_BOOL = 4; |
| 53 | const int FIELD_ID_VALUE_FLOAT = 5; |
| 54 | // for CountBucketInfo |
| 55 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 56 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 57 | const int FIELD_ID_COUNT = 3; |
| 58 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 59 | // TODO: add back AnomalyTracker. |
| 60 | CountMetricProducer::CountMetricProducer(const CountMetric& metric, const int conditionIndex, |
| 61 | const sp<ConditionWizard>& wizard) |
| 62 | // TODO: Pass in the start time from MetricsManager, instead of calling time() here. |
| 63 | : MetricProducer((time(nullptr) * NANO_SECONDS_IN_A_SECOND), conditionIndex, wizard), |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 64 | mMetric(metric) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 65 | // TODO: evaluate initial conditions. and set mConditionMet. |
| 66 | if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 67 | mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 68 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 69 | mBucketSizeNs = LLONG_MAX; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 72 | mAnomalyTrackers.reserve(metric.alerts_size()); |
| 73 | for (int i = 0; i < metric.alerts_size(); i++) { |
| 74 | const Alert& alert = metric.alerts(i); |
| 75 | if (alert.trigger_if_sum_gt() > 0 && alert.number_of_buckets() > 0) { |
| 76 | mAnomalyTrackers.push_back(std::make_unique<CountAnomalyTracker>(alert)); |
| 77 | } else { |
| 78 | ALOGW("Ignoring invalid count metric alert: threshold=%lld num_buckets= %d", |
| 79 | alert.trigger_if_sum_gt(), alert.number_of_buckets()); |
| 80 | } |
| 81 | } |
| 82 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 83 | // TODO: use UidMap if uid->pkg_name is required |
| 84 | mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end()); |
| 85 | |
| 86 | if (metric.links().size() > 0) { |
| 87 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 88 | metric.links().end()); |
| 89 | mConditionSliced = true; |
| 90 | } |
| 91 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 92 | startNewProtoOutputStream(mStartTimeNs); |
| 93 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 94 | VLOG("metric %lld created. bucket size %lld start_time: %lld", metric.metric_id(), |
| 95 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 98 | CountMetricProducer::~CountMetricProducer() { |
| 99 | VLOG("~CountMetricProducer() called"); |
| 100 | } |
| 101 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 102 | void CountMetricProducer::startNewProtoOutputStream(long long startTime) { |
| 103 | mProto = std::make_unique<ProtoOutputStream>(); |
| 104 | mProto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_ID, mMetric.metric_id()); |
| 105 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime); |
| 106 | mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 107 | } |
| 108 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 109 | void CountMetricProducer::finish() { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 112 | void CountMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 113 | VLOG("Metric %lld onSlicedConditionMayChange", mMetric.metric_id()); |
| 114 | } |
| 115 | |
| 116 | StatsLogReport CountMetricProducer::onDumpReport() { |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 117 | long long endTime = time(nullptr) * NANO_SECONDS_IN_A_SECOND; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 118 | |
| 119 | // Dump current bucket if it's stale. |
| 120 | // If current bucket is still on-going, don't force dump current bucket. |
| 121 | // In finish(), We can force dump current bucket. |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 122 | flushCounterIfNeeded(endTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 123 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 124 | for (const auto& counter : mPastBucketProtos) { |
| 125 | const HashableDimensionKey& hashableKey = counter.first; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 126 | auto it = mDimensionKeyMap.find(hashableKey); |
| 127 | if (it == mDimensionKeyMap.end()) { |
| 128 | ALOGE("Dimension key %s not found?!?! skip...", hashableKey.c_str()); |
| 129 | continue; |
| 130 | } |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 131 | long long wrapperToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_DATA); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 132 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 133 | // First fill dimension (KeyValuePairs). |
| 134 | for (const auto& kv : it->second) { |
| 135 | long long dimensionToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION); |
| 136 | mProto->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key()); |
| 137 | if (kv.has_value_str()) { |
| 138 | mProto->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_STR, kv.value_str()); |
| 139 | } 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). |
| 150 | for (const auto& proto : counter.second) { |
| 151 | size_t bufferSize = proto->size(); |
| 152 | char* buffer(new char[bufferSize]); |
| 153 | size_t pos = 0; |
| 154 | auto it = proto->data(); |
| 155 | while (it.readBuffer() != NULL) { |
| 156 | size_t toRead = it.currentToRead(); |
| 157 | std::memcpy(&buffer[pos], it.readBuffer(), toRead); |
| 158 | pos += toRead; |
| 159 | it.rp()->move(toRead); |
| 160 | } |
| 161 | mProto->write(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION, buffer, bufferSize); |
| 162 | } |
| 163 | |
| 164 | mProto->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 165 | } |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 166 | |
| 167 | mProto->end(mProtoToken); |
| 168 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, |
| 169 | (long long)mCurrentBucketStartTimeNs); |
| 170 | |
| 171 | size_t bufferSize = mProto->size(); |
| 172 | VLOG("metric %lld dump report now...", mMetric.metric_id()); |
| 173 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[bufferSize]); |
| 174 | size_t pos = 0; |
| 175 | auto it = mProto->data(); |
| 176 | while (it.readBuffer() != NULL) { |
| 177 | size_t toRead = it.currentToRead(); |
| 178 | std::memcpy(&buffer[pos], it.readBuffer(), toRead); |
| 179 | pos += toRead; |
| 180 | it.rp()->move(toRead); |
| 181 | } |
| 182 | |
| 183 | startNewProtoOutputStream(endTime); |
| 184 | mPastBucketProtos.clear(); |
| 185 | mByteSize = 0; |
| 186 | |
| 187 | // TODO: Once we migrate all MetricProducers to use ProtoOutputStream, we should return this: |
| 188 | // return std::move(buffer); |
| 189 | return StatsLogReport(); |
| 190 | |
| 191 | // TODO: Clear mDimensionKeyMap once the report is dumped. |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 194 | void CountMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 195 | VLOG("Metric %lld onConditionChanged", mMetric.metric_id()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 196 | mCondition = conditionMet; |
| 197 | } |
| 198 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 199 | void CountMetricProducer::onMatchedLogEventInternal( |
| 200 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 201 | const map<string, HashableDimensionKey>& conditionKey, bool condition, |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 202 | const LogEvent& event, bool scheduledPull) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 203 | uint64_t eventTimeNs = event.GetTimestampNs(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 204 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 205 | flushCounterIfNeeded(eventTimeNs); |
| 206 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 207 | if (condition == false) { |
| 208 | return; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 209 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 210 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 211 | auto it = mCurrentSlicedCounter.find(eventKey); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 212 | |
| 213 | if (it == mCurrentSlicedCounter.end()) { |
| 214 | // create a counter for the new key |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 215 | mCurrentSlicedCounter[eventKey] = 1; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 216 | |
| 217 | } else { |
| 218 | // increment the existing value |
| 219 | auto& count = it->second; |
| 220 | count++; |
| 221 | } |
| 222 | |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 223 | // TODO: Re-add anomaly detection (similar to): |
| 224 | // for (auto& tracker : mAnomalyTrackers) { |
| 225 | // tracker->checkAnomaly(mCounter); |
| 226 | // } |
| 227 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 228 | VLOG("metric %lld %s->%d", mMetric.metric_id(), eventKey.c_str(), |
| 229 | mCurrentSlicedCounter[eventKey]); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 232 | // When a new matched event comes in, we check if event falls into the current |
| 233 | // bucket. If not, flush the old counter to past buckets and initialize the new bucket. |
| 234 | void CountMetricProducer::flushCounterIfNeeded(const uint64_t eventTimeNs) { |
| 235 | if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTimeNs) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 239 | // adjust the bucket start time |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 240 | int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 241 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 242 | for (const auto& counter : mCurrentSlicedCounter) { |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 243 | unique_ptr<ProtoOutputStream> proto = make_unique<ProtoOutputStream>(); |
| 244 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 245 | (long long)mCurrentBucketStartTimeNs); |
| 246 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 247 | (long long)mCurrentBucketStartTimeNs + mBucketSizeNs); |
| 248 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)counter.second); |
| 249 | |
| 250 | auto& bucketList = mPastBucketProtos[counter.first]; |
| 251 | bucketList.push_back(std::move(proto)); |
| 252 | mByteSize += proto->size(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 253 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 254 | VLOG("metric %lld, dump key value: %s -> %d", mMetric.metric_id(), counter.first.c_str(), |
| 255 | counter.second); |
| 256 | } |
| 257 | |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 258 | // TODO: Re-add anomaly detection (similar to): |
| 259 | // for (auto& tracker : mAnomalyTrackers) { |
| 260 | // tracker->addPastBucket(mCounter, numBucketsForward); |
| 261 | //} |
| 262 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 263 | // Reset counters |
| 264 | mCurrentSlicedCounter.clear(); |
| 265 | |
| 266 | mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs; |
| 267 | VLOG("metric %lld: new bucket start time: %lld", mMetric.metric_id(), |
| 268 | (long long)mCurrentBucketStartTimeNs); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 269 | } |
| 270 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 271 | // Rough estimate of CountMetricProducer buffer stored. This number will be |
| 272 | // greater than actual data size as it contains each dimension of |
| 273 | // CountMetricData is duplicated. |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 274 | size_t CountMetricProducer::byteSize() { |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame^] | 275 | return mByteSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 278 | } // namespace statsd |
| 279 | } // namespace os |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 280 | } // namespace android |