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