blob: 0564c7d1f6214dd8cec5773327216db4f4706ea2 [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"
yro947fbce2017-11-15 22:50:23 -080018#include "storage/StorageManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019
20#include "stats_util.h"
21
yro87d983c2017-11-14 21:31:43 -080022#include <android-base/file.h>
23#include <dirent.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024#include <stdio.h>
yro87d983c2017-11-14 21:31:43 -080025#include <vector>
26#include "android-base/stringprintf.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070027
28namespace android {
29namespace os {
30namespace statsd {
31
Yao Chenf09569f2017-12-13 17:00:51 -080032using std::map;
33using std::pair;
34using std::set;
35using std::string;
36using std::vector;
37
yro03faf092017-12-12 00:17:50 -080038#define STATS_SERVICE_DIR "/data/misc/stats-service"
yro87d983c2017-11-14 21:31:43 -080039
yro87d983c2017-11-14 21:31:43 -080040using android::base::StringPrintf;
41using std::unique_ptr;
42
Joe Onorato9fc9edf2017-10-15 20:08:52 -070043ConfigManager::ConfigManager() {
44}
45
46ConfigManager::~ConfigManager() {
47}
48
49void ConfigManager::Startup() {
Yao Chenf09569f2017-12-13 17:00:51 -080050 map<ConfigKey, StatsdConfig> configsFromDisk;
51 StorageManager::readConfigFromDisk(configsFromDisk);
52 // TODO(b/70667694): Make the configs from disk be used. And remove the fake config,
53 // and tests shouldn't call this Startup(), maybe call StartupForTest() so we don't read
54 // configs from disk for tests.
55 // for (const auto& pair : configsFromDisk) {
56 // UpdateConfig(pair.first, pair.second);
57 //}
Yao Chen3c0b95c2017-12-16 14:34:20 -080058
59 // Uncomment the following line and use the hard coded config for development.
60 // UpdateConfig(ConfigKey(1000, "fake"), build_fake_config());
Joe Onorato9fc9edf2017-10-15 20:08:52 -070061}
62
63void ConfigManager::AddListener(const sp<ConfigListener>& listener) {
64 mListeners.push_back(listener);
65}
66
67void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
Yao Chenf09569f2017-12-13 17:00:51 -080068 // Add to set
69 mConfigs.insert(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070070
71 // Save to disk
yro87d983c2017-11-14 21:31:43 -080072 update_saved_configs(key, config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070073
74 // Tell everyone
75 for (auto& listener : mListeners) {
76 listener->OnConfigUpdated(key, config);
77 }
78}
79
David Chenadaf8b32017-11-03 15:42:08 -070080void ConfigManager::SetConfigReceiver(const ConfigKey& key, const string& pkg, const string& cls) {
81 mConfigReceivers[key] = pair<string, string>(pkg, cls);
82}
83
84void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) {
85 mConfigReceivers.erase(key);
86}
87
Joe Onorato9fc9edf2017-10-15 20:08:52 -070088void ConfigManager::RemoveConfig(const ConfigKey& key) {
Yao Chenf09569f2017-12-13 17:00:51 -080089 auto it = mConfigs.find(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070090 if (it != mConfigs.end()) {
91 // Remove from map
92 mConfigs.erase(it);
93
Joe Onorato9fc9edf2017-10-15 20:08:52 -070094 // Tell everyone
95 for (auto& listener : mListeners) {
96 listener->OnConfigRemoved(key);
97 }
98 }
yro9c98c052017-11-19 14:33:56 -080099
100 // Remove from disk. There can still be a lingering file on disk so we check
101 // whether or not the config was on memory.
102 remove_saved_configs(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700103}
104
yro87d983c2017-11-14 21:31:43 -0800105void ConfigManager::remove_saved_configs(const ConfigKey& key) {
yro87d983c2017-11-14 21:31:43 -0800106 string prefix = StringPrintf("%d-%s", key.GetUid(), key.GetName().c_str());
yro947fbce2017-11-15 22:50:23 -0800107 StorageManager::deletePrefixedFiles(STATS_SERVICE_DIR, prefix.c_str());
yro87d983c2017-11-14 21:31:43 -0800108}
109
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700110void ConfigManager::RemoveConfigs(int uid) {
111 vector<ConfigKey> removed;
112
113 for (auto it = mConfigs.begin(); it != mConfigs.end();) {
114 // Remove from map
Yao Chenf09569f2017-12-13 17:00:51 -0800115 if (it->GetUid() == uid) {
116 removed.push_back(*it);
117 mConfigReceivers.erase(*it);
yro74fed972017-11-27 14:42:42 -0800118 it = mConfigs.erase(it);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700119 } else {
120 it++;
121 }
122 }
123
124 // Remove separately so if they do anything in the callback they can't mess up our iteration.
125 for (auto& key : removed) {
126 // Tell everyone
127 for (auto& listener : mListeners) {
128 listener->OnConfigRemoved(key);
129 }
130 }
131}
132
yro74fed972017-11-27 14:42:42 -0800133void ConfigManager::RemoveAllConfigs() {
134 vector<ConfigKey> removed;
135
136 for (auto it = mConfigs.begin(); it != mConfigs.end();) {
137 // Remove from map
Yao Chenf09569f2017-12-13 17:00:51 -0800138 removed.push_back(*it);
139 auto receiverIt = mConfigReceivers.find(*it);
yro74fed972017-11-27 14:42:42 -0800140 if (receiverIt != mConfigReceivers.end()) {
Yao Chenf09569f2017-12-13 17:00:51 -0800141 mConfigReceivers.erase(*it);
yro74fed972017-11-27 14:42:42 -0800142 }
143 it = mConfigs.erase(it);
144 }
145
146 // Remove separately so if they do anything in the callback they can't mess up our iteration.
147 for (auto& key : removed) {
148 // Tell everyone
149 for (auto& listener : mListeners) {
150 listener->OnConfigRemoved(key);
151 }
152 }
153}
154
Yangster7c334a12017-11-22 14:24:24 -0800155vector<ConfigKey> ConfigManager::GetAllConfigKeys() const {
David Chen1d7b0cd2017-11-15 14:20:04 -0800156 vector<ConfigKey> ret;
157 for (auto it = mConfigs.cbegin(); it != mConfigs.cend(); ++it) {
Yao Chenf09569f2017-12-13 17:00:51 -0800158 ret.push_back(*it);
David Chen1d7b0cd2017-11-15 14:20:04 -0800159 }
160 return ret;
161}
162
Yangster7c334a12017-11-22 14:24:24 -0800163const pair<string, string> ConfigManager::GetConfigReceiver(const ConfigKey& key) const {
David Chen1d7b0cd2017-11-15 14:20:04 -0800164 auto it = mConfigReceivers.find(key);
165 if (it == mConfigReceivers.end()) {
166 return pair<string,string>();
167 } else {
168 return it->second;
169 }
170}
171
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700172void ConfigManager::Dump(FILE* out) {
173 fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
174 fprintf(out, " uid name\n");
Yao Chenf09569f2017-12-13 17:00:51 -0800175 for (const auto& key : mConfigs) {
176 fprintf(out, " %6d %s\n", key.GetUid(), key.GetName().c_str());
177 auto receiverIt = mConfigReceivers.find(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800178 if (receiverIt != mConfigReceivers.end()) {
179 fprintf(out, " -> received by %s, %s\n", receiverIt->second.first.c_str(),
180 receiverIt->second.second.c_str());
181 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700182 }
183}
184
yro87d983c2017-11-14 21:31:43 -0800185void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) {
186 mkdir(STATS_SERVICE_DIR, S_IRWXU);
yro9c98c052017-11-19 14:33:56 -0800187
188 // If there is a pre-existing config with same key we should first delete it.
189 remove_saved_configs(key);
190
191 // Then we save the latest config.
yro87d983c2017-11-14 21:31:43 -0800192 string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
193 key.GetName().c_str(), time(nullptr));
yro947fbce2017-11-15 22:50:23 -0800194 const int numBytes = config.ByteSize();
195 vector<uint8_t> buffer(numBytes);
196 config.SerializeToArray(&buffer[0], numBytes);
197 StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700198}
199
Yangster-mac756cd482017-11-21 21:58:44 -0800200StatsdConfig build_fake_config() {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700201 // HACK: Hard code a test metric for counting screen on events...
202 StatsdConfig config;
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800203 config.set_name("CONFIG_12345");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700204
Yao Chen5154a3792017-10-30 22:57:06 -0700205 int WAKE_LOCK_TAG_ID = 1111; // put a fake id here to make testing easier.
Yao Chen729093d2017-10-16 10:33:26 -0700206 int WAKE_LOCK_UID_KEY_ID = 1;
Yao Chen5154a3792017-10-30 22:57:06 -0700207 int WAKE_LOCK_NAME_KEY = 3;
208 int WAKE_LOCK_STATE_KEY = 4;
Yao Chen729093d2017-10-16 10:33:26 -0700209 int WAKE_LOCK_ACQUIRE_VALUE = 1;
210 int WAKE_LOCK_RELEASE_VALUE = 0;
211
212 int APP_USAGE_ID = 12345;
213 int APP_USAGE_UID_KEY_ID = 1;
214 int APP_USAGE_STATE_KEY = 2;
215 int APP_USAGE_FOREGROUND = 1;
216 int APP_USAGE_BACKGROUND = 0;
217
Bookatzc1a050a2017-10-10 15:49:28 -0700218 int SCREEN_EVENT_TAG_ID = 29;
Yao Chen729093d2017-10-16 10:33:26 -0700219 int SCREEN_EVENT_STATE_KEY = 1;
220 int SCREEN_EVENT_ON_VALUE = 2;
221 int SCREEN_EVENT_OFF_VALUE = 1;
222
Bookatzc1a050a2017-10-10 15:49:28 -0700223 int UID_PROCESS_STATE_TAG_ID = 27;
Yao Chen729093d2017-10-16 10:33:26 -0700224 int UID_PROCESS_STATE_UID_KEY = 1;
225
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700226 int KERNEL_WAKELOCK_TAG_ID = 1004;
Chenjie Yu6842a8c2017-12-05 22:34:34 -0800227 int KERNEL_WAKELOCK_COUNT_KEY = 2;
228 int KERNEL_WAKELOCK_NAME_KEY = 1;
Chenjie Yub3dda412017-10-24 13:41:59 -0700229
Yangster1d4d6862017-10-31 12:58:51 -0700230 int DEVICE_TEMPERATURE_TAG_ID = 33;
231 int DEVICE_TEMPERATURE_KEY = 1;
232
Yao Chen729093d2017-10-16 10:33:26 -0700233 // Count Screen ON events.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700234 CountMetric* metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800235 metric->set_name("METRIC_1");
Yao Chen729093d2017-10-16 10:33:26 -0700236 metric->set_what("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700237 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
238
Bookatzd3606c72017-10-19 10:13:49 -0700239 // Anomaly threshold for screen-on count.
Yao Chenf09569f2017-12-13 17:00:51 -0800240 // TODO(b/70627390): Uncomment once the bug is fixed.
241 /*Alert* alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800242 alert->set_name("ALERT_1");
243 alert->set_metric_name("METRIC_1");
Bookatzd3606c72017-10-19 10:13:49 -0700244 alert->set_number_of_buckets(6);
245 alert->set_trigger_if_sum_gt(10);
246 alert->set_refractory_period_secs(30);
Bookatzd1fd2422017-11-22 15:21:03 -0800247 Alert::IncidentdDetails* details = alert->mutable_incidentd_details();
248 details->add_section(12);
Yao Chenf09569f2017-12-13 17:00:51 -0800249 details->add_section(13);*/
Bookatzd3606c72017-10-19 10:13:49 -0700250
Yao Chen729093d2017-10-16 10:33:26 -0700251 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700252 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800253 metric->set_name("METRIC_2");
Yao Chen729093d2017-10-16 10:33:26 -0700254 metric->set_what("PROCESS_STATE_CHANGE");
255 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
256 KeyMatcher* keyMatcher = metric->add_dimension();
257 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700258
Yang Lu3eba6212017-10-25 19:54:45 -0700259 // Anomaly threshold for background count.
Yao Chenf09569f2017-12-13 17:00:51 -0800260 // TODO(b/70627390): Uncomment once the bug is fixed.
261 /*
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800262 alert = config.add_alert();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800263 alert->set_name("ALERT_2");
264 alert->set_metric_name("METRIC_2");
Yang Lu3eba6212017-10-25 19:54:45 -0700265 alert->set_number_of_buckets(4);
266 alert->set_trigger_if_sum_gt(30);
267 alert->set_refractory_period_secs(20);
Bookatzd1fd2422017-11-22 15:21:03 -0800268 details = alert->mutable_incidentd_details();
269 details->add_section(14);
Yao Chenf09569f2017-12-13 17:00:51 -0800270 details->add_section(15);*/
Yang Lu3eba6212017-10-25 19:54:45 -0700271
Yao Chen729093d2017-10-16 10:33:26 -0700272 // Count process state changes, slice by uid, while SCREEN_IS_OFF
273 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800274 metric->set_name("METRIC_3");
Yao Chen729093d2017-10-16 10:33:26 -0700275 metric->set_what("PROCESS_STATE_CHANGE");
276 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
277 keyMatcher = metric->add_dimension();
278 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
279 metric->set_condition("SCREEN_IS_OFF");
280
Yao Chen5110bed2017-10-23 12:50:02 -0700281 // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
Yao Chen729093d2017-10-16 10:33:26 -0700282 metric = config.add_count_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800283 metric->set_name("METRIC_4");
Yao Chen729093d2017-10-16 10:33:26 -0700284 metric->set_what("APP_GET_WL");
285 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
286 keyMatcher = metric->add_dimension();
287 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
288 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Stefan Lafona5b51912017-12-05 21:43:52 -0800289 MetricConditionLink* link = metric->add_links();
Yao Chen729093d2017-10-16 10:33:26 -0700290 link->set_condition("APP_IS_BACKGROUND");
Stefan Lafona5b51912017-12-05 21:43:52 -0800291 link->add_key_in_what()->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen729093d2017-10-16 10:33:26 -0700292 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
293
Yao Chen5154a3792017-10-30 22:57:06 -0700294 // Duration of an app holding any wl, while screen on and app in background, slice by uid
Yao Chen729093d2017-10-16 10:33:26 -0700295 DurationMetric* durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800296 durationMetric->set_name("METRIC_5");
Yao Chen729093d2017-10-16 10:33:26 -0700297 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800298 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen729093d2017-10-16 10:33:26 -0700299 keyMatcher = durationMetric->add_dimension();
300 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800301 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800302 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen729093d2017-10-16 10:33:26 -0700303 link = durationMetric->add_links();
304 link->set_condition("APP_IS_BACKGROUND");
Stefan Lafona5b51912017-12-05 21:43:52 -0800305 link->add_key_in_what()->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen729093d2017-10-16 10:33:26 -0700306 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
307
Yao Chen5154a3792017-10-30 22:57:06 -0700308 // max Duration of an app holding any wl, while screen on and app in background, slice by uid
309 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800310 durationMetric->set_name("METRIC_6");
Yao Chen5154a3792017-10-30 22:57:06 -0700311 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800312 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen5154a3792017-10-30 22:57:06 -0700313 keyMatcher = durationMetric->add_dimension();
314 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen967b2052017-11-07 16:36:43 -0800315 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800316 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700317 link = durationMetric->add_links();
318 link->set_condition("APP_IS_BACKGROUND");
Stefan Lafona5b51912017-12-05 21:43:52 -0800319 link->add_key_in_what()->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen5154a3792017-10-30 22:57:06 -0700320 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
321
322 // Duration of an app holding any wl, while screen on and app in background
323 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800324 durationMetric->set_name("METRIC_7");
Yao Chen5154a3792017-10-30 22:57:06 -0700325 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800326 durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
Yao Chen967b2052017-11-07 16:36:43 -0800327 durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
Yao Chen5c925ad2017-11-15 14:15:46 -0800328 durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
Yao Chen5154a3792017-10-30 22:57:06 -0700329 link = durationMetric->add_links();
330 link->set_condition("APP_IS_BACKGROUND");
Stefan Lafona5b51912017-12-05 21:43:52 -0800331 link->add_key_in_what()->set_key(WAKE_LOCK_UID_KEY_ID);
Yao Chen5154a3792017-10-30 22:57:06 -0700332 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
333
334 // Duration of screen on time.
335 durationMetric = config.add_duration_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800336 durationMetric->set_name("METRIC_8");
Yangster1d4d6862017-10-31 12:58:51 -0700337 durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800338 durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
Yao Chen5154a3792017-10-30 22:57:06 -0700339 durationMetric->set_what("SCREEN_IS_ON");
340
Bookatz450099d2017-11-30 17:09:30 -0800341 // Anomaly threshold for background count.
Yao Chenf09569f2017-12-13 17:00:51 -0800342 // TODO(b/70627390): Uncomment once the bug is fixed.
343 /*
Bookatz450099d2017-11-30 17:09:30 -0800344 alert = config.add_alert();
345 alert->set_name("ALERT_8");
346 alert->set_metric_name("METRIC_8");
347 alert->set_number_of_buckets(4);
348 alert->set_trigger_if_sum_gt(2000000000); // 2 seconds
349 alert->set_refractory_period_secs(120);
350 details = alert->mutable_incidentd_details();
Yao Chenf09569f2017-12-13 17:00:51 -0800351 details->add_section(-1);*/
Bookatz450099d2017-11-30 17:09:30 -0800352
Chenjie Yub3dda412017-10-24 13:41:59 -0700353 // Value metric to count KERNEL_WAKELOCK when screen turned on
354 ValueMetric* valueMetric = config.add_value_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800355 valueMetric->set_name("METRIC_6");
Chenjie Yub3dda412017-10-24 13:41:59 -0700356 valueMetric->set_what("KERNEL_WAKELOCK");
Chenjie Yu6842a8c2017-12-05 22:34:34 -0800357 valueMetric->set_value_field(KERNEL_WAKELOCK_COUNT_KEY);
Chenjie Yub3dda412017-10-24 13:41:59 -0700358 valueMetric->set_condition("SCREEN_IS_ON");
359 keyMatcher = valueMetric->add_dimension();
360 keyMatcher->set_key(KERNEL_WAKELOCK_NAME_KEY);
361 // This is for testing easier. We should never set bucket size this small.
362 valueMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
363
Yao Chen5110bed2017-10-23 12:50:02 -0700364 // Add an EventMetric to log process state change events.
365 EventMetric* eventMetric = config.add_event_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800366 eventMetric->set_name("METRIC_9");
Yao Chen5110bed2017-10-23 12:50:02 -0700367 eventMetric->set_what("SCREEN_TURNED_ON");
368
Yangster1d4d6862017-10-31 12:58:51 -0700369 // Add an GaugeMetric.
370 GaugeMetric* gaugeMetric = config.add_gauge_metric();
Stefan Lafon7c8f0a52017-11-21 14:49:09 -0800371 gaugeMetric->set_name("METRIC_10");
Yangster1d4d6862017-10-31 12:58:51 -0700372 gaugeMetric->set_what("DEVICE_TEMPERATURE");
373 gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
374 gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
375
Yao Chen729093d2017-10-16 10:33:26 -0700376 // Event matchers............
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800377 AtomMatcher* temperatureAtomMatcher = config.add_atom_matcher();
378 temperatureAtomMatcher->set_name("DEVICE_TEMPERATURE");
379 temperatureAtomMatcher->mutable_simple_atom_matcher()->set_tag(
Yangster1d4d6862017-10-31 12:58:51 -0700380 DEVICE_TEMPERATURE_TAG_ID);
381
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800382 AtomMatcher* eventMatcher = config.add_atom_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700383 eventMatcher->set_name("SCREEN_TURNED_ON");
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800384 SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
385 simpleAtomMatcher->set_tag(SCREEN_EVENT_TAG_ID);
386 KeyValueMatcher* keyValueMatcher = simpleAtomMatcher->add_key_value_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700387 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
388 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700389
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800390 eventMatcher = config.add_atom_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700391 eventMatcher->set_name("SCREEN_TURNED_OFF");
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800392 simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
393 simpleAtomMatcher->set_tag(SCREEN_EVENT_TAG_ID);
394 keyValueMatcher = simpleAtomMatcher->add_key_value_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700395 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
396 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700397
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800398 eventMatcher = config.add_atom_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700399 eventMatcher->set_name("PROCESS_STATE_CHANGE");
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800400 simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
401 simpleAtomMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700402
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800403 eventMatcher = config.add_atom_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700404 eventMatcher->set_name("APP_GOES_BACKGROUND");
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800405 simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
406 simpleAtomMatcher->set_tag(APP_USAGE_ID);
407 keyValueMatcher = simpleAtomMatcher->add_key_value_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700408 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
409 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700410
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800411 eventMatcher = config.add_atom_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700412 eventMatcher->set_name("APP_GOES_FOREGROUND");
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800413 simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
414 simpleAtomMatcher->set_tag(APP_USAGE_ID);
415 keyValueMatcher = simpleAtomMatcher->add_key_value_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700416 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
417 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700418
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800419 eventMatcher = config.add_atom_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700420 eventMatcher->set_name("APP_GET_WL");
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800421 simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
422 simpleAtomMatcher->set_tag(WAKE_LOCK_TAG_ID);
423 keyValueMatcher = simpleAtomMatcher->add_key_value_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700424 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
425 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700426
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800427 eventMatcher = config.add_atom_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700428 eventMatcher->set_name("APP_RELEASE_WL");
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800429 simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
430 simpleAtomMatcher->set_tag(WAKE_LOCK_TAG_ID);
431 keyValueMatcher = simpleAtomMatcher->add_key_value_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700432 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
433 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700434
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700435 // pulled events
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800436 eventMatcher = config.add_atom_matcher();
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700437 eventMatcher->set_name("KERNEL_WAKELOCK");
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800438 simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
439 simpleAtomMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700440
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800441 // Predicates.............
442 Predicate* predicate = config.add_predicate();
443 predicate->set_name("SCREEN_IS_ON");
444 SimplePredicate* simplePredicate = predicate->mutable_simple_predicate();
445 simplePredicate->set_start("SCREEN_TURNED_ON");
446 simplePredicate->set_stop("SCREEN_TURNED_OFF");
447 simplePredicate->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700448
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800449 predicate = config.add_predicate();
450 predicate->set_name("SCREEN_IS_OFF");
451 simplePredicate = predicate->mutable_simple_predicate();
452 simplePredicate->set_start("SCREEN_TURNED_OFF");
453 simplePredicate->set_stop("SCREEN_TURNED_ON");
454 simplePredicate->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700455
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800456 predicate = config.add_predicate();
457 predicate->set_name("APP_IS_BACKGROUND");
458 simplePredicate = predicate->mutable_simple_predicate();
459 simplePredicate->set_start("APP_GOES_BACKGROUND");
460 simplePredicate->set_stop("APP_GOES_FOREGROUND");
461 KeyMatcher* predicate_dimension1 = simplePredicate->add_dimension();
462 predicate_dimension1->set_key(APP_USAGE_UID_KEY_ID);
463 simplePredicate->set_count_nesting(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700464
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800465 predicate = config.add_predicate();
466 predicate->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
467 Predicate_Combination* combination_predicate = predicate->mutable_combination();
468 combination_predicate->set_operation(LogicalOperation::AND);
469 combination_predicate->add_predicate("APP_IS_BACKGROUND");
470 combination_predicate->add_predicate("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700471
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800472 predicate = config.add_predicate();
473 predicate->set_name("WL_HELD_PER_APP_PER_NAME");
474 simplePredicate = predicate->mutable_simple_predicate();
475 simplePredicate->set_start("APP_GET_WL");
476 simplePredicate->set_stop("APP_RELEASE_WL");
477 KeyMatcher* predicate_dimension = simplePredicate->add_dimension();
478 predicate_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
479 predicate_dimension = simplePredicate->add_dimension();
480 predicate_dimension->set_key(WAKE_LOCK_NAME_KEY);
481 simplePredicate->set_count_nesting(true);
Yao Chen967b2052017-11-07 16:36:43 -0800482
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800483 predicate = config.add_predicate();
484 predicate->set_name("WL_HELD_PER_APP");
485 simplePredicate = predicate->mutable_simple_predicate();
486 simplePredicate->set_start("APP_GET_WL");
487 simplePredicate->set_stop("APP_RELEASE_WL");
488 simplePredicate->set_initial_value(SimplePredicate_InitialValue_FALSE);
489 predicate_dimension = simplePredicate->add_dimension();
490 predicate_dimension->set_key(WAKE_LOCK_UID_KEY_ID);
491 simplePredicate->set_count_nesting(true);
Yao Chen5154a3792017-10-30 22:57:06 -0700492
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700493 return config;
494}
495
496} // namespace statsd
497} // namespace os
498} // namespace android