blob: 9ca7d62733b3825082b335b814857724d75279d8 [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
21#include <vector>
22
23#include <stdio.h>
24
25namespace android {
26namespace os {
27namespace statsd {
28
29static StatsdConfig build_fake_config();
30
31ConfigManager::ConfigManager() {
32}
33
34ConfigManager::~ConfigManager() {
35}
36
37void ConfigManager::Startup() {
38 // TODO: Implement me -- read from storage and call onto all of the listeners.
39 // Instead, we'll just make a fake one.
40
41 // this should be called from StatsService when it receives a statsd_config
42 UpdateConfig(ConfigKey(0, "fake"), build_fake_config());
43}
44
45void ConfigManager::AddListener(const sp<ConfigListener>& listener) {
46 mListeners.push_back(listener);
47}
48
49void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
50 // Add to map
51 mConfigs[key] = config;
52 // Why doesn't this work? mConfigs.insert({key, config});
53
54 // Save to disk
55 update_saved_configs();
56
57 // Tell everyone
58 for (auto& listener : mListeners) {
59 listener->OnConfigUpdated(key, config);
60 }
61}
62
63void ConfigManager::RemoveConfig(const ConfigKey& key) {
64 unordered_map<ConfigKey, StatsdConfig>::iterator it = mConfigs.find(key);
65 if (it != mConfigs.end()) {
66 // Remove from map
67 mConfigs.erase(it);
68
69 // Save to disk
70 update_saved_configs();
71
72 // Tell everyone
73 for (auto& listener : mListeners) {
74 listener->OnConfigRemoved(key);
75 }
76 }
77 // If we didn't find it, just quietly ignore it.
78}
79
80void ConfigManager::RemoveConfigs(int uid) {
81 vector<ConfigKey> removed;
82
83 for (auto it = mConfigs.begin(); it != mConfigs.end();) {
84 // Remove from map
85 if (it->first.GetUid() == uid) {
86 removed.push_back(it->first);
87 it = mConfigs.erase(it);
88 } else {
89 it++;
90 }
91 }
92
93 // Remove separately so if they do anything in the callback they can't mess up our iteration.
94 for (auto& key : removed) {
95 // Tell everyone
96 for (auto& listener : mListeners) {
97 listener->OnConfigRemoved(key);
98 }
99 }
100}
101
102void ConfigManager::Dump(FILE* out) {
103 fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
104 fprintf(out, " uid name\n");
105 for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin();
106 it != mConfigs.end(); it++) {
107 fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str());
108 // TODO: Print the contents of the config too.
109 }
110}
111
112void ConfigManager::update_saved_configs() {
113 // TODO: Implement me -- write to disk.
114}
115
116static StatsdConfig build_fake_config() {
117 // HACK: Hard code a test metric for counting screen on events...
118 StatsdConfig config;
119 config.set_config_id(12345L);
120
Yao Chen729093d2017-10-16 10:33:26 -0700121 int WAKE_LOCK_TAG_ID = 11;
122 int WAKE_LOCK_UID_KEY_ID = 1;
123 int WAKE_LOCK_STATE_KEY = 2;
124 int WAKE_LOCK_ACQUIRE_VALUE = 1;
125 int WAKE_LOCK_RELEASE_VALUE = 0;
126
127 int APP_USAGE_ID = 12345;
128 int APP_USAGE_UID_KEY_ID = 1;
129 int APP_USAGE_STATE_KEY = 2;
130 int APP_USAGE_FOREGROUND = 1;
131 int APP_USAGE_BACKGROUND = 0;
132
Bookatzc1a050a2017-10-10 15:49:28 -0700133 int SCREEN_EVENT_TAG_ID = 29;
Yao Chen729093d2017-10-16 10:33:26 -0700134 int SCREEN_EVENT_STATE_KEY = 1;
135 int SCREEN_EVENT_ON_VALUE = 2;
136 int SCREEN_EVENT_OFF_VALUE = 1;
137
Bookatzc1a050a2017-10-10 15:49:28 -0700138 int UID_PROCESS_STATE_TAG_ID = 27;
Yao Chen729093d2017-10-16 10:33:26 -0700139 int UID_PROCESS_STATE_UID_KEY = 1;
140
141 // Count Screen ON events.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700142 CountMetric* metric = config.add_count_metric();
Yao Chen729093d2017-10-16 10:33:26 -0700143 metric->set_metric_id(1);
144 metric->set_what("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700145 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
146
Bookatzd3606c72017-10-19 10:13:49 -0700147 // Anomaly threshold for screen-on count.
148 Alert* alert = metric->add_alerts();
149 alert->set_number_of_buckets(6);
150 alert->set_trigger_if_sum_gt(10);
151 alert->set_refractory_period_secs(30);
152
Yao Chen729093d2017-10-16 10:33:26 -0700153 // Count process state changes, slice by uid.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700154 metric = config.add_count_metric();
Yao Chen729093d2017-10-16 10:33:26 -0700155 metric->set_metric_id(2);
156 metric->set_what("PROCESS_STATE_CHANGE");
157 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
158 KeyMatcher* keyMatcher = metric->add_dimension();
159 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700160
Yao Chen729093d2017-10-16 10:33:26 -0700161 // Count process state changes, slice by uid, while SCREEN_IS_OFF
162 metric = config.add_count_metric();
163 metric->set_metric_id(3);
164 metric->set_what("PROCESS_STATE_CHANGE");
165 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
166 keyMatcher = metric->add_dimension();
167 keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
168 metric->set_condition("SCREEN_IS_OFF");
169
170 // Count wake lock, slice by uid, while SCREEN_IS_OFF and app in background
171 metric = config.add_count_metric();
172 metric->set_metric_id(4);
173 metric->set_what("APP_GET_WL");
174 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
175 keyMatcher = metric->add_dimension();
176 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
177 metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
178 EventConditionLink* link = metric->add_links();
179 link->set_condition("APP_IS_BACKGROUND");
180 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
181 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
182
183 // Duration of an app holding wl, while screen on and app in background
184 DurationMetric* durationMetric = config.add_duration_metric();
185 durationMetric->set_metric_id(5);
186 durationMetric->set_start("APP_GET_WL");
187 durationMetric->set_stop("APP_RELEASE_WL");
188 durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
189 durationMetric->set_type(DurationMetric_AggregationType_DURATION_SUM);
190 keyMatcher = durationMetric->add_dimension();
191 keyMatcher->set_key(WAKE_LOCK_UID_KEY_ID);
192 durationMetric->set_predicate("APP_IS_BACKGROUND_AND_SCREEN_ON");
193 link = durationMetric->add_links();
194 link->set_condition("APP_IS_BACKGROUND");
195 link->add_key_in_main()->set_key(WAKE_LOCK_UID_KEY_ID);
196 link->add_key_in_condition()->set_key(APP_USAGE_UID_KEY_ID);
197
198 // Event matchers............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700199 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700200 eventMatcher->set_name("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700201 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700202 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
203 KeyValueMatcher* keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
204 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
205 keyValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700206
207 eventMatcher = config.add_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700208 eventMatcher->set_name("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700209 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700210 simpleLogEntryMatcher->set_tag(SCREEN_EVENT_TAG_ID);
211 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
212 keyValueMatcher->mutable_key_matcher()->set_key(SCREEN_EVENT_STATE_KEY);
213 keyValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700214
Yao Chen729093d2017-10-16 10:33:26 -0700215 eventMatcher = config.add_log_entry_matcher();
216 eventMatcher->set_name("PROCESS_STATE_CHANGE");
217 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
218 simpleLogEntryMatcher->set_tag(UID_PROCESS_STATE_TAG_ID);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700219
Yao Chen729093d2017-10-16 10:33:26 -0700220 eventMatcher = config.add_log_entry_matcher();
221 eventMatcher->set_name("APP_GOES_BACKGROUND");
222 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
223 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
224 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
225 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
226 keyValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700227
Yao Chen729093d2017-10-16 10:33:26 -0700228 eventMatcher = config.add_log_entry_matcher();
229 eventMatcher->set_name("APP_GOES_FOREGROUND");
230 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
231 simpleLogEntryMatcher->set_tag(APP_USAGE_ID);
232 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
233 keyValueMatcher->mutable_key_matcher()->set_key(APP_USAGE_STATE_KEY);
234 keyValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700235
Yao Chen729093d2017-10-16 10:33:26 -0700236 eventMatcher = config.add_log_entry_matcher();
237 eventMatcher->set_name("APP_GET_WL");
238 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
239 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
240 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
241 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
242 keyValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700243
Yao Chen729093d2017-10-16 10:33:26 -0700244 eventMatcher = config.add_log_entry_matcher();
245 eventMatcher->set_name("APP_RELEASE_WL");
246 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
247 simpleLogEntryMatcher->set_tag(WAKE_LOCK_TAG_ID);
248 keyValueMatcher = simpleLogEntryMatcher->add_key_value_matcher();
249 keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY);
250 keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700251
Yao Chen729093d2017-10-16 10:33:26 -0700252 // Conditions.............
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700253 Condition* condition = config.add_condition();
254 condition->set_name("SCREEN_IS_ON");
255 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700256 simpleCondition->set_start("SCREEN_TURNED_ON");
257 simpleCondition->set_stop("SCREEN_TURNED_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700258
259 condition = config.add_condition();
260 condition->set_name("SCREEN_IS_OFF");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700261 simpleCondition = condition->mutable_simple_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700262 simpleCondition->set_start("SCREEN_TURNED_OFF");
263 simpleCondition->set_stop("SCREEN_TURNED_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700264
265 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700266 condition->set_name("APP_IS_BACKGROUND");
267 simpleCondition = condition->mutable_simple_condition();
268 simpleCondition->set_start("APP_GOES_BACKGROUND");
269 simpleCondition->set_stop("APP_GOES_FOREGROUND");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700270
271 condition = config.add_condition();
Yao Chen729093d2017-10-16 10:33:26 -0700272 condition->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
273 Condition_Combination* combination_condition = condition->mutable_combination();
274 combination_condition->set_operation(LogicalOperation::AND);
275 combination_condition->add_condition("APP_IS_BACKGROUND");
276 combination_condition->add_condition("SCREEN_IS_ON");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700277
278 return config;
279}
280
281} // namespace statsd
282} // namespace os
283} // namespace android