blob: 6c013477af37456bda131f086a233eca3bc61eb6 [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 {
36 std::shared_ptr<FieldValueMap> mFields;
37 int64_t mTimestamps;
38};
39
yro2b0f8862017-11-06 14:27:31 -080040struct GaugeBucket {
41 int64_t mBucketStartNs;
42 int64_t mBucketEndNs;
Yangster-mac34ea1102018-01-29 12:40:55 -080043 std::vector<GaugeAtom> mGaugeAtoms;
Yangster-mace2cd6d52017-11-09 20:38:30 -080044 uint64_t mBucketNum;
yro2b0f8862017-11-06 14:27:31 -080045};
46
Yangster-mac34ea1102018-01-29 12:40:55 -080047typedef std::unordered_map<HashableDimensionKey, std::vector<GaugeAtom>>
48 DimToGaugeAtomsMap;
Yangster-mac20877162017-12-22 17:19:39 -080049
Yangster1d4d6862017-10-31 12:58:51 -070050// This gauge metric producer first register the puller to automatically pull the gauge at the
51// beginning of each bucket. If the condition is met, insert it to the bucket info. Otherwise
52// proactively pull the gauge when the condition is changed to be true. Therefore, the gauge metric
53// producer always reports the guage at the earliest time of the bucket when the condition is met.
54class GaugeMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver {
55public:
Yangster-mac34ea1102018-01-29 12:40:55 -080056 GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric,
Yao Chenb3561512017-11-21 18:07:17 -080057 const int conditionIndex, const sp<ConditionWizard>& wizard,
Yangster-maca070b6a2018-01-04 13:28:38 -080058 const int pullTagId, const int64_t startTimeNs);
Yangster1d4d6862017-10-31 12:58:51 -070059
60 virtual ~GaugeMetricProducer();
61
62 // Handles when the pulled data arrives.
63 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override;
64
Yangster1d4d6862017-10-31 12:58:51 -070065protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080066 void onMatchedLogEventInternalLocked(
67 const size_t matcherIndex, const HashableDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -080068 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -080069 const LogEvent& event) override;
yro2b0f8862017-11-06 14:27:31 -080070
Yangster1d4d6862017-10-31 12:58:51 -070071private:
Yao Chen288c6002017-12-12 13:43:18 -080072 void onDumpReportLocked(const uint64_t dumpTimeNs,
73 android::util::ProtoOutputStream* protoOutput) override;
Yangster-mac20877162017-12-22 17:19:39 -080074 void onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080075
Chenjie Yud9dfda72017-12-11 17:41:20 -080076 // for testing
77 GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric,
78 const int conditionIndex, const sp<ConditionWizard>& wizard,
Yangster-maca070b6a2018-01-04 13:28:38 -080079 const int pullTagId, const uint64_t startTimeNs,
Chenjie Yud9dfda72017-12-11 17:41:20 -080080 std::shared_ptr<StatsPullerManager> statsPullerManager);
81
Yangsterf2bee6f2017-11-29 12:01:05 -080082 // Internal interface to handle condition change.
83 void onConditionChangedLocked(const bool conditionMet, const uint64_t eventTime) override;
84
85 // Internal interface to handle sliced condition change.
86 void onSlicedConditionMayChangeLocked(const uint64_t eventTime) override;
87
88 // Internal function to calculate the current used bytes.
89 size_t byteSizeLocked() const override;
90
Yao Chen884c8c12018-01-26 10:36:25 -080091 void dumpStatesLocked(FILE* out, bool verbose) const override{};
92
Yangsterf2bee6f2017-11-29 12:01:05 -080093 // Util function to flush the old packet.
94 void flushIfNeededLocked(const uint64_t& eventTime);
95
Chenjie Yud9dfda72017-12-11 17:41:20 -080096 std::shared_ptr<StatsPullerManager> mStatsPullerManager;
Yangster1d4d6862017-10-31 12:58:51 -070097 // tagId for pulled data. -1 if this is not pulled
98 const int mPullTagId;
99
Yangster1d4d6862017-10-31 12:58:51 -0700100 // Save the past buckets and we can clear when the StatsLogReport is dumped.
yro2b0f8862017-11-06 14:27:31 -0800101 // TODO: Add a lock to mPastBuckets.
102 std::unordered_map<HashableDimensionKey, std::vector<GaugeBucket>> mPastBuckets;
Yangster1d4d6862017-10-31 12:58:51 -0700103
104 // The current bucket.
Yangster-mac34ea1102018-01-29 12:40:55 -0800105 std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket;
Yangster1d4d6862017-10-31 12:58:51 -0700106
Chenjie Yud9dfda72017-12-11 17:41:20 -0800107 // The current bucket for anomaly detection.
Yangster-mac20877162017-12-22 17:19:39 -0800108 std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800109
110 // Translate Atom based bucket to single numeric value bucket for anomaly
111 void updateCurrentSlicedBucketForAnomaly();
112
Chenjie Yud9dfda72017-12-11 17:41:20 -0800113 // Whitelist of fields to report. Empty means all are reported.
Yangster-mac20877162017-12-22 17:19:39 -0800114 FieldFilter mFieldFilter;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800115
Yangster-mac34ea1102018-01-29 12:40:55 -0800116 GaugeMetric::SamplingType mSamplingType;
117
Chenjie Yud9dfda72017-12-11 17:41:20 -0800118 // apply a whitelist on the original input
Yangster-mac20877162017-12-22 17:19:39 -0800119 std::shared_ptr<FieldValueMap> getGaugeFields(const LogEvent& event);
Yangster1d4d6862017-10-31 12:58:51 -0700120
Yangsterf2bee6f2017-11-29 12:01:05 -0800121 // Util function to check whether the specified dimension hits the guardrail.
122 bool hitGuardRailLocked(const HashableDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -0800123
Yangster-mace2cd6d52017-11-09 20:38:30 -0800124 static const size_t kBucketSize = sizeof(GaugeBucket{});
yro2b0f8862017-11-06 14:27:31 -0800125
Yangster-mace2cd6d52017-11-09 20:38:30 -0800126 FRIEND_TEST(GaugeMetricProducerTest, TestWithCondition);
127 FRIEND_TEST(GaugeMetricProducerTest, TestNoCondition);
128 FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection);
Yangster1d4d6862017-10-31 12:58:51 -0700129};
130
131} // namespace statsd
132} // namespace os
133} // namespace android