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