blob: 4b7e49a8033680d72a619250ad3a88399c4fd8a5 [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);
David Chenadaf8b32017-11-03 15:42:08 -0700105 mConfigReceivers.erase(it->first);
yro74fed972017-11-27 14:42:42 -0800106 it = mConfigs.erase(it);
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
yro74fed972017-11-27 14:42:42 -0800121void ConfigManager::RemoveAllConfigs() {
122 vector<ConfigKey> removed;
123
124 for (auto it = mConfigs.begin(); it != mConfigs.end();) {
125 // Remove from map
126 removed.push_back(it->first);
127 auto receiverIt = mConfigReceivers.find(it->first);
128 if (receiverIt != mConfigReceivers.end()) {
129 mConfigReceivers.erase(it->first);
130 }
131 it = mConfigs.erase(it);
132 }
133
134 // Remove separately so if they do anything in the callback they can't mess up our iteration.
135 for (auto& key : removed) {
136 // Tell everyone
137 for (auto& listener : mListeners) {
138 listener->OnConfigRemoved(key);
139 }
140 }
141}
142
Yangster7c334a12017-11-22 14:24:24 -0800143vector<ConfigKey> ConfigManager::GetAllConfigKeys() const {
David Chen1d7b0cd2017-11-15 14:20:04 -0800144 vector<ConfigKey> ret;
145 for (auto it = mConfigs.cbegin(); it != mConfigs.cend(); ++it) {
146 ret.push_back(it->first);
147 }
148 return ret;
149}
150
Yangster7c334a12017-11-22 14:24:24 -0800151const pair<string, string> ConfigManager::GetConfigReceiver(const ConfigKey& key) const {
David Chen1d7b0cd2017-11-15 14:20:04 -0800152 auto it = mConfigReceivers.find(key);
153 if (it == mConfigReceivers.end()) {
154 return pair<string,string>();
155 } else {
156 return it->second;
157 }
158}
159
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700160void ConfigManager::Dump(FILE* out) {
161 fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
162 fprintf(out, " uid name\n");
163 for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin();
164 it != mConfigs.end(); it++) {
165 fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800166 auto receiverIt = mConfigReceivers.find(it->first);
167 if (receiverIt != mConfigReceivers.end()) {
168 fprintf(out, " -> received by %s, %s\n", receiverIt->second.first.c_str(),
169 receiverIt->second.second.c_str());
170 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700171 // TODO: Print the contents of the config too.
172 }
173}
174
yro87d983c2017-11-14 21:31:43 -0800175void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) {
176 mkdir(STATS_SERVICE_DIR, S_IRWXU);
yro9c98c052017-11-19 14:33:56 -0800177
178 // If there is a pre-existing config with same key we should first delete it.
179 remove_saved_configs(key);
180
181 // Then we save the latest config.
yro87d983c2017-11-14 21:31:43 -0800182 string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
183 key.GetName().c_str(), time(nullptr));
yro947fbce2017-11-15 22:50:23 -0800184 const int numBytes = config.ByteSize();
185 vector<uint8_t> buffer(numBytes);
186 config.SerializeToArray(&buffer[0], numBytes);
187 StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700188}
189
Yangster-mac756cd482017-11-21 21:58:44 -0800190StatsdConfig build_fake_config() {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700191 // HACK: Hard code a test metric for counting screen on events...
192 StatsdConfig config;
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800193 config.set_name("CONFIG_12345");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700194
Yao Chen5154a3792017-10-30 22:57:06 -0700195 int WAKE_LOCK_TAG_ID = 1111; // put a fake id here to make testing easier.
Yao Chen729093d2017-10-16 10:33:26 -0700196 int WAKE_LOCK_UID_KEY_ID = 1;
Yao Chen5154a3792017-10-30 22:57:06 -0700197 int WAKE_LOCK_NAME_KEY = 3;
198 int WAKE_LOCK_STATE_KEY = 4;
Yao Chen729093d2017-10-16 10:33:26 -0700199 int WAKE_LOCK_ACQUIRE_VALUE = 1;
200 int WAKE_LOCK_RELEASE_VALUE = 0;
201
202 int APP_USAGE_ID = 12345;
203 int APP_USAGE_UID_KEY_ID = 1;
204 int APP_USAGE_STATE_KEY = 2;
205 int APP_USAGE_FOREGROUND = 1;
206 int APP_USAGE_BACKGROUND = 0;
207
Bookatzc1a050a2017-10-10 15:49:28 -0700208 int SCREEN_EVENT_TAG_ID = 29;
Yao Chen729093d2017-10-16 10:33:26 -0700209 int SCREEN_EVENT_STATE_KEY = 1;
210 int SCREEN_EVENT_ON_VALUE = 2;
211 int SCREEN_EVENT_OFF_VALUE = 1;
212
Bookatzc1a050a2017-10-10 15:49:28 -0700213 int UID_PROCESS_STATE_TAG_ID = 27;
Yao Chen729093d2017-10-16 10:33:26 -0700214 int UID_PROCESS_STATE_UID_KEY = 1;
215
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700216 int KERNEL_WAKELOCK_TAG_ID = 1004;
Chenjie Yub3dda412017-10-24 13:41:59 -0700217 int KERNEL_WAKELOCK_NAME_KEY = 4;
218
Yangster1d4d6862017-10-31 12:58:51 -0700219 int DEVICE_TEMPERATURE_TAG_ID = 33;
220 int DEVICE_TEMPERATURE_KEY = 1;
221
Yao Chen729093d2017-10-16 10:33:26 -0700222 // Count Screen ON events.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700223 CountMetric* metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800224 metric->set_name("METRIC_1");
Yao Chen729093d2017-10-16 10:33:26 -0700225 metric->set_what("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700226 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
227
Bookatzd3606c72017-10-19 10:13:49 -0700228 // Anomaly threshold for screen-on count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800229 Alert* alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800230 alert->set_name("ALERT_1");
231 alert->set_metric_name("METRIC_1");
Bookatzd3606c72017-10-19 10:13:49 -0700232 alert->set_number_of_buckets(6);
233 alert->set_trigger_if_sum_gt(10);
234 alert->set_refractory_period_secs(30);
Bookatzd1fd2422017-11-22 15:21:03 -0800235 Alert::IncidentdDetails* details = alert->mutable_incidentd_details();
236 details->add_section(12);
237 details->add_section(13);
Bookatzd3606c72017-10-19 10:13:49 -0700238
Yao Chen729093d2017-10-16 10:33:26 -0700239 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700240 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800241 metric->set_name("METRIC_2");
Yao Chen729093d2017-10-16 10:33:26 -0700242 metric->set_what("PROCESS_STATE_CHANGE");
243 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
244 KeyMatcher* keyMatcher = metric->add_dimension();
245 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700246
Yang Lu3eba6212017-10-25 19:54:45 -0700247 // Anomaly threshold for background count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800248 alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800249 alert->set_name("ALERT_2");
250 alert->set_metric_name("METRIC_2");
Yang Lu3eba6212017-10-25 19:54:45 -0700251 alert->set_number_of_buckets(4);
252 alert->set_trigger_if_sum_gt(30);
253 alert->set_refractory_period_secs(20);
Bookatzd1fd2422017-11-22 15:21:03 -0800254 details = alert->mutable_incidentd_details();
255 details->add_section(14);
256 details->add_section(15);
Yang Lu3eba6212017-10-25 19:54:45 -0700257
Yao Chen729093d2017-10-16 10:33:26 -0700258 // Count process state changes, slice by uid, while SCREEN_IS_OFF
259 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800260 metric->set_name("METRIC_3");
Yao Chen729093d2017-10-16 10:33:26 -0700261 metric->set_what("PROCESS_STATE_CHANGE");
262 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
263 keyMatcher = metric->add_dimension();
264 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
265 metric->set_condition("SCREEN_IS_OFF");
266
Yao Chen5110bed2017-10-23 12:50:02 -0700267 // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
Yao Chen729093d2017-10-16 10:33:26 -0700268 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800269 metric->set_name("METRIC_4");
Yao Chen729093d2017-10-16 10:33:26 -0700270 metric->set_what("APP_GET_WL");
271 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
272 keyMatcher = metric->add_dimension();
273 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
274 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
275 EventConditionLink* link = metric->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
Yao Chen5154a3792017-10-30 22:57:06 -0700280 // Duration of an app holding any wl, while screen on and app in background, slice by uid
Yao Chen729093d2017-10-16 10:33:26 -0700281 DurationMetric* durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800282 durationMetric->set_name("METRIC_5");
Yao Chen729093d2017-10-16 10:33:26 -0700283 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800284 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen729093d2017-10-16 10:33:26 -0700285 keyMatcher = durationMetric->add_dimension();
286 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800287 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800288 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen729093d2017-10-16 10:33:26 -0700289 link = durationMetric->add_links();
290 link->set_condition("APP_IS_BACKGROUND");
291 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
292 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
293
Yao Chen5154a3792017-10-30 22:57:06 -0700294 // max Duration of an app holding any wl, while screen on and app in background, slice by uid
295 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800296 durationMetric->set_name("METRIC_6");
Yao Chen5154a3792017-10-30 22:57:06 -0700297 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800298 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen5154a3792017-10-30 22:57:06 -0700299 keyMatcher = durationMetric->add_dimension();
300 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800301 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800302 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700303 link = durationMetric->add_links();
304 link->set_condition("APP_IS_BACKGROUND");
305 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
306 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
307
308 // Duration of an app holding any wl, while screen on and app in background
309 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800310 durationMetric->set_name("METRIC_7");
Yao Chen5154a3792017-10-30 22:57:06 -0700311 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800312 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen967b2052017-11-07 16:36:43 -0800313 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800314 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700315 link = durationMetric->add_links();
316 link->set_condition("APP_IS_BACKGROUND");
317 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
318 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
319
320 // Duration of screen on time.
321 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800322 durationMetric->set_name("METRIC_8");
Yangster1d4d6862017-10-31 12:58:51 -0700323 durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800324 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen5154a3792017-10-30 22:57:06 -0700325 durationMetric->set_what("SCREEN_IS_ON");
326
Chenjie Yub3dda412017-10-24 13:41:59 -0700327 // Value metric to count KERNEL_WAKELOCK when screen turned on
328 ValueMetric* valueMetric = config.add_value_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800329 valueMetric->set_name("METRIC_6");
Chenjie Yub3dda412017-10-24 13:41:59 -0700330 valueMetric->set_what("KERNEL_WAKELOCK");
331 valueMetric->set_value_field(1);
332 valueMetric->set_condition("SCREEN_IS_ON");
333 keyMatcher = valueMetric->add_dimension();
334 keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY);
335 // This is for testing easier. We should never set bucket size this small.
336 valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
337
Yao Chen5110bed2017-10-23 12:50:02 -0700338 // Add an EventMetric to log process state change events.
339 EventMetric* eventMetric = config.add_event_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800340 eventMetric->set_name("METRIC_9");
Yao Chen5110bed2017-10-23 12:50:02 -0700341 eventMetric->set_what("SCREEN_TURNED_ON");
342
Yangster1d4d6862017-10-31 12:58:51 -0700343 // Add an GaugeMetric.
344 GaugeMetric* gaugeMetric = config.add_gauge_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800345 gaugeMetric->set_name("METRIC_10");
Yangster1d4d6862017-10-31 12:58:51 -0700346 gaugeMetric->set_what("DEVICE_TEMPERATURE");
347 gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
348 gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
349
Yao Chen729093d2017-10-16 10:33:26 -0700350 // Event matchers............
Yangster1d4d6862017-10-31 12:58:51 -0700351 LogEntryMatcher* temperatureEntryMatcher = config.add_log_entry_matcher();
352 temperatureEntryMatcher->set_name("DEVICE_TEMPERATURE");
353 temperatureEntryMatcher->mutable_simple_log_entry_matcher()->set_tag(
354 DEVICE_TEMPERATURE_TAG_ID);
355
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700356 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700357 eventMatcher->set_name("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700358 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700359 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
360 KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
361 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
362 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700363
364 eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700365 eventMatcher->set_name("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700366 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700367 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
368 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
369 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
370 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700371
Yao Chen729093d2017-10-16 10:33:26 -0700372 eventMatcher = config.add_log_entry_matcher();
373 eventMatcher->set_name("PROCESS_STATE_CHANGE");
374 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
375 simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700376
Yao Chen729093d2017-10-16 10:33:26 -0700377 eventMatcher = config.add_log_entry_matcher();
378 eventMatcher->set_name("APP_GOES_BACKGROUND");
379 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
380 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
381 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
382 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
383 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700384
Yao Chen729093d2017-10-16 10:33:26 -0700385 eventMatcher = config.add_log_entry_matcher();
386 eventMatcher->set_name("APP_GOES_FOREGROUND");
387 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
388 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
389 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
390 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
391 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700392
Yao Chen729093d2017-10-16 10:33:26 -0700393 eventMatcher = config.add_log_entry_matcher();
394 eventMatcher->set_name("APP_GET_WL");
395 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
396 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
397 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
398 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
399 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700400
Yao Chen729093d2017-10-16 10:33:26 -0700401 eventMatcher = config.add_log_entry_matcher();
402 eventMatcher->set_name("APP_RELEASE_WL");
403 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
404 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
405 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
406 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
407 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700408
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700409 // pulled events
410 eventMatcher = config.add_log_entry_matcher();
411 eventMatcher->set_name("KERNEL_WAKELOCK");
412 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
413 simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID);
414
Yao Chen729093d2017-10-16 10:33:26 -0700415 // Conditions.............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700416 Condition* condition = config.add_condition();
417 condition->set_name("SCREEN_IS_ON");
418 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700419 simpleCondition->set_start("SCREEN_TURNED_ON");
420 simpleCondition->set_stop("SCREEN_TURNED_OFF");
Yao Chen967b2052017-11-07 16:36:43 -0800421 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700422
423 condition = config.add_condition();
424 condition->set_name("SCREEN_IS_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700425 simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700426 simpleCondition->set_start("SCREEN_TURNED_OFF");
427 simpleCondition->set_stop("SCREEN_TURNED_ON");
Yao Chen967b2052017-11-07 16:36:43 -0800428 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700429
430 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700431 condition->set_name("APP_IS_BACKGROUND");
432 simpleCondition = condition->mutable_simple_condition();
433 simpleCondition->set_start("APP_GOES_BACKGROUND");
434 simpleCondition->set_stop("APP_GOES_FOREGROUND");
Yao Chen5154a3792017-10-30 22:57:06 -0700435 KeyMatcher* condition_dimension1 = simpleCondition->add_dimension();
436 condition_dimension1->set_key(APP_USAGE_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800437 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700438
439 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700440 condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
441 Condition_Combination* combination_condition = condition->mutable_combination();
442 combination_condition->set_operation(LogicalOperation::AND);
443 combination_condition->add_condition("APP_IS_BACKGROUND");
444 combination_condition->add_condition("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700445
Yao Chen5154a3792017-10-30 22:57:06 -0700446 condition = config.add_condition();
Yao Chen967b2052017-11-07 16:36:43 -0800447 condition->set_name("WL_HELD_PER_APP_PER_NAME");
Yao Chen5154a3792017-10-30 22:57:06 -0700448 simpleCondition = condition->mutable_simple_condition();
449 simpleCondition->set_start("APP_GET_WL");
450 simpleCondition->set_stop("APP_RELEASE_WL");
451 KeyMatcher* condition_dimension = simpleCondition->add_dimension();
452 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
453 condition_dimension = simpleCondition->add_dimension();
454 condition_dimension->set_key(WAKE_LOCK_NAME_KEY);
Yao Chen967b2052017-11-07 16:36:43 -0800455 simpleCondition->set_count_nesting(true);
456
457 condition = config.add_condition();
458 condition->set_name("WL_HELD_PER_APP");
459 simpleCondition = condition->mutable_simple_condition();
460 simpleCondition->set_start("APP_GET_WL");
461 simpleCondition->set_stop("APP_RELEASE_WL");
462 simpleCondition->set_initial_value(SimpleCondition_InitialValue_FALSE);
463 condition_dimension = simpleCondition->add_dimension();
464 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
465 simpleCondition->set_count_nesting(true);
Yao Chen5154a3792017-10-30 22:57:06 -0700466
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700467 return config;
468}
469
470} // namespace statsd
471} // namespace os
472} // namespace android