Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | #pragma once |
| 17 | |
| 18 | #include "config/ConfigKey.h" |
| 19 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
| 20 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 21 | #include <gtest/gtest_prod.h> |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 22 | #include <mutex> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace os { |
| 28 | namespace statsd { |
| 29 | |
| 30 | // Keeps track of stats of statsd. |
| 31 | // Single instance shared across the process. All methods are thread safe. |
| 32 | class StatsdStats { |
| 33 | public: |
| 34 | static StatsdStats& getInstance(); |
| 35 | ~StatsdStats(){}; |
| 36 | |
| 37 | // TODO: set different limit if the device is low ram. |
| 38 | const static int kDimensionKeySizeSoftLimit = 300; |
| 39 | const static int kDimensionKeySizeHardLimit = 500; |
| 40 | |
| 41 | const static int kMaxConfigCount = 10; |
| 42 | const static int kMaxConditionCountPerConfig = 200; |
| 43 | const static int kMaxMetricCountPerConfig = 300; |
| 44 | const static int kMaxMatcherCountPerConfig = 500; |
| 45 | |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame^] | 46 | const static int kMaxTimestampCount = 20; |
| 47 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Report a new config has been received and report the static stats about the config. |
| 50 | * |
| 51 | * The static stats include: the count of metrics, conditions, matchers, and alerts. |
| 52 | * If the config is not valid, this config stats will be put into icebox immediately. |
| 53 | */ |
| 54 | void noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount, |
| 55 | int matchersCount, int alertCount, bool isValid); |
| 56 | /** |
| 57 | * Report a config has been removed. |
| 58 | */ |
| 59 | void noteConfigRemoved(const ConfigKey& key); |
| 60 | |
| 61 | /** |
| 62 | * Report a broadcast has been sent to a config owner to collect the data. |
| 63 | */ |
| 64 | void noteBroadcastSent(const ConfigKey& key); |
| 65 | |
| 66 | /** |
| 67 | * Report a config's metrics data has been dropped. |
| 68 | */ |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 69 | void noteDataDropped(const ConfigKey& key); |
| 70 | |
| 71 | /** |
| 72 | * Report metrics data report has been sent. |
| 73 | * |
| 74 | * The report may be requested via StatsManager API, or through adb cmd. |
| 75 | */ |
| 76 | void noteMetricsReportSent(const ConfigKey& key); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Report the size of output tuple of a condition. |
| 80 | * |
| 81 | * Note: only report when the condition has an output dimension, and the tuple |
| 82 | * count > kDimensionKeySizeSoftLimit. |
| 83 | * |
| 84 | * [key]: The config key that this condition belongs to. |
| 85 | * [name]: The name of the condition. |
| 86 | * [size]: The output tuple size. |
| 87 | */ |
| 88 | void noteConditionDimensionSize(const ConfigKey& key, const std::string& name, int size); |
| 89 | |
| 90 | /** |
| 91 | * Report the size of output tuple of a metric. |
| 92 | * |
| 93 | * Note: only report when the metric has an output dimension, and the tuple |
| 94 | * count > kDimensionKeySizeSoftLimit. |
| 95 | * |
| 96 | * [key]: The config key that this metric belongs to. |
| 97 | * [name]: The name of the metric. |
| 98 | * [size]: The output tuple size. |
| 99 | */ |
| 100 | void noteMetricDimensionSize(const ConfigKey& key, const std::string& name, int size); |
| 101 | |
| 102 | /** |
| 103 | * Report a matcher has been matched. |
| 104 | * |
| 105 | * [key]: The config key that this matcher belongs to. |
| 106 | * [name]: The name of the matcher. |
| 107 | */ |
| 108 | void noteMatcherMatched(const ConfigKey& key, const std::string& name); |
| 109 | |
| 110 | /** |
| 111 | * Report an atom event has been logged. |
| 112 | */ |
| 113 | void noteAtomLogged(int atomId, int32_t timeSec); |
| 114 | |
| 115 | /** |
| 116 | * Reset the historical stats. Including all stats in icebox, and the tracked stats about |
| 117 | * metrics, matchers, and atoms. The active configs will be kept and StatsdStats will continue |
| 118 | * to collect stats after reset() has been called. |
| 119 | */ |
| 120 | void reset(); |
| 121 | |
| 122 | /** |
| 123 | * Output the stats in protobuf binary format to [buffer]. |
| 124 | * |
| 125 | * [reset]: whether to clear the historical stats after the call. |
| 126 | */ |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 127 | void dumpStats(std::vector<uint8_t>* buffer, bool reset); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | StatsdStats(); |
| 131 | |
| 132 | mutable std::mutex mLock; |
| 133 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 134 | int32_t mStartTimeSec; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 135 | |
| 136 | // The stats about the configs that are still in use. |
| 137 | std::map<const ConfigKey, StatsdStatsReport_ConfigStats> mConfigStats; |
| 138 | |
| 139 | // Stores the stats for the configs that are no longer in use. |
| 140 | std::vector<const StatsdStatsReport_ConfigStats> mIceBox; |
| 141 | |
| 142 | // Stores the number of output tuple of condition trackers when it's bigger than |
| 143 | // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1, |
| 144 | // it means some data has been dropped. |
| 145 | std::map<const ConfigKey, std::map<const std::string, int>> mConditionStats; |
| 146 | |
| 147 | // Stores the number of output tuple of metric producers when it's bigger than |
| 148 | // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1, |
| 149 | // it means some data has been dropped. |
| 150 | std::map<const ConfigKey, std::map<const std::string, int>> mMetricsStats; |
| 151 | |
| 152 | // Stores the number of times a pushed atom is logged. |
| 153 | // The size of the vector is the largest pushed atom id in atoms.proto + 1. Atoms |
| 154 | // out of that range will be dropped (it's either pulled atoms or test atoms). |
| 155 | // This is a vector, not a map because it will be accessed A LOT -- for each stats log. |
| 156 | std::vector<int> mPushedAtomStats; |
| 157 | |
| 158 | // Stores how many times a matcher have been matched. |
| 159 | std::map<const ConfigKey, std::map<const std::string, int>> mMatcherStats; |
| 160 | |
| 161 | void noteConfigRemovedInternalLocked(const ConfigKey& key); |
| 162 | |
| 163 | void resetInternalLocked(); |
| 164 | |
| 165 | void addSubStatsToConfig(const ConfigKey& key, StatsdStatsReport_ConfigStats& configStats); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 166 | |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame^] | 167 | void noteDataDropped(const ConfigKey& key, int32_t timeSec); |
| 168 | |
| 169 | void noteMetricsReportSent(const ConfigKey& key, int32_t timeSec); |
| 170 | |
| 171 | void noteBroadcastSent(const ConfigKey& key, int32_t timeSec); |
| 172 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 173 | FRIEND_TEST(StatsdStatsTest, TestValidConfigAdd); |
| 174 | FRIEND_TEST(StatsdStatsTest, TestInvalidConfigAdd); |
| 175 | FRIEND_TEST(StatsdStatsTest, TestConfigRemove); |
| 176 | FRIEND_TEST(StatsdStatsTest, TestSubStats); |
| 177 | FRIEND_TEST(StatsdStatsTest, TestAtomLog); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame^] | 178 | FRIEND_TEST(StatsdStatsTest, TestTimestampThreshold); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // namespace statsd |
| 182 | } // namespace os |
| 183 | } // namespace android |