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