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" |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 20 | #include "statslog.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 22 | #include <gtest/gtest_prod.h> |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 23 | #include <log/log_time.h> |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 24 | #include <list> |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | #include <mutex> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
| 33 | // Keeps track of stats of statsd. |
| 34 | // Single instance shared across the process. All methods are thread safe. |
| 35 | class StatsdStats { |
| 36 | public: |
| 37 | static StatsdStats& getInstance(); |
| 38 | ~StatsdStats(){}; |
| 39 | |
| 40 | // TODO: set different limit if the device is low ram. |
| 41 | const static int kDimensionKeySizeSoftLimit = 300; |
| 42 | const static int kDimensionKeySizeHardLimit = 500; |
| 43 | |
| 44 | const static int kMaxConfigCount = 10; |
| 45 | const static int kMaxConditionCountPerConfig = 200; |
| 46 | const static int kMaxMetricCountPerConfig = 300; |
| 47 | const static int kMaxMatcherCountPerConfig = 500; |
| 48 | |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 49 | // The max number of old config stats we keep. |
| 50 | const static int kMaxIceBoxSize = 20; |
| 51 | |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 52 | const static int kMaxTimestampCount = 20; |
| 53 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 54 | const static int kMaxLogSourceCount = 50; |
| 55 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 56 | // Max memory allowed for storing metrics per configuration. When this limit is approached, |
| 57 | // statsd will send a broadcast so that the client can fetch the data and clear this memory. |
| 58 | static const size_t kMaxMetricsBytesPerConfig = 128 * 1024; |
| 59 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 60 | // Cap the UID map's memory usage to this. This should be fairly high since the UID information |
| 61 | // is critical for understanding the metrics. |
| 62 | const static size_t kMaxBytesUsedUidMap = 50 * 1024; |
| 63 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 64 | /* Minimum period between two broadcasts in nanoseconds. */ |
| 65 | static const unsigned long long kMinBroadcastPeriodNs = 60 * NS_PER_SEC; |
| 66 | |
| 67 | /* Min period between two checks of byte size per config key in nanoseconds. */ |
| 68 | static const unsigned long long kMinByteSizeCheckPeriodNs = 10 * NS_PER_SEC; |
| 69 | |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 70 | // Default minimum interval between pulls for an atom. Pullers can return cached values if |
| 71 | // another pull request happens within this interval. |
| 72 | static std::map<int, long> kPullerCooldownMap; |
| 73 | |
| 74 | // Default cooldown time for a puller |
| 75 | static const long kDefaultPullerCooldown = 1; |
| 76 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Report a new config has been received and report the static stats about the config. |
| 79 | * |
| 80 | * The static stats include: the count of metrics, conditions, matchers, and alerts. |
| 81 | * If the config is not valid, this config stats will be put into icebox immediately. |
| 82 | */ |
| 83 | void noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount, |
| 84 | int matchersCount, int alertCount, bool isValid); |
| 85 | /** |
| 86 | * Report a config has been removed. |
| 87 | */ |
| 88 | void noteConfigRemoved(const ConfigKey& key); |
| 89 | |
| 90 | /** |
| 91 | * Report a broadcast has been sent to a config owner to collect the data. |
| 92 | */ |
| 93 | void noteBroadcastSent(const ConfigKey& key); |
| 94 | |
| 95 | /** |
| 96 | * Report a config's metrics data has been dropped. |
| 97 | */ |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 98 | void noteDataDropped(const ConfigKey& key); |
| 99 | |
| 100 | /** |
| 101 | * Report metrics data report has been sent. |
| 102 | * |
| 103 | * The report may be requested via StatsManager API, or through adb cmd. |
| 104 | */ |
| 105 | void noteMetricsReportSent(const ConfigKey& key); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Report the size of output tuple of a condition. |
| 109 | * |
| 110 | * Note: only report when the condition has an output dimension, and the tuple |
| 111 | * count > kDimensionKeySizeSoftLimit. |
| 112 | * |
| 113 | * [key]: The config key that this condition belongs to. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 114 | * [id]: The id of the condition. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 115 | * [size]: The output tuple size. |
| 116 | */ |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 117 | void noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Report the size of output tuple of a metric. |
| 121 | * |
| 122 | * Note: only report when the metric has an output dimension, and the tuple |
| 123 | * count > kDimensionKeySizeSoftLimit. |
| 124 | * |
| 125 | * [key]: The config key that this metric belongs to. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 126 | * [id]: The id of the metric. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 127 | * [size]: The output tuple size. |
| 128 | */ |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 129 | void noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Report a matcher has been matched. |
| 133 | * |
| 134 | * [key]: The config key that this matcher belongs to. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 135 | * [id]: The id of the matcher. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 136 | */ |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 137 | void noteMatcherMatched(const ConfigKey& key, const int64_t& id); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 138 | |
| 139 | /** |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 140 | * Report that an anomaly detection alert has been declared. |
| 141 | * |
| 142 | * [key]: The config key that this alert belongs to. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 143 | * [id]: The id of the alert. |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 144 | */ |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 145 | void noteAnomalyDeclared(const ConfigKey& key, const int64_t& id); |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 146 | |
| 147 | /** |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 148 | * Report an atom event has been logged. |
| 149 | */ |
| 150 | void noteAtomLogged(int atomId, int32_t timeSec); |
| 151 | |
| 152 | /** |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 153 | * Report that statsd modified the anomaly alarm registered with StatsCompanionService. |
| 154 | */ |
| 155 | void noteRegisteredAnomalyAlarmChanged(); |
| 156 | |
| 157 | /** |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 158 | * Records the number of snapshot and delta entries that are being dropped from the uid map. |
| 159 | */ |
| 160 | void noteUidMapDropped(int snapshots, int deltas); |
| 161 | |
| 162 | /** |
| 163 | * Updates the number of snapshots currently stored in the uid map. |
| 164 | */ |
| 165 | void setUidMapSnapshots(int snapshots); |
| 166 | void setUidMapChanges(int changes); |
| 167 | void setCurrentUidMapMemory(int bytes); |
| 168 | |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 169 | // Update minimum interval between pulls for an pulled atom |
| 170 | void updateMinPullIntervalSec(int pullAtomId, long intervalSec); |
| 171 | |
| 172 | // Notify pull request for an atom |
| 173 | void notePull(int pullAtomId); |
| 174 | |
| 175 | // Notify pull request for an atom served from cached data |
| 176 | void notePullFromCache(int pullAtomId); |
| 177 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 178 | /** |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 179 | * Reset the historical stats. Including all stats in icebox, and the tracked stats about |
| 180 | * metrics, matchers, and atoms. The active configs will be kept and StatsdStats will continue |
| 181 | * to collect stats after reset() has been called. |
| 182 | */ |
| 183 | void reset(); |
| 184 | |
| 185 | /** |
| 186 | * Output the stats in protobuf binary format to [buffer]. |
| 187 | * |
| 188 | * [reset]: whether to clear the historical stats after the call. |
| 189 | */ |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 190 | void dumpStats(std::vector<uint8_t>* buffer, bool reset); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 191 | |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame^] | 192 | /** |
| 193 | * Output statsd stats in human readable format to [out] file. |
| 194 | */ |
| 195 | void dumpStats(FILE* out) const; |
| 196 | |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 197 | typedef struct { |
| 198 | long totalPull; |
| 199 | long totalPullFromCache; |
| 200 | long minPullIntervalSec; |
| 201 | } PulledAtomStats; |
| 202 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 203 | private: |
| 204 | StatsdStats(); |
| 205 | |
| 206 | mutable std::mutex mLock; |
| 207 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 208 | int32_t mStartTimeSec; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 209 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 210 | // Track the number of dropped entries used by the uid map. |
| 211 | StatsdStatsReport_UidMapStats mUidMapStats; |
| 212 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 213 | // The stats about the configs that are still in use. |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 214 | // The map size is capped by kMaxConfigCount. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 215 | std::map<const ConfigKey, StatsdStatsReport_ConfigStats> mConfigStats; |
| 216 | |
| 217 | // Stores the stats for the configs that are no longer in use. |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 218 | // The size of the vector is capped by kMaxIceBoxSize. |
| 219 | std::list<const StatsdStatsReport_ConfigStats> mIceBox; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 220 | |
| 221 | // Stores the number of output tuple of condition trackers when it's bigger than |
| 222 | // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1, |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 223 | // it means some data has been dropped. The map size is capped by kMaxConfigCount. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 224 | std::map<const ConfigKey, std::map<const int64_t, int>> mConditionStats; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 225 | |
| 226 | // Stores the number of output tuple of metric producers when it's bigger than |
| 227 | // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1, |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 228 | // it means some data has been dropped. The map size is capped by kMaxConfigCount. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 229 | std::map<const ConfigKey, std::map<const int64_t, int>> mMetricsStats; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 230 | |
| 231 | // Stores the number of times a pushed atom is logged. |
| 232 | // The size of the vector is the largest pushed atom id in atoms.proto + 1. Atoms |
| 233 | // out of that range will be dropped (it's either pulled atoms or test atoms). |
| 234 | // This is a vector, not a map because it will be accessed A LOT -- for each stats log. |
| 235 | std::vector<int> mPushedAtomStats; |
| 236 | |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 237 | // Maps PullAtomId to its stats. The size is capped by the puller atom counts. |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 238 | std::map<int, PulledAtomStats> mPulledAtomStats; |
| 239 | |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 240 | // Stores the number of times statsd modified the anomaly alarm registered with |
| 241 | // StatsCompanionService. |
| 242 | int mAnomalyAlarmRegisteredStats = 0; |
| 243 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 244 | // Stores the number of times an anomaly detection alert has been declared |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 245 | // (per config, per alert name). The map size is capped by kMaxConfigCount. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 246 | std::map<const ConfigKey, std::map<const int64_t, int>> mAlertStats; |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 247 | |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 248 | // Stores how many times a matcher have been matched. The map size is capped by kMaxConfigCount. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 249 | std::map<const ConfigKey, std::map<const int64_t, int>> mMatcherStats; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 250 | |
| 251 | void noteConfigRemovedInternalLocked(const ConfigKey& key); |
| 252 | |
| 253 | void resetInternalLocked(); |
| 254 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 255 | void addSubStatsToConfigLocked(const ConfigKey& key, |
| 256 | StatsdStatsReport_ConfigStats& configStats); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 257 | |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 258 | void noteDataDropped(const ConfigKey& key, int32_t timeSec); |
| 259 | |
| 260 | void noteMetricsReportSent(const ConfigKey& key, int32_t timeSec); |
| 261 | |
| 262 | void noteBroadcastSent(const ConfigKey& key, int32_t timeSec); |
| 263 | |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 264 | void addToIceBoxLocked(const StatsdStatsReport_ConfigStats& stats); |
| 265 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 266 | FRIEND_TEST(StatsdStatsTest, TestValidConfigAdd); |
| 267 | FRIEND_TEST(StatsdStatsTest, TestInvalidConfigAdd); |
| 268 | FRIEND_TEST(StatsdStatsTest, TestConfigRemove); |
| 269 | FRIEND_TEST(StatsdStatsTest, TestSubStats); |
| 270 | FRIEND_TEST(StatsdStatsTest, TestAtomLog); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 271 | FRIEND_TEST(StatsdStatsTest, TestTimestampThreshold); |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 272 | FRIEND_TEST(StatsdStatsTest, TestAnomalyMonitor); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | } // namespace statsd |
| 276 | } // namespace os |
Stefan Lafon | c6f2fa2 | 2018-01-04 22:03:29 -0800 | [diff] [blame] | 277 | } // namespace android |