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 | #define DEBUG true // STOPSHIP if true |
| 17 | #include "Log.h" |
| 18 | |
| 19 | #include "StatsdStats.h" |
| 20 | |
| 21 | #include <android/util/ProtoOutputStream.h> |
| 22 | #include "statslog.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
| 27 | |
| 28 | using android::util::FIELD_COUNT_REPEATED; |
| 29 | using android::util::FIELD_TYPE_BOOL; |
| 30 | using android::util::FIELD_TYPE_FLOAT; |
| 31 | using android::util::FIELD_TYPE_INT32; |
| 32 | using android::util::FIELD_TYPE_INT64; |
| 33 | using android::util::FIELD_TYPE_MESSAGE; |
| 34 | using android::util::FIELD_TYPE_STRING; |
| 35 | using android::util::ProtoOutputStream; |
| 36 | using std::lock_guard; |
| 37 | using std::map; |
| 38 | using std::string; |
| 39 | using std::vector; |
| 40 | |
| 41 | const int FIELD_ID_BEGIN_TIME = 1; |
| 42 | const int FIELD_ID_END_TIME = 2; |
| 43 | const int FIELD_ID_CONFIG_STATS = 3; |
| 44 | const int FIELD_ID_MATCHER_STATS = 4; |
| 45 | const int FIELD_ID_CONDITION_STATS = 5; |
| 46 | const int FIELD_ID_METRIC_STATS = 6; |
| 47 | const int FIELD_ID_ATOM_STATS = 7; |
| 48 | |
| 49 | const int FIELD_ID_MATCHER_STATS_NAME = 1; |
| 50 | const int FIELD_ID_MATCHER_STATS_COUNT = 2; |
| 51 | |
| 52 | const int FIELD_ID_CONDITION_STATS_NAME = 1; |
| 53 | const int FIELD_ID_CONDITION_STATS_COUNT = 2; |
| 54 | |
| 55 | const int FIELD_ID_METRIC_STATS_NAME = 1; |
| 56 | const int FIELD_ID_METRIC_STATS_COUNT = 2; |
| 57 | |
| 58 | const int FIELD_ID_ATOM_STATS_TAG = 1; |
| 59 | const int FIELD_ID_ATOM_STATS_COUNT = 2; |
| 60 | |
| 61 | // TODO: add stats for pulled atoms. |
| 62 | StatsdStats::StatsdStats() { |
| 63 | mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1); |
| 64 | mStartTime = time(nullptr); |
| 65 | } |
| 66 | |
| 67 | StatsdStats& StatsdStats::getInstance() { |
| 68 | static StatsdStats statsInstance; |
| 69 | return statsInstance; |
| 70 | } |
| 71 | |
| 72 | void StatsdStats::noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount, |
| 73 | int matchersCount, int alertsCount, bool isValid) { |
| 74 | lock_guard<std::mutex> lock(mLock); |
| 75 | int32_t nowTimeSec = time(nullptr); |
| 76 | |
| 77 | // If there is an existing config for the same key, icebox the old config. |
| 78 | noteConfigRemovedInternalLocked(key); |
| 79 | |
| 80 | StatsdStatsReport_ConfigStats configStats; |
| 81 | configStats.set_uid(key.GetUid()); |
| 82 | configStats.set_name(key.GetName()); |
| 83 | configStats.set_creation_time_sec(nowTimeSec); |
| 84 | configStats.set_metric_count(metricsCount); |
| 85 | configStats.set_condition_count(conditionsCount); |
| 86 | configStats.set_matcher_count(matchersCount); |
| 87 | configStats.set_alert_count(alertsCount); |
| 88 | configStats.set_is_valid(isValid); |
| 89 | |
| 90 | if (isValid) { |
| 91 | mConfigStats[key] = configStats; |
| 92 | } else { |
| 93 | configStats.set_deletion_time_sec(nowTimeSec); |
| 94 | mIceBox.push_back(configStats); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) { |
| 99 | auto it = mConfigStats.find(key); |
| 100 | if (it != mConfigStats.end()) { |
| 101 | int32_t nowTimeSec = time(nullptr); |
| 102 | it->second.set_deletion_time_sec(nowTimeSec); |
| 103 | // Add condition stats, metrics stats, matcher stats |
| 104 | addSubStatsToConfig(key, it->second); |
| 105 | // Remove them after they are added to the config stats. |
| 106 | mMatcherStats.erase(key); |
| 107 | mMetricsStats.erase(key); |
| 108 | mConditionStats.erase(key); |
| 109 | mIceBox.push_back(it->second); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void StatsdStats::noteConfigRemoved(const ConfigKey& key) { |
| 114 | lock_guard<std::mutex> lock(mLock); |
| 115 | noteConfigRemovedInternalLocked(key); |
| 116 | } |
| 117 | |
| 118 | void StatsdStats::noteBroadcastSent(const ConfigKey& key) { |
| 119 | lock_guard<std::mutex> lock(mLock); |
| 120 | auto it = mConfigStats.find(key); |
| 121 | if (it == mConfigStats.end()) { |
| 122 | ALOGE("Config key %s not found!", key.ToString().c_str()); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | it->second.add_broadcast_sent_time_sec(time(nullptr)); |
| 127 | } |
| 128 | |
| 129 | void StatsdStats::noteDataDrop(const ConfigKey& key) { |
| 130 | lock_guard<std::mutex> lock(mLock); |
| 131 | auto it = mConfigStats.find(key); |
| 132 | if (it == mConfigStats.end()) { |
| 133 | ALOGE("Config key %s not found!", key.ToString().c_str()); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | it->second.add_data_drop_time_sec(time(nullptr)); |
| 138 | } |
| 139 | |
| 140 | void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const string& name, int size) { |
| 141 | lock_guard<std::mutex> lock(mLock); |
| 142 | // if name doesn't exist before, it will create the key with count 0. |
| 143 | auto& conditionSizeMap = mConditionStats[key]; |
| 144 | if (size > conditionSizeMap[name]) { |
| 145 | conditionSizeMap[name] = size; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const string& name, int size) { |
| 150 | lock_guard<std::mutex> lock(mLock); |
| 151 | // if name doesn't exist before, it will create the key with count 0. |
| 152 | auto& metricsDimensionMap = mMetricsStats[key]; |
| 153 | if (size > metricsDimensionMap[name]) { |
| 154 | metricsDimensionMap[name] = size; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void StatsdStats::noteMatcherMatched(const ConfigKey& key, const string& name) { |
| 159 | lock_guard<std::mutex> lock(mLock); |
| 160 | auto& matcherStats = mMatcherStats[key]; |
| 161 | matcherStats[name]++; |
| 162 | } |
| 163 | |
| 164 | void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) { |
| 165 | lock_guard<std::mutex> lock(mLock); |
| 166 | |
| 167 | if (timeSec < mStartTime) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | if (atomId > android::util::kMaxPushedAtomId) { |
| 172 | ALOGW("not interested in atom %d", atomId); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | mPushedAtomStats[atomId]++; |
| 177 | } |
| 178 | |
| 179 | void StatsdStats::reset() { |
| 180 | lock_guard<std::mutex> lock(mLock); |
| 181 | resetInternalLocked(); |
| 182 | } |
| 183 | |
| 184 | void StatsdStats::resetInternalLocked() { |
| 185 | // Reset the historical data, but keep the active ConfigStats |
| 186 | mStartTime = time(nullptr); |
| 187 | mIceBox.clear(); |
| 188 | mConditionStats.clear(); |
| 189 | mMetricsStats.clear(); |
| 190 | std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0); |
| 191 | mMatcherStats.clear(); |
| 192 | } |
| 193 | |
| 194 | void StatsdStats::addSubStatsToConfig(const ConfigKey& key, |
| 195 | StatsdStatsReport_ConfigStats& configStats) { |
| 196 | // Add matcher stats |
| 197 | if (mMatcherStats.find(key) != mMatcherStats.end()) { |
| 198 | const auto& matcherStats = mMatcherStats[key]; |
| 199 | for (const auto& stats : matcherStats) { |
| 200 | auto output = configStats.add_matcher_stats(); |
| 201 | output->set_name(stats.first); |
| 202 | output->set_matched_times(stats.second); |
| 203 | VLOG("matcher %s matched %d times", stats.first.c_str(), stats.second); |
| 204 | } |
| 205 | } |
| 206 | // Add condition stats |
| 207 | if (mConditionStats.find(key) != mConditionStats.end()) { |
| 208 | const auto& conditionStats = mConditionStats[key]; |
| 209 | for (const auto& stats : conditionStats) { |
| 210 | auto output = configStats.add_condition_stats(); |
| 211 | output->set_name(stats.first); |
| 212 | output->set_max_tuple_counts(stats.second); |
| 213 | VLOG("condition %s max output tuple size %d", stats.first.c_str(), stats.second); |
| 214 | } |
| 215 | } |
| 216 | // Add metrics stats |
| 217 | if (mMetricsStats.find(key) != mMetricsStats.end()) { |
| 218 | const auto& conditionStats = mMetricsStats[key]; |
| 219 | for (const auto& stats : conditionStats) { |
| 220 | auto output = configStats.add_metric_stats(); |
| 221 | output->set_name(stats.first); |
| 222 | output->set_max_tuple_counts(stats.second); |
| 223 | VLOG("metrics %s max output tuple size %d", stats.first.c_str(), stats.second); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void StatsdStats::dumpStats(std::vector<int8_t>* output, bool reset) { |
| 229 | lock_guard<std::mutex> lock(mLock); |
| 230 | |
| 231 | if (DEBUG) { |
| 232 | time_t t = time(nullptr); |
| 233 | struct tm* tm = localtime(&t); |
| 234 | char timeBuffer[80]; |
| 235 | strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm); |
| 236 | VLOG("=================StatsdStats dump begins===================="); |
| 237 | VLOG("Stats collection start second: %s", timeBuffer); |
| 238 | } |
| 239 | ProtoOutputStream proto; |
| 240 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTime); |
| 241 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)time(nullptr)); |
| 242 | |
| 243 | VLOG("%lu Config in icebox: ", (unsigned long)mIceBox.size()); |
| 244 | for (const auto& configStats : mIceBox) { |
| 245 | const int numBytes = configStats.ByteSize(); |
| 246 | vector<char> buffer(numBytes); |
| 247 | configStats.SerializeToArray(&buffer[0], numBytes); |
| 248 | proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0], |
| 249 | buffer.size()); |
| 250 | |
| 251 | // surround the whole block with DEBUG, so that compiler can strip out the code |
| 252 | // in production. |
| 253 | if (DEBUG) { |
| 254 | VLOG("*****ICEBOX*****"); |
| 255 | VLOG("Config {%d-%s}: creation=%d, deletion=%d, #metric=%d, #condition=%d, " |
| 256 | "#matcher=%d, #alert=%d, #valid=%d", |
| 257 | configStats.uid(), configStats.name().c_str(), configStats.creation_time_sec(), |
| 258 | configStats.deletion_time_sec(), configStats.metric_count(), |
| 259 | configStats.condition_count(), configStats.matcher_count(), |
| 260 | configStats.alert_count(), configStats.is_valid()); |
| 261 | |
| 262 | for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) { |
| 263 | VLOG("\tbroadcast time: %d", broadcastTime); |
| 264 | } |
| 265 | |
| 266 | for (const auto& dataDropTime : configStats.data_drop_time_sec()) { |
| 267 | VLOG("\tdata drop time: %d", dataDropTime); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | for (auto& pair : mConfigStats) { |
| 273 | auto& configStats = pair.second; |
| 274 | if (DEBUG) { |
| 275 | VLOG("********Active Configs***********"); |
| 276 | VLOG("Config {%d-%s}: creation=%d, deletion=%d, #metric=%d, #condition=%d, " |
| 277 | "#matcher=%d, #alert=%d, #valid=%d", |
| 278 | configStats.uid(), configStats.name().c_str(), configStats.creation_time_sec(), |
| 279 | configStats.deletion_time_sec(), configStats.metric_count(), |
| 280 | configStats.condition_count(), configStats.matcher_count(), |
| 281 | configStats.alert_count(), configStats.is_valid()); |
| 282 | for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) { |
| 283 | VLOG("\tbroadcast time: %d", broadcastTime); |
| 284 | } |
| 285 | |
| 286 | for (const auto& dataDropTime : configStats.data_drop_time_sec()) { |
| 287 | VLOG("\tdata drop time: %d", dataDropTime); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | addSubStatsToConfig(pair.first, configStats); |
| 292 | |
| 293 | const int numBytes = configStats.ByteSize(); |
| 294 | vector<char> buffer(numBytes); |
| 295 | configStats.SerializeToArray(&buffer[0], numBytes); |
| 296 | proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0], |
| 297 | buffer.size()); |
| 298 | // reset the sub stats, the source of truth is in the individual map |
| 299 | // they will be repopulated when dumpStats() is called again. |
| 300 | configStats.clear_matcher_stats(); |
| 301 | configStats.clear_condition_stats(); |
| 302 | configStats.clear_metric_stats(); |
| 303 | } |
| 304 | |
| 305 | VLOG("********Atom stats***********"); |
| 306 | const size_t atomCounts = mPushedAtomStats.size(); |
| 307 | for (size_t i = 2; i < atomCounts; i++) { |
| 308 | if (mPushedAtomStats[i] > 0) { |
| 309 | long long token = |
| 310 | proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED); |
| 311 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i); |
| 312 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]); |
| 313 | proto.end(token); |
| 314 | |
| 315 | VLOG("Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | output->clear(); |
| 320 | size_t bufferSize = proto.size(); |
| 321 | output->resize(bufferSize); |
| 322 | |
| 323 | size_t pos = 0; |
| 324 | auto it = proto.data(); |
| 325 | while (it.readBuffer() != NULL) { |
| 326 | size_t toRead = it.currentToRead(); |
| 327 | std::memcpy(&((*output)[pos]), it.readBuffer(), toRead); |
| 328 | pos += toRead; |
| 329 | it.rp()->move(toRead); |
| 330 | } |
| 331 | |
| 332 | if (reset) { |
| 333 | resetInternalLocked(); |
| 334 | } |
| 335 | |
| 336 | VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize); |
| 337 | VLOG("=================StatsdStats dump ends===================="); |
| 338 | } |
| 339 | |
| 340 | } // namespace statsd |
| 341 | } // namespace os |
| 342 | } // namespace android |