blob: bc3a7b27b9eeafb91537974d1c1bc9df5058262a [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
48 UpdateConfig(ConfigKey(0, "fake"), build_fake_config());
49}
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
yro87d983c2017-11-14 21:31:43 -080083 // Remove from disk
84 remove_saved_configs(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070085
86 // Tell everyone
87 for (auto& listener : mListeners) {
88 listener->OnConfigRemoved(key);
89 }
90 }
91 // If we didn't find it, just quietly ignore it.
92}
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) {
105 if (remove(StringPrintf("%s/%d-%s", STATS_SERVICE_DIR, key.GetUid(),
106 key.GetName().c_str())
107 .c_str()) != 0) {
108 ALOGD("no file found");
109 }
110 }
111 }
112}
113
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700114void ConfigManager::RemoveConfigs(int uid) {
115 vector<ConfigKey> removed;
116
117 for (auto it = mConfigs.begin(); it != mConfigs.end();) {
118 // Remove from map
119 if (it->first.GetUid() == uid) {
120 removed.push_back(it->first);
121 it = mConfigs.erase(it);
David Chenadaf8b32017-11-03 15:42:08 -0700122 mConfigReceivers.erase(it->first);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700123 } else {
124 it++;
125 }
126 }
127
128 // Remove separately so if they do anything in the callback they can't mess up our iteration.
129 for (auto& key : removed) {
130 // Tell everyone
131 for (auto& listener : mListeners) {
132 listener->OnConfigRemoved(key);
133 }
134 }
135}
136
137void ConfigManager::Dump(FILE* out) {
138 fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
139 fprintf(out, " uid name\n");
140 for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin();
141 it != mConfigs.end(); it++) {
142 fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str());
143 // TODO: Print the contents of the config too.
144 }
145}
146
yro87d983c2017-11-14 21:31:43 -0800147void ConfigManager::readConfigFromDisk() {
148 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
149 if (dir == NULL) {
150 ALOGD("no default config on disk");
151 return;
152 }
153
154 dirent* de;
155 while ((de = readdir(dir.get()))) {
156 char* name = de->d_name;
157 if (name[0] == '.') continue;
158 ALOGD("file %s", name);
159
160 int index = 0;
161 int uid = 0;
162 string configName;
163 char* substr = strtok(name, "-");
164 // Timestamp lives at index 2 but we skip parsing it as it's not needed.
165 while (substr != nullptr && index < 2) {
166 if (index) {
167 uid = atoi(substr);
168 } else {
169 configName = substr;
170 }
171 index++;
172 }
173 if (index < 2) continue;
174 string file_name = StringPrintf("%s/%s", STATS_SERVICE_DIR, name);
175 ALOGD("full file %s", file_name.c_str());
176 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
177 if (fd != -1) {
178 string content;
179 if (android::base::ReadFdToString(fd, &content)) {
180 StatsdConfig config;
181 if (config.ParseFromString(content)) {
182 mConfigs[ConfigKey(uid, configName)] = config;
183 ALOGD("map key uid=%d|name=%s", uid, name);
184 }
185 }
186 close(fd);
187 }
188 }
189}
190
191void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) {
192 mkdir(STATS_SERVICE_DIR, S_IRWXU);
193 string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
194 key.GetName().c_str(), time(nullptr));
195 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
196 if (fd != -1) {
197 const int numBytes = config.ByteSize();
198 vector<uint8_t> buffer(numBytes);
199 config.SerializeToArray(&buffer[0], numBytes);
200 int result = write(fd, &buffer[0], numBytes);
201 close(fd);
202 bool wroteKey = (result == numBytes);
203 ALOGD("wrote to file %d", wroteKey);
204 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700205}
206
207static StatsdConfig build_fake_config() {
208 // HACK: Hard code a test metric for counting screen on events...
209 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800210 config.set_name("12345");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700211
Yao Chen5154a3792017-10-30 22:57:06 -0700212 int WAKE_LOCK_TAG_ID = 1111; // put a fake id here to make testing easier.
Yao Chen729093d2017-10-16 10:33:26 -0700213 int WAKE_LOCK_UID_KEY_ID = 1;
Yao Chen5154a3792017-10-30 22:57:06 -0700214 int WAKE_LOCK_NAME_KEY = 3;
215 int WAKE_LOCK_STATE_KEY = 4;
Yao Chen729093d2017-10-16 10:33:26 -0700216 int WAKE_LOCK_ACQUIRE_VALUE = 1;
217 int WAKE_LOCK_RELEASE_VALUE = 0;
218
219 int APP_USAGE_ID = 12345;
220 int APP_USAGE_UID_KEY_ID = 1;
221 int APP_USAGE_STATE_KEY = 2;
222 int APP_USAGE_FOREGROUND = 1;
223 int APP_USAGE_BACKGROUND = 0;
224
Bookatzc1a050a2017-10-10 15:49:28 -0700225 int SCREEN_EVENT_TAG_ID = 29;
Yao Chen729093d2017-10-16 10:33:26 -0700226 int SCREEN_EVENT_STATE_KEY = 1;
227 int SCREEN_EVENT_ON_VALUE = 2;
228 int SCREEN_EVENT_OFF_VALUE = 1;
229
Bookatzc1a050a2017-10-10 15:49:28 -0700230 int UID_PROCESS_STATE_TAG_ID = 27;
Yao Chen729093d2017-10-16 10:33:26 -0700231 int UID_PROCESS_STATE_UID_KEY = 1;
232
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700233 int KERNEL_WAKELOCK_TAG_ID = 1004;
Chenjie Yub3dda412017-10-24 13:41:59 -0700234 int KERNEL_WAKELOCK_NAME_KEY = 4;
235
Yangster1d4d6862017-10-31 12:58:51 -0700236 int DEVICE_TEMPERATURE_TAG_ID = 33;
237 int DEVICE_TEMPERATURE_KEY = 1;
238
Yao Chen729093d2017-10-16 10:33:26 -0700239 // Count Screen ON events.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700240 CountMetric* metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800241 metric->set_name("1");
Yao Chen729093d2017-10-16 10:33:26 -0700242 metric->set_what("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700243 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
244
Bookatzd3606c72017-10-19 10:13:49 -0700245 // Anomaly threshold for screen-on count.
Yangster-macd1815dc2017-11-13 21:43:15 -0800246 Alert* alert = config.add_alerts();
247 alert->set_name("1");
Bookatzd3606c72017-10-19 10:13:49 -0700248 alert->set_number_of_buckets(6);
249 alert->set_trigger_if_sum_gt(10);
250 alert->set_refractory_period_secs(30);
251
Yao Chen729093d2017-10-16 10:33:26 -0700252 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700253 metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800254 metric->set_name("2");
Yao Chen729093d2017-10-16 10:33:26 -0700255 metric->set_what("PROCESS_STATE_CHANGE");
256 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
257 KeyMatcher* keyMatcher = metric->add_dimension();
258 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700259
Yang Lu3eba6212017-10-25 19:54:45 -0700260 // Anomaly threshold for background count.
Yangster-macd1815dc2017-11-13 21:43:15 -0800261 alert = config.add_alerts();
262 alert->set_name("2");
Yang Lu3eba6212017-10-25 19:54:45 -0700263 alert->set_number_of_buckets(4);
264 alert->set_trigger_if_sum_gt(30);
265 alert->set_refractory_period_secs(20);
266
Yao Chen729093d2017-10-16 10:33:26 -0700267 // Count process state changes, slice by uid, while SCREEN_IS_OFF
268 metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800269 metric->set_name("3");
Yao Chen729093d2017-10-16 10:33:26 -0700270 metric->set_what("PROCESS_STATE_CHANGE");
271 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
272 keyMatcher = metric->add_dimension();
273 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
274 metric->set_condition("SCREEN_IS_OFF");
275
Yao Chen5110bed2017-10-23 12:50:02 -0700276 // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
Yao Chen729093d2017-10-16 10:33:26 -0700277 metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800278 metric->set_name("4");
Yao Chen729093d2017-10-16 10:33:26 -0700279 metric->set_what("APP_GET_WL");
280 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
281 keyMatcher = metric->add_dimension();
282 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
283 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
284 EventConditionLink* link = metric->add_links();
285 link->set_condition("APP_IS_BACKGROUND");
286 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
287 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
288
Yao Chen5154a3792017-10-30 22:57:06 -0700289 // Duration of an app holding any wl, while screen on and app in background, slice by uid
Yao Chen729093d2017-10-16 10:33:26 -0700290 DurationMetric* durationMetric = config.add_duration_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800291 durationMetric->set_name("5");
Yao Chen729093d2017-10-16 10:33:26 -0700292 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
293 durationMetric->set_type(DurationMetric_AggregationType_DURATION_SUM);
294 keyMatcher = durationMetric->add_dimension();
295 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800296 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800297 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen729093d2017-10-16 10:33:26 -0700298 link = durationMetric->add_links();
299 link->set_condition("APP_IS_BACKGROUND");
300 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
301 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
302
Yao Chen5154a3792017-10-30 22:57:06 -0700303 // max Duration of an app holding any wl, while screen on and app in background, slice by uid
304 durationMetric = config.add_duration_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800305 durationMetric->set_name("6");
Yao Chen5154a3792017-10-30 22:57:06 -0700306 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
307 durationMetric->set_type(DurationMetric_AggregationType_DURATION_MAX_SPARSE);
308 keyMatcher = durationMetric->add_dimension();
309 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800310 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800311 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700312 link = durationMetric->add_links();
313 link->set_condition("APP_IS_BACKGROUND");
314 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
315 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
316
317 // Duration of an app holding any wl, while screen on and app in background
318 durationMetric = config.add_duration_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800319 durationMetric->set_name("7");
Yao Chen5154a3792017-10-30 22:57:06 -0700320 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
321 durationMetric->set_type(DurationMetric_AggregationType_DURATION_MAX_SPARSE);
Yao Chen967b2052017-11-07 16:36:43 -0800322 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800323 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700324 link = durationMetric->add_links();
325 link->set_condition("APP_IS_BACKGROUND");
326 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
327 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
328
329 // Duration of screen on time.
330 durationMetric = config.add_duration_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800331 durationMetric->set_name("8");
Yangster1d4d6862017-10-31 12:58:51 -0700332 durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
Yao Chen5154a3792017-10-30 22:57:06 -0700333 durationMetric->set_type(DurationMetric_AggregationType_DURATION_SUM);
334 durationMetric->set_what("SCREEN_IS_ON");
335
Chenjie Yub3dda412017-10-24 13:41:59 -0700336 // Value metric to count KERNEL_WAKELOCK when screen turned on
337 ValueMetric* valueMetric = config.add_value_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800338 valueMetric->set_name("6");
Chenjie Yub3dda412017-10-24 13:41:59 -0700339 valueMetric->set_what("KERNEL_WAKELOCK");
340 valueMetric->set_value_field(1);
341 valueMetric->set_condition("SCREEN_IS_ON");
342 keyMatcher = valueMetric->add_dimension();
343 keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY);
344 // This is for testing easier. We should never set bucket size this small.
345 valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
346
Yao Chen5110bed2017-10-23 12:50:02 -0700347 // Add an EventMetric to log process state change events.
348 EventMetric* eventMetric = config.add_event_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800349 eventMetric->set_name("9");
Yao Chen5110bed2017-10-23 12:50:02 -0700350 eventMetric->set_what("SCREEN_TURNED_ON");
351
Yangster1d4d6862017-10-31 12:58:51 -0700352 // Add an GaugeMetric.
353 GaugeMetric* gaugeMetric = config.add_gauge_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800354 gaugeMetric->set_name("10");
Yangster1d4d6862017-10-31 12:58:51 -0700355 gaugeMetric->set_what("DEVICE_TEMPERATURE");
356 gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
357 gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
358
Yao Chen729093d2017-10-16 10:33:26 -0700359 // Event matchers............
Yangster1d4d6862017-10-31 12:58:51 -0700360 LogEntryMatcher* temperatureEntryMatcher = config.add_log_entry_matcher();
361 temperatureEntryMatcher->set_name("DEVICE_TEMPERATURE");
362 temperatureEntryMatcher->mutable_simple_log_entry_matcher()->set_tag(
363 DEVICE_TEMPERATURE_TAG_ID);
364
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700365 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700366 eventMatcher->set_name("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700367 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700368 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
369 KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
370 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
371 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700372
373 eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700374 eventMatcher->set_name("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700375 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700376 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
377 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
378 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
379 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700380
Yao Chen729093d2017-10-16 10:33:26 -0700381 eventMatcher = config.add_log_entry_matcher();
382 eventMatcher->set_name("PROCESS_STATE_CHANGE");
383 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
384 simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700385
Yao Chen729093d2017-10-16 10:33:26 -0700386 eventMatcher = config.add_log_entry_matcher();
387 eventMatcher->set_name("APP_GOES_BACKGROUND");
388 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
389 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
390 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
391 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
392 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700393
Yao Chen729093d2017-10-16 10:33:26 -0700394 eventMatcher = config.add_log_entry_matcher();
395 eventMatcher->set_name("APP_GOES_FOREGROUND");
396 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
397 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
398 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
399 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
400 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700401
Yao Chen729093d2017-10-16 10:33:26 -0700402 eventMatcher = config.add_log_entry_matcher();
403 eventMatcher->set_name("APP_GET_WL");
404 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
405 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
406 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
407 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
408 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700409
Yao Chen729093d2017-10-16 10:33:26 -0700410 eventMatcher = config.add_log_entry_matcher();
411 eventMatcher->set_name("APP_RELEASE_WL");
412 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
413 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
414 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
415 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
416 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700417
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700418 // pulled events
419 eventMatcher = config.add_log_entry_matcher();
420 eventMatcher->set_name("KERNEL_WAKELOCK");
421 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
422 simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID);
423
Yao Chen729093d2017-10-16 10:33:26 -0700424 // Conditions.............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700425 Condition* condition = config.add_condition();
426 condition->set_name("SCREEN_IS_ON");
427 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700428 simpleCondition->set_start("SCREEN_TURNED_ON");
429 simpleCondition->set_stop("SCREEN_TURNED_OFF");
Yao Chen967b2052017-11-07 16:36:43 -0800430 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700431
432 condition = config.add_condition();
433 condition->set_name("SCREEN_IS_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700434 simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700435 simpleCondition->set_start("SCREEN_TURNED_OFF");
436 simpleCondition->set_stop("SCREEN_TURNED_ON");
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");
441 simpleCondition = condition->mutable_simple_condition();
442 simpleCondition->set_start("APP_GOES_BACKGROUND");
443 simpleCondition->set_stop("APP_GOES_FOREGROUND");
Yao Chen5154a3792017-10-30 22:57:06 -0700444 KeyMatcher* condition_dimension1 = simpleCondition->add_dimension();
445 condition_dimension1->set_key(APP_USAGE_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800446 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700447
448 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700449 condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
450 Condition_Combination* combination_condition = condition->mutable_combination();
451 combination_condition->set_operation(LogicalOperation::AND);
452 combination_condition->add_condition("APP_IS_BACKGROUND");
453 combination_condition->add_condition("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700454
Yao Chen5154a3792017-10-30 22:57:06 -0700455 condition = config.add_condition();
Yao Chen967b2052017-11-07 16:36:43 -0800456 condition->set_name("WL_HELD_PER_APP_PER_NAME");
Yao Chen5154a3792017-10-30 22:57:06 -0700457 simpleCondition = condition->mutable_simple_condition();
458 simpleCondition->set_start("APP_GET_WL");
459 simpleCondition->set_stop("APP_RELEASE_WL");
460 KeyMatcher* condition_dimension = simpleCondition->add_dimension();
461 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
462 condition_dimension = simpleCondition->add_dimension();
463 condition_dimension->set_key(WAKE_LOCK_NAME_KEY);
Yao Chen967b2052017-11-07 16:36:43 -0800464 simpleCondition->set_count_nesting(true);
465
466 condition = config.add_condition();
467 condition->set_name("WL_HELD_PER_APP");
468 simpleCondition = condition->mutable_simple_condition();
469 simpleCondition->set_start("APP_GET_WL");
470 simpleCondition->set_stop("APP_RELEASE_WL");
471 simpleCondition->set_initial_value(SimpleCondition_InitialValue_FALSE);
472 condition_dimension = simpleCondition->add_dimension();
473 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
474 simpleCondition->set_count_nesting(true);
Yao Chen5154a3792017-10-30 22:57:06 -0700475
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700476 return config;
477}
478
479} // namespace statsd
480} // namespace os
481} // namespace android