blob: 71d5912df6ae197ee1c00877c653379ddad55368 [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"
27#include "MetricProducer.h"
Yangster1d4d6862017-10-31 12:58:51 -070028#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Chenjie Yud9dfda72017-12-11 17:41:20 -080029#include "../stats_util.h"
Yangster1d4d6862017-10-31 12:58:51 -070030
31namespace android {
32namespace os {
33namespace statsd {
34
Yangster-mac34ea1102018-01-29 12:40:55 -080035struct GaugeAtom {
Yangster-mac15f6bbc2018-04-08 11:52:26 -070036 GaugeAtom(std::shared_ptr<vector<FieldValue>> fields, int64_t elapsedTimeNs, int wallClockNs)
37 : mFields(fields), mElapsedTimestamps(elapsedTimeNs), mWallClockTimestampNs(wallClockNs) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080038 }
39 std::shared_ptr<vector<FieldValue>> mFields;
Yangster-mac15f6bbc2018-04-08 11:52:26 -070040 int64_t mElapsedTimestamps;
41 int64_t mWallClockTimestampNs;
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,
Yao Chenb3561512017-11-21 18:07:17 -080060 const int conditionIndex, const sp<ConditionWizard>& wizard,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070061 const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs);
Yangster1d4d6862017-10-31 12:58:51 -070062
63 virtual ~GaugeMetricProducer();
64
65 // Handles when the pulled data arrives.
66 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override;
67
David Chen27785a82018-01-19 17:06:45 -080068 // GaugeMetric needs to immediately trigger another pull when we create the partial bucket.
Yangster-macb142cc82018-03-30 15:22:08 -070069 void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -080070 const int64_t version) override {
71 std::lock_guard<std::mutex> lock(mMutex);
72
73 if (eventTimeNs > getCurrentBucketEndTimeNs()) {
74 // Flush full buckets on the normal path up to the latest bucket boundary.
75 flushIfNeededLocked(eventTimeNs);
76 }
77 flushCurrentBucketLocked(eventTimeNs);
78 mCurrentBucketStartTimeNs = eventTimeNs;
79 if (mPullTagId != -1) {
Yangster-mac15f6bbc2018-04-08 11:52:26 -070080 pullLocked(eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -080081 }
82 };
83
Yangster1d4d6862017-10-31 12:58:51 -070084protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080085 void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -080086 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -080087 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -080088 const LogEvent& event) override;
yro2b0f8862017-11-06 14:27:31 -080089
Yangster1d4d6862017-10-31 12:58:51 -070090private:
Yangster-macb142cc82018-03-30 15:22:08 -070091 void onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -070092 const bool include_current_partial_bucket,
Yao Chen288c6002017-12-12 13:43:18 -080093 android::util::ProtoOutputStream* protoOutput) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080094
Chenjie Yud9dfda72017-12-11 17:41:20 -080095 // for testing
96 GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric,
97 const int conditionIndex, const sp<ConditionWizard>& wizard,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070098 const int pullTagId,
99 const int64_t timeBaseNs, const int64_t startTimeNs,
Chenjie Yud9dfda72017-12-11 17:41:20 -0800100 std::shared_ptr<StatsPullerManager> statsPullerManager);
101
Yangsterf2bee6f2017-11-29 12:01:05 -0800102 // Internal interface to handle condition change.
Yangster-macb142cc82018-03-30 15:22:08 -0700103 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800104
105 // Internal interface to handle sliced condition change.
Yangster-macb142cc82018-03-30 15:22:08 -0700106 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -0800107
108 // Internal function to calculate the current used bytes.
109 size_t byteSizeLocked() const override;
110
Yangster-maca78d0082018-03-12 12:02:56 -0700111 void dumpStatesLocked(FILE* out, bool verbose) const override;
Yao Chen884c8c12018-01-26 10:36:25 -0800112
Yangster-macb142cc82018-03-30 15:22:08 -0700113 void dropDataLocked(const int64_t dropTimeNs) override;
Yao Chen06dba5d2018-01-26 13:38:16 -0800114
Yangsterf2bee6f2017-11-29 12:01:05 -0800115 // Util function to flush the old packet.
Yangster-macb142cc82018-03-30 15:22:08 -0700116 void flushIfNeededLocked(const int64_t& eventTime) override;
David Chen27785a82018-01-19 17:06:45 -0800117
Yangster-macb142cc82018-03-30 15:22:08 -0700118 void flushCurrentBucketLocked(const int64_t& eventTimeNs) override;
David Chen27785a82018-01-19 17:06:45 -0800119
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700120 void pullLocked(const int64_t timestampNs);
Yangsterf2bee6f2017-11-29 12:01:05 -0800121
Yao Chen8a8d16c2018-02-08 14:50:40 -0800122 int mTagId;
123
Chenjie Yud9dfda72017-12-11 17:41:20 -0800124 std::shared_ptr<StatsPullerManager> mStatsPullerManager;
Yangster1d4d6862017-10-31 12:58:51 -0700125 // tagId for pulled data. -1 if this is not pulled
126 const int mPullTagId;
127
Yangster1d4d6862017-10-31 12:58:51 -0700128 // Save the past buckets and we can clear when the StatsLogReport is dumped.
yro2b0f8862017-11-06 14:27:31 -0800129 // TODO: Add a lock to mPastBuckets.
Yangster-mac93694462018-01-22 20:49:31 -0800130 std::unordered_map<MetricDimensionKey, std::vector<GaugeBucket>> mPastBuckets;
Yangster1d4d6862017-10-31 12:58:51 -0700131
David Chen27785a82018-01-19 17:06:45 -0800132 // The current partial bucket.
Yangster-mac34ea1102018-01-29 12:40:55 -0800133 std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket;
Yangster1d4d6862017-10-31 12:58:51 -0700134
David Chen27785a82018-01-19 17:06:45 -0800135 // The current full bucket for anomaly detection. This is updated to the latest value seen for
136 // this slice (ie, for partial buckets, we use the last partial bucket in this full bucket).
Yangster-mac20877162017-12-22 17:19:39 -0800137 std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800138
David Chen81245fd2018-04-12 14:33:37 -0700139 // Pairs of (elapsed start, elapsed end) denoting buckets that were skipped.
140 std::list<std::pair<int64_t, int64_t>> mSkippedBuckets;
141
142 const int64_t mMinBucketSizeNs;
143
David Chen27785a82018-01-19 17:06:45 -0800144 // Translate Atom based bucket to single numeric value bucket for anomaly and updates the map
145 // for each slice with the latest value.
Chenjie Yud9dfda72017-12-11 17:41:20 -0800146 void updateCurrentSlicedBucketForAnomaly();
147
Chenjie Yud9dfda72017-12-11 17:41:20 -0800148 // Whitelist of fields to report. Empty means all are reported.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800149 std::vector<Matcher> mFieldMatchers;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800150
Yangster-mac34ea1102018-01-29 12:40:55 -0800151 GaugeMetric::SamplingType mSamplingType;
152
Chenjie Yud9dfda72017-12-11 17:41:20 -0800153 // apply a whitelist on the original input
Yao Chen8a8d16c2018-02-08 14:50:40 -0800154 std::shared_ptr<vector<FieldValue>> getGaugeFields(const LogEvent& event);
Yangster1d4d6862017-10-31 12:58:51 -0700155
Yangsterf2bee6f2017-11-29 12:01:05 -0800156 // Util function to check whether the specified dimension hits the guardrail.
Yangster-mac93694462018-01-22 20:49:31 -0800157 bool hitGuardRailLocked(const MetricDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -0800158
Yangster-mace2cd6d52017-11-09 20:38:30 -0800159 static const size_t kBucketSize = sizeof(GaugeBucket{});
yro2b0f8862017-11-06 14:27:31 -0800160
Chenjie Yuc5875052018-03-09 10:13:11 -0800161 const size_t mDimensionSoftLimit;
162
163 const size_t mDimensionHardLimit;
164
Yangster-mace2cd6d52017-11-09 20:38:30 -0800165 FRIEND_TEST(GaugeMetricProducerTest, TestWithCondition);
Yao Chen427d3722018-03-22 15:21:52 -0700166 FRIEND_TEST(GaugeMetricProducerTest, TestWithSlicedCondition);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800167 FRIEND_TEST(GaugeMetricProducerTest, TestNoCondition);
David Chen27785a82018-01-19 17:06:45 -0800168 FRIEND_TEST(GaugeMetricProducerTest, TestPushedEventsWithUpgrade);
169 FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithUpgrade);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800170 FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection);
Yangster1d4d6862017-10-31 12:58:51 -0700171};
172
173} // namespace statsd
174} // namespace os
175} // namespace android