blob: 445ee59947e9fb8e4d7db2d0ddf671b4c95d9f5b [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);
Bookatzd1fd2422017-11-22 15:21:03 -0800213 Alert::IncidentdDetails* details = alert->mutable_incidentd_details();
214 details->add_section(12);
215 details->add_section(13);
Bookatzd3606c72017-10-19 10:13:49 -0700216
Yao Chen729093d2017-10-16 10:33:26 -0700217 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700218 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800219 metric->set_name("METRIC_2");
Yao Chen729093d2017-10-16 10:33:26 -0700220 metric->set_what("PROCESS_STATE_CHANGE");
221 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
222 KeyMatcher* keyMatcher = metric->add_dimension();
223 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700224
Yang Lu3eba6212017-10-25 19:54:45 -0700225 // Anomaly threshold for background count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800226 alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800227 alert->set_name("ALERT_2");
228 alert->set_metric_name("METRIC_2");
Yang Lu3eba6212017-10-25 19:54:45 -0700229 alert->set_number_of_buckets(4);
230 alert->set_trigger_if_sum_gt(30);
231 alert->set_refractory_period_secs(20);
Bookatzd1fd2422017-11-22 15:21:03 -0800232 details = alert->mutable_incidentd_details();
233 details->add_section(14);
234 details->add_section(15);
Yang Lu3eba6212017-10-25 19:54:45 -0700235
Yao Chen729093d2017-10-16 10:33:26 -0700236 // Count process state changes, slice by uid, while SCREEN_IS_OFF
237 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800238 metric->set_name("METRIC_3");
Yao Chen729093d2017-10-16 10:33:26 -0700239 metric->set_what("PROCESS_STATE_CHANGE");
240 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
241 keyMatcher = metric->add_dimension();
242 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
243 metric->set_condition("SCREEN_IS_OFF");
244
Yao Chen5110bed2017-10-23 12:50:02 -0700245 // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
Yao Chen729093d2017-10-16 10:33:26 -0700246 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800247 metric->set_name("METRIC_4");
Yao Chen729093d2017-10-16 10:33:26 -0700248 metric->set_what("APP_GET_WL");
249 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
250 keyMatcher = metric->add_dimension();
251 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
252 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
253 EventConditionLink* link = metric->add_links();
254 link->set_condition("APP_IS_BACKGROUND");
255 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
256 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
257
Yao Chen5154a3792017-10-30 22:57:06 -0700258 // Duration of an app holding any wl, while screen on and app in background, slice by uid
Yao Chen729093d2017-10-16 10:33:26 -0700259 DurationMetric* durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800260 durationMetric->set_name("METRIC_5");
Yao Chen729093d2017-10-16 10:33:26 -0700261 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800262 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen729093d2017-10-16 10:33:26 -0700263 keyMatcher = durationMetric->add_dimension();
264 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800265 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800266 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen729093d2017-10-16 10:33:26 -0700267 link = durationMetric->add_links();
268 link->set_condition("APP_IS_BACKGROUND");
269 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
270 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
271
Yao Chen5154a3792017-10-30 22:57:06 -0700272 // max Duration of an app holding any wl, while screen on and app in background, slice by uid
273 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800274 durationMetric->set_name("METRIC_6");
Yao Chen5154a3792017-10-30 22:57:06 -0700275 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800276 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen5154a3792017-10-30 22:57:06 -0700277 keyMatcher = durationMetric->add_dimension();
278 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800279 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800280 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700281 link = durationMetric->add_links();
282 link->set_condition("APP_IS_BACKGROUND");
283 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
284 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
285
286 // Duration of an app holding any wl, while screen on and app in background
287 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800288 durationMetric->set_name("METRIC_7");
Yao Chen5154a3792017-10-30 22:57:06 -0700289 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800290 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen967b2052017-11-07 16:36:43 -0800291 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800292 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700293 link = durationMetric->add_links();
294 link->set_condition("APP_IS_BACKGROUND");
295 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
296 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
297
298 // Duration of screen on time.
299 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800300 durationMetric->set_name("METRIC_8");
Yangster1d4d6862017-10-31 12:58:51 -0700301 durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800302 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen5154a3792017-10-30 22:57:06 -0700303 durationMetric->set_what("SCREEN_IS_ON");
304
Chenjie Yub3dda412017-10-24 13:41:59 -0700305 // Value metric to count KERNEL_WAKELOCK when screen turned on
306 ValueMetric* valueMetric = config.add_value_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800307 valueMetric->set_name("METRIC_6");
Chenjie Yub3dda412017-10-24 13:41:59 -0700308 valueMetric->set_what("KERNEL_WAKELOCK");
309 valueMetric->set_value_field(1);
310 valueMetric->set_condition("SCREEN_IS_ON");
311 keyMatcher = valueMetric->add_dimension();
312 keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY);
313 // This is for testing easier. We should never set bucket size this small.
314 valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
315
Yao Chen5110bed2017-10-23 12:50:02 -0700316 // Add an EventMetric to log process state change events.
317 EventMetric* eventMetric = config.add_event_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800318 eventMetric->set_name("METRIC_9");
Yao Chen5110bed2017-10-23 12:50:02 -0700319 eventMetric->set_what("SCREEN_TURNED_ON");
320
Yangster1d4d6862017-10-31 12:58:51 -0700321 // Add an GaugeMetric.
322 GaugeMetric* gaugeMetric = config.add_gauge_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800323 gaugeMetric->set_name("METRIC_10");
Yangster1d4d6862017-10-31 12:58:51 -0700324 gaugeMetric->set_what("DEVICE_TEMPERATURE");
325 gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
326 gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
327
Yao Chen729093d2017-10-16 10:33:26 -0700328 // Event matchers............
Yangster1d4d6862017-10-31 12:58:51 -0700329 LogEntryMatcher* temperatureEntryMatcher = config.add_log_entry_matcher();
330 temperatureEntryMatcher->set_name("DEVICE_TEMPERATURE");
331 temperatureEntryMatcher->mutable_simple_log_entry_matcher()->set_tag(
332 DEVICE_TEMPERATURE_TAG_ID);
333
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700334 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700335 eventMatcher->set_name("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700336 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700337 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
338 KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
339 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
340 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700341
342 eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700343 eventMatcher->set_name("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700344 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700345 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
346 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
347 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
348 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700349
Yao Chen729093d2017-10-16 10:33:26 -0700350 eventMatcher = config.add_log_entry_matcher();
351 eventMatcher->set_name("PROCESS_STATE_CHANGE");
352 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
353 simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700354
Yao Chen729093d2017-10-16 10:33:26 -0700355 eventMatcher = config.add_log_entry_matcher();
356 eventMatcher->set_name("APP_GOES_BACKGROUND");
357 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
358 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
359 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
360 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
361 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700362
Yao Chen729093d2017-10-16 10:33:26 -0700363 eventMatcher = config.add_log_entry_matcher();
364 eventMatcher->set_name("APP_GOES_FOREGROUND");
365 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
366 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
367 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
368 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
369 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700370
Yao Chen729093d2017-10-16 10:33:26 -0700371 eventMatcher = config.add_log_entry_matcher();
372 eventMatcher->set_name("APP_GET_WL");
373 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
374 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
375 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
376 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
377 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700378
Yao Chen729093d2017-10-16 10:33:26 -0700379 eventMatcher = config.add_log_entry_matcher();
380 eventMatcher->set_name("APP_RELEASE_WL");
381 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
382 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
383 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
384 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
385 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700386
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700387 // pulled events
388 eventMatcher = config.add_log_entry_matcher();
389 eventMatcher->set_name("KERNEL_WAKELOCK");
390 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
391 simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID);
392
Yao Chen729093d2017-10-16 10:33:26 -0700393 // Conditions.............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700394 Condition* condition = config.add_condition();
395 condition->set_name("SCREEN_IS_ON");
396 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700397 simpleCondition->set_start("SCREEN_TURNED_ON");
398 simpleCondition->set_stop("SCREEN_TURNED_OFF");
Yao Chen967b2052017-11-07 16:36:43 -0800399 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700400
401 condition = config.add_condition();
402 condition->set_name("SCREEN_IS_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700403 simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700404 simpleCondition->set_start("SCREEN_TURNED_OFF");
405 simpleCondition->set_stop("SCREEN_TURNED_ON");
Yao Chen967b2052017-11-07 16:36:43 -0800406 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700407
408 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700409 condition->set_name("APP_IS_BACKGROUND");
410 simpleCondition = condition->mutable_simple_condition();
411 simpleCondition->set_start("APP_GOES_BACKGROUND");
412 simpleCondition->set_stop("APP_GOES_FOREGROUND");
Yao Chen5154a3792017-10-30 22:57:06 -0700413 KeyMatcher* condition_dimension1 = simpleCondition->add_dimension();
414 condition_dimension1->set_key(APP_USAGE_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800415 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700416
417 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700418 condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
419 Condition_Combination* combination_condition = condition->mutable_combination();
420 combination_condition->set_operation(LogicalOperation::AND);
421 combination_condition->add_condition("APP_IS_BACKGROUND");
422 combination_condition->add_condition("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700423
Yao Chen5154a3792017-10-30 22:57:06 -0700424 condition = config.add_condition();
Yao Chen967b2052017-11-07 16:36:43 -0800425 condition->set_name("WL_HELD_PER_APP_PER_NAME");
Yao Chen5154a3792017-10-30 22:57:06 -0700426 simpleCondition = condition->mutable_simple_condition();
427 simpleCondition->set_start("APP_GET_WL");
428 simpleCondition->set_stop("APP_RELEASE_WL");
429 KeyMatcher* condition_dimension = simpleCondition->add_dimension();
430 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
431 condition_dimension = simpleCondition->add_dimension();
432 condition_dimension->set_key(WAKE_LOCK_NAME_KEY);
Yao Chen967b2052017-11-07 16:36:43 -0800433 simpleCondition->set_count_nesting(true);
434
435 condition = config.add_condition();
436 condition->set_name("WL_HELD_PER_APP");
437 simpleCondition = condition->mutable_simple_condition();
438 simpleCondition->set_start("APP_GET_WL");
439 simpleCondition->set_stop("APP_RELEASE_WL");
440 simpleCondition->set_initial_value(SimpleCondition_InitialValue_FALSE);
441 condition_dimension = simpleCondition->add_dimension();
442 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
443 simpleCondition->set_count_nesting(true);
Yao Chen5154a3792017-10-30 22:57:06 -0700444
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700445 return config;
446}
447
448} // namespace statsd
449} // namespace os
450} // namespace android