blob: ac2192cdb3c58173c003e1ee429cb5fded4d3717 [file] [log] [blame]
Joe Onorato9fc9edf2017-10-15 20:08:52 -07001/*
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"
yro947fbce2017-11-15 22:50:23 -080018#include "storage/StorageManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019
20#include "stats_util.h"
21
yro87d983c2017-11-14 21:31:43 -080022#include <android-base/file.h>
23#include <dirent.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024#include <stdio.h>
yro87d983c2017-11-14 21:31:43 -080025#include <vector>
26#include "android-base/stringprintf.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070027
28namespace android {
29namespace os {
30namespace statsd {
31
yro87d983c2017-11-14 21:31:43 -080032#define STATS_SERVICE_DIR "/data/system/stats-service"
33
yro87d983c2017-11-14 21:31:43 -080034using android::base::StringPrintf;
35using std::unique_ptr;
36
Joe Onorato9fc9edf2017-10-15 20:08:52 -070037ConfigManager::ConfigManager() {
38}
39
40ConfigManager::~ConfigManager() {
41}
42
43void ConfigManager::Startup() {
yro947fbce2017-11-15 22:50:23 -080044 StorageManager::readConfigFromDisk(mConfigs);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070045
46 // this should be called from StatsService when it receives a statsd_config
Yao Chen7ee94152017-11-17 09:44:40 -080047 UpdateConfig(ConfigKey(1000, "fake"), build_fake_config());
Joe Onorato9fc9edf2017-10-15 20:08:52 -070048}
49
50void ConfigManager::AddListener(const sp<ConfigListener>& listener) {
51 mListeners.push_back(listener);
52}
53
54void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
55 // Add to map
56 mConfigs[key] = config;
57 // Why doesn't this work? mConfigs.insert({key, config});
58
59 // Save to disk
yro87d983c2017-11-14 21:31:43 -080060 update_saved_configs(key, config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070061
62 // Tell everyone
63 for (auto& listener : mListeners) {
64 listener->OnConfigUpdated(key, config);
65 }
66}
67
David Chenadaf8b32017-11-03 15:42:08 -070068void ConfigManager::SetConfigReceiver(const ConfigKey& key, const string& pkg, const string& cls) {
69 mConfigReceivers[key] = pair<string, string>(pkg, cls);
70}
71
72void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) {
73 mConfigReceivers.erase(key);
74}
75
Joe Onorato9fc9edf2017-10-15 20:08:52 -070076void ConfigManager::RemoveConfig(const ConfigKey& key) {
77 unordered_map<ConfigKey, StatsdConfig>::iterator it = mConfigs.find(key);
78 if (it != mConfigs.end()) {
79 // Remove from map
80 mConfigs.erase(it);
81
Joe Onorato9fc9edf2017-10-15 20:08:52 -070082 // Tell everyone
83 for (auto& listener : mListeners) {
84 listener->OnConfigRemoved(key);
85 }
86 }
yro9c98c052017-11-19 14:33:56 -080087
88 // Remove from disk. There can still be a lingering file on disk so we check
89 // whether or not the config was on memory.
90 remove_saved_configs(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070091}
92
yro87d983c2017-11-14 21:31:43 -080093void ConfigManager::remove_saved_configs(const ConfigKey& key) {
yro87d983c2017-11-14 21:31:43 -080094 string prefix = StringPrintf("%d-%s", key.GetUid(), key.GetName().c_str());
yro947fbce2017-11-15 22:50:23 -080095 StorageManager::deletePrefixedFiles(STATS_SERVICE_DIR, prefix.c_str());
yro87d983c2017-11-14 21:31:43 -080096}
97
Joe Onorato9fc9edf2017-10-15 20:08:52 -070098void ConfigManager::RemoveConfigs(int uid) {
99 vector<ConfigKey> removed;
100
101 for (auto it = mConfigs.begin(); it != mConfigs.end();) {
102 // Remove from map
103 if (it->first.GetUid() == uid) {
104 removed.push_back(it->first);
105 it = mConfigs.erase(it);
David Chenadaf8b32017-11-03 15:42:08 -0700106 mConfigReceivers.erase(it->first);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700107 } else {
108 it++;
109 }
110 }
111
112 // Remove separately so if they do anything in the callback they can't mess up our iteration.
113 for (auto& key : removed) {
114 // Tell everyone
115 for (auto& listener : mListeners) {
116 listener->OnConfigRemoved(key);
117 }
118 }
119}
120
Yangster7c334a12017-11-22 14:24:24 -0800121vector<ConfigKey> ConfigManager::GetAllConfigKeys() const {
David Chen1d7b0cd2017-11-15 14:20:04 -0800122 vector<ConfigKey> ret;
123 for (auto it = mConfigs.cbegin(); it != mConfigs.cend(); ++it) {
124 ret.push_back(it->first);
125 }
126 return ret;
127}
128
Yangster7c334a12017-11-22 14:24:24 -0800129const pair<string, string> ConfigManager::GetConfigReceiver(const ConfigKey& key) const {
David Chen1d7b0cd2017-11-15 14:20:04 -0800130 auto it = mConfigReceivers.find(key);
131 if (it == mConfigReceivers.end()) {
132 return pair<string,string>();
133 } else {
134 return it->second;
135 }
136}
137
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700138void ConfigManager::Dump(FILE* out) {
139 fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
140 fprintf(out, " uid name\n");
141 for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin();
142 it != mConfigs.end(); it++) {
143 fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800144 auto receiverIt = mConfigReceivers.find(it->first);
145 if (receiverIt != mConfigReceivers.end()) {
146 fprintf(out, " -> received by %s, %s\n", receiverIt->second.first.c_str(),
147 receiverIt->second.second.c_str());
148 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700149 // TODO: Print the contents of the config too.
150 }
151}
152
yro87d983c2017-11-14 21:31:43 -0800153void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) {
154 mkdir(STATS_SERVICE_DIR, S_IRWXU);
yro9c98c052017-11-19 14:33:56 -0800155
156 // If there is a pre-existing config with same key we should first delete it.
157 remove_saved_configs(key);
158
159 // Then we save the latest config.
yro87d983c2017-11-14 21:31:43 -0800160 string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
161 key.GetName().c_str(), time(nullptr));
yro947fbce2017-11-15 22:50:23 -0800162 const int numBytes = config.ByteSize();
163 vector<uint8_t> buffer(numBytes);
164 config.SerializeToArray(&buffer[0], numBytes);
165 StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700166}
167
Yangster-mac756cd482017-11-21 21:58:44 -0800168StatsdConfig build_fake_config() {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700169 // HACK: Hard code a test metric for counting screen on events...
170 StatsdConfig config;
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800171 config.set_name("CONFIG_12345");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700172
Yao Chen5154a3792017-10-30 22:57:06 -0700173 int WAKE_LOCK_TAG_ID = 1111; // put a fake id here to make testing easier.
Yao Chen729093d2017-10-16 10:33:26 -0700174 int WAKE_LOCK_UID_KEY_ID = 1;
Yao Chen5154a3792017-10-30 22:57:06 -0700175 int WAKE_LOCK_NAME_KEY = 3;
176 int WAKE_LOCK_STATE_KEY = 4;
Yao Chen729093d2017-10-16 10:33:26 -0700177 int WAKE_LOCK_ACQUIRE_VALUE = 1;
178 int WAKE_LOCK_RELEASE_VALUE = 0;
179
180 int APP_USAGE_ID = 12345;
181 int APP_USAGE_UID_KEY_ID = 1;
182 int APP_USAGE_STATE_KEY = 2;
183 int APP_USAGE_FOREGROUND = 1;
184 int APP_USAGE_BACKGROUND = 0;
185
Bookatzc1a050a2017-10-10 15:49:28 -0700186 int SCREEN_EVENT_TAG_ID = 29;
Yao Chen729093d2017-10-16 10:33:26 -0700187 int SCREEN_EVENT_STATE_KEY = 1;
188 int SCREEN_EVENT_ON_VALUE = 2;
189 int SCREEN_EVENT_OFF_VALUE = 1;
190
Bookatzc1a050a2017-10-10 15:49:28 -0700191 int UID_PROCESS_STATE_TAG_ID = 27;
Yao Chen729093d2017-10-16 10:33:26 -0700192 int UID_PROCESS_STATE_UID_KEY = 1;
193
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700194 int KERNEL_WAKELOCK_TAG_ID = 1004;
Chenjie Yub3dda412017-10-24 13:41:59 -0700195 int KERNEL_WAKELOCK_NAME_KEY = 4;
196
Yangster1d4d6862017-10-31 12:58:51 -0700197 int DEVICE_TEMPERATURE_TAG_ID = 33;
198 int DEVICE_TEMPERATURE_KEY = 1;
199
Yao Chen729093d2017-10-16 10:33:26 -0700200 // Count Screen ON events.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700201 CountMetric* metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800202 metric->set_name("METRIC_1");
Yao Chen729093d2017-10-16 10:33:26 -0700203 metric->set_what("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700204 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
205
Bookatzd3606c72017-10-19 10:13:49 -0700206 // Anomaly threshold for screen-on count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800207 Alert* alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800208 alert->set_name("ALERT_1");
209 alert->set_metric_name("METRIC_1");
Bookatzd3606c72017-10-19 10:13:49 -0700210 alert->set_number_of_buckets(6);
211 alert->set_trigger_if_sum_gt(10);
212 alert->set_refractory_period_secs(30);
213
Yao Chen729093d2017-10-16 10:33:26 -0700214 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700215 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800216 metric->set_name("METRIC_2");
Yao Chen729093d2017-10-16 10:33:26 -0700217 metric->set_what("PROCESS_STATE_CHANGE");
218 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
219 KeyMatcher* keyMatcher = metric->add_dimension();
220 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700221
Yang Lu3eba6212017-10-25 19:54:45 -0700222 // Anomaly threshold for background count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800223 alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800224 alert->set_name("ALERT_2");
225 alert->set_metric_name("METRIC_2");
Yang Lu3eba6212017-10-25 19:54:45 -0700226 alert->set_number_of_buckets(4);
227 alert->set_trigger_if_sum_gt(30);
228 alert->set_refractory_period_secs(20);
229
Yao Chen729093d2017-10-16 10:33:26 -0700230 // Count process state changes, slice by uid, while SCREEN_IS_OFF
231 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800232 metric->set_name("METRIC_3");
Yao Chen729093d2017-10-16 10:33:26 -0700233 metric->set_what("PROCESS_STATE_CHANGE");
234 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
235 keyMatcher = metric->add_dimension();
236 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
237 metric->set_condition("SCREEN_IS_OFF");
238
Yao Chen5110bed2017-10-23 12:50:02 -0700239 // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
Yao Chen729093d2017-10-16 10:33:26 -0700240 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800241 metric->set_name("METRIC_4");
Yao Chen729093d2017-10-16 10:33:26 -0700242 metric->set_what("APP_GET_WL");
243 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
244 keyMatcher = metric->add_dimension();
245 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
246 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
247 EventConditionLink* link = metric->add_links();
248 link->set_condition("APP_IS_BACKGROUND");
249 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
250 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
251
Yao Chen5154a3792017-10-30 22:57:06 -0700252 // Duration of an app holding any wl, while screen on and app in background, slice by uid
Yao Chen729093d2017-10-16 10:33:26 -0700253 DurationMetric* durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800254 durationMetric->set_name("METRIC_5");
Yao Chen729093d2017-10-16 10:33:26 -0700255 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800256 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen729093d2017-10-16 10:33:26 -0700257 keyMatcher = durationMetric->add_dimension();
258 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800259 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800260 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen729093d2017-10-16 10:33:26 -0700261 link = durationMetric->add_links();
262 link->set_condition("APP_IS_BACKGROUND");
263 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
264 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
265
Yao Chen5154a3792017-10-30 22:57:06 -0700266 // max Duration of an app holding any wl, while screen on and app in background, slice by uid
267 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800268 durationMetric->set_name("METRIC_6");
Yao Chen5154a3792017-10-30 22:57:06 -0700269 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800270 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen5154a3792017-10-30 22:57:06 -0700271 keyMatcher = durationMetric->add_dimension();
272 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800273 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800274 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700275 link = durationMetric->add_links();
276 link->set_condition("APP_IS_BACKGROUND");
277 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
278 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
279
280 // Duration of an app holding any wl, while screen on and app in background
281 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800282 durationMetric->set_name("METRIC_7");
Yao Chen5154a3792017-10-30 22:57:06 -0700283 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800284 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen967b2052017-11-07 16:36:43 -0800285 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800286 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700287 link = durationMetric->add_links();
288 link->set_condition("APP_IS_BACKGROUND");
289 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
290 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
291
292 // Duration of screen on time.
293 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800294 durationMetric->set_name("METRIC_8");
Yangster1d4d6862017-10-31 12:58:51 -0700295 durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800296 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen5154a3792017-10-30 22:57:06 -0700297 durationMetric->set_what("SCREEN_IS_ON");
298
Chenjie Yub3dda412017-10-24 13:41:59 -0700299 // Value metric to count KERNEL_WAKELOCK when screen turned on
300 ValueMetric* valueMetric = config.add_value_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800301 valueMetric->set_name("METRIC_6");
Chenjie Yub3dda412017-10-24 13:41:59 -0700302 valueMetric->set_what("KERNEL_WAKELOCK");
303 valueMetric->set_value_field(1);
304 valueMetric->set_condition("SCREEN_IS_ON");
305 keyMatcher = valueMetric->add_dimension();
306 keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY);
307 // This is for testing easier. We should never set bucket size this small.
308 valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
309
Yao Chen5110bed2017-10-23 12:50:02 -0700310 // Add an EventMetric to log process state change events.
311 EventMetric* eventMetric = config.add_event_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800312 eventMetric->set_name("METRIC_9");
Yao Chen5110bed2017-10-23 12:50:02 -0700313 eventMetric->set_what("SCREEN_TURNED_ON");
314
Yangster1d4d6862017-10-31 12:58:51 -0700315 // Add an GaugeMetric.
316 GaugeMetric* gaugeMetric = config.add_gauge_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800317 gaugeMetric->set_name("METRIC_10");
Yangster1d4d6862017-10-31 12:58:51 -0700318 gaugeMetric->set_what("DEVICE_TEMPERATURE");
319 gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
320 gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
321
Yao Chen729093d2017-10-16 10:33:26 -0700322 // Event matchers............
Yangster1d4d6862017-10-31 12:58:51 -0700323 LogEntryMatcher* temperatureEntryMatcher = config.add_log_entry_matcher();
324 temperatureEntryMatcher->set_name("DEVICE_TEMPERATURE");
325 temperatureEntryMatcher->mutable_simple_log_entry_matcher()->set_tag(
326 DEVICE_TEMPERATURE_TAG_ID);
327
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700328 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700329 eventMatcher->set_name("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700330 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700331 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
332 KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
333 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
334 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700335
336 eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700337 eventMatcher->set_name("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700338 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700339 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
340 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
341 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
342 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700343
Yao Chen729093d2017-10-16 10:33:26 -0700344 eventMatcher = config.add_log_entry_matcher();
345 eventMatcher->set_name("PROCESS_STATE_CHANGE");
346 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
347 simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700348
Yao Chen729093d2017-10-16 10:33:26 -0700349 eventMatcher = config.add_log_entry_matcher();
350 eventMatcher->set_name("APP_GOES_BACKGROUND");
351 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
352 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
353 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
354 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
355 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700356
Yao Chen729093d2017-10-16 10:33:26 -0700357 eventMatcher = config.add_log_entry_matcher();
358 eventMatcher->set_name("APP_GOES_FOREGROUND");
359 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
360 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
361 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
362 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
363 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700364
Yao Chen729093d2017-10-16 10:33:26 -0700365 eventMatcher = config.add_log_entry_matcher();
366 eventMatcher->set_name("APP_GET_WL");
367 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
368 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
369 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
370 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
371 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700372
Yao Chen729093d2017-10-16 10:33:26 -0700373 eventMatcher = config.add_log_entry_matcher();
374 eventMatcher->set_name("APP_RELEASE_WL");
375 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
376 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
377 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
378 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
379 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700380
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700381 // pulled events
382 eventMatcher = config.add_log_entry_matcher();
383 eventMatcher->set_name("KERNEL_WAKELOCK");
384 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
385 simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID);
386
Yao Chen729093d2017-10-16 10:33:26 -0700387 // Conditions.............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700388 Condition* condition = config.add_condition();
389 condition->set_name("SCREEN_IS_ON");
390 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700391 simpleCondition->set_start("SCREEN_TURNED_ON");
392 simpleCondition->set_stop("SCREEN_TURNED_OFF");
Yao Chen967b2052017-11-07 16:36:43 -0800393 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700394
395 condition = config.add_condition();
396 condition->set_name("SCREEN_IS_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700397 simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700398 simpleCondition->set_start("SCREEN_TURNED_OFF");
399 simpleCondition->set_stop("SCREEN_TURNED_ON");
Yao Chen967b2052017-11-07 16:36:43 -0800400 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700401
402 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700403 condition->set_name("APP_IS_BACKGROUND");
404 simpleCondition = condition->mutable_simple_condition();
405 simpleCondition->set_start("APP_GOES_BACKGROUND");
406 simpleCondition->set_stop("APP_GOES_FOREGROUND");
Yao Chen5154a3792017-10-30 22:57:06 -0700407 KeyMatcher* condition_dimension1 = simpleCondition->add_dimension();
408 condition_dimension1->set_key(APP_USAGE_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800409 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700410
411 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700412 condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
413 Condition_Combination* combination_condition = condition->mutable_combination();
414 combination_condition->set_operation(LogicalOperation::AND);
415 combination_condition->add_condition("APP_IS_BACKGROUND");
416 combination_condition->add_condition("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700417
Yao Chen5154a3792017-10-30 22:57:06 -0700418 condition = config.add_condition();
Yao Chen967b2052017-11-07 16:36:43 -0800419 condition->set_name("WL_HELD_PER_APP_PER_NAME");
Yao Chen5154a3792017-10-30 22:57:06 -0700420 simpleCondition = condition->mutable_simple_condition();
421 simpleCondition->set_start("APP_GET_WL");
422 simpleCondition->set_stop("APP_RELEASE_WL");
423 KeyMatcher* condition_dimension = simpleCondition->add_dimension();
424 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
425 condition_dimension = simpleCondition->add_dimension();
426 condition_dimension->set_key(WAKE_LOCK_NAME_KEY);
Yao Chen967b2052017-11-07 16:36:43 -0800427 simpleCondition->set_count_nesting(true);
428
429 condition = config.add_condition();
430 condition->set_name("WL_HELD_PER_APP");
431 simpleCondition = condition->mutable_simple_condition();
432 simpleCondition->set_start("APP_GET_WL");
433 simpleCondition->set_stop("APP_RELEASE_WL");
434 simpleCondition->set_initial_value(SimpleCondition_InitialValue_FALSE);
435 condition_dimension = simpleCondition->add_dimension();
436 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
437 simpleCondition->set_count_nesting(true);
Yao Chen5154a3792017-10-30 22:57:06 -0700438
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700439 return config;
440}
441
442} // namespace statsd
443} // namespace os
444} // namespace android