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