blob: 2eb584b097ea57ce969baa56f8ae27acd6a7d927 [file] [log] [blame]
Yangster1d4d6862017-10-31 12:58:51 -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#pragma once
18
19#include <unordered_map>
20
yro2b0f8862017-11-06 14:27:31 -080021#include <android/util/ProtoOutputStream.h>
Yangster-mace2cd6d52017-11-09 20:38:30 -080022#include <gtest/gtest_prod.h>
Yangster1d4d6862017-10-31 12:58:51 -070023#include "../condition/ConditionTracker.h"
24#include "../external/PullDataReceiver.h"
25#include "../external/StatsPullerManager.h"
26#include "../matchers/matcher_util.h"
Yangster-mac32f07af2018-10-13 17:08:11 -070027#include "../matchers/EventMatcherWizard.h"
Yangster1d4d6862017-10-31 12:58:51 -070028#include "MetricProducer.h"
Yangster1d4d6862017-10-31 12:58:51 -070029#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Chenjie Yud9dfda72017-12-11 17:41:20 -080030#include "../stats_util.h"
Yangster1d4d6862017-10-31 12:58:51 -070031
32namespace android {
33namespace os {
34namespace statsd {
35
Yangster-mac34ea1102018-01-29 12:40:55 -080036struct GaugeAtom {
Bookatzfe2dde82018-08-28 13:24:40 -070037 GaugeAtom(std::shared_ptr<vector<FieldValue>> fields, int64_t elapsedTimeNs)
Muhammad Qureshia7de0002020-04-15 11:34:35 -070038 : mFields(fields), mElapsedTimestampNs(elapsedTimeNs) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080039 }
40 std::shared_ptr<vector<FieldValue>> mFields;
Muhammad Qureshia7de0002020-04-15 11:34:35 -070041 int64_t mElapsedTimestampNs;
Yangster-mac34ea1102018-01-29 12:40:55 -080042};
43
yro2b0f8862017-11-06 14:27:31 -080044struct GaugeBucket {
45 int64_t mBucketStartNs;
46 int64_t mBucketEndNs;
Yangster-mac34ea1102018-01-29 12:40:55 -080047 std::vector<GaugeAtom> mGaugeAtoms;
yro2b0f8862017-11-06 14:27:31 -080048};
49
Yangster-mac93694462018-01-22 20:49:31 -080050typedef std::unordered_map<MetricDimensionKey, std::vector<GaugeAtom>>
Yangster-mac34ea1102018-01-29 12:40:55 -080051 DimToGaugeAtomsMap;
Yangster-mac20877162017-12-22 17:19:39 -080052
Yangster1d4d6862017-10-31 12:58:51 -070053// This gauge metric producer first register the puller to automatically pull the gauge at the
54// beginning of each bucket. If the condition is met, insert it to the bucket info. Otherwise
55// proactively pull the gauge when the condition is changed to be true. Therefore, the gauge metric
56// producer always reports the guage at the earliest time of the bucket when the condition is met.
57class GaugeMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver {
58public:
tsaichristined21aacf2019-10-07 14:47:38 -070059 GaugeMetricProducer(
60 const ConfigKey& key, const GaugeMetric& gaugeMetric, const int conditionIndex,
61 const sp<ConditionWizard>& conditionWizard, const int whatMatcherIndex,
62 const sp<EventMatcherWizard>& matcherWizard, const int pullTagId,
63 const int triggerAtomId, const int atomId, const int64_t timeBaseNs,
64 const int64_t startTimeNs, const sp<StatsPullerManager>& pullerManager,
65 const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {},
66 const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
Tej Singh3be093b2020-03-04 20:08:38 -080067 eventDeactivationMap = {});
Yangster1d4d6862017-10-31 12:58:51 -070068
69 virtual ~GaugeMetricProducer();
70
71 // Handles when the pulled data arrives.
Olivier Gaillardc5f11c42019-02-05 12:44:58 +000072 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data,
Olivier Gaillard11203df2019-02-06 13:18:09 +000073 bool pullSuccess, int64_t originalPullTimeNs) override;
Yangster1d4d6862017-10-31 12:58:51 -070074
David Chen27785a82018-01-19 17:06:45 -080075 // GaugeMetric needs to immediately trigger another pull when we create the partial bucket.
Tej Singhe678cb72020-04-14 16:23:30 -070076 void notifyAppUpgrade(const int64_t& eventTimeNs) override {
David Chen27785a82018-01-19 17:06:45 -080077 std::lock_guard<std::mutex> lock(mMutex);
78
Chenjie Yucd1b7972019-01-16 20:38:15 -080079 if (!mSplitBucketForAppUpgrade) {
80 return;
81 }
Tej Singhe678cb72020-04-14 16:23:30 -070082 flushLocked(eventTimeNs);
83 if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) {
84 pullAndMatchEventsLocked(eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -080085 }
Tej Singhe678cb72020-04-14 16:23:30 -070086 };
87
88 // GaugeMetric needs to immediately trigger another pull when we create the partial bucket.
89 void onStatsdInitCompleted(const int64_t& eventTimeNs) override {
90 std::lock_guard<std::mutex> lock(mMutex);
91
92 flushLocked(eventTimeNs);
Chenjie Yue077f572018-11-02 11:51:37 -070093 if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) {
Yangster-mac32f07af2018-10-13 17:08:11 -070094 pullAndMatchEventsLocked(eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -080095 }
96 };
97
Yangster1d4d6862017-10-31 12:58:51 -070098protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080099 void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800100 const size_t matcherIndex, const MetricDimensionKey& eventKey,
tsaichristinec876b492019-12-10 13:47:05 -0800101 const ConditionKey& conditionKey, bool condition, const LogEvent& event,
102 const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
yro2b0f8862017-11-06 14:27:31 -0800103
Yangster1d4d6862017-10-31 12:58:51 -0700104private:
Yangster-macb142cc82018-03-30 15:22:08 -0700105 void onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700106 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700107 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000108 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700109 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800110 android::util::ProtoOutputStream* protoOutput) override;
Yangster-maca802d732018-04-24 07:50:38 -0700111 void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800112
113 // Internal interface to handle condition change.
Yangster-macb142cc82018-03-30 15:22:08 -0700114 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800115
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700116 // Internal interface to handle active state change.
117 void onActiveStateChangedLocked(const int64_t& eventTimeNs) override;
118
Yangsterf2bee6f2017-11-29 12:01:05 -0800119 // Internal interface to handle sliced condition change.
Yangster-macb142cc82018-03-30 15:22:08 -0700120 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800121
122 // Internal function to calculate the current used bytes.
123 size_t byteSizeLocked() const override;
124
Yangster-maca78d0082018-03-12 12:02:56 -0700125 void dumpStatesLocked(FILE* out, bool verbose) const override;
Yao Chen884c8c12018-01-26 10:36:25 -0800126
Yangster-macb142cc82018-03-30 15:22:08 -0700127 void dropDataLocked(const int64_t dropTimeNs) override;
Yao Chen06dba5d2018-01-26 13:38:16 -0800128
Yangsterf2bee6f2017-11-29 12:01:05 -0800129 // Util function to flush the old packet.
Yangster-macb142cc82018-03-30 15:22:08 -0700130 void flushIfNeededLocked(const int64_t& eventTime) override;
David Chen27785a82018-01-19 17:06:45 -0800131
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000132 void flushCurrentBucketLocked(const int64_t& eventTimeNs,
133 const int64_t& nextBucketStartTimeNs) override;
David Chen27785a82018-01-19 17:06:45 -0800134
Tej Singh3be093b2020-03-04 20:08:38 -0800135 void prepareFirstBucketLocked() override;
136
Yangster-mac32f07af2018-10-13 17:08:11 -0700137 void pullAndMatchEventsLocked(const int64_t timestampNs);
138
139 const int mWhatMatcherIndex;
140
141 sp<EventMatcherWizard> mEventMatcherWizard;
Yangsterf2bee6f2017-11-29 12:01:05 -0800142
Chenjie Yue2219202018-06-08 10:07:51 -0700143 sp<StatsPullerManager> mPullerManager;
Yangster1d4d6862017-10-31 12:58:51 -0700144 // tagId for pulled data. -1 if this is not pulled
145 const int mPullTagId;
146
Chenjie Yu88588972018-08-03 09:49:22 -0700147 // tagId for atoms that trigger the pulling, if any
148 const int mTriggerAtomId;
149
150 // tagId for output atom
151 const int mAtomId;
152
Chenjie Yue1361ed2018-07-23 17:33:09 -0700153 // if this is pulled metric
154 const bool mIsPulled;
155
Yangster1d4d6862017-10-31 12:58:51 -0700156 // Save the past buckets and we can clear when the StatsLogReport is dumped.
Yangster-mac93694462018-01-22 20:49:31 -0800157 std::unordered_map<MetricDimensionKey, std::vector<GaugeBucket>> mPastBuckets;
Yangster1d4d6862017-10-31 12:58:51 -0700158
David Chen27785a82018-01-19 17:06:45 -0800159 // The current partial bucket.
Yangster-mac34ea1102018-01-29 12:40:55 -0800160 std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket;
Yangster1d4d6862017-10-31 12:58:51 -0700161
David Chen27785a82018-01-19 17:06:45 -0800162 // The current full bucket for anomaly detection. This is updated to the latest value seen for
163 // this slice (ie, for partial buckets, we use the last partial bucket in this full bucket).
Yangster-mac20877162017-12-22 17:19:39 -0800164 std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800165
David Chen81245fd2018-04-12 14:33:37 -0700166 const int64_t mMinBucketSizeNs;
167
David Chen27785a82018-01-19 17:06:45 -0800168 // Translate Atom based bucket to single numeric value bucket for anomaly and updates the map
169 // for each slice with the latest value.
Chenjie Yud9dfda72017-12-11 17:41:20 -0800170 void updateCurrentSlicedBucketForAnomaly();
171
Chenjie Yud9dfda72017-12-11 17:41:20 -0800172 // Whitelist of fields to report. Empty means all are reported.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800173 std::vector<Matcher> mFieldMatchers;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800174
Yangster-mac34ea1102018-01-29 12:40:55 -0800175 GaugeMetric::SamplingType mSamplingType;
176
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800177 const int64_t mMaxPullDelayNs;
178
Chenjie Yud9dfda72017-12-11 17:41:20 -0800179 // apply a whitelist on the original input
Yao Chen8a8d16c2018-02-08 14:50:40 -0800180 std::shared_ptr<vector<FieldValue>> getGaugeFields(const LogEvent& event);
Yangster1d4d6862017-10-31 12:58:51 -0700181
Yangsterf2bee6f2017-11-29 12:01:05 -0800182 // Util function to check whether the specified dimension hits the guardrail.
Yangster-mac93694462018-01-22 20:49:31 -0800183 bool hitGuardRailLocked(const MetricDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -0800184
Yangster-mace2cd6d52017-11-09 20:38:30 -0800185 static const size_t kBucketSize = sizeof(GaugeBucket{});
yro2b0f8862017-11-06 14:27:31 -0800186
Chenjie Yuc5875052018-03-09 10:13:11 -0800187 const size_t mDimensionSoftLimit;
188
189 const size_t mDimensionHardLimit;
190
Yangster-mac50b0c9a2018-05-10 17:13:12 -0700191 const size_t mGaugeAtomsPerDimensionLimit;
192
Chenjie Yucd1b7972019-01-16 20:38:15 -0800193 const bool mSplitBucketForAppUpgrade;
194
Chenjie Yue1361ed2018-07-23 17:33:09 -0700195 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithCondition);
196 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithSlicedCondition);
197 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsNoCondition);
Chenjie Yucd1b7972019-01-16 20:38:15 -0800198 FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithAppUpgradeDisabled);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700199 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsAnomalyDetection);
200 FRIEND_TEST(GaugeMetricProducerTest, TestFirstBucket);
Chenjie Yu88588972018-08-03 09:49:22 -0700201 FRIEND_TEST(GaugeMetricProducerTest, TestPullOnTrigger);
Chenjie Yu4c31f672018-08-21 15:42:40 -0700202 FRIEND_TEST(GaugeMetricProducerTest, TestRemoveDimensionInOutput);
Tej Singhe678cb72020-04-14 16:23:30 -0700203
204 FRIEND_TEST(GaugeMetricProducerTest_PartialBucket, TestPushedEvents);
205 FRIEND_TEST(GaugeMetricProducerTest_PartialBucket, TestPulled);
Yangster1d4d6862017-10-31 12:58:51 -0700206};
207
208} // namespace statsd
209} // namespace os
210} // namespace android