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