blob: d480941ed311b534e1b019b9822f4b0aeb08fe1f [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)
38 : mFields(fields), mElapsedTimestamps(elapsedTimeNs) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080039 }
40 std::shared_ptr<vector<FieldValue>> mFields;
Yangster-mac15f6bbc2018-04-08 11:52:26 -070041 int64_t mElapsedTimestamps;
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:
Yangster-mac34ea1102018-01-29 12:40:55 -080059 GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric,
Yangster-mac32f07af2018-10-13 17:08:11 -070060 const int conditionIndex, const sp<ConditionWizard>& conditionWizard,
61 const int whatMatcherIndex,
62 const sp<EventMatcherWizard>& matcherWizard,
Chenjie Yu88588972018-08-03 09:49:22 -070063 const int pullTagId, const int triggerAtomId, const int atomId,
64 const int64_t timeBaseNs, const int64_t startTimeNs,
Chenjie Yue2219202018-06-08 10:07:51 -070065 const sp<StatsPullerManager>& pullerManager);
Yangster1d4d6862017-10-31 12:58:51 -070066
67 virtual ~GaugeMetricProducer();
68
69 // Handles when the pulled data arrives.
Olivier Gaillardc5f11c42019-02-05 12:44:58 +000070 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data,
71 bool pullSuccess) override;
Yangster1d4d6862017-10-31 12:58:51 -070072
David Chen27785a82018-01-19 17:06:45 -080073 // GaugeMetric needs to immediately trigger another pull when we create the partial bucket.
Yangster-macb142cc82018-03-30 15:22:08 -070074 void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -080075 const int64_t version) override {
76 std::lock_guard<std::mutex> lock(mMutex);
77
Chenjie Yucd1b7972019-01-16 20:38:15 -080078 if (!mSplitBucketForAppUpgrade) {
79 return;
80 }
David Chen27785a82018-01-19 17:06:45 -080081 if (eventTimeNs > getCurrentBucketEndTimeNs()) {
82 // Flush full buckets on the normal path up to the latest bucket boundary.
83 flushIfNeededLocked(eventTimeNs);
84 }
85 flushCurrentBucketLocked(eventTimeNs);
86 mCurrentBucketStartTimeNs = eventTimeNs;
Chenjie Yue077f572018-11-02 11:51:37 -070087 if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) {
Yangster-mac32f07af2018-10-13 17:08:11 -070088 pullAndMatchEventsLocked(eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -080089 }
90 };
91
Yangster1d4d6862017-10-31 12:58:51 -070092protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080093 void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -080094 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -080095 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -080096 const LogEvent& event) override;
yro2b0f8862017-11-06 14:27:31 -080097
Yangster1d4d6862017-10-31 12:58:51 -070098private:
Yangster-macb142cc82018-03-30 15:22:08 -070099 void onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700100 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700101 const bool erase_data,
Yangster-mac9def8e32018-04-17 13:55:51 -0700102 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800103 android::util::ProtoOutputStream* protoOutput) override;
Yangster-maca802d732018-04-24 07:50:38 -0700104 void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800105
106 // Internal interface to handle condition change.
Yangster-macb142cc82018-03-30 15:22:08 -0700107 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800108
109 // Internal interface to handle sliced condition change.
Yangster-macb142cc82018-03-30 15:22:08 -0700110 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800111
112 // Internal function to calculate the current used bytes.
113 size_t byteSizeLocked() const override;
114
Yangster-maca78d0082018-03-12 12:02:56 -0700115 void dumpStatesLocked(FILE* out, bool verbose) const override;
Yao Chen884c8c12018-01-26 10:36:25 -0800116
Yangster-macb142cc82018-03-30 15:22:08 -0700117 void dropDataLocked(const int64_t dropTimeNs) override;
Yao Chen06dba5d2018-01-26 13:38:16 -0800118
Yangsterf2bee6f2017-11-29 12:01:05 -0800119 // Util function to flush the old packet.
Yangster-macb142cc82018-03-30 15:22:08 -0700120 void flushIfNeededLocked(const int64_t& eventTime) override;
David Chen27785a82018-01-19 17:06:45 -0800121
Yangster-macb142cc82018-03-30 15:22:08 -0700122 void flushCurrentBucketLocked(const int64_t& eventTimeNs) override;
David Chen27785a82018-01-19 17:06:45 -0800123
Yangster-mac32f07af2018-10-13 17:08:11 -0700124 void pullAndMatchEventsLocked(const int64_t timestampNs);
125
126 const int mWhatMatcherIndex;
127
128 sp<EventMatcherWizard> mEventMatcherWizard;
Yangsterf2bee6f2017-11-29 12:01:05 -0800129
Chenjie Yue2219202018-06-08 10:07:51 -0700130 sp<StatsPullerManager> mPullerManager;
Yangster1d4d6862017-10-31 12:58:51 -0700131 // tagId for pulled data. -1 if this is not pulled
132 const int mPullTagId;
133
Chenjie Yu88588972018-08-03 09:49:22 -0700134 // tagId for atoms that trigger the pulling, if any
135 const int mTriggerAtomId;
136
137 // tagId for output atom
138 const int mAtomId;
139
Chenjie Yue1361ed2018-07-23 17:33:09 -0700140 // if this is pulled metric
141 const bool mIsPulled;
142
Yangster1d4d6862017-10-31 12:58:51 -0700143 // Save the past buckets and we can clear when the StatsLogReport is dumped.
Yangster-mac93694462018-01-22 20:49:31 -0800144 std::unordered_map<MetricDimensionKey, std::vector<GaugeBucket>> mPastBuckets;
Yangster1d4d6862017-10-31 12:58:51 -0700145
David Chen27785a82018-01-19 17:06:45 -0800146 // The current partial bucket.
Yangster-mac34ea1102018-01-29 12:40:55 -0800147 std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket;
Yangster1d4d6862017-10-31 12:58:51 -0700148
David Chen27785a82018-01-19 17:06:45 -0800149 // The current full bucket for anomaly detection. This is updated to the latest value seen for
150 // this slice (ie, for partial buckets, we use the last partial bucket in this full bucket).
Yangster-mac20877162017-12-22 17:19:39 -0800151 std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800152
David Chen81245fd2018-04-12 14:33:37 -0700153 // Pairs of (elapsed start, elapsed end) denoting buckets that were skipped.
154 std::list<std::pair<int64_t, int64_t>> mSkippedBuckets;
155
156 const int64_t mMinBucketSizeNs;
157
David Chen27785a82018-01-19 17:06:45 -0800158 // Translate Atom based bucket to single numeric value bucket for anomaly and updates the map
159 // for each slice with the latest value.
Chenjie Yud9dfda72017-12-11 17:41:20 -0800160 void updateCurrentSlicedBucketForAnomaly();
161
Chenjie Yud9dfda72017-12-11 17:41:20 -0800162 // Whitelist of fields to report. Empty means all are reported.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800163 std::vector<Matcher> mFieldMatchers;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800164
Yangster-mac34ea1102018-01-29 12:40:55 -0800165 GaugeMetric::SamplingType mSamplingType;
166
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800167 const int64_t mMaxPullDelayNs;
168
Chenjie Yud9dfda72017-12-11 17:41:20 -0800169 // apply a whitelist on the original input
Yao Chen8a8d16c2018-02-08 14:50:40 -0800170 std::shared_ptr<vector<FieldValue>> getGaugeFields(const LogEvent& event);
Yangster1d4d6862017-10-31 12:58:51 -0700171
Yangsterf2bee6f2017-11-29 12:01:05 -0800172 // Util function to check whether the specified dimension hits the guardrail.
Yangster-mac93694462018-01-22 20:49:31 -0800173 bool hitGuardRailLocked(const MetricDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -0800174
Yangster-mace2cd6d52017-11-09 20:38:30 -0800175 static const size_t kBucketSize = sizeof(GaugeBucket{});
yro2b0f8862017-11-06 14:27:31 -0800176
Chenjie Yuc5875052018-03-09 10:13:11 -0800177 const size_t mDimensionSoftLimit;
178
179 const size_t mDimensionHardLimit;
180
Yangster-mac50b0c9a2018-05-10 17:13:12 -0700181 const size_t mGaugeAtomsPerDimensionLimit;
182
Chenjie Yucd1b7972019-01-16 20:38:15 -0800183 const bool mSplitBucketForAppUpgrade;
184
Chenjie Yue1361ed2018-07-23 17:33:09 -0700185 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithCondition);
186 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithSlicedCondition);
187 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsNoCondition);
David Chen27785a82018-01-19 17:06:45 -0800188 FRIEND_TEST(GaugeMetricProducerTest, TestPushedEventsWithUpgrade);
189 FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithUpgrade);
Chenjie Yucd1b7972019-01-16 20:38:15 -0800190 FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithAppUpgradeDisabled);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700191 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsAnomalyDetection);
192 FRIEND_TEST(GaugeMetricProducerTest, TestFirstBucket);
Chenjie Yu88588972018-08-03 09:49:22 -0700193 FRIEND_TEST(GaugeMetricProducerTest, TestPullOnTrigger);
Chenjie Yu4c31f672018-08-21 15:42:40 -0700194 FRIEND_TEST(GaugeMetricProducerTest, TestRemoveDimensionInOutput);
Yangster1d4d6862017-10-31 12:58:51 -0700195};
196
197} // namespace statsd
198} // namespace os
199} // namespace android