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 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 48 | // Cap the UID map's memory usage to this. This should be fairly high since the UID information |
| 49 | // is critical for understanding the metrics. |
| 50 | const static size_t kMaxBytesUsedUidMap = 50 * 1024; |
| 51 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Report a new config has been received and report the static stats about the config. |
| 54 | * |
| 55 | * The static stats include: the count of metrics, conditions, matchers, and alerts. |
| 56 | * If the config is not valid, this config stats will be put into icebox immediately. |
| 57 | */ |
| 58 | void noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount, |
| 59 | int matchersCount, int alertCount, bool isValid); |
| 60 | /** |
| 61 | * Report a config has been removed. |
| 62 | */ |
| 63 | void noteConfigRemoved(const ConfigKey& key); |
| 64 | |
| 65 | /** |
| 66 | * Report a broadcast has been sent to a config owner to collect the data. |
| 67 | */ |
| 68 | void noteBroadcastSent(const ConfigKey& key); |
| 69 | |
| 70 | /** |
| 71 | * Report a config's metrics data has been dropped. |
| 72 | */ |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 73 | void noteDataDropped(const ConfigKey& key); |
| 74 | |
| 75 | /** |
| 76 | * Report metrics data report has been sent. |
| 77 | * |
| 78 | * The report may be requested via StatsManager API, or through adb cmd. |
| 79 | */ |
| 80 | void noteMetricsReportSent(const ConfigKey& key); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Report the size of output tuple of a condition. |
| 84 | * |
| 85 | * Note: only report when the condition has an output dimension, and the tuple |
| 86 | * count > kDimensionKeySizeSoftLimit. |
| 87 | * |
| 88 | * [key]: The config key that this condition belongs to. |
| 89 | * [name]: The name of the condition. |
| 90 | * [size]: The output tuple size. |
| 91 | */ |
| 92 | void noteConditionDimensionSize(const ConfigKey& key, const std::string& name, int size); |
| 93 | |
| 94 | /** |
| 95 | * Report the size of output tuple of a metric. |
| 96 | * |
| 97 | * Note: only report when the metric has an output dimension, and the tuple |
| 98 | * count > kDimensionKeySizeSoftLimit. |
| 99 | * |
| 100 | * [key]: The config key that this metric belongs to. |
| 101 | * [name]: The name of the metric. |
| 102 | * [size]: The output tuple size. |
| 103 | */ |
| 104 | void noteMetricDimensionSize(const ConfigKey& key, const std::string& name, int size); |
| 105 | |
| 106 | /** |
| 107 | * Report a matcher has been matched. |
| 108 | * |
| 109 | * [key]: The config key that this matcher belongs to. |
| 110 | * [name]: The name of the matcher. |
| 111 | */ |
| 112 | void noteMatcherMatched(const ConfigKey& key, const std::string& name); |
| 113 | |
| 114 | /** |
| 115 | * Report an atom event has been logged. |
| 116 | */ |
| 117 | void noteAtomLogged(int atomId, int32_t timeSec); |
| 118 | |
| 119 | /** |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 120 | * Records the number of snapshot and delta entries that are being dropped from the uid map. |
| 121 | */ |
| 122 | void noteUidMapDropped(int snapshots, int deltas); |
| 123 | |
| 124 | /** |
| 125 | * Updates the number of snapshots currently stored in the uid map. |
| 126 | */ |
| 127 | void setUidMapSnapshots(int snapshots); |
| 128 | void setUidMapChanges(int changes); |
| 129 | void setCurrentUidMapMemory(int bytes); |
| 130 | |
| 131 | /** |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 132 | * Reset the historical stats. Including all stats in icebox, and the tracked stats about |
| 133 | * metrics, matchers, and atoms. The active configs will be kept and StatsdStats will continue |
| 134 | * to collect stats after reset() has been called. |
| 135 | */ |
| 136 | void reset(); |
| 137 | |
| 138 | /** |
| 139 | * Output the stats in protobuf binary format to [buffer]. |
| 140 | * |
| 141 | * [reset]: whether to clear the historical stats after the call. |
| 142 | */ |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 143 | void dumpStats(std::vector<uint8_t>* buffer, bool reset); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | StatsdStats(); |
| 147 | |
| 148 | mutable std::mutex mLock; |
| 149 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 150 | int32_t mStartTimeSec; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 151 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 152 | // Track the number of dropped entries used by the uid map. |
| 153 | StatsdStatsReport_UidMapStats mUidMapStats; |
| 154 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 155 | // The stats about the configs that are still in use. |
| 156 | std::map<const ConfigKey, StatsdStatsReport_ConfigStats> mConfigStats; |
| 157 | |
| 158 | // Stores the stats for the configs that are no longer in use. |
| 159 | std::vector<const StatsdStatsReport_ConfigStats> mIceBox; |
| 160 | |
| 161 | // Stores the number of output tuple of condition trackers when it's bigger than |
| 162 | // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1, |
| 163 | // it means some data has been dropped. |
| 164 | std::map<const ConfigKey, std::map<const std::string, int>> mConditionStats; |
| 165 | |
| 166 | // Stores the number of output tuple of metric producers when it's bigger than |
| 167 | // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1, |
| 168 | // it means some data has been dropped. |
| 169 | std::map<const ConfigKey, std::map<const std::string, int>> mMetricsStats; |
| 170 | |
| 171 | // Stores the number of times a pushed atom is logged. |
| 172 | // The size of the vector is the largest pushed atom id in atoms.proto + 1. Atoms |
| 173 | // out of that range will be dropped (it's either pulled atoms or test atoms). |
| 174 | // This is a vector, not a map because it will be accessed A LOT -- for each stats log. |
| 175 | std::vector<int> mPushedAtomStats; |
| 176 | |
| 177 | // Stores how many times a matcher have been matched. |
| 178 | std::map<const ConfigKey, std::map<const std::string, int>> mMatcherStats; |
| 179 | |
| 180 | void noteConfigRemovedInternalLocked(const ConfigKey& key); |
| 181 | |
| 182 | void resetInternalLocked(); |
| 183 | |
| 184 | void addSubStatsToConfig(const ConfigKey& key, StatsdStatsReport_ConfigStats& configStats); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 185 | |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 186 | void noteDataDropped(const ConfigKey& key, int32_t timeSec); |
| 187 | |
| 188 | void noteMetricsReportSent(const ConfigKey& key, int32_t timeSec); |
| 189 | |
| 190 | void noteBroadcastSent(const ConfigKey& key, int32_t timeSec); |
| 191 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 192 | FRIEND_TEST(StatsdStatsTest, TestValidConfigAdd); |
| 193 | FRIEND_TEST(StatsdStatsTest, TestInvalidConfigAdd); |
| 194 | FRIEND_TEST(StatsdStatsTest, TestConfigRemove); |
| 195 | FRIEND_TEST(StatsdStatsTest, TestSubStats); |
| 196 | FRIEND_TEST(StatsdStatsTest, TestAtomLog); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 197 | FRIEND_TEST(StatsdStatsTest, TestTimestampThreshold); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | } // namespace statsd |
| 201 | } // namespace os |
| 202 | } // namespace android |