blob: 21256097a76635ad3b6debeb4dc77d94cbd205c9 [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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070033static StatsdConfig build_fake_config();
34
yro87d983c2017-11-14 21:31:43 -080035using android::base::StringPrintf;
36using std::unique_ptr;
37
Joe Onorato9fc9edf2017-10-15 20:08:52 -070038ConfigManager::ConfigManager() {
39}
40
41ConfigManager::~ConfigManager() {
42}
43
44void ConfigManager::Startup() {
yro87d983c2017-11-14 21:31:43 -080045 readConfigFromDisk();
Joe Onorato9fc9edf2017-10-15 20:08:52 -070046
47 // this should be called from StatsService when it receives a statsd_config
Yao Chen7ee94152017-11-17 09:44:40 -080048 UpdateConfig(ConfigKey(1000, "fake"), build_fake_config());
Joe Onorato9fc9edf2017-10-15 20:08:52 -070049}
50
51void ConfigManager::AddListener(const sp<ConfigListener>& listener) {
52 mListeners.push_back(listener);
53}
54
55void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
56 // Add to map
57 mConfigs[key] = config;
58 // Why doesn't this work? mConfigs.insert({key, config});
59
60 // Save to disk
yro87d983c2017-11-14 21:31:43 -080061 update_saved_configs(key, config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070062
63 // Tell everyone
64 for (auto& listener : mListeners) {
65 listener->OnConfigUpdated(key, config);
66 }
67}
68
David Chenadaf8b32017-11-03 15:42:08 -070069void ConfigManager::SetConfigReceiver(const ConfigKey& key, const string& pkg, const string& cls) {
70 mConfigReceivers[key] = pair<string, string>(pkg, cls);
71}
72
73void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) {
74 mConfigReceivers.erase(key);
75}
76
Joe Onorato9fc9edf2017-10-15 20:08:52 -070077void ConfigManager::RemoveConfig(const ConfigKey& key) {
78 unordered_map<ConfigKey, StatsdConfig>::iterator it = mConfigs.find(key);
79 if (it != mConfigs.end()) {
80 // Remove from map
81 mConfigs.erase(it);
82
Joe Onorato9fc9edf2017-10-15 20:08:52 -070083 // Tell everyone
84 for (auto& listener : mListeners) {
85 listener->OnConfigRemoved(key);
86 }
87 }
yro9c98c052017-11-19 14:33:56 -080088
89 // Remove from disk. There can still be a lingering file on disk so we check
90 // whether or not the config was on memory.
91 remove_saved_configs(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070092}
93
yro87d983c2017-11-14 21:31:43 -080094void ConfigManager::remove_saved_configs(const ConfigKey& key) {
95 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
96 if (dir == NULL) {
97 ALOGD("no default config on disk");
98 return;
99 }
100 string prefix = StringPrintf("%d-%s", key.GetUid(), key.GetName().c_str());
101 dirent* de;
102 while ((de = readdir(dir.get()))) {
103 char* name = de->d_name;
104 if (name[0] != '.' && strncmp(name, prefix.c_str(), prefix.size()) == 0) {
yro9c98c052017-11-19 14:33:56 -0800105 if (remove(StringPrintf("%s/%s", STATS_SERVICE_DIR, name).c_str()) != 0) {
yro87d983c2017-11-14 21:31:43 -0800106 ALOGD("no file found");
107 }
108 }
109 }
110}
111
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700112void ConfigManager::RemoveConfigs(int uid) {
113 vector<ConfigKey> removed;
114
115 for (auto it = mConfigs.begin(); it != mConfigs.end();) {
116 // Remove from map
117 if (it->first.GetUid() == uid) {
118 removed.push_back(it->first);
119 it = mConfigs.erase(it);
David Chenadaf8b32017-11-03 15:42:08 -0700120 mConfigReceivers.erase(it->first);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700121 } else {
122 it++;
123 }
124 }
125
126 // Remove separately so if they do anything in the callback they can't mess up our iteration.
127 for (auto& key : removed) {
128 // Tell everyone
129 for (auto& listener : mListeners) {
130 listener->OnConfigRemoved(key);
131 }
132 }
133}
134
David Chen1d7b0cd2017-11-15 14:20:04 -0800135vector<ConfigKey> ConfigManager::GetAllConfigKeys() {
136 vector<ConfigKey> ret;
137 for (auto it = mConfigs.cbegin(); it != mConfigs.cend(); ++it) {
138 ret.push_back(it->first);
139 }
140 return ret;
141}
142
143const pair<string, string> ConfigManager::GetConfigReceiver(const ConfigKey& key) {
144 auto it = mConfigReceivers.find(key);
145 if (it == mConfigReceivers.end()) {
146 return pair<string,string>();
147 } else {
148 return it->second;
149 }
150}
151
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700152void ConfigManager::Dump(FILE* out) {
153 fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
154 fprintf(out, " uid name\n");
155 for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin();
156 it != mConfigs.end(); it++) {
157 fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800158 auto receiverIt = mConfigReceivers.find(it->first);
159 if (receiverIt != mConfigReceivers.end()) {
160 fprintf(out, " -> received by %s, %s\n", receiverIt->second.first.c_str(),
161 receiverIt->second.second.c_str());
162 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700163 // TODO: Print the contents of the config too.
164 }
165}
166
yro87d983c2017-11-14 21:31:43 -0800167void ConfigManager::readConfigFromDisk() {
168 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
169 if (dir == NULL) {
170 ALOGD("no default config on disk");
171 return;
172 }
173
174 dirent* de;
175 while ((de = readdir(dir.get()))) {
176 char* name = de->d_name;
177 if (name[0] == '.') continue;
178 ALOGD("file %s", name);
179
180 int index = 0;
181 int uid = 0;
182 string configName;
183 char* substr = strtok(name, "-");
184 // Timestamp lives at index 2 but we skip parsing it as it's not needed.
185 while (substr != nullptr && index < 2) {
186 if (index) {
187 uid = atoi(substr);
188 } else {
189 configName = substr;
190 }
191 index++;
192 }
193 if (index < 2) continue;
194 string file_name = StringPrintf("%s/%s", STATS_SERVICE_DIR, name);
195 ALOGD("full file %s", file_name.c_str());
196 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
197 if (fd != -1) {
198 string content;
199 if (android::base::ReadFdToString(fd, &content)) {
200 StatsdConfig config;
201 if (config.ParseFromString(content)) {
202 mConfigs[ConfigKey(uid, configName)] = config;
203 ALOGD("map key uid=%d|name=%s", uid, name);
204 }
205 }
206 close(fd);
207 }
208 }
209}
210
211void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) {
212 mkdir(STATS_SERVICE_DIR, S_IRWXU);
yro9c98c052017-11-19 14:33:56 -0800213
214 // If there is a pre-existing config with same key we should first delete it.
215 remove_saved_configs(key);
216
217 // Then we save the latest config.
yro87d983c2017-11-14 21:31:43 -0800218 string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
219 key.GetName().c_str(), time(nullptr));
220 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
221 if (fd != -1) {
222 const int numBytes = config.ByteSize();
223 vector<uint8_t> buffer(numBytes);
224 config.SerializeToArray(&buffer[0], numBytes);
225 int result = write(fd, &buffer[0], numBytes);
226 close(fd);
227 bool wroteKey = (result == numBytes);
228 ALOGD("wrote to file %d", wroteKey);
229 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700230}
231
232static StatsdConfig build_fake_config() {
233 // HACK: Hard code a test metric for counting screen on events...
234 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800235 config.set_name("12345");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700236
Yao Chen5154a3792017-10-30 22:57:06 -0700237 int WAKE_LOCK_TAG_ID = 1111; // put a fake id here to make testing easier.
Yao Chen729093d2017-10-16 10:33:26 -0700238 int WAKE_LOCK_UID_KEY_ID = 1;
Yao Chen5154a3792017-10-30 22:57:06 -0700239 int WAKE_LOCK_NAME_KEY = 3;
240 int WAKE_LOCK_STATE_KEY = 4;
Yao Chen729093d2017-10-16 10:33:26 -0700241 int WAKE_LOCK_ACQUIRE_VALUE = 1;
242 int WAKE_LOCK_RELEASE_VALUE = 0;
243
244 int APP_USAGE_ID = 12345;
245 int APP_USAGE_UID_KEY_ID = 1;
246 int APP_USAGE_STATE_KEY = 2;
247 int APP_USAGE_FOREGROUND = 1;
248 int APP_USAGE_BACKGROUND = 0;
249
Bookatzc1a050a2017-10-10 15:49:28 -0700250 int SCREEN_EVENT_TAG_ID = 29;
Yao Chen729093d2017-10-16 10:33:26 -0700251 int SCREEN_EVENT_STATE_KEY = 1;
252 int SCREEN_EVENT_ON_VALUE = 2;
253 int SCREEN_EVENT_OFF_VALUE = 1;
254
Bookatzc1a050a2017-10-10 15:49:28 -0700255 int UID_PROCESS_STATE_TAG_ID = 27;
Yao Chen729093d2017-10-16 10:33:26 -0700256 int UID_PROCESS_STATE_UID_KEY = 1;
257
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700258 int KERNEL_WAKELOCK_TAG_ID = 1004;
Chenjie Yub3dda412017-10-24 13:41:59 -0700259 int KERNEL_WAKELOCK_NAME_KEY = 4;
260
Yangster1d4d6862017-10-31 12:58:51 -0700261 int DEVICE_TEMPERATURE_TAG_ID = 33;
262 int DEVICE_TEMPERATURE_KEY = 1;
263
Yao Chen729093d2017-10-16 10:33:26 -0700264 // Count Screen ON events.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700265 CountMetric* metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800266 metric->set_name("1");
Yao Chen729093d2017-10-16 10:33:26 -0700267 metric->set_what("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700268 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
269
Bookatzd3606c72017-10-19 10:13:49 -0700270 // Anomaly threshold for screen-on count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800271 Alert* alert = config.add_alert();
Yangster-macd1815dc2017-11-13 21:43:15 -0800272 alert->set_name("1");
Bookatzd3606c72017-10-19 10:13:49 -0700273 alert->set_number_of_buckets(6);
274 alert->set_trigger_if_sum_gt(10);
275 alert->set_refractory_period_secs(30);
276
Yao Chen729093d2017-10-16 10:33:26 -0700277 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700278 metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800279 metric->set_name("2");
Yao Chen729093d2017-10-16 10:33:26 -0700280 metric->set_what("PROCESS_STATE_CHANGE");
281 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
282 KeyMatcher* keyMatcher = metric->add_dimension();
283 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700284
Yang Lu3eba6212017-10-25 19:54:45 -0700285 // Anomaly threshold for background count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800286 alert = config.add_alert();
Yangster-macd1815dc2017-11-13 21:43:15 -0800287 alert->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800294 metric->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800303 metric->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800316 durationMetric->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800330 durationMetric->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800344 durationMetric->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800356 durationMetric->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800363 valueMetric->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800374 eventMetric->set_name("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();
Yangster-macd1815dc2017-11-13 21:43:15 -0800379 gaugeMetric->set_name("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