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