blob: 9680e4ac81091c36d3353979de6d4bcf9714bd93 [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
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
David Chen1d7b0cd2017-11-15 14:20:04 -0800137vector<ConfigKey> ConfigManager::GetAllConfigKeys() {
138 vector<ConfigKey> ret;
139 for (auto it = mConfigs.cbegin(); it != mConfigs.cend(); ++it) {
140 ret.push_back(it->first);
141 }
142 return ret;
143}
144
145const pair<string, string> ConfigManager::GetConfigReceiver(const ConfigKey& key) {
146 auto it = mConfigReceivers.find(key);
147 if (it == mConfigReceivers.end()) {
148 return pair<string,string>();
149 } else {
150 return it->second;
151 }
152}
153
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700154void ConfigManager::Dump(FILE* out) {
155 fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
156 fprintf(out, " uid name\n");
157 for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin();
158 it != mConfigs.end(); it++) {
159 fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800160 auto receiverIt = mConfigReceivers.find(it->first);
161 if (receiverIt != mConfigReceivers.end()) {
162 fprintf(out, " -> received by %s, %s\n", receiverIt->second.first.c_str(),
163 receiverIt->second.second.c_str());
164 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700165 // TODO: Print the contents of the config too.
166 }
167}
168
yro87d983c2017-11-14 21:31:43 -0800169void ConfigManager::readConfigFromDisk() {
170 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
171 if (dir == NULL) {
172 ALOGD("no default config on disk");
173 return;
174 }
175
176 dirent* de;
177 while ((de = readdir(dir.get()))) {
178 char* name = de->d_name;
179 if (name[0] == '.') continue;
180 ALOGD("file %s", name);
181
182 int index = 0;
183 int uid = 0;
184 string configName;
185 char* substr = strtok(name, "-");
186 // Timestamp lives at index 2 but we skip parsing it as it's not needed.
187 while (substr != nullptr && index < 2) {
188 if (index) {
189 uid = atoi(substr);
190 } else {
191 configName = substr;
192 }
193 index++;
194 }
195 if (index < 2) continue;
196 string file_name = StringPrintf("%s/%s", STATS_SERVICE_DIR, name);
197 ALOGD("full file %s", file_name.c_str());
198 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
199 if (fd != -1) {
200 string content;
201 if (android::base::ReadFdToString(fd, &content)) {
202 StatsdConfig config;
203 if (config.ParseFromString(content)) {
204 mConfigs[ConfigKey(uid, configName)] = config;
205 ALOGD("map key uid=%d|name=%s", uid, name);
206 }
207 }
208 close(fd);
209 }
210 }
211}
212
213void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) {
214 mkdir(STATS_SERVICE_DIR, S_IRWXU);
215 string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
216 key.GetName().c_str(), time(nullptr));
217 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
218 if (fd != -1) {
219 const int numBytes = config.ByteSize();
220 vector<uint8_t> buffer(numBytes);
221 config.SerializeToArray(&buffer[0], numBytes);
222 int result = write(fd, &buffer[0], numBytes);
223 close(fd);
224 bool wroteKey = (result == numBytes);
225 ALOGD("wrote to file %d", wroteKey);
226 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700227}
228
229static StatsdConfig build_fake_config() {
230 // HACK: Hard code a test metric for counting screen on events...
231 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800232 config.set_name("12345");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700233
Yao Chen5154a3792017-10-30 22:57:06 -0700234 int WAKE_LOCK_TAG_ID = 1111; // put a fake id here to make testing easier.
Yao Chen729093d2017-10-16 10:33:26 -0700235 int WAKE_LOCK_UID_KEY_ID = 1;
Yao Chen5154a3792017-10-30 22:57:06 -0700236 int WAKE_LOCK_NAME_KEY = 3;
237 int WAKE_LOCK_STATE_KEY = 4;
Yao Chen729093d2017-10-16 10:33:26 -0700238 int WAKE_LOCK_ACQUIRE_VALUE = 1;
239 int WAKE_LOCK_RELEASE_VALUE = 0;
240
241 int APP_USAGE_ID = 12345;
242 int APP_USAGE_UID_KEY_ID = 1;
243 int APP_USAGE_STATE_KEY = 2;
244 int APP_USAGE_FOREGROUND = 1;
245 int APP_USAGE_BACKGROUND = 0;
246
Bookatzc1a050a2017-10-10 15:49:28 -0700247 int SCREEN_EVENT_TAG_ID = 29;
Yao Chen729093d2017-10-16 10:33:26 -0700248 int SCREEN_EVENT_STATE_KEY = 1;
249 int SCREEN_EVENT_ON_VALUE = 2;
250 int SCREEN_EVENT_OFF_VALUE = 1;
251
Bookatzc1a050a2017-10-10 15:49:28 -0700252 int UID_PROCESS_STATE_TAG_ID = 27;
Yao Chen729093d2017-10-16 10:33:26 -0700253 int UID_PROCESS_STATE_UID_KEY = 1;
254
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700255 int KERNEL_WAKELOCK_TAG_ID = 1004;
Chenjie Yub3dda412017-10-24 13:41:59 -0700256 int KERNEL_WAKELOCK_NAME_KEY = 4;
257
Yangster1d4d6862017-10-31 12:58:51 -0700258 int DEVICE_TEMPERATURE_TAG_ID = 33;
259 int DEVICE_TEMPERATURE_KEY = 1;
260
Yao Chen729093d2017-10-16 10:33:26 -0700261 // Count Screen ON events.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700262 CountMetric* metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800263 metric->set_name("1");
Yao Chen729093d2017-10-16 10:33:26 -0700264 metric->set_what("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700265 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
266
Bookatzd3606c72017-10-19 10:13:49 -0700267 // Anomaly threshold for screen-on count.
Yangster-macd1815dc2017-11-13 21:43:15 -0800268 Alert* alert = config.add_alerts();
269 alert->set_name("1");
Bookatzd3606c72017-10-19 10:13:49 -0700270 alert->set_number_of_buckets(6);
271 alert->set_trigger_if_sum_gt(10);
272 alert->set_refractory_period_secs(30);
273
Yao Chen729093d2017-10-16 10:33:26 -0700274 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700275 metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800276 metric->set_name("2");
Yao Chen729093d2017-10-16 10:33:26 -0700277 metric->set_what("PROCESS_STATE_CHANGE");
278 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
279 KeyMatcher* keyMatcher = metric->add_dimension();
280 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700281
Yang Lu3eba6212017-10-25 19:54:45 -0700282 // Anomaly threshold for background count.
Yangster-macd1815dc2017-11-13 21:43:15 -0800283 alert = config.add_alerts();
284 alert->set_name("2");
Yang Lu3eba6212017-10-25 19:54:45 -0700285 alert->set_number_of_buckets(4);
286 alert->set_trigger_if_sum_gt(30);
287 alert->set_refractory_period_secs(20);
288
Yao Chen729093d2017-10-16 10:33:26 -0700289 // Count process state changes, slice by uid, while SCREEN_IS_OFF
290 metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800291 metric->set_name("3");
Yao Chen729093d2017-10-16 10:33:26 -0700292 metric->set_what("PROCESS_STATE_CHANGE");
293 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
294 keyMatcher = metric->add_dimension();
295 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
296 metric->set_condition("SCREEN_IS_OFF");
297
Yao Chen5110bed2017-10-23 12:50:02 -0700298 // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
Yao Chen729093d2017-10-16 10:33:26 -0700299 metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800300 metric->set_name("4");
Yao Chen729093d2017-10-16 10:33:26 -0700301 metric->set_what("APP_GET_WL");
302 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
303 keyMatcher = metric->add_dimension();
304 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
305 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
306 EventConditionLink* link = metric->add_links();
307 link->set_condition("APP_IS_BACKGROUND");
308 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
309 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
310
Yao Chen5154a3792017-10-30 22:57:06 -0700311 // Duration of an app holding any wl, while screen on and app in background, slice by uid
Yao Chen729093d2017-10-16 10:33:26 -0700312 DurationMetric* durationMetric = config.add_duration_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800313 durationMetric->set_name("5");
Yao Chen729093d2017-10-16 10:33:26 -0700314 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
315 durationMetric->set_type(DurationMetric_AggregationType_DURATION_SUM);
316 keyMatcher = durationMetric->add_dimension();
317 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800318 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800319 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen729093d2017-10-16 10:33:26 -0700320 link = durationMetric->add_links();
321 link->set_condition("APP_IS_BACKGROUND");
322 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
323 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
324
Yao Chen5154a3792017-10-30 22:57:06 -0700325 // max Duration of an app holding any wl, while screen on and app in background, slice by uid
326 durationMetric = config.add_duration_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800327 durationMetric->set_name("6");
Yao Chen5154a3792017-10-30 22:57:06 -0700328 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
329 durationMetric->set_type(DurationMetric_AggregationType_DURATION_MAX_SPARSE);
330 keyMatcher = durationMetric->add_dimension();
331 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800332 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800333 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700334 link = durationMetric->add_links();
335 link->set_condition("APP_IS_BACKGROUND");
336 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
337 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
338
339 // Duration of an app holding any wl, while screen on and app in background
340 durationMetric = config.add_duration_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800341 durationMetric->set_name("7");
Yao Chen5154a3792017-10-30 22:57:06 -0700342 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
343 durationMetric->set_type(DurationMetric_AggregationType_DURATION_MAX_SPARSE);
Yao Chen967b2052017-11-07 16:36:43 -0800344 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800345 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700346 link = durationMetric->add_links();
347 link->set_condition("APP_IS_BACKGROUND");
348 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
349 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
350
351 // Duration of screen on time.
352 durationMetric = config.add_duration_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800353 durationMetric->set_name("8");
Yangster1d4d6862017-10-31 12:58:51 -0700354 durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
Yao Chen5154a3792017-10-30 22:57:06 -0700355 durationMetric->set_type(DurationMetric_AggregationType_DURATION_SUM);
356 durationMetric->set_what("SCREEN_IS_ON");
357
Chenjie Yub3dda412017-10-24 13:41:59 -0700358 // Value metric to count KERNEL_WAKELOCK when screen turned on
359 ValueMetric* valueMetric = config.add_value_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800360 valueMetric->set_name("6");
Chenjie Yub3dda412017-10-24 13:41:59 -0700361 valueMetric->set_what("KERNEL_WAKELOCK");
362 valueMetric->set_value_field(1);
363 valueMetric->set_condition("SCREEN_IS_ON");
364 keyMatcher = valueMetric->add_dimension();
365 keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY);
366 // This is for testing easier. We should never set bucket size this small.
367 valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
368
Yao Chen5110bed2017-10-23 12:50:02 -0700369 // Add an EventMetric to log process state change events.
370 EventMetric* eventMetric = config.add_event_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800371 eventMetric->set_name("9");
Yao Chen5110bed2017-10-23 12:50:02 -0700372 eventMetric->set_what("SCREEN_TURNED_ON");
373
Yangster1d4d6862017-10-31 12:58:51 -0700374 // Add an GaugeMetric.
375 GaugeMetric* gaugeMetric = config.add_gauge_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800376 gaugeMetric->set_name("10");
Yangster1d4d6862017-10-31 12:58:51 -0700377 gaugeMetric->set_what("DEVICE_TEMPERATURE");
378 gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
379 gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
380
Yao Chen729093d2017-10-16 10:33:26 -0700381 // Event matchers............
Yangster1d4d6862017-10-31 12:58:51 -0700382 LogEntryMatcher* temperatureEntryMatcher = config.add_log_entry_matcher();
383 temperatureEntryMatcher->set_name("DEVICE_TEMPERATURE");
384 temperatureEntryMatcher->mutable_simple_log_entry_matcher()->set_tag(
385 DEVICE_TEMPERATURE_TAG_ID);
386
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700387 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700388 eventMatcher->set_name("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700389 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700390 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
391 KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
392 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
393 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700394
395 eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700396 eventMatcher->set_name("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700397 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700398 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
399 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
400 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
401 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700402
Yao Chen729093d2017-10-16 10:33:26 -0700403 eventMatcher = config.add_log_entry_matcher();
404 eventMatcher->set_name("PROCESS_STATE_CHANGE");
405 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
406 simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
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("APP_GOES_BACKGROUND");
410 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
411 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
412 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
413 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
414 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700415
Yao Chen729093d2017-10-16 10:33:26 -0700416 eventMatcher = config.add_log_entry_matcher();
417 eventMatcher->set_name("APP_GOES_FOREGROUND");
418 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
419 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
420 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
421 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
422 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700423
Yao Chen729093d2017-10-16 10:33:26 -0700424 eventMatcher = config.add_log_entry_matcher();
425 eventMatcher->set_name("APP_GET_WL");
426 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
427 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
428 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
429 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
430 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700431
Yao Chen729093d2017-10-16 10:33:26 -0700432 eventMatcher = config.add_log_entry_matcher();
433 eventMatcher->set_name("APP_RELEASE_WL");
434 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
435 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
436 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
437 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
438 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700439
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700440 // pulled events
441 eventMatcher = config.add_log_entry_matcher();
442 eventMatcher->set_name("KERNEL_WAKELOCK");
443 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
444 simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID);
445
Yao Chen729093d2017-10-16 10:33:26 -0700446 // Conditions.............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700447 Condition* condition = config.add_condition();
448 condition->set_name("SCREEN_IS_ON");
449 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700450 simpleCondition->set_start("SCREEN_TURNED_ON");
451 simpleCondition->set_stop("SCREEN_TURNED_OFF");
Yao Chen967b2052017-11-07 16:36:43 -0800452 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700453
454 condition = config.add_condition();
455 condition->set_name("SCREEN_IS_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700456 simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700457 simpleCondition->set_start("SCREEN_TURNED_OFF");
458 simpleCondition->set_stop("SCREEN_TURNED_ON");
Yao Chen967b2052017-11-07 16:36:43 -0800459 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700460
461 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700462 condition->set_name("APP_IS_BACKGROUND");
463 simpleCondition = condition->mutable_simple_condition();
464 simpleCondition->set_start("APP_GOES_BACKGROUND");
465 simpleCondition->set_stop("APP_GOES_FOREGROUND");
Yao Chen5154a3792017-10-30 22:57:06 -0700466 KeyMatcher* condition_dimension1 = simpleCondition->add_dimension();
467 condition_dimension1->set_key(APP_USAGE_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800468 simpleCondition->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700469
470 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700471 condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
472 Condition_Combination* combination_condition = condition->mutable_combination();
473 combination_condition->set_operation(LogicalOperation::AND);
474 combination_condition->add_condition("APP_IS_BACKGROUND");
475 combination_condition->add_condition("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700476
Yao Chen5154a3792017-10-30 22:57:06 -0700477 condition = config.add_condition();
Yao Chen967b2052017-11-07 16:36:43 -0800478 condition->set_name("WL_HELD_PER_APP_PER_NAME");
Yao Chen5154a3792017-10-30 22:57:06 -0700479 simpleCondition = condition->mutable_simple_condition();
480 simpleCondition->set_start("APP_GET_WL");
481 simpleCondition->set_stop("APP_RELEASE_WL");
482 KeyMatcher* condition_dimension = simpleCondition->add_dimension();
483 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
484 condition_dimension = simpleCondition->add_dimension();
485 condition_dimension->set_key(WAKE_LOCK_NAME_KEY);
Yao Chen967b2052017-11-07 16:36:43 -0800486 simpleCondition->set_count_nesting(true);
487
488 condition = config.add_condition();
489 condition->set_name("WL_HELD_PER_APP");
490 simpleCondition = condition->mutable_simple_condition();
491 simpleCondition->set_start("APP_GET_WL");
492 simpleCondition->set_stop("APP_RELEASE_WL");
493 simpleCondition->set_initial_value(SimpleCondition_InitialValue_FALSE);
494 condition_dimension = simpleCondition->add_dimension();
495 condition_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
496 simpleCondition->set_count_nesting(true);
Yao Chen5154a3792017-10-30 22:57:06 -0700497
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700498 return config;
499}
500
501} // namespace statsd
502} // namespace os
503} // namespace android