1 /* 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 21 #include <android/util/ProtoOutputStream.h> 22 #include <gtest/gtest_prod.h> 23 #include "../condition/ConditionTracker.h" 24 #include "../external/PullDataReceiver.h" 25 #include "../external/StatsPullerManager.h" 26 #include "../matchers/matcher_util.h" 27 #include "../matchers/EventMatcherWizard.h" 28 #include "MetricProducer.h" 29 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" 30 #include "../stats_util.h" 31 32 namespace android { 33 namespace os { 34 namespace statsd { 35 36 struct GaugeAtom { 37 GaugeAtom(std::shared_ptr<vector<FieldValue>> fields, int64_t elapsedTimeNs) 38 : mFields(fields), mElapsedTimestamps(elapsedTimeNs) { 39 } 40 std::shared_ptr<vector<FieldValue>> mFields; 41 int64_t mElapsedTimestamps; 42 }; 43 44 struct GaugeBucket { 45 int64_t mBucketStartNs; 46 int64_t mBucketEndNs; 47 std::vector<GaugeAtom> mGaugeAtoms; 48 }; 49 50 typedef std::unordered_map<MetricDimensionKey, std::vector<GaugeAtom>> 51 DimToGaugeAtomsMap; 52 53 // 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. 57 class GaugeMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver { 58 public: 59 GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric, 60 const int conditionIndex, const sp<ConditionWizard>& conditionWizard, 61 const int whatMatcherIndex, 62 const sp<EventMatcherWizard>& matcherWizard, 63 const int pullTagId, const int triggerAtomId, const int atomId, 64 const int64_t timeBaseNs, const int64_t startTimeNs, 65 const sp<StatsPullerManager>& pullerManager); 66 67 virtual ~GaugeMetricProducer(); 68 69 // Handles when the pulled data arrives. 70 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data, 71 bool pullSuccess, int64_t originalPullTimeNs) override; 72 73 // GaugeMetric needs to immediately trigger another pull when we create the partial bucket. 74 void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid, 75 const int64_t version) override { 76 std::lock_guard<std::mutex> lock(mMutex); 77 78 if (!mSplitBucketForAppUpgrade) { 79 return; 80 } 81 if (eventTimeNs > getCurrentBucketEndTimeNs()) { 82 // Flush full buckets on the normal path up to the latest bucket boundary. 83 flushIfNeededLocked(eventTimeNs); 84 } 85 flushCurrentBucketLocked(eventTimeNs, eventTimeNs); 86 if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { 87 pullAndMatchEventsLocked(eventTimeNs); 88 } 89 }; 90 91 protected: 92 void onMatchedLogEventInternalLocked( 93 const size_t matcherIndex, const MetricDimensionKey& eventKey, 94 const ConditionKey& conditionKey, bool condition, 95 const LogEvent& event) override; 96 97 private: 98 void onDumpReportLocked(const int64_t dumpTimeNs, 99 const bool include_current_partial_bucket, 100 const bool erase_data, 101 const DumpLatency dumpLatency, 102 std::set<string> *str_set, 103 android::util::ProtoOutputStream* protoOutput) override; 104 void clearPastBucketsLocked(const int64_t dumpTimeNs) override; 105 106 // Internal interface to handle condition change. 107 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override; 108 109 // Internal interface to handle active state change. 110 void onActiveStateChangedLocked(const int64_t& eventTimeNs) override; 111 112 // Internal interface to handle sliced condition change. 113 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override; 114 115 // Internal function to calculate the current used bytes. 116 size_t byteSizeLocked() const override; 117 118 void dumpStatesLocked(FILE* out, bool verbose) const override; 119 120 void dropDataLocked(const int64_t dropTimeNs) override; 121 122 // Util function to flush the old packet. 123 void flushIfNeededLocked(const int64_t& eventTime) override; 124 125 void flushCurrentBucketLocked(const int64_t& eventTimeNs, 126 const int64_t& nextBucketStartTimeNs) override; 127 128 void prepareFirstBucketLocked() override; 129 130 void pullAndMatchEventsLocked(const int64_t timestampNs); 131 132 const int mWhatMatcherIndex; 133 134 sp<EventMatcherWizard> mEventMatcherWizard; 135 136 sp<StatsPullerManager> mPullerManager; 137 // tagId for pulled data. -1 if this is not pulled 138 const int mPullTagId; 139 140 // tagId for atoms that trigger the pulling, if any 141 const int mTriggerAtomId; 142 143 // tagId for output atom 144 const int mAtomId; 145 146 // if this is pulled metric 147 const bool mIsPulled; 148 149 // Save the past buckets and we can clear when the StatsLogReport is dumped. 150 std::unordered_map<MetricDimensionKey, std::vector<GaugeBucket>> mPastBuckets; 151 152 // The current partial bucket. 153 std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket; 154 155 // The current full bucket for anomaly detection. This is updated to the latest value seen for 156 // this slice (ie, for partial buckets, we use the last partial bucket in this full bucket). 157 std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly; 158 159 // Pairs of (elapsed start, elapsed end) denoting buckets that were skipped. 160 std::list<std::pair<int64_t, int64_t>> mSkippedBuckets; 161 162 const int64_t mMinBucketSizeNs; 163 164 // Translate Atom based bucket to single numeric value bucket for anomaly and updates the map 165 // for each slice with the latest value. 166 void updateCurrentSlicedBucketForAnomaly(); 167 168 // Whitelist of fields to report. Empty means all are reported. 169 std::vector<Matcher> mFieldMatchers; 170 171 GaugeMetric::SamplingType mSamplingType; 172 173 const int64_t mMaxPullDelayNs; 174 175 // apply a whitelist on the original input 176 std::shared_ptr<vector<FieldValue>> getGaugeFields(const LogEvent& event); 177 178 // Util function to check whether the specified dimension hits the guardrail. 179 bool hitGuardRailLocked(const MetricDimensionKey& newKey); 180 181 static const size_t kBucketSize = sizeof(GaugeBucket{}); 182 183 const size_t mDimensionSoftLimit; 184 185 const size_t mDimensionHardLimit; 186 187 const size_t mGaugeAtomsPerDimensionLimit; 188 189 const bool mSplitBucketForAppUpgrade; 190 191 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithCondition); 192 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithSlicedCondition); 193 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsNoCondition); 194 FRIEND_TEST(GaugeMetricProducerTest, TestPushedEventsWithUpgrade); 195 FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithUpgrade); 196 FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithAppUpgradeDisabled); 197 FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsAnomalyDetection); 198 FRIEND_TEST(GaugeMetricProducerTest, TestFirstBucket); 199 FRIEND_TEST(GaugeMetricProducerTest, TestPullOnTrigger); 200 FRIEND_TEST(GaugeMetricProducerTest, TestRemoveDimensionInOutput); 201 }; 202 203 } // namespace statsd 204 } // namespace os 205 } // namespace android 206