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 | */ |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 16 | #define DEBUG false // STOPSHIP if true |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 17 | #include "Log.h" |
| 18 | |
| 19 | #include "StatsdStats.h" |
| 20 | |
| 21 | #include <android/util/ProtoOutputStream.h> |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 22 | #include "../stats_log_util.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 23 | #include "statslog.h" |
yro | 665208d | 2018-03-13 18:08:09 -0700 | [diff] [blame] | 24 | #include "storage/StorageManager.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace os { |
| 28 | namespace statsd { |
| 29 | |
| 30 | using android::util::FIELD_COUNT_REPEATED; |
| 31 | using android::util::FIELD_TYPE_BOOL; |
| 32 | using android::util::FIELD_TYPE_FLOAT; |
| 33 | using android::util::FIELD_TYPE_INT32; |
| 34 | using android::util::FIELD_TYPE_INT64; |
| 35 | using android::util::FIELD_TYPE_MESSAGE; |
| 36 | using android::util::FIELD_TYPE_STRING; |
| 37 | using android::util::ProtoOutputStream; |
| 38 | using std::lock_guard; |
| 39 | using std::map; |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 40 | using std::shared_ptr; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 41 | using std::string; |
| 42 | using std::vector; |
| 43 | |
| 44 | const int FIELD_ID_BEGIN_TIME = 1; |
| 45 | const int FIELD_ID_END_TIME = 2; |
| 46 | const int FIELD_ID_CONFIG_STATS = 3; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 47 | const int FIELD_ID_ATOM_STATS = 7; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 48 | const int FIELD_ID_UIDMAP_STATS = 8; |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 49 | const int FIELD_ID_ANOMALY_ALARM_STATS = 9; |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 50 | // const int FIELD_ID_PULLED_ATOM_STATS = 10; // The proto is written in stats_log_util.cpp |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 51 | const int FIELD_ID_LOGGER_ERROR_STATS = 11; |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 52 | const int FIELD_ID_PERIODIC_ALARM_STATS = 12; |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 53 | const int FIELD_ID_LOG_LOSS_STATS = 14; |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame^] | 54 | const int FIELD_ID_SYSTEM_SERVER_RESTART = 15; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 55 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 56 | const int FIELD_ID_ATOM_STATS_TAG = 1; |
| 57 | const int FIELD_ID_ATOM_STATS_COUNT = 2; |
| 58 | |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 59 | const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1; |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 60 | const int FIELD_ID_PERIODIC_ALARMS_REGISTERED = 1; |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 61 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 62 | const int FIELD_ID_LOGGER_STATS_TIME = 1; |
| 63 | const int FIELD_ID_LOGGER_STATS_ERROR_CODE = 2; |
| 64 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 65 | const int FIELD_ID_CONFIG_STATS_UID = 1; |
| 66 | const int FIELD_ID_CONFIG_STATS_ID = 2; |
| 67 | const int FIELD_ID_CONFIG_STATS_CREATION = 3; |
Tej Singh | 5260eea | 2018-04-04 17:27:14 -0700 | [diff] [blame] | 68 | const int FIELD_ID_CONFIG_STATS_RESET = 19; |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 69 | const int FIELD_ID_CONFIG_STATS_DELETION = 4; |
| 70 | const int FIELD_ID_CONFIG_STATS_METRIC_COUNT = 5; |
| 71 | const int FIELD_ID_CONFIG_STATS_CONDITION_COUNT = 6; |
| 72 | const int FIELD_ID_CONFIG_STATS_MATCHER_COUNT = 7; |
| 73 | const int FIELD_ID_CONFIG_STATS_ALERT_COUNT = 8; |
| 74 | const int FIELD_ID_CONFIG_STATS_VALID = 9; |
| 75 | const int FIELD_ID_CONFIG_STATS_BROADCAST = 10; |
| 76 | const int FIELD_ID_CONFIG_STATS_DATA_DROP = 11; |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 77 | const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME = 12; |
| 78 | const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES = 20; |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 79 | const int FIELD_ID_CONFIG_STATS_MATCHER_STATS = 13; |
| 80 | const int FIELD_ID_CONFIG_STATS_CONDITION_STATS = 14; |
| 81 | const int FIELD_ID_CONFIG_STATS_METRIC_STATS = 15; |
| 82 | const int FIELD_ID_CONFIG_STATS_ALERT_STATS = 16; |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 83 | const int FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS = 17; |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 84 | const int FIELD_ID_CONFIG_STATS_ANNOTATION = 18; |
| 85 | const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT64 = 1; |
| 86 | const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT32 = 2; |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 87 | |
| 88 | const int FIELD_ID_MATCHER_STATS_ID = 1; |
| 89 | const int FIELD_ID_MATCHER_STATS_COUNT = 2; |
| 90 | const int FIELD_ID_CONDITION_STATS_ID = 1; |
| 91 | const int FIELD_ID_CONDITION_STATS_COUNT = 2; |
| 92 | const int FIELD_ID_METRIC_STATS_ID = 1; |
| 93 | const int FIELD_ID_METRIC_STATS_COUNT = 2; |
| 94 | const int FIELD_ID_ALERT_STATS_ID = 1; |
| 95 | const int FIELD_ID_ALERT_STATS_COUNT = 2; |
| 96 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 97 | const int FIELD_ID_UID_MAP_CHANGES = 1; |
| 98 | const int FIELD_ID_UID_MAP_BYTES_USED = 2; |
| 99 | const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 3; |
| 100 | const int FIELD_ID_UID_MAP_DELETED_APPS = 4; |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 101 | |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 102 | const std::map<int, std::pair<size_t, size_t>> StatsdStats::kAtomDimensionKeySizeLimitMap = { |
| 103 | {android::util::CPU_TIME_PER_UID_FREQ, {6000, 10000}}, |
| 104 | }; |
| 105 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 106 | // TODO: add stats for pulled atoms. |
| 107 | StatsdStats::StatsdStats() { |
| 108 | mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 109 | mStartTimeSec = getWallClockSec(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | StatsdStats& StatsdStats::getInstance() { |
| 113 | static StatsdStats statsInstance; |
| 114 | return statsInstance; |
| 115 | } |
| 116 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 117 | void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) { |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 118 | // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize. |
| 119 | if (mIceBox.size() == kMaxIceBoxSize) { |
| 120 | mIceBox.pop_front(); |
| 121 | } |
| 122 | mIceBox.push_back(stats); |
| 123 | } |
| 124 | |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 125 | void StatsdStats::noteConfigReceived( |
| 126 | const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount, |
| 127 | int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations, |
| 128 | bool isValid) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 129 | lock_guard<std::mutex> lock(mLock); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 130 | int32_t nowTimeSec = getWallClockSec(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 131 | |
| 132 | // If there is an existing config for the same key, icebox the old config. |
| 133 | noteConfigRemovedInternalLocked(key); |
| 134 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 135 | shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>(); |
| 136 | configStats->uid = key.GetUid(); |
| 137 | configStats->id = key.GetId(); |
| 138 | configStats->creation_time_sec = nowTimeSec; |
| 139 | configStats->metric_count = metricsCount; |
| 140 | configStats->condition_count = conditionsCount; |
| 141 | configStats->matcher_count = matchersCount; |
| 142 | configStats->alert_count = alertsCount; |
| 143 | configStats->is_valid = isValid; |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 144 | for (auto& v : annotations) { |
| 145 | configStats->annotations.emplace_back(v); |
| 146 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 147 | |
| 148 | if (isValid) { |
| 149 | mConfigStats[key] = configStats; |
| 150 | } else { |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 151 | configStats->deletion_time_sec = nowTimeSec; |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 152 | addToIceBoxLocked(configStats); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) { |
| 157 | auto it = mConfigStats.find(key); |
| 158 | if (it != mConfigStats.end()) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 159 | int32_t nowTimeSec = getWallClockSec(); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 160 | it->second->deletion_time_sec = nowTimeSec; |
Yao Chen | f6723df | 2018-01-08 15:11:58 -0800 | [diff] [blame] | 161 | addToIceBoxLocked(it->second); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 162 | mConfigStats.erase(it); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | void StatsdStats::noteConfigRemoved(const ConfigKey& key) { |
| 167 | lock_guard<std::mutex> lock(mLock); |
| 168 | noteConfigRemovedInternalLocked(key); |
| 169 | } |
| 170 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 171 | void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) { |
| 172 | auto it = mConfigStats.find(key); |
| 173 | if (it != mConfigStats.end()) { |
| 174 | it->second->reset_time_sec = getWallClockSec(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void StatsdStats::noteConfigReset(const ConfigKey& key) { |
| 179 | lock_guard<std::mutex> lock(mLock); |
| 180 | noteConfigResetInternalLocked(key); |
| 181 | } |
| 182 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 183 | void StatsdStats::noteLogLost(int64_t timestampNs) { |
| 184 | lock_guard<std::mutex> lock(mLock); |
| 185 | if (mLogLossTimestampNs.size() == kMaxLoggerErrors) { |
| 186 | mLogLossTimestampNs.pop_front(); |
| 187 | } |
| 188 | mLogLossTimestampNs.push_back(timestampNs); |
| 189 | } |
| 190 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 191 | void StatsdStats::noteBroadcastSent(const ConfigKey& key) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 192 | noteBroadcastSent(key, getWallClockSec()); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 196 | lock_guard<std::mutex> lock(mLock); |
| 197 | auto it = mConfigStats.find(key); |
| 198 | if (it == mConfigStats.end()) { |
| 199 | ALOGE("Config key %s not found!", key.ToString().c_str()); |
| 200 | return; |
| 201 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 202 | if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) { |
| 203 | it->second->broadcast_sent_time_sec.pop_front(); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 204 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 205 | it->second->broadcast_sent_time_sec.push_back(timeSec); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 208 | void StatsdStats::noteDataDropped(const ConfigKey& key) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 209 | noteDataDropped(key, getWallClockSec()); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void StatsdStats::noteDataDropped(const ConfigKey& key, int32_t timeSec) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 213 | lock_guard<std::mutex> lock(mLock); |
| 214 | auto it = mConfigStats.find(key); |
| 215 | if (it == mConfigStats.end()) { |
| 216 | ALOGE("Config key %s not found!", key.ToString().c_str()); |
| 217 | return; |
| 218 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 219 | if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) { |
| 220 | it->second->data_drop_time_sec.pop_front(); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 221 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 222 | it->second->data_drop_time_sec.push_back(timeSec); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 225 | void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) { |
| 226 | noteMetricsReportSent(key, num_bytes, getWallClockSec()); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 229 | void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes, |
| 230 | int32_t timeSec) { |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 231 | lock_guard<std::mutex> lock(mLock); |
| 232 | auto it = mConfigStats.find(key); |
| 233 | if (it == mConfigStats.end()) { |
| 234 | ALOGE("Config key %s not found!", key.ToString().c_str()); |
| 235 | return; |
| 236 | } |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 237 | if (it->second->dump_report_stats.size() == kMaxTimestampCount) { |
| 238 | it->second->dump_report_stats.pop_front(); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 239 | } |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 240 | it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes)); |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 241 | } |
| 242 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 243 | void StatsdStats::noteUidMapDropped(int deltas) { |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 244 | lock_guard<std::mutex> lock(mLock); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 245 | mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 246 | } |
| 247 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 248 | void StatsdStats::noteUidMapAppDeletionDropped() { |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 249 | lock_guard<std::mutex> lock(mLock); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 250 | mUidMapStats.deleted_apps++; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void StatsdStats::setUidMapChanges(int changes) { |
| 254 | lock_guard<std::mutex> lock(mLock); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 255 | mUidMapStats.changes = changes; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void StatsdStats::setCurrentUidMapMemory(int bytes) { |
| 259 | lock_guard<std::mutex> lock(mLock); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 260 | mUidMapStats.bytes_used = bytes; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 263 | void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 264 | lock_guard<std::mutex> lock(mLock); |
| 265 | // if name doesn't exist before, it will create the key with count 0. |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 266 | auto statsIt = mConfigStats.find(key); |
| 267 | if (statsIt == mConfigStats.end()) { |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | auto& conditionSizeMap = statsIt->second->condition_stats; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 272 | if (size > conditionSizeMap[id]) { |
| 273 | conditionSizeMap[id] = size; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 277 | void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 278 | lock_guard<std::mutex> lock(mLock); |
| 279 | // if name doesn't exist before, it will create the key with count 0. |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 280 | auto statsIt = mConfigStats.find(key); |
| 281 | if (statsIt == mConfigStats.end()) { |
| 282 | return; |
| 283 | } |
| 284 | auto& metricsDimensionMap = statsIt->second->metric_stats; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 285 | if (size > metricsDimensionMap[id]) { |
| 286 | metricsDimensionMap[id] = size; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 290 | void StatsdStats::noteMetricDimensionInConditionSize( |
| 291 | const ConfigKey& key, const int64_t& id, int size) { |
| 292 | lock_guard<std::mutex> lock(mLock); |
| 293 | // if name doesn't exist before, it will create the key with count 0. |
| 294 | auto statsIt = mConfigStats.find(key); |
| 295 | if (statsIt == mConfigStats.end()) { |
| 296 | return; |
| 297 | } |
| 298 | auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats; |
| 299 | if (size > metricsDimensionMap[id]) { |
| 300 | metricsDimensionMap[id] = size; |
| 301 | } |
| 302 | } |
| 303 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 304 | void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 305 | lock_guard<std::mutex> lock(mLock); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 306 | |
| 307 | auto statsIt = mConfigStats.find(key); |
| 308 | if (statsIt == mConfigStats.end()) { |
| 309 | return; |
| 310 | } |
| 311 | statsIt->second->matcher_stats[id]++; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 314 | void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) { |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 315 | lock_guard<std::mutex> lock(mLock); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 316 | auto statsIt = mConfigStats.find(key); |
| 317 | if (statsIt == mConfigStats.end()) { |
| 318 | return; |
| 319 | } |
| 320 | statsIt->second->alert_stats[id]++; |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 323 | void StatsdStats::noteRegisteredAnomalyAlarmChanged() { |
| 324 | lock_guard<std::mutex> lock(mLock); |
| 325 | mAnomalyAlarmRegisteredStats++; |
| 326 | } |
| 327 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 328 | void StatsdStats::noteRegisteredPeriodicAlarmChanged() { |
| 329 | lock_guard<std::mutex> lock(mLock); |
| 330 | mPeriodicAlarmRegisteredStats++; |
| 331 | } |
| 332 | |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 333 | void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) { |
| 334 | lock_guard<std::mutex> lock(mLock); |
| 335 | mPulledAtomStats[pullAtomId].minPullIntervalSec = intervalSec; |
| 336 | } |
| 337 | |
| 338 | void StatsdStats::notePull(int pullAtomId) { |
| 339 | lock_guard<std::mutex> lock(mLock); |
| 340 | mPulledAtomStats[pullAtomId].totalPull++; |
| 341 | } |
| 342 | |
| 343 | void StatsdStats::notePullFromCache(int pullAtomId) { |
| 344 | lock_guard<std::mutex> lock(mLock); |
| 345 | mPulledAtomStats[pullAtomId].totalPullFromCache++; |
| 346 | } |
| 347 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 348 | void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) { |
| 349 | lock_guard<std::mutex> lock(mLock); |
| 350 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 351 | if (atomId > android::util::kMaxPushedAtomId) { |
| 352 | ALOGW("not interested in atom %d", atomId); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | mPushedAtomStats[atomId]++; |
| 357 | } |
| 358 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame^] | 359 | void StatsdStats::noteSystemServerRestart(int32_t timeSec) { |
| 360 | lock_guard<std::mutex> lock(mLock); |
| 361 | |
| 362 | if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) { |
| 363 | mSystemServerRestartSec.pop_front(); |
| 364 | } |
| 365 | mSystemServerRestartSec.push_back(timeSec); |
| 366 | } |
| 367 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 368 | void StatsdStats::noteLoggerError(int error) { |
| 369 | lock_guard<std::mutex> lock(mLock); |
| 370 | // grows strictly one at a time. so it won't > kMaxLoggerErrors |
| 371 | if (mLoggerErrors.size() == kMaxLoggerErrors) { |
| 372 | mLoggerErrors.pop_front(); |
| 373 | } |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 374 | mLoggerErrors.push_back(std::make_pair(getWallClockSec(), error)); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 377 | void StatsdStats::reset() { |
| 378 | lock_guard<std::mutex> lock(mLock); |
| 379 | resetInternalLocked(); |
| 380 | } |
| 381 | |
| 382 | void StatsdStats::resetInternalLocked() { |
| 383 | // Reset the historical data, but keep the active ConfigStats |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 384 | mStartTimeSec = getWallClockSec(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 385 | mIceBox.clear(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 386 | std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0); |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 387 | mAnomalyAlarmRegisteredStats = 0; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 388 | mPeriodicAlarmRegisteredStats = 0; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 389 | mLoggerErrors.clear(); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame^] | 390 | mSystemServerRestartSec.clear(); |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 391 | mLogLossTimestampNs.clear(); |
Yao Chen | 0fac5b1 | 2017-11-28 16:07:02 -0800 | [diff] [blame] | 392 | for (auto& config : mConfigStats) { |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 393 | config.second->broadcast_sent_time_sec.clear(); |
| 394 | config.second->data_drop_time_sec.clear(); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 395 | config.second->dump_report_stats.clear(); |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 396 | config.second->annotations.clear(); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 397 | config.second->matcher_stats.clear(); |
| 398 | config.second->condition_stats.clear(); |
| 399 | config.second->metric_stats.clear(); |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 400 | config.second->metric_dimension_in_condition_stats.clear(); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 401 | config.second->alert_stats.clear(); |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 402 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 405 | string buildTimeString(int64_t timeSec) { |
| 406 | time_t t = timeSec; |
| 407 | struct tm* tm = localtime(&t); |
| 408 | char timeBuffer[80]; |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame^] | 409 | strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 410 | return string(timeBuffer); |
| 411 | } |
| 412 | |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 413 | void StatsdStats::dumpStats(FILE* out) const { |
| 414 | lock_guard<std::mutex> lock(mLock); |
| 415 | time_t t = mStartTimeSec; |
| 416 | struct tm* tm = localtime(&t); |
| 417 | char timeBuffer[80]; |
| 418 | strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm); |
| 419 | fprintf(out, "Stats collection start second: %s\n", timeBuffer); |
| 420 | fprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size()); |
| 421 | for (const auto& configStats : mIceBox) { |
| 422 | fprintf(out, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 423 | "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, " |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 424 | "#matcher=%d, #alert=%d, valid=%d\n", |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 425 | configStats->uid, (long long)configStats->id, configStats->creation_time_sec, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 426 | configStats->deletion_time_sec, configStats->reset_time_sec, |
| 427 | configStats->metric_count, |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 428 | configStats->condition_count, configStats->matcher_count, configStats->alert_count, |
| 429 | configStats->is_valid); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 430 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 431 | for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) { |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 432 | fprintf(out, "\tbroadcast time: %d\n", broadcastTime); |
| 433 | } |
| 434 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 435 | for (const auto& dataDropTime : configStats->data_drop_time_sec) { |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 436 | fprintf(out, "\tdata drop time: %d\n", dataDropTime); |
| 437 | } |
| 438 | } |
| 439 | fprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size()); |
| 440 | for (auto& pair : mConfigStats) { |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 441 | auto& configStats = pair.second; |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 442 | fprintf(out, |
| 443 | "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, " |
| 444 | "#matcher=%d, #alert=%d, valid=%d\n", |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 445 | configStats->uid, (long long)configStats->id, configStats->creation_time_sec, |
| 446 | configStats->deletion_time_sec, configStats->metric_count, |
| 447 | configStats->condition_count, configStats->matcher_count, configStats->alert_count, |
| 448 | configStats->is_valid); |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 449 | for (const auto& annotation : configStats->annotations) { |
| 450 | fprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first, |
| 451 | annotation.second); |
| 452 | } |
| 453 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 454 | for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) { |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 455 | fprintf(out, "\tbroadcast time: %s(%lld)\n", |
| 456 | buildTimeString(broadcastTime).c_str(), (long long)broadcastTime); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 459 | for (const auto& dataDropTime : configStats->data_drop_time_sec) { |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 460 | fprintf(out, "\tdata drop time: %s(%lld)\n", |
| 461 | buildTimeString(dataDropTime).c_str(), (long long)dataDropTime); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 464 | for (const auto& dump : configStats->dump_report_stats) { |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 465 | fprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n", |
| 466 | buildTimeString(dump.first).c_str(), (long long)dump.first, |
| 467 | (long long)dump.second); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 470 | for (const auto& stats : pair.second->matcher_stats) { |
| 471 | fprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 472 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 473 | |
| 474 | for (const auto& stats : pair.second->condition_stats) { |
| 475 | fprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first, |
| 476 | stats.second); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 477 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 478 | |
| 479 | for (const auto& stats : pair.second->condition_stats) { |
| 480 | fprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first, |
| 481 | stats.second); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 482 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 483 | |
| 484 | for (const auto& stats : pair.second->alert_stats) { |
| 485 | fprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
yro | 665208d | 2018-03-13 18:08:09 -0700 | [diff] [blame] | 488 | fprintf(out, "********Disk Usage stats***********\n"); |
| 489 | StorageManager::printStats(out); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 490 | fprintf(out, "********Pushed Atom stats***********\n"); |
| 491 | const size_t atomCounts = mPushedAtomStats.size(); |
| 492 | for (size_t i = 2; i < atomCounts; i++) { |
| 493 | if (mPushedAtomStats[i] > 0) { |
| 494 | fprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | fprintf(out, "********Pulled Atom stats***********\n"); |
| 499 | for (const auto& pair : mPulledAtomStats) { |
| 500 | fprintf(out, "Atom %d->%ld, %ld, %ld\n", (int)pair.first, (long)pair.second.totalPull, |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 501 | (long)pair.second.totalPullFromCache, (long)pair.second.minPullIntervalSec); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if (mAnomalyAlarmRegisteredStats > 0) { |
| 505 | fprintf(out, "********AnomalyAlarmStats stats***********\n"); |
| 506 | fprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats); |
| 507 | } |
| 508 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 509 | if (mPeriodicAlarmRegisteredStats > 0) { |
| 510 | fprintf(out, "********SubscriberAlarmStats stats***********\n"); |
| 511 | fprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats); |
| 512 | } |
| 513 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 514 | fprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n", |
| 515 | mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps, |
| 516 | mUidMapStats.dropped_changes); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 517 | |
| 518 | for (const auto& error : mLoggerErrors) { |
| 519 | time_t error_time = error.first; |
| 520 | struct tm* error_tm = localtime(&error_time); |
| 521 | char buffer[80]; |
| 522 | strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M%p\n", error_tm); |
| 523 | fprintf(out, "Logger error %d at %s\n", error.second, buffer); |
| 524 | } |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame^] | 525 | |
| 526 | for (const auto& restart : mSystemServerRestartSec) { |
| 527 | fprintf(out, "System server restarts at %s(%lld)\n", |
| 528 | buildTimeString(restart).c_str(), (long long)restart); |
| 529 | } |
| 530 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 531 | for (const auto& loss : mLogLossTimestampNs) { |
| 532 | fprintf(out, "Log loss detected at %lld (elapsedRealtimeNs)\n", (long long)loss); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 533 | } |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 536 | void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 537 | uint64_t token = |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 538 | proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS); |
| 539 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid); |
| 540 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id); |
| 541 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 542 | if (configStats.reset_time_sec != 0) { |
| 543 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec); |
| 544 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 545 | if (configStats.deletion_time_sec != 0) { |
| 546 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION, |
| 547 | configStats.deletion_time_sec); |
| 548 | } |
| 549 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count); |
| 550 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT, |
| 551 | configStats.condition_count); |
| 552 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count); |
| 553 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count); |
| 554 | proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid); |
| 555 | |
| 556 | for (const auto& broadcast : configStats.broadcast_sent_time_sec) { |
| 557 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED, |
| 558 | broadcast); |
| 559 | } |
| 560 | |
| 561 | for (const auto& drop : configStats.data_drop_time_sec) { |
| 562 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP | FIELD_COUNT_REPEATED, |
| 563 | drop); |
| 564 | } |
| 565 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 566 | for (const auto& dump : configStats.dump_report_stats) { |
| 567 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME | |
| 568 | FIELD_COUNT_REPEATED, |
| 569 | dump.first); |
| 570 | } |
| 571 | |
| 572 | for (const auto& dump : configStats.dump_report_stats) { |
| 573 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES | |
| 574 | FIELD_COUNT_REPEATED, |
| 575 | (long long)dump.second); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 576 | } |
| 577 | |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 578 | for (const auto& annotation : configStats.annotations) { |
| 579 | uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 580 | FIELD_ID_CONFIG_STATS_ANNOTATION); |
| 581 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64, |
| 582 | (long long)annotation.first); |
| 583 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second); |
| 584 | proto->end(token); |
| 585 | } |
| 586 | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 587 | for (const auto& pair : configStats.matcher_stats) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 588 | uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 589 | FIELD_ID_CONFIG_STATS_MATCHER_STATS); |
| 590 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first); |
| 591 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second); |
| 592 | proto->end(tmpToken); |
| 593 | } |
| 594 | |
| 595 | for (const auto& pair : configStats.condition_stats) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 596 | uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 597 | FIELD_ID_CONFIG_STATS_CONDITION_STATS); |
| 598 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first); |
| 599 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second); |
| 600 | proto->end(tmpToken); |
| 601 | } |
| 602 | |
| 603 | for (const auto& pair : configStats.metric_stats) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 604 | uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 605 | FIELD_ID_CONFIG_STATS_METRIC_STATS); |
| 606 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first); |
| 607 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second); |
| 608 | proto->end(tmpToken); |
| 609 | } |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 610 | for (const auto& pair : configStats.metric_dimension_in_condition_stats) { |
| 611 | uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 612 | FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS); |
| 613 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first); |
| 614 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second); |
| 615 | proto->end(tmpToken); |
| 616 | } |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 617 | |
| 618 | for (const auto& pair : configStats.alert_stats) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 619 | uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 620 | FIELD_ID_CONFIG_STATS_ALERT_STATS); |
| 621 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first); |
| 622 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second); |
| 623 | proto->end(tmpToken); |
| 624 | } |
| 625 | |
| 626 | proto->end(token); |
| 627 | } |
| 628 | |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 629 | void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 630 | lock_guard<std::mutex> lock(mLock); |
| 631 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 632 | ProtoOutputStream proto; |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 633 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 634 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 635 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 636 | for (const auto& configStats : mIceBox) { |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 637 | addConfigStatsToProto(*configStats, &proto); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | for (auto& pair : mConfigStats) { |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 641 | addConfigStatsToProto(*(pair.second), &proto); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 644 | const size_t atomCounts = mPushedAtomStats.size(); |
| 645 | for (size_t i = 2; i < atomCounts; i++) { |
| 646 | if (mPushedAtomStats[i] > 0) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 647 | uint64_t token = |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 648 | proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED); |
| 649 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i); |
| 650 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]); |
| 651 | proto.end(token); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 655 | for (const auto& pair : mPulledAtomStats) { |
| 656 | android::os::statsd::writePullerStatsToStream(pair, &proto); |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 659 | if (mAnomalyAlarmRegisteredStats > 0) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 660 | uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS); |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 661 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED, |
| 662 | mAnomalyAlarmRegisteredStats); |
| 663 | proto.end(token); |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 666 | if (mPeriodicAlarmRegisteredStats > 0) { |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 667 | uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS); |
| 668 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 669 | mPeriodicAlarmRegisteredStats); |
| 670 | proto.end(token); |
| 671 | } |
| 672 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 673 | uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 674 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes); |
| 675 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 676 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 677 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps); |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame] | 678 | proto.end(uidMapToken); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 679 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 680 | for (const auto& error : mLoggerErrors) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 681 | uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 682 | FIELD_COUNT_REPEATED); |
| 683 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_TIME, error.first); |
| 684 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_ERROR_CODE, error.second); |
| 685 | proto.end(token); |
| 686 | } |
| 687 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 688 | for (const auto& loss : mLogLossTimestampNs) { |
| 689 | proto.write(FIELD_TYPE_INT64 | FIELD_ID_LOG_LOSS_STATS | FIELD_COUNT_REPEATED, |
| 690 | (long long)loss); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame^] | 693 | for (const auto& restart : mSystemServerRestartSec) { |
| 694 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED, |
| 695 | restart); |
| 696 | } |
| 697 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 698 | output->clear(); |
| 699 | size_t bufferSize = proto.size(); |
| 700 | output->resize(bufferSize); |
| 701 | |
| 702 | size_t pos = 0; |
| 703 | auto it = proto.data(); |
| 704 | while (it.readBuffer() != NULL) { |
| 705 | size_t toRead = it.currentToRead(); |
| 706 | std::memcpy(&((*output)[pos]), it.readBuffer(), toRead); |
| 707 | pos += toRead; |
| 708 | it.rp()->move(toRead); |
| 709 | } |
| 710 | |
| 711 | if (reset) { |
| 712 | resetInternalLocked(); |
| 713 | } |
| 714 | |
| 715 | VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | } // namespace statsd |
| 719 | } // namespace os |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 720 | } // namespace android |