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