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 "statsd_test_util.h"
16 
17 #include <aidl/android/util/StatsEventParcel.h>
18 #include "stats_event.h"
19 
20 using aidl::android::util::StatsEventParcel;
21 using std::shared_ptr;
22 
23 namespace android {
24 namespace os {
25 namespace statsd {
26 
outputStreamToProto(ProtoOutputStream * proto)27 StatsLogReport outputStreamToProto(ProtoOutputStream* proto) {
28     vector<uint8_t> bytes;
29     bytes.resize(proto->size());
30     size_t pos = 0;
31     sp<ProtoReader> reader = proto->data();
32 
33     while (reader->readBuffer() != NULL) {
34         size_t toRead = reader->currentToRead();
35         std::memcpy(&((bytes)[pos]), reader->readBuffer(), toRead);
36         pos += toRead;
37         reader->move(toRead);
38     }
39 
40     StatsLogReport report;
41     report.ParseFromArray(bytes.data(), bytes.size());
42     return report;
43 }
44 
CreateSimpleAtomMatcher(const string & name,int atomId)45 AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId) {
46     AtomMatcher atom_matcher;
47     atom_matcher.set_id(StringToId(name));
48     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
49     simple_atom_matcher->set_atom_id(atomId);
50     return atom_matcher;
51 }
52 
CreateTemperatureAtomMatcher()53 AtomMatcher CreateTemperatureAtomMatcher() {
54     return CreateSimpleAtomMatcher("TemperatureMatcher", util::TEMPERATURE);
55 }
56 
CreateScheduledJobStateChangedAtomMatcher(const string & name,ScheduledJobStateChanged::State state)57 AtomMatcher CreateScheduledJobStateChangedAtomMatcher(const string& name,
58                                                       ScheduledJobStateChanged::State state) {
59     AtomMatcher atom_matcher;
60     atom_matcher.set_id(StringToId(name));
61     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
62     simple_atom_matcher->set_atom_id(util::SCHEDULED_JOB_STATE_CHANGED);
63     auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
64     field_value_matcher->set_field(3);  // State field.
65     field_value_matcher->set_eq_int(state);
66     return atom_matcher;
67 }
68 
CreateStartScheduledJobAtomMatcher()69 AtomMatcher CreateStartScheduledJobAtomMatcher() {
70     return CreateScheduledJobStateChangedAtomMatcher("ScheduledJobStart",
71                                                      ScheduledJobStateChanged::STARTED);
72 }
73 
CreateFinishScheduledJobAtomMatcher()74 AtomMatcher CreateFinishScheduledJobAtomMatcher() {
75     return CreateScheduledJobStateChangedAtomMatcher("ScheduledJobFinish",
76                                                      ScheduledJobStateChanged::FINISHED);
77 }
78 
CreateScreenBrightnessChangedAtomMatcher()79 AtomMatcher CreateScreenBrightnessChangedAtomMatcher() {
80     AtomMatcher atom_matcher;
81     atom_matcher.set_id(StringToId("ScreenBrightnessChanged"));
82     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
83     simple_atom_matcher->set_atom_id(util::SCREEN_BRIGHTNESS_CHANGED);
84     return atom_matcher;
85 }
86 
CreateUidProcessStateChangedAtomMatcher()87 AtomMatcher CreateUidProcessStateChangedAtomMatcher() {
88     AtomMatcher atom_matcher;
89     atom_matcher.set_id(StringToId("UidProcessStateChanged"));
90     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
91     simple_atom_matcher->set_atom_id(util::UID_PROCESS_STATE_CHANGED);
92     return atom_matcher;
93 }
94 
CreateWakelockStateChangedAtomMatcher(const string & name,WakelockStateChanged::State state)95 AtomMatcher CreateWakelockStateChangedAtomMatcher(const string& name,
96                                                   WakelockStateChanged::State state) {
97     AtomMatcher atom_matcher;
98     atom_matcher.set_id(StringToId(name));
99     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
100     simple_atom_matcher->set_atom_id(util::WAKELOCK_STATE_CHANGED);
101     auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
102     field_value_matcher->set_field(4);  // State field.
103     field_value_matcher->set_eq_int(state);
104     return atom_matcher;
105 }
106 
CreateAcquireWakelockAtomMatcher()107 AtomMatcher CreateAcquireWakelockAtomMatcher() {
108     return CreateWakelockStateChangedAtomMatcher("AcquireWakelock", WakelockStateChanged::ACQUIRE);
109 }
110 
CreateReleaseWakelockAtomMatcher()111 AtomMatcher CreateReleaseWakelockAtomMatcher() {
112     return CreateWakelockStateChangedAtomMatcher("ReleaseWakelock", WakelockStateChanged::RELEASE);
113 }
114 
CreateBatterySaverModeStateChangedAtomMatcher(const string & name,BatterySaverModeStateChanged::State state)115 AtomMatcher CreateBatterySaverModeStateChangedAtomMatcher(
116     const string& name, BatterySaverModeStateChanged::State state) {
117     AtomMatcher atom_matcher;
118     atom_matcher.set_id(StringToId(name));
119     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
120     simple_atom_matcher->set_atom_id(util::BATTERY_SAVER_MODE_STATE_CHANGED);
121     auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
122     field_value_matcher->set_field(1);  // State field.
123     field_value_matcher->set_eq_int(state);
124     return atom_matcher;
125 }
126 
CreateBatterySaverModeStartAtomMatcher()127 AtomMatcher CreateBatterySaverModeStartAtomMatcher() {
128     return CreateBatterySaverModeStateChangedAtomMatcher(
129         "BatterySaverModeStart", BatterySaverModeStateChanged::ON);
130 }
131 
132 
CreateBatterySaverModeStopAtomMatcher()133 AtomMatcher CreateBatterySaverModeStopAtomMatcher() {
134     return CreateBatterySaverModeStateChangedAtomMatcher(
135         "BatterySaverModeStop", BatterySaverModeStateChanged::OFF);
136 }
137 
CreateBatteryStateChangedAtomMatcher(const string & name,BatteryPluggedStateEnum state)138 AtomMatcher CreateBatteryStateChangedAtomMatcher(const string& name,
139                                                  BatteryPluggedStateEnum state) {
140     AtomMatcher atom_matcher;
141     atom_matcher.set_id(StringToId(name));
142     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
143     simple_atom_matcher->set_atom_id(util::PLUGGED_STATE_CHANGED);
144     auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
145     field_value_matcher->set_field(1);  // State field.
146     field_value_matcher->set_eq_int(state);
147     return atom_matcher;
148 }
149 
CreateBatteryStateNoneMatcher()150 AtomMatcher CreateBatteryStateNoneMatcher() {
151     return CreateBatteryStateChangedAtomMatcher("BatteryPluggedNone",
152                                                 BatteryPluggedStateEnum::BATTERY_PLUGGED_NONE);
153 }
154 
CreateBatteryStateUsbMatcher()155 AtomMatcher CreateBatteryStateUsbMatcher() {
156     return CreateBatteryStateChangedAtomMatcher("BatteryPluggedUsb",
157                                                 BatteryPluggedStateEnum::BATTERY_PLUGGED_USB);
158 }
159 
CreateScreenStateChangedAtomMatcher(const string & name,android::view::DisplayStateEnum state)160 AtomMatcher CreateScreenStateChangedAtomMatcher(
161     const string& name, android::view::DisplayStateEnum state) {
162     AtomMatcher atom_matcher;
163     atom_matcher.set_id(StringToId(name));
164     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
165     simple_atom_matcher->set_atom_id(util::SCREEN_STATE_CHANGED);
166     auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
167     field_value_matcher->set_field(1);  // State field.
168     field_value_matcher->set_eq_int(state);
169     return atom_matcher;
170 }
171 
CreateScreenTurnedOnAtomMatcher()172 AtomMatcher CreateScreenTurnedOnAtomMatcher() {
173     return CreateScreenStateChangedAtomMatcher("ScreenTurnedOn",
174             android::view::DisplayStateEnum::DISPLAY_STATE_ON);
175 }
176 
CreateScreenTurnedOffAtomMatcher()177 AtomMatcher CreateScreenTurnedOffAtomMatcher() {
178     return CreateScreenStateChangedAtomMatcher("ScreenTurnedOff",
179             ::android::view::DisplayStateEnum::DISPLAY_STATE_OFF);
180 }
181 
CreateSyncStateChangedAtomMatcher(const string & name,SyncStateChanged::State state)182 AtomMatcher CreateSyncStateChangedAtomMatcher(
183     const string& name, SyncStateChanged::State state) {
184     AtomMatcher atom_matcher;
185     atom_matcher.set_id(StringToId(name));
186     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
187     simple_atom_matcher->set_atom_id(util::SYNC_STATE_CHANGED);
188     auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
189     field_value_matcher->set_field(3);  // State field.
190     field_value_matcher->set_eq_int(state);
191     return atom_matcher;
192 }
193 
CreateSyncStartAtomMatcher()194 AtomMatcher CreateSyncStartAtomMatcher() {
195     return CreateSyncStateChangedAtomMatcher("SyncStart", SyncStateChanged::ON);
196 }
197 
CreateSyncEndAtomMatcher()198 AtomMatcher CreateSyncEndAtomMatcher() {
199     return CreateSyncStateChangedAtomMatcher("SyncEnd", SyncStateChanged::OFF);
200 }
201 
CreateActivityForegroundStateChangedAtomMatcher(const string & name,ActivityForegroundStateChanged::State state)202 AtomMatcher CreateActivityForegroundStateChangedAtomMatcher(
203     const string& name, ActivityForegroundStateChanged::State state) {
204     AtomMatcher atom_matcher;
205     atom_matcher.set_id(StringToId(name));
206     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
207     simple_atom_matcher->set_atom_id(util::ACTIVITY_FOREGROUND_STATE_CHANGED);
208     auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
209     field_value_matcher->set_field(4);  // Activity field.
210     field_value_matcher->set_eq_int(state);
211     return atom_matcher;
212 }
213 
CreateMoveToBackgroundAtomMatcher()214 AtomMatcher CreateMoveToBackgroundAtomMatcher() {
215     return CreateActivityForegroundStateChangedAtomMatcher(
216         "Background", ActivityForegroundStateChanged::BACKGROUND);
217 }
218 
CreateMoveToForegroundAtomMatcher()219 AtomMatcher CreateMoveToForegroundAtomMatcher() {
220     return CreateActivityForegroundStateChangedAtomMatcher(
221         "Foreground", ActivityForegroundStateChanged::FOREGROUND);
222 }
223 
CreateProcessLifeCycleStateChangedAtomMatcher(const string & name,ProcessLifeCycleStateChanged::State state)224 AtomMatcher CreateProcessLifeCycleStateChangedAtomMatcher(
225     const string& name, ProcessLifeCycleStateChanged::State state) {
226     AtomMatcher atom_matcher;
227     atom_matcher.set_id(StringToId(name));
228     auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
229     simple_atom_matcher->set_atom_id(util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
230     auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
231     field_value_matcher->set_field(3);  // Process state field.
232     field_value_matcher->set_eq_int(state);
233     return atom_matcher;
234 }
235 
CreateProcessCrashAtomMatcher()236 AtomMatcher CreateProcessCrashAtomMatcher() {
237     return CreateProcessLifeCycleStateChangedAtomMatcher(
238         "Crashed", ProcessLifeCycleStateChanged::CRASHED);
239 }
240 
CreateScheduledJobPredicate()241 Predicate CreateScheduledJobPredicate() {
242     Predicate predicate;
243     predicate.set_id(StringToId("ScheduledJobRunningPredicate"));
244     predicate.mutable_simple_predicate()->set_start(StringToId("ScheduledJobStart"));
245     predicate.mutable_simple_predicate()->set_stop(StringToId("ScheduledJobFinish"));
246     return predicate;
247 }
248 
CreateBatterySaverModePredicate()249 Predicate CreateBatterySaverModePredicate() {
250     Predicate predicate;
251     predicate.set_id(StringToId("BatterySaverIsOn"));
252     predicate.mutable_simple_predicate()->set_start(StringToId("BatterySaverModeStart"));
253     predicate.mutable_simple_predicate()->set_stop(StringToId("BatterySaverModeStop"));
254     return predicate;
255 }
256 
CreateDeviceUnpluggedPredicate()257 Predicate CreateDeviceUnpluggedPredicate() {
258     Predicate predicate;
259     predicate.set_id(StringToId("DeviceUnplugged"));
260     predicate.mutable_simple_predicate()->set_start(StringToId("BatteryPluggedNone"));
261     predicate.mutable_simple_predicate()->set_stop(StringToId("BatteryPluggedUsb"));
262     return predicate;
263 }
264 
CreateScreenIsOnPredicate()265 Predicate CreateScreenIsOnPredicate() {
266     Predicate predicate;
267     predicate.set_id(StringToId("ScreenIsOn"));
268     predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOn"));
269     predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOff"));
270     return predicate;
271 }
272 
CreateScreenIsOffPredicate()273 Predicate CreateScreenIsOffPredicate() {
274     Predicate predicate;
275     predicate.set_id(1111123);
276     predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOff"));
277     predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOn"));
278     return predicate;
279 }
280 
CreateHoldingWakelockPredicate()281 Predicate CreateHoldingWakelockPredicate() {
282     Predicate predicate;
283     predicate.set_id(StringToId("HoldingWakelock"));
284     predicate.mutable_simple_predicate()->set_start(StringToId("AcquireWakelock"));
285     predicate.mutable_simple_predicate()->set_stop(StringToId("ReleaseWakelock"));
286     return predicate;
287 }
288 
CreateIsSyncingPredicate()289 Predicate CreateIsSyncingPredicate() {
290     Predicate predicate;
291     predicate.set_id(33333333333333);
292     predicate.mutable_simple_predicate()->set_start(StringToId("SyncStart"));
293     predicate.mutable_simple_predicate()->set_stop(StringToId("SyncEnd"));
294     return predicate;
295 }
296 
CreateIsInBackgroundPredicate()297 Predicate CreateIsInBackgroundPredicate() {
298     Predicate predicate;
299     predicate.set_id(StringToId("IsInBackground"));
300     predicate.mutable_simple_predicate()->set_start(StringToId("Background"));
301     predicate.mutable_simple_predicate()->set_stop(StringToId("Foreground"));
302     return predicate;
303 }
304 
CreateScreenState()305 State CreateScreenState() {
306     State state;
307     state.set_id(StringToId("ScreenState"));
308     state.set_atom_id(util::SCREEN_STATE_CHANGED);
309     return state;
310 }
311 
CreateUidProcessState()312 State CreateUidProcessState() {
313     State state;
314     state.set_id(StringToId("UidProcessState"));
315     state.set_atom_id(util::UID_PROCESS_STATE_CHANGED);
316     return state;
317 }
318 
CreateOverlayState()319 State CreateOverlayState() {
320     State state;
321     state.set_id(StringToId("OverlayState"));
322     state.set_atom_id(util::OVERLAY_STATE_CHANGED);
323     return state;
324 }
325 
CreateScreenStateWithOnOffMap(int64_t screenOnId,int64_t screenOffId)326 State CreateScreenStateWithOnOffMap(int64_t screenOnId, int64_t screenOffId) {
327     State state;
328     state.set_id(StringToId("ScreenStateOnOff"));
329     state.set_atom_id(util::SCREEN_STATE_CHANGED);
330 
331     auto map = CreateScreenStateOnOffMap(screenOnId, screenOffId);
332     *state.mutable_map() = map;
333 
334     return state;
335 }
336 
CreateScreenStateWithSimpleOnOffMap(int64_t screenOnId,int64_t screenOffId)337 State CreateScreenStateWithSimpleOnOffMap(int64_t screenOnId, int64_t screenOffId) {
338     State state;
339     state.set_id(StringToId("ScreenStateSimpleOnOff"));
340     state.set_atom_id(util::SCREEN_STATE_CHANGED);
341 
342     auto map = CreateScreenStateSimpleOnOffMap(screenOnId, screenOffId);
343     *state.mutable_map() = map;
344 
345     return state;
346 }
347 
CreateScreenStateOnGroup(int64_t screenOnId)348 StateMap_StateGroup CreateScreenStateOnGroup(int64_t screenOnId) {
349     StateMap_StateGroup group;
350     group.set_group_id(screenOnId);
351     group.add_value(android::view::DisplayStateEnum::DISPLAY_STATE_ON);
352     group.add_value(android::view::DisplayStateEnum::DISPLAY_STATE_VR);
353     group.add_value(android::view::DisplayStateEnum::DISPLAY_STATE_ON_SUSPEND);
354     return group;
355 }
356 
CreateScreenStateOffGroup(int64_t screenOffId)357 StateMap_StateGroup CreateScreenStateOffGroup(int64_t screenOffId) {
358     StateMap_StateGroup group;
359     group.set_group_id(screenOffId);
360     group.add_value(android::view::DisplayStateEnum::DISPLAY_STATE_OFF);
361     group.add_value(android::view::DisplayStateEnum::DISPLAY_STATE_DOZE);
362     group.add_value(android::view::DisplayStateEnum::DISPLAY_STATE_DOZE_SUSPEND);
363     return group;
364 }
365 
CreateScreenStateSimpleOnGroup(int64_t screenOnId)366 StateMap_StateGroup CreateScreenStateSimpleOnGroup(int64_t screenOnId) {
367     StateMap_StateGroup group;
368     group.set_group_id(screenOnId);
369     group.add_value(android::view::DisplayStateEnum::DISPLAY_STATE_ON);
370     return group;
371 }
372 
CreateScreenStateSimpleOffGroup(int64_t screenOffId)373 StateMap_StateGroup CreateScreenStateSimpleOffGroup(int64_t screenOffId) {
374     StateMap_StateGroup group;
375     group.set_group_id(screenOffId);
376     group.add_value(android::view::DisplayStateEnum::DISPLAY_STATE_OFF);
377     return group;
378 }
379 
CreateScreenStateOnOffMap(int64_t screenOnId,int64_t screenOffId)380 StateMap CreateScreenStateOnOffMap(int64_t screenOnId, int64_t screenOffId) {
381     StateMap map;
382     *map.add_group() = CreateScreenStateOnGroup(screenOnId);
383     *map.add_group() = CreateScreenStateOffGroup(screenOffId);
384     return map;
385 }
386 
CreateScreenStateSimpleOnOffMap(int64_t screenOnId,int64_t screenOffId)387 StateMap CreateScreenStateSimpleOnOffMap(int64_t screenOnId, int64_t screenOffId) {
388     StateMap map;
389     *map.add_group() = CreateScreenStateSimpleOnGroup(screenOnId);
390     *map.add_group() = CreateScreenStateSimpleOffGroup(screenOffId);
391     return map;
392 }
393 
addPredicateToPredicateCombination(const Predicate & predicate,Predicate * combinationPredicate)394 void addPredicateToPredicateCombination(const Predicate& predicate,
395                                         Predicate* combinationPredicate) {
396     combinationPredicate->mutable_combination()->add_predicate(predicate.id());
397 }
398 
CreateAttributionUidDimensions(const int atomId,const std::vector<Position> & positions)399 FieldMatcher CreateAttributionUidDimensions(const int atomId,
400                                             const std::vector<Position>& positions) {
401     FieldMatcher dimensions;
402     dimensions.set_field(atomId);
403     for (const auto position : positions) {
404         auto child = dimensions.add_child();
405         child->set_field(1);
406         child->set_position(position);
407         child->add_child()->set_field(1);
408     }
409     return dimensions;
410 }
411 
CreateAttributionUidAndTagDimensions(const int atomId,const std::vector<Position> & positions)412 FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId,
413                                                  const std::vector<Position>& positions) {
414     FieldMatcher dimensions;
415     dimensions.set_field(atomId);
416     for (const auto position : positions) {
417         auto child = dimensions.add_child();
418         child->set_field(1);
419         child->set_position(position);
420         child->add_child()->set_field(1);
421         child->add_child()->set_field(2);
422     }
423     return dimensions;
424 }
425 
CreateDimensions(const int atomId,const std::vector<int> & fields)426 FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields) {
427     FieldMatcher dimensions;
428     dimensions.set_field(atomId);
429     for (const int field : fields) {
430         dimensions.add_child()->set_field(field);
431     }
432     return dimensions;
433 }
434 
CreateAttributionUidAndOtherDimensions(const int atomId,const std::vector<Position> & positions,const std::vector<int> & fields)435 FieldMatcher CreateAttributionUidAndOtherDimensions(const int atomId,
436                                                     const std::vector<Position>& positions,
437                                                     const std::vector<int>& fields) {
438     FieldMatcher dimensions = CreateAttributionUidDimensions(atomId, positions);
439 
440     for (const int field : fields) {
441         dimensions.add_child()->set_field(field);
442     }
443     return dimensions;
444 }
445 
446 // START: get primary key functions
getUidProcessKey(int uid,HashableDimensionKey * key)447 void getUidProcessKey(int uid, HashableDimensionKey* key) {
448     int pos1[] = {1, 0, 0};
449     Field field1(27 /* atom id */, pos1, 0 /* depth */);
450     Value value1((int32_t)uid);
451 
452     key->addValue(FieldValue(field1, value1));
453 }
454 
getOverlayKey(int uid,string packageName,HashableDimensionKey * key)455 void getOverlayKey(int uid, string packageName, HashableDimensionKey* key) {
456     int pos1[] = {1, 0, 0};
457     int pos2[] = {2, 0, 0};
458 
459     Field field1(59 /* atom id */, pos1, 0 /* depth */);
460     Field field2(59 /* atom id */, pos2, 0 /* depth */);
461 
462     Value value1((int32_t)uid);
463     Value value2(packageName);
464 
465     key->addValue(FieldValue(field1, value1));
466     key->addValue(FieldValue(field2, value2));
467 }
468 
getPartialWakelockKey(int uid,const std::string & tag,HashableDimensionKey * key)469 void getPartialWakelockKey(int uid, const std::string& tag, HashableDimensionKey* key) {
470     int pos1[] = {1, 1, 1};
471     int pos3[] = {2, 0, 0};
472     int pos4[] = {3, 0, 0};
473 
474     Field field1(10 /* atom id */, pos1, 2 /* depth */);
475 
476     Field field3(10 /* atom id */, pos3, 0 /* depth */);
477     Field field4(10 /* atom id */, pos4, 0 /* depth */);
478 
479     Value value1((int32_t)uid);
480     Value value3((int32_t)1 /*partial*/);
481     Value value4(tag);
482 
483     key->addValue(FieldValue(field1, value1));
484     key->addValue(FieldValue(field3, value3));
485     key->addValue(FieldValue(field4, value4));
486 }
487 
getPartialWakelockKey(int uid,HashableDimensionKey * key)488 void getPartialWakelockKey(int uid, HashableDimensionKey* key) {
489     int pos1[] = {1, 1, 1};
490     int pos3[] = {2, 0, 0};
491 
492     Field field1(10 /* atom id */, pos1, 2 /* depth */);
493     Field field3(10 /* atom id */, pos3, 0 /* depth */);
494 
495     Value value1((int32_t)uid);
496     Value value3((int32_t)1 /*partial*/);
497 
498     key->addValue(FieldValue(field1, value1));
499     key->addValue(FieldValue(field3, value3));
500 }
501 // END: get primary key functions
502 
writeAttribution(AStatsEvent * statsEvent,const vector<int> & attributionUids,const vector<string> & attributionTags)503 void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids,
504                       const vector<string>& attributionTags) {
505     vector<const char*> cTags(attributionTags.size());
506     for (int i = 0; i < cTags.size(); i++) {
507         cTags[i] = attributionTags[i].c_str();
508     }
509 
510     AStatsEvent_writeAttributionChain(statsEvent,
511                                       reinterpret_cast<const uint32_t*>(attributionUids.data()),
512                                       cTags.data(), attributionUids.size());
513 }
514 
parseStatsEventToLogEvent(AStatsEvent * statsEvent,LogEvent * logEvent)515 void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent) {
516     AStatsEvent_build(statsEvent);
517 
518     size_t size;
519     uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
520     logEvent->parseBuffer(buf, size);
521 
522     AStatsEvent_release(statsEvent);
523 }
524 
CreateTwoValueLogEvent(LogEvent * logEvent,int atomId,int64_t eventTimeNs,int32_t value1,int32_t value2)525 void CreateTwoValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1,
526                             int32_t value2) {
527     AStatsEvent* statsEvent = AStatsEvent_obtain();
528     AStatsEvent_setAtomId(statsEvent, atomId);
529     AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
530 
531     AStatsEvent_writeInt32(statsEvent, value1);
532     AStatsEvent_writeInt32(statsEvent, value2);
533 
534     parseStatsEventToLogEvent(statsEvent, logEvent);
535 }
536 
CreateTwoValueLogEvent(int atomId,int64_t eventTimeNs,int32_t value1,int32_t value2)537 shared_ptr<LogEvent> CreateTwoValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1,
538                                             int32_t value2) {
539     shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
540     CreateTwoValueLogEvent(logEvent.get(), atomId, eventTimeNs, value1, value2);
541     return logEvent;
542 }
543 
CreateThreeValueLogEvent(LogEvent * logEvent,int atomId,int64_t eventTimeNs,int32_t value1,int32_t value2,int32_t value3)544 void CreateThreeValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1,
545                               int32_t value2, int32_t value3) {
546     AStatsEvent* statsEvent = AStatsEvent_obtain();
547     AStatsEvent_setAtomId(statsEvent, atomId);
548     AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
549 
550     AStatsEvent_writeInt32(statsEvent, value1);
551     AStatsEvent_writeInt32(statsEvent, value2);
552     AStatsEvent_writeInt32(statsEvent, value3);
553 
554     parseStatsEventToLogEvent(statsEvent, logEvent);
555 }
556 
CreateThreeValueLogEvent(int atomId,int64_t eventTimeNs,int32_t value1,int32_t value2,int32_t value3)557 shared_ptr<LogEvent> CreateThreeValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1,
558                                               int32_t value2, int32_t value3) {
559     shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
560     CreateThreeValueLogEvent(logEvent.get(), atomId, eventTimeNs, value1, value2, value3);
561     return logEvent;
562 }
563 
CreateRepeatedValueLogEvent(LogEvent * logEvent,int atomId,int64_t eventTimeNs,int32_t value)564 void CreateRepeatedValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs,
565                                  int32_t value) {
566     AStatsEvent* statsEvent = AStatsEvent_obtain();
567     AStatsEvent_setAtomId(statsEvent, atomId);
568     AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
569 
570     AStatsEvent_writeInt32(statsEvent, value);
571     AStatsEvent_writeInt32(statsEvent, value);
572 
573     parseStatsEventToLogEvent(statsEvent, logEvent);
574 }
575 
CreateRepeatedValueLogEvent(int atomId,int64_t eventTimeNs,int32_t value)576 shared_ptr<LogEvent> CreateRepeatedValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value) {
577     shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
578     CreateRepeatedValueLogEvent(logEvent.get(), atomId, eventTimeNs, value);
579     return logEvent;
580 }
581 
CreateNoValuesLogEvent(LogEvent * logEvent,int atomId,int64_t eventTimeNs)582 void CreateNoValuesLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs) {
583     AStatsEvent* statsEvent = AStatsEvent_obtain();
584     AStatsEvent_setAtomId(statsEvent, atomId);
585     AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
586 
587     parseStatsEventToLogEvent(statsEvent, logEvent);
588 }
589 
CreateNoValuesLogEvent(int atomId,int64_t eventTimeNs)590 shared_ptr<LogEvent> CreateNoValuesLogEvent(int atomId, int64_t eventTimeNs) {
591     shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
592     CreateNoValuesLogEvent(logEvent.get(), atomId, eventTimeNs);
593     return logEvent;
594 }
595 
makeUidLogEvent(int atomId,int64_t eventTimeNs,int uid,int data1,int data2)596 shared_ptr<LogEvent> makeUidLogEvent(int atomId, int64_t eventTimeNs, int uid, int data1,
597                                      int data2) {
598     AStatsEvent* statsEvent = AStatsEvent_obtain();
599     AStatsEvent_setAtomId(statsEvent, atomId);
600     AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
601 
602     AStatsEvent_writeInt32(statsEvent, uid);
603     AStatsEvent_addBoolAnnotation(statsEvent, ANNOTATION_ID_IS_UID, true);
604     AStatsEvent_writeInt32(statsEvent, data1);
605     AStatsEvent_writeInt32(statsEvent, data2);
606 
607     shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
608     parseStatsEventToLogEvent(statsEvent, logEvent.get());
609     return logEvent;
610 }
611 
makeAttributionLogEvent(int atomId,int64_t eventTimeNs,const vector<int> & uids,const vector<string> & tags,int data1,int data2)612 shared_ptr<LogEvent> makeAttributionLogEvent(int atomId, int64_t eventTimeNs,
613                                              const vector<int>& uids, const vector<string>& tags,
614                                              int data1, int data2) {
615     AStatsEvent* statsEvent = AStatsEvent_obtain();
616     AStatsEvent_setAtomId(statsEvent, atomId);
617     AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
618 
619     writeAttribution(statsEvent, uids, tags);
620     AStatsEvent_writeInt32(statsEvent, data1);
621     AStatsEvent_writeInt32(statsEvent, data2);
622 
623     shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
624     parseStatsEventToLogEvent(statsEvent, logEvent.get());
625     return logEvent;
626 }
627 
makeMockUidMapForOneHost(int hostUid,const vector<int> & isolatedUids)628 sp<MockUidMap> makeMockUidMapForOneHost(int hostUid, const vector<int>& isolatedUids) {
629     sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>();
630     EXPECT_CALL(*uidMap, getHostUidOrSelf(_)).WillRepeatedly(ReturnArg<0>());
631     for (const int isolatedUid : isolatedUids) {
632         EXPECT_CALL(*uidMap, getHostUidOrSelf(isolatedUid)).WillRepeatedly(Return(hostUid));
633     }
634 
635     return uidMap;
636 }
637 
makeMockUidMapForPackage(const string & pkg,const set<int32_t> & uids)638 sp<MockUidMap> makeMockUidMapForPackage(const string& pkg, const set<int32_t>& uids) {
639     sp<MockUidMap> uidMap = new StrictMock<MockUidMap>();
640     EXPECT_CALL(*uidMap, getAppUid(_)).Times(AnyNumber());
641     EXPECT_CALL(*uidMap, getAppUid(pkg)).WillRepeatedly(Return(uids));
642 
643     return uidMap;
644 }
645 
CreateScreenStateChangedEvent(uint64_t timestampNs,const android::view::DisplayStateEnum state,int loggerUid)646 std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(uint64_t timestampNs,
647                                                         const android::view::DisplayStateEnum state,
648                                                         int loggerUid) {
649     AStatsEvent* statsEvent = AStatsEvent_obtain();
650     AStatsEvent_setAtomId(statsEvent, util::SCREEN_STATE_CHANGED);
651     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
652     AStatsEvent_writeInt32(statsEvent, state);
653     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true);
654     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, false);
655 
656     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(loggerUid, /*pid=*/0);
657     parseStatsEventToLogEvent(statsEvent, logEvent.get());
658     return logEvent;
659 }
660 
CreateBatterySaverOnEvent(uint64_t timestampNs)661 std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs) {
662     AStatsEvent* statsEvent = AStatsEvent_obtain();
663     AStatsEvent_setAtomId(statsEvent, util::BATTERY_SAVER_MODE_STATE_CHANGED);
664     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
665     AStatsEvent_writeInt32(statsEvent, BatterySaverModeStateChanged::ON);
666     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true);
667     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, false);
668 
669     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
670     parseStatsEventToLogEvent(statsEvent, logEvent.get());
671     return logEvent;
672 }
673 
CreateBatterySaverOffEvent(uint64_t timestampNs)674 std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs) {
675     AStatsEvent* statsEvent = AStatsEvent_obtain();
676     AStatsEvent_setAtomId(statsEvent, util::BATTERY_SAVER_MODE_STATE_CHANGED);
677     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
678     AStatsEvent_writeInt32(statsEvent, BatterySaverModeStateChanged::OFF);
679     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true);
680     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, false);
681 
682     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
683     parseStatsEventToLogEvent(statsEvent, logEvent.get());
684     return logEvent;
685 }
686 
CreateBatteryStateChangedEvent(const uint64_t timestampNs,const BatteryPluggedStateEnum state)687 std::unique_ptr<LogEvent> CreateBatteryStateChangedEvent(const uint64_t timestampNs, const BatteryPluggedStateEnum state) {
688     AStatsEvent* statsEvent = AStatsEvent_obtain();
689     AStatsEvent_setAtomId(statsEvent, util::PLUGGED_STATE_CHANGED);
690     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
691     AStatsEvent_writeInt32(statsEvent, state);
692 
693     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
694     parseStatsEventToLogEvent(statsEvent, logEvent.get());
695     return logEvent;
696 }
697 
CreateScreenBrightnessChangedEvent(uint64_t timestampNs,int level)698 std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(uint64_t timestampNs, int level) {
699     AStatsEvent* statsEvent = AStatsEvent_obtain();
700     AStatsEvent_setAtomId(statsEvent, util::SCREEN_BRIGHTNESS_CHANGED);
701     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
702     AStatsEvent_writeInt32(statsEvent, level);
703 
704     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
705     parseStatsEventToLogEvent(statsEvent, logEvent.get());
706     return logEvent;
707 }
708 
CreateScheduledJobStateChangedEvent(const vector<int> & attributionUids,const vector<string> & attributionTags,const string & jobName,const ScheduledJobStateChanged::State state,uint64_t timestampNs)709 std::unique_ptr<LogEvent> CreateScheduledJobStateChangedEvent(
710         const vector<int>& attributionUids, const vector<string>& attributionTags,
711         const string& jobName, const ScheduledJobStateChanged::State state, uint64_t timestampNs) {
712     AStatsEvent* statsEvent = AStatsEvent_obtain();
713     AStatsEvent_setAtomId(statsEvent, util::SCHEDULED_JOB_STATE_CHANGED);
714     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
715 
716     writeAttribution(statsEvent, attributionUids, attributionTags);
717     AStatsEvent_writeString(statsEvent, jobName.c_str());
718     AStatsEvent_writeInt32(statsEvent, state);
719 
720     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
721     parseStatsEventToLogEvent(statsEvent, logEvent.get());
722     return logEvent;
723 }
724 
CreateStartScheduledJobEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const string & jobName)725 std::unique_ptr<LogEvent> CreateStartScheduledJobEvent(uint64_t timestampNs,
726                                                        const vector<int>& attributionUids,
727                                                        const vector<string>& attributionTags,
728                                                        const string& jobName) {
729     return CreateScheduledJobStateChangedEvent(attributionUids, attributionTags, jobName,
730                                                ScheduledJobStateChanged::STARTED, timestampNs);
731 }
732 
733 // Create log event when scheduled job finishes.
CreateFinishScheduledJobEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const string & jobName)734 std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent(uint64_t timestampNs,
735                                                         const vector<int>& attributionUids,
736                                                         const vector<string>& attributionTags,
737                                                         const string& jobName) {
738     return CreateScheduledJobStateChangedEvent(attributionUids, attributionTags, jobName,
739                                                ScheduledJobStateChanged::FINISHED, timestampNs);
740 }
741 
CreateWakelockStateChangedEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const string & wakelockName,const WakelockStateChanged::State state)742 std::unique_ptr<LogEvent> CreateWakelockStateChangedEvent(uint64_t timestampNs,
743                                                           const vector<int>& attributionUids,
744                                                           const vector<string>& attributionTags,
745                                                           const string& wakelockName,
746                                                           const WakelockStateChanged::State state) {
747     AStatsEvent* statsEvent = AStatsEvent_obtain();
748     AStatsEvent_setAtomId(statsEvent, util::WAKELOCK_STATE_CHANGED);
749     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
750 
751     writeAttribution(statsEvent, attributionUids, attributionTags);
752     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID, true);
753     AStatsEvent_writeInt32(statsEvent, android::os::WakeLockLevelEnum::PARTIAL_WAKE_LOCK);
754     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true);
755     AStatsEvent_writeString(statsEvent, wakelockName.c_str());
756     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true);
757     AStatsEvent_writeInt32(statsEvent, state);
758     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true);
759     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, true);
760 
761     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
762     parseStatsEventToLogEvent(statsEvent, logEvent.get());
763     return logEvent;
764 }
765 
CreateAcquireWakelockEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const string & wakelockName)766 std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(uint64_t timestampNs,
767                                                      const vector<int>& attributionUids,
768                                                      const vector<string>& attributionTags,
769                                                      const string& wakelockName) {
770     return CreateWakelockStateChangedEvent(timestampNs, attributionUids, attributionTags,
771                                            wakelockName, WakelockStateChanged::ACQUIRE);
772 }
773 
CreateReleaseWakelockEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const string & wakelockName)774 std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(uint64_t timestampNs,
775                                                      const vector<int>& attributionUids,
776                                                      const vector<string>& attributionTags,
777                                                      const string& wakelockName) {
778     return CreateWakelockStateChangedEvent(timestampNs, attributionUids, attributionTags,
779                                            wakelockName, WakelockStateChanged::RELEASE);
780 }
781 
CreateActivityForegroundStateChangedEvent(uint64_t timestampNs,const int uid,const ActivityForegroundStateChanged::State state)782 std::unique_ptr<LogEvent> CreateActivityForegroundStateChangedEvent(
783         uint64_t timestampNs, const int uid, const ActivityForegroundStateChanged::State state) {
784     AStatsEvent* statsEvent = AStatsEvent_obtain();
785     AStatsEvent_setAtomId(statsEvent, util::ACTIVITY_FOREGROUND_STATE_CHANGED);
786     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
787 
788     AStatsEvent_writeInt32(statsEvent, uid);
789     AStatsEvent_writeString(statsEvent, "pkg_name");
790     AStatsEvent_writeString(statsEvent, "class_name");
791     AStatsEvent_writeInt32(statsEvent, state);
792 
793     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
794     parseStatsEventToLogEvent(statsEvent, logEvent.get());
795     return logEvent;
796 }
797 
CreateMoveToBackgroundEvent(uint64_t timestampNs,const int uid)798 std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(uint64_t timestampNs, const int uid) {
799     return CreateActivityForegroundStateChangedEvent(timestampNs, uid,
800                                                      ActivityForegroundStateChanged::BACKGROUND);
801 }
802 
CreateMoveToForegroundEvent(uint64_t timestampNs,const int uid)803 std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(uint64_t timestampNs, const int uid) {
804     return CreateActivityForegroundStateChangedEvent(timestampNs, uid,
805                                                      ActivityForegroundStateChanged::FOREGROUND);
806 }
807 
CreateSyncStateChangedEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const string & name,const SyncStateChanged::State state)808 std::unique_ptr<LogEvent> CreateSyncStateChangedEvent(uint64_t timestampNs,
809                                                       const vector<int>& attributionUids,
810                                                       const vector<string>& attributionTags,
811                                                       const string& name,
812                                                       const SyncStateChanged::State state) {
813     AStatsEvent* statsEvent = AStatsEvent_obtain();
814     AStatsEvent_setAtomId(statsEvent, util::SYNC_STATE_CHANGED);
815     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
816 
817     writeAttribution(statsEvent, attributionUids, attributionTags);
818     AStatsEvent_writeString(statsEvent, name.c_str());
819     AStatsEvent_writeInt32(statsEvent, state);
820 
821     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
822     parseStatsEventToLogEvent(statsEvent, logEvent.get());
823     return logEvent;
824 }
825 
CreateSyncStartEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const string & name)826 std::unique_ptr<LogEvent> CreateSyncStartEvent(uint64_t timestampNs,
827                                                const vector<int>& attributionUids,
828                                                const vector<string>& attributionTags,
829                                                const string& name) {
830     return CreateSyncStateChangedEvent(timestampNs, attributionUids, attributionTags, name,
831                                        SyncStateChanged::ON);
832 }
833 
CreateSyncEndEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const string & name)834 std::unique_ptr<LogEvent> CreateSyncEndEvent(uint64_t timestampNs,
835                                              const vector<int>& attributionUids,
836                                              const vector<string>& attributionTags,
837                                              const string& name) {
838     return CreateSyncStateChangedEvent(timestampNs, attributionUids, attributionTags, name,
839                                        SyncStateChanged::OFF);
840 }
841 
CreateProcessLifeCycleStateChangedEvent(uint64_t timestampNs,const int uid,const ProcessLifeCycleStateChanged::State state)842 std::unique_ptr<LogEvent> CreateProcessLifeCycleStateChangedEvent(
843         uint64_t timestampNs, const int uid, const ProcessLifeCycleStateChanged::State state) {
844     AStatsEvent* statsEvent = AStatsEvent_obtain();
845     AStatsEvent_setAtomId(statsEvent, util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
846     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
847 
848     AStatsEvent_writeInt32(statsEvent, uid);
849     AStatsEvent_writeString(statsEvent, "");
850     AStatsEvent_writeInt32(statsEvent, state);
851 
852     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
853     parseStatsEventToLogEvent(statsEvent, logEvent.get());
854     return logEvent;
855 }
856 
CreateAppCrashEvent(uint64_t timestampNs,const int uid)857 std::unique_ptr<LogEvent> CreateAppCrashEvent(uint64_t timestampNs, const int uid) {
858     return CreateProcessLifeCycleStateChangedEvent(timestampNs, uid,
859                                                    ProcessLifeCycleStateChanged::CRASHED);
860 }
861 
CreateAppCrashOccurredEvent(uint64_t timestampNs,const int uid)862 std::unique_ptr<LogEvent> CreateAppCrashOccurredEvent(uint64_t timestampNs, const int uid) {
863     AStatsEvent* statsEvent = AStatsEvent_obtain();
864     AStatsEvent_setAtomId(statsEvent, util::APP_CRASH_OCCURRED);
865     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
866 
867     AStatsEvent_writeInt32(statsEvent, uid);
868     AStatsEvent_writeString(statsEvent, "eventType");
869     AStatsEvent_writeString(statsEvent, "processName");
870 
871     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
872     parseStatsEventToLogEvent(statsEvent, logEvent.get());
873     return logEvent;
874 }
875 
CreateIsolatedUidChangedEvent(uint64_t timestampNs,int hostUid,int isolatedUid,bool is_create)876 std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(uint64_t timestampNs, int hostUid,
877                                                         int isolatedUid, bool is_create) {
878     AStatsEvent* statsEvent = AStatsEvent_obtain();
879     AStatsEvent_setAtomId(statsEvent, util::ISOLATED_UID_CHANGED);
880     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
881 
882     AStatsEvent_writeInt32(statsEvent, hostUid);
883     AStatsEvent_writeInt32(statsEvent, isolatedUid);
884     AStatsEvent_writeInt32(statsEvent, is_create);
885 
886     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
887     parseStatsEventToLogEvent(statsEvent, logEvent.get());
888     return logEvent;
889 }
890 
CreateUidProcessStateChangedEvent(uint64_t timestampNs,int uid,const android::app::ProcessStateEnum state)891 std::unique_ptr<LogEvent> CreateUidProcessStateChangedEvent(
892         uint64_t timestampNs, int uid, const android::app::ProcessStateEnum state) {
893     AStatsEvent* statsEvent = AStatsEvent_obtain();
894     AStatsEvent_setAtomId(statsEvent, util::UID_PROCESS_STATE_CHANGED);
895     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
896 
897     AStatsEvent_writeInt32(statsEvent, uid);
898     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_IS_UID, true);
899     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true);
900     AStatsEvent_writeInt32(statsEvent, state);
901     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true);
902     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, false);
903 
904     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
905     parseStatsEventToLogEvent(statsEvent, logEvent.get());
906     return logEvent;
907 }
908 
CreateBleScanStateChangedEvent(uint64_t timestampNs,const vector<int> & attributionUids,const vector<string> & attributionTags,const BleScanStateChanged::State state,const bool filtered,const bool firstMatch,const bool opportunistic)909 std::unique_ptr<LogEvent> CreateBleScanStateChangedEvent(uint64_t timestampNs,
910                                                          const vector<int>& attributionUids,
911                                                          const vector<string>& attributionTags,
912                                                          const BleScanStateChanged::State state,
913                                                          const bool filtered, const bool firstMatch,
914                                                          const bool opportunistic) {
915     AStatsEvent* statsEvent = AStatsEvent_obtain();
916     AStatsEvent_setAtomId(statsEvent, util::BLE_SCAN_STATE_CHANGED);
917     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
918 
919     writeAttribution(statsEvent, attributionUids, attributionTags);
920     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID, true);
921     AStatsEvent_writeInt32(statsEvent, state);
922     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true);
923     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, true);
924     if (state == util::BLE_SCAN_STATE_CHANGED__STATE__RESET) {
925         AStatsEvent_addInt32Annotation(statsEvent, ANNOTATION_ID_TRIGGER_STATE_RESET,
926                                        util::BLE_SCAN_STATE_CHANGED__STATE__OFF);
927     }
928     AStatsEvent_writeBool(statsEvent, filtered);  // filtered
929     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true);
930     AStatsEvent_writeBool(statsEvent, firstMatch);  // first match
931     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true);
932     AStatsEvent_writeBool(statsEvent, opportunistic);  // opportunistic
933     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true);
934 
935     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
936     parseStatsEventToLogEvent(statsEvent, logEvent.get());
937     return logEvent;
938 }
939 
CreateOverlayStateChangedEvent(int64_t timestampNs,const int32_t uid,const string & packageName,const bool usingAlertWindow,const OverlayStateChanged::State state)940 std::unique_ptr<LogEvent> CreateOverlayStateChangedEvent(int64_t timestampNs, const int32_t uid,
941                                                          const string& packageName,
942                                                          const bool usingAlertWindow,
943                                                          const OverlayStateChanged::State state) {
944     AStatsEvent* statsEvent = AStatsEvent_obtain();
945     AStatsEvent_setAtomId(statsEvent, util::OVERLAY_STATE_CHANGED);
946     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
947 
948     AStatsEvent_writeInt32(statsEvent, uid);
949     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_IS_UID, true);
950     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true);
951     AStatsEvent_writeString(statsEvent, packageName.c_str());
952     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true);
953     AStatsEvent_writeBool(statsEvent, usingAlertWindow);
954     AStatsEvent_writeInt32(statsEvent, state);
955     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true);
956     AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, false);
957 
958     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
959     parseStatsEventToLogEvent(statsEvent, logEvent.get());
960     return logEvent;
961 }
962 
CreateStatsLogProcessor(const int64_t timeBaseNs,const int64_t currentTimeNs,const StatsdConfig & config,const ConfigKey & key,const shared_ptr<IPullAtomCallback> & puller,const int32_t atomTag,const sp<UidMap> uidMap)963 sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs,
964                                               const StatsdConfig& config, const ConfigKey& key,
965                                               const shared_ptr<IPullAtomCallback>& puller,
966                                               const int32_t atomTag, const sp<UidMap> uidMap) {
967     sp<StatsPullerManager> pullerManager = new StatsPullerManager();
968     if (puller != nullptr) {
969         pullerManager->RegisterPullAtomCallback(/*uid=*/0, atomTag, NS_PER_SEC, NS_PER_SEC * 10, {},
970                                                 puller);
971     }
972     sp<AlarmMonitor> anomalyAlarmMonitor =
973         new AlarmMonitor(1,
974                          [](const shared_ptr<IStatsCompanionService>&, int64_t){},
975                          [](const shared_ptr<IStatsCompanionService>&){});
976     sp<AlarmMonitor> periodicAlarmMonitor =
977         new AlarmMonitor(1,
978                          [](const shared_ptr<IStatsCompanionService>&, int64_t){},
979                          [](const shared_ptr<IStatsCompanionService>&){});
980     sp<StatsLogProcessor> processor =
981             new StatsLogProcessor(uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor,
982                                   timeBaseNs, [](const ConfigKey&) { return true; },
983                                   [](const int&, const vector<int64_t>&) {return true;});
984     processor->OnConfigUpdated(currentTimeNs, key, config);
985     return processor;
986 }
987 
sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> * events)988 void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events) {
989   std::sort(events->begin(), events->end(),
990             [](const std::unique_ptr<LogEvent>& a, const std::unique_ptr<LogEvent>& b) {
991               return a->GetElapsedTimestampNs() < b->GetElapsedTimestampNs();
992             });
993 }
994 
StringToId(const string & str)995 int64_t StringToId(const string& str) {
996     return static_cast<int64_t>(std::hash<std::string>()(str));
997 }
998 
ValidateWakelockAttributionUidAndTagDimension(const DimensionsValue & value,const int atomId,const int uid,const string & tag)999 void ValidateWakelockAttributionUidAndTagDimension(const DimensionsValue& value, const int atomId,
1000                                                    const int uid, const string& tag) {
1001     EXPECT_EQ(value.field(), atomId);
1002     ASSERT_EQ(value.value_tuple().dimensions_value_size(), 2);
1003     // Attribution field.
1004     EXPECT_EQ(value.value_tuple().dimensions_value(0).field(), 1);
1005     // Uid field.
1006     ASSERT_EQ(value.value_tuple().dimensions_value(0).value_tuple().dimensions_value_size(), 1);
1007     EXPECT_EQ(value.value_tuple().dimensions_value(0).value_tuple().dimensions_value(0).field(), 1);
1008     EXPECT_EQ(value.value_tuple().dimensions_value(0).value_tuple().dimensions_value(0).value_int(),
1009               uid);
1010     // Tag field.
1011     EXPECT_EQ(value.value_tuple().dimensions_value(1).field(), 3);
1012     EXPECT_EQ(value.value_tuple().dimensions_value(1).value_str(), tag);
1013 }
1014 
ValidateAttributionUidDimension(const DimensionsValue & value,int atomId,int uid)1015 void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid) {
1016     EXPECT_EQ(value.field(), atomId);
1017     ASSERT_EQ(value.value_tuple().dimensions_value_size(), 1);
1018     // Attribution field.
1019     EXPECT_EQ(value.value_tuple().dimensions_value(0).field(), 1);
1020     // Uid only.
1021     EXPECT_EQ(value.value_tuple().dimensions_value(0)
1022         .value_tuple().dimensions_value_size(), 1);
1023     EXPECT_EQ(value.value_tuple().dimensions_value(0)
1024         .value_tuple().dimensions_value(0).field(), 1);
1025     EXPECT_EQ(value.value_tuple().dimensions_value(0)
1026         .value_tuple().dimensions_value(0).value_int(), uid);
1027 }
1028 
ValidateUidDimension(const DimensionsValue & value,int node_idx,int atomId,int uid)1029 void ValidateUidDimension(const DimensionsValue& value, int node_idx, int atomId, int uid) {
1030     EXPECT_EQ(value.field(), atomId);
1031     ASSERT_GT(value.value_tuple().dimensions_value_size(), node_idx);
1032     // Attribution field.
1033     EXPECT_EQ(value.value_tuple().dimensions_value(node_idx).field(), 1);
1034     EXPECT_EQ(value.value_tuple().dimensions_value(node_idx)
1035         .value_tuple().dimensions_value(0).field(), 1);
1036     EXPECT_EQ(value.value_tuple().dimensions_value(node_idx)
1037         .value_tuple().dimensions_value(0).value_int(), uid);
1038 }
1039 
ValidateAttributionUidAndTagDimension(const DimensionsValue & value,int node_idx,int atomId,int uid,const std::string & tag)1040 void ValidateAttributionUidAndTagDimension(
1041     const DimensionsValue& value, int node_idx, int atomId, int uid, const std::string& tag) {
1042     EXPECT_EQ(value.field(), atomId);
1043     ASSERT_GT(value.value_tuple().dimensions_value_size(), node_idx);
1044     // Attribution field.
1045     EXPECT_EQ(1, value.value_tuple().dimensions_value(node_idx).field());
1046     // Uid only.
1047     EXPECT_EQ(2, value.value_tuple().dimensions_value(node_idx)
1048         .value_tuple().dimensions_value_size());
1049     EXPECT_EQ(1, value.value_tuple().dimensions_value(node_idx)
1050         .value_tuple().dimensions_value(0).field());
1051     EXPECT_EQ(uid, value.value_tuple().dimensions_value(node_idx)
1052         .value_tuple().dimensions_value(0).value_int());
1053     EXPECT_EQ(2, value.value_tuple().dimensions_value(node_idx)
1054         .value_tuple().dimensions_value(1).field());
1055     EXPECT_EQ(tag, value.value_tuple().dimensions_value(node_idx)
1056         .value_tuple().dimensions_value(1).value_str());
1057 }
1058 
ValidateAttributionUidAndTagDimension(const DimensionsValue & value,int atomId,int uid,const std::string & tag)1059 void ValidateAttributionUidAndTagDimension(
1060     const DimensionsValue& value, int atomId, int uid, const std::string& tag) {
1061     EXPECT_EQ(value.field(), atomId);
1062     ASSERT_EQ(1, value.value_tuple().dimensions_value_size());
1063     // Attribution field.
1064     EXPECT_EQ(1, value.value_tuple().dimensions_value(0).field());
1065     // Uid only.
1066     EXPECT_EQ(value.value_tuple().dimensions_value(0)
1067         .value_tuple().dimensions_value_size(), 2);
1068     EXPECT_EQ(value.value_tuple().dimensions_value(0)
1069         .value_tuple().dimensions_value(0).field(), 1);
1070     EXPECT_EQ(value.value_tuple().dimensions_value(0)
1071         .value_tuple().dimensions_value(0).value_int(), uid);
1072     EXPECT_EQ(value.value_tuple().dimensions_value(0)
1073         .value_tuple().dimensions_value(1).field(), 2);
1074     EXPECT_EQ(value.value_tuple().dimensions_value(0)
1075         .value_tuple().dimensions_value(1).value_str(), tag);
1076 }
1077 
EqualsTo(const DimensionsValue & s1,const DimensionsValue & s2)1078 bool EqualsTo(const DimensionsValue& s1, const DimensionsValue& s2) {
1079     if (s1.field() != s2.field()) {
1080         return false;
1081     }
1082     if (s1.value_case() != s2.value_case()) {
1083         return false;
1084     }
1085     switch (s1.value_case()) {
1086         case DimensionsValue::ValueCase::kValueStr:
1087             return (s1.value_str() == s2.value_str());
1088         case DimensionsValue::ValueCase::kValueInt:
1089             return s1.value_int() == s2.value_int();
1090         case DimensionsValue::ValueCase::kValueLong:
1091             return s1.value_long() == s2.value_long();
1092         case DimensionsValue::ValueCase::kValueBool:
1093             return s1.value_bool() == s2.value_bool();
1094         case DimensionsValue::ValueCase::kValueFloat:
1095             return s1.value_float() == s2.value_float();
1096         case DimensionsValue::ValueCase::kValueTuple: {
1097             if (s1.value_tuple().dimensions_value_size() !=
1098                 s2.value_tuple().dimensions_value_size()) {
1099                 return false;
1100             }
1101             bool allMatched = true;
1102             for (int i = 0; allMatched && i < s1.value_tuple().dimensions_value_size(); ++i) {
1103                 allMatched &= EqualsTo(s1.value_tuple().dimensions_value(i),
1104                                        s2.value_tuple().dimensions_value(i));
1105             }
1106             return allMatched;
1107         }
1108         case DimensionsValue::ValueCase::VALUE_NOT_SET:
1109         default:
1110             return true;
1111     }
1112 }
1113 
LessThan(const google::protobuf::RepeatedPtrField<StateValue> & s1,const google::protobuf::RepeatedPtrField<StateValue> & s2)1114 bool LessThan(const google::protobuf::RepeatedPtrField<StateValue>& s1,
1115               const google::protobuf::RepeatedPtrField<StateValue>& s2) {
1116     if (s1.size() != s2.size()) {
1117         return s1.size() < s2.size();
1118     }
1119     for (int i = 0; i < s1.size(); i++) {
1120         const StateValue& state1 = s1[i];
1121         const StateValue& state2 = s2[i];
1122         if (state1.atom_id() != state2.atom_id()) {
1123             return state1.atom_id() < state2.atom_id();
1124         }
1125         if (state1.value() != state2.value()) {
1126             return state1.value() < state2.value();
1127         }
1128         if (state1.group_id() != state2.group_id()) {
1129             return state1.group_id() < state2.group_id();
1130         }
1131     }
1132     return false;
1133 }
1134 
LessThan(const DimensionsValue & s1,const DimensionsValue & s2)1135 bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2) {
1136     if (s1.field() != s2.field()) {
1137         return s1.field() < s2.field();
1138     }
1139     if (s1.value_case() != s2.value_case()) {
1140         return s1.value_case() < s2.value_case();
1141     }
1142     switch (s1.value_case()) {
1143         case DimensionsValue::ValueCase::kValueStr:
1144             return s1.value_str() < s2.value_str();
1145         case DimensionsValue::ValueCase::kValueInt:
1146             return s1.value_int() < s2.value_int();
1147         case DimensionsValue::ValueCase::kValueLong:
1148             return s1.value_long() < s2.value_long();
1149         case DimensionsValue::ValueCase::kValueBool:
1150             return (int)s1.value_bool() < (int)s2.value_bool();
1151         case DimensionsValue::ValueCase::kValueFloat:
1152             return s1.value_float() < s2.value_float();
1153         case DimensionsValue::ValueCase::kValueTuple: {
1154             if (s1.value_tuple().dimensions_value_size() !=
1155                 s2.value_tuple().dimensions_value_size()) {
1156                 return s1.value_tuple().dimensions_value_size() <
1157                        s2.value_tuple().dimensions_value_size();
1158             }
1159             for (int i = 0; i < s1.value_tuple().dimensions_value_size(); ++i) {
1160                 if (EqualsTo(s1.value_tuple().dimensions_value(i),
1161                              s2.value_tuple().dimensions_value(i))) {
1162                     continue;
1163                 } else {
1164                     return LessThan(s1.value_tuple().dimensions_value(i),
1165                                     s2.value_tuple().dimensions_value(i));
1166                 }
1167             }
1168             return false;
1169         }
1170         case DimensionsValue::ValueCase::VALUE_NOT_SET:
1171         default:
1172             return false;
1173     }
1174 }
1175 
LessThan(const DimensionsPair & s1,const DimensionsPair & s2)1176 bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2) {
1177     if (LessThan(s1.dimInWhat, s2.dimInWhat)) {
1178         return true;
1179     } else if (LessThan(s2.dimInWhat, s1.dimInWhat)) {
1180         return false;
1181     }
1182 
1183     return LessThan(s1.stateValues, s2.stateValues);
1184 }
1185 
backfillStringInDimension(const std::map<uint64_t,string> & str_map,DimensionsValue * dimension)1186 void backfillStringInDimension(const std::map<uint64_t, string>& str_map,
1187                                DimensionsValue* dimension) {
1188     if (dimension->has_value_str_hash()) {
1189         auto it = str_map.find((uint64_t)(dimension->value_str_hash()));
1190         if (it != str_map.end()) {
1191             dimension->clear_value_str_hash();
1192             dimension->set_value_str(it->second);
1193         } else {
1194             ALOGE("Can not find the string hash: %llu",
1195                 (unsigned long long)dimension->value_str_hash());
1196         }
1197     } else if (dimension->has_value_tuple()) {
1198         auto value_tuple = dimension->mutable_value_tuple();
1199         for (int i = 0; i < value_tuple->dimensions_value_size(); ++i) {
1200             backfillStringInDimension(str_map, value_tuple->mutable_dimensions_value(i));
1201         }
1202     }
1203 }
1204 
backfillStringInReport(ConfigMetricsReport * config_report)1205 void backfillStringInReport(ConfigMetricsReport *config_report) {
1206     std::map<uint64_t, string> str_map;
1207     for (const auto& str : config_report->strings()) {
1208         uint64_t hash = Hash64(str);
1209         if (str_map.find(hash) != str_map.end()) {
1210             ALOGE("String hash conflicts: %s %s", str.c_str(), str_map[hash].c_str());
1211         }
1212         str_map[hash] = str;
1213     }
1214     for (int i = 0; i < config_report->metrics_size(); ++i) {
1215         auto metric_report = config_report->mutable_metrics(i);
1216         if (metric_report->has_count_metrics()) {
1217             backfillStringInDimension(str_map, metric_report->mutable_count_metrics());
1218         } else if (metric_report->has_duration_metrics()) {
1219             backfillStringInDimension(str_map, metric_report->mutable_duration_metrics());
1220         } else if (metric_report->has_gauge_metrics()) {
1221             backfillStringInDimension(str_map, metric_report->mutable_gauge_metrics());
1222         } else if (metric_report->has_value_metrics()) {
1223             backfillStringInDimension(str_map, metric_report->mutable_value_metrics());
1224         }
1225     }
1226     // Backfill the package names.
1227     for (int i = 0 ; i < config_report->uid_map().snapshots_size(); ++i) {
1228         auto snapshot = config_report->mutable_uid_map()->mutable_snapshots(i);
1229         for (int j = 0 ; j < snapshot->package_info_size(); ++j) {
1230             auto package_info = snapshot->mutable_package_info(j);
1231             if (package_info->has_name_hash()) {
1232                 auto it = str_map.find((uint64_t)(package_info->name_hash()));
1233                 if (it != str_map.end()) {
1234                     package_info->clear_name_hash();
1235                     package_info->set_name(it->second);
1236                 } else {
1237                     ALOGE("Can not find the string package name hash: %llu",
1238                         (unsigned long long)package_info->name_hash());
1239                 }
1240 
1241             }
1242         }
1243     }
1244     // Backfill the app name in app changes.
1245     for (int i = 0 ; i < config_report->uid_map().changes_size(); ++i) {
1246         auto change = config_report->mutable_uid_map()->mutable_changes(i);
1247         if (change->has_app_hash()) {
1248             auto it = str_map.find((uint64_t)(change->app_hash()));
1249             if (it != str_map.end()) {
1250                 change->clear_app_hash();
1251                 change->set_app(it->second);
1252             } else {
1253                 ALOGE("Can not find the string change app name hash: %llu",
1254                     (unsigned long long)change->app_hash());
1255             }
1256         }
1257     }
1258 }
1259 
backfillStringInReport(ConfigMetricsReportList * config_report_list)1260 void backfillStringInReport(ConfigMetricsReportList *config_report_list) {
1261     for (int i = 0; i < config_report_list->reports_size(); ++i) {
1262         backfillStringInReport(config_report_list->mutable_reports(i));
1263     }
1264 }
1265 
backfillDimensionPath(const DimensionsValue & path,const google::protobuf::RepeatedPtrField<DimensionsValue> & leafValues,int * leafIndex,DimensionsValue * dimension)1266 bool backfillDimensionPath(const DimensionsValue& path,
1267                            const google::protobuf::RepeatedPtrField<DimensionsValue>& leafValues,
1268                            int* leafIndex,
1269                            DimensionsValue* dimension) {
1270     dimension->set_field(path.field());
1271     if (path.has_value_tuple()) {
1272         for (int i = 0; i < path.value_tuple().dimensions_value_size(); ++i) {
1273             if (!backfillDimensionPath(
1274                 path.value_tuple().dimensions_value(i), leafValues, leafIndex,
1275                 dimension->mutable_value_tuple()->add_dimensions_value())) {
1276                 return false;
1277             }
1278         }
1279     } else {
1280         if (*leafIndex < 0 || *leafIndex >= leafValues.size()) {
1281             return false;
1282         }
1283         dimension->MergeFrom(leafValues.Get(*leafIndex));
1284         (*leafIndex)++;
1285     }
1286     return true;
1287 }
1288 
backfillDimensionPath(const DimensionsValue & path,const google::protobuf::RepeatedPtrField<DimensionsValue> & leafValues,DimensionsValue * dimension)1289 bool backfillDimensionPath(const DimensionsValue& path,
1290                            const google::protobuf::RepeatedPtrField<DimensionsValue>& leafValues,
1291                            DimensionsValue* dimension) {
1292     int leafIndex = 0;
1293     return backfillDimensionPath(path, leafValues, &leafIndex, dimension);
1294 }
1295 
backfillDimensionPath(ConfigMetricsReportList * config_report_list)1296 void backfillDimensionPath(ConfigMetricsReportList *config_report_list) {
1297     for (int i = 0; i < config_report_list->reports_size(); ++i) {
1298         auto report = config_report_list->mutable_reports(i);
1299         for (int j = 0; j < report->metrics_size(); ++j) {
1300             auto metric_report = report->mutable_metrics(j);
1301             if (metric_report->has_dimensions_path_in_what() ||
1302                 metric_report->has_dimensions_path_in_condition()) {
1303                 auto whatPath = metric_report->dimensions_path_in_what();
1304                 auto conditionPath = metric_report->dimensions_path_in_condition();
1305                 if (metric_report->has_count_metrics()) {
1306                     backfillDimensionPath(whatPath, conditionPath,
1307                                           metric_report->mutable_count_metrics());
1308                 } else if (metric_report->has_duration_metrics()) {
1309                     backfillDimensionPath(whatPath, conditionPath,
1310                                           metric_report->mutable_duration_metrics());
1311                 } else if (metric_report->has_gauge_metrics()) {
1312                     backfillDimensionPath(whatPath, conditionPath,
1313                                           metric_report->mutable_gauge_metrics());
1314                 } else if (metric_report->has_value_metrics()) {
1315                     backfillDimensionPath(whatPath, conditionPath,
1316                                           metric_report->mutable_value_metrics());
1317                 }
1318                 metric_report->clear_dimensions_path_in_what();
1319                 metric_report->clear_dimensions_path_in_condition();
1320             }
1321         }
1322     }
1323 }
1324 
backfillStartEndTimestamp(StatsLogReport * report)1325 void backfillStartEndTimestamp(StatsLogReport *report) {
1326     const int64_t timeBaseNs = report->time_base_elapsed_nano_seconds();
1327     const int64_t bucketSizeNs = report->bucket_size_nano_seconds();
1328     if (report->has_count_metrics()) {
1329         backfillStartEndTimestampForMetrics(
1330             timeBaseNs, bucketSizeNs, report->mutable_count_metrics());
1331     } else if (report->has_duration_metrics()) {
1332         backfillStartEndTimestampForMetrics(
1333             timeBaseNs, bucketSizeNs, report->mutable_duration_metrics());
1334     } else if (report->has_gauge_metrics()) {
1335         backfillStartEndTimestampForMetrics(
1336             timeBaseNs, bucketSizeNs, report->mutable_gauge_metrics());
1337         if (report->gauge_metrics().skipped_size() > 0) {
1338             backfillStartEndTimestampForSkippedBuckets(
1339                 timeBaseNs, report->mutable_gauge_metrics());
1340         }
1341     } else if (report->has_value_metrics()) {
1342         backfillStartEndTimestampForMetrics(
1343             timeBaseNs, bucketSizeNs, report->mutable_value_metrics());
1344         if (report->value_metrics().skipped_size() > 0) {
1345             backfillStartEndTimestampForSkippedBuckets(
1346                 timeBaseNs, report->mutable_value_metrics());
1347         }
1348     }
1349 }
1350 
backfillStartEndTimestamp(ConfigMetricsReport * config_report)1351 void backfillStartEndTimestamp(ConfigMetricsReport *config_report) {
1352     for (int j = 0; j < config_report->metrics_size(); ++j) {
1353         backfillStartEndTimestamp(config_report->mutable_metrics(j));
1354     }
1355 }
1356 
backfillStartEndTimestamp(ConfigMetricsReportList * config_report_list)1357 void backfillStartEndTimestamp(ConfigMetricsReportList *config_report_list) {
1358     for (int i = 0; i < config_report_list->reports_size(); ++i) {
1359         backfillStartEndTimestamp(config_report_list->mutable_reports(i));
1360     }
1361 }
1362 
onPullAtom(int atomTag,const shared_ptr<IPullAtomResultReceiver> & resultReceiver)1363 Status FakeSubsystemSleepCallback::onPullAtom(int atomTag,
1364         const shared_ptr<IPullAtomResultReceiver>& resultReceiver) {
1365     // Convert stats_events into StatsEventParcels.
1366     std::vector<StatsEventParcel> parcels;
1367     for (int i = 1; i < 3; i++) {
1368         AStatsEvent* event = AStatsEvent_obtain();
1369         AStatsEvent_setAtomId(event, atomTag);
1370         std::string subsystemName = "subsystem_name_";
1371         subsystemName = subsystemName + std::to_string(i);
1372         AStatsEvent_writeString(event, subsystemName.c_str());
1373         AStatsEvent_writeString(event, "subsystem_subname foo");
1374         AStatsEvent_writeInt64(event, /*count= */ i);
1375         AStatsEvent_writeInt64(event, /*time_millis= */ i * 100);
1376         AStatsEvent_build(event);
1377         size_t size;
1378         uint8_t* buffer = AStatsEvent_getBuffer(event, &size);
1379 
1380         StatsEventParcel p;
1381         // vector.assign() creates a copy, but this is inevitable unless
1382         // stats_event.h/c uses a vector as opposed to a buffer.
1383         p.buffer.assign(buffer, buffer + size);
1384         parcels.push_back(std::move(p));
1385         AStatsEvent_release(event);
1386     }
1387     resultReceiver->pullFinished(atomTag, /*success=*/true, parcels);
1388     return Status::ok();
1389 }
1390 
1391 }  // namespace statsd
1392 }  // namespace os
1393 }  // namespace android
1394