Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -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 | |
| 17 | #define DEBUG true // STOPSHIP if true |
| 18 | #include "Log.h" |
| 19 | |
| 20 | #include "ValueMetricProducer.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | #include "guardrail/StatsdStats.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 22 | |
| 23 | #include <cutils/log.h> |
| 24 | #include <limits.h> |
| 25 | #include <stdlib.h> |
| 26 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 27 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 28 | using android::util::FIELD_TYPE_BOOL; |
| 29 | using android::util::FIELD_TYPE_FLOAT; |
| 30 | using android::util::FIELD_TYPE_INT32; |
| 31 | using android::util::FIELD_TYPE_INT64; |
| 32 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 33 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 34 | using android::util::ProtoOutputStream; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 35 | using std::list; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 36 | using std::make_pair; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 37 | using std::make_shared; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 38 | using std::map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 39 | using std::shared_ptr; |
| 40 | using std::unique_ptr; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 41 | using std::unordered_map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 42 | |
| 43 | namespace android { |
| 44 | namespace os { |
| 45 | namespace statsd { |
| 46 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 47 | // for StatsLogReport |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 48 | const int FIELD_ID_NAME = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 49 | const int FIELD_ID_START_REPORT_NANOS = 2; |
| 50 | const int FIELD_ID_END_REPORT_NANOS = 3; |
| 51 | const int FIELD_ID_VALUE_METRICS = 7; |
| 52 | // for ValueMetricDataWrapper |
| 53 | const int FIELD_ID_DATA = 1; |
| 54 | // for ValueMetricData |
| 55 | const int FIELD_ID_DIMENSION = 1; |
| 56 | const int FIELD_ID_BUCKET_INFO = 2; |
| 57 | // for KeyValuePair |
| 58 | const int FIELD_ID_KEY = 1; |
| 59 | const int FIELD_ID_VALUE_STR = 2; |
| 60 | const int FIELD_ID_VALUE_INT = 3; |
| 61 | const int FIELD_ID_VALUE_BOOL = 4; |
| 62 | const int FIELD_ID_VALUE_FLOAT = 5; |
| 63 | // for ValueBucketInfo |
| 64 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 65 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 66 | const int FIELD_ID_VALUE = 3; |
| 67 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 68 | static const uint64_t kDefaultBucketSizeMillis = 60 * 60 * 1000L; |
| 69 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 70 | // ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 71 | ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, |
| 72 | const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 73 | const sp<ConditionWizard>& wizard, const int pullTagId, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 74 | const uint64_t startTimeNs, |
| 75 | shared_ptr<StatsPullerManager> statsPullerManager) |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 76 | : MetricProducer(key, startTimeNs, conditionIndex, wizard), |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 77 | mMetric(metric), |
| 78 | mStatsPullerManager(statsPullerManager), |
| 79 | mPullTagId(pullTagId) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 80 | // TODO: valuemetric for pushed events may need unlimited bucket length |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 81 | if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) { |
| 82 | mBucketSizeNs = mMetric.bucket().bucket_size_millis() * 1000 * 1000; |
| 83 | } else { |
| 84 | mBucketSizeNs = kDefaultBucketSizeMillis * 1000 * 1000; |
| 85 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 86 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 87 | mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 88 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 89 | if (metric.links().size() > 0) { |
| 90 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 91 | metric.links().end()); |
| 92 | mConditionSliced = true; |
| 93 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 94 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 95 | if (!metric.has_condition() && mPullTagId != -1) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 96 | VLOG("Setting up periodic pulling for %d", mPullTagId); |
| 97 | mStatsPullerManager->RegisterReceiver(mPullTagId, this, |
| 98 | metric.bucket().bucket_size_millis()); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 99 | } |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 100 | VLOG("value metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(), |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 101 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 104 | // for testing |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 105 | ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, |
| 106 | const int conditionIndex, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 107 | const sp<ConditionWizard>& wizard, const int pullTagId, |
| 108 | const uint64_t startTimeNs) |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 109 | : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 110 | make_shared<StatsPullerManager>()) { |
| 111 | } |
| 112 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 113 | ValueMetricProducer::~ValueMetricProducer() { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 114 | VLOG("~ValueMetricProducer() called"); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 115 | if (mPullTagId != -1) { |
| 116 | mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); |
| 117 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 120 | void ValueMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 121 | VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 124 | void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 125 | ProtoOutputStream* protoOutput) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 126 | VLOG("metric %s dump report now...", mMetric.name().c_str()); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 127 | flushIfNeededLocked(dumpTimeNs); |
| 128 | protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name()); |
| 129 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs); |
| 130 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 131 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 132 | for (const auto& pair : mPastBuckets) { |
| 133 | const HashableDimensionKey& hashableKey = pair.first; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 134 | VLOG(" dimension key %s", hashableKey.c_str()); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 135 | auto it = mDimensionKeyMap.find(hashableKey); |
| 136 | if (it == mDimensionKeyMap.end()) { |
| 137 | ALOGE("Dimension key %s not found?!?! skip...", hashableKey.c_str()); |
| 138 | continue; |
| 139 | } |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 140 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 141 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 142 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 143 | // First fill dimension (KeyValuePairs). |
| 144 | for (const auto& kv : it->second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 145 | long long dimensionToken = protoOutput->start( |
| 146 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION); |
| 147 | protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 148 | if (kv.has_value_str()) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 149 | protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 150 | } else if (kv.has_value_int()) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 151 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 152 | } else if (kv.has_value_bool()) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 153 | protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 154 | } else if (kv.has_value_float()) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 155 | protoOutput->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 156 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 157 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Then fill bucket_info (ValueBucketInfo). |
| 161 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 162 | long long bucketInfoToken = protoOutput->start( |
| 163 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 164 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 165 | (long long)bucket.mBucketStartNs); |
| 166 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 167 | (long long)bucket.mBucketEndNs); |
| 168 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue); |
| 169 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 170 | VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs, |
| 171 | (long long)bucket.mBucketEndNs, (long long)bucket.mValue); |
| 172 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 173 | protoOutput->end(wrapperToken); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 174 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 175 | protoOutput->end(protoToken); |
| 176 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 177 | |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 178 | VLOG("metric %s dump report now...", mMetric.name().c_str()); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 179 | mPastBuckets.clear(); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame^] | 180 | mStartTimeNs = mCurrentBucketStartTimeNs; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 181 | // TODO: Clear mDimensionKeyMap once the report is dumped. |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 184 | void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 185 | mCondition = condition; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 186 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 187 | if (mPullTagId != -1) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 188 | if (mCondition == true) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 189 | mStatsPullerManager->RegisterReceiver(mPullTagId, this, |
| 190 | mMetric.bucket().bucket_size_millis()); |
| 191 | } else if (mCondition == false) { |
| 192 | mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 193 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 194 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 195 | vector<shared_ptr<LogEvent>> allData; |
| 196 | if (mStatsPullerManager->Pull(mPullTagId, &allData)) { |
| 197 | if (allData.size() == 0) { |
| 198 | return; |
| 199 | } |
| 200 | for (const auto& data : allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 201 | onMatchedLogEventLocked(0, *data, false); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 202 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 203 | flushIfNeededLocked(eventTime); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 204 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 205 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 206 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 210 | std::lock_guard<std::mutex> lock(mMutex); |
| 211 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 212 | if (mCondition == true || !mMetric.has_condition()) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 213 | if (allData.size() == 0) { |
| 214 | return; |
| 215 | } |
| 216 | uint64_t eventTime = allData.at(0)->GetTimestampNs(); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 217 | // alarm is not accurate and might drift. |
| 218 | if (eventTime > mCurrentBucketStartTimeNs + mBucketSizeNs * 3 / 2) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 219 | flushIfNeededLocked(eventTime); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 220 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 221 | for (const auto& data : allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 222 | onMatchedLogEventLocked(0, *data, true); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 223 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 224 | flushIfNeededLocked(eventTime); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 228 | bool ValueMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 229 | // ===========GuardRail============== |
| 230 | // 1. Report the tuple count if the tuple count > soft limit |
| 231 | if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) { |
| 232 | return false; |
| 233 | } |
| 234 | if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 235 | size_t newTupleCount = mCurrentSlicedBucket.size() + 1; |
| 236 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(), |
| 237 | newTupleCount); |
| 238 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 239 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
| 240 | ALOGE("ValueMetric %s dropping data for dimension key %s", mMetric.name().c_str(), |
| 241 | newKey.c_str()); |
| 242 | return true; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return false; |
| 247 | } |
| 248 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 249 | void ValueMetricProducer::onMatchedLogEventInternalLocked( |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 250 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 251 | const map<string, HashableDimensionKey>& conditionKey, bool condition, |
| 252 | const LogEvent& event, bool scheduledPull) { |
| 253 | uint64_t eventTimeNs = event.GetTimestampNs(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 254 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 255 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 256 | (long long)mCurrentBucketStartTimeNs); |
| 257 | return; |
| 258 | } |
| 259 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 260 | if (hitGuardRailLocked(eventKey)) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 261 | return; |
| 262 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 263 | Interval& interval = mCurrentSlicedBucket[eventKey]; |
| 264 | |
| 265 | long value = get_value(event); |
| 266 | |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 267 | if (mPullTagId != -1) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 268 | if (scheduledPull) { |
| 269 | // scheduled pull always sets beginning of current bucket and end |
| 270 | // of next bucket |
| 271 | if (interval.raw.size() > 0) { |
| 272 | interval.raw.back().second = value; |
| 273 | } else { |
| 274 | interval.raw.push_back(make_pair(value, value)); |
| 275 | } |
| 276 | Interval& nextInterval = mNextSlicedBucket[eventKey]; |
| 277 | if (nextInterval.raw.size() == 0) { |
| 278 | nextInterval.raw.push_back(make_pair(value, 0)); |
| 279 | } else { |
| 280 | nextInterval.raw.front().first = value; |
| 281 | } |
| 282 | } else { |
| 283 | if (mCondition == true) { |
| 284 | interval.raw.push_back(make_pair(value, 0)); |
| 285 | } else { |
| 286 | if (interval.raw.size() != 0) { |
| 287 | interval.raw.back().second = value; |
| 288 | } else { |
| 289 | interval.tainted = true; |
| 290 | VLOG("Data on condition true missing!"); |
| 291 | } |
| 292 | } |
| 293 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 294 | } else { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 295 | flushIfNeededLocked(eventTimeNs); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 296 | interval.raw.push_back(make_pair(value, 0)); |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 300 | long ValueMetricProducer::get_value(const LogEvent& event) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 301 | status_t err = NO_ERROR; |
| 302 | long val = event.GetLong(mMetric.value_field(), &err); |
| 303 | if (err == NO_ERROR) { |
| 304 | return val; |
| 305 | } else { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 306 | VLOG("Can't find value in message. %s", event.ToString().c_str()); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 311 | void ValueMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 312 | if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTimeNs) { |
| 313 | VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs, |
| 314 | (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs)); |
| 315 | return; |
| 316 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 317 | VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs, |
| 318 | (int)mCurrentSlicedBucket.size()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 319 | ValueBucket info; |
| 320 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 321 | info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 322 | info.mBucketNum = mCurrentBucketNum; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 323 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 324 | int tainted = 0; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 325 | for (const auto& slice : mCurrentSlicedBucket) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 326 | long value = 0; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 327 | if (mPullTagId != -1) { |
| 328 | for (const auto& pair : slice.second.raw) { |
| 329 | value += (pair.second - pair.first); |
| 330 | } |
| 331 | } else { |
| 332 | for (const auto& pair : slice.second.raw) { |
| 333 | value += pair.first; |
| 334 | } |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 335 | } |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 336 | tainted += slice.second.tainted; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 337 | info.mValue = value; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 338 | VLOG(" %s, %ld, %d", slice.first.c_str(), value, tainted); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 339 | // it will auto create new vector of ValuebucketInfo if the key is not found. |
| 340 | auto& bucketList = mPastBuckets[slice.first]; |
| 341 | bucketList.push_back(info); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // Reset counters |
| 345 | mCurrentSlicedBucket.swap(mNextSlicedBucket); |
| 346 | mNextSlicedBucket.clear(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 347 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 348 | int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 349 | mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs; |
| 350 | mCurrentBucketNum += numBucketsForward; |
| 351 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 352 | if (numBucketsForward > 1) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 353 | VLOG("Skipping forward %lld buckets", (long long)numBucketsForward); |
| 354 | } |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 355 | VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(), |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 356 | (long long)mCurrentBucketStartTimeNs); |
| 357 | } |
| 358 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 359 | size_t ValueMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 360 | size_t totalSize = 0; |
| 361 | for (const auto& pair : mPastBuckets) { |
| 362 | totalSize += pair.second.size() * kBucketSize; |
| 363 | } |
| 364 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 367 | } // namespace statsd |
| 368 | } // namespace os |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 369 | } // namespace android |