blob: 408eeda67b95eb006fa46c06b725dd7afd5eae10 [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"
18
19#include "stats_util.h"
20
yro87d983c2017-11-14 21:31:43 -080021#include <android-base/file.h>
22#include <dirent.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include <stdio.h>
yro87d983c2017-11-14 21:31:43 -080024#include <vector>
25#include "android-base/stringprintf.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026
27namespace android {
28namespace os {
29namespace statsd {
30
yro87d983c2017-11-14 21:31:43 -080031#define STATS_SERVICE_DIR "/data/system/stats-service"
32
yro87d983c2017-11-14 21:31:43 -080033using android::base::StringPrintf;
34using std::unique_ptr;
35
Joe Onorato9fc9edf2017-10-15 20:08:52 -070036ConfigManager::ConfigManager() {
37}
38
39ConfigManager::~ConfigManager() {
40}
41
42void ConfigManager::Startup() {
yro87d983c2017-11-14 21:31:43 -080043 readConfigFromDisk();
Joe Onorato9fc9edf2017-10-15 20:08:52 -070044
45 // this should be called from StatsService when it receives a statsd_config
Yao Chen7ee94152017-11-17 09:44:40 -080046 UpdateConfig(ConfigKey(1000, "fake"), build_fake_config());
Joe Onorato9fc9edf2017-10-15 20:08:52 -070047}
48
49void ConfigManager::AddListener(const sp<ConfigListener>& listener) {
50 mListeners.push_back(listener);
51}
52
53void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
54 // Add to map
55 mConfigs[key] = config;
56 // Why doesn't this work? mConfigs.insert({key, config});
57
58 // Save to disk
yro87d983c2017-11-14 21:31:43 -080059 update_saved_configs(key, config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070060
61 // Tell everyone
62 for (auto& listener : mListeners) {
63 listener->OnConfigUpdated(key, config);
64 }
65}
66
David Chenadaf8b32017-11-03 15:42:08 -070067void ConfigManager::SetConfigReceiver(const ConfigKey& key, const string& pkg, const string& cls) {
68 mConfigReceivers[key] = pair<string, string>(pkg, cls);
69}
70
71void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) {
72 mConfigReceivers.erase(key);
73}
74
Joe Onorato9fc9edf2017-10-15 20:08:52 -070075void ConfigManager::RemoveConfig(const ConfigKey& key) {
76 unordered_map<ConfigKey, StatsdConfig>::iterator it = mConfigs.find(key);
77 if (it != mConfigs.end()) {
78 // Remove from map
79 mConfigs.erase(it);
80
Joe Onorato9fc9edf2017-10-15 20:08:52 -070081 // Tell everyone
82 for (auto& listener : mListeners) {
83 listener->OnConfigRemoved(key);
84 }
85 }
yro9c98c052017-11-19 14:33:56 -080086
87 // Remove from disk. There can still be a lingering file on disk so we check
88 // whether or not the config was on memory.
89 remove_saved_configs(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070090}
91
yro87d983c2017-11-14 21:31:43 -080092void ConfigManager::remove_saved_configs(const ConfigKey& key) {
93 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
94 if (dir == NULL) {
95 ALOGD("no default config on disk");
96 return;
97 }
98 string prefix = StringPrintf("%d-%s", key.GetUid(), key.GetName().c_str());
99 dirent* de;
100 while ((de = readdir(dir.get()))) {
101 char* name = de->d_name;
102 if (name[0] != '.' && strncmp(name, prefix.c_str(), prefix.size()) == 0) {
yro9c98c052017-11-19 14:33:56 -0800103 if (remove(StringPrintf("%s/%s", STATS_SERVICE_DIR, name).c_str()) != 0) {
yro87d983c2017-11-14 21:31:43 -0800104 ALOGD("no file found");
105 }
106 }
107 }
108}
109
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700110void ConfigManager::RemoveConfigs(int uid) {
111 vector<ConfigKey> removed;
112
113 for (auto it = mConfigs.begin(); it != mConfigs.end();) {
114 // Remove from map
115 if (it->first.GetUid() == uid) {
116 removed.push_back(it->first);
117 it = mConfigs.erase(it);
David Chenadaf8b32017-11-03 15:42:08 -0700118 mConfigReceivers.erase(it->first);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700119 } else {
120 it++;
121 }
122 }
123
124 // Remove separately so if they do anything in the callback they can't mess up our iteration.
125 for (auto& key : removed) {
126 // Tell everyone
127 for (auto& listener : mListeners) {
128 listener->OnConfigRemoved(key);
129 }
130 }
131}
132
David Chen1d7b0cd2017-11-15 14:20:04 -0800133vector<ConfigKey> ConfigManager::GetAllConfigKeys() {
134 vector<ConfigKey> ret;
135 for (auto it = mConfigs.cbegin(); it != mConfigs.cend(); ++it) {
136 ret.push_back(it->first);
137 }
138 return ret;
139}
140
141const pair<string, string> ConfigManager::GetConfigReceiver(const ConfigKey& key) {
142 auto it = mConfigReceivers.find(key);
143 if (it == mConfigReceivers.end()) {
144 return pair<string,string>();
145 } else {
146 return it->second;
147 }
148}
149
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700150void ConfigManager::Dump(FILE* out) {
151 fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
152 fprintf(out, " uid name\n");
153 for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin();
154 it != mConfigs.end(); it++) {
155 fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800156 auto receiverIt = mConfigReceivers.find(it->first);
157 if (receiverIt != mConfigReceivers.end()) {
158 fprintf(out, " -> received by %s, %s\n", receiverIt->second.first.c_str(),
159 receiverIt->second.second.c_str());
160 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700161 // TODO: Print the contents of the config too.
162 }
163}
164
yro87d983c2017-11-14 21:31:43 -0800165void ConfigManager::readConfigFromDisk() {
166 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
167 if (dir == NULL) {
168 ALOGD("no default config on disk");
169 return;
170 }
171
172 dirent* de;
173 while ((de = readdir(dir.get()))) {
174 char* name = de->d_name;
175 if (name[0] == '.') continue;
176 ALOGD("file %s", name);
177
178 int index = 0;
179 int uid = 0;
180 string configName;
181 char* substr = strtok(name, "-");
182 // Timestamp lives at index 2 but we skip parsing it as it's not needed.
183 while (substr != nullptr && index < 2) {
184 if (index) {
185 uid = atoi(substr);
186 } else {
187 configName = substr;
188 }
189 index++;
190 }
191 if (index < 2) continue;
192 string file_name = StringPrintf("%s/%s", STATS_SERVICE_DIR, name);
193 ALOGD("full file %s", file_name.c_str());
194 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
195 if (fd != -1) {
196 string content;
197 if (android::base::ReadFdToString(fd, &content)) {
198 StatsdConfig config;
199 if (config.ParseFromString(content)) {
200 mConfigs[ConfigKey(uid, configName)] = config;
201 ALOGD("map key uid=%d|name=%s", uid, name);
202 }
203 }
204 close(fd);
205 }
206 }
207}
208
209void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) {
210 mkdir(STATS_SERVICE_DIR, S_IRWXU);
yro9c98c052017-11-19 14:33:56 -0800211
212 // If there is a pre-existing config with same key we should first delete it.
213 remove_saved_configs(key);
214
215 // Then we save the latest config.
yro87d983c2017-11-14 21:31:43 -0800216 string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
217 key.GetName().c_str(), time(nullptr));
218 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
219 if (fd != -1) {
220 const int numBytes = config.ByteSize();
221 vector<uint8_t> buffer(numBytes);
222 config.SerializeToArray(&buffer[0], numBytes);
223 int result = write(fd, &buffer[0], numBytes);
224 close(fd);
225 bool wroteKey = (result == numBytes);
226 ALOGD("wrote to file %d", wroteKey);
227 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700228}
229
Yangster-mac756cd482017-11-21 21:58:44 -0800230StatsdConfig build_fake_config() {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700231 // HACK: Hard code a test metric for counting screen on events...
232 StatsdConfig config;
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800233 config.set_name("CONFIG_12345");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700234
Yao Chen5154a3792017-10-30 22:57:06 -0700235 int WAKE_LOCK_TAG_ID = 1111; // put a fake id here to make testing easier.
Yao Chen729093d2017-10-16 10:33:26 -0700236 int WAKE_LOCK_UID_KEY_ID = 1;
Yao Chen5154a3792017-10-30 22:57:06 -0700237 int WAKE_LOCK_NAME_KEY = 3;
238 int WAKE_LOCK_STATE_KEY = 4;
Yao Chen729093d2017-10-16 10:33:26 -0700239 int WAKE_LOCK_ACQUIRE_VALUE = 1;
240 int WAKE_LOCK_RELEASE_VALUE = 0;
241
242 int APP_USAGE_ID = 12345;
243 int APP_USAGE_UID_KEY_ID = 1;
244 int APP_USAGE_STATE_KEY = 2;
245 int APP_USAGE_FOREGROUND = 1;
246 int APP_USAGE_BACKGROUND = 0;
247
Bookatzc1a050a2017-10-10 15:49:28 -0700248 int SCREEN_EVENT_TAG_ID = 29;
Yao Chen729093d2017-10-16 10:33:26 -0700249 int SCREEN_EVENT_STATE_KEY = 1;
250 int SCREEN_EVENT_ON_VALUE = 2;
251 int SCREEN_EVENT_OFF_VALUE = 1;
252
Bookatzc1a050a2017-10-10 15:49:28 -0700253 int UID_PROCESS_STATE_TAG_ID = 27;
Yao Chen729093d2017-10-16 10:33:26 -0700254 int UID_PROCESS_STATE_UID_KEY = 1;
255
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700256 int KERNEL_WAKELOCK_TAG_ID = 1004;
Chenjie Yub3dda412017-10-24 13:41:59 -0700257 int KERNEL_WAKELOCK_NAME_KEY = 4;
258
Yangster1d4d6862017-10-31 12:58:51 -0700259 int DEVICE_TEMPERATURE_TAG_ID = 33;
260 int DEVICE_TEMPERATURE_KEY = 1;
261
Yao Chen729093d2017-10-16 10:33:26 -0700262 // Count Screen ON events.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700263 CountMetric* metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800264 metric->set_name("METRIC_1");
Yao Chen729093d2017-10-16 10:33:26 -0700265 metric->set_what("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700266 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
267
Bookatzd3606c72017-10-19 10:13:49 -0700268 // Anomaly threshold for screen-on count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800269 Alert* alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800270 alert->set_name("ALERT_1");
271 alert->set_metric_name("METRIC_1");
Bookatzd3606c72017-10-19 10:13:49 -0700272 alert->set_number_of_buckets(6);
273 alert->set_trigger_if_sum_gt(10);
274 alert->set_refractory_period_secs(30);
275
Yao Chen729093d2017-10-16 10:33:26 -0700276 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700277 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800278 metric->set_name("METRIC_2");
Yao Chen729093d2017-10-16 10:33:26 -0700279 metric->set_what("PROCESS_STATE_CHANGE");
280 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
281 KeyMatcher* keyMatcher = metric->add_dimension();
282 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700283
Yang Lu3eba6212017-10-25 19:54:45 -0700284 // Anomaly threshold for background count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800285 alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800286 alert->set_name("ALERT_2");
287 alert->set_metric_name("METRIC_2");
Yang Lu3eba6212017-10-25 19:54:45 -0700288 alert->set_number_of_buckets(4);
289 alert->set_trigger_if_sum_gt(30);
290 alert->set_refractory_period_secs(20);
291
Yao Chen729093d2017-10-16 10:33:26 -0700292 // Count process state changes, slice by uid, while SCREEN_IS_OFF
293 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800294 metric->set_name("METRIC_3");
Yao Chen729093d2017-10-16 10:33:26 -0700295 metric->set_what("PROCESS_STATE_CHANGE");
296 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
297 keyMatcher = metric->add_dimension();
298 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
299 metric->set_condition("SCREEN_IS_OFF");
300
Yao Chen5110bed2017-10-23 12:50:02 -0700301 // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
Yao Chen729093d2017-10-16 10:33:26 -0700302 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800303 metric->set_name("METRIC_4");
Yao Chen729093d2017-10-16 10:33:26 -0700304 metric->set_what("APP_GET_WL");
305 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
306 keyMatcher = metric->add_dimension();
307 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
308 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
309 EventConditionLink* link = metric->add_links();
310 link->set_condition("APP_IS_BACKGROUND");
311 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
312 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
313
Yao Chen5154a3792017-10-30 22:57:06 -0700314 // Duration of an app holding any wl, while screen on and app in background, slice by uid
Yao Chen729093d2017-10-16 10:33:26 -0700315 DurationMetric* durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800316 durationMetric->set_name("METRIC_5");
Yao Chen729093d2017-10-16 10:33:26 -0700317 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800318 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen729093d2017-10-16 10:33:26 -0700319 keyMatcher = durationMetric->add_dimension();
320 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800321 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800322 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen729093d2017-10-16 10:33:26 -0700323 link = durationMetric->add_links();
324 link->set_condition("APP_IS_BACKGROUND");
325 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
326 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
327
Yao Chen5154a3792017-10-30 22:57:06 -0700328 // max Duration of an app holding any wl, while screen on and app in background, slice by uid
329 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800330 durationMetric->set_name("METRIC_6");
Yao Chen5154a3792017-10-30 22:57:06 -0700331 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800332 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen5154a3792017-10-30 22:57:06 -0700333 keyMatcher = durationMetric->add_dimension();
334 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800335 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800336 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700337 link = durationMetric->add_links();
338 link->set_condition("APP_IS_BACKGROUND");
339 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
340 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
341
342 // Duration of an app holding any wl, while screen on and app in background
343 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800344 durationMetric->set_name("METRIC_7");
Yao Chen5154a3792017-10-30 22:57:06 -0700345 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800346 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen967b2052017-11-07 16:36:43 -0800347 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800348 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700349 link = durationMetric->add_links();
350 link->set_condition("APP_IS_BACKGROUND");
351 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
352 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
353
354 // Duration of screen on time.
355 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800356 durationMetric->set_name("METRIC_8");
Yangster1d4d6862017-10-31 12:58:51 -0700357 durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800358 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen5154a3792017-10-30 22:57:06 -0700359 durationMetric->set_what("SCREEN_IS_ON");
360
Chenjie Yub3dda412017-10-24 13:41:59 -0700361 // Value metric to count KERNEL_WAKELOCK when screen turned on
362 ValueMetric* valueMetric = config.add_value_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800363 valueMetric->set_name("METRIC_6");
Chenjie Yub3dda412017-10-24 13:41:59 -0700364 valueMetric->set_what("KERNEL_WAKELOCK");
365 valueMetric->set_value_field(1);
366 valueMetric->set_condition("SCREEN_IS_ON");
367 keyMatcher = valueMetric->add_dimension();
368 keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY);
369 // This is for testing easier. We should never set bucket size this small.
370 valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
371
Yao Chen5110bed2017-10-23 12:50:02 -0700372 // Add an EventMetric to log process state change events.
373 EventMetric* eventMetric = config.add_event_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800374 eventMetric->set_name("METRIC_9");
Yao Chen5110bed2017-10-23 12:50:02 -0700375 eventMetric->set_what("SCREEN_TURNED_ON");
376
Yangster1d4d6862017-10-31 12:58:51 -0700377 // Add an GaugeMetric.
378 GaugeMetric* gaugeMetric = config.add_gauge_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800379 gaugeMetric->set_name("METRIC_10");
Yangster1d4d6862017-10-31 12:58:51 -0700380 gaugeMetric->set_what("DEVICE_TEMPERATURE");
381 gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
382 gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
383
Yao Chen729093d2017-10-16 10:33:26 -0700384 // Event matchers............
Yangster1d4d6862017-10-31 12:58:51 -0700385 LogEntryMatcher* temperatureEntryMatcher = config.add_log_entry_matcher();
386 temperatureEntryMatcher->set_name("DEVICE_TEMPERATURE");
387 temperatureEntryMatcher->mutable_simple_log_entry_matcher()->set_tag(
388 DEVICE_TEMPERATURE_TAG_ID);
389
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700390 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700391 eventMatcher->set_name("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700392 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700393 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
394 KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
395 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
396 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700397
398 eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700399 eventMatcher->set_name("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700400 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700401 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
402 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
403 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
404 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700405
Yao Chen729093d2017-10-16 10:33:26 -0700406 eventMatcher = config.add_log_entry_matcher();
407 eventMatcher->set_name("PROCESS_STATE_CHANGE");
408 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
409 simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700410
Yao Chen729093d2017-10-16 10:33:26 -0700411 eventMatcher = config.add_log_entry_matcher();
412 eventMatcher->set_name("APP_GOES_BACKGROUND");
413 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
414 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
415 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
416 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
417 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700418
Yao Chen729093d2017-10-16 10:33:26 -0700419 eventMatcher = config.add_log_entry_matcher();
420 eventMatcher->set_name("APP_GOES_FOREGROUND");
421 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
422 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
423 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
424 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
425 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700426
Yao Chen729093d2017-10-16 10:33:26 -0700427 eventMatcher = config.add_log_entry_matcher();
428 eventMatcher->set_name("APP_GET_WL");
429 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
430 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
431 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
432 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
433 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700434
Yao Chen729093d2017-10-16 10:33:26 -0700435 eventMatcher = config.add_log_entry_matcher();
436 eventMatcher->set_name("APP_RELEASE_WL");
437 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
438 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
439 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
440 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
441 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700442
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700443 // pulled events
444 eventMatcher = config.add_log_entry_matcher();
445 eventMatcher->set_name("KERNEL_WAKELOCK");
446 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
447 simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID);
448
Yao Chen729093d2017-10-16 10:33:26 -0700449 // Conditions.............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700450 Condition* condition = config.add_condition();
451 condition->set_name("SCREEN_IS_ON");
452 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700453 simpleCondition->set_start("SCREEN_TURNED_ON");
454 simpleCondition->set_stop("SCREEN_TURNED_OFF");
Yao Chen967b2052017-11-07 16:36:43 -0800455 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700456
457 condition = config.add_condition();
458 condition->set_name("SCREEN_IS_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700459 simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700460 simpleCondition->set_start("SCREEN_TURNED_OFF");
461 simpleCondition->set_stop("SCREEN_TURNED_ON");
Yao Chen967b2052017-11-07 16:36:43 -0800462 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700463
464 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700465 condition->set_name("APP_IS_BACKGROUND");
466 simpleCondition = condition->mutable_simple_condition();
467 simpleCondition->set_start("APP_GOES_BACKGROUND");
468 simpleCondition->set_stop("APP_GOES_FOREGROUND");
Yao Chen5154a3792017-10-30 22:57:06 -0700469 KeyMatcher* condition_dimension1 = simpleCondition->add_dimension();
470 condition_dimension1->set_key(APP_USAGE_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800471 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700472
473 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700474 condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
475 Condition_Combination* combination_condition = condition->mutable_combination();
476 combination_condition->set_operation(LogicalOperation::AND);
477 combination_condition->add_condition("APP_IS_BACKGROUND");
478 combination_condition->add_condition("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700479
Yao Chen5154a3792017-10-30 22:57:06 -0700480 condition = config.add_condition();
Yao Chen967b2052017-11-07 16:36:43 -0800481 condition->set_name("WL_HELD_PER_APP_PER_NAME");
Yao Chen5154a3792017-10-30 22:57:06 -0700482 simpleCondition = condition->mutable_simple_condition();
483 simpleCondition->set_start("APP_GET_WL");
484 simpleCondition->set_stop("APP_RELEASE_WL");
485 KeyMatcher* condition_dimension = simpleCondition->add_dimension();
486 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
487 condition_dimension = simpleCondition->add_dimension();
488 condition_dimension->set_key(WAKE_LOCK_NAME_KEY);
Yao Chen967b2052017-11-07 16:36:43 -0800489 simpleCondition->set_count_nesting(true);
490
491 condition = config.add_condition();
492 condition->set_name("WL_HELD_PER_APP");
493 simpleCondition = condition->mutable_simple_condition();
494 simpleCondition->set_start("APP_GET_WL");
495 simpleCondition->set_stop("APP_RELEASE_WL");
496 simpleCondition->set_initial_value(SimpleCondition_InitialValue_FALSE);
497 condition_dimension = simpleCondition->add_dimension();
498 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
499 simpleCondition->set_count_nesting(true);
Yao Chen5154a3792017-10-30 22:57:06 -0700500
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700501 return config;
502}
503
504} // namespace statsd
505} // namespace os
506} // namespace android