1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "metric_util.h"
16
17 namespace android {
18 namespace os {
19 namespace statsd {
20
CreateSimpleAtomMatcher(const string & name,int atomId)21 AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId) {
22 AtomMatcher atom_matcher;
23 atom_matcher.set_id(StringToId(name));
24 auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
25 simple_atom_matcher->set_atom_id(atomId);
26 return atom_matcher;
27 }
28
CreateScheduledJobStateChangedAtomMatcher(const string & name,ScheduledJobStateChanged::State state)29 AtomMatcher CreateScheduledJobStateChangedAtomMatcher(const string& name,
30 ScheduledJobStateChanged::State state) {
31 AtomMatcher atom_matcher;
32 atom_matcher.set_id(StringToId(name));
33 auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
34 simple_atom_matcher->set_atom_id(android::util::SCHEDULED_JOB_STATE_CHANGED);
35 auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
36 field_value_matcher->set_field(3); // State field.
37 field_value_matcher->set_eq_int(state);
38 return atom_matcher;
39 }
40
CreateStartScheduledJobAtomMatcher()41 AtomMatcher CreateStartScheduledJobAtomMatcher() {
42 return CreateScheduledJobStateChangedAtomMatcher("ScheduledJobStart",
43 ScheduledJobStateChanged::STARTED);
44 }
45
CreateFinishScheduledJobAtomMatcher()46 AtomMatcher CreateFinishScheduledJobAtomMatcher() {
47 return CreateScheduledJobStateChangedAtomMatcher("ScheduledJobFinish",
48 ScheduledJobStateChanged::FINISHED);
49 }
50
CreateScreenBrightnessChangedAtomMatcher()51 AtomMatcher CreateScreenBrightnessChangedAtomMatcher() {
52 AtomMatcher atom_matcher;
53 atom_matcher.set_id(StringToId("ScreenBrightnessChanged"));
54 auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
55 simple_atom_matcher->set_atom_id(android::util::SCREEN_BRIGHTNESS_CHANGED);
56 return atom_matcher;
57 }
58
CreateUidProcessStateChangedAtomMatcher()59 AtomMatcher CreateUidProcessStateChangedAtomMatcher() {
60 AtomMatcher atom_matcher;
61 atom_matcher.set_id(StringToId("UidProcessStateChanged"));
62 auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
63 simple_atom_matcher->set_atom_id(android::util::UID_PROCESS_STATE_CHANGED);
64 return atom_matcher;
65 }
66
CreateWakelockStateChangedAtomMatcher(const string & name,WakelockStateChanged::State state)67 AtomMatcher CreateWakelockStateChangedAtomMatcher(const string& name,
68 WakelockStateChanged::State state) {
69 AtomMatcher atom_matcher;
70 atom_matcher.set_id(StringToId(name));
71 auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
72 simple_atom_matcher->set_atom_id(android::util::WAKELOCK_STATE_CHANGED);
73 auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
74 field_value_matcher->set_field(4); // State field.
75 field_value_matcher->set_eq_int(state);
76 return atom_matcher;
77 }
78
CreateAcquireWakelockAtomMatcher()79 AtomMatcher CreateAcquireWakelockAtomMatcher() {
80 return CreateWakelockStateChangedAtomMatcher("AcquireWakelock", WakelockStateChanged::ACQUIRE);
81 }
82
CreateReleaseWakelockAtomMatcher()83 AtomMatcher CreateReleaseWakelockAtomMatcher() {
84 return CreateWakelockStateChangedAtomMatcher("ReleaseWakelock", WakelockStateChanged::RELEASE);
85 }
86
CreateScreenStateChangedAtomMatcher(const string & name,android::view::DisplayStateEnum state)87 AtomMatcher CreateScreenStateChangedAtomMatcher(
88 const string& name, android::view::DisplayStateEnum state) {
89 AtomMatcher atom_matcher;
90 atom_matcher.set_id(StringToId(name));
91 auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
92 simple_atom_matcher->set_atom_id(android::util::SCREEN_STATE_CHANGED);
93 auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
94 field_value_matcher->set_field(1); // State field.
95 field_value_matcher->set_eq_int(state);
96 return atom_matcher;
97 }
98
CreateScreenTurnedOnAtomMatcher()99 AtomMatcher CreateScreenTurnedOnAtomMatcher() {
100 return CreateScreenStateChangedAtomMatcher("ScreenTurnedOn",
101 android::view::DisplayStateEnum::DISPLAY_STATE_ON);
102 }
103
CreateScreenTurnedOffAtomMatcher()104 AtomMatcher CreateScreenTurnedOffAtomMatcher() {
105 return CreateScreenStateChangedAtomMatcher("ScreenTurnedOff",
106 ::android::view::DisplayStateEnum::DISPLAY_STATE_OFF);
107 }
108
CreateSyncStateChangedAtomMatcher(const string & name,SyncStateChanged::State state)109 AtomMatcher CreateSyncStateChangedAtomMatcher(
110 const string& name, SyncStateChanged::State state) {
111 AtomMatcher atom_matcher;
112 atom_matcher.set_id(StringToId(name));
113 auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
114 simple_atom_matcher->set_atom_id(android::util::SYNC_STATE_CHANGED);
115 auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
116 field_value_matcher->set_field(3); // State field.
117 field_value_matcher->set_eq_int(state);
118 return atom_matcher;
119 }
120
CreateSyncStartAtomMatcher()121 AtomMatcher CreateSyncStartAtomMatcher() {
122 return CreateSyncStateChangedAtomMatcher("SyncStart", SyncStateChanged::ON);
123 }
124
CreateSyncEndAtomMatcher()125 AtomMatcher CreateSyncEndAtomMatcher() {
126 return CreateSyncStateChangedAtomMatcher("SyncEnd", SyncStateChanged::OFF);
127 }
128
CreateActivityForegroundStateChangedAtomMatcher(const string & name,ActivityForegroundStateChanged::State state)129 AtomMatcher CreateActivityForegroundStateChangedAtomMatcher(
130 const string& name, ActivityForegroundStateChanged::State state) {
131 AtomMatcher atom_matcher;
132 atom_matcher.set_id(StringToId(name));
133 auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
134 simple_atom_matcher->set_atom_id(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED);
135 auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
136 field_value_matcher->set_field(4); // Activity field.
137 field_value_matcher->set_eq_int(state);
138 return atom_matcher;
139 }
140
CreateMoveToBackgroundAtomMatcher()141 AtomMatcher CreateMoveToBackgroundAtomMatcher() {
142 return CreateActivityForegroundStateChangedAtomMatcher(
143 "MoveToBackground", ActivityForegroundStateChanged::BACKGROUND);
144 }
145
CreateMoveToForegroundAtomMatcher()146 AtomMatcher CreateMoveToForegroundAtomMatcher() {
147 return CreateActivityForegroundStateChangedAtomMatcher(
148 "MoveToForeground", ActivityForegroundStateChanged::FOREGROUND);
149 }
150
CreateScheduledJobPredicate()151 Predicate CreateScheduledJobPredicate() {
152 Predicate predicate;
153 predicate.set_id(StringToId("ScheduledJobRunningPredicate"));
154 predicate.mutable_simple_predicate()->set_start(StringToId("ScheduledJobStart"));
155 predicate.mutable_simple_predicate()->set_stop(StringToId("ScheduledJobFinish"));
156 return predicate;
157 }
158
CreateBatterySaverModePredicate()159 Predicate CreateBatterySaverModePredicate() {
160 Predicate predicate;
161 predicate.set_id(StringToId("BatterySaverIsOn"));
162 predicate.mutable_simple_predicate()->set_start(StringToId("BatterySaverModeStart"));
163 predicate.mutable_simple_predicate()->set_stop(StringToId("BatterySaverModeStop"));
164 return predicate;
165 }
166
CreateScreenIsOnPredicate()167 Predicate CreateScreenIsOnPredicate() {
168 Predicate predicate;
169 predicate.set_id(StringToId("ScreenIsOn"));
170 predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOn"));
171 predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOff"));
172 return predicate;
173 }
174
CreateScreenIsOffPredicate()175 Predicate CreateScreenIsOffPredicate() {
176 Predicate predicate;
177 predicate.set_id(1111123);
178 predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOff"));
179 predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOn"));
180 return predicate;
181 }
182
CreateHoldingWakelockPredicate()183 Predicate CreateHoldingWakelockPredicate() {
184 Predicate predicate;
185 predicate.set_id(StringToId("HoldingWakelock"));
186 predicate.mutable_simple_predicate()->set_start(StringToId("AcquireWakelock"));
187 predicate.mutable_simple_predicate()->set_stop(StringToId("ReleaseWakelock"));
188 return predicate;
189 }
190
CreateIsSyncingPredicate()191 Predicate CreateIsSyncingPredicate() {
192 Predicate predicate;
193 predicate.set_id(33333333333333);
194 predicate.mutable_simple_predicate()->set_start(StringToId("SyncStart"));
195 predicate.mutable_simple_predicate()->set_stop(StringToId("SyncEnd"));
196 return predicate;
197 }
198
CreateIsInBackgroundPredicate()199 Predicate CreateIsInBackgroundPredicate() {
200 Predicate predicate;
201 predicate.set_id(StringToId("IsInBackground"));
202 predicate.mutable_simple_predicate()->set_start(StringToId("MoveToBackground"));
203 predicate.mutable_simple_predicate()->set_stop(StringToId("MoveToForeground"));
204 return predicate;
205 }
206
addPredicateToPredicateCombination(const Predicate & predicate,Predicate * combinationPredicate)207 void addPredicateToPredicateCombination(const Predicate& predicate,
208 Predicate* combinationPredicate) {
209 combinationPredicate->mutable_combination()->add_predicate(predicate.id());
210 }
211
CreateAttributionUidDimensions(const int atomId,const std::vector<Position> & positions)212 FieldMatcher CreateAttributionUidDimensions(const int atomId,
213 const std::vector<Position>& positions) {
214 FieldMatcher dimensions;
215 dimensions.set_field(atomId);
216 for (const auto position : positions) {
217 auto child = dimensions.add_child();
218 child->set_field(1);
219 child->set_position(position);
220 child->add_child()->set_field(1);
221 }
222 return dimensions;
223 }
224
CreateAttributionUidAndTagDimensions(const int atomId,const std::vector<Position> & positions)225 FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId,
226 const std::vector<Position>& positions) {
227 FieldMatcher dimensions;
228 dimensions.set_field(atomId);
229 for (const auto position : positions) {
230 auto child = dimensions.add_child();
231 child->set_field(1);
232 child->set_position(position);
233 child->add_child()->set_field(1);
234 child->add_child()->set_field(2);
235 }
236 return dimensions;
237 }
238
CreateDimensions(const int atomId,const std::vector<int> & fields)239 FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields) {
240 FieldMatcher dimensions;
241 dimensions.set_field(atomId);
242 for (const int field : fields) {
243 dimensions.add_child()->set_field(field);
244 }
245 return dimensions;
246 }
247
CreateScreenStateChangedEvent(const android::view::DisplayStateEnum state,uint64_t timestampNs)248 std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
249 const android::view::DisplayStateEnum state, uint64_t timestampNs) {
250 auto event = std::make_unique<LogEvent>(android::util::SCREEN_STATE_CHANGED, timestampNs);
251 event->write(state);
252 event->init();
253 return event;
254 }
255
CreateScreenBrightnessChangedEvent(int level,uint64_t timestampNs)256 std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(
257 int level, uint64_t timestampNs) {
258 auto event = std::make_unique<LogEvent>(android::util::SCREEN_BRIGHTNESS_CHANGED, timestampNs);
259 (event->write(level));
260 event->init();
261 return event;
262
263 }
264
CreateScheduledJobStateChangedEvent(const std::vector<AttributionNodeInternal> & attributions,const string & jobName,const ScheduledJobStateChanged::State state,uint64_t timestampNs)265 std::unique_ptr<LogEvent> CreateScheduledJobStateChangedEvent(
266 const std::vector<AttributionNodeInternal>& attributions, const string& jobName,
267 const ScheduledJobStateChanged::State state, uint64_t timestampNs) {
268 auto event = std::make_unique<LogEvent>(android::util::SCHEDULED_JOB_STATE_CHANGED, timestampNs);
269 event->write(attributions);
270 event->write(jobName);
271 event->write(state);
272 event->init();
273 return event;
274 }
275
CreateStartScheduledJobEvent(const std::vector<AttributionNodeInternal> & attributions,const string & name,uint64_t timestampNs)276 std::unique_ptr<LogEvent> CreateStartScheduledJobEvent(
277 const std::vector<AttributionNodeInternal>& attributions,
278 const string& name, uint64_t timestampNs) {
279 return CreateScheduledJobStateChangedEvent(
280 attributions, name, ScheduledJobStateChanged::STARTED, timestampNs);
281 }
282
283 // Create log event when scheduled job finishes.
CreateFinishScheduledJobEvent(const std::vector<AttributionNodeInternal> & attributions,const string & name,uint64_t timestampNs)284 std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent(
285 const std::vector<AttributionNodeInternal>& attributions,
286 const string& name, uint64_t timestampNs) {
287 return CreateScheduledJobStateChangedEvent(
288 attributions, name, ScheduledJobStateChanged::FINISHED, timestampNs);
289 }
290
CreateWakelockStateChangedEvent(const std::vector<AttributionNodeInternal> & attributions,const string & wakelockName,const WakelockStateChanged::State state,uint64_t timestampNs)291 std::unique_ptr<LogEvent> CreateWakelockStateChangedEvent(
292 const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
293 const WakelockStateChanged::State state, uint64_t timestampNs) {
294 auto event = std::make_unique<LogEvent>(android::util::WAKELOCK_STATE_CHANGED, timestampNs);
295 event->write(attributions);
296 event->write(android::os::WakeLockLevelEnum::PARTIAL_WAKE_LOCK);
297 event->write(wakelockName);
298 event->write(state);
299 event->init();
300 return event;
301 }
302
CreateAcquireWakelockEvent(const std::vector<AttributionNodeInternal> & attributions,const string & wakelockName,uint64_t timestampNs)303 std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(
304 const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
305 uint64_t timestampNs) {
306 return CreateWakelockStateChangedEvent(
307 attributions, wakelockName, WakelockStateChanged::ACQUIRE, timestampNs);
308 }
309
CreateReleaseWakelockEvent(const std::vector<AttributionNodeInternal> & attributions,const string & wakelockName,uint64_t timestampNs)310 std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(
311 const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
312 uint64_t timestampNs) {
313 return CreateWakelockStateChangedEvent(
314 attributions, wakelockName, WakelockStateChanged::RELEASE, timestampNs);
315 }
316
CreateActivityForegroundStateChangedEvent(const int uid,const ActivityForegroundStateChanged::State state,uint64_t timestampNs)317 std::unique_ptr<LogEvent> CreateActivityForegroundStateChangedEvent(
318 const int uid, const ActivityForegroundStateChanged::State state, uint64_t timestampNs) {
319 auto event = std::make_unique<LogEvent>(
320 android::util::ACTIVITY_FOREGROUND_STATE_CHANGED, timestampNs);
321 event->write(uid);
322 event->write("pkg_name");
323 event->write("class_name");
324 event->write(state);
325 event->init();
326 return event;
327 }
328
CreateMoveToBackgroundEvent(const int uid,uint64_t timestampNs)329 std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(const int uid, uint64_t timestampNs) {
330 return CreateActivityForegroundStateChangedEvent(
331 uid, ActivityForegroundStateChanged::BACKGROUND, timestampNs);
332 }
333
CreateMoveToForegroundEvent(const int uid,uint64_t timestampNs)334 std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(const int uid, uint64_t timestampNs) {
335 return CreateActivityForegroundStateChangedEvent(
336 uid, ActivityForegroundStateChanged::FOREGROUND, timestampNs);
337 }
338
CreateSyncStateChangedEvent(const std::vector<AttributionNodeInternal> & attributions,const string & name,const SyncStateChanged::State state,uint64_t timestampNs)339 std::unique_ptr<LogEvent> CreateSyncStateChangedEvent(
340 const std::vector<AttributionNodeInternal>& attributions, const string& name,
341 const SyncStateChanged::State state, uint64_t timestampNs) {
342 auto event = std::make_unique<LogEvent>(android::util::SYNC_STATE_CHANGED, timestampNs);
343 event->write(attributions);
344 event->write(name);
345 event->write(state);
346 event->init();
347 return event;
348 }
349
CreateSyncStartEvent(const std::vector<AttributionNodeInternal> & attributions,const string & name,uint64_t timestampNs)350 std::unique_ptr<LogEvent> CreateSyncStartEvent(
351 const std::vector<AttributionNodeInternal>& attributions, const string& name,
352 uint64_t timestampNs) {
353 return CreateSyncStateChangedEvent(attributions, name, SyncStateChanged::ON, timestampNs);
354 }
355
CreateSyncEndEvent(const std::vector<AttributionNodeInternal> & attributions,const string & name,uint64_t timestampNs)356 std::unique_ptr<LogEvent> CreateSyncEndEvent(
357 const std::vector<AttributionNodeInternal>& attributions, const string& name,
358 uint64_t timestampNs) {
359 return CreateSyncStateChangedEvent(attributions, name, SyncStateChanged::OFF, timestampNs);
360 }
361
CreateStatsLogProcessor(const long timeBaseSec,const StatsdConfig & config,const ConfigKey & key)362 sp<StatsLogProcessor> CreateStatsLogProcessor(const long timeBaseSec, const StatsdConfig& config,
363 const ConfigKey& key) {
364 sp<UidMap> uidMap = new UidMap();
365 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
366 sp<AlarmMonitor> anomalyAlarmMonitor;
367 sp<AlarmMonitor> periodicAlarmMonitor;
368 sp<StatsLogProcessor> processor =
369 new StatsLogProcessor(uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor,
370 timeBaseSec * NS_PER_SEC, [](const ConfigKey&) { return true; },
371 [](const int&, const vector<int64_t>&) { return true; });
372 processor->OnConfigUpdated(timeBaseSec * NS_PER_SEC, key, config);
373 return processor;
374 }
375
CreateAttribution(const int & uid,const string & tag)376 AttributionNodeInternal CreateAttribution(const int& uid, const string& tag) {
377 AttributionNodeInternal attribution;
378 attribution.set_uid(uid);
379 attribution.set_tag(tag);
380 return attribution;
381 }
382
sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> * events)383 void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events) {
384 std::sort(events->begin(), events->end(),
385 [](const std::unique_ptr<LogEvent>& a, const std::unique_ptr<LogEvent>& b) {
386 return a->GetElapsedTimestampNs() < b->GetElapsedTimestampNs();
387 });
388 }
389
StringToId(const string & str)390 int64_t StringToId(const string& str) {
391 return static_cast<int64_t>(std::hash<std::string>()(str));
392 }
393
394
395 } // namespace statsd
396 } // namespace os
397 } // namespace android
398