blob: eed7841ab435a77743012aeec27276b76c3e6411 [file] [log] [blame]
Chenjie Yub3dda412017-10-24 13:41:59 -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
17#define DEBUG true // STOPSHIP if true
18#include "Log.h"
19
20#include "ValueMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070022
23#include <cutils/log.h>
24#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;
yro2b0f8862017-11-06 14:27:31 -080034using android::util::ProtoOutputStream;
Chenjie Yub3dda412017-10-24 13:41:59 -070035using std::list;
Chenjie Yu6736c892017-11-09 10:50:09 -080036using std::make_pair;
Chenjie Yub3dda412017-10-24 13:41:59 -070037using std::make_shared;
Yao Chen93fe3a32017-11-02 13:52:59 -070038using std::map;
Chenjie Yub3dda412017-10-24 13:41:59 -070039using std::shared_ptr;
40using std::unique_ptr;
Yao Chen93fe3a32017-11-02 13:52:59 -070041using std::unordered_map;
Chenjie Yub3dda412017-10-24 13:41:59 -070042
43namespace android {
44namespace os {
45namespace statsd {
46
yro2b0f8862017-11-06 14:27:31 -080047// for StatsLogReport
Yangster-macd1815dc2017-11-13 21:43:15 -080048const int FIELD_ID_NAME = 1;
yro2b0f8862017-11-06 14:27:31 -080049const int FIELD_ID_START_REPORT_NANOS = 2;
50const int FIELD_ID_END_REPORT_NANOS = 3;
51const int FIELD_ID_VALUE_METRICS = 7;
52// for ValueMetricDataWrapper
53const int FIELD_ID_DATA = 1;
54// for ValueMetricData
55const int FIELD_ID_DIMENSION = 1;
56const int FIELD_ID_BUCKET_INFO = 2;
57// for KeyValuePair
58const int FIELD_ID_KEY = 1;
59const int FIELD_ID_VALUE_STR = 2;
60const int FIELD_ID_VALUE_INT = 3;
61const int FIELD_ID_VALUE_BOOL = 4;
62const int FIELD_ID_VALUE_FLOAT = 5;
63// for ValueBucketInfo
64const int FIELD_ID_START_BUCKET_NANOS = 1;
65const int FIELD_ID_END_BUCKET_NANOS = 2;
66const int FIELD_ID_VALUE = 3;
67
Chenjie Yu6736c892017-11-09 10:50:09 -080068static const uint64_t kDefaultBucketSizeMillis = 60 * 60 * 1000L;
69
Chenjie Yub3dda412017-10-24 13:41:59 -070070// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Yao Chenb3561512017-11-21 18:07:17 -080071ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
72 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070073 const sp<ConditionWizard>& wizard, const int pullTagId,
Chenjie Yu6736c892017-11-09 10:50:09 -080074 const uint64_t startTimeNs,
75 shared_ptr<StatsPullerManager> statsPullerManager)
Yao Chenb3561512017-11-21 18:07:17 -080076 : MetricProducer(key, startTimeNs, conditionIndex, wizard),
Chenjie Yu6736c892017-11-09 10:50:09 -080077 mMetric(metric),
78 mStatsPullerManager(statsPullerManager),
79 mPullTagId(pullTagId) {
Yao Chen93fe3a32017-11-02 13:52:59 -070080 // TODO: valuemetric for pushed events may need unlimited bucket length
Chenjie Yu6736c892017-11-09 10:50:09 -080081 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 Yub3dda412017-10-24 13:41:59 -070086
Yao Chen93fe3a32017-11-02 13:52:59 -070087 mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end());
Chenjie Yub3dda412017-10-24 13:41:59 -070088
Yao Chen93fe3a32017-11-02 13:52:59 -070089 if (metric.links().size() > 0) {
90 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
91 metric.links().end());
92 mConditionSliced = true;
93 }
Chenjie Yub3dda412017-10-24 13:41:59 -070094
Yao Chen93fe3a32017-11-02 13:52:59 -070095 if (!metric.has_condition() && mPullTagId != -1) {
Chenjie Yu6736c892017-11-09 10:50:09 -080096 VLOG("Setting up periodic pulling for %d", mPullTagId);
97 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
98 metric.bucket().bucket_size_millis());
Yao Chen93fe3a32017-11-02 13:52:59 -070099 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700100
yro2b0f8862017-11-06 14:27:31 -0800101 startNewProtoOutputStream(mStartTimeNs);
102
Yangster-macd1815dc2017-11-13 21:43:15 -0800103 VLOG("value metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
Yao Chen93fe3a32017-11-02 13:52:59 -0700104 (long long)mBucketSizeNs, (long long)mStartTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700105}
106
Chenjie Yu6736c892017-11-09 10:50:09 -0800107// for testing
Yao Chenb3561512017-11-21 18:07:17 -0800108ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
109 const int conditionIndex,
Chenjie Yu6736c892017-11-09 10:50:09 -0800110 const sp<ConditionWizard>& wizard, const int pullTagId,
111 const uint64_t startTimeNs)
Yao Chenb3561512017-11-21 18:07:17 -0800112 : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs,
Chenjie Yu6736c892017-11-09 10:50:09 -0800113 make_shared<StatsPullerManager>()) {
114}
115
Chenjie Yub3dda412017-10-24 13:41:59 -0700116ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700117 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800118 if (mPullTagId != -1) {
119 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
120 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700121}
122
yro2b0f8862017-11-06 14:27:31 -0800123void ValueMetricProducer::startNewProtoOutputStream(long long startTime) {
Yangster8de69392017-11-27 13:48:29 -0800124 std::lock_guard<std::shared_timed_mutex> writeLock(mRWMutex);
yro2b0f8862017-11-06 14:27:31 -0800125 mProto = std::make_unique<ProtoOutputStream>();
Yangster-macd1815dc2017-11-13 21:43:15 -0800126 mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
yro2b0f8862017-11-06 14:27:31 -0800127 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
128 mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
129}
130
Chenjie Yub3dda412017-10-24 13:41:59 -0700131void ValueMetricProducer::finish() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700132 // TODO: write the StatsLogReport to dropbox using
133 // DropboxWriter.
Chenjie Yub3dda412017-10-24 13:41:59 -0700134}
135
Chenjie Yub3dda412017-10-24 13:41:59 -0700136void ValueMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800137 VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
Chenjie Yub3dda412017-10-24 13:41:59 -0700138}
139
Yangster8de69392017-11-27 13:48:29 -0800140void ValueMetricProducer::SerializeBuckets() {
141 std::lock_guard<std::shared_timed_mutex> writeLock(mRWMutex);
Yao Chen93fe3a32017-11-02 13:52:59 -0700142 for (const auto& pair : mPastBuckets) {
143 const HashableDimensionKey& hashableKey = pair.first;
yro2b0f8862017-11-06 14:27:31 -0800144 VLOG(" dimension key %s", hashableKey.c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700145 auto it = mDimensionKeyMap.find(hashableKey);
146 if (it == mDimensionKeyMap.end()) {
147 ALOGE("Dimension key %s not found?!?! skip...", hashableKey.c_str());
148 continue;
149 }
yrob0378b02017-11-09 20:36:25 -0800150 long long wrapperToken =
151 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700152
yro2b0f8862017-11-06 14:27:31 -0800153 // First fill dimension (KeyValuePairs).
154 for (const auto& kv : it->second) {
yrob0378b02017-11-09 20:36:25 -0800155 long long dimensionToken =
156 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
yro2b0f8862017-11-06 14:27:31 -0800157 mProto->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key());
158 if (kv.has_value_str()) {
Yao Chen1ff4f432017-11-16 17:01:40 -0800159 mProto->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str());
yro2b0f8862017-11-06 14:27:31 -0800160 } else if (kv.has_value_int()) {
161 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int());
162 } else if (kv.has_value_bool()) {
163 mProto->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool());
164 } else if (kv.has_value_float()) {
165 mProto->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float());
166 }
167 mProto->end(dimensionToken);
168 }
169
170 // Then fill bucket_info (ValueBucketInfo).
171 for (const auto& bucket : pair.second) {
yrob0378b02017-11-09 20:36:25 -0800172 long long bucketInfoToken =
173 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
yro2b0f8862017-11-06 14:27:31 -0800174 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
175 (long long)bucket.mBucketStartNs);
176 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
177 (long long)bucket.mBucketEndNs);
178 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
179 mProto->end(bucketInfoToken);
180 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
181 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
182 }
183 mProto->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700184 }
yro2b0f8862017-11-06 14:27:31 -0800185 mProto->end(mProtoToken);
186 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS,
187 (long long)mCurrentBucketStartTimeNs);
Yangster8de69392017-11-27 13:48:29 -0800188 mPastBuckets.clear();
189}
yro2b0f8862017-11-06 14:27:31 -0800190
Yangster8de69392017-11-27 13:48:29 -0800191std::unique_ptr<std::vector<uint8_t>> ValueMetricProducer::onDumpReport() {
Yangster-macd1815dc2017-11-13 21:43:15 -0800192 VLOG("metric %s dump report now...", mMetric.name().c_str());
Yangster8de69392017-11-27 13:48:29 -0800193
194 SerializeBuckets();
yro17adac92017-11-08 23:16:29 -0800195 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto();
yro2b0f8862017-11-06 14:27:31 -0800196
197 startNewProtoOutputStream(time(nullptr) * NS_PER_SEC);
yro2b0f8862017-11-06 14:27:31 -0800198
yro17adac92017-11-08 23:16:29 -0800199 return buffer;
yro2b0f8862017-11-06 14:27:31 -0800200 // TODO: Clear mDimensionKeyMap once the report is dumped.
Chenjie Yub3dda412017-10-24 13:41:59 -0700201}
202
203void ValueMetricProducer::onConditionChanged(const bool condition, const uint64_t eventTime) {
Yangster8de69392017-11-27 13:48:29 -0800204 vector<shared_ptr<LogEvent>> allData;
Chenjie Yub3dda412017-10-24 13:41:59 -0700205
Yangster8de69392017-11-27 13:48:29 -0800206 // TODO(yanglu): move the following logic to a seperate function to make it lockable.
207 {
208 std::lock_guard<std::shared_timed_mutex> writeLock(mRWMutex);
209 mCondition = condition;
210
211 if (mPullTagId == -1) {
212 return;
213 }
214
Chenjie Yub3dda412017-10-24 13:41:59 -0700215 if (mCondition == true) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800216 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
217 mMetric.bucket().bucket_size_millis());
218 } else if (mCondition == false) {
219 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yub3dda412017-10-24 13:41:59 -0700220 }
Yangster8de69392017-11-27 13:48:29 -0800221 if (!mStatsPullerManager->Pull(mPullTagId, &allData)) {
222 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700223 }
Yangster8de69392017-11-27 13:48:29 -0800224 }
225
226 if (allData.size() == 0) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700227 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700228 }
Yangster8de69392017-11-27 13:48:29 -0800229
230 // onMatchedLogEventInternal holds the write lock and is thread-safe.
231 for (const auto& data : allData) {
232 onMatchedLogEvent(0, *data, false);
233 }
234 // flushIfNeeded holds the write lock and is thread-safe.
235 flushIfNeeded(eventTime);
236}
237
238bool ValueMetricProducer::IsConditionMet() const {
239 std::shared_lock<std::shared_timed_mutex> readLock(mRWMutex);
240 return mCondition == true || !mMetric.has_condition();
Chenjie Yub3dda412017-10-24 13:41:59 -0700241}
242
243void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangster8de69392017-11-27 13:48:29 -0800244 if (IsConditionMet()) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700245 if (allData.size() == 0) {
246 return;
247 }
248 uint64_t eventTime = allData.at(0)->GetTimestampNs();
Chenjie Yu6736c892017-11-09 10:50:09 -0800249 // alarm is not accurate and might drift.
250 if (eventTime > mCurrentBucketStartTimeNs + mBucketSizeNs * 3 / 2) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800251 flushIfNeeded(eventTime);
Chenjie Yu6736c892017-11-09 10:50:09 -0800252 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700253 for (const auto& data : allData) {
254 onMatchedLogEvent(0, *data, true);
255 }
Yangster-mace2cd6d52017-11-09 20:38:30 -0800256 flushIfNeeded(eventTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700257 }
258}
259
Yao Chenb3561512017-11-21 18:07:17 -0800260bool ValueMetricProducer::hitGuardRail(const HashableDimensionKey& newKey) {
Yangster8de69392017-11-27 13:48:29 -0800261 std::shared_lock<std::shared_timed_mutex> readLock(mRWMutex);
Yao Chenb3561512017-11-21 18:07:17 -0800262 // ===========GuardRail==============
263 // 1. Report the tuple count if the tuple count > soft limit
264 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
265 return false;
266 }
267 if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
268 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
269 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(),
270 newTupleCount);
271 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
272 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
273 ALOGE("ValueMetric %s dropping data for dimension key %s", mMetric.name().c_str(),
274 newKey.c_str());
275 return true;
276 }
277 }
278
279 return false;
280}
281
Yangster8de69392017-11-27 13:48:29 -0800282void ValueMetricProducer::onMatchedLogEventInternal_pull(const uint64_t& eventTimeNs,
283 const HashableDimensionKey& eventKey,
284 const long& value, bool scheduledPull) {
285 std::lock_guard<std::shared_timed_mutex> writeLock(mRWMutex);
286
Yao Chen93fe3a32017-11-02 13:52:59 -0700287 if (eventTimeNs < mCurrentBucketStartTimeNs) {
288 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
289 (long long)mCurrentBucketStartTimeNs);
290 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700291 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700292 Interval& interval = mCurrentSlicedBucket[eventKey];
Yangster8de69392017-11-27 13:48:29 -0800293 if (scheduledPull) {
294 // scheduled pull always sets beginning of current bucket and end
295 // of next bucket
296 if (interval.raw.size() > 0) {
297 interval.raw.back().second = value;
Chenjie Yu6736c892017-11-09 10:50:09 -0800298 } else {
Yangster8de69392017-11-27 13:48:29 -0800299 interval.raw.push_back(make_pair(value, value));
300 }
301 Interval& nextInterval = mNextSlicedBucket[eventKey];
302 if (nextInterval.raw.size() == 0) {
303 nextInterval.raw.push_back(make_pair(value, 0));
304 } else {
305 nextInterval.raw.front().first = value;
Yao Chen93fe3a32017-11-02 13:52:59 -0700306 }
Chenjie Yu6736c892017-11-09 10:50:09 -0800307 } else {
Yangster8de69392017-11-27 13:48:29 -0800308 if (mCondition == true) {
309 interval.raw.push_back(make_pair(value, 0));
310 } else {
311 if (interval.raw.size() != 0) {
312 interval.raw.back().second = value;
313 } else {
314 interval.tainted = true;
315 VLOG("Data on condition true missing!");
316 }
317 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700318 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700319}
320
Yangster8de69392017-11-27 13:48:29 -0800321void ValueMetricProducer::onMatchedLogEventInternal_push(const uint64_t& eventTimeNs,
322 const HashableDimensionKey& eventKey,
323 const long& value) {
324 std::lock_guard<std::shared_timed_mutex> writeLock(mRWMutex);
325 if (eventTimeNs < mCurrentBucketStartTimeNs) {
326 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
327 (long long)mCurrentBucketStartTimeNs);
328 return;
329 }
330 mCurrentSlicedBucket[eventKey].raw.push_back(make_pair(value, 0));
331}
332
333void ValueMetricProducer::onMatchedLogEventInternal(
334 const size_t matcherIndex, const HashableDimensionKey& eventKey,
335 const map<string, HashableDimensionKey>& conditionKey, bool condition,
336 const LogEvent& event, bool scheduledPull) {
337 uint64_t eventTimeNs = event.GetTimestampNs();
338 long value = get_value(event);
339 if (hitGuardRail(eventKey)) {
340 return;
341 }
342 if (mPullTagId != -1) {
343 onMatchedLogEventInternal_pull(eventTimeNs, eventKey, value, scheduledPull);
344 } else {
345 flushIfNeeded(eventTimeNs);
346 onMatchedLogEventInternal_push(eventTimeNs, eventKey, value);
347 }
348}
349
350long ValueMetricProducer::get_value(const LogEvent& event) const {
Yao Chen93fe3a32017-11-02 13:52:59 -0700351 status_t err = NO_ERROR;
352 long val = event.GetLong(mMetric.value_field(), &err);
353 if (err == NO_ERROR) {
354 return val;
355 } else {
Chenjie Yu6736c892017-11-09 10:50:09 -0800356 VLOG("Can't find value in message. %s", event.ToString().c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700357 return 0;
358 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700359}
360
Yangster-mace2cd6d52017-11-09 20:38:30 -0800361void ValueMetricProducer::flushIfNeeded(const uint64_t eventTimeNs) {
Yangster8de69392017-11-27 13:48:29 -0800362 std::lock_guard<std::shared_timed_mutex> writeLock(mRWMutex);
Chenjie Yub3dda412017-10-24 13:41:59 -0700363 if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTimeNs) {
364 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
365 (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs));
366 return;
367 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700368 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
369 (int)mCurrentSlicedBucket.size());
yro2b0f8862017-11-06 14:27:31 -0800370 ValueBucket info;
371 info.mBucketStartNs = mCurrentBucketStartTimeNs;
372 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800373 info.mBucketNum = mCurrentBucketNum;
Chenjie Yub3dda412017-10-24 13:41:59 -0700374
Chenjie Yu6736c892017-11-09 10:50:09 -0800375 int tainted = 0;
Chenjie Yub3dda412017-10-24 13:41:59 -0700376 for (const auto& slice : mCurrentSlicedBucket) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700377 long value = 0;
Chenjie Yu6736c892017-11-09 10:50:09 -0800378 if (mPullTagId != -1) {
379 for (const auto& pair : slice.second.raw) {
380 value += (pair.second - pair.first);
381 }
382 } else {
383 for (const auto& pair : slice.second.raw) {
384 value += pair.first;
385 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700386 }
Chenjie Yu6736c892017-11-09 10:50:09 -0800387 tainted += slice.second.tainted;
yro2b0f8862017-11-06 14:27:31 -0800388 info.mValue = value;
Chenjie Yu6736c892017-11-09 10:50:09 -0800389 VLOG(" %s, %ld, %d", slice.first.c_str(), value, tainted);
Yao Chen93fe3a32017-11-02 13:52:59 -0700390 // it will auto create new vector of ValuebucketInfo if the key is not found.
391 auto& bucketList = mPastBuckets[slice.first];
392 bucketList.push_back(info);
Chenjie Yub3dda412017-10-24 13:41:59 -0700393 }
394
395 // Reset counters
396 mCurrentSlicedBucket.swap(mNextSlicedBucket);
397 mNextSlicedBucket.clear();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800398
Chenjie Yub3dda412017-10-24 13:41:59 -0700399 int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800400 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
401 mCurrentBucketNum += numBucketsForward;
402
Yao Chen93fe3a32017-11-02 13:52:59 -0700403 if (numBucketsForward > 1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700404 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
405 }
Yangster-macd1815dc2017-11-13 21:43:15 -0800406 VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(),
Chenjie Yub3dda412017-10-24 13:41:59 -0700407 (long long)mCurrentBucketStartTimeNs);
408}
409
Yangster7c334a12017-11-22 14:24:24 -0800410size_t ValueMetricProducer::byteSize() const {
Yangster8de69392017-11-27 13:48:29 -0800411 std::shared_lock<std::shared_timed_mutex> readLock(mRWMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800412 size_t totalSize = 0;
413 for (const auto& pair : mPastBuckets) {
414 totalSize += pair.second.size() * kBucketSize;
415 }
416 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800417}
418
Chenjie Yub3dda412017-10-24 13:41:59 -0700419} // namespace statsd
420} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700421} // namespace android