Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
| 17 | #include "config/ConfigManager.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 18 | #include "storage/StorageManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 19 | |
| 20 | #include "stats_util.h" |
| 21 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 22 | #include <android-base/file.h> |
| 23 | #include <dirent.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 25 | #include <vector> |
| 26 | #include "android-base/stringprintf.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace statsd { |
| 31 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 32 | #define STATS_SERVICE_DIR "/data/system/stats-service" |
| 33 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 34 | using android::base::StringPrintf; |
| 35 | using std::unique_ptr; |
| 36 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 37 | ConfigManager::ConfigManager() { |
| 38 | } |
| 39 | |
| 40 | ConfigManager::~ConfigManager() { |
| 41 | } |
| 42 | |
| 43 | void ConfigManager::Startup() { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 44 | StorageManager::readConfigFromDisk(mConfigs); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 45 | |
| 46 | // this should be called from StatsService when it receives a statsd_config |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 47 | UpdateConfig(ConfigKey(1000, "fake"), build_fake_config()); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void ConfigManager::AddListener(const sp<ConfigListener>& listener) { |
| 51 | mListeners.push_back(listener); |
| 52 | } |
| 53 | |
| 54 | void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) { |
| 55 | // Add to map |
| 56 | mConfigs[key] = config; |
| 57 | // Why doesn't this work? mConfigs.insert({key, config}); |
| 58 | |
| 59 | // Save to disk |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 60 | update_saved_configs(key, config); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 61 | |
| 62 | // Tell everyone |
| 63 | for (auto& listener : mListeners) { |
| 64 | listener->OnConfigUpdated(key, config); |
| 65 | } |
| 66 | } |
| 67 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 68 | void ConfigManager::SetConfigReceiver(const ConfigKey& key, const string& pkg, const string& cls) { |
| 69 | mConfigReceivers[key] = pair<string, string>(pkg, cls); |
| 70 | } |
| 71 | |
| 72 | void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) { |
| 73 | mConfigReceivers.erase(key); |
| 74 | } |
| 75 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 76 | void ConfigManager::RemoveConfig(const ConfigKey& key) { |
| 77 | unordered_map<ConfigKey, StatsdConfig>::iterator it = mConfigs.find(key); |
| 78 | if (it != mConfigs.end()) { |
| 79 | // Remove from map |
| 80 | mConfigs.erase(it); |
| 81 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 82 | // Tell everyone |
| 83 | for (auto& listener : mListeners) { |
| 84 | listener->OnConfigRemoved(key); |
| 85 | } |
| 86 | } |
yro | 9c98c05 | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 87 | |
| 88 | // Remove from disk. There can still be a lingering file on disk so we check |
| 89 | // whether or not the config was on memory. |
| 90 | remove_saved_configs(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 91 | } |
| 92 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 93 | void ConfigManager::remove_saved_configs(const ConfigKey& key) { |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 94 | string prefix = StringPrintf("%d-%s", key.GetUid(), key.GetName().c_str()); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 95 | StorageManager::deletePrefixedFiles(STATS_SERVICE_DIR, prefix.c_str()); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 98 | void ConfigManager::RemoveConfigs(int uid) { |
| 99 | vector<ConfigKey> removed; |
| 100 | |
| 101 | for (auto it = mConfigs.begin(); it != mConfigs.end();) { |
| 102 | // Remove from map |
| 103 | if (it->first.GetUid() == uid) { |
| 104 | removed.push_back(it->first); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 105 | mConfigReceivers.erase(it->first); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 106 | it = mConfigs.erase(it); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 107 | } else { |
| 108 | it++; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 113 | for (auto& key : removed) { |
| 114 | // Tell everyone |
| 115 | for (auto& listener : mListeners) { |
| 116 | listener->OnConfigRemoved(key); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 121 | void ConfigManager::RemoveAllConfigs() { |
| 122 | vector<ConfigKey> removed; |
| 123 | |
| 124 | for (auto it = mConfigs.begin(); it != mConfigs.end();) { |
| 125 | // Remove from map |
| 126 | removed.push_back(it->first); |
| 127 | auto receiverIt = mConfigReceivers.find(it->first); |
| 128 | if (receiverIt != mConfigReceivers.end()) { |
| 129 | mConfigReceivers.erase(it->first); |
| 130 | } |
| 131 | it = mConfigs.erase(it); |
| 132 | } |
| 133 | |
| 134 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 135 | for (auto& key : removed) { |
| 136 | // Tell everyone |
| 137 | for (auto& listener : mListeners) { |
| 138 | listener->OnConfigRemoved(key); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 143 | vector<ConfigKey> ConfigManager::GetAllConfigKeys() const { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 144 | vector<ConfigKey> ret; |
| 145 | for (auto it = mConfigs.cbegin(); it != mConfigs.cend(); ++it) { |
| 146 | ret.push_back(it->first); |
| 147 | } |
| 148 | return ret; |
| 149 | } |
| 150 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 151 | const pair<string, string> ConfigManager::GetConfigReceiver(const ConfigKey& key) const { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 152 | auto it = mConfigReceivers.find(key); |
| 153 | if (it == mConfigReceivers.end()) { |
| 154 | return pair<string,string>(); |
| 155 | } else { |
| 156 | return it->second; |
| 157 | } |
| 158 | } |
| 159 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 160 | void ConfigManager::Dump(FILE* out) { |
| 161 | fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size()); |
| 162 | fprintf(out, " uid name\n"); |
| 163 | for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin(); |
| 164 | it != mConfigs.end(); it++) { |
| 165 | fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 166 | auto receiverIt = mConfigReceivers.find(it->first); |
| 167 | if (receiverIt != mConfigReceivers.end()) { |
| 168 | fprintf(out, " -> received by %s, %s\n", receiverIt->second.first.c_str(), |
| 169 | receiverIt->second.second.c_str()); |
| 170 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 171 | // TODO: Print the contents of the config too. |
| 172 | } |
| 173 | } |
| 174 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 175 | void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) { |
| 176 | mkdir(STATS_SERVICE_DIR, S_IRWXU); |
yro | 9c98c05 | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 177 | |
| 178 | // If there is a pre-existing config with same key we should first delete it. |
| 179 | remove_saved_configs(key); |
| 180 | |
| 181 | // Then we save the latest config. |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 182 | string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(), |
| 183 | key.GetName().c_str(), time(nullptr)); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 184 | const int numBytes = config.ByteSize(); |
| 185 | vector<uint8_t> buffer(numBytes); |
| 186 | config.SerializeToArray(&buffer[0], numBytes); |
| 187 | StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Yangster-mac | 756cd48 | 2017-11-21 21:58:44 -0800 | [diff] [blame] | 190 | StatsdConfig build_fake_config() { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 191 | // HACK: Hard code a test metric for counting screen on events... |
| 192 | StatsdConfig config; |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 193 | config.set_name("CONFIG_12345"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 194 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 195 | int WAKE_LOCK_TAG_ID = 1111; // put a fake id here to make testing easier. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 196 | int WAKE_LOCK_UID_KEY_ID = 1; |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 197 | int WAKE_LOCK_NAME_KEY = 3; |
| 198 | int WAKE_LOCK_STATE_KEY = 4; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 199 | int WAKE_LOCK_ACQUIRE_VALUE = 1; |
| 200 | int WAKE_LOCK_RELEASE_VALUE = 0; |
| 201 | |
| 202 | int APP_USAGE_ID = 12345; |
| 203 | int APP_USAGE_UID_KEY_ID = 1; |
| 204 | int APP_USAGE_STATE_KEY = 2; |
| 205 | int APP_USAGE_FOREGROUND = 1; |
| 206 | int APP_USAGE_BACKGROUND = 0; |
| 207 | |
Bookatz | c1a050a | 2017-10-10 15:49:28 -0700 | [diff] [blame] | 208 | int SCREEN_EVENT_TAG_ID = 29; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 209 | int SCREEN_EVENT_STATE_KEY = 1; |
| 210 | int SCREEN_EVENT_ON_VALUE = 2; |
| 211 | int SCREEN_EVENT_OFF_VALUE = 1; |
| 212 | |
Bookatz | c1a050a | 2017-10-10 15:49:28 -0700 | [diff] [blame] | 213 | int UID_PROCESS_STATE_TAG_ID = 27; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 214 | int UID_PROCESS_STATE_UID_KEY = 1; |
| 215 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 216 | int KERNEL_WAKELOCK_TAG_ID = 1004; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 217 | int KERNEL_WAKELOCK_NAME_KEY = 4; |
| 218 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 219 | int DEVICE_TEMPERATURE_TAG_ID = 33; |
| 220 | int DEVICE_TEMPERATURE_KEY = 1; |
| 221 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 222 | // Count Screen ON events. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 223 | CountMetric* metric = config.add_count_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 224 | metric->set_name("METRIC_1"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 225 | metric->set_what("SCREEN_TURNED_ON"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 226 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 227 | |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 228 | // Anomaly threshold for screen-on count. |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 229 | Alert* alert = config.add_alert(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 230 | alert->set_name("ALERT_1"); |
| 231 | alert->set_metric_name("METRIC_1"); |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 232 | alert->set_number_of_buckets(6); |
| 233 | alert->set_trigger_if_sum_gt(10); |
| 234 | alert->set_refractory_period_secs(30); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 235 | Alert::IncidentdDetails* details = alert->mutable_incidentd_details(); |
| 236 | details->add_section(12); |
| 237 | details->add_section(13); |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 238 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 239 | // Count process state changes, slice by uid. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 240 | metric = config.add_count_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 241 | metric->set_name("METRIC_2"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 242 | metric->set_what("PROCESS_STATE_CHANGE"); |
| 243 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 244 | KeyMatcher* keyMatcher = metric->add_dimension(); |
| 245 | keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 246 | |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 247 | // Anomaly threshold for background count. |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 248 | alert = config.add_alert(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 249 | alert->set_name("ALERT_2"); |
| 250 | alert->set_metric_name("METRIC_2"); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 251 | alert->set_number_of_buckets(4); |
| 252 | alert->set_trigger_if_sum_gt(30); |
| 253 | alert->set_refractory_period_secs(20); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 254 | details = alert->mutable_incidentd_details(); |
| 255 | details->add_section(14); |
| 256 | details->add_section(15); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 257 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 258 | // Count process state changes, slice by uid, while SCREEN_IS_OFF |
| 259 | metric = config.add_count_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 260 | metric->set_name("METRIC_3"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 261 | metric->set_what("PROCESS_STATE_CHANGE"); |
| 262 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 263 | keyMatcher = metric->add_dimension(); |
| 264 | keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY); |
| 265 | metric->set_condition("SCREEN_IS_OFF"); |
| 266 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 267 | // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 268 | metric = config.add_count_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 269 | metric->set_name("METRIC_4"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 270 | metric->set_what("APP_GET_WL"); |
| 271 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 272 | keyMatcher = metric->add_dimension(); |
| 273 | keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID); |
| 274 | metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON"); |
| 275 | EventConditionLink* link = metric->add_links(); |
| 276 | link->set_condition("APP_IS_BACKGROUND"); |
| 277 | link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID); |
| 278 | link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID); |
| 279 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 280 | // Duration of an app holding any wl, while screen on and app in background, slice by uid |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 281 | DurationMetric* durationMetric = config.add_duration_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 282 | durationMetric->set_name("METRIC_5"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 283 | durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 284 | durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 285 | keyMatcher = durationMetric->add_dimension(); |
| 286 | keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 287 | durationMetric->set_what("WL_HELD_PER_APP_PER_NAME"); |
Yao Chen | 5c925ad | 2017-11-15 14:15:46 -0800 | [diff] [blame] | 288 | durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON"); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 289 | link = durationMetric->add_links(); |
| 290 | link->set_condition("APP_IS_BACKGROUND"); |
| 291 | link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID); |
| 292 | link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID); |
| 293 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 294 | // max Duration of an app holding any wl, while screen on and app in background, slice by uid |
| 295 | durationMetric = config.add_duration_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 296 | durationMetric->set_name("METRIC_6"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 297 | durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 298 | durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 299 | keyMatcher = durationMetric->add_dimension(); |
| 300 | keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 301 | durationMetric->set_what("WL_HELD_PER_APP_PER_NAME"); |
Yao Chen | 5c925ad | 2017-11-15 14:15:46 -0800 | [diff] [blame] | 302 | durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 303 | link = durationMetric->add_links(); |
| 304 | link->set_condition("APP_IS_BACKGROUND"); |
| 305 | link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID); |
| 306 | link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID); |
| 307 | |
| 308 | // Duration of an app holding any wl, while screen on and app in background |
| 309 | durationMetric = config.add_duration_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 310 | durationMetric->set_name("METRIC_7"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 311 | durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 312 | durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 313 | durationMetric->set_what("WL_HELD_PER_APP_PER_NAME"); |
Yao Chen | 5c925ad | 2017-11-15 14:15:46 -0800 | [diff] [blame] | 314 | durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 315 | link = durationMetric->add_links(); |
| 316 | link->set_condition("APP_IS_BACKGROUND"); |
| 317 | link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID); |
| 318 | link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID); |
| 319 | |
| 320 | // Duration of screen on time. |
| 321 | durationMetric = config.add_duration_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 322 | durationMetric->set_name("METRIC_8"); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 323 | durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 324 | durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 325 | durationMetric->set_what("SCREEN_IS_ON"); |
| 326 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 327 | // Value metric to count KERNEL_WAKELOCK when screen turned on |
| 328 | ValueMetric* valueMetric = config.add_value_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 329 | valueMetric->set_name("METRIC_6"); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 330 | valueMetric->set_what("KERNEL_WAKELOCK"); |
| 331 | valueMetric->set_value_field(1); |
| 332 | valueMetric->set_condition("SCREEN_IS_ON"); |
| 333 | keyMatcher = valueMetric->add_dimension(); |
| 334 | keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY); |
| 335 | // This is for testing easier. We should never set bucket size this small. |
| 336 | valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L); |
| 337 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 338 | // Add an EventMetric to log process state change events. |
| 339 | EventMetric* eventMetric = config.add_event_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 340 | eventMetric->set_name("METRIC_9"); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 341 | eventMetric->set_what("SCREEN_TURNED_ON"); |
| 342 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 343 | // Add an GaugeMetric. |
| 344 | GaugeMetric* gaugeMetric = config.add_gauge_metric(); |
Stefan Lafon | 7c8f0a5 | 2017-11-21 14:49:09 -0800 | [diff] [blame] | 345 | gaugeMetric->set_name("METRIC_10"); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 346 | gaugeMetric->set_what("DEVICE_TEMPERATURE"); |
| 347 | gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY); |
| 348 | gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L); |
| 349 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 350 | // Event matchers............ |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 351 | LogEntryMatcher* temperatureEntryMatcher = config.add_log_entry_matcher(); |
| 352 | temperatureEntryMatcher->set_name("DEVICE_TEMPERATURE"); |
| 353 | temperatureEntryMatcher->mutable_simple_log_entry_matcher()->set_tag( |
| 354 | DEVICE_TEMPERATURE_TAG_ID); |
| 355 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 356 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 357 | eventMatcher->set_name("SCREEN_TURNED_ON"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 358 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 359 | simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID); |
| 360 | KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher(); |
| 361 | keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY); |
| 362 | keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 363 | |
| 364 | eventMatcher = config.add_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 365 | eventMatcher->set_name("SCREEN_TURNED_OFF"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 366 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 367 | simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID); |
| 368 | keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher(); |
| 369 | keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY); |
| 370 | keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 371 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 372 | eventMatcher = config.add_log_entry_matcher(); |
| 373 | eventMatcher->set_name("PROCESS_STATE_CHANGE"); |
| 374 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 375 | simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 376 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 377 | eventMatcher = config.add_log_entry_matcher(); |
| 378 | eventMatcher->set_name("APP_GOES_BACKGROUND"); |
| 379 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 380 | simpleLogEntryMatcher->set_tag(APP_USAGE_ID); |
| 381 | keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher(); |
| 382 | keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY); |
| 383 | keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 384 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 385 | eventMatcher = config.add_log_entry_matcher(); |
| 386 | eventMatcher->set_name("APP_GOES_FOREGROUND"); |
| 387 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 388 | simpleLogEntryMatcher->set_tag(APP_USAGE_ID); |
| 389 | keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher(); |
| 390 | keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY); |
| 391 | keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 392 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 393 | eventMatcher = config.add_log_entry_matcher(); |
| 394 | eventMatcher->set_name("APP_GET_WL"); |
| 395 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 396 | simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID); |
| 397 | keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher(); |
| 398 | keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY); |
| 399 | keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 400 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 401 | eventMatcher = config.add_log_entry_matcher(); |
| 402 | eventMatcher->set_name("APP_RELEASE_WL"); |
| 403 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 404 | simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID); |
| 405 | keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher(); |
| 406 | keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY); |
| 407 | keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 408 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 409 | // pulled events |
| 410 | eventMatcher = config.add_log_entry_matcher(); |
| 411 | eventMatcher->set_name("KERNEL_WAKELOCK"); |
| 412 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 413 | simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID); |
| 414 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 415 | // Conditions............. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 416 | Condition* condition = config.add_condition(); |
| 417 | condition->set_name("SCREEN_IS_ON"); |
| 418 | SimpleCondition* simpleCondition = condition->mutable_simple_condition(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 419 | simpleCondition->set_start("SCREEN_TURNED_ON"); |
| 420 | simpleCondition->set_stop("SCREEN_TURNED_OFF"); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 421 | simpleCondition->set_count_nesting(false); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 422 | |
| 423 | condition = config.add_condition(); |
| 424 | condition->set_name("SCREEN_IS_OFF"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 425 | simpleCondition = condition->mutable_simple_condition(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 426 | simpleCondition->set_start("SCREEN_TURNED_OFF"); |
| 427 | simpleCondition->set_stop("SCREEN_TURNED_ON"); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 428 | simpleCondition->set_count_nesting(false); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 429 | |
| 430 | condition = config.add_condition(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 431 | condition->set_name("APP_IS_BACKGROUND"); |
| 432 | simpleCondition = condition->mutable_simple_condition(); |
| 433 | simpleCondition->set_start("APP_GOES_BACKGROUND"); |
| 434 | simpleCondition->set_stop("APP_GOES_FOREGROUND"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 435 | KeyMatcher* condition_dimension1 = simpleCondition->add_dimension(); |
| 436 | condition_dimension1->set_key(APP_USAGE_UID_KEY_ID); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 437 | simpleCondition->set_count_nesting(false); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 438 | |
| 439 | condition = config.add_condition(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 440 | condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON"); |
| 441 | Condition_Combination* combination_condition = condition->mutable_combination(); |
| 442 | combination_condition->set_operation(LogicalOperation::AND); |
| 443 | combination_condition->add_condition("APP_IS_BACKGROUND"); |
| 444 | combination_condition->add_condition("SCREEN_IS_ON"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 445 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 446 | condition = config.add_condition(); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 447 | condition->set_name("WL_HELD_PER_APP_PER_NAME"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 448 | simpleCondition = condition->mutable_simple_condition(); |
| 449 | simpleCondition->set_start("APP_GET_WL"); |
| 450 | simpleCondition->set_stop("APP_RELEASE_WL"); |
| 451 | KeyMatcher* condition_dimension = simpleCondition->add_dimension(); |
| 452 | condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID); |
| 453 | condition_dimension = simpleCondition->add_dimension(); |
| 454 | condition_dimension->set_key(WAKE_LOCK_NAME_KEY); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 455 | simpleCondition->set_count_nesting(true); |
| 456 | |
| 457 | condition = config.add_condition(); |
| 458 | condition->set_name("WL_HELD_PER_APP"); |
| 459 | simpleCondition = condition->mutable_simple_condition(); |
| 460 | simpleCondition->set_start("APP_GET_WL"); |
| 461 | simpleCondition->set_stop("APP_RELEASE_WL"); |
| 462 | simpleCondition->set_initial_value(SimpleCondition_InitialValue_FALSE); |
| 463 | condition_dimension = simpleCondition->add_dimension(); |
| 464 | condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID); |
| 465 | simpleCondition->set_count_nesting(true); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 466 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 467 | return config; |
| 468 | } |
| 469 | |
| 470 | } // namespace statsd |
| 471 | } // namespace os |
| 472 | } // namespace android |