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; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 44 | const int FIELD_ID_ATOM_STATS = 7; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 45 | const int FIELD_ID_UIDMAP_STATS = 8; |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 46 | const int FIELD_ID_ANOMALY_ALARM_STATS = 9; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 47 | |
| 48 | const int FIELD_ID_MATCHER_STATS_NAME = 1; |
| 49 | const int FIELD_ID_MATCHER_STATS_COUNT = 2; |
| 50 | |
| 51 | const int FIELD_ID_CONDITION_STATS_NAME = 1; |
| 52 | const int FIELD_ID_CONDITION_STATS_COUNT = 2; |
| 53 | |
| 54 | const int FIELD_ID_METRIC_STATS_NAME = 1; |
| 55 | const int FIELD_ID_METRIC_STATS_COUNT = 2; |
| 56 | |
| 57 | const int FIELD_ID_ATOM_STATS_TAG = 1; |
| 58 | const int FIELD_ID_ATOM_STATS_COUNT = 2; |
| 59 | |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 60 | const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1; |
| 61 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 62 | // TODO: add stats for pulled atoms. |
| 63 | StatsdStats::StatsdStats() { |
| 64 | mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 65 | mStartTimeSec = time(nullptr); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | StatsdStats& StatsdStats::getInstance() { |
| 69 | static StatsdStats statsInstance; |
| 70 | return statsInstance; |
| 71 | } |
| 72 | |
| 73 | void StatsdStats::noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount, |
| 74 | int matchersCount, int alertsCount, bool isValid) { |
| 75 | lock_guard<std::mutex> lock(mLock); |
| 76 | int32_t nowTimeSec = time(nullptr); |
| 77 | |
| 78 | // If there is an existing config for the same key, icebox the old config. |
| 79 | noteConfigRemovedInternalLocked(key); |
| 80 | |
| 81 | StatsdStatsReport_ConfigStats configStats; |
| 82 | configStats.set_uid(key.GetUid()); |
| 83 | configStats.set_name(key.GetName()); |
| 84 | configStats.set_creation_time_sec(nowTimeSec); |
| 85 | configStats.set_metric_count(metricsCount); |
| 86 | configStats.set_condition_count(conditionsCount); |
| 87 | configStats.set_matcher_count(matchersCount); |
| 88 | configStats.set_alert_count(alertsCount); |
| 89 | configStats.set_is_valid(isValid); |
| 90 | |
| 91 | if (isValid) { |
| 92 | mConfigStats[key] = configStats; |
| 93 | } else { |
| 94 | configStats.set_deletion_time_sec(nowTimeSec); |
| 95 | mIceBox.push_back(configStats); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) { |
| 100 | auto it = mConfigStats.find(key); |
| 101 | if (it != mConfigStats.end()) { |
| 102 | int32_t nowTimeSec = time(nullptr); |
| 103 | it->second.set_deletion_time_sec(nowTimeSec); |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 104 | // Add condition stats, metrics stats, matcher stats, alert stats |
| 105 | addSubStatsToConfigLocked(key, it->second); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 106 | // Remove them after they are added to the config stats. |
| 107 | mMatcherStats.erase(key); |
| 108 | mMetricsStats.erase(key); |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 109 | mAlertStats.erase(key); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 110 | mConditionStats.erase(key); |
| 111 | mIceBox.push_back(it->second); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 112 | mConfigStats.erase(it); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void StatsdStats::noteConfigRemoved(const ConfigKey& key) { |
| 117 | lock_guard<std::mutex> lock(mLock); |
| 118 | noteConfigRemovedInternalLocked(key); |
| 119 | } |
| 120 | |
| 121 | void StatsdStats::noteBroadcastSent(const ConfigKey& key) { |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 122 | noteBroadcastSent(key, time(nullptr)); |
| 123 | } |
| 124 | |
| 125 | void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 126 | lock_guard<std::mutex> lock(mLock); |
| 127 | auto it = mConfigStats.find(key); |
| 128 | if (it == mConfigStats.end()) { |
| 129 | ALOGE("Config key %s not found!", key.ToString().c_str()); |
| 130 | return; |
| 131 | } |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 132 | if (it->second.broadcast_sent_time_sec_size() >= kMaxTimestampCount) { |
| 133 | auto timestampList = it->second.mutable_broadcast_sent_time_sec(); |
| 134 | // This is O(N) operation. It shouldn't happen often, and N is only 20. |
| 135 | timestampList->erase(timestampList->begin()); |
| 136 | } |
| 137 | it->second.add_broadcast_sent_time_sec(timeSec); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 140 | void StatsdStats::noteDataDropped(const ConfigKey& key) { |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 141 | noteDataDropped(key, time(nullptr)); |
| 142 | } |
| 143 | |
| 144 | void StatsdStats::noteDataDropped(const ConfigKey& key, int32_t timeSec) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 145 | lock_guard<std::mutex> lock(mLock); |
| 146 | auto it = mConfigStats.find(key); |
| 147 | if (it == mConfigStats.end()) { |
| 148 | ALOGE("Config key %s not found!", key.ToString().c_str()); |
| 149 | return; |
| 150 | } |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 151 | if (it->second.data_drop_time_sec_size() >= kMaxTimestampCount) { |
| 152 | auto timestampList = it->second.mutable_data_drop_time_sec(); |
| 153 | // This is O(N) operation. It shouldn't happen often, and N is only 20. |
| 154 | timestampList->erase(timestampList->begin()); |
| 155 | } |
| 156 | it->second.add_data_drop_time_sec(timeSec); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 159 | void StatsdStats::noteMetricsReportSent(const ConfigKey& key) { |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 160 | noteMetricsReportSent(key, time(nullptr)); |
| 161 | } |
| 162 | |
| 163 | void StatsdStats::noteMetricsReportSent(const ConfigKey& key, int32_t timeSec) { |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 164 | lock_guard<std::mutex> lock(mLock); |
| 165 | auto it = mConfigStats.find(key); |
| 166 | if (it == mConfigStats.end()) { |
| 167 | ALOGE("Config key %s not found!", key.ToString().c_str()); |
| 168 | return; |
| 169 | } |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 170 | if (it->second.dump_report_time_sec_size() >= kMaxTimestampCount) { |
| 171 | auto timestampList = it->second.mutable_dump_report_time_sec(); |
| 172 | // This is O(N) operation. It shouldn't happen often, and N is only 20. |
| 173 | timestampList->erase(timestampList->begin()); |
| 174 | } |
| 175 | it->second.add_dump_report_time_sec(timeSec); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 176 | } |
| 177 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 178 | void StatsdStats::noteUidMapDropped(int snapshots, int deltas) { |
| 179 | lock_guard<std::mutex> lock(mLock); |
| 180 | mUidMapStats.set_dropped_snapshots(mUidMapStats.dropped_snapshots() + snapshots); |
| 181 | mUidMapStats.set_dropped_changes(mUidMapStats.dropped_changes() + deltas); |
| 182 | } |
| 183 | |
| 184 | void StatsdStats::setUidMapSnapshots(int snapshots) { |
| 185 | lock_guard<std::mutex> lock(mLock); |
| 186 | mUidMapStats.set_snapshots(snapshots); |
| 187 | } |
| 188 | |
| 189 | void StatsdStats::setUidMapChanges(int changes) { |
| 190 | lock_guard<std::mutex> lock(mLock); |
| 191 | mUidMapStats.set_changes(changes); |
| 192 | } |
| 193 | |
| 194 | void StatsdStats::setCurrentUidMapMemory(int bytes) { |
| 195 | lock_guard<std::mutex> lock(mLock); |
| 196 | mUidMapStats.set_bytes_used(bytes); |
| 197 | } |
| 198 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 199 | void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const string& name, int size) { |
| 200 | lock_guard<std::mutex> lock(mLock); |
| 201 | // if name doesn't exist before, it will create the key with count 0. |
| 202 | auto& conditionSizeMap = mConditionStats[key]; |
| 203 | if (size > conditionSizeMap[name]) { |
| 204 | conditionSizeMap[name] = size; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const string& name, int size) { |
| 209 | lock_guard<std::mutex> lock(mLock); |
| 210 | // if name doesn't exist before, it will create the key with count 0. |
| 211 | auto& metricsDimensionMap = mMetricsStats[key]; |
| 212 | if (size > metricsDimensionMap[name]) { |
| 213 | metricsDimensionMap[name] = size; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void StatsdStats::noteMatcherMatched(const ConfigKey& key, const string& name) { |
| 218 | lock_guard<std::mutex> lock(mLock); |
| 219 | auto& matcherStats = mMatcherStats[key]; |
| 220 | matcherStats[name]++; |
| 221 | } |
| 222 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 223 | void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const string& name) { |
| 224 | lock_guard<std::mutex> lock(mLock); |
| 225 | auto& alertStats = mAlertStats[key]; |
| 226 | alertStats[name]++; |
| 227 | } |
| 228 | |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 229 | void StatsdStats::noteRegisteredAnomalyAlarmChanged() { |
| 230 | lock_guard<std::mutex> lock(mLock); |
| 231 | mAnomalyAlarmRegisteredStats++; |
| 232 | } |
| 233 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 234 | void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) { |
| 235 | lock_guard<std::mutex> lock(mLock); |
| 236 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 237 | if (timeSec < mStartTimeSec) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | |
| 241 | if (atomId > android::util::kMaxPushedAtomId) { |
| 242 | ALOGW("not interested in atom %d", atomId); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | mPushedAtomStats[atomId]++; |
| 247 | } |
| 248 | |
| 249 | void StatsdStats::reset() { |
| 250 | lock_guard<std::mutex> lock(mLock); |
| 251 | resetInternalLocked(); |
| 252 | } |
| 253 | |
| 254 | void StatsdStats::resetInternalLocked() { |
| 255 | // Reset the historical data, but keep the active ConfigStats |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 256 | mStartTimeSec = time(nullptr); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 257 | mIceBox.clear(); |
| 258 | mConditionStats.clear(); |
| 259 | mMetricsStats.clear(); |
| 260 | std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0); |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 261 | mAlertStats.clear(); |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 262 | mAnomalyAlarmRegisteredStats = 0; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 263 | mMatcherStats.clear(); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 264 | for (auto& config : mConfigStats) { |
| 265 | config.second.clear_broadcast_sent_time_sec(); |
| 266 | config.second.clear_data_drop_time_sec(); |
| 267 | config.second.clear_dump_report_time_sec(); |
| 268 | config.second.clear_matcher_stats(); |
| 269 | config.second.clear_condition_stats(); |
| 270 | config.second.clear_metric_stats(); |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 271 | config.second.clear_alert_stats(); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 272 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 275 | void StatsdStats::addSubStatsToConfigLocked(const ConfigKey& key, |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 276 | StatsdStatsReport_ConfigStats& configStats) { |
| 277 | // Add matcher stats |
| 278 | if (mMatcherStats.find(key) != mMatcherStats.end()) { |
| 279 | const auto& matcherStats = mMatcherStats[key]; |
| 280 | for (const auto& stats : matcherStats) { |
| 281 | auto output = configStats.add_matcher_stats(); |
| 282 | output->set_name(stats.first); |
| 283 | output->set_matched_times(stats.second); |
| 284 | VLOG("matcher %s matched %d times", stats.first.c_str(), stats.second); |
| 285 | } |
| 286 | } |
| 287 | // Add condition stats |
| 288 | if (mConditionStats.find(key) != mConditionStats.end()) { |
| 289 | const auto& conditionStats = mConditionStats[key]; |
| 290 | for (const auto& stats : conditionStats) { |
| 291 | auto output = configStats.add_condition_stats(); |
| 292 | output->set_name(stats.first); |
| 293 | output->set_max_tuple_counts(stats.second); |
| 294 | VLOG("condition %s max output tuple size %d", stats.first.c_str(), stats.second); |
| 295 | } |
| 296 | } |
| 297 | // Add metrics stats |
| 298 | if (mMetricsStats.find(key) != mMetricsStats.end()) { |
| 299 | const auto& conditionStats = mMetricsStats[key]; |
| 300 | for (const auto& stats : conditionStats) { |
| 301 | auto output = configStats.add_metric_stats(); |
| 302 | output->set_name(stats.first); |
| 303 | output->set_max_tuple_counts(stats.second); |
| 304 | VLOG("metrics %s max output tuple size %d", stats.first.c_str(), stats.second); |
| 305 | } |
| 306 | } |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 307 | // Add anomaly detection alert stats |
| 308 | if (mAlertStats.find(key) != mAlertStats.end()) { |
| 309 | const auto& alertStats = mAlertStats[key]; |
| 310 | for (const auto& stats : alertStats) { |
| 311 | auto output = configStats.add_alert_stats(); |
| 312 | output->set_name(stats.first); |
| 313 | output->set_declared_times(stats.second); |
| 314 | VLOG("alert %s declared %d times", stats.first.c_str(), stats.second); |
| 315 | } |
| 316 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 319 | void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 320 | lock_guard<std::mutex> lock(mLock); |
| 321 | |
| 322 | if (DEBUG) { |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 323 | time_t t = mStartTimeSec; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 324 | struct tm* tm = localtime(&t); |
| 325 | char timeBuffer[80]; |
| 326 | strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm); |
| 327 | VLOG("=================StatsdStats dump begins===================="); |
| 328 | VLOG("Stats collection start second: %s", timeBuffer); |
| 329 | } |
| 330 | ProtoOutputStream proto; |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 331 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 332 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)time(nullptr)); |
| 333 | |
| 334 | VLOG("%lu Config in icebox: ", (unsigned long)mIceBox.size()); |
| 335 | for (const auto& configStats : mIceBox) { |
| 336 | const int numBytes = configStats.ByteSize(); |
| 337 | vector<char> buffer(numBytes); |
| 338 | configStats.SerializeToArray(&buffer[0], numBytes); |
| 339 | proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0], |
| 340 | buffer.size()); |
| 341 | |
| 342 | // surround the whole block with DEBUG, so that compiler can strip out the code |
| 343 | // in production. |
| 344 | if (DEBUG) { |
| 345 | VLOG("*****ICEBOX*****"); |
| 346 | VLOG("Config {%d-%s}: creation=%d, deletion=%d, #metric=%d, #condition=%d, " |
| 347 | "#matcher=%d, #alert=%d, #valid=%d", |
| 348 | configStats.uid(), configStats.name().c_str(), configStats.creation_time_sec(), |
| 349 | configStats.deletion_time_sec(), configStats.metric_count(), |
| 350 | configStats.condition_count(), configStats.matcher_count(), |
| 351 | configStats.alert_count(), configStats.is_valid()); |
| 352 | |
| 353 | for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) { |
| 354 | VLOG("\tbroadcast time: %d", broadcastTime); |
| 355 | } |
| 356 | |
| 357 | for (const auto& dataDropTime : configStats.data_drop_time_sec()) { |
| 358 | VLOG("\tdata drop time: %d", dataDropTime); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | for (auto& pair : mConfigStats) { |
| 364 | auto& configStats = pair.second; |
| 365 | if (DEBUG) { |
| 366 | VLOG("********Active Configs***********"); |
| 367 | VLOG("Config {%d-%s}: creation=%d, deletion=%d, #metric=%d, #condition=%d, " |
| 368 | "#matcher=%d, #alert=%d, #valid=%d", |
| 369 | configStats.uid(), configStats.name().c_str(), configStats.creation_time_sec(), |
| 370 | configStats.deletion_time_sec(), configStats.metric_count(), |
| 371 | configStats.condition_count(), configStats.matcher_count(), |
| 372 | configStats.alert_count(), configStats.is_valid()); |
| 373 | for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) { |
| 374 | VLOG("\tbroadcast time: %d", broadcastTime); |
| 375 | } |
| 376 | |
| 377 | for (const auto& dataDropTime : configStats.data_drop_time_sec()) { |
| 378 | VLOG("\tdata drop time: %d", dataDropTime); |
| 379 | } |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 380 | |
| 381 | for (const auto& dumpTime : configStats.dump_report_time_sec()) { |
| 382 | VLOG("\tdump report time: %d", dumpTime); |
| 383 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 386 | addSubStatsToConfigLocked(pair.first, configStats); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 387 | |
| 388 | const int numBytes = configStats.ByteSize(); |
| 389 | vector<char> buffer(numBytes); |
| 390 | configStats.SerializeToArray(&buffer[0], numBytes); |
| 391 | proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0], |
| 392 | buffer.size()); |
| 393 | // reset the sub stats, the source of truth is in the individual map |
| 394 | // they will be repopulated when dumpStats() is called again. |
| 395 | configStats.clear_matcher_stats(); |
| 396 | configStats.clear_condition_stats(); |
| 397 | configStats.clear_metric_stats(); |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame^] | 398 | configStats.clear_alert_stats(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | VLOG("********Atom stats***********"); |
| 402 | const size_t atomCounts = mPushedAtomStats.size(); |
| 403 | for (size_t i = 2; i < atomCounts; i++) { |
| 404 | if (mPushedAtomStats[i] > 0) { |
| 405 | long long token = |
| 406 | proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED); |
| 407 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i); |
| 408 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]); |
| 409 | proto.end(token); |
| 410 | |
| 411 | VLOG("Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]); |
| 412 | } |
| 413 | } |
| 414 | |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 415 | if (mAnomalyAlarmRegisteredStats > 0) { |
| 416 | VLOG("********AnomalyAlarmStats stats***********"); |
| 417 | long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS); |
| 418 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED, |
| 419 | mAnomalyAlarmRegisteredStats); |
| 420 | proto.end(token); |
| 421 | VLOG("Anomaly alarm registrations: %d", mAnomalyAlarmRegisteredStats); |
| 422 | } |
| 423 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 424 | const int numBytes = mUidMapStats.ByteSize(); |
| 425 | vector<char> buffer(numBytes); |
| 426 | mUidMapStats.SerializeToArray(&buffer[0], numBytes); |
| 427 | proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS, &buffer[0], buffer.size()); |
| 428 | VLOG("UID map stats: bytes=%d, snapshots=%d, changes=%d, snapshots lost=%d, changes " |
| 429 | "lost=%d", |
| 430 | mUidMapStats.bytes_used(), mUidMapStats.snapshots(), mUidMapStats.changes(), |
| 431 | mUidMapStats.dropped_snapshots(), mUidMapStats.dropped_changes()); |
| 432 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 433 | output->clear(); |
| 434 | size_t bufferSize = proto.size(); |
| 435 | output->resize(bufferSize); |
| 436 | |
| 437 | size_t pos = 0; |
| 438 | auto it = proto.data(); |
| 439 | while (it.readBuffer() != NULL) { |
| 440 | size_t toRead = it.currentToRead(); |
| 441 | std::memcpy(&((*output)[pos]), it.readBuffer(), toRead); |
| 442 | pos += toRead; |
| 443 | it.rp()->move(toRead); |
| 444 | } |
| 445 | |
| 446 | if (reset) { |
| 447 | resetInternalLocked(); |
| 448 | } |
| 449 | |
| 450 | VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize); |
| 451 | VLOG("=================StatsdStats dump ends===================="); |
| 452 | } |
| 453 | |
| 454 | } // namespace statsd |
| 455 | } // namespace os |
| 456 | } // namespace android |