blob: 2a4d6e22a2edb872914dc14ef1f93581f4414107 [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
121 // One count metric to count screen on
122 CountMetric* metric = config.add_count_metric();
123 metric->set_metric_id(20150717L);
124 metric->set_what("SCREEN_IS_ON");
125 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
126
127 // One count metric to count PHOTO_CHANGE_OR_CHROME_CRASH
128 metric = config.add_count_metric();
129 metric->set_metric_id(20150718L);
130 metric->set_what("PHOTO_PROCESS_STATE_CHANGE");
131 metric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
132 metric->set_condition("SCREEN_IS_ON");
133
134 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
135 eventMatcher->set_name("SCREEN_IS_ON");
136
137 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
138 simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/);
139 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
140 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
141 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
142 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
143
144 eventMatcher = config.add_log_entry_matcher();
145 eventMatcher->set_name("SCREEN_IS_OFF");
146
147 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
148 simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/);
149 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
150 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
151 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
152 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
153
154 LogEntryMatcher* procEventMatcher = config.add_log_entry_matcher();
155 procEventMatcher->set_name("PHOTO_CRASH");
156
157 SimpleLogEntryMatcher* simpleLogMatcher2 = procEventMatcher->mutable_simple_log_entry_matcher();
158 simpleLogMatcher2->add_tag(1112 /*PROCESS_STATE_CHANGE*/);
159 KeyValueMatcher* keyValueMatcher = simpleLogMatcher2->add_key_value_matcher();
160 keyValueMatcher->mutable_key_matcher()->set_key(1002 /*pkg*/);
161 keyValueMatcher->set_eq_string(
162 "com.google.android.apps.photos" /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
163
164 keyValueMatcher = simpleLogMatcher2->add_key_value_matcher();
165 keyValueMatcher->mutable_key_matcher()->set_key(1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
166 keyValueMatcher->set_eq_int(2);
167
168 procEventMatcher = config.add_log_entry_matcher();
169 procEventMatcher->set_name("PHOTO_START");
170
171 simpleLogMatcher2 = procEventMatcher->mutable_simple_log_entry_matcher();
172 simpleLogMatcher2->add_tag(1112 /*PROCESS_STATE_CHANGE*/);
173 keyValueMatcher = simpleLogMatcher2->add_key_value_matcher();
174 keyValueMatcher->mutable_key_matcher()->set_key(1002 /*pkg*/);
175 keyValueMatcher->set_eq_string(
176 "com.google.android.apps.photos" /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
177
178 keyValueMatcher = simpleLogMatcher2->add_key_value_matcher();
179 keyValueMatcher->mutable_key_matcher()->set_key(1 /*STATE*/);
180 keyValueMatcher->set_eq_int(1);
181
182 procEventMatcher = config.add_log_entry_matcher();
183 procEventMatcher->set_name("PHOTO_PROCESS_STATE_CHANGE");
184 LogEntryMatcher_Combination* combinationMatcher = procEventMatcher->mutable_combination();
185 combinationMatcher->set_operation(LogicalOperation::OR);
186 combinationMatcher->add_matcher("PHOTO_START");
187 combinationMatcher->add_matcher("PHOTO_CRASH");
188
189 procEventMatcher = config.add_log_entry_matcher();
190 procEventMatcher->set_name("CHROME_CRASH");
191
192 simpleLogMatcher2 = procEventMatcher->mutable_simple_log_entry_matcher();
193 simpleLogMatcher2->add_tag(1112 /*PROCESS_STATE_CHANGE*/);
194 keyValueMatcher = simpleLogMatcher2->add_key_value_matcher();
195 keyValueMatcher->mutable_key_matcher()->set_key(1002 /*pkg*/);
196 keyValueMatcher->set_eq_string(
197 "com.android.chrome" /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
198
199 keyValueMatcher = simpleLogMatcher2->add_key_value_matcher();
200 keyValueMatcher->mutable_key_matcher()->set_key(1 /*STATE*/);
201 keyValueMatcher->set_eq_int(2);
202
203 procEventMatcher = config.add_log_entry_matcher();
204 procEventMatcher->set_name("PHOTO_CHANGE_OR_CHROME_CRASH");
205 combinationMatcher = procEventMatcher->mutable_combination();
206 combinationMatcher->set_operation(LogicalOperation::OR);
207 combinationMatcher->add_matcher("PHOTO_PROCESS_STATE_CHANGE");
208 combinationMatcher->add_matcher("CHROME_CRASH");
209
210 Condition* condition = config.add_condition();
211 condition->set_name("SCREEN_IS_ON");
212 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
213 simpleCondition->set_start("SCREEN_IS_ON");
214 simpleCondition->set_stop("SCREEN_IS_OFF");
215
216 condition = config.add_condition();
217 condition->set_name("PHOTO_STARTED");
218
219 simpleCondition = condition->mutable_simple_condition();
220 simpleCondition->set_start("PHOTO_START");
221 simpleCondition->set_stop("PHOTO_CRASH");
222
223 condition = config.add_condition();
224 condition->set_name("SCREEN_IS_OFF");
225
226 simpleCondition = condition->mutable_simple_condition();
227 simpleCondition->set_start("SCREEN_IS_OFF");
228 simpleCondition->set_stop("SCREEN_IS_ON");
229
230 condition = config.add_condition();
231 condition->set_name("SCREEN_IS_EITHER_ON_OFF");
232
233 Condition_Combination* combination = condition->mutable_combination();
234 combination->set_operation(LogicalOperation::OR);
235 combination->add_condition("SCREEN_IS_ON");
236 combination->add_condition("SCREEN_IS_OFF");
237
238 condition = config.add_condition();
239 condition->set_name("SCREEN_IS_NEITHER_ON_OFF");
240
241 combination = condition->mutable_combination();
242 combination->set_operation(LogicalOperation::NOR);
243 combination->add_condition("SCREEN_IS_ON");
244 combination->add_condition("SCREEN_IS_OFF");
245
246 return config;
247}
248
249} // namespace statsd
250} // namespace os
251} // namespace android