blob: 0c9252e2095f85ca853e2755ed19e023a5688bab [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;
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800235 config.set_name("CONFIG_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();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800266 metric->set_name("METRIC_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();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800272 alert->set_name("ALERT_1");
273 alert->set_metric_name("METRIC_1");
Bookatzd3606c72017-10-19 10:13:49 -0700274 alert->set_number_of_buckets(6);
275 alert->set_trigger_if_sum_gt(10);
276 alert->set_refractory_period_secs(30);
277
Yao Chen729093d2017-10-16 10:33:26 -0700278 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700279 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800280 metric->set_name("METRIC_2");
Yao Chen729093d2017-10-16 10:33:26 -0700281 metric->set_what("PROCESS_STATE_CHANGE");
282 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
283 KeyMatcher* keyMatcher = metric->add_dimension();
284 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700285
Yang Lu3eba6212017-10-25 19:54:45 -0700286 // Anomaly threshold for background count.
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800287 alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800288 alert->set_name("ALERT_2");
289 alert->set_metric_name("METRIC_2");
Yang Lu3eba6212017-10-25 19:54:45 -0700290 alert->set_number_of_buckets(4);
291 alert->set_trigger_if_sum_gt(30);
292 alert->set_refractory_period_secs(20);
293
Yao Chen729093d2017-10-16 10:33:26 -0700294 // Count process state changes, slice by uid, while SCREEN_IS_OFF
295 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800296 metric->set_name("METRIC_3");
Yao Chen729093d2017-10-16 10:33:26 -0700297 metric->set_what("PROCESS_STATE_CHANGE");
298 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
299 keyMatcher = metric->add_dimension();
300 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
301 metric->set_condition("SCREEN_IS_OFF");
302
Yao Chen5110bed2017-10-23 12:50:02 -0700303 // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
Yao Chen729093d2017-10-16 10:33:26 -0700304 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800305 metric->set_name("METRIC_4");
Yao Chen729093d2017-10-16 10:33:26 -0700306 metric->set_what("APP_GET_WL");
307 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
308 keyMatcher = metric->add_dimension();
309 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
310 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
311 EventConditionLink* link = metric->add_links();
312 link->set_condition("APP_IS_BACKGROUND");
313 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
314 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
315
Yao Chen5154a3792017-10-30 22:57:06 -0700316 // Duration of an app holding any wl, while screen on and app in background, slice by uid
Yao Chen729093d2017-10-16 10:33:26 -0700317 DurationMetric* durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800318 durationMetric->set_name("METRIC_5");
Yao Chen729093d2017-10-16 10:33:26 -0700319 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800320 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen729093d2017-10-16 10:33:26 -0700321 keyMatcher = durationMetric->add_dimension();
322 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800323 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800324 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen729093d2017-10-16 10:33:26 -0700325 link = durationMetric->add_links();
326 link->set_condition("APP_IS_BACKGROUND");
327 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
328 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
329
Yao Chen5154a3792017-10-30 22:57:06 -0700330 // max Duration of an app holding any wl, while screen on and app in background, slice by uid
331 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800332 durationMetric->set_name("METRIC_6");
Yao Chen5154a3792017-10-30 22:57:06 -0700333 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800334 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen5154a3792017-10-30 22:57:06 -0700335 keyMatcher = durationMetric->add_dimension();
336 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800337 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800338 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700339 link = durationMetric->add_links();
340 link->set_condition("APP_IS_BACKGROUND");
341 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
342 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
343
344 // Duration of an app holding any wl, while screen on and app in background
345 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800346 durationMetric->set_name("METRIC_7");
Yao Chen5154a3792017-10-30 22:57:06 -0700347 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800348 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen967b2052017-11-07 16:36:43 -0800349 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800350 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700351 link = durationMetric->add_links();
352 link->set_condition("APP_IS_BACKGROUND");
353 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
354 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
355
356 // Duration of screen on time.
357 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800358 durationMetric->set_name("METRIC_8");
Yangster1d4d6862017-10-31 12:58:51 -0700359 durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800360 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen5154a3792017-10-30 22:57:06 -0700361 durationMetric->set_what("SCREEN_IS_ON");
362
Chenjie Yub3dda412017-10-24 13:41:59 -0700363 // Value metric to count KERNEL_WAKELOCK when screen turned on
364 ValueMetric* valueMetric = config.add_value_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800365 valueMetric->set_name("METRIC_6");
Chenjie Yub3dda412017-10-24 13:41:59 -0700366 valueMetric->set_what("KERNEL_WAKELOCK");
367 valueMetric->set_value_field(1);
368 valueMetric->set_condition("SCREEN_IS_ON");
369 keyMatcher = valueMetric->add_dimension();
370 keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY);
371 // This is for testing easier. We should never set bucket size this small.
372 valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
373
Yao Chen5110bed2017-10-23 12:50:02 -0700374 // Add an EventMetric to log process state change events.
375 EventMetric* eventMetric = config.add_event_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800376 eventMetric->set_name("METRIC_9");
Yao Chen5110bed2017-10-23 12:50:02 -0700377 eventMetric->set_what("SCREEN_TURNED_ON");
378
Yangster1d4d6862017-10-31 12:58:51 -0700379 // Add an GaugeMetric.
380 GaugeMetric* gaugeMetric = config.add_gauge_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800381 gaugeMetric->set_name("METRIC_10");
Yangster1d4d6862017-10-31 12:58:51 -0700382 gaugeMetric->set_what("DEVICE_TEMPERATURE");
383 gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
384 gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
385
Yao Chen729093d2017-10-16 10:33:26 -0700386 // Event matchers............
Yangster1d4d6862017-10-31 12:58:51 -0700387 LogEntryMatcher* temperatureEntryMatcher = config.add_log_entry_matcher();
388 temperatureEntryMatcher->set_name("DEVICE_TEMPERATURE");
389 temperatureEntryMatcher->mutable_simple_log_entry_matcher()->set_tag(
390 DEVICE_TEMPERATURE_TAG_ID);
391
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700392 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700393 eventMatcher->set_name("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700394 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700395 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
396 KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
397 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
398 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700399
400 eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700401 eventMatcher->set_name("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700402 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700403 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
404 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
405 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
406 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700407
Yao Chen729093d2017-10-16 10:33:26 -0700408 eventMatcher = config.add_log_entry_matcher();
409 eventMatcher->set_name("PROCESS_STATE_CHANGE");
410 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
411 simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700412
Yao Chen729093d2017-10-16 10:33:26 -0700413 eventMatcher = config.add_log_entry_matcher();
414 eventMatcher->set_name("APP_GOES_BACKGROUND");
415 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
416 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
417 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
418 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
419 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700420
Yao Chen729093d2017-10-16 10:33:26 -0700421 eventMatcher = config.add_log_entry_matcher();
422 eventMatcher->set_name("APP_GOES_FOREGROUND");
423 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
424 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
425 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
426 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
427 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700428
Yao Chen729093d2017-10-16 10:33:26 -0700429 eventMatcher = config.add_log_entry_matcher();
430 eventMatcher->set_name("APP_GET_WL");
431 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
432 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
433 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
434 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
435 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700436
Yao Chen729093d2017-10-16 10:33:26 -0700437 eventMatcher = config.add_log_entry_matcher();
438 eventMatcher->set_name("APP_RELEASE_WL");
439 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
440 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
441 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
442 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
443 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700444
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700445 // pulled events
446 eventMatcher = config.add_log_entry_matcher();
447 eventMatcher->set_name("KERNEL_WAKELOCK");
448 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
449 simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID);
450
Yao Chen729093d2017-10-16 10:33:26 -0700451 // Conditions.............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700452 Condition* condition = config.add_condition();
453 condition->set_name("SCREEN_IS_ON");
454 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700455 simpleCondition->set_start("SCREEN_TURNED_ON");
456 simpleCondition->set_stop("SCREEN_TURNED_OFF");
Yao Chen967b2052017-11-07 16:36:43 -0800457 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700458
459 condition = config.add_condition();
460 condition->set_name("SCREEN_IS_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700461 simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700462 simpleCondition->set_start("SCREEN_TURNED_OFF");
463 simpleCondition->set_stop("SCREEN_TURNED_ON");
Yao Chen967b2052017-11-07 16:36:43 -0800464 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700465
466 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700467 condition->set_name("APP_IS_BACKGROUND");
468 simpleCondition = condition->mutable_simple_condition();
469 simpleCondition->set_start("APP_GOES_BACKGROUND");
470 simpleCondition->set_stop("APP_GOES_FOREGROUND");
Yao Chen5154a3792017-10-30 22:57:06 -0700471 KeyMatcher* condition_dimension1 = simpleCondition->add_dimension();
472 condition_dimension1->set_key(APP_USAGE_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800473 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700474
475 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700476 condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
477 Condition_Combination* combination_condition = condition->mutable_combination();
478 combination_condition->set_operation(LogicalOperation::AND);
479 combination_condition->add_condition("APP_IS_BACKGROUND");
480 combination_condition->add_condition("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700481
Yao Chen5154a3792017-10-30 22:57:06 -0700482 condition = config.add_condition();
Yao Chen967b2052017-11-07 16:36:43 -0800483 condition->set_name("WL_HELD_PER_APP_PER_NAME");
Yao Chen5154a3792017-10-30 22:57:06 -0700484 simpleCondition = condition->mutable_simple_condition();
485 simpleCondition->set_start("APP_GET_WL");
486 simpleCondition->set_stop("APP_RELEASE_WL");
487 KeyMatcher* condition_dimension = simpleCondition->add_dimension();
488 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
489 condition_dimension = simpleCondition->add_dimension();
490 condition_dimension->set_key(WAKE_LOCK_NAME_KEY);
Yao Chen967b2052017-11-07 16:36:43 -0800491 simpleCondition->set_count_nesting(true);
492
493 condition = config.add_condition();
494 condition->set_name("WL_HELD_PER_APP");
495 simpleCondition = condition->mutable_simple_condition();
496 simpleCondition->set_start("APP_GET_WL");
497 simpleCondition->set_stop("APP_RELEASE_WL");
498 simpleCondition->set_initial_value(SimpleCondition_InitialValue_FALSE);
499 condition_dimension = simpleCondition->add_dimension();
500 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
501 simpleCondition->set_count_nesting(true);
Yao Chen5154a3792017-10-30 22:57:06 -0700502
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700503 return config;
504}
505
506} // namespace statsd
507} // namespace os
508} // namespace android