blob: aa0cae26080da75ed8e5a43b142c147fde465d42 [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.
Yangster-macb142cc82018-03-30 15:22:08 -070076 void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -080077 const int64_t version) override {
78 std::lock_guard<std::mutex> lock(mMutex);
79
Chenjie Yucd1b7972019-01-16 20:38:15 -080080 if (!mSplitBucketForAppUpgrade) {
81 return;
82 }
David Chen27785a82018-01-19 17:06:45 -080083 if (eventTimeNs > getCurrentBucketEndTimeNs()) {
84 // Flush full buckets on the normal path up to the latest bucket boundary.
85 flushIfNeededLocked(eventTimeNs);
86 }
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +000087 flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
Chenjie Yue077f572018-11-02 11:51:37 -070088 if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) {
Yangster-mac32f07af2018-10-13 17:08:11 -070089 pullAndMatchEventsLocked(eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -080090 }
91 };
92
Yangster1d4d6862017-10-31 12:58:51 -070093protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080094 void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -080095 const size_t matcherIndex, const MetricDimensionKey& eventKey,
tsaichristinec876b492019-12-10 13:47:05 -080096 const ConditionKey& conditionKey, bool condition, const LogEvent& event,
97 const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
yro2b0f8862017-11-06 14:27:31 -080098
Yangster1d4d6862017-10-31 12:58:51 -070099private:
Yangster-macb142cc82018-03-30 15:22:08 -0700100 void onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700101 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700102 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000103 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700104 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800105 android::util::ProtoOutputStream* protoOutput) override;
Yangster-maca802d732018-04-24 07:50:38 -0700106 void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800107
108 // Internal interface to handle condition change.
Yangster-macb142cc82018-03-30 15:22:08 -0700109 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800110
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700111 // Internal interface to handle active state change.
112 void onActiveStateChangedLocked(const int64_t& eventTimeNs) override;
113
Yangsterf2bee6f2017-11-29 12:01:05 -0800114 // Internal interface to handle sliced condition change.
Yangster-macb142cc82018-03-30 15:22:08 -0700115 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800116
117 // Internal function to calculate the current used bytes.
118 size_t byteSizeLocked() const override;
119
Yangster-maca78d0082018-03-12 12:02:56 -0700120 void dumpStatesLocked(FILE* out, bool verbose) const override;
Yao Chen884c8c12018-01-26 10:36:25 -0800121
Yangster-macb142cc82018-03-30 15:22:08 -0700122 void dropDataLocked(const int64_t dropTimeNs) override;
Yao Chen06dba5d2018-01-26 13:38:16 -0800123
Yangsterf2bee6f2017-11-29 12:01:05 -0800124 // Util function to flush the old packet.
Yangster-macb142cc82018-03-30 15:22:08 -0700125 void flushIfNeededLocked(const int64_t& eventTime) override;
David Chen27785a82018-01-19 17:06:45 -0800126
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000127 void flushCurrentBucketLocked(const int64_t& eventTimeNs,
128 const int64_t& nextBucketStartTimeNs) override;
David Chen27785a82018-01-19 17:06:45 -0800129
Tej Singh3be093b2020-03-04 20:08:38 -0800130 void prepareFirstBucketLocked() override;
131
Yangster-mac32f07af2018-10-13 17:08:11 -0700132 void pullAndMatchEventsLocked(const int64_t timestampNs);
133
134 const int mWhatMatcherIndex;
135
136 sp<EventMatcherWizard> mEventMatcherWizard;
Yangsterf2bee6f2017-11-29 12:01:05 -0800137
Chenjie Yue2219202018-06-08 10:07:51 -0700138 sp<StatsPullerManager> mPullerManager;
Yangster1d4d6862017-10-31 12:58:51 -0700139 // tagId for pulled data. -1 if this is not pulled
140 const int mPullTagId;
141
Chenjie Yu88588972018-08-03 09:49:22 -0700142 // tagId for atoms that trigger the pulling, if any
143 const int mTriggerAtomId;
144
145 // tagId for output atom
146 const int mAtomId;
147
Chenjie Yue1361ed2018-07-23 17:33:09 -0700148 // if this is pulled metric
149 const bool mIsPulled;
150
Yangster1d4d6862017-10-31 12:58:51 -0700151 // Save the past buckets and we can clear when the StatsLogReport is dumped.
Yangster-mac93694462018-01-22 20:49:31 -0800152 std::unordered_map<MetricDimensionKey, std::vector<GaugeBucket>> mPastBuckets;
Yangster1d4d6862017-10-31 12:58:51 -0700153
David Chen27785a82018-01-19 17:06:45 -0800154 // The current partial bucket.
Yangster-mac34ea1102018-01-29 12:40:55 -0800155 std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket;
Yangster1d4d6862017-10-31 12:58:51 -0700156
David Chen27785a82018-01-19 17:06:45 -0800157 // The current full bucket for anomaly detection. This is updated to the latest value seen for
158 // this slice (ie, for partial buckets, we use the last partial bucket in this full bucket).
Yangster-mac20877162017-12-22 17:19:39 -0800159 std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800160
David Chen81245fd2018-04-12 14:33:37 -0700161 const int64_t mMinBucketSizeNs;
162
David Chen27785a82018-01-19 17:06:45 -0800163 // Translate Atom based bucket to single numeric value bucket for anomaly and updates the map
164 // for each slice with the latest value.
Chenjie Yud9dfda72017-12-11 17:41:20 -0800165 void updateCurrentSlicedBucketForAnomaly();
166
Chenjie Yud9dfda72017-12-11 17:41:20 -0800167 // Whitelist of fields to report. Empty means all are reported.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800168 std::vector<Matcher> mFieldMatchers;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800169
Yangster-mac34ea1102018-01-29 12:40:55 -0800170 GaugeMetric::SamplingType mSamplingType;
171
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800172 const int64_t mMaxPullDelayNs;
173
Chenjie Yud9dfda72017-12-11 17:41:20 -0800174 // apply a whitelist on the original input
Yao Chen8a8d16c2018-02-08 14:50:40 -0800175 std::shared_ptr<vector<FieldValue>> getGaugeFields(const LogEvent& event);
Yangster1d4d6862017-10-31 12:58:51 -0700176
Yangsterf2bee6f2017-11-29 12:01:05 -0800177 // Util function to check whether the specified dimension hits the guardrail.
Yangster-mac93694462018-01-22 20:49:31 -0800178 bool hitGuardRailLocked(const MetricDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -0800179
Yangster-mace2cd6d52017-11-09 20:38:30 -0800180 static const size_t kBucketSize = sizeof(GaugeBucket{});
yro2b0f8862017-11-06 14:27:31 -0800181
Chenjie Yuc5875052018-03-09 10:13:11 -0800182 const size_t mDimensionSoftLimit;
183
184 const size_t mDimensionHardLimit;
185
Yangster-mac50b0c9a2018-05-10 17:13:12 -0700186 const size_t mGaugeAtomsPerDimensionLimit;
187
Chenjie Yucd1b7972019-01-16 20:38:15 -0800188 const bool mSplitBucketForAppUpgrade;
189
Chenjie Yue1361ed2018-07-23 17:33:09 -0700190 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithCondition);
191 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithSlicedCondition);
192 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsNoCondition);
David Chen27785a82018-01-19 17:06:45 -0800193 FRIEND_TEST(GaugeMetricProducerTest, TestPushedEventsWithUpgrade);
194 FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithUpgrade);
Chenjie Yucd1b7972019-01-16 20:38:15 -0800195 FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithAppUpgradeDisabled);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700196 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsAnomalyDetection);
197 FRIEND_TEST(GaugeMetricProducerTest, TestFirstBucket);
Chenjie Yu88588972018-08-03 09:49:22 -0700198 FRIEND_TEST(GaugeMetricProducerTest, TestPullOnTrigger);
Chenjie Yu4c31f672018-08-21 15:42:40 -0700199 FRIEND_TEST(GaugeMetricProducerTest, TestRemoveDimensionInOutput);
Yangster1d4d6862017-10-31 12:58:51 -0700200};
201
202} // namespace statsd
203} // namespace os
204} // namespace android