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